summaryrefslogtreecommitdiff
path: root/libxfpm/hal-battery.c
blob: 8753fbaa410ec2f2380a7631705a829ebad608f5 (plain)
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * * Copyright (C) 2008-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 <glib.h>
#include <glib/gi18n.h>

#include "hal-battery.h"
#include "hal-manager.h"
#include "hal-enum.h"

static void hal_battery_finalize   (GObject *object);

static void hal_battery_get_property(GObject *object,
				    guint prop_id,
				    GValue *value,
				    GParamSpec *pspec);

#define HAL_BATTERY_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE((o), HAL_TYPE_BATTERY, HalBatteryPrivate))

struct HalBatteryPrivate
{
    /* Properties read-only */
    HalDeviceType type;
    
    gboolean  is_present;
    gboolean  is_charging;
    gboolean  is_discharging;
    
    gchar    *unit;
    gchar    *technology;
    gchar    *vendor;
    gchar    *model;
    guint     percentage;

    guint32   current_charge;
    guint32   last_full;
    
    guint32   reporting_design;
    guint32   reporting_last_full;
    
    guint      time;
    
};

enum
{
    PROP_0,
    PROP_TYPE,
    PROP_IS_PRESENT,
    PROP_IS_CHARGING,
    PROP_IS_DISCHARGING,
    PROP_CURRENT_CHARGE,
    PROP_PERCENTAGE,
    PROP_REPORTING_DESIGN,
    PROP_LAST_FULL,
    PROP_REPORTING_LAST_FULL,
    PROP_TIME,
    PROP_TECHNOLOGY,
    PROP_VENDOR,
    PROP_MODEL,
    PROP_UNIT
};

