[gpm] [PATCH 2/2] Unify osd popup dialog into separate file to avoid code duplication



From: Alex Murray <murray alex gmail com>


Signed-off-by: Alex Murray <murray alex gmail com>
---
 src/Makefile.am         |    2 +
 src/gpm-backlight.c     |  111 +++------------------------------------------
 src/gpm-kbd-backlight.c |  103 ++----------------------------------------
 src/gpm-osd-dialog.c    |  115 +++++++++++++++++++++++++++++++++++++++++++++++
 src/gpm-osd-dialog.h    |   31 +++++++++++++
 5 files changed, 160 insertions(+), 202 deletions(-)
 create mode 100644 src/gpm-osd-dialog.c
 create mode 100644 src/gpm-osd-dialog.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 009e7a8..73d184b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -91,6 +91,8 @@ gnome_power_manager_SOURCES =				\
 	gpm-dpms.c					\
 	gpm-phone.h					\
 	gpm-phone.c					\
+	gpm-osd-dialog.h				\
+	gpm-osd-dialog.c				\
 	gpm-backlight.h					\
 	gpm-backlight.c					\
 	gpm-idle.h					\
diff --git a/src/gpm-backlight.c b/src/gpm-backlight.c
index 49ae9e1..e6e5494 100644
--- a/src/gpm-backlight.c
+++ b/src/gpm-backlight.c
@@ -45,7 +45,7 @@
 #include "gpm-brightness.h"
 #include "gpm-control.h"
 #include "gpm-common.h"
-#include "gsd-media-keys-window.h"
+#include "gpm-osd-dialog.h"
 #include "gpm-dpms.h"
 #include "gpm-idle.h"
 #include "gpm-marshal.h"
@@ -184,97 +184,6 @@ gpm_backlight_set_brightness (GpmBacklight *backlight, guint percentage, GError
 }
 
 /**
- * gpm_backlight_dialog_init:
- *
- * Initialises the popup, and makes sure that it matches the compositing of the screen.
- **/
-static void
-gpm_backlight_dialog_init (GpmBacklight *backlight)
-{
-	if (backlight->priv->popup != NULL
-	    && !gsd_osd_window_is_valid (GSD_OSD_WINDOW (backlight->priv->popup))) {
-		gtk_widget_destroy (backlight->priv->popup);
-		backlight->priv->popup = NULL;
-	}
-
-	if (backlight->priv->popup == NULL) {
-		backlight->priv->popup= gsd_media_keys_window_new ();
-		gsd_media_keys_window_set_action_custom (GSD_MEDIA_KEYS_WINDOW (backlight->priv->popup),
-							 "gpm-brightness-lcd",
-							 TRUE);
-		gtk_window_set_position (GTK_WINDOW (backlight->priv->popup), GTK_WIN_POS_NONE);
-	}
-}
-
-/**
- * gpm_backlight_dialog_show:
- *
- * Show the brightness popup, and place it nicely on the screen.
- **/
-static void
-gpm_backlight_dialog_show (GpmBacklight *backlight)
-{
-	int            orig_w;
-	int            orig_h;
-	int            screen_w;
-	int            screen_h;
-	int            x;
-	int            y;
-	int            pointer_x;
-	int            pointer_y;
-	GtkRequisition win_req;
-	GdkScreen     *pointer_screen;
-	GdkRectangle   geometry;
-	int            monitor;
-        GdkDisplay    *display;
-        GdkDeviceManager *device_manager;
-        GdkDevice     *device;
-
-	/*
-	 * get the window size
-	 * if the window hasn't been mapped, it doesn't necessarily
-	 * know its true size, yet, so we need to jump through hoops
-	 */
-	gtk_window_get_default_size (GTK_WINDOW (backlight->priv->popup), &orig_w, &orig_h);
-	gtk_widget_get_preferred_size(backlight->priv->popup, &win_req, NULL);
-
-	if (win_req.width > orig_w) {
-		orig_w = win_req.width;
-	}
-	if (win_req.height > orig_h) {
-		orig_h = win_req.height;
-	}
-
-	pointer_screen = NULL;
-        display = gtk_widget_get_display (backlight->priv->popup);
-        device_manager = gdk_display_get_device_manager (display);
-        device = gdk_device_manager_get_client_pointer (device_manager);
-        gdk_device_get_position (device,
-				 &pointer_screen,
-				 &pointer_x,
-				 &pointer_y);
-	monitor = gdk_screen_get_monitor_at_point (pointer_screen,
-						   pointer_x,
-						   pointer_y);
-
-	gdk_screen_get_monitor_geometry (pointer_screen,
-					 monitor,
-					 &geometry);
-
-	screen_w = geometry.width;
-	screen_h = geometry.height;
-
-	x = ((screen_w - orig_w) / 2) + geometry.x;
-	y = geometry.y + (screen_h / 2) + (screen_h / 2 - orig_h) / 2;
-
-	gtk_window_move (GTK_WINDOW (backlight->priv->popup), x, y);
-
-	gtk_widget_show (backlight->priv->popup);
-
-	gdk_display_sync (gtk_widget_get_display (backlight->priv->popup));
-}
-
-/**
  * gpm_common_sum_scale:
  *
  * Finds the average between value1 and value2 set on a scale factor
@@ -369,10 +278,10 @@ gpm_backlight_brightness_evaluate_and_set (GpmBacklight *backlight, gboolean int
 
 	/* only show dialog if interactive */
 	if (interactive) {
-		gpm_backlight_dialog_init (backlight);
+		gpm_osd_dialog_init (&backlight->priv->popup, "gpm-brightness-lcd");
 		gsd_media_keys_window_set_volume_level (GSD_MEDIA_KEYS_WINDOW (backlight->priv->popup),
 							round (brightness));
-		gpm_backlight_dialog_show (backlight);
+		gpm_osd_dialog_show (backlight->priv->popup);
 	}
 
 	ret = gpm_brightness_set (backlight->priv->brightness, value, &hw_changed);
