~ubuntu-branches/ubuntu/precise/xfce4-power-manager/precise

« back to all changes in this revision

Viewing changes to libxfpm/hal-battery.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-09 18:28:34 UTC
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: james.westby@ubuntu.com-20101209182834-tjz13qcewqlq19eu
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

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
 
#include <glib/gi18n.h>
31
 
 
32
 
#include "hal-battery.h"
33
 
#include "hal-manager.h"
34
 
#include "hal-enum.h"
35
 
 
36
 
static void hal_battery_finalize   (GObject *object);
37
 
 
38
 
static void hal_battery_get_property(GObject *object,
39
 
                                    guint prop_id,
40
 
                                    GValue *value,
41
 
                                    GParamSpec *pspec);
42
 
 
43
 
#define HAL_BATTERY_GET_PRIVATE(o) \
44
 
(G_TYPE_INSTANCE_GET_PRIVATE((o), HAL_TYPE_BATTERY, HalBatteryPrivate))
45
 
 
46
 
#define FREE_STR_PROP(str)                          \
47
 
    if ( str )                                      \
48
 
        g_free (str);                               \
49
 
    str = NULL;
50
 
 
51
 
struct HalBatteryPrivate
52
 
{
53
 
    /* Properties read-only */
54
 
    HalDeviceType type;
55
 
    
56
 
    gboolean  is_present;
57
 
    gboolean  is_charging;
58
 
    gboolean  is_discharging;
59
 
    
60
 
    guint     percentage;
61
 
 
62
 
    guint32   current_charge;
63
 
    guint32   last_full;
64
 
    
65
 
    guint32   reporting_design;
66
 
    guint32   reporting_last_full;
67
 
    
68
 
    guint      time;
69
 
    
70
 
};
71
 
 
72
 
enum
73
 
{
74
 
    PROP_0,
75
 
    PROP_TYPE,
76
 
    PROP_IS_PRESENT,
77
 
    PROP_IS_CHARGING,
78
 
    PROP_IS_DISCHARGING,
79
 
    PROP_CURRENT_CHARGE,
80
 
    PROP_PERCENTAGE,
81
 
    PROP_REPORTING_DESIGN,
82
 
    PROP_LAST_FULL,
83
 
    PROP_REPORTING_LAST_FULL,
84
 
    PROP_TIME,
85
 
    PROP_TECHNOLOGY,
86
 
    PROP_VENDOR,
87
 
    PROP_MODEL,
88
 
    PROP_UNIT
89
 
};
90
 
 
91
 
enum
92
 
{
93
 
    BATTERY_CHANGED,
94
 
    LAST_SIGNAL
95
 
};
96
 
 
97
 
static guint signals[LAST_SIGNAL] = { 0 };
98
 
 
99
 
G_DEFINE_TYPE(HalBattery, hal_battery, HAL_TYPE_DEVICE)
100
 
 
101
 
static gchar *
102
 
hal_battery_get_info_string (HalBattery *battery, const gchar *key)
103
 
{
104
 
    gchar *val = NULL;
105
 
    
106
 
    if ( hal_device_has_key (HAL_DEVICE (battery), key) )
107
 
    {
108
 
        val = hal_device_get_property_string (HAL_DEVICE(battery), key);
109
 
    }
110
 
    
111
 
    return val;
112
 
}
113
 
 
114
 
static void
115
 
hal_battery_class_init(HalBatteryClass *klass)
116
 
