diff options
19 files changed, 127 insertions, 28 deletions
@@ -1,3 +1,12 @@ +2006-08-22 Jorn Baayen <jorn@openedhand.com> + + * applets/battery/Makefile.am: + * applets/battery/battery.c: (timeout), (mb_panel_applet_create): + * applets/battery/data/Makefile.am: + * configure.ac: + + Use icons from gnome-power-manager for battery applet for now. + 2006-08-21 Jorn Baayen <jorn@openedhand.com> * applets/Makefile.am: diff --git a/applets/battery/Makefile.am b/applets/battery/Makefile.am index a07a1f2..d954f75 100644 --- a/applets/battery/Makefile.am +++ b/applets/battery/Makefile.am @@ -1,4 +1,8 @@ -AM_CPPFLAGS=-DPKGDATADIR=\"$(pkgdatadir)\" -DGETTEXT_PACKAGE=\"matchbox-panel\" +SUBDIRS = data + +AM_CPPFLAGS=-DPKGDATADIR=\"$(pkgdatadir)\" \ + -DGETTEXT_PACKAGE=\"matchbox-panel\" \ + -DDATADIR=\"$(sharedir)/matchbox-panel/battery/\" AM_CFLAGS = -Wall -g $(MATCHBOX_PANEL_CFLAGS) \ -I$(top_srcdir) -I$(top_builddir) -Werror diff --git a/applets/battery/battery.c b/applets/battery/battery.c index fc55743..a99cae3 100644 --- a/applets/battery/battery.c +++ b/applets/battery/battery.c @@ -10,29 +10,88 @@ #include <apm.h> #include <matchbox-panel/mb-panel.h> +typedef struct { + GtkImage *image; + guint size; + const char *last_img; + + guint timeout_id; +} BatteryApplet; + /* Applet destroyed */ static void -destroy_cb (GtkImage *image) +battery_applet_free (BatteryApplet *applet) { - guint timeout_id; - - /* Remove timeout */ - timeout_id = - GPOINTER_TO_UINT - (g_object_get_data (G_OBJECT (image), "timeout-id")); + g_source_remove (applet->timeout_id); - g_source_remove (timeout_id); + g_slice_free (BatteryApplet, applet); } -/* Called every minute */ +/* Called every 5 minutes */ static gboolean -timeout (GtkImage *image) +timeout (BatteryApplet *applet) { apm_info info; + const char *img; + GdkPixbuf *pixbuf; + GError *error; apm_read (&info); - /* TODO choose appropriate graphics to display */ + if (info.battery_status == BATTERY_STATUS_ABSENT) + img = DATADIR "/ac-adapter.png"; + else { + if (info.ac_line_status == AC_LINE_STATUS_ON) { + if (info.battery_percentage < 10) + img = DATADIR "battery-charging-000.png"; + else if (info.battery_percentage < 30) + img = DATADIR "battery-charging-020.png"; + else if (info.battery_percentage < 50) + img = DATADIR "battery-charging-040.png"; + else if (info.battery_percentage < 70) + img = DATADIR "battery-charging-060.png"; + else if (info.battery_percentage < 90) + img = DATADIR "battery-charging-080.png"; + else + img = DATADIR "battery-charging-100.png"; + } else { + if (info.battery_percentage < 10) + img = DATADIR "battery-discharging-000.png"; + else if (info.battery_percentage < 30) + img = DATADIR "battery-discharging-020.png"; + else if (info.battery_percentage < 50) + img = DATADIR "battery-discharging-040.png"; + else if (info.battery_percentage < 70) + img = DATADIR "battery-discharging-060.png"; + else if (info.battery_percentage < 90) + img = DATADIR "battery-discharging-080.png"; + else + img = DATADIR "battery-discharging-100.png"; + } + } + + /* Don't recreate pixbuf if we will display the same image */ + if (img == applet->last_img) + return TRUE; + + applet->last_img = img; + + /* Load pixbuf */ + error = NULL; + pixbuf = gdk_pixbuf_new_from_file_at_scale (img, + applet->size, + applet->size, + TRUE, + &error); + if (pixbuf) { + gtk_image_set_from_pixbuf (applet->image, pixbuf); + + g_object_unref (pixbuf); + } else { + g_warning (error->message); + + g_error_free (error); + } /* Keep going */ return TRUE; @@ -43,32 +102,41 @@ mb_panel_applet_create (const char *id, int panel_width, int panel_height) { + BatteryApplet *applet; GtkWidget *image; - guint timeout_id; - /* Create label */ + /* Check that we have APM support */ + if (!apm_exists ()) { + g_warning ("No APM support"); + + return NULL; + } + + /* Create applet data structure */ + applet = g_slice_new (BatteryApplet); + + applet->size = MIN (panel_width, panel_height); + applet->last_img = NULL; + + /* Create image */ image = gtk_image_new (); + applet->image = GTK_IMAGE (image); gtk_widget_set_name (image, "MatchboxPanelBatteryMonitor"); - timeout (GTK_IMAGE (image)); + g_object_weak_ref (G_OBJECT (image), + (GWeakNotify) battery_applet_free, + applet); - g_signal_connect (image, - "destroy", - G_CALLBACK (destroy_cb), - NULL); + /* Set up a timeout that will be called every 5 minutes */ + applet->timeout_id = g_timeout_add (5 * 60 * 1000, + (GSourceFunc) timeout, + applet); - /* Set up a timeout that will be called every minute */ - timeout_id = g_timeout_add (60 * 1000, - (GSourceFunc) timeout, - image); - - g_object_set_data (G_OBJECT (image), - "timeout-id", - GUINT_TO_POINTER (timeout_id)); + timeout (applet); /* Show! */ gtk_widget_show (image); return image; -}; +} diff --git a/applets/battery/data/Makefile.am b/applets/battery/data/Makefile.am new file mode 100644 index 0000000..13ab0ee --- /dev/null +++ b/applets/battery/data/Makefile.am @@ -0,0 +1,17 @@ +datadir = $(sharedir)/matchbox-panel/battery +data_DATA = ac-adapter.png \ + battery-charged.png \ + battery-charging-000.png \ + battery-charging-020.png \ + battery-charging-040.png \ + battery-charging-060.png \ + battery-charging-080.png \ + battery-charging-100.png \ + battery-discharging-000.png \ + battery-discharging-020.png \ + battery-discharging-040.png \ + battery-discharging-060.png \ + battery-discharging-080.png \ + battery-discharging-100.png + +MAINTAINERCLEANFILES = Makefile.in diff --git a/applets/battery/data/ac-adapter.png b/applets/battery/data/ac-adapter.png Binary files differnew file mode 100644 index 0000000..da38543 --- /dev/null +++ b/applets/battery/data/ac-adapter.png diff --git a/applets/battery/data/battery-charged.png b/applets/battery/data/battery-charged.png Binary files differnew file mode 100644 index 0000000..f164c73 --- /dev/null +++ b/applets/battery/data/battery-charged.png diff --git a/applets/battery/data/battery-charging-000.png b/applets/battery/data/battery-charging-000.png Binary files differnew file mode 100644 index 0000000..8d60292 --- /dev/null +++ b/applets/battery/data/battery-charging-000.png diff --git a/applets/battery/data/battery-charging-020.png b/applets/battery/data/battery-charging-020.png Binary files differnew file mode 100644 index 0000000..f88cb74 --- /dev/null +++ b/applets/battery/data/battery-charging-020.png diff --git a/applets/battery/data/battery-charging-040.png b/applets/battery/data/battery-charging-040.png Binary files differnew file mode 100644 index 0000000..4a7cb40 --- /dev/null +++ b/applets/battery/data/battery-charging-040.png diff --git a/applets/battery/data/battery-charging-060.png b/applets/battery/data/battery-charging-060.png Binary files differnew file mode 100644 index 0000000..360b2d0 --- /dev/null +++ b/applets/battery/data/battery-charging-060.png diff --git a/applets/battery/data/battery-charging-080.png b/applets/battery/data/battery-charging-080.png Binary files differnew file mode 100644 index 0000000..1605a5f --- /dev/null +++ b/applets/battery/data/battery-charging-080.png diff --git a/applets/battery/data/battery-charging-100.png b/applets/battery/data/battery-charging-100.png Binary files differnew file mode 100644 index 0000000..b5f3417 --- /dev/null +++ b/applets/battery/data/battery-charging-100.png diff --git a/applets/battery/data/battery-discharging-000.png b/applets/battery/data/battery-discharging-000.png Binary files differnew file mode 100644 index 0000000..602dc4e --- /dev/null +++ b/applets/battery/data/battery-discharging-000.png diff --git a/applets/battery/data/battery-discharging-020.png b/applets/battery/data/battery-discharging-020.png Binary files differnew file mode 100644 index 0000000..db9882b --- /dev/null +++ b/applets/battery/data/battery-discharging-020.png diff --git a/applets/battery/data/battery-discharging-040.png b/applets/battery/data/battery-discharging-040.png Binary files differnew file mode 100644 index 0000000..710f788 --- /dev/null +++ b/applets/battery/data/battery-discharging-040.png diff --git a/applets/battery/data/battery-discharging-060.png b/applets/battery/data/battery-discharging-060.png Binary files differnew file mode 100644 index 0000000..070a35f --- /dev/null +++ b/applets/battery/data/battery-discharging-060.png diff --git a/applets/battery/data/battery-discharging-080.png b/applets/battery/data/battery-discharging-080.png Binary files differnew file mode 100644 index 0000000..382ada3 --- /dev/null +++ b/applets/battery/data/battery-discharging-080.png diff --git a/applets/battery/data/battery-discharging-100.png b/applets/battery/data/battery-discharging-100.png Binary files differnew file mode 100644 index 0000000..38ad324 --- /dev/null +++ b/applets/battery/data/battery-discharging-100.png diff --git a/configure.ac b/configure.ac index 45c9485..880f889 100644 --- a/configure.ac +++ b/configure.ac @@ -67,6 +67,7 @@ applets/clock/Makefile applets/launcher/Makefile applets/systray/Makefile applets/battery/Makefile +applets/battery/data/Makefile po/Makefile.in po/Makefile ]) |