[Goodies-commits] r7129 - in xfce4-power-manager/trunk: . panel-plugins/brightness po

Ali Abdallah aliov at xfce.org
Tue Apr 7 11:16:47 CEST 2009


Author: aliov
Date: 2009-04-07 09:16:47 +0000 (Tue, 07 Apr 2009)
New Revision: 7129

Added:
   xfce4-power-manager/trunk/panel-plugins/brightness/brightness-button.c
   xfce4-power-manager/trunk/panel-plugins/brightness/brightness-button.h
   xfce4-power-manager/trunk/panel-plugins/brightness/brightness-proxy.c
   xfce4-power-manager/trunk/panel-plugins/brightness/brightness-proxy.h
Modified:
   xfce4-power-manager/trunk/ChangeLog
   xfce4-power-manager/trunk/panel-plugins/brightness/Makefile.am
   xfce4-power-manager/trunk/panel-plugins/brightness/brightness-plugin.c
   xfce4-power-manager/trunk/po/POTFILES.in
   xfce4-power-manager/trunk/po/xfce4-power-manager.pot
Log:
Updates on the brightness plugin to act like a menu so it will close the popup when it doesn't have input focus

Modified: xfce4-power-manager/trunk/ChangeLog
===================================================================
--- xfce4-power-manager/trunk/ChangeLog	2009-04-06 19:12:05 UTC (rev 7128)
+++ xfce4-power-manager/trunk/ChangeLog	2009-04-07 09:16:47 UTC (rev 7129)
@@ -1,4 +1,7 @@
 
+2009-04-07 11:16 Ali aliov at xfce.org 
+	 * : Updates on the brightness plugin to act like a menu so it will close the popup when it doesn't have input focus
+
 2009-04-06 19:47 Ali aliov at xfce.org 
 	 * : Fix some typos
 

Modified: xfce4-power-manager/trunk/panel-plugins/brightness/Makefile.am
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/brightness/Makefile.am	2009-04-06 19:12:05 UTC (rev 7128)
+++ xfce4-power-manager/trunk/panel-plugins/brightness/Makefile.am	2009-04-07 09:16:47 UTC (rev 7129)
@@ -2,7 +2,11 @@
 plugin_PROGRAMS = xfce4-brightness-plugin
 
 xfce4_brightness_plugin_SOURCES =		\
-	brightness-plugin.c
+	brightness-plugin.c			\
+	brightness-button.c			\
+	brightness-button.h			\
+	brightness-proxy.c			\
+	brightness-proxy.h
 
 xfce4_brightness_plugin_CFLAGS =		\
 	-I$(top_srcdir)				\