{
117
 
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
118
 
 
119
 
    object_class->get_property = hal_battery_get_property;
120
 
    object_class->finalize = hal_battery_finalize;
121
 
 
122
 
    signals[BATTERY_CHANGED] =
123
 
        g_signal_new("battery-changed",
124
 
                     HAL_TYPE_BATTERY,
125
 
                     G_SIGNAL_RUN_LAST,
126
 
                     G_STRUCT_OFFSET(HalBatteryClass, battery_changed),
127
 
                     NULL, NULL,
128
 
                     g_cclosure_marshal_VOID__VOID,
129
 
                     G_TYPE_NONE, 0, G_TYPE_NONE);
130
 
    
131
 
    g_object_class_install_property(object_class,
132
 
                                    PROP_IS_PRESENT,
133
 
                                    g_param_spec_boolean("is-present",
134
 
                                                         NULL, NULL,
135
 
                                                         FALSE,
136
 
                                                         G_PARAM_READABLE));
137
 
 
138
 
    g_object_class_install_property(object_class,
139
 
                                    PROP_IS_CHARGING,
140
 
                                    g_param_spec_boolean("is-charging",
141
 
                                                         NULL, NULL,
142
 
                                                         FALSE,
143
 
                                                         G_PARAM_READABLE));
144
 
                                                         
145
 
    g_object_class_install_property(object_class,
146
 
                                    PROP_IS_DISCHARGING,
147
 
                                    g_param_spec_boolean("is-discharging",
148
 
                                                         NULL, NULL,
149
 
                                                         FALSE,
150
 
                                                         G_PARAM_READABLE));
151
 
    g_object_class_install_property(object_class,
152
 
                                    PROP_CURRENT_CHARGE,
153
 
                                    g_param_spec_uint("current-charge",
154
 
                                                      NULL, NULL,
155
 
                                                      0,
156
 
                                                      G_MAXUINT32,
157
 
                                                      0,
158
 
                                                      G_PARAM_READABLE));
159
 
                                                      
160
 
    g_object_class_install_property(object_class,
161
 
                                    PROP_LAST_FULL,
162
 
                                    g_param_spec_uint("last-full",
163
 
                                                      NULL, NULL,
164
 
                                                      0,
165
 
                                                      G_MAXUINT32,
166
 
                                                      0,
167
 
                                                      G_PARAM_READABLE));
168
 
                                                      
169
 
     g_object_class_install_property(object_class,
170
 
                                    PROP_REPORTING_DESIGN,
171
 
                                    g_param_spec_uint("reporting-design",
172
 
                                                      NULL, NULL,
173
 
                                                      0,
174
 
                                                      G_MAXUINT32,
175
 
                                                      0,
176
 
                                                      G_PARAM_READABLE));
177
 
    g_object_class_install_property(object_class,
178
 
                                    PROP_REPORTING_LAST_FULL,
179
 
                                    g_param_spec_uint("reporting-last-full",
180
 
                                                      NULL, NULL,
181
 
                                                      0,
182
 
                                                      G_MAXUINT32,
183
 
                                                      0,
184
 
                                                      G_PARAM_READABLE));
185
 
    g_object_class_install_property(object_class,
186
 
                                    PROP_TIME,
187
 
                                    g_param_spec_uint("time",
188
 
                                                      NULL, NULL,
189
 
                                                      0,
190
 
                                                      G_MAXINT,
191
 
                                                      0,
192
 
                                                      G_PARAM_READABLE));
193
 
    g_object_class_install_property(object_class,
194
 
                                    PROP_TYPE,
195
 
                                    g_param_spec_uint("type",
196
 
                                                      NULL, NULL,
197
 
                                                      0,
198
 
                                                      HAL_DEVICE_TYPE_UNKNOWN,
199
 
                                                      HAL_DEVICE_TYPE_UNKNOWN,
200
 
                                                      G_PARAM_READABLE));
201
 
    g_object_class_install_property(object_class,
202
 
                                    PROP_PERCENTAGE,
203
 
                                    g_param_spec_uint("percentage",
204
 
                                                      NULL, NULL,
205
 
                                                      0,
206
 
                                                      G_MAXINT,
207
 
                                                      0,
208
 
                                                      G_PARAM_READABLE));
209
 
                                                      
210
 
    g_object_class_install_property(object_class,
211
 
                                    PROP_TECHNOLOGY,
212
 
                                    g_param_spec_string("technology",
213
 
                                                        NULL, NULL,
214
 
                                                        NULL,
215
 
                                                        G_PARAM_READABLE));
216
 
     g_object_class_install_property(object_class,
217
 
                                    PROP_VENDOR,
218
 
                                    g_param_spec_string("vendor",
219
 
                                                        NULL, NULL,
220
 
                                                        NULL,
221
 
                                                        G_PARAM_READABLE));
222
 
    g_object_class_install_property(object_class,
223
 
                                    PROP_MODEL,
224
 
                                    g_param_spec_string("model",
225
 
                                                        NULL, NULL,
226
 
                                                        NULL,
227
 
                                                        G_PARAM_READABLE));
228
 
    g_object_class_install_property(object_class,
229
 
                                    PROP_UNIT,
230
 
                                    g_param_spec_string("unit",
231
 
                                                         NULL, NULL,
232
 
                                                         NULL,
233
 
                                                         G_PARAM_READABLE));
234
 
                                                         
235
 
    g_type_class_add_private(klass,sizeof(HalBatteryPrivate));
236
 
}
237
 
 
238
 
