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

« back to all changes in this revision

Viewing changes to src/xfpm-engine.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) 2008-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 <libxfce4util/libxfce4util.h>
32
 
 
33
 
#include "libxfpm/hal-manager.h"
34
 
#include "libxfpm/xfpm-string.h"
35
 
#include "libxfpm/xfpm-common.h"
36
 
#include "libxfpm/xfpm-notify.h"
37
 
 
38
 
#ifdef HAVE_DPMS
39
 
#include "xfpm-dpms.h"
40
 
#endif
41
 
 
42
 
#include "xfpm-engine.h"
43
 
#include "xfpm-supply.h"
44
 
#include "xfpm-adapter.h"
45
 
#include "xfpm-xfconf.h"
46
 
#include "xfpm-cpu.h"
47
 
#include "xfpm-button.h"
48
 
#include "xfpm-inhibit.h"
49
 
#include "xfpm-backlight.h"
50
 
#include "xfpm-screen-saver.h"
51
 
#include "xfpm-shutdown.h"
52
 
#include "xfpm-button-hal.h"
53
 
#include "egg-idletime.h"
54
 
#include "xfpm-errors.h"
55
 
#include "xfpm-config.h"
56
 
#include "xfpm-enum-types.h"
57
 
#include "xfpm-debug.h"
58
 
 
59
 
static void xfpm_engine_finalize (GObject * object);
60
 
 
61
 
static void xfpm_engine_dbus_class_init (XfpmEngineClass * klass);
62
 
static void xfpm_engine_dbus_init (XfpmEngine * engine);
63
 
 
64
 
#define XFPM_ENGINE_GET_PRIVATE(o) \
65
 
(G_TYPE_INSTANCE_GET_PRIVATE((o), XFPM_TYPE_ENGINE, XfpmEnginePrivate))
66
 
 
67
 
struct XfpmEnginePrivate
68
 
{
69
 
    XfpmXfconf          *conf;
70
 
    XfpmSupply          *supply;
71
 
    XfpmNotify          *notify;
72
 
#ifdef SYSTEM_IS_LINUX
73
 
    XfpmCpu             *cpu;
74
 
#endif
75
 
    XfpmBacklight       *bk;
76
 
    XfpmAdapter         *adapter;
77
 
    XfpmInhibit         *inhibit;
78
 
    XfpmShutdown        *shutdown;
79
 
    XfpmButton          *button;
80
 
    XfpmButtonHal       *bt_hal;
81
 
    EggIdletime         *idle;
82
 
    XfpmScreenSaver     *srv;
83
 
#ifdef HAVE_DPMS
84
 
    XfpmDpms *dpms;
85
 
#endif
86
 
    gboolean inhibited;
87
 
 
88
 
    guint8 power_management;
89
 
    gboolean on_battery;
90
 
    gboolean is_laptop;
91
 
 
92
 
    gboolean has_lcd_brightness;
93
 
    
94
 
};
95
 
 
96
 
enum
97
 
{
98
 
    ON_BATTERY_CHANGED,
99
 
    LOW_BATTERY_CHANGED,
100
 
    LAST_SIGNAL
101
 
};
102
 
 
103
 
static guint signals [LAST_SIGNAL] = { 0 };
104
 
 
105
 
G_DEFINE_TYPE (XfpmEngine, xfpm_engine, G_TYPE_OBJECT)
106
 
 
107
 
static gboolean xfpm_engine_do_suspend (XfpmEngine * engine)
108
 
{
109
 
    GError *error = NULL;
110
 
 
111
 
    xfpm_suspend (engine->priv->shutdown, &error);
112
 
 
113
 
    if (error)
114
 
    {
115
 
        g_warning ("%s", error->message);
116
 
        g_error_free (error);
117
 
    }
118
 
    return FALSE;
119
 
}
120
 
 
121
 
static gboolean
122
 
xfpm_engine_do_hibernate (XfpmEngine * engine)
123
 
{
124
 
    GError *error = NULL;
125
 
 
126
 
    xfpm_hibernate (engine->priv->shutdown, &error);
127
 
 
128
 
    if (error)
129
 
    {
130
 
        g_warning ("%s", error->message);
131
 
        g_error_free (error);
132
 
    }
133
 
    return FALSE;
134
 
}
135
 
 
136
 
static gboolean
137
 
xfpm_engine_do_shutdown (XfpmEngine * engine)
138
 
{
139
 
    GError *error = NULL;
140
 
 
141
 
    xfpm_shutdown (engine->priv->shutdown, &error);
142
 
 
143
 
    if (error)
144
 
    {
145
 
        g_warning ("%s", error->message);
146
 
        g_error_free (error);
147
 
    }
148
 
    return FALSE;
149
 
}
150
 
 
151
 
/*
152
 
 * Map of int to strings shutdown values
153
 
 */
154
 
static const gchar * G_GNUC_PURE
155
 
xfpm_int_to_shutdown_string (gint val)
156
 
