gnome-power-manager r3093 - in branches/gnome-2-24: . src



Author: rhughes
Date: Mon Nov 17 10:17:34 2008
New Revision: 3093
URL: http://svn.gnome.org/viewvc/gnome-power-manager?rev=3093&view=rev

Log:
2008-11-17  Richard Hughes  <richard hughsie com>

* src/gpm-button.c: (gpm_button_emit_type),
(gpm_button_filter_x_events), (gpm_button_xevent_key),
(gpm_button_class_init), (emit_button_pressed),
(hal_device_property_modified_cb), (hal_device_condition_cb),
(gpm_button_init), (gpm_button_finalize):
Backport the duplicate button handling from trunk.
This should fix where the HAL button comes before the X button.


Modified:
   branches/gnome-2-24/ChangeLog
   branches/gnome-2-24/src/gpm-button.c

Modified: branches/gnome-2-24/src/gpm-button.c
==============================================================================
--- branches/gnome-2-24/src/gpm-button.c	(original)
+++ branches/gnome-2-24/src/gpm-button.c	Mon Nov 17 10:17:34 2008
@@ -49,7 +49,9 @@
 {
 	GdkScreen		*screen;
 	GdkWindow		*window;
-	GHashTable		*hash_to_hal;
+	GHashTable		*keysym_to_name_hash;
+	gchar			*last_button;
+	GTimer			*timer;
 	gboolean		 lid_is_closed;
 	HalGManager		*hal_manager; /* remove when input events is in the kernel */
 	HalGDevicestore		*hal_devicestore;
@@ -65,26 +67,55 @@
 
 G_DEFINE_TYPE (GpmButton, gpm_button, G_TYPE_OBJECT)
 
+#define GPM_BUTTON_DUPLICATE_TIMEOUT	0.25f
+
+/**
+ * gpm_button_emit_type:
+ **/
+gboolean
+gpm_button_emit_type (GpmButton *button, const gchar *type)
+{
+	g_return_val_if_fail (button != NULL, FALSE);
+	g_return_val_if_fail (GPM_IS_BUTTON (button), FALSE);
+
+	/* did we just have this button before the timeout? */
+	if (g_strcmp0 (type, button->priv->last_button) == 0 &&
+	    g_timer_elapsed (button->priv->timer, NULL) < GPM_BUTTON_DUPLICATE_TIMEOUT) {
+		egg_debug ("ignoring duplicate button %s", type);
+		return FALSE;
+	}
+
+	egg_debug ("emitting button-pressed : %s", type);
+	g_signal_emit (button, signals [BUTTON_PRESSED], 0, type);
+
+	/* save type and last size */
+	g_free (button->priv->last_button);
+	button->priv->last_button = g_strdup (type);
+	g_timer_reset (button->priv->timer);
+
+	return TRUE;
+}
+
+/**
+ * gpm_button_filter_x_events:
+ **/
 static GdkFilterReturn
-gpm_button_filter_x_events (GdkXEvent *xevent,
-			    GdkEvent  *event,
-			    gpointer   data)
+gpm_button_filter_x_events (GdkXEvent *xevent, GdkEvent *event, gpointer data)
 {
 	GpmButton *button = (GpmButton *) data;
 	XEvent *xev = (XEvent *) xevent;
 	guint keycode;
-	gchar *key;
+	const gchar *key;
 	gchar *keycode_str;
 
-	if (xev->type != KeyPress) {
+	if (xev->type != KeyPress)
 		return GDK_FILTER_CONTINUE;
-	}
 
 	keycode = xev->xkey.keycode;
 
 	/* is the key string already in our DB? */
 	keycode_str = g_strdup_printf ("0x%x", keycode);
-	key = g_hash_table_lookup (button->priv->hash_to_hal, (gpointer) keycode_str);
+	key = g_hash_table_lookup (button->priv->keysym_to_name_hash, (gpointer) keycode_str);
 	g_free (keycode_str);
 
 	/* found anything? */
@@ -94,8 +125,8 @@
 		return GDK_FILTER_CONTINUE;
 	}
 
-	egg_debug ("Key %i mapped to HAL key %s", keycode, key);
-	g_signal_emit (button, signals [BUTTON_PRESSED], 0, key);
+	egg_debug ("Key %i mapped to key %s", keycode, key);
+	gpm_button_emit_type (button, key);
 
 	return GDK_FILTER_REMOVE;
 }
@@ -182,7 +213,7 @@
 
 	/* is the key string already in our DB? */
 	keycode_str = g_strdup_printf ("0x%x", keycode);
-	key = g_hash_table_lookup (button->priv->hash_to_hal, (gpointer) keycode_str);
+	key = g_hash_table_lookup (button->priv->keysym_to_name_hash, (gpointer) keycode_str);
 	if (key != NULL) {
 		egg_warning ("found in hash %i", keycode);
 		g_free (keycode_str);
@@ -198,7 +229,7 @@
 	}
 
 	/* add to hash table */
-	g_hash_table_insert (button->priv->hash_to_hal, (gpointer) keycode_str, (gpointer) g_strdup (hal_key));
+	g_hash_table_insert (button->priv->keysym_to_name_hash, (gpointer) keycode_str, (gpointer) g_strdup (hal_key));
 
 	/* the key is freed in the hash function unref */
 	return TRUE;
