'batt': make sure 'Size' is at least 1.
[lxde/lxpanel.git] / plugins / batt / batt.c
CommitLineData
8f11d5f8
HJYP
1/*
2 * ACPI battery monitor plugin for LXPanel
3 *
4 * Copyright (C) 2007 by Greg McNew <gmcnew@gmail.com>
802d5540 5 * Copyright (C) 2008 by Hong Jen Yee <pcman.tw@gmail.com>
c887dfa0 6 * Copyright (C) 2009 by Juergen Hoetzel <juergen@archlinux.org>
b840f7cc 7 * Copyright (C) 2014 by Andriy Grytsenko <andrej@rep.kiev.ua>
8f11d5f8
HJYP
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 *
24 * This plugin monitors battery usage on ACPI-enabled systems by reading the
c887dfa0
JH
25 * battery information found in /sys/class/power_supply. The update interval is
26 * user-configurable and defaults to 3 second.
8f11d5f8
HJYP
27 *
28 * The battery's remaining life is estimated from its current charge and current
29 * rate of discharge. The user may configure an alarm command to be run when
30 * their estimated remaining battery life reaches a certain level.
31 */
32
e751fe05
HJYP
33/* FIXME:
34 * Here are somethings need to be improvec:
35 * 1. Replace pthread stuff with gthread counterparts for portability.
e751fe05
HJYP
36 * 3. Add an option to hide the plugin when AC power is used or there is no battery.
37 * 4. Handle failure gracefully under systems other than Linux.
38*/
39
40#include <glib.h>
8f11d5f8 41#include <glib/gi18n.h>
c0ed75bf 42#include <pthread.h> /* used by pthread_create() and alarmThread */
8f11d5f8
HJYP
43#include <semaphore.h> /* used by update() and alarmProcess() for alarms */
44#include <stdlib.h>
802d5540 45#include <string.h>
9c9d3a00 46#include <stdarg.h>
8f11d5f8 47
811afd8f 48#include "dbg.h" /* for ENTER and RET macros */
c887dfa0 49#include "batt_sys.h"
813c2d44 50#include "plugin.h" /* all other APIs including panel configuration */
8f11d5f8 51
8f11d5f8
HJYP
52/* The last MAX_SAMPLES samples are averaged when charge rates are evaluated.
53 This helps prevent spikes in the "time left" values the user sees. */
54#define MAX_SAMPLES 10
55
802d5540 56typedef struct {
8f11d5f8
HJYP
57 char *alarmCommand,
58 *backgroundColor,
59 *chargingColor1,
60 *chargingColor2,
61 *dischargingColor1,
802d5540
HJYP
62 *dischargingColor2;
63 GdkColor background,
64 charging1,
65 charging2,
66 discharging1,
67 discharging2;
f176735a 68 cairo_surface_t *pixmap;
4ebd3285 69 GtkContainer *box;
802d5540 70 GtkWidget *drawingArea;
6a1a639e 71 GtkOrientation orientation;
8f11d5f8
HJYP
72 unsigned int alarmTime,
73 border,
74 height,
75 length,
76 numSamples,
77 requestedBorder,
78 *rateSamples,
79 rateSamplesSum,
80 thickness,
81 timer,
802d5540
HJYP
82 state_elapsed_time,
83 info_elapsed_time,
8f11d5f8 84 wasCharging,
04a2f050
HJYP
85 width,
86 hide_if_no_battery;
8f11d5f8 87 sem_t alarmProcessLock;
c887dfa0 88 battery* b;
3051b49c 89 gboolean has_ac_adapter;
5c0c599d 90 gboolean show_extended_information;
a7bd16a4 91 LXPanel *panel;
813c2d44 92 config_setting_t *settings;
c887dfa0 93} lx_battery;
8f11d5f8
HJYP
94
95
96typedef struct {
97 char *command;
98 sem_t *lock;
4a974f26 99} Alarm;
8f11d5f8 100
813c2d44 101static void destructor(gpointer data);
c887dfa0 102static void update_display(lx_battery *lx_b, gboolean repaint);
3051b49c 103
8f11d5f8
HJYP
104/* alarmProcess takes the address of a dynamically allocated alarm struct (which
105 it must free). It ensures that alarm commands do not run concurrently. */
c0ed75bf 106static void * alarmProcess(void *arg) {
4a974f26 107 Alarm *a = (Alarm *) arg;
8f11d5f8
HJYP
108
109 sem_wait(a->lock);
6a1a639e
AG
110 if (system(a->command) != 0)
111 g_warning("plugin batt: failed to execute alarm command \"%s\"", a->command);
8f11d5f8
HJYP
112 sem_post(a->lock);
113
114 g_free(a);
c0ed75bf 115 return NULL;
8f11d5f8
HJYP
116}
117
9c9d3a00
HG
118
119static void append(gchar **tooltip, gchar *fmt, ...)
120{
121 gchar *old = *tooltip;
122 gchar *new;
123 va_list va;
124
125 va_start(va, fmt);
126 new = g_strdup_vprintf(fmt, va);
127 va_end(va);
128
129 *tooltip = g_strconcat(old, new, NULL);
130
131 g_free(old);
132 g_free(new);
133}
134
135
f855ac58
HG
136/* Make a tooltip string, and display remaining charge time if the battery
137 is charging or remaining life if it's discharging */
138static gchar* make_tooltip(lx_battery* lx_b, gboolean isCharging)
139{
140 gchar * tooltip;
9c9d3a00
HG
141 gchar * indent = " ";
142 battery *b = lx_b->b;
f855ac58 143
029d9f0c 144 if (b == NULL)
811afd8f 145 return NULL;
029d9f0c 146
f855ac58 147 if (isCharging) {
811afd8f
AG
148 int hours = lx_b->b->seconds / 3600;
149 int left_seconds = lx_b->b->seconds - 3600 * hours;
150 int minutes = left_seconds / 60;
151 tooltip = g_strdup_printf(
152 _("Battery: %d%% charged, %d:%02d until full"),
153 lx_b->b->percentage,
154 hours,
155 minutes );
f855ac58 156 } else {
811afd8f
AG
157 /* if we have enough rate information for battery */
158 if (lx_b->b->percentage != 100) {
159 int hours = lx_b->b->seconds / 3600;
160 int left_seconds = lx_b->b->seconds - 3600 * hours;
161 int minutes = left_seconds / 60;
162 tooltip = g_strdup_printf(
163 _("Battery: %d%% charged, %d:%02d left"),
164 lx_b->b->percentage,
165 hours,
166 minutes );
167 } else {
168 tooltip = g_strdup_printf(
169 _("Battery: %d%% charged"),
170 100 );
171 }
f855ac58
HG
172 }
173
5c0c599d 174 if (!lx_b->show_extended_information) {
811afd8f 175 return tooltip;
5c0c599d
HG
176 }
177
9c9d3a00 178 if (b->energy_full_design != -1)
811afd8f 179 append(&tooltip, _("\n%sEnergy full design:\t\t%5d mWh"), indent, b->energy_full_design);
9c9d3a00 180 if (b->energy_full != -1)
811afd8f 181 append(&tooltip, _("\n%sEnergy full:\t\t\t%5d mWh"), indent, b->energy_full);
9c9d3a00 182 if (b->energy_now != -1)
811afd8f 183 append(&tooltip, _("\n%sEnergy now:\t\t\t%5d mWh"), indent, b->energy_now);
9c9d3a00 184 if (b->power_now != -1)
811afd8f 185 append(&tooltip, _("\n%sPower now:\t\t\t%5d mW"), indent, b->power_now);
9c9d3a00
HG
186
187 if (b->charge_full_design != -1)
811afd8f 188 append(&tooltip, _("\n%sCharge full design:\t%5d mAh"), indent, b->charge_full_design);
9c9d3a00 189 if (b->charge_full != -1)
811afd8f 190 append(&tooltip, _("\n%sCharge full:\t\t\t%5d mAh"), indent, b->charge_full);
9c9d3a00 191 if (b->charge_now != -1)
811afd8f 192 append(&tooltip, _("\n%sCharge now:\t\t\t%5d mAh"), indent, b->charge_now);
9c9d3a00 193 if (b->current_now != -1)
811afd8f 194 append(&tooltip, _("\n%sCurrent now:\t\t\t%5d mA"), indent, b->current_now);
9c9d3a00
HG
195
196 if (b->voltage_now != -1)
811afd8f 197 append(&tooltip, _("\n%sCurrent Voltage:\t\t%.3lf V"), indent, b->voltage_now / 1000.0);
9c9d3a00 198
f855ac58
HG
199 return tooltip;
200}
201
5c0c599d
HG
202static void set_tooltip_text(lx_battery* lx_b)
203{
029d9f0c 204 if (lx_b->b == NULL)
811afd8f 205 return;
5c0c599d
HG
206 gboolean isCharging = battery_is_charging(lx_b->b);
207 gchar *tooltip = make_tooltip(lx_b, isCharging);
208 gtk_widget_set_tooltip_text(lx_b->drawingArea, tooltip);
209 g_free(tooltip);
210}
8f11d5f8 211
802d5540
HJYP
212/* FIXME:
213 Don't repaint if percentage of remaining charge and remaining time aren't changed. */
c887dfa0 214void update_display(lx_battery *lx_b, gboolean repaint) {
f176735a 215 cairo_t *cr;
c887dfa0
JH
216 battery *b = lx_b->b;
217 /* unit: mW */
7f9ba86b 218 int rate;
eef7d6f6 219 gboolean isCharging;
8f11d5f8 220
c887dfa0 221 if (! lx_b->pixmap )
802d5540 222 return;
8f11d5f8 223
f176735a
RM
224 cr = cairo_create(lx_b->pixmap);
225 cairo_set_line_width (cr, 1.0);
226
dc9d3b94
HG
227 /* draw background */
228 gdk_cairo_set_source_color(cr, &lx_b->background);
229 cairo_rectangle(cr, 0, 0, lx_b->width, lx_b->height);
230 cairo_fill(cr);
231
c887dfa0 232 /* no battery is found */
811afd8f 233 if( b == NULL )
802d5540 234 {
811afd8f 235 gtk_widget_set_tooltip_text( lx_b->drawingArea, _("No batteries found") );
f50948df
AG
236 if (lx_b->hide_if_no_battery)
237 {
238 gtk_widget_hide(gtk_widget_get_parent(lx_b->drawingArea));
239 repaint = FALSE;
240 }
811afd8f 241 goto update_done;
c887dfa0 242 }
811afd8f 243
c887dfa0 244 /* fixme: only one battery supported */
8f11d5f8 245
7f9ba86b 246 rate = lx_b->b->current_now;
c887dfa0 247 isCharging = battery_is_charging ( b );
811afd8f 248
c887dfa0 249 /* Consider running the alarm command */
b37f46a0 250 if ( !isCharging && rate > 0 &&
6a1a639e 251 ( ( battery_get_remaining( b ) / 60 ) < (int)lx_b->alarmTime ) )
c887dfa0 252 {
811afd8f
AG
253 /* Shrug this should be done using glibs process functions */
254 /* Alarms should not run concurrently; determine whether an alarm is
255 already running */
256 int alarmCanRun;
257 sem_getvalue(&(lx_b->alarmProcessLock), &alarmCanRun);
258
259 /* Run the alarm command if it isn't already running */
260 if (alarmCanRun) {
261
262 Alarm *a = (Alarm *) malloc(sizeof(Alarm));
263 a->command = lx_b->alarmCommand;
264 a->lock = &(lx_b->alarmProcessLock);
265
266 /* Manage the alarm process in a new thread, which which will be
267 responsible for freeing the alarm struct it's given */
268 pthread_t alarmThread;
269 pthread_create(&alarmThread, NULL, alarmProcess, a);
270 }
c887dfa0 271 }
8f11d5f8 272
5c0c599d 273 set_tooltip_text(lx_b);
eef7d6f6 274
4ebd3285 275 int chargeLevel = lx_b->b->percentage * lx_b->length / 100;
eef7d6f6 276
cfde283a 277 if (lx_b->orientation == GTK_ORIENTATION_HORIZONTAL) {
eef7d6f6 278
811afd8f
AG
279 /* Draw the battery bar vertically, using color 1 for the left half and
280 color 2 for the right half */
f176735a
RM
281 gdk_cairo_set_source_color(cr,
282 isCharging ? &lx_b->charging1 : &lx_b->discharging1);
4ebd3285
AG
283 cairo_rectangle(cr, 0, lx_b->height - chargeLevel,
284 lx_b->width / 2, chargeLevel);
f176735a
RM
285 cairo_fill(cr);
286 gdk_cairo_set_source_color(cr,
287 isCharging ? &lx_b->charging2 : &lx_b->discharging2);
4ebd3285
AG
288 cairo_rectangle(cr, lx_b->width / 2, lx_b->height - chargeLevel,
289 (lx_b->width + 1) / 2, chargeLevel);
f176735a 290 cairo_fill(cr);
eef7d6f6
HG
291
292 }
293 else {
294
811afd8f
AG
295 /* Draw the battery bar horizontally, using color 1 for the top half and
296 color 2 for the bottom half */
f176735a
RM
297 gdk_cairo_set_source_color(cr,
298 isCharging ? &lx_b->charging1 : &lx_b->discharging1);
4ebd3285 299 cairo_rectangle(cr, 0, 0, chargeLevel, lx_b->height / 2);
f176735a
RM
300 cairo_fill(cr);
301 gdk_cairo_set_source_color(cr,
302 isCharging ? &lx_b->charging2 : &lx_b->discharging2);
4ebd3285
AG
303 cairo_rectangle(cr, 0, (lx_b->height + 1) / 2,
304 chargeLevel, lx_b->height / 2);
f176735a 305 cairo_fill(cr);
eef7d6f6
HG
306
307 }
f50948df 308 gtk_widget_show(gtk_widget_get_parent(lx_b->drawingArea));
dc9d3b94
HG
309
310update_done:
eef7d6f6 311 if( repaint )
811afd8f 312 gtk_widget_queue_draw( lx_b->drawingArea );
e4f8b15f
HG
313
314 check_cairo_status(cr);
315 cairo_destroy(cr);
8f11d5f8
HJYP
316}
317
71671bf6 318/* This callback is called every 3 seconds */
c887dfa0 319static int update_timout(lx_battery *lx_b) {
dc9d3b94 320 battery *bat;
251cfd3e
AG
321 if (g_source_is_destroyed(g_main_current_source()))
322 return FALSE;
52cb5b58 323 GDK_THREADS_ENTER();
c887dfa0
JH
324 lx_b->state_elapsed_time++;
325 lx_b->info_elapsed_time++;
71671bf6 326
dc9d3b94
HG
327 bat = battery_update( lx_b->b );
328 if (bat == NULL)
329 {
811afd8f 330 battery_free(lx_b->b);
dc9d3b94 331
811afd8f
AG
332 /* maybe in the mean time a battery has been inserted. */
333 lx_b->b = battery_get();
dc9d3b94 334 }
802d5540 335
c887dfa0 336 update_display( lx_b, TRUE );
802d5540 337
52cb5b58 338 GDK_THREADS_LEAVE();
7c2a8701 339 return TRUE;
52cb5b58 340}
8f11d5f8
HJYP
341
342/* An update will be performed whenever the user clicks on the charge bar */
813c2d44 343static gboolean buttonPressEvent(GtkWidget *p, GdkEventButton *event,
a7bd16a4 344 LXPanel *panel)
813c2d44
AG
345{
346 lx_battery *lx_b = lxpanel_plugin_get_data(p);
8f11d5f8 347
c887dfa0 348 update_display(lx_b, TRUE);
f50948df 349 /* FIXME: open some application for lid/power management may be? */
8f11d5f8 350
1639d6b3 351 return FALSE;
8f11d5f8
HJYP
352}
353
8f11d5f8 354static gint configureEvent(GtkWidget *widget, GdkEventConfigure *event,
175f73d1
AG
355 lx_battery *lx_b)
356{
357 GtkAllocation allocation;
8f11d5f8
HJYP
358
359 ENTER;
360
175f73d1 361 gtk_widget_get_allocation(widget, &allocation);
f50948df
AG
362 if (allocation.width <= 1 && allocation.height <= 1)
363 {
364 /* If plugin is hidden currently then we get 1x1 here */
365 RET(TRUE);
366 }
367
c887dfa0 368 if (lx_b->pixmap)
f176735a 369 cairo_surface_destroy(lx_b->pixmap);
8f11d5f8
HJYP
370
371 /* Update the plugin's dimensions */
175f73d1
AG
372 lx_b->width = allocation.width;
373 lx_b->height = allocation.height;
cfde283a 374 if (lx_b->orientation == GTK_ORIENTATION_HORIZONTAL) {
c887dfa0 375 lx_b->length = lx_b->height;
8f11d5f8
HJYP
376 }
377 else {
c887dfa0 378 lx_b->length = lx_b->width;
8f11d5f8
HJYP
379 }
380
175f73d1
AG
381 lx_b->pixmap = cairo_image_surface_create (CAIRO_FORMAT_RGB24, allocation.width,
382 allocation.height);
e4f8b15f 383 check_cairo_surface_status(&lx_b->pixmap);
8f11d5f8
HJYP
384
385 /* Perform an update so the bar will look right in its new orientation */
c887dfa0 386 update_display(lx_b, FALSE);
8f11d5f8 387
4ebd3285
AG
388 /* we enforce border width here as it seems GtkEventBox doesn't apply it initially */
389 gtk_container_set_border_width(lx_b->box, lx_b->border);
390
8f11d5f8 391 RET(TRUE);
8f11d5f8
HJYP
392}
393
394
c887dfa0 395static gint exposeEvent(GtkWidget *widget, GdkEventExpose *event, lx_battery *lx_b) {
8f11d5f8 396
e4f8b15f 397 ENTER;
175f73d1
AG
398 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
399 GtkStyle *style = gtk_widget_get_style(lx_b->drawingArea);
400
f176735a
RM
401 gdk_cairo_region(cr, event->region);
402 cairo_clip(cr);
8f11d5f8 403
175f73d1 404 gdk_cairo_set_source_color(cr, &style->black);
f176735a
RM
405 cairo_set_source_surface(cr, lx_b->pixmap, 0, 0);
406 cairo_paint(cr);
e4f8b15f
HG
407
408 check_cairo_status(cr);
f176735a 409 cairo_destroy(cr);
e751fe05 410
8f11d5f8 411 RET(FALSE);
8f11d5f8
HJYP
412}
413
4ebd3285
AG
414/* updates length, border, and height/width appropriate to orientation */
415static void updateSizes(lx_battery *b)
416{
417 b->length = panel_get_height(b->panel);
418 b->border = MIN(b->requestedBorder, (MAX(1, b->length) - 1) / 2);
419 b->length -= 2 * b->border;
420 if (b->orientation == GTK_ORIENTATION_HORIZONTAL)
421 b->height = b->length;
422 else
423 b->width = b->length;
424}
425
8f11d5f8 426
a7bd16a4 427static GtkWidget * constructor(LXPanel *panel, config_setting_t *settings)
8f11d5f8 428{
8f11d5f8
HJYP
429 ENTER;
430
c887dfa0 431 lx_battery *lx_b;
813c2d44
AG
432 GtkWidget *p;
433 const char *str;
434 int tmp_int;
435
436 lx_b = g_new0(lx_battery, 1);
c887dfa0
JH
437
438 /* get available battery */
439 lx_b->b = battery_get ();
813c2d44
AG
440
441 p = gtk_event_box_new();
442 lxpanel_plugin_set_data(p, lx_b, destructor);
09fa171b 443 gtk_widget_set_has_window(p, FALSE);
4542c20d 444
4ebd3285 445 lx_b->box = GTK_CONTAINER(p);
c887dfa0
JH
446 lx_b->drawingArea = gtk_drawing_area_new();
447 gtk_widget_add_events( lx_b->drawingArea, GDK_BUTTON_PRESS_MASK );
8f11d5f8 448
4ebd3285 449 gtk_container_add(lx_b->box, lx_b->drawingArea);
4542c20d 450
813c2d44 451 lx_b->orientation = panel_get_orientation(panel);
8f11d5f8 452
c887dfa0 453 gtk_widget_show(lx_b->drawingArea);
8f11d5f8 454
c887dfa0 455 sem_init(&(lx_b->alarmProcessLock), 0, 1);
8f11d5f8 456
c887dfa0
JH
457 lx_b->alarmCommand = lx_b->backgroundColor = lx_b->chargingColor1 = lx_b->chargingColor2
458 = lx_b->dischargingColor1 = lx_b->dischargingColor2 = NULL;
8f11d5f8
HJYP
459
460 /* Set default values for integers */
c887dfa0
JH
461 lx_b->alarmTime = 5;
462 lx_b->requestedBorder = 1;
4ebd3285 463 lx_b->thickness = 8;
8f11d5f8 464
813c2d44
AG
465 /* remember instance data */
466 lx_b->panel = panel;
467 lx_b->settings = settings;
8f11d5f8 468
06e29ce1 469 lx_b->show_extended_information = FALSE;
5c0c599d 470
6a1a639e
AG
471 if (config_setting_lookup_int(settings, "HideIfNoBattery", &tmp_int))
472 lx_b->hide_if_no_battery = (tmp_int != 0);
813c2d44
AG
473 if (config_setting_lookup_string(settings, "AlarmCommand", &str))
474 lx_b->alarmCommand = g_strdup(str);
475 if (config_setting_lookup_string(settings, "BackgroundColor", &str))
476 lx_b->backgroundColor = g_strdup(str);
477 if (config_setting_lookup_string(settings, "ChargingColor1", &str))
478 lx_b->chargingColor1 = g_strdup(str);
479 if (config_setting_lookup_string(settings, "ChargingColor2", &str))
480 lx_b->chargingColor2 = g_strdup(str);
481 if (config_setting_lookup_string(settings, "DischargingColor1", &str))
482 lx_b->dischargingColor1 = g_strdup(str);
483 if (config_setting_lookup_string(settings, "DischargingColor2", &str))
484 lx_b->dischargingColor2 = g_strdup(str);
6a1a639e
AG
485 if (config_setting_lookup_int(settings, "AlarmTime", &tmp_int))
486 lx_b->alarmTime = MAX(0, tmp_int);
487 if (config_setting_lookup_int(settings, "BorderWidth", &tmp_int))
4ebd3285
AG
488 lx_b->requestedBorder = CLAMP(tmp_int, 0, 6);
489 if (config_setting_lookup_int(settings, "Size", &tmp_int))
813c2d44 490 lx_b->thickness = MAX(1, tmp_int);
813c2d44
AG
491 if (config_setting_lookup_int(settings, "ShowExtendedInformation", &tmp_int))
492 lx_b->show_extended_information = (tmp_int != 0);
8f11d5f8
HJYP
493
494 /* Make sure the border value is acceptable */
4ebd3285
AG
495 updateSizes(lx_b);
496 if (lx_b->orientation == GTK_ORIENTATION_HORIZONTAL)
497 {
498 lx_b->width = lx_b->thickness;
499 gtk_widget_set_size_request(lx_b->drawingArea, lx_b->width, -1);
500 }
501 else
502 {
503 lx_b->height = lx_b->thickness;
504 gtk_widget_set_size_request(lx_b->drawingArea, -1, lx_b->height);
505 }
506 gtk_container_set_border_width(lx_b->box, lx_b->border);
507
508 g_signal_connect (G_OBJECT (lx_b->drawingArea),"configure-event",
509 G_CALLBACK (configureEvent), (gpointer) lx_b);
510 g_signal_connect (G_OBJECT (lx_b->drawingArea), "expose-event",
511 G_CALLBACK (exposeEvent), (gpointer) lx_b);
8f11d5f8
HJYP
512
513 /* Apply more default options */
c887dfa0 514 if (! lx_b->alarmCommand)
2bbdeeca 515 lx_b->alarmCommand = g_strconcat("xmessage ", _("Battery low"), NULL);
c887dfa0
JH
516 if (! lx_b->backgroundColor)
517 lx_b->backgroundColor = g_strdup("black");
518 if (! lx_b->chargingColor1)
519 lx_b->chargingColor1 = g_strdup("#28f200");
520 if (! lx_b->chargingColor2)
521 lx_b->chargingColor2 = g_strdup("#22cc00");
522 if (! lx_b->dischargingColor1)
523 lx_b->dischargingColor1 = g_strdup("#ffee00");
524 if (! lx_b->dischargingColor2)
525 lx_b->dischargingColor2 = g_strdup("#d9ca00");
526
527 gdk_color_parse(lx_b->backgroundColor, &lx_b->background);
528 gdk_color_parse(lx_b->chargingColor1, &lx_b->charging1);
529 gdk_color_parse(lx_b->chargingColor2, &lx_b->charging2);
530 gdk_color_parse(lx_b->dischargingColor1, &lx_b->discharging1);
531 gdk_color_parse(lx_b->dischargingColor2, &lx_b->discharging2);
8f11d5f8
HJYP
532
533 /* Start the update loop */
13d82f29 534 lx_b->timer = g_timeout_add_seconds( 9, (GSourceFunc) update_timout, (gpointer) lx_b);
c887dfa0 535
813c2d44 536 RET(p);
8f11d5f8
HJYP
537}
538
539
540static void
813c2d44 541destructor(gpointer data)
8f11d5f8 542{
8f11d5f8
HJYP
543 ENTER;
544
813c2d44 545 lx_battery *b = (lx_battery *)data;
52cb5b58 546
029d9f0c 547 if (b->b != NULL)
811afd8f 548 battery_free(b->b);
029d9f0c 549
52cb5b58 550 if (b->pixmap)
f176735a 551 cairo_surface_destroy(b->pixmap);
8f11d5f8 552
8f11d5f8
HJYP
553 g_free(b->alarmCommand);
554 g_free(b->backgroundColor);
555 g_free(b->chargingColor1);
556 g_free(b->chargingColor2);
557 g_free(b->dischargingColor1);
558 g_free(b->dischargingColor2);
802d5540 559
8f11d5f8 560 g_free(b->rateSamples);
8f11d5f8 561 sem_destroy(&(b->alarmProcessLock));
3396dfea 562 if (b->timer)
563 g_source_remove(b->timer);
8f11d5f8
HJYP
564 g_free(b);
565
566 RET();
567
568}
569
570
a7bd16a4 571static void orientation(LXPanel *panel, GtkWidget *p) {
8f11d5f8
HJYP
572
573 ENTER;
574
813c2d44 575 lx_battery *b = lxpanel_plugin_get_data(p);
8f11d5f8 576
813c2d44
AG
577 if (b->orientation != panel_get_orientation(panel)) {
578 b->orientation = panel_get_orientation(panel);
4ebd3285
AG
579 updateSizes(b);
580 if (b->orientation == GTK_ORIENTATION_HORIZONTAL)
581 {
582 b->width = b->thickness;
583 gtk_widget_set_size_request(b->drawingArea, b->width, -1);
584 }
585 else
586 {
587 b->height = b->thickness;
588 gtk_widget_set_size_request(b->drawingArea, -1, b->height);
589 }
8f11d5f8
HJYP
590 }
591
592 RET();
593}
594
595
813c2d44 596static gboolean applyConfig(gpointer user_data)
8f11d5f8 597{
8f11d5f8
HJYP
598 ENTER;
599
813c2d44 600 lx_battery *b = lxpanel_plugin_get_data(user_data);
8f11d5f8
HJYP
601
602 /* Update colors */
603 if (b->backgroundColor &&
813c2d44
AG
604 gdk_color_parse(b->backgroundColor, &b->background))
605 config_group_set_string(b->settings, "BackgroundColor", b->backgroundColor);
606 if (b->chargingColor1 && gdk_color_parse(b->chargingColor1, &b->charging1))
607 config_group_set_string(b->settings, "ChargingColor1", b->chargingColor1);
608 if (b->chargingColor2 && gdk_color_parse(b->chargingColor2, &b->charging2))
609 config_group_set_string(b->settings, "ChargingColor2", b->chargingColor2);
8f11d5f8 610 if (b->dischargingColor1 &&
813c2d44
AG
611 gdk_color_parse(b->dischargingColor1, &b->discharging1))
612 config_group_set_string(b->settings, "DischargingColor1", b->dischargingColor1);
8f11d5f8 613 if (b->dischargingColor2 &&
813c2d44
AG
614 gdk_color_parse(b->dischargingColor2, &b->discharging2))
615 config_group_set_string(b->settings, "DischargingColor2", b->dischargingColor2);
8f11d5f8 616
7538019a
AG
617 /* Make sure it is at least 1 px */
618 if (b->thickness < 1)
619 b->thickness = 1;
620
e751fe05 621 /* Make sure the border value is acceptable */
4ebd3285
AG
622 b->requestedBorder = MIN(b->requestedBorder, 6);
623 updateSizes(b);
8f11d5f8 624
8f11d5f8 625 /* Resize the widget */
4ebd3285 626 gtk_container_set_border_width(b->box, b->border);
cfde283a 627 if (b->orientation == GTK_ORIENTATION_HORIZONTAL)
4ebd3285 628 {
8f11d5f8 629 b->width = b->thickness;
4ebd3285
AG
630 gtk_widget_set_size_request(b->drawingArea, b->width, -1);
631 }
8f11d5f8 632 else
4ebd3285 633 {
8f11d5f8 634 b->height = b->thickness;
4ebd3285
AG
635 gtk_widget_set_size_request(b->drawingArea, -1, b->height);
636 }
f50948df
AG
637 /* ensure visibility if requested */
638 if (!b->hide_if_no_battery)
639 gtk_widget_show(user_data);
640 else if (b->b == NULL)
641 gtk_widget_hide(user_data);
8f11d5f8 642
2bbdeeca
AG
643 if (b->alarmCommand == NULL)
644 b->alarmCommand = g_strconcat("xmessage ", _("Battery low"), NULL);
645
5c0c599d
HG
646 /* update tooltip */
647 set_tooltip_text(b);
648
813c2d44 649 /* update settings */
813c2d44 650 config_group_set_int(b->settings, "HideIfNoBattery", b->hide_if_no_battery);
813c2d44
AG
651 config_group_set_string(b->settings, "AlarmCommand", b->alarmCommand);
652 config_group_set_int(b->settings, "AlarmTime", b->alarmTime);
653 config_group_set_int(b->settings, "BorderWidth", b->requestedBorder);
654 config_group_set_int(b->settings, "Size", b->thickness);
655 config_group_set_int(b->settings, "ShowExtendedInformation",
656 b->show_extended_information);
657
6a1a639e 658 RET(FALSE);
8f11d5f8
HJYP
659}
660
661
752ee4e2 662static GtkWidget *config(LXPanel *panel, GtkWidget *p) {
813c2d44 663 lx_battery *b = lxpanel_plugin_get_data(p);
131514c9 664 return lxpanel_generic_config_dlg(_("Battery Monitor"),
813c2d44 665 panel, applyConfig, p,
e2957bd2 666 _("Hide if there is no battery"), &b->hide_if_no_battery, CONF_TYPE_BOOL,
e2957bd2
HJYP
667 _("Alarm command"), &b->alarmCommand, CONF_TYPE_STR,
668 _("Alarm time (minutes left)"), &b->alarmTime, CONF_TYPE_INT,
669 _("Background color"), &b->backgroundColor, CONF_TYPE_STR,
670 _("Charging color 1"), &b->chargingColor1, CONF_TYPE_STR,
671 _("Charging color 2"), &b->chargingColor2, CONF_TYPE_STR,
672 _("Discharging color 1"), &b->dischargingColor1, CONF_TYPE_STR,
673 _("Discharging color 2"), &b->dischargingColor2, CONF_TYPE_STR,
674 _("Border width"), &b->requestedBorder, CONF_TYPE_INT,
675 _("Size"), &b->thickness, CONF_TYPE_INT,
5c0c599d 676 _("Show Extended Information"), &b->show_extended_information, CONF_TYPE_BOOL,
8f11d5f8 677 NULL);
8f11d5f8
HJYP
678}
679
680
813c2d44 681FM_DEFINE_MODULE(lxpanel_gtk, batt)
8f11d5f8 682
813c2d44
AG
683/* Plugin descriptor. */
684LXPanelPluginInit fm_module_init_lxpanel_gtk = {
3c3e9c9e 685 .name = N_("Battery Monitor"),
3c3e9c9e
HG
686 .description = N_("Display battery status using ACPI"),
687
813c2d44 688 .new_instance = constructor,
3c3e9c9e 689 .config = config,
813c2d44
AG
690 .reconfigure = orientation,
691 .button_press_event = buttonPressEvent
8f11d5f8 692};
f855ac58
HG
693
694
695/* vim: set sw=4 sts=4 : */