{
157
 
    if ( val == XFPM_DO_NOTHING )
158
 
        return "Nothing";
159
 
    else if ( val == XFPM_DO_SUSPEND)
160
 
        return "Suspend";
161
 
    else if ( val == XFPM_DO_HIBERNATE)
162
 
        return "Hibernate";
163
 
    else if ( val == XFPM_DO_SHUTDOWN)
164
 
        return "Shutdown";
165
 
    else if ( val == XFPM_ASK)
166
 
        return "Ask";
167
 
    
168
 
    return "Invalid";
169
 
}
170
 
 
171
 
static void
172
 
xfpm_engine_shutdown_request (XfpmEngine * engine,
173
 
                              XfpmShutdownRequest shutdown, gboolean critical)
174
 
{
175
 
    gboolean lock_screen;
176
 
    const gchar *action = xfpm_int_to_shutdown_string (shutdown);
177
 
 
178
 
    g_object_get (G_OBJECT (engine->priv->conf),
179
 
                  LOCK_SCREEN_ON_SLEEP, &lock_screen,
180
 
                  NULL);
181
 
 
182
 
    if (xfpm_strequal (action, "Nothing"))
183
 
    {
184
 
        TRACE ("Button is disabled in configuration");
185
 
        return;
186
 
    }
187
 
    else if ( xfpm_strequal (action, "Ask") && xfpm_strequal (action, "Invalid"))
188
 
    {
189
 
        g_warning ("Invalid configuration action %s", action);
190
 
        return;
191
 
    }
192
 
    else if ( engine->priv->inhibited == TRUE && critical == FALSE )
193
 
    {
194
 
        return;
195
 
    }
196
 
    else
197
 
    {
198
 
        TRACE ("Going to do %s\n", action);
199
 
 
200
 
        if (shutdown == XFPM_DO_SHUTDOWN)
201
 
        {
202
 
            xfpm_engine_do_shutdown (engine);
203
 
        }
204
 
        else if (shutdown == XFPM_DO_HIBERNATE)
205
 
        {
206
 
            xfpm_shutdown_add_callback (engine->priv->shutdown, 
207
 
                                        (GSourceFunc) xfpm_engine_do_hibernate,
208
 
                                        lock_screen,
209
 
                                        engine);
210
 
        }
211
 
        else if (shutdown == XFPM_DO_SUSPEND)
212
 
        {
213
 
            xfpm_shutdown_add_callback (engine->priv->shutdown, 
214
 
                                        (GSourceFunc) xfpm_engine_do_suspend,
215
 
                                        lock_screen,
216
 
                                        engine);
217
 
        }
218
 
 
219
 
    }
220
 
}
221
 
 
222
 
static void
223
 
xfpm_engine_shutdown_request_battery_cb (XfpmSupply * supply,
224
 
                                         gboolean critical,
225
 
                                         XfpmShutdownRequest action,
226
 
                                         XfpmEngine * engine)
227
 
{
228
 
    if ( engine->priv->inhibited && critical == FALSE ) /* We can ignore the request here */
229
 
        return;
230
 
        
231
 
    xfpm_engine_shutdown_request (engine, action, critical);
232
 
}
233
 
 
234
 
static void
235
 
xfpm_engine_lid_event (XfpmButtonHal *bt_hal, gboolean pressed, XfpmEngine *engine)
236
 
{
237
 
    XfpmLidTriggerAction action;
238
 
    
239
 
    g_object_get (G_OBJECT (engine->priv->conf),
240
 
                      engine->priv->on_battery ? LID_SWITCH_ON_BATTERY_CFG : LID_SWITCH_ON_AC_CFG, &action,
241
 
                      NULL);
242
 
 
243
 
    if ( pressed )
244
 
    {
245
 
        XFPM_DEBUG_ENUM ("LID close event", action, XFPM_TYPE_LID_TRIGGER_ACTION);
246
 
        
247
 
        if ( action == LID_TRIGGER_NOTHING )
248
 
        {
249
 
            if ( !xfpm_is_multihead_connected () )
250
 
                xfpm_dpms_force_level (engine->priv->dpms, DPMSModeOff);
251
 
        }
252
 
        else if ( action == LID_TRIGGER_LOCK_SCREEN )
253
 
        {
254
 
            if ( !xfpm_is_multihead_connected () )
255
 
                xfpm_lock_screen ();
256
 
        }
257
 
        else 
258
 
            xfpm_engine_shutdown_request (engine, action, FALSE);
259
 
    }
260
 
    else
261
 
    {
262
 
        XFPM_DEBUG_ENUM ("LID opened", action, XFPM_TYPE_LID_TRIGGER_ACTION);
263
 
        xfpm_dpms_force_level (engine->priv->dpms, DPMSModeOn);
264
 
    }
265
 
}
266
 
 
267
 
static void
268
 
xfpm_engine_button_pressed_cb (XfpmButton *button,
269
 
                               XfpmButtonKey type, XfpmEngine * engine)
270
 
