aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog8
-rw-r--r--applets/Makefile.am2
-rw-r--r--applets/brightness/Makefile.am15
-rw-r--r--applets/brightness/brightness.c162
-rw-r--r--applets/brightness/data/Makefile.am6
-rw-r--r--applets/brightness/data/brightness-max.pngbin0 -> 1544 bytes
-rw-r--r--applets/brightness/data/brightness-medium.pngbin0 -> 1453 bytes
-rw-r--r--applets/brightness/data/brightness-min.pngbin0 -> 442 bytes
-rw-r--r--configure.ac2
9 files changed, 193 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 7ca7ff4..2c064f6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,9 +1,15 @@
+2008-02-04 Ross Burton <ross@openedhand.com>
+
+ * configure.ac:
+ * applets/Makefile.am:
+ * applets/brightness/*:
+ Add a brightness applet, thanks Manuel Teira.
+
2008-02-01 Thomas Wood <thomas@openedhand.com>
* applets/windowselector/mb-applet-windowselector.png:
Updated icon
-
2008-01-17 Ross Burton <ross@openedhand.com>
* applets/systray/na-tray-manager.c:
diff --git a/applets/Makefile.am b/applets/Makefile.am
index d5e17a3..dd989f9 100644
--- a/applets/Makefile.am
+++ b/applets/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = clock launcher systray showdesktop windowselector
+SUBDIRS = brightness clock launcher systray showdesktop windowselector
if HAVE_LIBAPM
SUBDIRS += battery
diff --git a/applets/brightness/Makefile.am b/applets/brightness/Makefile.am
new file mode 100644
index 0000000..e26fa42
--- /dev/null
+++ b/applets/brightness/Makefile.am
@@ -0,0 +1,15 @@
+SUBDIRS = data
+
+AM_CPPFLAGS=-DPKGDATADIR=\"$(pkgdatadir)\" \
+ -DGETTEXT_PACKAGE=\"matchbox-panel\" \
+ -DDATADIR=\"$(pkgdatadir)/brightness/\"
+AM_CFLAGS = -Wall -g $(MATCHBOX_PANEL_CFLAGS) \
+ -I$(top_srcdir) -I$(top_builddir) -Werror
+
+appletdir = $(pkglibdir)
+applet_LTLIBRARIES = libbrightness.la
+
+libbrightness_la_SOURCES = brightness.c
+libbrightness_la_LDFLAGS = -avoid-version
+
+MAINTAINERCLEANFILES = Makefile.in
diff --git a/applets/brightness/brightness.c b/applets/brightness/brightness.c
new file mode 100644
index 0000000..65ca6b3
--- /dev/null
+++ b/applets/brightness/brightness.c
@@ -0,0 +1,162 @@
+/*
+ * Author: Manuel Teira <manuel.teira@telefonica.net>
+ *
+ * Licensed under the GPL v2 or greater.
+ */
+
+#include <matchbox-panel/mb-panel.h>
+#include <gtk/gtkscalebutton.h>
+#include <gtk/gtkicontheme.h>
+#include <string.h>
+#include <errno.h>
+
+typedef struct {
+ GtkWidget *scale;
+ gint32 current_brightness;
+ gint32 max_brightness;
+ gchar *sysfs_mbn;
+ gchar *sysfs_bn;
+} BrightnessApplet;
+
+static gint32 sysfs_value_get(const gchar *path)
+{
+ FILE *fd;
+ if ((fd = fopen(path, "r")) == NULL) {
+ g_debug("Error in sysfs_value_get for path %s: %s",
+ path, g_strerror(errno));
+ return -1;
+ }
+ gint32 value;
+ fscanf(fd, "%d", &value);
+ fclose(fd);
+ return value;
+}
+
+static gboolean sysfs_value_set(const gchar *path, gint32 value)
+{
+ FILE *fd;
+ if ((fd = fopen(path, "w")) == NULL) {
+ g_debug ("Error in sysfs_value_set for path %s: %s",
+ path, g_strerror(errno));
+ return FALSE;
+ }
+ fprintf(fd, "%d", value);
+ fclose(fd);
+ return TRUE;
+}
+
+/* Applet destroyed */
+static void
+brightness_applet_free(BrightnessApplet *applet)
+{
+ g_free(applet->sysfs_mbn);
+ g_free(applet->sysfs_bn);
+ g_slice_free(BrightnessApplet, applet);
+}
+
+static void
+brightness_changed_cb(GtkScaleButton *scale,
+ gdouble val,
+ BrightnessApplet *applet)
+{
+ gint32 b = (val * applet->max_brightness) / 100;
+ if (b != applet->current_brightness) {
+ if (sysfs_value_set(applet->sysfs_bn, b)) {
+ applet->current_brightness = b;
+ }
+ }
+}
+
+static gboolean
+applet_initialize(BrightnessApplet *applet)
+{
+ GError *error = NULL;
+ gchar *base = g_build_filename("/sys", "class", "backlight", NULL);
+ GDir *dir = g_dir_open(base, 0, &error);
+ if (dir) {
+ const gchar *entry = NULL;
+ gchar *path = NULL;
+ while ((entry = g_dir_read_name(dir)) != NULL) {
+ path = g_build_filename(base, entry, NULL);
+
+ if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
+ applet->sysfs_bn =
+ g_build_filename(path,
+ "brightness", NULL);
+ applet->sysfs_mbn =
+ g_build_filename(path,
+ "max_brightness", NULL);
+
+ if (g_file_test(applet->sysfs_bn,
+ G_FILE_TEST_IS_REGULAR) &&
+ g_file_test(applet->sysfs_mbn,
+ G_FILE_TEST_IS_REGULAR)) {
+
+ break;
+ } else {
+ g_free(applet->sysfs_bn);
+ g_free(applet->sysfs_mbn);
+ applet->sysfs_bn = NULL;
+ applet->sysfs_mbn = NULL;
+ }
+ }
+ g_free(path);
+ }
+ g_dir_close(dir);
+ } else {
+ g_error("Error opening directory %s", base);
+ }
+
+ if (applet->sysfs_bn && applet->sysfs_mbn) {
+ applet->max_brightness = sysfs_value_get(applet->sysfs_mbn);
+ applet->current_brightness = sysfs_value_get(applet->sysfs_bn);
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}
+
+G_MODULE_EXPORT GtkWidget *
+mb_panel_applet_create(const char *id, GtkOrientation orientation)
+{
+ BrightnessApplet *applet;
+ GtkWidget *scale;
+ GtkIconTheme *theme;
+ static const gchar *icons[] = {
+ "brightness-min",
+ "brightness-max",
+ "brightness-min",
+ "brightness-medium",
+ "brightness-max",
+ NULL};
+
+ theme = gtk_icon_theme_get_default();
+ gtk_icon_theme_append_search_path(theme, DATADIR);
+
+ applet = g_slice_new(BrightnessApplet);
+ scale = gtk_scale_button_new(GTK_ICON_SIZE_BUTTON,
+ 1, 100, 1,
+ icons);
+ applet->scale = scale;
+ gtk_widget_set_name(scale, "MatchboxPanelBrightness");
+
+ if (!applet_initialize(applet)) {
+ g_error("Error initializing applet");
+ brightness_applet_free(applet);
+ return NULL;
+ }
+ gdouble val = (100 * applet->current_brightness) /
+ applet->max_brightness;
+ gtk_scale_button_set_value(GTK_SCALE_BUTTON(scale), val);
+ g_signal_connect(scale,
+ "value-changed",
+ G_CALLBACK(brightness_changed_cb),
+ applet);
+ g_object_weak_ref(G_OBJECT(scale),
+ (GWeakNotify) brightness_applet_free,
+ applet);
+
+ gtk_widget_show(scale);
+
+ return scale;
+}
diff --git a/applets/brightness/data/Makefile.am b/applets/brightness/data/Makefile.am
new file mode 100644
index 0000000..5b619c5
--- /dev/null
+++ b/applets/brightness/data/Makefile.am
@@ -0,0 +1,6 @@
+imagedir = $(pkgdatadir)/brightness
+image_DATA = brightness-min.png \
+ brightness-medium.png \
+ brightness-max.png
+
+MAINTAINERCLEANFILES = Makefile.in
diff --git a/applets/brightness/data/brightness-max.png b/applets/brightness/data/brightness-max.png
new file mode 100644
index 0000000..6d75249
--- /dev/null
+++ b/applets/brightness/data/brightness-max.png
Binary files differ
diff --git a/applets/brightness/data/brightness-medium.png b/applets/brightness/data/brightness-medium.png
new file mode 100644
index 0000000..ac40bd6
--- /dev/null
+++ b/applets/brightness/data/brightness-medium.png
Binary files differ
diff --git a/applets/brightness/data/brightness-min.png b/applets/brightness/data/brightness-min.png
new file mode 100644
index 0000000..09c8498
--- /dev/null
+++ b/applets/brightness/data/brightness-min.png
Binary files differ
diff --git a/configure.ac b/configure.ac
index 1a6f5be..63100b3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -76,6 +76,8 @@ applets/showdesktop/Makefile
applets/windowselector/Makefile
applets/battery/Makefile
applets/battery/data/Makefile
+applets/brightness/Makefile
+applets/brightness/data/Makefile
po/Makefile.in
po/Makefile
])