~noskcaj/ubuntu/saucy/xfce4-power-manager/1.2.0-2ubuntu1

« back to all changes in this revision

Viewing changes to panel-plugins/brightness/brightness-proxy.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-09 18:28:34 UTC
  • mfrom: (2.3.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20101209182834-efb7dinmf9ssp3es
Tags: 1.0.1-0ubuntu1
Upload to natty (pkg-xfce svn r4665), no Ubuntu changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * * Copyright (C) 2009 Ali <aliov@xfce.org>
3
 
 *
4
 
 * Licensed under the GNU General Public License Version 2
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
20
 
 
21
 
#ifdef HAVE_CONFIG_H
22
 
#include <config.h>
23
 
#endif
24
 
 
25
 
#include <stdio.h>
26
 
#include <stdlib.h>
27
 
#include <string.h>
28
 
 
29
 
#include <glib.h>
30
 
 
31
 
#include <dbus/dbus-glib.h>
32
 
 
33
 
#include <libxfce4util/libxfce4util.h>
34
 
 
35
 
#include "libxfpm/hal-manager.h"
36
 
#include "libxfpm/hal-device.h"
37
 
 
38
 
#include "brightness-proxy.h"
39
 
 
40
 
static void brightness_proxy_finalize   (GObject *object);
41
 
 
42
 
#define BRIGHTNESS_PROXY_GET_PRIVATE(o) \
43
 
(G_TYPE_INSTANCE_GET_PRIVATE ((o), BRIGHTNESS_TYPE_PROXY, BrightnessProxyPrivate))
44
 
 
45
 
struct BrightnessProxyPrivate
46
 
{
47
 
    DBusGConnection *bus;
48
 
    DBusGConnection *session;
49
 
    DBusGProxy      *proxy;
50
 
    guint            max_level;
51
 
    gboolean         has_hw;
52
 
};
53
 
 
54
 
G_DEFINE_TYPE (BrightnessProxy, brightness_proxy, G_TYPE_OBJECT)
55
 
 
56
 
static void
57
 
brightness_proxy_get_device (BrightnessProxy *brightness)
58
 
{
59
 
    HalManager *manager;
60
 
    HalDevice *device;
61
 
    gchar **udis = NULL;
62
 
    
63
 
    manager = hal_manager_new ();
64
 
    
65
 
    udis = hal_manager_find_device_by_capability (manager, "laptop_panel");
66
 
    
67
 
    if (!udis || !udis[0] )
68
 
    {
69
 
        TRACE ("No laptop panel found on the system");
70
 
        brightness->priv->has_hw = FALSE;
71
 
        brightness->priv->proxy = NULL;
72
 
        goto out;
73
 
    }
74
 
    
75
 
    device = hal_device_new ();
76
 
    hal_device_set_udi (device, udis[0]);
77
 
    
78
 
    brightness->priv->max_level =
79
 
        hal_device_get_property_int (device, "laptop_panel.num_levels") -1;
80
 
        
81
 
    TRACE("Laptop panel %s with max level %d", udis[0], brightness->priv->max_level);
82
 
    
83
 
    brightness->priv->proxy = dbus_g_proxy_new_for_name (brightness->priv->bus,
84
 
                                                         "org.freedesktop.Hal",
85
 
                                                         udis[0],
86
 
                                                         "org.freedesktop.Hal.Device.LaptopPanel");
87
 
    brightness->priv->has_hw = TRUE;
88
 
    
89
 
    g_object_unref (device);
90
 
out:
91
 
    g_object_unref (manager);
92
 
}
93
 
 
94
 
static void
95
 
brightness_proxy_class_init (BrightnessProxyClass *klass)
96
 
{
97
 
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
98
 
 
99
 
    object_class->finalize = brightness_proxy_finalize;
100
 
 
101
 
    g_type_class_add_private (klass, sizeof (BrightnessProxyPrivate));
102
 
}
103
 
 
104
 
static void
105
 
brightness_proxy_init (BrightnessProxy *brightness_proxy)
106
 
{
107
 
    brightness_proxy->priv = BRIGHTNESS_PROXY_GET_PRIVATE (brightness_proxy);
108
 
    
109
 
    brightness_proxy->priv->max_level = 0;
110
 
    
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);
114
 
    
115
 
    brightness_proxy_get_device (brightness_proxy);
116
 
}
117
 
 
118
 
static void
119
 