{
271
 
    XfpmShutdownRequest req = XFPM_DO_NOTHING;
272
 
    
273
 
    XFPM_DEBUG_ENUM ("Received button press event", type, XFPM_TYPE_BUTTON_KEY);
274
 
  
275
 
    if ( engine->priv->inhibited )
276
 
    {
277
 
        TRACE("Power manager automatic sleep is currently disabled");
278
 
        return;
279
 
    }
280
 
    
281
 
    if ( type == BUTTON_MON_BRIGHTNESS_DOWN || type == BUTTON_MON_BRIGHTNESS_UP )
282
 
        return;
283
 
    
284
 
    if ( type == BUTTON_POWER_OFF )
285
 
    {
286
 
        g_object_get (G_OBJECT (engine->priv->conf),
287
 
                      POWER_SWITCH_CFG, &req,
288
 
                      NULL);
289
 
    }
290
 
    else if ( type == BUTTON_SLEEP )
291
 
    {
292
 
        g_object_get (G_OBJECT (engine->priv->conf),
293
 
                      SLEEP_SWITCH_CFG, &req,
294
 
                      NULL);
295
 
    }
296
 
    else if ( type == BUTTON_HIBERNATE )
297
 
    {
298
 
        g_object_get (G_OBJECT (engine->priv->conf),
299
 
                      HIBERNATE_SWITCH_CFG, &req,
300
 
                      NULL);
301
 
    }
302
 
    else
303
 
    {
304
 
        g_return_if_reached ();
305
 
    }
306
 
 
307
 
    XFPM_DEBUG_ENUM ("Shutdown request : ", req, XFPM_TYPE_SHUTDOWN_REQUEST);
308
 
        
309
 
    if ( req == XFPM_ASK )
310
 
        xfpm_shutdown_ask (engine->priv->shutdown);
311
 
    else
312
 
        xfpm_engine_shutdown_request (engine, req, FALSE);
313
 
}
314
 
 
315
 
static void
316
 
xfpm_engine_check_hal_iface (XfpmEngine * engine)
317
 
{
318
 
    gboolean can_suspend, can_hibernate, caller;
319
 
 
320
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
321
 
                  "caller-privilege", &caller,
322
 
                  "can-suspend", &can_suspend,
323
 
                  "can-hibernate", &can_hibernate,
324
 
                  NULL);
325
 
 
326
 
    if ( caller )
327
 
    {
328
 
        if (can_hibernate)
329
 
            engine->priv->power_management |= SYSTEM_CAN_HIBERNATE;
330
 
        if (can_suspend)
331
 
            engine->priv->power_management |= SYSTEM_CAN_SUSPEND;
332
 
    }
333
 
 
334
 
  //FIXME: Show errors here
335
 
}
336
 
 
337
 
static void
338
 
xfpm_engine_supply_notify_cb (GObject *object, GParamSpec *spec, XfpmEngine *engine)
339
 
{
340
 
    gboolean low_power;
341
 
    
342
 
    if ( xfpm_strequal (spec->name, "on-low-battery") )
343
 
    {
344
 
        g_object_get (object, "on-low-battery", &low_power, NULL);
345
 
        TRACE ("On low battery changed %s", xfpm_bool_to_string (low_power));
346
 
        g_signal_emit (G_OBJECT (engine), signals [LOW_BATTERY_CHANGED], 0, low_power);
347
 
    }
348
 
}
349
 
 
350
 
static gboolean
351
 
xfpm_engine_load_all (gpointer data)
352
 
{
353
 
    XfpmEngine *engine;
354
 
    HalManager *manager;
355
 
 
356
 
    engine = XFPM_ENGINE (data);
357
 
 
358
 
    xfpm_engine_check_hal_iface (engine);
359
 
    
360
 
    manager = hal_manager_new ();
361
 
 
362
 
    if ( hal_manager_get_is_laptop (manager))
363
 
        engine->priv->is_laptop = TRUE;
364
 
    else
365
 
        engine->priv->is_laptop = FALSE;
366
 
 
367
 
    g_object_unref (manager);
368
 
 
369
 
#ifdef HAVE_DPMS
370
 
    engine->priv->dpms = xfpm_dpms_new ();
371
 
#endif
372
 
 
373
 
#ifdef SYSTEM_IS_LINUX
374
 
    if (engine->priv->is_laptop)
375
 
        engine->priv->cpu = xfpm_cpu_new ();
376
 
#endif
377
 
 
378
 
    engine->priv->supply = xfpm_supply_new (engine->priv->power_management);
379
 
    g_signal_connect (G_OBJECT (engine->priv->supply), "shutdown-request",
380
 
                      G_CALLBACK (xfpm_engine_shutdown_request_battery_cb),
381
 
                      engine);
382
 
                      
383
 
    g_signal_connect (G_OBJECT (engine->priv->supply), "notify",
384
 
                      G_CALLBACK (xfpm_engine_supply_notify_cb), engine);
385
 
                      
386
 
    xfpm_supply_monitor (engine->priv->supply);
387
 
 
388
 
    engine->priv->button = xfpm_button_new ();
389
 
 
390
 
    g_signal_connect (engine->priv->button, "button-pressed",
391
 
                      G_CALLBACK (xfpm_engine_button_pressed_cb), engine);
392
 
 
393
 
    engine->priv->bt_hal = xfpm_button_hal_get ();
394
 
    g_signal_connect (engine->priv->bt_hal, "lid_event",
395
 
                      G_CALLBACK (xfpm_engine_lid_event), engine);
396
 
  /*
397
 
   * Brightness HAL
398
 
   */
399
 
    if (engine->priv->is_laptop)
400
 
    {
401
 
        engine->priv->bk = xfpm_backlight_new ();
402
 
        engine->priv->has_lcd_brightness =
403
 
        xfpm_backlight_has_hw (engine->priv->bk);
404
 
    }
405
 
    return FALSE;
406
 
}
407
 
 
408
 
