aboutsummaryrefslogtreecommitdiffstats
path: root/applets/battery/battery.c
blob: 9e6196b89d7b1709ff30f6b80f28e67f1458d99c (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
/* 
 * (C) 2006 OpenedHand Ltd.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *
 * Licensed under the GPL v2 or greater.
 */

#include <apm.h>
#include <string.h>
#include <matchbox-panel/mb-panel.h>
#include <matchbox-panel/mb-panel-scaling-image.h>

typedef struct {
        MBPanelScalingImage *image;
        const char *last_icon;

        guint timeout_id;
} BatteryApplet;

/* Applet destroyed */
static void
battery_applet_free (BatteryApplet *applet)
{
        g_source_remove (applet->timeout_id);

        g_slice_free (BatteryApplet, applet);
}

/* Called every 5 minutes */
static gboolean
timeout (BatteryApplet *applet)
{
        apm_info info;
        const char *icon;

        memset (&info, 0, sizeof (apm_info));
        apm_read (&info);

        if (info.battery_status == BATTERY_STATUS_ABSENT)
                icon = DATADIR "/ac-adapter.png";
        else {
                if (info.ac_line_status == AC_LINE_STATUS_ON) {
                        if (info.battery_percentage < 10)
                                icon = DATADIR "battery-charging-000.png";
                        else if (info.battery_percentage < 30)
                                icon = DATADIR "battery-charging-020.png";
                        else if (info.battery_percentage < 50)
                                icon = DATADIR "battery-charging-040.png";
                        else if (info.battery_percentage < 70)
                                icon = DATADIR "battery-charging-060.png";
                        else if (info.battery_percentage < 90)
                                icon = DATADIR "battery-charging-080.png";
                        else
                                icon = DATADIR "battery-charging-100.png";
                } else {
                        if (info.battery_percentage < 10)
                                icon = DATADIR "battery-discharging-000.png";
                        else if (info.battery_percentage < 30)
                                icon = DATADIR "battery-discharging-020.png";
                        else if (info.battery_percentage < 50)
                                icon = DATADIR "battery-discharging-040.png";
                        else if (info.battery_percentage < 70)
                                icon = DATADIR "battery-discharging-060.png";
                        else if (info.battery_percentage < 90)
                                icon = DATADIR "battery-discharging-080.png";
                        else
                                icon = DATADIR "battery-discharging-100.png";
                }
        }

        /* Don't recreate pixbuf if we will display the same image */
        if (icon == applet->last_icon)
                return TRUE;

        applet->last_icon = icon;
        
        /* Load icon */
        mb_panel_scaling_image_set_icon (applet->image, icon);

        /* Keep going */
        return TRUE;
}

G_MODULE_EXPORT GtkWidget *
mb_panel_applet_create (const char    *id,
                        GtkOrientation orientation)
{
        BatteryApplet *applet;
        GtkWidget *image;

        /* Check that we have APM support */
        if (1 == apm_exists ()) {
                g_warning ("No APM support");

                return NULL;
        }

        /* Create applet data structure */
        applet = g_slice_new (BatteryApplet);

        applet->last_icon = NULL;

        /* Create image */
        image = mb_panel_scaling_image_new (orientation, NULL);
        applet->image = MB_PANEL_SCALING_IMAGE (image);

        gtk_widget_set_name (image, "MatchboxPanelBatteryMonitor");

        g_object_weak_ref (G_OBJECT (image),
                           (GWeakNotify) battery_applet_free,
                           applet);

        /* Set up a timeout that will be called every 5 minutes */
        applet->timeout_id = g_timeout_add (5 * 60 * 1000,
                                            (GSourceFunc) timeout,
                                            applet);

        timeout (applet);

        /* Show! */
        gtk_widget_show (image);

        return image;
}