diff options
author | Rob Taylor <rob.taylor@codethink.co.uk> | 2007-09-05 13:03:07 +0100 |
---|---|---|
committer | Rob Taylor <rob.taylor@codethink.co.uk> | 2007-09-05 13:03:07 +0100 |
commit | dda8bc9397a3f81e265a68b184cd4293b3734b6d (patch) | |
tree | e9d6c94d9c85fe8c0fa7bbd1f3c1a60465150fe3 | |
parent | 437a86a4b18a2be033225432fd099bcb7f0c3114 (diff) |
add idle.reset
Adds a reset key to the idle plugin. This allows other plugins to reset the state back to 0 and restart the timer. The idle plugin finds the current time idle and uses this as an offset until the xserver signals activity.
-rw-r--r-- | libidletime/libidletime.c | 35 | ||||
-rw-r--r-- | libidletime/libidletime.h | 4 | ||||
-rw-r--r-- | plugins/glue/idle/ohm-plugin-idle.c | 30 |
3 files changed, 57 insertions, 12 deletions
diff --git a/libidletime/libidletime.c b/libidletime/libidletime.c index 0d75b50..feecef1 100644 --- a/libidletime/libidletime.c +++ b/libidletime/libidletime.c @@ -41,6 +41,21 @@ static void idletime_finalize (GObject *object); #define LIBIDLETIME_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), LIBIDLETIME_TYPE, LibIdletimePrivate)) +inline gint64 +xsyncvalue_to_int64 (XSyncValue *value) +{ + return ((guint64) XSyncValueHigh32 (*value)) << 32 + | (guint64) XSyncValueLow32 (*value); +} + +inline XSyncValue +int64_to_xsyncvalue (gint64 value) +{ + XSyncValue ret; + XSyncIntsToValue (&ret, value, ((guint64)value) >> 32); + return ret; +} + struct LibIdletimePrivate { int sync_event; @@ -268,7 +283,7 @@ idletime_alarm_get (LibIdletime *idletime) * idletime_alarm_set: */ gboolean -idletime_alarm_set (LibIdletime *idletime, guint id, guint timeout) +idletime_alarm_set (LibIdletime *idletime, guint id, gint64 timeout) { LibIdletimeAlarm *alarm; if (id == 0) { @@ -294,7 +309,7 @@ idletime_alarm_set (LibIdletime *idletime, guint id, guint timeout) } /* set the timeout */ - XSyncIntToValue (&alarm->timeout, timeout); + alarm->timeout = int64_to_xsyncvalue(timeout); /* set, and start the timer */ idletime_xsync_alarm_set (idletime, alarm, TRUE); @@ -332,6 +347,22 @@ idletime_alarm_remove (LibIdletime *idletime, guint id) } /** + * idletime_get_current_idle: + * @idletime: An #LibIdletime instantiation + * + * Returns the current number of milliseconds idle + */ +gint64 +idletime_get_current_idle (LibIdletime *idletime) +{ + XSyncValue value; + if (XSyncQueryCounter (idletime->priv->dpy, idletime->priv->idle_counter, &value)) + return xsyncvalue_to_int64 (&value); + else + return 0LL; +} + +/** * idletime_class_init: * @klass: This class instance **/ diff --git a/libidletime/libidletime.h b/libidletime/libidletime.h index cfd7a81..dfaee7e 100644 --- a/libidletime/libidletime.h +++ b/libidletime/libidletime.h @@ -55,10 +55,12 @@ void idletime_alarm_reset_all (LibIdletime *idletime); guint idletime_alarm_get (LibIdletime *idletime); gboolean idletime_alarm_set (LibIdletime *idletime, guint id, - guint timeout); + gint64 timeout); gboolean idletime_alarm_remove (LibIdletime *idletime, guint id); +gint64 idletime_get_current_idle (LibIdletime *idletime); + G_END_DECLS #endif /* __LIBIDLETIME_H */ diff --git a/plugins/glue/idle/ohm-plugin-idle.c b/plugins/glue/idle/ohm-plugin-idle.c index 5c6bff4..3b55202 100644 --- a/plugins/glue/idle/ohm-plugin-idle.c +++ b/plugins/glue/idle/ohm-plugin-idle.c @@ -26,11 +26,12 @@ #include <stdlib.h> static LibIdletime *idletime = NULL; +static gint64 timeout_offset = 0; enum { CONF_XORG_HASXAUTH_CHANGED, CONF_IDLE_TIMEOUT_CHANGED, - CONF_IDLE_STATE_CHANGED, + CONF_IDLE_RESET, CONF_LAST }; @@ -43,6 +44,7 @@ ohm_alarm_expired_cb (LibIdletime *idletime, guint alarm, gpointer data) if (alarm == 0 ) { /* activity, reset state to 0 */ ohm_plugin_conf_set_key (plugin, "idle.state", 0); + timeout_offset = 0; } else { ohm_plugin_conf_get_key (plugin, "idle.state", &state); ohm_plugin_conf_set_key (plugin, "idle.state", state+1); @@ -68,6 +70,7 @@ plugin_connect_idletime (OhmPlugin *plugin) G_CALLBACK (ohm_alarm_expired_cb), plugin); ohm_plugin_conf_set_key (plugin, "idle.state", 0); + ohm_plugin_conf_set_key (plugin, "idle.reset", 0); ohm_plugin_conf_get_key (plugin, "idle.timeout", &timeout); @@ -111,6 +114,7 @@ plugin_initalize (OhmPlugin *plugin) static void plugin_notify (OhmPlugin *plugin, gint id, gint value) { + g_debug ("idle got notify, id = %d, value = %d", id, value); gint timeout; if (id == CONF_XORG_HASXAUTH_CHANGED) { if (value == 1) { @@ -120,12 +124,20 @@ plugin_notify (OhmPlugin *plugin, gint id, gint value) if (idletime) { if (id == CONF_IDLE_TIMEOUT_CHANGED ) { g_debug("setting new timeout %d", value); - idletime_alarm_set (idletime, 1, value); - } else if (id == CONF_IDLE_STATE_CHANGED ) { - if (value == 0) { - ohm_plugin_conf_get_key (plugin, "idle.timeout", &timeout); - idletime_alarm_set (idletime, 1, timeout); - } + idletime_alarm_set (idletime, 1, timeout_offset+value); + } else if (id == CONF_IDLE_RESET && value == 1) { + timeout_offset = idletime_get_current_idle (idletime); + ohm_plugin_conf_get_key (plugin, "idle.timeout", &timeout); + g_debug ("idle plugin reset. current timeout = %d, timeout-offset = %lld", timeout, timeout_offset); + + /* most of the time this isn't needed as the below set + * of idle.state will result in a timout change, + * but just incase it doesn't happen. or the + * new timeout is the same as the old.. + */ + idletime_alarm_set (idletime, 1, timeout_offset+timeout); + ohm_plugin_conf_set_key (plugin, "idle.reset", 0); + ohm_plugin_conf_set_key (plugin, "idle.state", 0); } } } @@ -148,9 +160,9 @@ OHM_PLUGIN_DESCRIPTION ( OHM_PLUGIN_REQUIRES ("xorg"); -OHM_PLUGIN_PROVIDES ("idle.state", "idle.timeout"); +OHM_PLUGIN_PROVIDES ("idle.state", "idle.timeout", "idle.reset"); OHM_PLUGIN_INTERESTED ( {"xorg.has_xauthority", CONF_XORG_HASXAUTH_CHANGED}, {"idle.timeout", CONF_IDLE_TIMEOUT_CHANGED}, - {"idle.state", CONF_IDLE_STATE_CHANGED}); + {"idle.reset", CONF_IDLE_RESET}); |