static void
409
 
xfpm_engine_adapter_changed_cb (XfpmAdapter * adapter, gboolean present,
410
 
                                XfpmEngine * engine)
411
 
{
412
 
    engine->priv->on_battery = !present;
413
 
    g_signal_emit (G_OBJECT (engine), signals [ON_BATTERY_CHANGED], 0, engine->priv->on_battery);
414
 
    egg_idletime_alarm_reset_all (engine->priv->idle);
415
 
}
416
 
 
417
 
static void
418
 
xfpm_engine_inhibit_changed_cb (XfpmInhibit * inhibit, gboolean inhibited,
419
 
                                XfpmEngine * engine)
420
 
{
421
 
    engine->priv->inhibited = inhibited;
422
 
}
423
 
 
424
 
static void
425
 
xfpm_engine_set_inactivity_timeouts (XfpmEngine *engine)
426
 
{
427
 
    guint on_ac, on_battery;
428
 
    
429
 
    g_object_get (G_OBJECT (engine->priv->conf),
430
 
                  ON_AC_INACTIVITY_TIMEOUT, &on_ac,
431
 
                  ON_BATTERY_INACTIVITY_TIMEOUT, &on_battery,
432
 
                  NULL);
433
 
    
434
 
#ifdef DEBUG
435
 
    if ( on_ac == 14 )
436
 
        TRACE ("setting inactivity sleep timeout on ac to never");
437
 
    else
438
 
        TRACE ("setting inactivity sleep timeout on ac to %d", on_ac);
439
 
    if ( on_battery == 14 )
440
 
        TRACE ("setting inactivity sleep timeout on battery to never");
441
 
    else
442
 
        TRACE ("setting inactivity sleep timeout on battery to %d", on_battery);
443
 
#endif
444
 
    
445
 
    if ( on_ac == 14 )
446
 
    {
447
 
        egg_idletime_alarm_remove (engine->priv->idle, TIMEOUT_INACTIVITY_ON_AC );
448
 
    }
449
 
    else
450
 
    {
451
 
        egg_idletime_alarm_set (engine->priv->idle, TIMEOUT_INACTIVITY_ON_AC, on_ac * 1000 * 60);
452
 
    }
453
 
    
454
 
    if ( on_battery == 14 )
455
 
    {
456
 
        egg_idletime_alarm_remove (engine->priv->idle, TIMEOUT_INACTIVITY_ON_BATTERY );
457
 
    }
458
 
    else
459
 
    {
460
 
        egg_idletime_alarm_set (engine->priv->idle, TIMEOUT_INACTIVITY_ON_BATTERY, on_battery * 1000 * 60);
461
 
    }
462
 
}
463
 
 
464
 
static void
465
 
xfpm_engine_alarm_timeout_cb (EggIdletime *idle, guint id, XfpmEngine *engine)
466
 
{
467
 
    XfpmShutdownRequest req = XFPM_DO_NOTHING;
468
 
    gchar *sleep_mode;
469
 
    gboolean saver;
470
 
    
471
 
    TRACE ("Alarm inactivity timeout id %d", id);
472
 
    
473
 
    if ( engine->priv->inhibited )
474
 
    {
475
 
        TRACE ("Power manager is currently inhibited");
476
 
        return;
477
 
    }
478
 
    
479
 
    saver = xfpm_screen_saver_get_inhibit (engine->priv->srv);
480
 
    
481
 
    if ( saver )
482
 
    {
483
 
        TRACE ("User disabled sleep on the context menu");
484
 
        return;
485
 
    }
486
 
    
487
 
    g_object_get (G_OBJECT (engine->priv->conf),
488
 
                  INACTIVITY_SLEEP_MODE, &sleep_mode,
489
 
                  NULL);
490
 
                  
491
 
    if ( !g_strcmp0 (sleep_mode, "Suspend") )
492
 
        req = XFPM_DO_SUSPEND;
493
 
    else
494
 
        req = XFPM_DO_HIBERNATE;
495
 
        
496
 
    g_free (sleep_mode);
497
 
 
498
 
    if ( id == TIMEOUT_INACTIVITY_ON_AC && engine->priv->on_battery == FALSE )
499
 
        xfpm_engine_shutdown_request (engine, req, FALSE);
500
 
    else if ( id ==  TIMEOUT_INACTIVITY_ON_BATTERY && engine->priv->is_laptop && engine->priv->on_battery  )
501
 
        xfpm_engine_shutdown_request (engine, req, FALSE);
502
 
    
503
 
}
504
 
 
505
 
