Add MCE plugin.
authorSalvatore Iovene <salvatore@iovene.com>
Tue, 8 Dec 2009 19:57:24 +0000 (21:57 +0200)
committerSalvatore Iovene <salvatore@iovene.com>
Tue, 8 Dec 2009 19:57:24 +0000 (21:57 +0200)
debian/rules
modules/Makefile.am
modules/maemo-tweaks-mce-save.c [new file with mode: 0644]
modules/maemo-tweaks-mce.c [new file with mode: 0644]

index 450f751..aed6576 100755 (executable)
@@ -46,4 +46,4 @@ binary-predeb/maemo-tweaks::
 binary-predeb/maemo-tweaks-modules::
        # set the Setuid bit
        chmod 4755 $(CURDIR)/debian/maemo-tweaks-modules/usr/bin/maemo-tweaks-desktop-save
-
+       chmod 4755 $(CURDIR)/debian/maemo-tweaks-modules/usr/bin/maemo-tweaks-mce-save
index 4f2afb1..216dc90 100644 (file)
@@ -5,11 +5,14 @@ AM_CPPFLAGS = \
        -I$(top_srcdir) \
        $(GTK_CFLAGS)   \
        $(HILDON_CFLAGS)        \
-       -DTRANSITIONS=\"/usr/share/hildon-desktop/transitions.ini\"
+       -DTRANSITIONS=\"/usr/share/hildon-desktop/transitions.ini\"     \
+       -DMCE=\"/etc/mce/mce.ini\"
 
 modulesdir = $(libdir)/maemo-tweaks/modules
 
-modules_LTLIBRARIES = libmaemo-tweaks-desktop.la
+modules_LTLIBRARIES = \
+       libmaemo-tweaks-desktop.la \
+       libmaemo-tweaks-mce.la
 
 libmaemo_tweaks_desktop_la_SOURCES = maemo-tweaks-desktop.c
 libmaemo_tweaks_desktop_la_LDFLAGS = -avoid-version -module
@@ -18,8 +21,22 @@ libmaemo_tweaks_desktop_la_LIBADD = \
        $(GTK_LIBS)     \
        $(HILDON_LIBS)
 
-bin_PROGRAMS = maemo-tweaks-desktop-save
+libmaemo_tweaks_mce_la_SOURCES = maemo-tweaks-mce.c
+libmaemo_tweaks_mce_la_LDFLAGS = -avoid-version -module
+libmaemo_tweaks_mce_la_LIBADD = \
+       $(libmaemo_tweaks_section)      \
+       $(GTK_LIBS)     \
+       $(HILDON_LIBS)
+
+bin_PROGRAMS = \
+       maemo-tweaks-desktop-save       \
+       maemo-tweaks-mce-save
+
 maemo_tweaks_desktop_save_SOURCES = maemo-tweaks-desktop-save.c
 maemo_tweaks_desktop_save_LDADD = \
        $(GLIB_LIBS)
 