enum
{
    BATTERY_CHANGED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE(HalBattery, hal_battery, HAL_TYPE_DEVICE)

static void
hal_battery_class_init(HalBatteryClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->get_property = hal_battery_get_property;
    object_class->finalize = hal_battery_finalize;

    signals[BATTERY_CHANGED] =
    	g_signal_new("battery-changed",
		     HAL_TYPE_BATTERY,
		     G_SIGNAL_RUN_LAST,
		     G_STRUCT_OFFSET(HalBatteryClass, battery_changed),
		     NULL, NULL,
		     g_cclosure_marshal_VOID__VOID,
		     G_TYPE_NONE, 0, G_TYPE_NONE);
    
    g_object_class_install_property(object_class,
				    PROP_IS_PRESENT,
				    g_param_spec_boolean("is-present",
				    			 NULL, NULL,
							 FALSE,
							 G_PARAM_READABLE));

    g_object_class_install_property(object_class,
				    PROP_IS_CHARGING,
				    g_param_spec_boolean("is-charging",
							 NULL, NULL,
							 FALSE,
							 G_PARAM_READABLE));
							 
    g_object_class_install_property(object_class,
				    PROP_IS_DISCHARGING,
				    g_param_spec_boolean("is-discharging",
							 NULL, NULL,
							 FALSE,
							 G_PARAM_READABLE));
    g_object_class_install_property(object_class,
				    PROP_CURRENT_CHARGE,
				    g_param_spec_uint("current-charge",
						      NULL, NULL,
						      0,
						      G_MAXUINT32,
						      0,
						      G_PARAM_READABLE));
						      
    g_object_class_install_property(object_class,
				    PROP_LAST_FULL,
				    g_param_spec_uint("last-full",
						      NULL, NULL,
						      0,
						      G_MAXUINT32,
						      0,
						      G_PARAM_READABLE));
						      
     g_object_class_install_property(object_class,
				    PROP_REPORTING_DESIGN,
				    g_param_spec_uint("reporting-design",
						      NULL, NULL,
						      0,
						      G_MAXUINT32,
						      0,
						      G_PARAM_READABLE));
    g_object_class_install_property(object_class,
				    PROP_REPORTING_LAST_FULL,
				    g_param_spec_uint("reporting-last-full",
						      NULL, NULL,
						      0,
						      G_MAXUINT32,
						      0,
						      G_PARAM_READABLE));
    g_object_class_install_property(object_class,
				    PROP_TIME,
				    g_param_spec_uint("time",
						      NULL, NULL,
						      0,
						      G_MAXINT,
						      0,
						      G_PARAM_READABLE));
    g_object_class_install_property(object_class,
				    PROP_TYPE,
				    g_param_spec_uint("type",
						      NULL, NULL,
						      0,
						      HAL_DEVICE_TYPE_UNKNOWN,
						      HAL_DEVICE_TYPE_UNKNOWN,
						      G_PARAM_READABLE));
    g_object_class_install_property(object_class,
				    PROP_PERCENTAGE,
				    g_param_spec_uint("percentage",
						      NULL, NULL,
						      0,
						      G_MAXINT,
						      0,
						      G_PARAM_READABLE));
						      
    g_object_class_install_property(object_class,
				    PROP_TECHNOLOGY,
				    g_param_spec_string("technology",
							NULL, NULL,
						        NULL,
						        G_PARAM_READABLE));
     g_object_class_install_property(object_class,
				    PROP_VENDOR,
				    g_param_spec_string("vendor",
							NULL, NULL,
						        NULL,
						        G_PARAM_READABLE));
    g_object_class_install_property(object_class,
				    PROP_MODEL,
				    g_param_spec_string("model",
							NULL, NULL,
						        NULL,
						        G_PARAM_READABLE));
    g_object_class_install_property(object_class,
				    PROP_UNIT,
				    g_param_spec_string("unit",
							 NULL, NULL,
							 NULL,
							 G_PARAM_READABLE));
							 
    g_type_class_add_private(klass,sizeof(HalBatteryPrivate));
}

static void
hal_battery_init (HalBattery *battery)
{

    battery->priv = HAL_BATTERY_GET_PRIVATE(battery);
    
    battery->priv->is_present      = FALSE;
    battery->priv->is_charging     = FALSE;
    battery->priv->is_discharging  = FALSE;

    battery->priv->unit            = NULL;
    battery->priv->vendor          = NULL;
    battery->priv->technology      = NULL;
    battery->priv->model           = NULL;
    battery->priv->type            = HAL_DEVICE_TYPE_UNKNOWN;
    
    battery->priv->percentage      = 0;
    battery->priv->current_charge  = 0;
    battery->priv->last_full       = 0;
    battery->priv->time            = 0;
    battery->priv->reporting_design = 0;
    battery->priv->reporting_last_full = 0;
}

static void hal_battery_get_property(GObject *object,
				    guint prop_id,
				    GValue *value,
				    GParamSpec *pspec)
{
    HalBattery *battery;
    battery = HAL_BATTERY(object);

    switch (prop_id)
    {
	case PROP_TYPE:
		g_value_set_uint (value, battery->priv->type);
		break;
	case PROP_IS_PRESENT:
		g_value_set_boolean (value, battery->priv->is_present);
		break;
	case PROP_IS_CHARGING:
		g_value_set_boolean (value, battery->priv->is_charging);
		break;
	case PROP_IS_DISCHARGING:
		g_value_set_boolean (value, battery->priv->is_discharging);
		break;
	case PROP_UNIT:
		g_value_set_string (value, battery->priv->unit);
		break;
	case PROP_TECHNOLOGY:
		g_value_set_string (value, battery->priv->technology);
		break;
	case PROP_VENDOR:
		g_value_set_string (value, battery->priv->vendor);
		break;
	case PROP_MODEL:
		g_value_set_string (value, battery->priv->model);
		break;
	case PROP_PERCENTAGE:
		g_value_set_uint (value, battery->priv->percentage);
		break;
	case PROP_CURRENT_CHARGE:
		g_value_set_uint (value, battery->priv->current_charge);
		break;
	case PROP_LAST_FULL:
		g_value_set_uint (value, battery->priv->last_full);
		break;
	case PROP_REPORTING_DESIGN:
		g_value_set_uint (value, battery->priv->reporting_design);
		break;
	case PROP_REPORTING_LAST_FULL:
		g_value_set_uint (value, battery->priv->reporting_last_full);
		break;	
	case PROP_TIME:
		g_value_set_uint (value, battery->priv->time);
		break;
	default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object,prop_id,pspec);
            break;
    }
}