Added: xfce4-power-manager/trunk/panel-plugins/brightness/brightness-button.c
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/brightness/brightness-button.c	                        (rev 0)
+++ xfce4-power-manager/trunk/panel-plugins/brightness/brightness-button.c	2009-04-07 09:16:47 UTC (rev 7129)
@@ -0,0 +1,645 @@
+/*
+ * * Copyright (C) 2009 Ali <aliov at xfce.org>
+ *
+ * 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
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <glib.h>
+#include <libxfce4util/libxfce4util.h>
+#include <libxfcegui4/libxfcegui4.h>
+
+#include "libxfpm/xfpm-common.h"
+
+#include "brightness-button.h"
+#include "brightness-proxy.h"
+
+/* Init */
+static void brightness_button_class_init (BrightnessButtonClass *klass);
+static void brightness_button_init       (BrightnessButton *button);
+static void brightness_button_finalize   (GObject *object);
+
+#define BRIGHTNESS_BUTTON_GET_PRIVATE(o) \
+(G_TYPE_INSTANCE_GET_PRIVATE ((o), BRIGHTNESS_TYPE_BUTTON, BrightnessButtonPrivate))
+
+struct BrightnessButtonPrivate
+{
+    XfcePanelPlugin *plugin;
+    BrightnessProxy *brightness;
+    
+    GtkWidget       *popup;
+    GtkWidget       *range;
+    GtkWidget       *plus;
+    GtkWidget       *minus;
+    GtkWidget       *image;
+    gboolean         popup_open;
+};
+
+enum
+{
+    PROP_0,
+    PROP_PLUGIN
+};
+
+G_DEFINE_TYPE (BrightnessButton, brightness_button, GTK_TYPE_BUTTON)
+
+static void
+brightness_button_set_property (GObject *object,
+				guint prop_id,
+				const GValue *value,
+				GParamSpec   *pspec)
+{
+    BrightnessButton *button = BRIGHTNESS_BUTTON (object);
+    switch (prop_id)
+    {
+	case PROP_PLUGIN:
+	    button->priv->plugin = XFCE_PANEL_PLUGIN (g_object_ref (g_value_get_object (value)));
+	    break;
+	default:
+	    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+	break;
+    }
+}
+
+static void
+brightness_button_grab_notify (BrightnessButton *button, gboolean was_grabbed)
+{
+    GdkDisplay *display;
+    if (was_grabbed != FALSE)
+	return;
+
+    if (!GTK_WIDGET_HAS_GRAB (button->priv->popup))
+	return;
+
+    if (gtk_widget_is_ancestor (gtk_grab_get_current (), button->priv->popup))
+	return;
+  
+    display = gtk_widget_get_display (button->priv->popup);
+    gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
+    gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
+    gtk_grab_remove (button->priv->popup);
+    gtk_widget_hide (button->priv->popup);
+    button->priv->popup_open = FALSE;
+}
+
+static void
+brightness_button_popup_grab_notify (GtkWidget *widget, gboolean was_grabbed, BrightnessButton *button)
+{
+    brightness_button_grab_notify (button, was_grabbed);
+}
+
+static void
+brightness_button_range_grab_notify (GtkWidget *widget, gboolean was_grabbed, BrightnessButton *button)
+{
+    brightness_button_grab_notify (button, was_grabbed);
+}
+
+static gboolean
+brightness_button_popup_broken_event (GtkWidget *widget, gboolean was_grabbed, BrightnessButton *button)
+{
+    brightness_button_grab_notify (button, FALSE);
+    return FALSE;
+}
+
+static void
+brightness_button_release_grab (BrightnessButton *button, GdkEventButton *event)
+{
+    GdkEventButton *e;
+    GdkDisplay *display;
+
+    display = gtk_widget_get_display (GTK_WIDGET (button));
+    gdk_display_keyboard_ungrab (display, event->time);
+    gdk_display_pointer_ungrab (display, event->time);
+    gtk_grab_remove (button->priv->popup);
+
+    gtk_widget_hide (button->priv->popup);
+
+    e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
+    e->window = GTK_WIDGET (button)->window;
+    e->type = GDK_BUTTON_RELEASE;
+    gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
+    e->window = event->window;
+    gdk_event_free ((GdkEvent *) e);
+    button->priv->popup_open = FALSE;
+}
+
+static gboolean
+brightness_button_popup_button_press_event (GtkWidget *widget, GdkEventButton *ev, BrightnessButton *button)
+{
+    if ( ev->type == GDK_BUTTON_PRESS )
+    {
+	brightness_button_release_grab (button, ev);
+	return TRUE;
+    }
+    return FALSE;
+}
+
+static gboolean
+brightness_button_popup_key_release_event (GtkWidget *widget, GdkEventKey *ev, gpointer data)
+{
+    
+    return FALSE;
+}
+
+static void
+brightness_button_set_tooltip (BrightnessButton *button)
+{
+    gboolean has_hw;
+    
+    has_hw = brightness_proxy_has_hw (button->priv->brightness);
+    
+    if ( has_hw )
+	gtk_widget_set_tooltip_text (GTK_WIDGET (button), _("Control your LCD brightness"));
+    else
+	gtk_widget_set_tooltip_text (GTK_WIDGET (button), _("No device found"));
+}
+
+static gboolean
+brightness_button_popup_win (GtkWidget *widget, GdkEvent *ev, guint32 time)
+{
+    gint x, y, orientation;
+    gint current_level;
+    GdkDisplay *display;
+    GdkScreen *screen;
+    BrightnessButton *button;
+    XfceScreenPosition pos;
+    gboolean has_hw;
+    
+    button = BRIGHTNESS_BUTTON (widget);
+    
+    has_hw = brightness_proxy_has_hw (button->priv->brightness);
+    
+    if ( !has_hw ) 
+	return FALSE;
+    
+    display = gtk_widget_get_display (widget);
+    screen = gtk_widget_get_screen (widget);
+    
+    gtk_window_set_screen (GTK_WINDOW (button->priv->popup), screen);
+    
+    gtk_widget_show_all (button->priv->popup);
+    
+    gtk_grab_add (button->priv->popup);
+
+    if (gdk_pointer_grab (button->priv->popup->window, TRUE,
+			GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
+			GDK_POINTER_MOTION_MASK, NULL, NULL, time)
+	  != GDK_GRAB_SUCCESS)
+    {
+	gtk_grab_remove (button->priv->popup);
+	gtk_widget_hide (button->priv->popup);
+	return FALSE;
+    }
+
+    if (gdk_keyboard_grab (button->priv->popup->window, TRUE, time) != GDK_GRAB_SUCCESS)
+    {
+	gdk_display_pointer_ungrab (display, time);
+	gtk_grab_remove (button->priv->popup);
+	gtk_widget_hide (button->priv->popup);
+	return FALSE;
+    }
+
+    gtk_widget_grab_focus (button->priv->popup);
+    gtk_widget_grab_focus (button->priv->range);
+    
+    /* Position */
+    gdk_window_get_origin (widget->window, &x, &y);
+
+    pos = xfce_panel_plugin_get_screen_position (button->priv->plugin);
+    orientation = xfce_panel_plugin_get_orientation (button->priv->plugin);
+    
+    /* top */
+    if ( pos == XFCE_SCREEN_POSITION_NW_H || 
+	 pos == XFCE_SCREEN_POSITION_N    ||
+	 pos == XFCE_SCREEN_POSITION_NE_H )
+    {
+	x += widget->allocation.x
+		+ widget->allocation.width/2;
+	y += widget->allocation.height;
+	x -= button->priv->popup->allocation.width/2;
+    }
+    /* left */
+    else if ( pos == XFCE_SCREEN_POSITION_NW_V ||
+	      pos == XFCE_SCREEN_POSITION_W    ||
+	      pos == XFCE_SCREEN_POSITION_SW_V )
+    {
+	y += widget->allocation.y
+		+ widget->allocation.height/2;
+	x += widget->allocation.width;
+	y -= button->priv->popup->allocation.height/2;
+    }
+    /* right */
+    else if ( pos == XFCE_SCREEN_POSITION_NE_V ||
+	      pos == XFCE_SCREEN_POSITION_E    ||
+	      pos == XFCE_SCREEN_POSITION_SE_V )
+    {
+	y += widget->allocation.y
+		+ widget->allocation.height/2;
+	x -= button->priv->popup->allocation.width;
+	y -= button->priv->popup->allocation.height/2;
+    }
+    /* bottom */
+    else if ( pos == XFCE_SCREEN_POSITION_SW_H ||
+	      pos == XFCE_SCREEN_POSITION_S    ||
+	      pos == XFCE_SCREEN_POSITION_SE_H )
+    {
+	x += widget->allocation.x
+		+ widget->allocation.width/2;
+	y -= button->priv->popup->allocation.height;
+	x -= button->priv->popup->allocation.width/2;
+    }
+    else if ( pos == XFCE_SCREEN_POSITION_FLOATING_H )
+    {
+	x += widget->allocation.x
+		+ widget->allocation.width/2;
+	x -= button->priv->popup->allocation.width/2;
+	if ( y > button->priv->popup->allocation.height )
+	    y -= button->priv->popup->allocation.height;
+	else 
+	     y += widget->allocation.height;
+    }
+    else if ( pos == XFCE_SCREEN_POSITION_FLOATING_V )
+    {
+	y -= button->priv->popup->allocation.height/2;
+	y += widget->allocation.y
+		+ widget->allocation.height/2;
+	if ( x < button->priv->popup->allocation.width )
+	    x += widget->allocation.width;
+	else
+	    x -= button->priv->popup->allocation.width;
+    }
+    else
+    {
+	brightness_button_release_grab (button, (GdkEventButton *)ev);
+	g_return_val_if_reached (FALSE);
+    }
+   
+    gtk_window_move (GTK_WINDOW(button->priv->popup), x, y);
+    TRACE("Displaying window on x=%d y=%d", x, y);
+    current_level = brightness_proxy_get_level (button->priv->brightness);
+    
+    gtk_range_set_value (GTK_RANGE(button->priv->range), current_level);
+    button->priv->popup_open = TRUE;
+    return TRUE;
+}
+
+static gboolean
+brightness_button_press_event (GtkWidget *widget, GdkEventButton *ev)
+{
+    return brightness_button_popup_win (widget, (GdkEvent *) ev, ev->time);
+}
+
+static void
+minus_clicked (GtkWidget *widget, BrightnessButton *button)
+{
+    guint level, max_level;
+    
+    max_level = brightness_proxy_get_max_level (button->priv->brightness);
+    level = (guint ) gtk_range_get_value (GTK_RANGE (button->priv->range));
+    
+    if ( level != 0 )
+	gtk_range_set_value (GTK_RANGE (button->priv->range), level - 1);
+}
+
+static void
+plus_clicked (GtkWidget *widget, BrightnessButton *button)
+{
+    guint level, max_level;
+    
+    max_level = brightness_proxy_get_max_level (button->priv->brightness);
+    level = (guint ) gtk_range_get_value (GTK_RANGE (button->priv->range));
+    
+    if ( level != max_level )
+	gtk_range_set_value (GTK_RANGE (button->priv->range), level + 1);
+}
+
+static void
+range_value_changed (GtkWidget *widget, BrightnessButton *button)
+{
+    guint range_level, hw_level;
+    
+    range_level = (guint) gtk_range_get_value (GTK_RANGE (button->priv->range));
+    
+    hw_level = brightness_proxy_get_level (button->priv->brightness);
+    
+    if ( hw_level != range_level )
+    {
+	brightness_proxy_set_level (button->priv->brightness, range_level);
+    }
+}
+
+static void
+brightness_button_create_popup (BrightnessButton *button)
+{
+    GtkWidget *box;
+    GtkOrientation orientation;
+    guint max_level;
+    gboolean has_hw;
+    
+    has_hw = brightness_proxy_has_hw (button->priv->brightness);
+    if ( !has_hw )
+	return;
+	
+    max_level = brightness_proxy_get_max_level (button->priv->brightness);
+     
+    button->priv->popup = gtk_window_new (GTK_WINDOW_POPUP);
+    gtk_window_set_decorated (GTK_WINDOW(button->priv->popup), FALSE);
+    
+    g_signal_connect (button->priv->popup, "grab-notify",
+		      G_CALLBACK (brightness_button_popup_grab_notify), button);
+    g_signal_connect (button->priv->popup, "grab-broken-event",
+		      G_CALLBACK (brightness_button_popup_broken_event), button);
+    g_signal_connect (button->priv->popup, "key_release_event",
+		      G_CALLBACK (brightness_button_popup_key_release_event), button);
+    g_signal_connect (button->priv->popup , "button_press_event",
+		      G_CALLBACK (brightness_button_popup_button_press_event), button);
+		      
+    orientation = xfce_panel_plugin_get_orientation (button->priv->plugin);
+
+    if ( orientation == GTK_ORIENTATION_VERTICAL)
+	box = gtk_hbox_new (FALSE, 2);
+    else
+	box = gtk_vbox_new (FALSE, 2);
+    
+    button->priv->minus = gtk_button_new_with_label ("-");
+    gtk_button_set_relief (GTK_BUTTON(button->priv->minus), GTK_RELIEF_NONE);
+    g_signal_connect (button->priv->minus, "clicked",
+		      G_CALLBACK (minus_clicked), button);
+    
+    if ( orientation == GTK_ORIENTATION_VERTICAL )
+    {
+	button->priv->range = gtk_hscale_new_with_range (0, max_level, 1);
+	gtk_widget_set_size_request (button->priv->range, 100, -1);
+    }
+    else
+    {
+	button->priv->range = gtk_vscale_new_with_range (0, max_level, 1);
+	gtk_widget_set_size_request (button->priv->range, -1, 100);
+    }
+    gtk_range_set_inverted (GTK_RANGE(button->priv->range), TRUE);
+    gtk_scale_set_draw_value (GTK_SCALE(button->priv->range), FALSE);
+    
+    g_signal_connect (button->priv->range, "grab-notify",
+		      G_CALLBACK (brightness_button_range_grab_notify), button);
+    g_signal_connect (button->priv->range, "value-changed",
+		      G_CALLBACK (range_value_changed), button);
+  
+    button->priv->plus = gtk_button_new_with_label ("+");
+    gtk_button_set_relief (GTK_BUTTON(button->priv->plus), GTK_RELIEF_NONE);
+    g_signal_connect (button->priv->plus, "clicked",
+		      G_CALLBACK (plus_clicked), button);
+
+    gtk_box_pack_start (GTK_BOX(box), button->priv->plus, FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX(box), button->priv->range, TRUE, TRUE, 0);
+    gtk_box_pack_start (GTK_BOX(box), button->priv->minus, FALSE, FALSE, 0);
+    
+    gtk_window_set_type_hint (GTK_WINDOW(button->priv->popup), GDK_WINDOW_TYPE_HINT_UTILITY );
+    
+    gtk_container_add (GTK_CONTAINER(button->priv->popup), box);
+}
+
+static void
+brightness_button_up (BrightnessButton *button)
+{
+    guint level;
+    guint max_level;
+    
+    level = brightness_proxy_get_level (button->priv->brightness);
+    max_level = brightness_proxy_get_max_level (button->priv->brightness);
+    
+    if ( level != max_level )
+    {
+	plus_clicked (NULL, button);
+    }
+}
+
+static void
+brightness_button_down (BrightnessButton *button)
+{
+    guint level;
+    level = brightness_proxy_get_level (button->priv->brightness);
+    
+    if ( level != 0 )
+    {
+	minus_clicked (NULL, button);
+    }
+}
+
+static gboolean
+brightness_button_scroll_event (GtkWidget *widget, GdkEventScroll *ev)
+{
+    gboolean hw_found;
+    BrightnessButton *button;
+    
+    button = BRIGHTNESS_BUTTON (widget);
+    
+    hw_found = brightness_proxy_has_hw (button->priv->brightness);
+    
+    if ( !hw_found )
+	return FALSE;
+	
+    if ( ev->direction == GDK_SCROLL_UP )
+    {
+	brightness_button_up (button);
+	return TRUE;
+    }
+    else if ( ev->direction == GDK_SCROLL_DOWN )
+    {
+	brightness_button_down (button);
+	return TRUE;
+    }
+    return FALSE;
+}
+
+static void
+brightness_button_class_init (BrightnessButtonClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+    object_class->finalize = brightness_button_finalize;
+    object_class->set_property = brightness_button_set_property;
+    
+    widget_class->button_press_event = brightness_button_press_event;
+    widget_class->scroll_event = brightness_button_scroll_event;
+
+    g_object_class_install_property (object_class,
+				     PROP_PLUGIN,
+				     g_param_spec_object ("plugin",
+							  NULL,
+							  NULL,
+							  XFCE_TYPE_PANEL_PLUGIN,
+							  G_PARAM_CONSTRUCT_ONLY |
+							  G_PARAM_WRITABLE));
+
+    g_type_class_add_private (klass, sizeof (BrightnessButtonPrivate));
+}
+
+static void
+brightness_button_init (BrightnessButton *button)
+{
+    button->priv = BRIGHTNESS_BUTTON_GET_PRIVATE (button);
+    
+    button->priv->brightness = brightness_proxy_new ();
+    
+    gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
+}
+
+static void
+brightness_button_finalize (GObject *object)
+{
+    BrightnessButton *button;
+
+    button = BRIGHTNESS_BUTTON (object);
+
+    g_object_unref (button->priv->brightness);
+    g_object_unref (button->priv->plugin);
+
+    G_OBJECT_CLASS (brightness_button_parent_class)->finalize (object);
+}
+
+GtkWidget *
+brightness_button_new (XfcePanelPlugin *plugin)
+{
+    BrightnessButton *button = NULL;
+    button = g_object_new (BRIGHTNESS_TYPE_BUTTON, "plugin", plugin, NULL);
+    return GTK_WIDGET (button);
+}
+
+static void
+destroy_popup (BrightnessButton *button)
+{
+    if ( GTK_IS_WIDGET (button->priv->range) )
+	gtk_widget_destroy (button->priv->range);
+	
+    if ( GTK_IS_WIDGET (button->priv->minus) )
+	gtk_widget_destroy (button->priv->minus);
+	
+    if ( GTK_IS_WIDGET (button->priv->plus) )
+	gtk_widget_destroy (button->priv->plus);
+	
+    if ( GTK_IS_WIDGET (button->priv->popup) )
+	gtk_widget_destroy (button->priv->popup);
+}
+
+static gboolean
+brightness_button_set_icon (BrightnessButton *button, gint width)
+{
+    gboolean hw_found;
+    GdkPixbuf *pixbuf;
+    const gchar *icon_name;
+    
+    hw_found = brightness_proxy_has_hw (button->priv->brightness);
+    
+    icon_name = hw_found ? "gpm-brightness-lcd" : "gpm-brightness-lcd-invalid";
+    
+    pixbuf = xfce_themed_icon_load (icon_name, width);
+    
+    if ( pixbuf )
+    {
+	gtk_image_set_from_pixbuf (GTK_IMAGE (button->priv->image), pixbuf);
+	g_object_unref (pixbuf);
+	return TRUE;
+    }
+    return FALSE;
+}
+
+static gboolean
+brightness_button_size_changed_cb (XfcePanelPlugin *plugin, gint size, BrightnessButton *button)
+{
+    gint width = size -2 - 2* MAX(GTK_WIDGET(button)->style->xthickness,
+				  GTK_WIDGET(button)->style->xthickness);
+				 
+    gtk_widget_set_size_request (GTK_WIDGET(plugin), size, size);
+    return brightness_button_set_icon (button, width);
+}
+
+static void
+reload_activated (GtkWidget *widget, BrightnessButton *button)
+{
+    gint size;
+    
+    brightness_proxy_reload (button->priv->brightness);
+    destroy_popup (button);
+    brightness_button_create_popup (button);
+    brightness_button_set_tooltip (button);
+    
+    size = xfce_panel_plugin_get_size (button->priv->plugin);
+    brightness_button_size_changed_cb (button->priv->plugin, size, button);
+}
+
+static void
+brightness_button_free_data_cb (XfcePanelPlugin *plugin, BrightnessButton *button)
+{
+    destroy_popup (button);
+    gtk_widget_destroy (GTK_WIDGET (button));
+}
+
+static void
+brightness_button_orientation_changed_cb (XfcePanelPlugin *plugin, GtkOrientation or, BrightnessButton *button)
+{
+    destroy_popup (button);
+    brightness_button_create_popup (button);
+}
+
+void brightness_button_show (BrightnessButton *button)
+{
+    GtkWidget *mi;
+    
+    g_return_if_fail (BRIGHTNESS_IS_BUTTON (button));
+    
+    gtk_container_add (GTK_CONTAINER (button->priv->plugin),
+		       GTK_WIDGET (button));
+		       
+    xfce_panel_plugin_add_action_widget (button->priv->plugin, GTK_WIDGET (button));
+    
+    button->priv->image = gtk_image_new ();
+    gtk_container_add (GTK_CONTAINER (button), button->priv->image);
+    
+    mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_REFRESH, NULL);
+    gtk_widget_show (mi);
+    
+    g_signal_connect (mi, "activate", 
+		      G_CALLBACK (reload_activated), button);
+		      
+    xfce_panel_plugin_menu_insert_item (button->priv->plugin, GTK_MENU_ITEM (mi));
+    
+    g_signal_connect (button->priv->plugin, "size-changed",
+		      G_CALLBACK (brightness_button_size_changed_cb), button);
+		      
+    g_signal_connect (button->priv->plugin, "orientation_changed", 
+		      G_CALLBACK (brightness_button_orientation_changed_cb), button);
+		      
+    g_signal_connect (button->priv->plugin, "free-data",
+		      G_CALLBACK (brightness_button_free_data_cb), button);
+    
+    g_signal_connect (button->priv->plugin, "about",
+		      G_CALLBACK (xfpm_about), _("Brightness plugin"));
+    
+    xfce_panel_plugin_menu_show_about (button->priv->plugin);
+    
+    gtk_widget_show_all (GTK_WIDGET(button));
+    brightness_button_create_popup (button);
+    brightness_button_set_tooltip (button);
+}