brightness_proxy_finalize (GObject *object)
120
 
{
121
 
    BrightnessProxy *brightness_proxy;
122
 
 
123
 
    brightness_proxy = BRIGHTNESS_PROXY (object);
124
 
    
125
 
    dbus_g_connection_unref (brightness_proxy->priv->bus);
126
 
    dbus_g_connection_unref (brightness_proxy->priv->session);
127
 
    
128
 
    if ( brightness_proxy->priv->proxy )
129
 
        g_object_unref (brightness_proxy->priv->proxy);
130
 
 
131
 
    G_OBJECT_CLASS (brightness_proxy_parent_class)->finalize (object);
132
 
}
133
 
 
134
 
static void
135
 
brightness_proxy_update_xfpm_brightness_level (BrightnessProxy *brightness, guint level)
136
 
{
137
 
    DBusGProxy *proxy;
138
 
    
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");
143
 
                                               
144
 
    if ( !proxy )
145
 
    {
146
 
        g_warning ("Failed to create proxy to Xfpm");
147
 
        return;
148
 
    }
149
 
 
150
 
    dbus_g_proxy_call_no_reply (proxy, "UpdateBrightness",
151
 
                                G_TYPE_UINT, level,
152
 
                                G_TYPE_INVALID,
153
 
                                G_TYPE_INVALID);
154
 
                                
155
 
    g_object_unref ( proxy );
156
 
}
157
 
 
158
 
BrightnessProxy *
159
 
brightness_proxy_new (void)
160
 
{
161
 
    BrightnessProxy *brightness_proxy = NULL;
162
 
    brightness_proxy = g_object_new (BRIGHTNESS_TYPE_PROXY, NULL);
163
 
    return brightness_proxy;
164
 
}
165
 
 
166
 
gboolean
167
 
brightness_proxy_set_level (BrightnessProxy *brightness, guint level)
168
 
{
169
 
    GError *error = NULL;
170
 
    gboolean ret;
171
 
    gint dummy;
172
 
    
173
 
    g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), FALSE);
174
 
    
175
 
    ret = dbus_g_proxy_call (brightness->priv->proxy, "SetBrightness", &error,
176
 
                             G_TYPE_INT, level,
177
 
                             G_TYPE_INVALID,
178
 
                             G_TYPE_INT, &dummy,
179
 
                             G_TYPE_INVALID );
180
 
    if ( error )
181
 
    {
182
 
        g_critical ("Error setting brightness level: %s\n", error->message);
183
 
        g_error_free (error);
184
 
        return FALSE;
185
 
    }
186
 
        
187
 
    brightness_proxy_update_xfpm_brightness_level (brightness, level);
188
 
    
189
 
    return ret;
190
 
}
191
 
 
192
 
guint
193
 
brightness_proxy_get_level (BrightnessProxy *brightness)
194
 
{
195
 
    GError *error = NULL;
196
 
    gint level = 0;
197
 
    gboolean ret;
198
 
    
199
 
    g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), 0);
200
 
    
201
 
    ret = dbus_g_proxy_call (brightness->priv->proxy, "GetBrightness", &error,
202
 
                             G_TYPE_INVALID,
203
 
                             G_TYPE_INT, &level,
204
 
                             G_TYPE_INVALID);
205
 
 
206
 
    if ( error )
207
 
    {
208
 
        g_critical ("Error getting brightness level: %s\n", error->message);
209
 
        g_error_free (error);
210
 
    }
211
 
    return level;
212
 
}
213
 
 
214
 
guint brightness_proxy_get_max_level (BrightnessProxy *brightness)
215
 
{
216
 
    g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), 0);
217
 
    
218
 
    return brightness->priv->max_level;
219
 
}
220
 
 
221
 
gboolean brightness_proxy_has_hw (BrightnessProxy *brightness)
222
 
{
223
 
    g_return_val_if_fail (BRIGHTNESS_IS_PROXY (brightness), FALSE);
224
 
    
225
 
    return brightness->priv->has_hw;
226
 
}
227
 
 
228
 
 
229
 
void brightness_proxy_reload (BrightnessProxy *brightness)
230
 
{
231
 
    g_return_if_fail (BRIGHTNESS_IS_PROXY (brightness));
232
 
    
233
 
    if ( brightness->priv->proxy )
234
 
        g_object_unref (brightness->priv->proxy);
235
 
        
236
 
    brightness_proxy_get_device (brightness);
237
 
}