@@ -457,10 +366,10 @@ gpm_backlight_button_pressed_cb (GpmButton *button, const gchar *type, GpmBackli
 		/* show the new value */
 		if (ret) {
 			gpm_brightness_get (backlight->priv->brightness, &percentage);
-			gpm_backlight_dialog_init (backlight);
+			gpm_osd_dialog_init (&backlight->priv->popup, "gpm-brightness-lcd");
 			gsd_media_keys_window_set_volume_level (GSD_MEDIA_KEYS_WINDOW (backlight->priv->popup),
 								percentage);
-			gpm_backlight_dialog_show (backlight);
+			gpm_osd_dialog_show (backlight->priv->popup);
 			/* save the new percentage */
 			backlight->priv->master_percentage = percentage;
 		}
@@ -476,10 +385,10 @@ gpm_backlight_button_pressed_cb (GpmButton *button, const gchar *type, GpmBackli
 		/* show the new value */
 		if (ret) {
 			gpm_brightness_get (backlight->priv->brightness, &percentage);
-			gpm_backlight_dialog_init (backlight);
+			gpm_osd_dialog_init (&backlight->priv->popup, "gpm-brightness-lcd");
 			gsd_media_keys_window_set_volume_level (GSD_MEDIA_KEYS_WINDOW (backlight->priv->popup),
 								percentage);
-			gpm_backlight_dialog_show (backlight);
+			gpm_osd_dialog_show (backlight->priv->popup);
 			/* save the new percentage */
 			backlight->priv->master_percentage = percentage;
 		}
@@ -876,11 +785,7 @@ gpm_backlight_init (GpmBacklight *backlight)
 	gpm_idle_set_timeout_dim (backlight->priv->idle, backlight->priv->idle_dim_timeout);
 
 	/* use a visual widget */
-	backlight->priv->popup = gsd_media_keys_window_new ();
-	gsd_media_keys_window_set_action_custom (GSD_MEDIA_KEYS_WINDOW (backlight->priv->popup),
-						 "gpm-brightness-lcd",
-						 TRUE);
-	gtk_window_set_position (GTK_WINDOW (backlight->priv->popup), GTK_WIN_POS_NONE);
+	gpm_osd_dialog_init (&backlight->priv->popup, "gpm-brightness-lcd");
 
 	/* DPMS mode poll class */
 	backlight->priv->dpms = gpm_dpms_new ();
diff --git a/src/gpm-kbd-backlight.c b/src/gpm-kbd-backlight.c
index 6478172..3e06f35 100644
--- a/src/gpm-kbd-backlight.c
+++ b/src/gpm-kbd-backlight.c
@@ -25,7 +25,7 @@
 
 #include "gpm-button.h"
 #include "gpm-common.h"
-#include "gsd-media-keys-window.h"
+#include "gpm-osd-dialog.h"
 #include "gpm-control.h"
 #include "gpm-idle.h"
 #include "gpm-kbd-backlight.h"
@@ -378,97 +378,6 @@ gpm_kbd_backlight_register_dbus (GpmKbdBacklight *backlight,
 	g_dbus_node_info_unref (node_info);
 }
 
-/**
- * gpm_kbd_backlight_dialog_init:
- *
- * Initialises the popup, and makes sure that it matches the compositing of the screen.
- **/
-static void
-gpm_kbd_backlight_dialog_init (GpmKbdBacklight *backlight)
-{
-	if (backlight->priv->popup != NULL
-	    && !gsd_osd_window_is_valid (GSD_OSD_WINDOW (backlight->priv->popup))) {
-		gtk_widget_destroy (backlight->priv->popup);
-		backlight->priv->popup = NULL;
-	}
-
-	if (backlight->priv->popup == NULL) {
-		backlight->priv->popup= gsd_media_keys_window_new ();
-		gsd_media_keys_window_set_action_custom (GSD_MEDIA_KEYS_WINDOW (backlight->priv->popup),
-							 "gpm-brightness-kbd",
-							 TRUE);
-		gtk_window_set_position (GTK_WINDOW (backlight->priv->popup), GTK_WIN_POS_NONE);
-	}
-}
-
-/**
- * gpm_kbd_backlight_dialog_show:
- *
- * Show the brightness popup, and place it nicely on the screen.
- **/
-static void
-gpm_kbd_backlight_dialog_show (GpmKbdBacklight *backlight)
-{
-	int            orig_w;
-	int            orig_h;
-	int            screen_w;
-	int            screen_h;
-	int            x;
-	int            y;
-	int            pointer_x;
-	int            pointer_y;
-	GtkRequisition win_req;
-	GdkScreen     *pointer_screen;
-	GdkRectangle   geometry;
-	int            monitor;
-        GdkDisplay    *display;
-        GdkDeviceManager *device_manager;
-        GdkDevice     *device;
-
-	/*
-	 * get the window size
-	 * if the window hasn't been mapped, it doesn't necessarily
-	 * know its true size, yet, so we need to jump through hoops
-	 */
-	gtk_window_get_default_size (GTK_WINDOW (backlight->priv->popup), &orig_w, &orig_h);
-	gtk_widget_get_preferred_size(backlight->priv->popup, &win_req, NULL);
-
-	if (win_req.width > orig_w) {
-		orig_w = win_req.width;
-	}
-	if (win_req.height > orig_h) {
-		orig_h = win_req.height;
-	}
-
-	pointer_screen = NULL;
-        display = gtk_widget_get_display (backlight->priv->popup);
-        device_manager = gdk_display_get_device_manager (display);
-        device = gdk_device_manager_get_client_pointer (device_manager);
-        gdk_device_get_position (device,
-				 &pointer_screen,
-				 &pointer_x,
-				 &pointer_y);
-	monitor = gdk_screen_get_monitor_at_point (pointer_screen,
-						   pointer_x,
-						   pointer_y);
-
-	gdk_screen_get_monitor_geometry (pointer_screen,
-					 monitor,
-					 &geometry);
-
-	screen_w = geometry.width;
-	screen_h = geometry.height;
-
-	x = ((screen_w - orig_w) / 2) + geometry.x;
-	y = geometry.y + (screen_h / 2) + (screen_h / 2 - orig_h) / 2;
-
-	gtk_window_move (GTK_WINDOW (backlight->priv->popup), x, y);
-
-	gtk_widget_show (backlight->priv->popup);
-
-	gdk_display_sync (gtk_widget_get_display (backlight->priv->popup));
-}
-
 static gboolean
 gpm_kbd_backlight_evaluate_power_source_and_set (GpmKbdBacklight *backlight)
 {
@@ -575,10 +484,10 @@ gpm_kbd_backlight_button_pressed_cb (GpmButton *button,
 		}
 	}
 	if (ret) {
-		gpm_kbd_backlight_dialog_init (backlight);
+		gpm_osd_dialog_init (&backlight->priv->popup, "gpm-brightness-kbd");
 		gsd_media_keys_window_set_volume_level (GSD_MEDIA_KEYS_WINDOW (backlight->priv->popup),
 							backlight->priv->brightness_percent);
-		gpm_kbd_backlight_dialog_show (backlight);
+		gpm_osd_dialog_show (backlight->priv->popup);
 	}
 }
 
@@ -813,12 +722,8 @@ noerr:
 	gpm_idle_set_timeout_dim (backlight->priv->idle, backlight->priv->idle_dim_timeout);
 
 	/* use a visual widget */
-	backlight->priv->popup = gsd_media_keys_window_new ();
-	gsd_media_keys_window_set_action_custom (GSD_MEDIA_KEYS_WINDOW (backlight->priv->popup),
-						 "gpm-brightness-kbd",
-						 TRUE);
+	gpm_osd_dialog_init(&backlight->priv->popup, "gpm-brightness-kbd");
 
-	gtk_window_set_position (GTK_WINDOW (backlight->priv->popup), GTK_WIN_POS_NONE);
 	/* make sure we turn the keyboard backlight back on after resuming */
 	backlight->priv->control = gpm_control_new ();
 	g_signal_connect (backlight->priv->control, "resume",
diff --git a/src/gpm-osd-dialog.c b/src/gpm-osd-dialog.c
new file mode 100644
index 0000000..927efb4
--- /dev/null
+++ b/src/gpm-osd-dialog.c
@@ -0,0 +1,115 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2011 Alex Murray <murray alex gmail com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "gpm-osd-dialog.h"
+
+/**
+ * gpm_osd_dialog_init:
+ *
+ * Initialises a popup dialog, and makes sure that it matches the compositing of the screen.
+ **/
+void
+gpm_osd_dialog_init (GtkWidget **popup,
+		     const gchar *icon_name)
+{
+	if (*popup != NULL
+	    && !gsd_osd_window_is_valid (GSD_OSD_WINDOW (*popup))) {
+		gtk_widget_destroy (*popup);
+		*popup = NULL;
+	}
+
+	if (*popup == NULL) {
+		*popup= gsd_media_keys_window_new ();
+		gsd_media_keys_window_set_action_custom (GSD_MEDIA_KEYS_WINDOW (*popup),
+							 icon_name,
+							 TRUE);
+		gtk_window_set_position (GTK_WINDOW (*popup), GTK_WIN_POS_NONE);
+	}
+}
+
+/**
+ * gpm_osd_dialog_show:
+ *
+ * Show the brightness popup, and place it nicely on the screen.
+ **/
+void
+gpm_osd_dialog_show (GtkWidget *popup)
+{
+	int            orig_w;
+	int            orig_h;
+	int            screen_w;
+	int            screen_h;
+	int            x;
+	int            y;
+	int            pointer_x;
+	int            pointer_y;
+	GtkRequisition win_req;
+	GdkScreen     *pointer_screen;
+	GdkRectangle   geometry;
+	int            monitor;
+        GdkDisplay    *display;
+        GdkDeviceManager *device_manager;
+        GdkDevice     *device;
+
+	/*
+	 * get the window size
+	 * if the window hasn't been mapped, it doesn't necessarily
+	 * know its true size, yet, so we need to jump through hoops
+	 */
+	gtk_window_get_default_size (GTK_WINDOW (popup), &orig_w, &orig_h);
+	gtk_widget_get_preferred_size(popup, &win_req, NULL);
+
+	if (win_req.width > orig_w) {
+		orig_w = win_req.width;
+	}
+	if (win_req.height > orig_h) {
+		orig_h = win_req.height;
+	}
+
+	pointer_screen = NULL;
+        display = gtk_widget_get_display (popup);
+        device_manager = gdk_display_get_device_manager (display);
+        device = gdk_device_manager_get_client_pointer (device_manager);
+        gdk_device_get_position (device,
+				 &pointer_screen,
+				 &pointer_x,
+				 &pointer_y);
+	monitor = gdk_screen_get_monitor_at_point (pointer_screen,
+						   pointer_x,
+						   pointer_y);
+
+	gdk_screen_get_monitor_geometry (pointer_screen,
+					 monitor,
+					 &geometry);
+
+	screen_w = geometry.width;
+	screen_h = geometry.height;
+
+	x = ((screen_w - orig_w) / 2) + geometry.x;
+	y = geometry.y + (screen_h / 2) + (screen_h / 2 - orig_h) / 2;
+
+	gtk_window_move (GTK_WINDOW (popup), x, y);
+
+	gtk_widget_show (popup);
+
+	gdk_display_sync (gtk_widget_get_display (popup));
+}
+
diff --git a/src/gpm-osd-dialog.h b/src/gpm-osd-dialog.h
new file mode 100644
index 0000000..77104c7
--- /dev/null
+++ b/src/gpm-osd-dialog.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2011 Alex Murray <murray alex gmail com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __GPM_OSD_DIALOG_H
+#define __GPM_OSD_DIALOG_H
+
+#include "gsd-media-keys-window.h"
+
+void	gpm_osd_dialog_init	(GtkWidget **popup,
+				 const gchar *icon_name);
+void	gpm_osd_dialog_show	(GtkWidget *popup);
+
+#endif /* __GPM_OSD_DIALOG_H */
-- 
1.7.1



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