Added: xfce4-power-manager/trunk/panel-plugins/brightness/brightness-button.h
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/brightness/brightness-button.h	                        (rev 0)
+++ xfce4-power-manager/trunk/panel-plugins/brightness/brightness-button.h	2009-04-07 09:16:47 UTC (rev 7129)
@@ -0,0 +1,57 @@
+/*
+ * * Copyright (C) 2009 Ali <aliov at xfce.org>
+ *
+ * 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 __BRIGHTNESS_BUTTON_H
+#define __BRIGHTNESS_BUTTON_H
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+#include <libxfce4panel/xfce-panel-plugin.h>
+
+G_BEGIN_DECLS
+
+#define BRIGHTNESS_TYPE_BUTTON        (brightness_button_get_type () )
+#define BRIGHTNESS_BUTTON(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), BRIGHTNESS_TYPE_BUTTON, BrightnessButton))
+#define BRIGHTNESS_IS_BUTTON(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), BRIGHTNESS_TYPE_BUTTON))
+
+typedef struct BrightnessButtonPrivate BrightnessButtonPrivate;
+
+typedef struct
+{
+    GtkButton         		 parent;
+    BrightnessButtonPrivate     *priv;
+    
+} BrightnessButton;
+
+typedef struct
+{
+    GtkButtonClass 		 parent_class;
+    
+} BrightnessButtonClass;
+
+GType        			 brightness_button_get_type        (void) G_GNUC_CONST;
+
+GtkWidget       		*brightness_button_new             (XfcePanelPlugin *plugin);
+
+void                             brightness_button_show            (BrightnessButton *button);
+
+G_END_DECLS
+
+#endif /* __BRIGHTNESS_BUTTON_H */