+maemo_tweaks_mce_save_SOURCES = maemo-tweaks-mce-save.c
+maemo_tweaks_mce_save_LDADD = \
+       $(GLIB_LIBS)
+
diff --git a/modules/maemo-tweaks-mce-save.c b/modules/maemo-tweaks-mce-save.c
new file mode 100644 (file)
index 0000000..62e76ff
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * vim:ts=4:sw=4:et:cindent:cino=(0
+ */ 
+
+#include <stdlib.h>
+#include <glib.h>
+
+#define FILENAME MCE
+
+typedef struct _value_t
+{
+    const gchar *group;
+    const gchar *name;
+    const gchar *value;
+} value_t;
+
+int main (int argc, char *argv[])
+{
+    GKeyFile *kf;
+    value_t v[1];
+    gint i;
+    GError *error = NULL;
+    gchar *data;
+    gsize size;
+
+    if (argc < 2)
+    {
+        exit (EXIT_FAILURE);
+    }
+
+    v[0].group = "PowerKey";
+    v[0].name = "PowerKeyShortAction";
+    v[0].value = argv[1];
+
+    kf = g_key_file_new ();
+    if (!g_key_file_load_from_file (kf, FILENAME,
+                                    G_KEY_FILE_KEEP_COMMENTS |
+                                    G_KEY_FILE_KEEP_TRANSLATIONS,
+                                    &error))
+    {
+        g_warning ("Error while reading %s: %s", FILENAME, error->message);
+        g_error_free (error);
+        exit (EXIT_FAILURE);
+    }
+
+    for (i = 0; i < argc - 1; i++)
+    {
+        g_key_file_set_string (kf, v[i].group, v[i].name, v[i].value);
+    }
+
+    data = g_key_file_to_data (kf, &size, &error);
+    if (data == NULL)
+    {
+        g_warning ("Error while writing keyfile data.");
+        g_error_free (error);
+        exit (EXIT_FAILURE);
+    }
+    
+    if (!g_file_set_contents (FILENAME, data, size, &error))
+    {
+        g_warning ("Error writing %s", FILENAME);
+        g_free (data);
+        g_error_free (error);
+        exit (EXIT_FAILURE);
+    }
+
+    g_free (data);
+    g_key_file_free (kf);
+
+    exit (EXIT_SUCCESS);
+}
+
diff --git a/modules/maemo-tweaks-mce.c b/modules/maemo-tweaks-mce.c
new file mode 100644 (file)
index 0000000..7b313c5
--- /dev/null
@@ -0,0 +1,234 @@
+/*
+ * vim:ts=4:sw=4:et:cindent:cino=(0
+ */ 
+
+#include <config.h>
+
+#include <gtk/gtk.h>
+#include <hildon/hildon-picker-button.h>
+#include <hildon/hildon-touch-selector.h>
+
+#include "libmaemo-tweaks-section/maemo-tweaks-section.h"
+#include "libmaemo-tweaks-section/maemo-tweaks-module.h"
+
+
+#define MAEMO_TWEAKS_TYPE_MCE_SECTION \
+        (maemo_tweaks_mce_section_type)
+#define MAEMO_TWEAKS_MCE_SECTION(obj) \
+        (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+        MAEMO_TWEAKS_TYPE_MCE_SECTION, \
+        MaemoTweaksMceSection))
+#define MAEMO_TWEAKS_MCE_SECTION_CLASS(k) \
+        (G_TYPE_CHECK_CLASS_CAST((k), \
+        MAEMO_TWEAKS_TYPE_MCE_SECTION, \
+        MaemoTweaksMceSectionClass))
+#define MAEMO_TWEAKS_IS_MCE_SECTION(obj) \
+        (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+        MAEMO_TWEAKS_TYPE_MCE_SECTION))
+
+enum {
+    SHORT_POWER_KEY_DISABLED,
+    SHORT_POWER_KEY_MENU,
+    SHORT_POWER_KEY_OFF,
+    SHORT_POWER_KEY_N
+};
+
+typedef struct _picker_t
+{
+    guint index;
+    const gchar *value;
+    const gchar *label;
+} picker_t;
+
+static picker_t spk [] = {
+    {SHORT_POWER_KEY_DISABLED, "disabled", "Disabled"},
+    {SHORT_POWER_KEY_MENU, "menu", "Show menu"},
+    {SHORT_POWER_KEY_OFF, "poweroff", "Power off"}
+};    
+
+typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
+typedef struct _MaemoTweaksMceSectionClass
+               MaemoTweaksMceSectionClass;
+
+struct _MaemoTweaksMceSection
+{
+    MaemoTweaksSection parent_instance;
+
+    GKeyFile *ini;
+    GtkWidget *short_power_key;
+};
+
+struct _MaemoTweaksMceSectionClass
+{
+    MaemoTweaksSectionClass parent_class;
+};
+
+
+static GType maemo_tweaks_mce_section_get_type (GTypeModule *module);
+static void maemo_tweaks_mce_section_class_init
+    (MaemoTweaksMceSectionClass *class);
+static void maemo_tweaks_mce_section_init
+    (MaemoTweaksMceSection *section);
+static void maemo_tweaks_mce_section_dispose (GObject *obj);
+
+static void _save (MaemoTweaksSection *section);
+
+static GType maemo_tweaks_mce_section_type = 0;
+static MaemoTweaksSectionClass *
+    maemo_tweaks_mce_section_parent_class = NULL;
+
+
+G_MODULE_EXPORT void
+maemo_tweaks_module_load (MaemoTweaksModule *module)
+{
+    maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
+}
+
+G_MODULE_EXPORT void
+maemo_tweaks_module_unload (MaemoTweaksModule *module)
+{
+}
+
+static GType
+maemo_tweaks_mce_section_get_type (GTypeModule *module)
+{
+    if (!maemo_tweaks_mce_section_type)
+    {
+        static const GTypeInfo section_info =
+        {
+            sizeof (MaemoTweaksMceSectionClass),
+            (GBaseInitFunc) NULL,
+            (GBaseFinalizeFunc) NULL,
+            (GClassInitFunc) maemo_tweaks_mce_section_class_init,
+            NULL,           /* class_finalize */
+            NULL,           /* class_data     */
+            sizeof (MaemoTweaksMceSection),
+            0,              /* n_preallocs    */
+            (GInstanceInitFunc) maemo_tweaks_mce_section_init
+        };
+
+        maemo_tweaks_mce_section_type =
+            g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
+                                         "MaemoTweaksMceSection",
+                                         &section_info, 0);
+    }
+
+    return maemo_tweaks_mce_section_type;
+}
+
+static void
+maemo_tweaks_mce_section_class_init
+    (MaemoTweaksMceSectionClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+    MaemoTweaksSectionClass *section_class =
+        MAEMO_TWEAKS_SECTION_CLASS (klass);
+
+    maemo_tweaks_mce_section_parent_class =
+        g_type_class_peek_parent (klass);
+
+    section_class->name   = "_Mce";
+    section_class->save = _save;
+
+    object_class->dispose = maemo_tweaks_mce_section_dispose;
+}
+
+GtkWidget * _build_short_power_key (void)
+{
+    gint i;
+    GtkWidget *button, *selector;
+
+    selector = hildon_touch_selector_new_text ();
+    for (i = 0; i < SHORT_POWER_KEY_N; i++)
+    {
+        hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
+                                           spk[i].label);
+    }
+
+    button = hildon_picker_button_new (HILDON_SIZE_AUTO,
+                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL);
+
+    hildon_button_set_title (HILDON_BUTTON (button),
+                             "Power button short press");
+    gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
+    hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
+                                       HILDON_TOUCH_SELECTOR (selector));
+
+    gtk_widget_show (button);
+    return button;
+}
+
+static void
+maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
+{
+    MaemoTweaksSection *iface;
+    gchar *short_power_key_value;
+    gint i;
+
+    section->short_power_key = _build_short_power_key ();
+
+    section->ini = g_key_file_new ();
+
+    if (!g_key_file_load_from_file (section->ini, MCE,
+                                    G_KEY_FILE_NONE, NULL))
+    {
+        g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
+        return;
+    }
+
+    short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
+                                                   "PowerKeyShortAction",
+                                                   NULL);
+
+    for (i = 0; i < SHORT_POWER_KEY_N; i++)
+    {
+        if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
+        {
+            hildon_picker_button_set_active
+                (HILDON_PICKER_BUTTON (section->short_power_key), i);
+        }
+    }
+
+
+    iface = MAEMO_TWEAKS_SECTION (section);
+    iface->widget = gtk_vbox_new (FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
+                        TRUE, TRUE, 0);
+}
+
+static void
+maemo_tweaks_mce_section_dispose (GObject *obj)
+{
+    MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
+    if (section->ini)
+    {
+        g_key_file_free (section->ini);
+        section->ini = NULL;
+    }
+
+    G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
+        (obj);
+}
+
+
+static void _save (MaemoTweaksSection *section)
+{
+    gchar *argv[3];
+    gint active;
+
+    active = hildon_picker_button_get_active
+        (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
+                               (section)->short_power_key));
+
+    argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
+    argv[1] = g_strdup_printf ("%s", spk[active].value);
+    argv[2] = NULL;
+
+    g_spawn_sync ("/home/user", argv, NULL,
+                  G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
+                  NULL, NULL, NULL, NULL, NULL, NULL);
+
+    g_free (argv[0]);
+    g_free (argv[1]);
+}
+