static void
hal_battery_finalize(GObject *object)
{
    HalBattery *battery;

    battery = HAL_BATTERY(object);
    
    if ( battery->priv->technology )
    	g_free (battery->priv->technology);
	
    if ( battery->priv->vendor )
    	g_free (battery->priv->vendor);
	
    if ( battery->priv->model )
    	g_free (battery->priv->model);
	
    if ( battery->priv->unit )
    	g_free (battery->priv->unit);

    G_OBJECT_CLASS(hal_battery_parent_class)->finalize(object);
}

static HalDeviceType G_GNUC_PURE
hal_battery_type_enum_from_string(const gchar *string)
{
    if ( !g_strcmp0 (string, "primary") )
    {
	return HAL_DEVICE_TYPE_PRIMARY;
    }
    else if ( !g_strcmp0 (string, "ups") )
    {
	return HAL_DEVICE_TYPE_UPS;
    }
    else if ( !g_strcmp0 (string, "mouse") )
    {
	return HAL_DEVICE_TYPE_MOUSE;
    }
    else if ( !g_strcmp0 (string, "keyboard") )
    {
	return HAL_DEVICE_TYPE_KEYBOARD;
    }
    else if ( !g_strcmp0 (string, "camera") )
    {
	return HAL_DEVICE_TYPE_CAMERA;
    }
    else if ( !g_strcmp0 (string, "keyboard_mouse") )
    {
	return HAL_DEVICE_TYPE_KEYBOARD_MOUSE;
    }
    
    return HAL_DEVICE_TYPE_UNKNOWN;
}

static HalDeviceType
hal_battery_get_device_type (HalBattery *battery)
{
    const gchar *udi;
    gchar *type = NULL;
    
    HalDeviceType type_enum = HAL_DEVICE_TYPE_UNKNOWN;
    
    udi = hal_device_get_udi (HAL_DEVICE(battery));
    
    g_return_val_if_fail (udi != NULL, HAL_DEVICE_TYPE_UNKNOWN);
    
    type = hal_device_get_property_string (HAL_DEVICE(battery), "battery.type");
    
    if ( type )
    {
	type_enum  = hal_battery_type_enum_from_string(type);
	g_free(type);
    }
    return type_enum;
}

static guint G_GNUC_CONST
_get_battery_percentage (guint32 last_full, guint32 current)
{
    guint val = 100;
    float f;
    
    if ( G_UNLIKELY (last_full <= current) ) return val;
    
    /*
     * Special case when we get 0 as last full
     * this happens for me once i had the battery
     * totally empty on my aspire one.
     */
    if ( G_UNLIKELY (last_full == 0 ) )
	return 0;
	
    f = (float)current/last_full *100;
	
	val = (guint)f;
	
    return val;   
}

static void
hal_battery_refresh_all (HalBattery *battery)
{
    
    battery->priv->is_present = 
    	hal_device_get_property_bool(HAL_DEVICE(battery), "battery.present");
	
    battery->priv->is_charging = 
    	hal_device_get_property_bool(HAL_DEVICE(battery), "battery.rechargeable.is_charging");
    
    battery->priv->is_discharging = 
    	hal_device_get_property_bool(HAL_DEVICE(battery), "battery.rechargeable.is_discharging");
    
    battery->priv->current_charge = 
    	hal_device_get_property_int(HAL_DEVICE(battery), "battery.charge_level.current");
	
    battery->priv->last_full = 
    	hal_device_get_property_int(HAL_DEVICE(battery), "battery.charge_level.last_full");
    
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.remaining_time") )
    	battery->priv->time = 
    		hal_device_get_property_int(HAL_DEVICE(battery), "battery.remaining_time");
    else
    	battery->priv->time = 0;
    
    //FIXME: calculate the percentage if it is not found on HAL
    if ( hal_device_has_key(HAL_DEVICE(battery), "battery.charge_level.percentage") )
     	battery->priv->percentage = 
    		hal_device_get_property_int(HAL_DEVICE(battery), "battery.charge_level.percentage");
    else battery->priv->percentage = _get_battery_percentage(battery->priv->last_full, battery->priv->current_charge);
    
    if ( hal_device_has_key(HAL_DEVICE(battery), "battery.reporting.last_full") )
     	battery->priv->reporting_last_full = 
    		hal_device_get_property_int(HAL_DEVICE(battery), "battery.reporting.last_full");
		
}