Modified: xfce4-power-manager/trunk/panel-plugins/brightness/brightness-plugin.c
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/brightness/brightness-plugin.c	2009-04-06 19:12:05 UTC (rev 7128)
+++ xfce4-power-manager/trunk/panel-plugins/brightness/brightness-plugin.c	2009-04-07 09:16:47 UTC (rev 7129)
@@ -30,565 +30,19 @@
 #include <glib.h>
 
 #include <libxfce4panel/xfce-panel-plugin.h>
-#include <libxfce4util/libxfce4util.h>
-#include <libxfcegui4/libxfcegui4.h>
 
 #include <dbus/dbus-glib.h>
 
-#include "libxfpm/hal-manager.h"
-#include "libxfpm/hal-device.h"
-#include "libxfpm/xfpm-common.h"
+#include "brightness-button.h"
 
-typedef struct
-{
-    DBusGConnection  *bus;
-    DBusGConnection  *session;
-    
-    XfcePanelPlugin  *plugin;
-    gint              max_level;
-    gint              current_level;
-    
-    gboolean          hw_found;
-    gboolean          xfpm_running;
-    
-    gboolean          open;
-    DBusGProxy       *proxy;
-    DBusGProxy       *xfpm_proxy;
-
-    GtkWidget        *button;
-    
-    GtkWidget        *scale;
-    GtkWidget        *image;
-    GtkWidget 	     *win;
-    GtkWidget        *plus;
-    GtkWidget        *minus;
-
-} brightness_t;
-
-
 static void