static void
506
 
xfpm_engine_settings_changed_cb (GObject *obj, GParamSpec *spec, XfpmEngine *engine)
507
 
{
508
 
    if ( g_str_has_prefix (spec->name, "inactivity") )
509
 
        xfpm_engine_set_inactivity_timeouts (engine);
510
 
}
511
 
 
512
 
static void
513
 
xfpm_engine_class_init (XfpmEngineClass * klass)
514
 
{
515
 
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
516
 
 
517
 
    signals [ON_BATTERY_CHANGED] = 
518
 
        g_signal_new("on-battery-changed",
519
 
                      XFPM_TYPE_ENGINE,
520
 
                      G_SIGNAL_RUN_LAST,
521
 
                      G_STRUCT_OFFSET(XfpmEngineClass, on_battery_changed),
522
 
                      NULL, NULL,
523
 
                      g_cclosure_marshal_VOID__BOOLEAN,
524
 
                      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
525
 
 
526
 
    signals [LOW_BATTERY_CHANGED] = 
527
 
        g_signal_new("low-battery-changed",
528
 
                      XFPM_TYPE_ENGINE,
529
 
                      G_SIGNAL_RUN_LAST,
530
 
                      G_STRUCT_OFFSET(XfpmEngineClass, low_battery_changed),
531
 
                      NULL, NULL,
532
 
                      g_cclosure_marshal_VOID__BOOLEAN,
533
 
                      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
534
 
 
535
 
    object_class->finalize = xfpm_engine_finalize;
536
 
 
537
 
    g_type_class_add_private (klass, sizeof (XfpmEnginePrivate));
538
 
 
539
 
    xfpm_engine_dbus_class_init (klass);
540
 
}
541
 
 
542
 
static void
543
 
xfpm_engine_init (XfpmEngine * engine)
544
 
{
545
 
    engine->priv = XFPM_ENGINE_GET_PRIVATE (engine);
546
 
 
547
 
    engine->priv->shutdown = xfpm_shutdown_new ();
548
 
    engine->priv->adapter = xfpm_adapter_new ();
549
 
    engine->priv->notify = xfpm_notify_new ();
550
 
    engine->priv->srv    = xfpm_screen_saver_new ();
551
 
    engine->priv->inhibit = xfpm_inhibit_new ();
552
 
    engine->priv->inhibited = FALSE;
553
 
 
554
 
    g_signal_connect (engine->priv->inhibit, "has-inhibit-changed",
555
 
                      G_CALLBACK (xfpm_engine_inhibit_changed_cb), engine);
556
 
 
557
 
    engine->priv->conf = NULL;
558
 
    engine->priv->supply = NULL;
559
 
#ifdef HAVE_DPMS
560
 
    engine->priv->dpms = NULL;
561
 
#endif
562
 
 
563
 
#ifdef SYSTEM_IS_LINUX
564
 
    engine->priv->cpu = NULL;
565
 
#endif
566
 
 
567
 
    engine->priv->button = NULL;
568
 
    engine->priv->bk = NULL;
569
 
 
570
 
    engine->priv->power_management = 0;
571
 
 
572
 
    xfpm_engine_dbus_init (engine);
573
 
 
574
 
    engine->priv->conf = xfpm_xfconf_new ();
575
 
 
576
 
    engine->priv->on_battery =
577
 
        !xfpm_adapter_get_present (engine->priv->adapter);
578
 
 
579
 
    g_signal_connect (engine->priv->adapter, "adapter-changed",
580
 
                      G_CALLBACK (xfpm_engine_adapter_changed_cb), engine);
581
 
 
582
 
    engine->priv->idle    = egg_idletime_new ();
583
 
 
584
 
    g_signal_connect (engine->priv->idle, "alarm-expired",
585
 
                      G_CALLBACK (xfpm_engine_alarm_timeout_cb), engine);
586
 
                      
587
 
    g_signal_connect (engine->priv->conf, "notify",
588
 
                      G_CALLBACK (xfpm_engine_settings_changed_cb), engine);
589
 
                    
590
 
    xfpm_engine_set_inactivity_timeouts (engine);
591
 
    
592
 
    g_idle_add ((GSourceFunc)xfpm_engine_load_all, engine);
593
 
}
594
 
 
595
 
static void
596
 
xfpm_engine_finalize (GObject * object)
597
 
{
598
 
    XfpmEngine *engine;
599
 
 
600
 
    engine = XFPM_ENGINE (object);
601
 
 
602
 
    g_object_unref (engine->priv->conf);
603
 
 
604
 
    g_object_unref (engine->priv->supply);
605
 
    
606
 
    g_object_unref (engine->priv->button);
607
 
    
608
 
    g_object_unref (engine->priv->idle);
609
 
    g_object_unref (engine->priv->bt_hal);
610
 
 
611
 
#ifdef HAVE_DPMS
612
 
    if (engine->priv->dpms)
613
 
        g_object_unref (engine->priv->dpms);
614
 
#endif
615
 
 
616
 
#ifdef SYSTEM_IS_LINUX
617
 
    if (engine->priv->cpu)
618
 
        g_object_unref (engine->priv->cpu);
619
 
#endif
620
 
 
621
 
    g_object_unref (engine->priv->shutdown);
622
 
 
623
 
    g_object_unref (engine->priv->adapter);
624
 
    
625
 
    g_object_unref (engine->priv->srv);
626
 
 
627
 
    if (engine->priv->bk)
628
 
        g_object_unref (engine->priv->bk);
629
 
 
630
 
    g_object_unref (engine->priv->notify);
631
 
 
632
 
    G_OBJECT_CLASS (xfpm_engine_parent_class)->finalize (object);
633
 
}
634
 
 
635
 
XfpmEngine *
636
 
xfpm_engine_new (void)
637
 
{
638
 
    XfpmEngine *engine = NULL;
639
 
    engine = g_object_new (XFPM_TYPE_ENGINE, NULL);
640
 
 
641
 
    return engine;
642
 
}
643
 
 
644
 
void
645
 
xfpm_engine_get_info (XfpmEngine * engine, GHashTable *hash)
646
 
{
647
 
    guint8 mapped_buttons;
648
 
    gboolean user_privilege = FALSE;
649
 
    gboolean can_suspend = FALSE;
650
 
    gboolean can_hibernate = FALSE;
651
 
    gboolean has_sleep_button = FALSE;
652
 
    gboolean has_hibernate_button = FALSE;
653
 
    gboolean has_power_button = FALSE;
654
 
    
655
 
    g_return_if_fail (XFPM_IS_ENGINE (engine));
656
 
 
657
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
658
 
                  "caller-privilege", &user_privilege,
659
 
                  "can-suspend", &can_suspend,
660
 
                  "can-hibernate", &can_hibernate, NULL);
661
 
                  
662
 
   
663
 
    mapped_buttons = xfpm_button_get_mapped (engine->priv->button);
664
 
    
665
 
    if ( mapped_buttons & SLEEP_KEY )
666
 
        has_sleep_button = TRUE;
667
 
    if ( mapped_buttons & HIBERNATE_KEY )
668
 
        has_hibernate_button = TRUE;
669
 
    if ( mapped_buttons & POWER_KEY )
670
 
        has_power_button = TRUE;
671
 
        
672
 
    g_hash_table_insert (hash, g_strdup ("caller-privilege"), g_strdup (xfpm_bool_to_string (user_privilege)));
673
 
    g_hash_table_insert (hash, g_strdup ("can-suspend"), g_strdup (xfpm_bool_to_string (can_suspend)));
674
 
    g_hash_table_insert (hash, g_strdup ("can-hibernate"), g_strdup (xfpm_bool_to_string (can_hibernate)));
675
 
    g_hash_table_insert (hash, g_strdup ("system-laptop"), g_strdup (xfpm_bool_to_string (engine->priv->is_laptop)));
676
 
    g_hash_table_insert (hash, g_strdup ("has-brightness"), g_strdup (xfpm_bool_to_string (engine->priv->has_lcd_brightness)));
677
 
    g_hash_table_insert (hash, g_strdup ("sleep-button"), g_strdup (xfpm_bool_to_string (has_sleep_button)));
678
 
    g_hash_table_insert (hash, g_strdup ("power-button"), g_strdup (xfpm_bool_to_string (has_power_button)));
679
 
    g_hash_table_insert (hash, g_strdup ("hibernate-button"), g_strdup (xfpm_bool_to_string (has_hibernate_button)));
680
 
}
681
 
 
682
 
void xfpm_engine_reload_hal_objects (XfpmEngine *engine)
683
 
{
684
 
    g_return_if_fail (XFPM_IS_ENGINE (engine));
685
 
    
686
 
    xfpm_adapter_reload (engine->priv->adapter);
687
 
    xfpm_supply_reload  (engine->priv->supply);
688
 
    
689
 
    
690
 
    xfpm_shutdown_reload (engine->priv->shutdown);
691
 
    
692
 
    if ( engine->priv->is_laptop )
693
 
    {
694
 
        xfpm_backlight_reload (engine->priv->bk);
695
 
#ifdef SYSTEM_IS_LINUX
696
 
        xfpm_cpu_reload       (engine->priv->cpu);
697
 
#endif
698
 
    }
699
 
}
700
 
 
701
 
/*
702
 
 * 
703
 
 * DBus server implementation for org.freedesktop.PowerManagement
704
 
 * 
705
 
 */
706
 
static gboolean xfpm_engine_dbus_shutdown (XfpmEngine *engine,
707
 
                                           GError **error);
708
 
 
709
 
static gboolean xfpm_engine_dbus_reboot   (XfpmEngine *engine,
710
 
                                           GError **error);
711
 
                                           
712
 
static gboolean xfpm_engine_dbus_hibernate (XfpmEngine * engine,
713
 
                                            GError **error);
714
 
 
715
 
static gboolean xfpm_engine_dbus_suspend (XfpmEngine * engine,
716
 
                                          GError ** error);
717
 
 
718
 
static gboolean xfpm_engine_dbus_can_reboot (XfpmEngine * engine,
719
 
                                             gboolean * OUT_can_reboot, 
720
 
                                             GError ** error);
721
 
 
722
 
static gboolean xfpm_engine_dbus_can_shutdown (XfpmEngine * engine,
723
 
                                               gboolean * OUT_can_reboot, 
724
 
                                               GError ** error);
725
 
 
726
 
static gboolean xfpm_engine_dbus_can_hibernate (XfpmEngine * engine,
727
 
                                                gboolean * OUT_can_hibernate,
728
 
                                                GError ** error);
729
 
 
730
 
static gboolean xfpm_engine_dbus_can_suspend (XfpmEngine * engine,
731
 
                                              gboolean * OUT_can_suspend,
732
 
                                              GError ** error);
733
 
 
734
 
static gboolean xfpm_engine_dbus_get_power_save_status (XfpmEngine * engine,
735
 
                                                        gboolean * OUT_save_power,
736
 
                                                        GError ** error);
737
 
 
738
 
static gboolean xfpm_engine_dbus_get_on_battery (XfpmEngine * engine,
739
 
                                                 gboolean * OUT_on_battery,
740
 
                                                 GError ** error);
741
 
 
742
 
static gboolean xfpm_engine_dbus_get_low_battery (XfpmEngine * engine,
743
 
                                                  gboolean * OUT_low_battery,
744
 
                                                  GError ** error);
745
 
 
746
 
#include "org.freedesktop.PowerManagement.h"
747
 
 
748
 
static void
749
 
xfpm_engine_dbus_class_init (XfpmEngineClass * klass)
750
 
{
751
 
    dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass),
752
 
                                     &dbus_glib_xfpm_engine_object_info);