static const gchar * G_GNUC_PURE
_translate_technology (const gchar *tech)
{
    if ( !g_strcmp0 (tech, "lithium-ion") )
    {
	return _("Lithium ion");
    }
    else if ( !g_strcmp0 (tech, "lead-acid") )
    {
	return _("Lead acid");
    }
    else if ( !g_strcmp0 (tech, "lithium-polymer") )
    {
	return _("Lithium polymer");
    }
    else if ( !g_strcmp0 (tech, "nickel-metal-hydride") )
    {
	return _("Nickel metal hydride");
    }
    
    return _("Unknown");
}

static const gchar * G_GNUC_PURE
_translate_unit (const gchar *unit)
{
    if ( !g_strcmp0 (unit, "mWh") )
    {
	return _("mWh");
    }
    else if ( !g_strcmp0 (unit, "mAh") )
    {
	return _("mAh");
    }
    
    return _("Unknown unit");
}

static void
hal_battery_get_battery_info (HalBattery *battery)
{
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.technology") )
    {
	gchar *tech = hal_device_get_property_string (HAL_DEVICE(battery), "battery.technology");
	if ( tech )
	{
	    battery->priv->technology = g_strdup (_translate_technology (tech));
	    g_free (tech);
	}
    }
    
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.vendor") )
    {
	gchar *vendor = hal_device_get_property_string (HAL_DEVICE(battery), "battery.vendor");
	if ( vendor )
	{
	    battery->priv->vendor = g_strdup ( vendor);
	    g_free (vendor);
	}
    }
    
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.model") )
    {
	gchar *model = hal_device_get_property_string (HAL_DEVICE(battery), "battery.model");
	if ( model )
	{
	    battery->priv->model = g_strdup (model);
	    g_free (model);
	}
    }
    
    battery->priv->reporting_design = hal_device_get_property_int (HAL_DEVICE(battery), 
							                   "battery.reporting.design");
							      
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.reporting.unit") )
    {
	gchar *unit = hal_device_get_property_string (HAL_DEVICE(battery), "battery.reporting.unit");
	
	if ( unit )
	{
	    battery->priv->unit = g_strdup(_translate_unit(unit));
	    g_free(unit);	    
	}
    }
}

static void
hal_battery_battery_changed_cb (HalBattery *battery, const gchar *key)
{
    if ( !g_strcmp0 (key, "battery.present") ||
    	 !g_strcmp0 (key, "battery.rechargeable.is_charging") ||
	 !g_strcmp0 (key, "battery.rechargeable.is_discharging") ||
	 !g_strcmp0 (key, "battery.charge_level.current")    ||
	 !g_strcmp0 (key, "battery.remaining_time") ||
	 !g_strcmp0 (key, "battery.charge_level.percentage") )
    {
	hal_battery_refresh_all (battery);
    	g_signal_emit (G_OBJECT (battery), signals[BATTERY_CHANGED], 0);
    }
    
}


static void
hal_battery_property_modified_cb(HalBattery *battery, 
			         const gchar *udi,
				 const gchar *key, 
			         gboolean is_removed,
			         gboolean is_added,
				 gpointer data)
{
    hal_battery_battery_changed_cb (battery, key);
}

HalBattery *
hal_battery_new (const gchar *udi)
{
    HalBattery *battery = NULL;
    
    battery = g_object_new (HAL_TYPE_BATTERY, NULL);
    hal_device_set_udi (HAL_DEVICE(battery), udi);
    
    battery->priv->type = hal_battery_get_device_type (battery);
   
    hal_battery_refresh_all (battery);
    hal_battery_get_battery_info (battery);
	
    hal_device_watch (HAL_DEVICE(battery));
    
    g_signal_connect (G_OBJECT(battery), "device-changed",
		      G_CALLBACK(hal_battery_property_modified_cb), battery);
    return battery;
}