-brightness_plugin_update_xfpm_brightness_level (brightness_t *plugin, guint level)
-{
-    dbus_g_proxy_call_no_reply (plugin->xfpm_proxy, "UpdateBrightness",
-			        G_TYPE_UINT, level,
-				G_TYPE_INVALID,
-				G_TYPE_INVALID);
-}
-
-static void
-brightness_plugin_set_tooltip (brightness_t *brightness)
-{
-    if ( brightness->hw_found )
-	gtk_widget_set_tooltip_text (brightness->button, _("Control your LCD brightness"));
-    else
-	gtk_widget_set_tooltip_text (brightness->button, _("No device found"));
-}
-
-static gint 
-brightness_plugin_get_level (brightness_t *brightness)
-{
-    GError *error = NULL;
-    gint level = 0;
-    gboolean ret;
-    
-    ret = dbus_g_proxy_call (brightness->proxy, "GetBrightness", &error,
-	 		     G_TYPE_INVALID,
-			     G_TYPE_INT, &level,
-			     G_TYPE_INVALID);
-
-    if ( error )
-    {
-	g_critical ("Error getting brightness level: %s\n", error->message);
-	g_error_free (error);
-    }
-    return level;
-}
-
-static gboolean
-brightness_plugin_set_level (brightness_t *brightness, gint level)
-{
-    GError *error = NULL;
-    gboolean ret;
-    gint dummy;
-    
-    ret = dbus_g_proxy_call (brightness->proxy, "SetBrightness", &error,
-			     G_TYPE_INT, level,
-			     G_TYPE_INVALID,
-			     G_TYPE_INT, &dummy,
-			     G_TYPE_INVALID );
-    if ( error )
-    {
-	g_critical ("Error setting brightness level: %s\n", error->message);
-	g_error_free (error);
-    }
-    else
-    {
-	brightness->current_level = level;
-    }
-    
-    if ( brightness->xfpm_running )
-	brightness_plugin_update_xfpm_brightness_level (brightness, level);
-    
-    return ret;
-}
-
-static void
-brightness_plugin_get_device (brightness_t *brightness)
-{
-    HalManager *manager;
-    HalDevice *device;
-    gchar **udis = NULL;
-    
-    //FIXME Don't connect blindly
-    brightness->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
-    
-    manager = hal_manager_new ();
-    
-    udis = hal_manager_find_device_by_capability (manager, "laptop_panel");
-    
-    if (!udis || !udis[0] )
-    {
-	TRACE ("No laptop panel found on the system");
-	brightness->hw_found = FALSE;
-	goto out;
-    }
-    
-    device = hal_device_new ();
-    hal_device_set_udi (device, udis[0]);
-    
-    brightness->max_level =
-	hal_device_get_property_int (device, "laptop_panel.num_levels") -1;
-	
-    TRACE("Laptop panel %s with max level %d", udis[0], brightness->max_level);
-    
-    brightness->proxy = dbus_g_proxy_new_for_name (brightness->bus,
-		   			           "org.freedesktop.Hal",
-						   udis[0],
-						   "org.freedesktop.Hal.Device.LaptopPanel");
-    brightness->hw_found = TRUE;
-    brightness->current_level = brightness_plugin_get_level (brightness);
-    
-    g_object_unref (device);
-out:
-    g_object_unref (manager);
-}
-
-static void 
-brightness_plugin_button_press_cb (GtkWidget *widget, brightness_t *plugin)
-{
-    gint x, y, orientation;
-    GdkDisplay *display;
-    GdkScreen *screen;
-    XfceScreenPosition pos;
-    
-    if ( !plugin->hw_found )
-	return;
-    
-    if ( plugin->open )
-    {
-	gtk_widget_hide (plugin->win);
-	plugin->open = FALSE;
-	return;
-    }
-    
-    display = gtk_widget_get_display (plugin->button);
-    screen = gtk_widget_get_screen (plugin->button);
-    
-    gtk_window_set_screen (GTK_WINDOW(plugin->win), screen);
-    gdk_window_get_origin (plugin->button->window, &x, &y);
-    gtk_widget_show_all (plugin->win);
-
-    pos = xfce_panel_plugin_get_screen_position (plugin->plugin);
-    orientation = xfce_panel_plugin_get_orientation (plugin->plugin);
-    
-    /* top */
-    if ( pos == XFCE_SCREEN_POSITION_NW_H || 
-	 pos == XFCE_SCREEN_POSITION_N    ||
-	 pos == XFCE_SCREEN_POSITION_NE_H )
-    {
-	x += plugin->button->allocation.x
-		+ plugin->button->allocation.width/2;
-	y += plugin->button->allocation.height;
-	x -= plugin->win->allocation.width/2;
-    }
-    /* left */
-    else if ( pos == XFCE_SCREEN_POSITION_NW_V ||
-	      pos == XFCE_SCREEN_POSITION_W    ||
-	      pos == XFCE_SCREEN_POSITION_SW_V )
-    {
-	y += plugin->button->allocation.y
-		+ plugin->button->allocation.height/2;
-	x += plugin->button->allocation.width;
-	y -= plugin->win->allocation.height/2;
-    }
-    /* right */
-    else if ( pos == XFCE_SCREEN_POSITION_NE_V ||
-	      pos == XFCE_SCREEN_POSITION_E    ||
-	      pos == XFCE_SCREEN_POSITION_SE_V )
-    {
-	y += plugin->button->allocation.y
-		+ plugin->button->allocation.height/2;
-	x -= plugin->win->allocation.width;
-	y -= plugin->win->allocation.height/2;
-    }
-    /* bottom */
-    else if ( pos == XFCE_SCREEN_POSITION_SW_H ||
-	      pos == XFCE_SCREEN_POSITION_S    ||
-	      pos == XFCE_SCREEN_POSITION_SE_H )
-    {
-	x += plugin->button->allocation.x
-		+ plugin->button->allocation.width/2;
-	y -= plugin->win->allocation.height;
-	x -= plugin->win->allocation.width/2;
-    }
-    else if ( pos == XFCE_SCREEN_POSITION_FLOATING_H )
-    {
-	x += plugin->button->allocation.x
-		+ plugin->button->allocation.width/2;
-	x -= plugin->win->allocation.width/2;
-	if ( y > plugin->win->allocation.height )
-	    y -= plugin->win->allocation.height;
-	else 
-	     y += plugin->button->allocation.height;
-    }
-    else if ( pos == XFCE_SCREEN_POSITION_FLOATING_V )
-    {
-	y -= plugin->win->allocation.height/2;
-	y += plugin->button->allocation.y
-		+ plugin->button->allocation.height/2;
-	if ( x < plugin->win->allocation.width )
-	    x += plugin->button->allocation.width;
-	else
-	    x -= plugin->win->allocation.width;
-    }
-    else
-    {
-	gtk_widget_hide (plugin->win);
-	g_return_if_reached ();
-    }
-   
-    gtk_window_move (GTK_WINDOW(plugin->win), x, y);
-    TRACE("Displaying window on x=%d y=%d", x, y);
-    plugin->current_level = brightness_plugin_get_level (plugin);
-    
-    gtk_range_set_value (GTK_RANGE(plugin->scale), plugin->current_level);
-    
-    plugin->open = TRUE;
-}
-
-
-static void
-plus_clicked (GtkWidget *widget, brightness_t *plugin)
-{
-    gint level = (gint)gtk_range_get_value (GTK_RANGE(plugin->scale));
-    
-    if ( level != plugin->max_level )
-	gtk_range_set_value (GTK_RANGE(plugin->scale), level + 1);
-}
-
-static void
-minus_clicked (GtkWidget *widget, brightness_t *plugin)
-{
-    gint level = (gint)gtk_range_get_value (GTK_RANGE(plugin->scale));
-    
-    if ( level != 0 )
-	gtk_range_set_value (GTK_RANGE(plugin->scale), level - 1);
-}
-
-static void
-scale_value_changed (GtkRange *range, brightness_t *plugin)
-{
-    gint range_level = (gint)gtk_range_get_value (range);
-    gint hw_level = brightness_plugin_get_level (plugin);
-    
-    if ( hw_level != range_level )
-    {
-	if (!brightness_plugin_set_level (plugin, range_level))
-	{
-	    g_warning("Failed to set brightness level\n");
-	}
-	brightness_plugin_set_tooltip (plugin);
-    }
-}
-
-static void
-brightness_plugin_destroy_popup (brightness_t *plugin)
-{
-    if ( plugin->win != NULL)
-    {
-	gtk_widget_destroy (plugin->win);
-	plugin->win = NULL;
-    }
-}
-
-static void
-brightness_plugin_create_popup (brightness_t *plugin)
-{
-    GtkWidget *box;
-    GtkOrientation orientation;
-    
-    plugin->win = gtk_window_new (GTK_WINDOW_POPUP);
-    gtk_window_set_decorated (GTK_WINDOW(plugin->win), FALSE);
-    
-    orientation = xfce_panel_plugin_get_orientation (plugin->plugin);
-
-    if ( orientation == GTK_ORIENTATION_VERTICAL)
-	box = gtk_hbox_new (FALSE, 2);
-    else
-	box = gtk_vbox_new (FALSE, 2);
-    
-    plugin->minus = gtk_button_new_with_label ("-");
-    gtk_button_set_relief (GTK_BUTTON(plugin->minus), GTK_RELIEF_NONE);
-    
-    if ( orientation == GTK_ORIENTATION_VERTICAL )
-    {
-	plugin->scale = gtk_hscale_new_with_range (0, plugin->max_level, 1);
-	gtk_widget_set_size_request (plugin->scale, 100, -1);
-    }
-    else
-    {
-	plugin->scale = gtk_vscale_new_with_range (0, plugin->max_level, 1);
-	gtk_widget_set_size_request (plugin->scale, -1, 100);
-    }
-    gtk_range_set_inverted (GTK_RANGE(plugin->scale), TRUE);
-    gtk_scale_set_draw_value (GTK_SCALE(plugin->scale), FALSE);
-  
-    
-    plugin->plus = gtk_button_new_with_label ("+");
-    gtk_button_set_relief (GTK_BUTTON(plugin->plus), GTK_RELIEF_NONE);
-
-    gtk_box_pack_start (GTK_BOX(box), plugin->plus, FALSE, FALSE, 0);
-    gtk_box_pack_start (GTK_BOX(box), plugin->scale, TRUE, TRUE, 0);
-    gtk_box_pack_start (GTK_BOX(box), plugin->minus, FALSE, FALSE, 0);
-    
-    gtk_window_set_type_hint (GTK_WINDOW(plugin->win), GDK_WINDOW_TYPE_HINT_UTILITY );
-    
-    gtk_container_add (GTK_CONTAINER(plugin->win), box);
-}
-
-static gboolean
-brightness_plugin_set_icon (brightness_t *brightness, gint width)
-{
-    GdkPixbuf *pixbuf;
-    const gchar *icon_name;
-    
-    icon_name = brightness->hw_found ? "gpm-brightness-lcd" : "gpm-brightness-lcd-invalid";
-    pixbuf = xfce_themed_icon_load (icon_name, width);
-    
-    if ( pixbuf )
-    {
-	gtk_image_set_from_pixbuf (GTK_IMAGE(brightness->image), pixbuf);
-	g_object_unref (pixbuf);
-	return TRUE;
-    }
-    return FALSE;
-}
-
-static gboolean
-brightness_plugin_size_changed_cb (XfcePanelPlugin *plugin, gint size, brightness_t *brightness)
-{
-    gint width = size -2 - 2* MAX(brightness->button->style->xthickness,
-				 brightness->button->style->xthickness);
-				 
-    gtk_widget_set_size_request (GTK_WIDGET(plugin), size, size);
-    return brightness_plugin_set_icon (brightness, width);
-}
-
-static void
-brightness_plugin_construct_popup (brightness_t *plugin)
-{
-    if ( plugin->win )
-    {
-	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->button), FALSE);
-	gtk_widget_destroy (plugin->win);
-    }
-    
-    brightness_plugin_create_popup (plugin);
-    plugin->open = FALSE;
-
-    g_signal_connect (plugin->plus, "clicked",
-		      G_CALLBACK (plus_clicked), plugin);
-		      
-    g_signal_connect (plugin->minus, "clicked",
-		      G_CALLBACK (minus_clicked), plugin);
-
-    g_signal_connect (plugin->scale, "value-changed",
-		      G_CALLBACK(scale_value_changed), plugin);
-}
-
-static void
-brightness_plugin_orientation_changed_cb (XfcePanelPlugin *plugin, 
-					  GtkOrientation orientation, 
-					  brightness_t *brightness)
-{
-    brightness_plugin_destroy_popup (brightness);
-    brightness_plugin_construct_popup (brightness);
-    
-}
-
-static void
-brightness_plugin_xfpm (brightness_t *plugin)
-{
-    plugin->session = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
-    
-    plugin->xfpm_proxy = dbus_g_proxy_new_for_name (plugin->session,
-						   "org.freedesktop.PowerManagement",
-						   "/org/freedesktop/PowerManagement/Backlight",
-						   "org.freedesktop.PowerManagement.Backlight");
-					       
-    if ( !plugin->xfpm_proxy )
-    {
-	g_warning ("Failed to create proxy");
-	plugin->xfpm_running = FALSE;
-	return;
-    }
-    
-    plugin->xfpm_running = TRUE;
-}
-
-static void brightness_plugin_free_data_cb (XfcePanelPlugin *plugin, brightness_t *brightness)
-{
-    if ( brightness->win )
-	gtk_widget_destroy (brightness->win);
-	
-    if ( brightness->bus)
-	dbus_g_connection_unref (brightness->bus);
-	
-    if ( brightness->proxy )
-	g_object_unref (brightness->proxy);
-	
-    if ( brightness->xfpm_proxy )
-	g_object_unref (brightness->xfpm_proxy );
-	
-    if ( brightness->session )
-	dbus_g_connection_unref (brightness->session);
-	
-    g_free (brightness);
-}
-
-static void
-reload_activated (GtkWidget *widget, brightness_t *brightness)
-{
-    gint size;
-    brightness_plugin_get_device (brightness);
-    
-    if ( brightness->hw_found )
-    {
-	brightness_plugin_construct_popup (brightness);
-	brightness_plugin_set_level (brightness, 9);
-	brightness_plugin_xfpm (brightness);
-    }
-
-    size = xfce_panel_plugin_get_size (brightness->plugin);
-    brightness_plugin_size_changed_cb (brightness->plugin, size, brightness);
-    brightness_plugin_set_tooltip (brightness);
-    
-}
-
-static void
-brightness_plugin_up (brightness_t *brightness)
-{
-    guint level;
-    level = brightness_plugin_get_level (brightness);
-    
-    if ( level != brightness->max_level )
-    {
-	plus_clicked (NULL, brightness);
-    }
-}
-
-static void
-brightness_plugin_down (brightness_t *brightness)
-{
-    guint level;
-    level = brightness_plugin_get_level (brightness);
-    
-    if ( level != 0 )
-    {
-	minus_clicked (NULL, brightness);
-    }
-}
-
-static gboolean
-brightness_plugin_scroll_event_cb (GtkWidget *button, GdkEventScroll *ev, brightness_t *brightness)
-{
-    if ( ev->direction  == GDK_SCROLL_DOWN )
-    {
-	brightness_plugin_down (brightness);
-	return TRUE;
-    }
-    else if ( ev->direction == GDK_SCROLL_UP )
-    {
-	brightness_plugin_up (brightness);
-	return TRUE;
-    }
-    return FALSE;
-}
-
-static void
-brightness_plugin_construct (brightness_t *plugin)
-{
-    GtkWidget *mi;
-    
-    plugin->image = gtk_image_new ();
-    plugin->button = gtk_toggle_button_new ();
-    
-    gtk_container_add (GTK_CONTAINER(plugin->button), plugin->image);
-    
-    gtk_button_set_relief (GTK_BUTTON(plugin->button), GTK_RELIEF_NONE);
-    
-    gtk_container_add (GTK_CONTAINER(plugin->plugin), plugin->button);
-    
-    xfce_panel_plugin_add_action_widget (plugin->plugin, plugin->button);
-
-    g_signal_connect (plugin->button, "clicked",
-		      G_CALLBACK (brightness_plugin_button_press_cb), plugin);
-    g_signal_connect (plugin->button, "scroll-event",
-		      G_CALLBACK (brightness_plugin_scroll_event_cb), plugin);
-		      
-    mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_REFRESH, NULL);
-    gtk_widget_show (mi);
-    g_signal_connect (mi, "activate",
-		      G_CALLBACK(reload_activated), plugin);
-		      
-    xfce_panel_plugin_menu_insert_item (plugin->plugin, GTK_MENU_ITEM(mi));
-		      
-    gtk_widget_show_all (plugin->button);
-}
-
-static void
 register_brightness_plugin (XfcePanelPlugin *plugin)
 {
-    brightness_t *brightness;
+    GtkWidget *button;
     
-    brightness = g_new0 (brightness_t, 1); 
+    button = brightness_button_new (plugin);
     
-    brightness->plugin = plugin;
-    
-    brightness_plugin_construct (brightness);
-    brightness_plugin_get_device (brightness);
-    
-    if ( brightness->hw_found )
-    {
-	brightness_plugin_construct_popup (brightness);
-	brightness_plugin_xfpm (brightness);
-    }
-    
-    brightness_plugin_set_tooltip (brightness);
-    
-    g_signal_connect (plugin, "free-data",
-		      G_CALLBACK(brightness_plugin_free_data_cb), brightness);
-		      
-    g_signal_connect (plugin, "size-changed",
-		      G_CALLBACK(brightness_plugin_size_changed_cb), brightness);
-		      
-    g_signal_connect (plugin, "orientation-changed",
-		      G_CALLBACK(brightness_plugin_orientation_changed_cb), brightness);
-		      
-    xfce_panel_plugin_menu_show_about(plugin);
-    
-    g_signal_connect (plugin, "about", G_CALLBACK(xfpm_about), _("Brightness plugin"));
-
+    brightness_button_show (BRIGHTNESS_BUTTON (button));
 }
 
 XFCE_PANEL_PLUGIN_REGISTER_EXTERNAL(register_brightness_plugin);