753
 
}
754
 
 
755
 
static void
756
 
xfpm_engine_dbus_init (XfpmEngine * engine)
757
 
{
758
 
    DBusGConnection *bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
759
 
 
760
 
    dbus_g_connection_register_g_object (bus,
761
 
                                         "/org/freedesktop/PowerManagement",
762
 
                                         G_OBJECT (engine));
763
 
}
764
 
 
765
 
static gboolean xfpm_engine_dbus_shutdown (XfpmEngine *engine,
766
 
                                           GError **error)
767
 
{
768
 
    gboolean caller_privilege, can_hibernate;
769
 
    
770
 
    TRACE ("Hibernate message received");
771
 
 
772
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
773
 
                  "caller-privilege", &caller_privilege,
774
 
                  "can-hibernate", &can_hibernate, NULL);
775
 
 
776
 
    if (!caller_privilege)
777
 
    {
778
 
        g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED,
779
 
                    _("Permission denied"));
780
 
        return FALSE;
781
 
    }
782
 
 
783
 
    if ( engine->priv->inhibited )
784
 
        return TRUE;
785
 
 
786
 
    xfpm_shutdown (engine->priv->shutdown, NULL);
787
 
    
788
 
    return TRUE;
789
 
}
790
 
 
791
 
static gboolean xfpm_engine_dbus_reboot   (XfpmEngine *engine,
792
 
                                           GError **error)
