2
* * Copyright (C) 2009 Ali <aliov@xfce.org>
4
* Licensed under the GNU General Public License Version 2
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31
#include <dbus/dbus-glib.h>
33
#include <libxfce4util/libxfce4util.h>
35
#include "libxfpm/hal-manager.h"
36
#include "libxfpm/hal-device.h"
38
#include "brightness-proxy.h"
40
static void brightness_proxy_finalize (GObject *object);
42
#define BRIGHTNESS_PROXY_GET_PRIVATE(o) \
43
(G_TYPE_INSTANCE_GET_PRIVATE ((o), BRIGHTNESS_TYPE_PROXY, BrightnessProxyPrivate))
45
struct BrightnessProxyPrivate
48
DBusGConnection *session;
54
G_DEFINE_TYPE (BrightnessProxy, brightness_proxy, G_TYPE_OBJECT)
57
brightness_proxy_get_device (BrightnessProxy *brightness)
63
manager = hal_manager_new ();
65
udis = hal_manager_find_device_by_capability (manager, "laptop_panel");
67
if (!udis || !udis[0] )
69
TRACE ("No laptop panel found on the system");
70
brightness->priv->has_hw = FALSE;
71
brightness->priv->proxy = NULL;
75
device = hal_device_new ();
76
hal_device_set_udi (device, udis[0]);
78
brightness->priv->max_level =
79
hal_device_get_property_int (device, "laptop_panel.num_levels") -1;
81
TRACE("Laptop panel %s with max level %d", udis[0], brightness->priv->max_level);
83
brightness->priv->proxy = dbus_g_proxy_new_for_name (brightness->priv->bus,
84
"org.freedesktop.Hal",
86
"org.freedesktop.Hal.Device.LaptopPanel");
87
brightness->priv->has_hw = TRUE;
89
g_object_unref (device);
91
g_object_unref (manager);
95
brightness_proxy_class_init (BrightnessProxyClass *klass)
97
GObjectClass *object_class = G_OBJECT_CLASS (klass);
99
object_class->finalize = brightness_proxy_finalize;
101
g_type_class_add_private (klass, sizeof (BrightnessProxyPrivate));
105
brightness_proxy_init (BrightnessProxy *brightness_proxy)
107
brightness_proxy->priv = BRIGHTNESS_PROXY_GET_PRIVATE (brightness_proxy);
109
brightness_proxy->priv->max_level = 0;
111
// FIXME, Don't connect blindly
112
brightness_proxy->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
113
brightness_proxy->priv->session = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
115
brightness_proxy_get_device (brightness_proxy);
119
brightness_proxy_finalize (GObject *object)
121
BrightnessProxy *brightness_proxy;
123
brightness_proxy = BRIGHTNESS_PROXY (object);
125
dbus_g_connection_unref (brightness_proxy->priv->bus);
126
dbus_g_connection_unref (brightness_proxy->priv->session);
128
if ( brightness_proxy->priv->proxy )
129
g_object_unref (brightness_proxy->priv->proxy);
131
G_OBJECT_CLASS (brightness_proxy_parent_class)->finalize (object);
135
brightness_proxy_update_xfpm_brightness_level (BrightnessProxy *brightness, guint level)
139
proxy = dbus_g_proxy_new_for_name (brightness->priv->session,
140
"org.freedesktop.PowerManagement",
141
"/org/freedesktop/PowerManagement/Backlight",
142
"org.freedesktop.PowerManagement.Backlight");
146
g_warning ("Failed to create proxy to Xfpm");
150
dbus_g_proxy_call_no_reply (proxy, "UpdateBrightness",
155
g_object_unref ( proxy );
159
brightness_proxy_new (void)
161
BrightnessProxy *brightness_proxy = NULL;
162
brightness_proxy = g_object_new (BRIGHTNESS_TYPE_PROXY, NULL);
163
return brightness_proxy;
167
brightness_proxy_set_level (BrightnessProxy *brightness, guint level)
169
GError *error = NULL;
173
g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), FALSE);
175
ret = dbus_g_proxy_call (brightness->priv->proxy, "SetBrightness", &error,
182
g_critical ("Error setting brightness level: %s\n", error->message);
183
g_error_free (error);
187
brightness_proxy_update_xfpm_brightness_level (brightness, level);
193
brightness_proxy_get_level (BrightnessProxy *brightness)
195
GError *error = NULL;
199
g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), 0);
201
ret = dbus_g_proxy_call (brightness->priv->proxy, "GetBrightness", &error,
208
g_critical ("Error getting brightness level: %s\n", error->message);
209
g_error_free (error);
214
guint brightness_proxy_get_max_level (BrightnessProxy *brightness)
216
g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), 0);
218
return brightness->priv->max_level;
221
gboolean brightness_proxy_has_hw (BrightnessProxy *brightness)
223
g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), FALSE);
225
return brightness->priv->has_hw;
229
void brightness_proxy_reload (BrightnessProxy *brightness)
231
g_return_if_fail (BRIGHTNESS_IS_PROXY (brightness));
233
if ( brightness->priv->proxy )
234
g_object_unref (brightness->priv->proxy);
236
brightness_proxy_get_device (brightness);