@@ -220,8 +251,7 @@
 			      G_TYPE_FROM_CLASS (object_class),
 			      G_SIGNAL_RUN_LAST,
 			      G_STRUCT_OFFSET (GpmButtonClass, button_pressed),
-			      NULL,
-			      NULL,
+			      NULL, NULL,
 			      gpm_marshal_VOID__STRING,
 			      G_TYPE_NONE, 1, G_TYPE_STRING);
 }
@@ -254,25 +284,22 @@
  *      button.type=""
  */
 static void
-emit_button_pressed (GpmButton   *button,
-		     HalGDevice  *device,
-		     const gchar *details)
+emit_button_pressed (GpmButton *button, HalGDevice *device, const gchar *details)
 {
 	gchar *type = NULL;
 	gboolean state;
-	const char *atype;
+	const gchar *atype;
 
 	g_return_if_fail (device != NULL);
 	g_return_if_fail (details != NULL);
 
 	if (strcmp (details, "") == 0) {
-		/* no details about the event, so we get more info
-		   for type 1 buttons */
+		/* no details about the event, so we get more info for type 1 buttons */
 		hal_gdevice_get_string (device, "button.type", &type, NULL);
 		/* hal may no longer be there */
 		if (type == NULL) {
 			egg_warning ("cannot get button type for %s", hal_gdevice_get_udi (device));
-			return;
+			goto out;
 		}
 	} else {
 		type = g_strdup (details);
@@ -282,16 +309,14 @@
 	/* Buttons without state should default to true. */
 	state = TRUE;
 	/* we need to get the button state for lid buttons */
-	if (strcmp (type, "lid") == 0) {
+	if (strcmp (type, "lid") == 0)
 		hal_gdevice_get_bool (device, "button.state.value", &state, NULL);
-	}
 
 	/* abstact away that HAL has an extra parameter */
-	if (strcmp (type, GPM_BUTTON_LID_DEP) == 0 && state == FALSE) {
+	if (strcmp (type, GPM_BUTTON_LID_DEP) == 0 && state == FALSE)
 		atype = GPM_BUTTON_LID_OPEN;
-	} else if (strcmp (type, GPM_BUTTON_LID_DEP) == 0 && state) {
+	else if (strcmp (type, GPM_BUTTON_LID_DEP) == 0 && state)
 		atype = GPM_BUTTON_LID_CLOSED;
-	}
 
 	/* filter out duplicate lid events */
 	if (strcmp (atype, GPM_BUTTON_LID_CLOSED) == 0) {
@@ -309,17 +334,9 @@
 		button->priv->lid_is_closed = FALSE;
 	}
 
-	/* the names changed in 0.5.8 */
-	if (strcmp (type, GPM_BUTTON_BRIGHT_UP_DEP) == 0) {
-		atype = GPM_BUTTON_BRIGHT_UP;
-	} else if (strcmp (type, GPM_BUTTON_BRIGHT_DOWN_DEP) == 0) {
-		atype = GPM_BUTTON_BRIGHT_DOWN;
-	}
-
-	/* we now emit all buttons, even the ones we don't know */
-	egg_debug ("emitting button-pressed : %s", atype);
-	g_signal_emit (button, signals [BUTTON_PRESSED], 0, atype);
-
+	/* we emit all buttons, even the ones we don't know */
+	gpm_button_emit_type (button, atype);
+out:
 	g_free (type);
 }
 
@@ -346,9 +363,8 @@
 		   key, is_added, is_removed, finally);
 
 	/* do not process keys that have been removed */
-	if (is_removed) {
+	if (is_removed)
 		return;
-	}
 
 	/* only match button* values */
 	if (strncmp (key, "button", 6) == 0) {
@@ -375,9 +391,8 @@
 {
 	egg_debug ("condition=%s, details=%s", condition, details);
 
-	if (strcmp (condition, "ButtonPressed") == 0) {
+	if (strcmp (condition, "ButtonPressed") == 0)
 		emit_button_pressed (button, device, details);
-	}
 }
 
 /**
@@ -479,7 +494,9 @@
 	button->priv->screen = gdk_screen_get_default ();
 	button->priv->window = gdk_screen_get_root_window (button->priv->screen);
 
-	button->priv->hash_to_hal = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+	button->priv->keysym_to_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+	button->priv->last_button = NULL;
+	button->priv->timer = g_timer_new ();
 
 	button->priv->lid_is_closed = FALSE;
 
@@ -489,7 +506,6 @@
 
 	if (have_xevents) {
 		/* register the brightness keys */
-//		gpm_button_xevent_key (button, XF86XK_Execute, GPM_BUTTON_POWER);
 		gpm_button_xevent_key (button, XF86XK_PowerOff, GPM_BUTTON_POWER);
 //		gpm_button_xevent_key (button, XF86XK_Suspend, GPM_BUTTON_SUSPEND);
 		gpm_button_xevent_key (button, XF86XK_Sleep, GPM_BUTTON_SUSPEND); /* should be configurable */
@@ -535,8 +551,10 @@
 
 	g_object_unref (button->priv->hal_manager);
 	g_object_unref (button->priv->hal_devicestore);
+	g_free (button->priv->last_button);
+	g_timer_destroy (button->priv->timer);
 
-	g_hash_table_unref (button->priv->hash_to_hal);
+	g_hash_table_unref (button->priv->keysym_to_name_hash);
 
 	G_OBJECT_CLASS (gpm_button_parent_class)->finalize (object);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]