1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/*
* * Copyright (C) 2009 Ali <aliov@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 <gtk/gtk.h>
#include <libxfce4util/libxfce4util.h>
#include "xfpm-backlight.h"
#include "xfpm-idle.h"
#include "xfpm-notify.h"
#include "xfpm-xfconf.h"
#include "xfpm-dkp.h"
#include "xfpm-config.h"
#include "xfpm-button.h"
#include "xfpm-brightness.h"
static void xfpm_backlight_finalize (GObject *object);
#define ALARM_DISABLED 9
#define BRIGHTNESS_POPUP_SIZE 180
#define XFPM_BACKLIGHT_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_BACKLIGHT, XfpmBacklightPrivate))
struct XfpmBacklightPrivate
{
XfpmBrightness *brightness;
XfpmDkp *dkp;
XfpmIdle *idle;
XfpmXfconf *conf;
XfpmButton *button;
XfpmNotify *notify;
GtkWidget *window;
GtkWidget *progress_bar;
NotifyNotification *n;
gulong timeout_id;
gulong destroy_id;
gboolean has_hw;
gboolean on_battery;
guint last_level;
guint max_level;
#ifdef WITH_HAL
gboolean brightness_in_hw;
#endif
};
G_DEFINE_TYPE (XfpmBacklight, xfpm_backlight, G_TYPE_OBJECT)
static void
xfpm_backlight_dim_brightness (XfpmBacklight *backlight)
{
gboolean ret;
ret = xfpm_brightness_get_level (backlight->priv->brightness, &backlight->priv->last_level);
if ( !ret )
{
g_warning ("Unable to get current brightness level");
return;
}
TRACE ("Current brightness level : %u", brightness->priv->last_level);
xfpm_brightness_dim_down (backlight->priv->brightness);
}
static gboolean
xfpm_backlight_destroy_popup (gpointer data)
{
XfpmBacklight *backlight;
backlight = XFPM_BACKLIGHT (data);
if ( backlight->priv->window )
{
gtk_widget_destroy (backlight->priv->window);
backlight->priv->window = NULL;
}
if ( backlight->priv->n )
{
g_object_unref (backlight->priv->n);
backlight->priv->n = NULL;
}
return FALSE;
}
static void
xfpm_backlight_show_notification (XfpmBacklight *backlight, guint level, guint max_level)
{
guint i;
gfloat value = 0;
NotifyNotification *n;
static const char *display_icon_name[] =
{
"notification-display-brightness-off",
"notification-display-brightness-low",
"notification-display-brightness-medium",
"notification-display-brightness-high",
"notification-display-brightness-full",
NULL
};
if ( !backlight->priv->n )
{
n = xfpm_notify_new_notification (backlight->priv->notify,
NULL,
NULL,
NULL,
0,
XFPM_NOTIFY_NORMAL,
NULL);
}
value = (gfloat) 100 * level / max_level;
i = (gint)value / 25;
notify_notification_set_hint_int32 (n,
"value",
value);
notify_notification_set_hint_string (n,
"x-canonical-private-synchronous",
"brightness");
notify_notification_update (n,
" ",
"",
display_icon_name[i]);
notify_notification_show (n, NULL);
backlight->priv->n = n;
}
static gboolean
xfpm_backlight_hide_popup_timeout (XfpmBacklight *backlight)
{
gtk_widget_hide (backlight->priv->window);
return FALSE;
}
static void
xfpm_backlight_create_popup (XfpmBacklight *backlight)
{
GtkWidget *vbox;
GtkWidget *img;
GtkWidget *align;
GtkObject *adj;
if ( backlight->priv->window != NULL )
return;
backlight->priv->window = gtk_window_new (GTK_WINDOW_POPUP);
g_object_set (G_OBJECT (backlight->priv->window),
"window-position", GTK_WIN_POS_CENTER_ALWAYS,
"decorated", FALSE,
"resizable", FALSE,
"type-hint", GDK_WINDOW_TYPE_HINT_UTILITY,
"app-paintable", TRUE,
NULL);
gtk_window_set_default_size (GTK_WINDOW (backlight->priv->window), BRIGHTNESS_POPUP_SIZE, BRIGHTNESS_POPUP_SIZE);
align = gtk_alignment_new (0., 0.5, 0, 0);
gtk_alignment_set_padding (GTK_ALIGNMENT (align), 5, 5, 5, 5);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (backlight->priv->window), align);
gtk_container_add (GTK_CONTAINER (align), vbox);
img = gtk_image_new_from_icon_name ("xfpm-brightness-lcd", GTK_ICON_SIZE_DIALOG);
gtk_box_pack_start (GTK_BOX (vbox), img, TRUE, TRUE, 0);
backlight->priv->progress_bar = gtk_progress_bar_new ();
adj = gtk_adjustment_new (0., 0., backlight->priv->max_level, 1., 0., 0.);
g_object_set (G_OBJECT (backlight->priv->progress_bar),
"adjustment", adj,
NULL);
gtk_box_pack_start (GTK_BOX (vbox), backlight->priv->progress_bar, TRUE, TRUE, 0);
gtk_widget_show_all (align);
}
static void
xfpm_backlight_show (XfpmBacklight *backlight, guint level)
{
gboolean sync;
gboolean show_popup;
g_debug ("Level %u", level);
g_object_get (G_OBJECT (backlight->priv->conf),
SHOW_BRIGHTNESS_POPUP, &show_popup,
NULL);
if ( !show_popup )
goto out;
g_object_get (G_OBJECT (backlight->priv->notify),
"sync", &sync,
NULL);
if ( sync )
{
xfpm_backlight_show_notification (backlight, level, backlight->priv->max_level);
}
else
{
GtkAdjustment *adj;
xfpm_backlight_create_popup (backlight);
g_object_get (G_OBJECT (backlight->priv->progress_bar),
"adjustment", &adj,
NULL);
gtk_adjustment_set_value (adj, level);
if ( !GTK_WIDGET_VISIBLE (backlight->priv->window))
gtk_window_present (GTK_WINDOW (backlight->priv->window));
if ( backlight->priv->timeout_id != 0 )
g_source_remove (backlight->priv->timeout_id);
backlight->priv->timeout_id =
g_timeout_add (900, (GSourceFunc) xfpm_backlight_hide_popup_timeout, backlight);
}
if ( backlight->priv->destroy_id != 0 )
{
g_source_remove (backlight->priv->destroy_id);
backlight->priv->destroy_id = 0;
}
out:
/* Release the memory after 60 seconds */
backlight->priv->destroy_id = g_timeout_add_seconds (60, (GSourceFunc) xfpm_backlight_destroy_popup, backlight);
}
static void
xfpm_backlight_alarm_timeout_cb (XfpmIdle *idle, guint id, XfpmBacklight *backlight)
{
if ( id == TIMEOUT_BRIGHTNESS_ON_AC && !backlight->priv->on_battery)
xfpm_backlight_dim_brightness (backlight);
else if ( id == TIMEOUT_BRIGHTNESS_ON_BATTERY && backlight->priv->on_battery)
xfpm_backlight_dim_brightness (backlight);
}
static void
xfpm_backlight_reset_cb (XfpmIdle *idle, XfpmBacklight *backlight)
{
xfpm_brightness_set_level (backlight->priv->brightness, backlight->priv->last_level);
}
static void
xfpm_backlight_button_pressed_cb (XfpmButton *button, XfpmButtonKey type, XfpmBacklight *backlight)
{
guint level;
gboolean ret = TRUE;
gboolean enable_brightness;
g_object_get (G_OBJECT (backlight->priv->conf),
ENABLE_BRIGHTNESS_CONTROL, &enable_brightness,
NULL);
if ( type == BUTTON_MON_BRIGHTNESS_UP )
{
#ifdef WITH_HAL
if ( !backlight->priv->brightness_in_hw && enable_brightness)
ret = xfpm_brightness_up (backlight->priv->brightness, &level);
#else
if ( enable_brightness )
ret = xfpm_brightness_up (backlight->priv->brightness, &level);
#endif
if ( ret )
xfpm_backlight_show (backlight, level);
}
else if ( type == BUTTON_MON_BRIGHTNESS_DOWN )
{
#ifdef WITH_HAL
if ( !backlight->priv->brightness_in_hw && enable_brightness )
ret = xfpm_brightness_down (backlight->priv->brightness, &level);
#else
if ( enable_brightness )
ret = xfpm_brightness_down (backlight->priv->brightness, &level);
#endif
if ( ret )
xfpm_backlight_show (backlight, level);
}
}
static void
xfpm_backlight_brightness_on_ac_settings_changed (XfpmBacklight *backlight)
{
guint timeout_on_ac;
g_object_get (G_OBJECT (backlight->priv->conf),
BRIGHTNESS_ON_AC, &timeout_on_ac,
NULL);
if ( timeout_on_ac == ALARM_DISABLED )
{
xfpm_idle_free_alarm (backlight->priv->idle, TIMEOUT_BRIGHTNESS_ON_AC );
}
else
{
xfpm_idle_set_alarm (backlight->priv->idle, TIMEOUT_BRIGHTNESS_ON_AC, timeout_on_ac * 1000);
}
}
static void
xfpm_backlight_brightness_on_battery_settings_changed (XfpmBacklight *backlight)
{
guint timeout_on_battery ;
g_object_get (G_OBJECT (backlight->priv->conf),
BRIGHTNESS_ON_BATTERY, &timeout_on_battery,
NULL);
if ( timeout_on_battery == ALARM_DISABLED )
{
xfpm_idle_free_alarm (backlight->priv->idle, TIMEOUT_BRIGHTNESS_ON_BATTERY );
}
else
{
xfpm_idle_set_alarm (backlight->priv->idle, TIMEOUT_BRIGHTNESS_ON_BATTERY, timeout_on_battery * 1000);
}
}
static void
xfpm_backlight_set_timeouts (XfpmBacklight *backlight)
{
xfpm_backlight_brightness_on_ac_settings_changed (backlight);
xfpm_backlight_brightness_on_battery_settings_changed (backlight);
}
static void
xfpm_backlight_on_battery_changed_cb (XfpmDkp *dkp, gboolean on_battery, XfpmBacklight *backlight)
{
backlight->priv->on_battery = on_battery;
}
static void
xfpm_backlight_class_init (XfpmBacklightClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = xfpm_backlight_finalize;
g_type_class_add_private (klass, sizeof (XfpmBacklightPrivate));
}
static void
xfpm_backlight_init (XfpmBacklight *backlight)
{
backlight->priv = XFPM_BACKLIGHT_GET_PRIVATE (backlight);
backlight->priv->brightness = xfpm_brightness_new ();
backlight->priv->has_hw = xfpm_brightness_setup (backlight->priv->brightness);
backlight->priv->notify = NULL;
backlight->priv->idle = NULL;
backlight->priv->conf = NULL;
backlight->priv->button = NULL;
backlight->priv->dkp = NULL;
backlight->priv->window = NULL;
backlight->priv->progress_bar = NULL;
backlight->priv->destroy_id = 0;
backlight->priv->timeout_id = 0;
if ( !backlight->priv->has_hw )
{
g_object_unref (backlight->priv->brightness);
backlight->priv->brightness = NULL;
}
else
{
backlight->priv->idle = xfpm_idle_new ();
backlight->priv->conf = xfpm_xfconf_new ();
backlight->priv->button = xfpm_button_new ();
backlight->priv->dkp = xfpm_dkp_get ();
backlight->priv->notify = xfpm_notify_new ();
backlight->priv->max_level = xfpm_brightness_get_max_level (backlight->priv->brightness);
#ifdef WITH_HAL
if ( xfpm_brightness_get_control (backlight->priv->brightness) == XFPM_BRIGHTNESS_CONTROL_HAL )
backlight->priv->brightness_in_hw = xfpm_brightness_in_hw (backlight->priv->brightness);
#endif
g_signal_connect (backlight->priv->idle, "alarm-timeout",
G_CALLBACK (xfpm_backlight_alarm_timeout_cb), backlight);
g_signal_connect (backlight->priv->idle, "reset",
G_CALLBACK(xfpm_backlight_reset_cb), backlight);
g_signal_connect (backlight->priv->button, "button-pressed",
G_CALLBACK (xfpm_backlight_button_pressed_cb), backlight);
g_signal_connect_swapped (backlight->priv->conf, "notify::" BRIGHTNESS_ON_AC,
G_CALLBACK (xfpm_backlight_brightness_on_ac_settings_changed), backlight);
g_signal_connect_swapped (backlight->priv->conf, "notify::" BRIGHTNESS_ON_BATTERY,
G_CALLBACK (xfpm_backlight_brightness_on_battery_settings_changed), backlight);
g_signal_connect (backlight->priv->dkp, "on-battery-changed",
G_CALLBACK (xfpm_backlight_on_battery_changed_cb), backlight);
g_object_get (G_OBJECT (backlight->priv->dkp),
"on-battery", &backlight->priv->on_battery,
NULL);
xfpm_backlight_set_timeouts (backlight);
}
}
static void
xfpm_backlight_finalize (GObject *object)
{
XfpmBacklight *backlight;
backlight = XFPM_BACKLIGHT (object);
xfpm_backlight_destroy_popup (backlight);
if ( backlight->priv->brightness )
g_object_unref (backlight->priv->brightness);
if ( backlight->priv->idle )
g_object_unref (backlight->priv->idle);
if ( backlight->priv->conf )
g_object_unref (backlight->priv->conf);
if ( backlight->priv->button )
g_object_unref (backlight->priv->button);
if ( backlight->priv->dkp )
g_object_unref (backlight->priv->dkp);
if ( backlight->priv->notify)
g_object_unref (backlight->priv->notify);
G_OBJECT_CLASS (xfpm_backlight_parent_class)->finalize (object);
}
XfpmBacklight *
xfpm_backlight_new (void)
{
XfpmBacklight *backlight = NULL;
backlight = g_object_new (XFPM_TYPE_BACKLIGHT, NULL);
return backlight;
}
gboolean xfpm_backlight_has_hw (XfpmBacklight *backlight)
{
return backlight->priv->has_hw;
}
|