static void
239
 
hal_battery_init (HalBattery *battery)
240
 
{
241
 
 
242
 
    battery->priv = HAL_BATTERY_GET_PRIVATE(battery);
243
 
    
244
 
    battery->priv->is_present      = FALSE;
245
 
    battery->priv->is_charging     = FALSE;
246
 
    battery->priv->is_discharging  = FALSE;
247
 
 
248
 
    battery->priv->type            = HAL_DEVICE_TYPE_UNKNOWN;
249
 
    
250
 
    battery->priv->percentage      = 0;
251
 
    battery->priv->current_charge  = 0;
252
 
    battery->priv->last_full       = 0;
253
 
    battery->priv->time            = 0;
254
 
    battery->priv->reporting_design = 0;
255
 
    battery->priv->reporting_last_full = 0;
256
 
}
257
 
 
258
 
static const gchar * G_GNUC_PURE
259
 
_translate_technology (const gchar *tech)
260
 
{
261
 
    if ( !g_strcmp0 (tech, "lithium-ion") )
262
 
    {
263
 
        return _("Lithium ion");
264
 
    }
265
 
    else if ( !g_strcmp0 (tech, "lead-acid") )
266
 
    {
267
 
        return _("Lead acid");
268
 
    }
269
 
    else if ( !g_strcmp0 (tech, "lithium-polymer") )
270
 
    {
271
 
        return _("Lithium polymer");
272
 
    }
273
 
    else if ( !g_strcmp0 (tech, "nickel-metal-hydride") )
274
 
    {
275
 
        return _("Nickel metal hydride");
276
 
    }
277
 
    
278
 
    return _("Unknown");
279
 
}
280
 
 
281
 
static const gchar * G_GNUC_PURE
282
 
_translate_unit (const gchar *unit)
283
 
{
284
 
    if ( !g_strcmp0 (unit, "mWh") )
285
 
    {
286
 
        return _("mWh");
287
 
    }
288
 
    else if ( !g_strcmp0 (unit, "mAh") )
289
 
    {
290
 
        return _("mAh");
291
 
    }
292
 
    
293
 
    return _("Unknown unit");
294
 
}
295
 
 
296
 
static void hal_battery_get_property(GObject *object,
297
 
                                    guint prop_id,
298
 
                                    GValue *value,
299
 
                                    GParamSpec *pspec)
300
 
{
301
 
    HalBattery *battery;
302
 
    battery = HAL_BATTERY(object);
303
 
 
304
 
    switch (prop_id)
305
 
    {
306
 
        case PROP_TYPE:
307
 
                g_value_set_uint (value, battery->priv->type);
308
 
                break;
309
 
        case PROP_IS_PRESENT:
310
 
                g_value_set_boolean (value, battery->priv->is_present);
311
 
                break;
312
 
        case PROP_IS_CHARGING:
313
 
                g_value_set_boolean (value, battery->priv->is_charging);
314
 
                break;
315
 
        case PROP_IS_DISCHARGING:
316
 
                g_value_set_boolean (value, battery->priv->is_discharging);
317
 
                break;
318
 
                
319
 
        case PROP_UNIT:
320
 
        {
321
 
                gchar *unit = NULL;
322
 
                gchar *val;
323
 
                val = hal_battery_get_info_string (battery, "battery.reporting.unit");
324
 
                if ( val )
325
 
                {
326
 
                    unit = g_strdup(_translate_unit (val));
327
 
                    g_free (val);
328
 
                }
329
 
                g_value_set_string (value, unit);
330
 
                break;
331
 
        }
332
 
        case PROP_TECHNOLOGY:
333
 
        {
334
 
                gchar *val;
335
 
                gchar *technology = NULL;
336
 
                val = hal_battery_get_info_string (battery, "battery.technology");
337
 
                if ( val )
338
 
                {
339
 
                    technology = g_strdup (_translate_technology (val));
340
 
                    g_free (val);
341
 
                }
342
 
                
343
 
                g_value_set_string (value, technology);
344
 
                g_free (technology);
345
 
                break;
346
 
        }
347
 
        case PROP_VENDOR:
348
 
        {
349
 
                gchar *vendor = NULL;
350
 
                vendor = hal_battery_get_info_string (battery, "battery.vendor");
351
 
                g_value_set_string (value, vendor);
352
 
                g_free (vendor);
353
 
                break;
354
 
        }
355
 
        case PROP_MODEL:
356
 
        {
357
 
                gchar *model = NULL;
358
 
                model = hal_battery_get_info_string (battery, "battery.model");
359
 
                g_value_set_string (value, model);
360
 
                g_free (model);
361
 
                break;
362
 
        }
363
 
        case PROP_PERCENTAGE:
364
 
                g_value_set_uint (value, battery->priv->percentage);
365
 
                break;
366
 
        case PROP_CURRENT_CHARGE:
367
 
                g_value_set_uint (value, battery->priv->current_charge);
368
 
                break;
369
 
        case PROP_LAST_FULL:
370
 
                g_value_set_uint (value, battery->priv->last_full);
371
 
                break;
372
 
        case PROP_REPORTING_DESIGN:
373
 
                g_value_set_uint (value, battery->priv->reporting_design);
374
 
                break;
375
 
        case PROP_REPORTING_LAST_FULL:
376
 
                g_value_set_uint (value, battery->priv->reporting_last_full);
377
 
                break;  
378
 
        case PROP_TIME:
379
 
                g_value_set_uint (value, battery->priv->time);
380
 
                break;
381
 
        default:
382
 
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object,prop_id,pspec);
383
 
            break;
384
 
    }