793
 
{
794
 
    gboolean caller_privilege;
795
 
    
796
 
    TRACE ("Reboot message received");
797
 
 
798
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
799
 
                  "caller-privilege", &caller_privilege,
800
 
                  NULL);
801
 
 
802
 
    if (!caller_privilege)
803
 
    {
804
 
        g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED,
805
 
                    _("Permission denied"));
806
 
        return FALSE;
807
 
    }
808
 
 
809
 
    if ( engine->priv->inhibited )
810
 
        return TRUE;
811
 
 
812
 
    xfpm_reboot (engine->priv->shutdown, NULL);
813
 
    
814
 
    return TRUE;
815
 
}
816
 
 
817
 
static gboolean
818
 
xfpm_engine_dbus_hibernate (XfpmEngine * engine, GError ** error)
819
 
{
820
 
    gboolean caller_privilege, can_hibernate;
821
 
    
822
 
    TRACE ("Hibernate message received");
823
 
 
824
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
825
 
                  "caller-privilege", &caller_privilege,
826
 
                  "can-hibernate", &can_hibernate, NULL);
827
 
 
828
 
    if (!caller_privilege)
829
 
    {
830
 
        g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED,
831
 
                    _("Permission denied"));
832
 
        return FALSE;
833
 
    }
834
 
 
835
 
    if (!can_hibernate)