Added: xfce4-power-manager/trunk/panel-plugins/brightness/brightness-proxy.c
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/brightness/brightness-proxy.c	                        (rev 0)
+++ xfce4-power-manager/trunk/panel-plugins/brightness/brightness-proxy.c	2009-04-07 09:16:47 UTC (rev 7129)
@@ -0,0 +1,240 @@
+/*
+ * * Copyright (C) 2009 Ali <aliov at xfce.org>
+ *
+ * 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
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <glib.h>
+
+#include <dbus/dbus-glib.h>
+
+#include <libxfce4util/libxfce4util.h>
+
+#include "libxfpm/hal-manager.h"
+#include "libxfpm/hal-device.h"
+
+#include "brightness-proxy.h"
+
+/* Init */
+static void brightness_proxy_class_init (BrightnessProxyClass *klass);
+static void brightness_proxy_init       (BrightnessProxy *brightness_proxy);
+static void brightness_proxy_finalize   (GObject *object);
+
+#define BRIGHTNESS_PROXY_GET_PRIVATE(o) \
+(G_TYPE_INSTANCE_GET_PRIVATE ((o), BRIGHTNESS_TYPE_PROXY, BrightnessProxyPrivate))
+
+struct BrightnessProxyPrivate
+{
+    DBusGConnection *bus;
+    DBusGConnection *session;
+    DBusGProxy      *proxy;
+    guint            max_level;
+    gboolean         has_hw;
+};
+
+G_DEFINE_TYPE (BrightnessProxy, brightness_proxy, G_TYPE_OBJECT)
+
+static void
+brightness_proxy_get_device (BrightnessProxy *brightness)
+{
+    HalManager *manager;
+    HalDevice *device;
+    gchar **udis = NULL;
+    
+    manager = hal_manager_new ();
+    
+    udis = hal_manager_find_device_by_capability (manager, "laptop_panel");
+    
+    if (!udis || !udis[0] )
+    {
+	TRACE ("No laptop panel found on the system");
+	brightness->priv->has_hw = FALSE;
+	brightness->priv->proxy = NULL;
+	goto out;
+    }
+    
+    device = hal_device_new ();
+    hal_device_set_udi (device, udis[0]);
+    
+    brightness->priv->max_level =
+	hal_device_get_property_int (device, "laptop_panel.num_levels") -1;
+	
+    TRACE("Laptop panel %s with max level %d", udis[0], brightness->priv->max_level);
+    
+    brightness->priv->proxy = dbus_g_proxy_new_for_name (brightness->priv->bus,
+		   			                 "org.freedesktop.Hal",
+						         udis[0],
+						         "org.freedesktop.Hal.Device.LaptopPanel");
+    brightness->priv->has_hw = TRUE;
+    
+    g_object_unref (device);
+out:
+    g_object_unref (manager);
+}
+
+static void
+brightness_proxy_class_init (BrightnessProxyClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+    object_class->finalize = brightness_proxy_finalize;
+
+    g_type_class_add_private (klass, sizeof (BrightnessProxyPrivate));
+}
+
+static void
+brightness_proxy_init (BrightnessProxy *brightness_proxy)
+{
+    brightness_proxy->priv = BRIGHTNESS_PROXY_GET_PRIVATE (brightness_proxy);
+    
+    brightness_proxy->priv->max_level = 0;
+    
+    // FIXME, Don't connect blindly
+    brightness_proxy->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
+    brightness_proxy->priv->session = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
+    
+    brightness_proxy_get_device (brightness_proxy);
+}
+
+static void
+brightness_proxy_finalize (GObject *object)
+{
+    BrightnessProxy *brightness_proxy;
+
+    brightness_proxy = BRIGHTNESS_PROXY (object);
+    
+    dbus_g_connection_unref (brightness_proxy->priv->bus);
+    dbus_g_connection_unref (brightness_proxy->priv->session);
+    
+    if ( brightness_proxy->priv->proxy )
+	g_object_unref (brightness_proxy->priv->proxy);
+
+    G_OBJECT_CLASS (brightness_proxy_parent_class)->finalize (object);
+}
+
+static void
+brightness_proxy_update_xfpm_brightness_level (BrightnessProxy *brightness, guint level)
+{
+    DBusGProxy *proxy;
+    
+    proxy = dbus_g_proxy_new_for_name (brightness->priv->session,
+				       "org.freedesktop.PowerManagement",
+				       "/org/freedesktop/PowerManagement/Backlight",
+				       "org.freedesktop.PowerManagement.Backlight");
+					       
+    if ( !proxy )
+    {
+	g_warning ("Failed to create proxy to Xfpm");
+	return;
+    }
+
+    dbus_g_proxy_call_no_reply (proxy, "UpdateBrightness",
+			        G_TYPE_UINT, level,
+				G_TYPE_INVALID,
+				G_TYPE_INVALID);
+				
+    g_object_unref ( proxy );
+}
+
+BrightnessProxy *
+brightness_proxy_new (void)
+{
+    BrightnessProxy *brightness_proxy = NULL;
+    brightness_proxy = g_object_new (BRIGHTNESS_TYPE_PROXY, NULL);
+    return brightness_proxy;
+}
+
+gboolean
+brightness_proxy_set_level (BrightnessProxy *brightness, guint level)
+{
+    g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), FALSE);
+    
+    GError *error = NULL;
+    gboolean ret;
+    gint dummy;
+    
+    ret = dbus_g_proxy_call (brightness->priv->proxy, "SetBrightness", &error,
+			     G_TYPE_INT, level,
+			     G_TYPE_INVALID,
+			     G_TYPE_INT, &dummy,
+			     G_TYPE_INVALID );
+    if ( error )
+    {
+	g_critical ("Error setting brightness level: %s\n", error->message);
+	g_error_free (error);
+	return FALSE;
+    }
+	
+    brightness_proxy_update_xfpm_brightness_level (brightness, level);
+    
+    return ret;
+}
+
+guint
+brightness_proxy_get_level (BrightnessProxy *brightness)
+{
+    g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), 0);
+    
+    GError *error = NULL;
+    gint level = 0;
+    gboolean ret;
+    
+    ret = dbus_g_proxy_call (brightness->priv->proxy, "GetBrightness", &error,
+	 		     G_TYPE_INVALID,
+			     G_TYPE_INT, &level,
+			     G_TYPE_INVALID);
+
+    if ( error )
+    {
+	g_critical ("Error getting brightness level: %s\n", error->message);
+	g_error_free (error);
+    }
+    return level;
+}
+
+guint brightness_proxy_get_max_level (BrightnessProxy *brightness)
+{
+    g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), 0);
+    
+    return brightness->priv->max_level;
+}
+
+gboolean brightness_proxy_has_hw (BrightnessProxy *brightness)
+{
+    g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), FALSE);
+    
+    return brightness->priv->has_hw;
+}
+
+
+void brightness_proxy_reload (BrightnessProxy *brightness)
+{
+    g_return_if_fail (BRIGHTNESS_IS_PROXY (brightness));
+    
+    if ( brightness->priv->proxy )
+	g_object_unref (brightness->priv->proxy);
+	
+    brightness_proxy_get_device (brightness);
+}