385
 
}
386
 
 
387
 
static void
388
 
hal_battery_finalize(GObject *object)
389
 
{
390
 
    HalBattery *battery;
391
 
 
392
 
    battery = HAL_BATTERY(object);
393
 
    
394
 
    G_OBJECT_CLASS(hal_battery_parent_class)->finalize(object);
395
 
}
396
 
 
397
 
static HalDeviceType G_GNUC_PURE
398
 
hal_battery_type_enum_from_string(const gchar *string)
399
 
{
400
 
    if ( !g_strcmp0 (string, "primary") )
401
 
    {
402
 
        return HAL_DEVICE_TYPE_PRIMARY;
403
 
    }
404
 
    else if ( !g_strcmp0 (string, "ups") )
405
 
    {
406
 
        return HAL_DEVICE_TYPE_UPS;
407
 
    }
408
 
    else if ( !g_strcmp0 (string, "mouse") )
409
 
    {
410
 
        return HAL_DEVICE_TYPE_MOUSE;
411
 
    }
412
 
    else if ( !g_strcmp0 (string, "keyboard") )
413
 
    {
414
 
        return HAL_DEVICE_TYPE_KEYBOARD;
415
 
    }
416
 
    else if ( !g_strcmp0 (string, "camera") )
417
 
    {
418
 
        return HAL_DEVICE_TYPE_CAMERA;
419
 
    }
420
 
    else if ( !g_strcmp0 (string, "keyboard_mouse") )
421
 
    {
422
 
        return HAL_DEVICE_TYPE_KEYBOARD_MOUSE;
423
 
    }
424
 
    
425
 
    return HAL_DEVICE_TYPE_UNKNOWN;
426
 
}
427
 
 
428
 
static HalDeviceType
429
 
hal_battery_get_device_type (HalBattery *battery)
430
 
{
431
 
    const gchar *udi;
432
 
    gchar *type = NULL;
433
 
    
434
 
    HalDeviceType type_enum = HAL_DEVICE_TYPE_UNKNOWN;
435
 
    
436
 
    udi = hal_device_get_udi (HAL_DEVICE(battery));
437
 
    
438
 
    g_return_val_if_fail (udi != NULL, HAL_DEVICE_TYPE_UNKNOWN);
439
 
    
440
 
    type = hal_device_get_property_string (HAL_DEVICE(battery), "battery.type");
441
 
    
442
 
    if ( type )
443
 
    {
444
 
        type_enum  = hal_battery_type_enum_from_string(type);
445
 
        g_free(type);
446
 
    }
447
 
    return type_enum;
448
 
}
449
 
 
450
 
static guint G_GNUC_CONST
451
 
_get_battery_percentage (guint32 last_full, guint32 current)
452
 
{
453
 
    guint val = 100;
454
 
    float f;
455
 
    
456
 
    if ( G_UNLIKELY (last_full <= current) ) return val;
457
 
    
458
 
    /*
459
 
     * Special case when we get 0 as last full
460
 
     * this happens for me once i had the battery
461
 
     * totally empty on my aspire one.
462
 
     */
463
 
    if ( G_UNLIKELY (last_full == 0 ) )
464
 
        return 0;
465
 
        
466
 
    f = (float)current/last_full *100;
467
 
        
468
 
        val = (guint)f;
469
 
        
470
 
    return val;   
471
 
}
472
 
 
473
 