836
 
    {
837
 
        g_set_error (error, XFPM_ERROR, XFPM_ERROR_NO_HARDWARE_SUPPORT,
838
 
                    _("Hibernate not supported"));
839
 
        return FALSE;
840
 
    }
841
 
 
842
 
    xfpm_engine_shutdown_request (engine, XFPM_DO_HIBERNATE, FALSE);
843
 
 
844
 
    return TRUE;
845
 
}
846
 
 
847
 
static gboolean
848
 
xfpm_engine_dbus_suspend (XfpmEngine * engine, GError ** error)
849
 
{
850
 
    gboolean caller_privilege, can_suspend;
851
 
    TRACE ("Suspend message received");
852
 
 
853
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
854
 
                  "caller-privilege", &caller_privilege,
855
 
                  "can-suspend", &can_suspend, NULL);
856
 
 
857
 
    if (!caller_privilege)
858
 
    {
859
 
        g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED,
860
 
                    _("Permission denied"));
861
 
        return FALSE;
862
 
    }
863
 
 
864
 
    if (!can_suspend)
865
 
    {
866
 
        g_set_error (error, XFPM_ERROR, XFPM_ERROR_NO_HARDWARE_SUPPORT,
867
 
                    _("Suspend not supported"));
868
 
        return FALSE;
869
 
    }
870
 
 
871
 
    xfpm_engine_shutdown_request (engine, XFPM_DO_SUSPEND, FALSE);
872
 
 
873
 
    return TRUE;
874
 
}
875
 
 
876
 
static gboolean
877
 
xfpm_engine_dbus_can_shutdown (XfpmEngine * engine,
878
 
                               gboolean * OUT_can_shutdown, GError ** error)
879
 
{
880
 
    TRACE ("Can shutdown message received");
881
 
    
882
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
883
 
                  "caller-privilege", OUT_can_shutdown, NULL);
884
 
 
885
 
    return TRUE;
886
 
}
887
 
 
888
 
static gboolean
889
 
xfpm_engine_dbus_can_reboot (XfpmEngine * engine,
890
 
                             gboolean * OUT_can_reboot, GError ** error)
891
 
{
892
 
    TRACE ("Can reboot message received");
893
 
    
894
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
895
 
                  "caller-privilege", OUT_can_reboot, NULL);
896
 
 
897
 
    return TRUE;
898
 
}
899
 
 
900
 
static gboolean
901
 
xfpm_engine_dbus_can_hibernate (XfpmEngine * engine,
902
 
                                gboolean * OUT_can_hibernate, GError ** error)
903
 
{
904
 
    TRACE ("Can hibernate message received");
905
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
906
 
                    "can-hibernate", OUT_can_hibernate, NULL);
907
 
 
908
 
    return TRUE;
909
 
}
910
 
 
911
 
static gboolean
912
 
xfpm_engine_dbus_can_suspend (XfpmEngine * engine,
913
 
                              gboolean * OUT_can_suspend, GError ** error)
914
 
{
915
 
    TRACE ("Can suspend message received");
916
 
    g_object_get (G_OBJECT (engine->priv->shutdown),
917
 
                  "can-suspend", OUT_can_suspend, NULL);
918
 
 
919
 
    return TRUE;
920
 
}
921
 
 
922
 
static gboolean xfpm_engine_dbus_get_power_save_status (XfpmEngine * engine,
923
 
                                                        gboolean * OUT_save_power,
924
 
                                                        GError ** error)
925
 
{
926
 
    gboolean save_power;
927
 
    
928
 
    TRACE ("Can power save message received");
929
 
    
930
 
    g_object_get (G_OBJECT (engine->priv->conf),
931
 
                  POWER_SAVE_ON_BATTERY, &save_power,
932
 
                  NULL);
933
 
                  
934
 
    *OUT_save_power = save_power && engine->priv->on_battery;
935
 
    
936
 
    return TRUE;
937
 
}
938
 
 
939
 
static gboolean
940
 
xfpm_engine_dbus_get_on_battery (XfpmEngine * engine,
941
 
                                 gboolean * OUT_on_battery, GError ** error)
942
 
{
943
 
    TRACE ("On battery message received");
944
 
    *OUT_on_battery = engine->priv->on_battery;
945
 
 
946
 
    return TRUE;
947
 
}
948
 
 
949
 
static gboolean
950
 
xfpm_engine_dbus_get_low_battery (XfpmEngine * engine,
951
 
                                  gboolean * OUT_low_battery, GError ** error)
952
 
{
953
 
    TRACE ("On low battery message received");
954
 
    *OUT_low_battery = xfpm_supply_on_low_battery (engine->priv->supply);
955
 
 
956
 
    return TRUE;
957
 
}