Added: xfce4-power-manager/trunk/panel-plugins/brightness/brightness-proxy.h
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/brightness/brightness-proxy.h	                        (rev 0)
+++ xfce4-power-manager/trunk/panel-plugins/brightness/brightness-proxy.h	2009-04-07 09:16:47 UTC (rev 7129)
@@ -0,0 +1,62 @@
+/*
+ * * Copyright (C) 2009 Ali <aliov at xfce.org>
+ *
+ * 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 __BRIGHTNESS_PROXY_H
+#define __BRIGHTNESS_PROXY_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define BRIGHTNESS_TYPE_PROXY        (brightness_proxy_get_type () )
+#define BRIGHTNESS_PROXY(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), BRIGHTNESS_TYPE_PROXY, BrightnessProxy))
+#define BRIGHTNESS_IS_PROXY(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), BRIGHTNESS_TYPE_PROXY))
+
+typedef struct BrightnessProxyPrivate BrightnessProxyPrivate;
+
+typedef struct
+{
+    GObject         		parent;
+    BrightnessProxyPrivate     *priv;
+    
+} BrightnessProxy;
+
+typedef struct
+{
+    GObjectClass 		parent_class;
+} BrightnessProxyClass;
+
+GType        			brightness_proxy_get_type        (void) G_GNUC_CONST;
+BrightnessProxy       	       *brightness_proxy_new             (void);
+
+gboolean                        brightness_proxy_set_level       (BrightnessProxy *brightness,
+								  guint level);
+
+guint                           brightness_proxy_get_level       (BrightnessProxy *brightness);
+
+guint                           brightness_proxy_get_max_level   (BrightnessProxy *brightness) G_GNUC_PURE;
+
+gboolean                        brightness_proxy_has_hw          (BrightnessProxy *brightness) G_GNUC_PURE;
+
+void                            brightness_proxy_reload          (BrightnessProxy *brightness);
+
+G_END_DECLS
+
+#endif /* __BRIGHTNESS_PROXY_H */