static void
474
 
hal_battery_refresh_all (HalBattery *battery)
475
 
{
476
 
    
477
 
    battery->priv->is_present = 
478
 
        hal_device_get_property_bool(HAL_DEVICE(battery), "battery.present");
479
 
        
480
 
    battery->priv->is_charging = 
481
 
        hal_device_get_property_bool(HAL_DEVICE(battery), "battery.rechargeable.is_charging");
482
 
    
483
 
    battery->priv->is_discharging = 
484
 
        hal_device_get_property_bool(HAL_DEVICE(battery), "battery.rechargeable.is_discharging");
485
 
    
486
 
    battery->priv->current_charge = 
487
 
        hal_device_get_property_int(HAL_DEVICE(battery), "battery.charge_level.current");
488
 
        
489
 
    battery->priv->last_full = 
490
 
        hal_device_get_property_int(HAL_DEVICE(battery), "battery.charge_level.last_full");
491
 
    
492
 
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.remaining_time") )
493
 
        battery->priv->time = 
494
 
                hal_device_get_property_int(HAL_DEVICE(battery), "battery.remaining_time");
495
 
    else
496
 
        battery->priv->time = 0;
497
 
    
498
 
    if ( hal_device_has_key(HAL_DEVICE(battery), "battery.charge_level.percentage") )
499
 
        battery->priv->percentage = 
500
 
                hal_device_get_property_int(HAL_DEVICE(battery), "battery.charge_level.percentage");
501
 
    else battery->priv->percentage = _get_battery_percentage(battery->priv->last_full, battery->priv->current_charge);
502
 
    
503
 
    if ( hal_device_has_key(HAL_DEVICE(battery), "battery.reporting.last_full") )
504
 
        battery->priv->reporting_last_full = 
505
 
                hal_device_get_property_int(HAL_DEVICE(battery), "battery.reporting.last_full");
506
 
 
507
 
    battery->priv->reporting_design = hal_device_get_property_int (HAL_DEVICE(battery), 
508
 
                                                                   "battery.reporting.design");
509
 
}
510
 
 
511
 
static void
512
 
hal_battery_battery_changed_cb (HalBattery *battery, const gchar *key)
513
 
{
514
 
    if ( !g_strcmp0 (key, "battery.present") ||
515
 
         !g_strcmp0 (key, "battery.rechargeable.is_charging") ||
516
 
         !g_strcmp0 (key, "battery.rechargeable.is_discharging") ||
517
 
         !g_strcmp0 (key, "battery.charge_level.current")    ||
518
 
         !g_strcmp0 (key, "battery.remaining_time") ||
519
 
         !g_strcmp0 (key, "battery.charge_level.percentage") )
520
 
    {
521
 
        hal_battery_refresh_all (battery);
522
 
        g_signal_emit (G_OBJECT (battery), signals[BATTERY_CHANGED], 0);
523
 
    }
524
 
}
525
 
 
526
 
static void
527
 
hal_battery_property_modified_cb(HalBattery *battery, 
528
 
                                 const gchar *udi,
529
 
                                 const gchar *key, 
530
 
                                 gboolean is_removed,
531
 
                                 gboolean is_added,
532
 
                                 gpointer data)
533
 
{
534
 
    hal_battery_battery_changed_cb (battery, key);
535
 
}
536
 
 
537
 
HalBattery *
538
 
hal_battery_new (const gchar *udi)
539
 
{
540
 
    HalBattery *battery = NULL;
541
 
    
542
 
    battery = g_object_new (HAL_TYPE_BATTERY, NULL);
543
 
    hal_device_set_udi (HAL_DEVICE(battery), udi);
544
 
    
545
 
    battery->priv->type = hal_battery_get_device_type (battery);
546
 
   
547
 
    hal_battery_refresh_all (battery);
548
 
        
549
 
    hal_device_watch (HAL_DEVICE(battery));
550
 
    
551
 
    g_signal_connect (G_OBJECT(battery), "device-changed",
552
 
                      G_CALLBACK(hal_battery_property_modified_cb), battery);
553
 
    return battery;
554
 
}