Modified: xfce4-power-manager/trunk/po/POTFILES.in
===================================================================
--- xfce4-power-manager/trunk/po/POTFILES.in	2009-04-06 19:12:05 UTC (rev 7128)
+++ xfce4-power-manager/trunk/po/POTFILES.in	2009-04-07 09:16:47 UTC (rev 7129)
@@ -33,7 +33,7 @@
 libxfpm/xfpm-dbus.c
 libxfpm/xfpm-popups.c
 libxfpm/xfpm-notify.c
-panel-plugins/brightness/brightness-plugin.c
+panel-plugins/brightness/brightness-button.c
 panel-plugins/brightness/xfce4-brightness-plugin.desktop.in.in
 panel-plugins/inhibit/inhibit-plugin.c
 panel-plugins/inhibit/xfce4-inhibit-plugin.desktop.in.in

Modified: xfce4-power-manager/trunk/po/xfce4-power-manager.pot
===================================================================
--- xfce4-power-manager/trunk/po/xfce4-power-manager.pot	2009-04-06 19:12:05 UTC (rev 7128)
+++ xfce4-power-manager/trunk/po/xfce4-power-manager.pot	2009-04-07 09:16:47 UTC (rev 7129)
@@ -8,7 +8,7 @@
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2009-04-06 19:46+0200\n"
+"POT-Creation-Date: 2009-04-07 11:07+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL at ADDRESS>\n"
 "Language-Team: LANGUAGE <LL at li.org>\n"
@@ -593,16 +593,16 @@
 msgid "translator-credits"
 msgstr ""
 
-#: ../panel-plugins/brightness/brightness-plugin.c:82
+#: ../panel-plugins/brightness/brightness-button.c:173
 #: ../panel-plugins/brightness/xfce4-brightness-plugin.desktop.in.in.h:2
 msgid "Control your LCD brightness"
 msgstr ""
 
-#: ../panel-plugins/brightness/brightness-plugin.c:84
+#: ../panel-plugins/brightness/brightness-button.c:175
 msgid "No device found"
 msgstr ""
 
-#: ../panel-plugins/brightness/brightness-plugin.c:590
+#: ../panel-plugins/brightness/brightness-button.c:585
 #: ../panel-plugins/brightness/xfce4-brightness-plugin.desktop.in.in.h:1
 msgid "Brightness plugin"
 msgstr ""




More information about the Goodies-commits mailing list