Add the double power key press.
[tweakr] / modules / maemo-tweaks-mce.c
1 /*
2  * vim:ts=4:sw=4:et:cindent:cino=(0
3  */ 
4
5 #include <config.h>
6
7 #include <gtk/gtk.h>
8 #include <hildon/hildon-picker-button.h>
9 #include <hildon/hildon-touch-selector.h>
10
11 #include "libmaemo-tweaks-section/maemo-tweaks-section.h"
12 #include "libmaemo-tweaks-section/maemo-tweaks-module.h"
13
14
15 #define MAEMO_TWEAKS_TYPE_MCE_SECTION \
16         (maemo_tweaks_mce_section_type)
17 #define MAEMO_TWEAKS_MCE_SECTION(obj) \
18         (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
19         MAEMO_TWEAKS_TYPE_MCE_SECTION, \
20         MaemoTweaksMceSection))
21 #define MAEMO_TWEAKS_MCE_SECTION_CLASS(k) \
22         (G_TYPE_CHECK_CLASS_CAST((k), \
23         MAEMO_TWEAKS_TYPE_MCE_SECTION, \
24         MaemoTweaksMceSectionClass))
25 #define MAEMO_TWEAKS_IS_MCE_SECTION(obj) \
26         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
27         MAEMO_TWEAKS_TYPE_MCE_SECTION))
28
29 #define DISABLE_LABEL "Disable"
30 #define SHOW_MENU_LABEL "Show menu"
31 #define POWER_OFF_LABEL "Power off"
32 #define LOCK_LABEL "Lock"
33
34 enum {
35     SHORT_POWER_KEY_DISABLED,
36     SHORT_POWER_KEY_MENU,
37     SHORT_POWER_KEY_OFF,
38     SHORT_POWER_KEY_N
39 };
40
41 enum {
42     LONG_POWER_KEY_DISABLED,
43     LONG_POWER_KEY_MENU,
44     LONG_POWER_KEY_OFF,
45     LONG_POWER_KEY_N
46 };
47
48 enum {
49     DOUBLE_POWER_KEY_DISABLED,
50     DOUBLE_POWER_KEY_MENU,
51     DOUBLE_POWER_KEY_OFF,
52     DOUBLE_POWER_KEY_LOCK,
53     DOUBLE_POWER_KEY_N
54 };
55
56
57 typedef struct _picker_t
58 {
59     guint index;
60     const gchar *value;
61     const gchar *label;
62 } picker_t;
63
64 static picker_t spk [] = {
65     {SHORT_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
66     {SHORT_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
67     {SHORT_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL}
68 };
69
70 static picker_t lpk [] = {
71     {LONG_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
72     {LONG_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
73     {LONG_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL}
74 };
75
76 static picker_t dpk [] = {
77     {DOUBLE_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
78     {DOUBLE_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
79     {DOUBLE_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL},
80     {DOUBLE_POWER_KEY_LOCK, "tklock", LOCK_LABEL}
81 };
82
83
84 typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
85 typedef struct _MaemoTweaksMceSectionClass
86                MaemoTweaksMceSectionClass;
87
88 struct _MaemoTweaksMceSection
89 {
90     MaemoTweaksSection parent_instance;
91
92     GKeyFile *ini;
93     GtkWidget *short_power_key;
94     GtkWidget *long_power_key;
95     GtkWidget *double_power_key;
96
97     guint value_changed : 1;
98 };
99
100 struct _MaemoTweaksMceSectionClass
101 {
102     MaemoTweaksSectionClass parent_class;
103 };
104
105
106 static GType maemo_tweaks_mce_section_get_type (GTypeModule *module);
107 static void maemo_tweaks_mce_section_class_init
108     (MaemoTweaksMceSectionClass *class);
109 static void maemo_tweaks_mce_section_init
110     (MaemoTweaksMceSection *section);
111 static void maemo_tweaks_mce_section_dispose (GObject *obj);
112
113 static void _save (MaemoTweaksSection *section, gboolean *requires_restart);
114
115 static GType maemo_tweaks_mce_section_type = 0;
116 static MaemoTweaksSectionClass *
117     maemo_tweaks_mce_section_parent_class = NULL;
118
119
120 G_MODULE_EXPORT void
121 maemo_tweaks_module_load (MaemoTweaksModule *module)
122 {
123     maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
124 }
125
126 G_MODULE_EXPORT void
127 maemo_tweaks_module_unload (MaemoTweaksModule *module)
128 {
129 }
130
131 static GType
132 maemo_tweaks_mce_section_get_type (GTypeModule *module)
133 {
134     if (!maemo_tweaks_mce_section_type)
135     {
136         static const GTypeInfo section_info =
137         {
138             sizeof (MaemoTweaksMceSectionClass),
139             (GBaseInitFunc) NULL,
140             (GBaseFinalizeFunc) NULL,
141             (GClassInitFunc) maemo_tweaks_mce_section_class_init,
142             NULL,           /* class_finalize */
143             NULL,           /* class_data     */
144             sizeof (MaemoTweaksMceSection),
145             0,              /* n_preallocs    */
146             (GInstanceInitFunc) maemo_tweaks_mce_section_init
147         };
148
149         maemo_tweaks_mce_section_type =
150             g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
151                                          "MaemoTweaksMceSection",
152                                          &section_info, 0);
153     }
154
155     return maemo_tweaks_mce_section_type;
156 }
157
158 static void
159 maemo_tweaks_mce_section_class_init
160     (MaemoTweaksMceSectionClass *klass)
161 {
162     GObjectClass *object_class = G_OBJECT_CLASS (klass);
163     MaemoTweaksSectionClass *section_class =
164         MAEMO_TWEAKS_SECTION_CLASS (klass);
165
166     maemo_tweaks_mce_section_parent_class =
167         g_type_class_peek_parent (klass);
168
169     section_class->name   = "_Mce";
170     section_class->save = _save;
171
172     object_class->dispose = maemo_tweaks_mce_section_dispose;
173 }
174
175 static void _value_changed (HildonPickerButton *b, gpointer user_data)
176 {
177     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (user_data);
178
179     section->value_changed = TRUE;
180 }
181
182 GtkWidget * _build_short_power_key (MaemoTweaksMceSection *section)
183 {
184     gint i;
185     GtkWidget *button, *selector;
186
187     selector = hildon_touch_selector_new_text ();
188     for (i = 0; i < SHORT_POWER_KEY_N; i++)
189     {
190         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
191                                            spk[i].label);
192     }
193
194     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
195                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
196
197     hildon_button_set_title (HILDON_BUTTON (button),
198                              "Power key: short press");
199     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
200     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
201                                        HILDON_TOUCH_SELECTOR (selector));
202
203     g_signal_connect (G_OBJECT (button), "value-changed",
204                       G_CALLBACK (_value_changed), section);
205
206     gtk_widget_show (button);
207     return button;
208 }
209
210 GtkWidget * _build_long_power_key (MaemoTweaksMceSection *section)
211 {
212     gint i;
213     GtkWidget *button, *selector;
214
215     selector = hildon_touch_selector_new_text ();
216     for (i = 0; i < LONG_POWER_KEY_N; i++)
217     {
218         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
219                                            spk[i].label);
220     }
221
222     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
223                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
224
225     hildon_button_set_title (HILDON_BUTTON (button),
226                              "Power key: long press");
227     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
228     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
229                                        HILDON_TOUCH_SELECTOR (selector));
230
231     g_signal_connect (G_OBJECT (button), "value-changed",
232                       G_CALLBACK (_value_changed), section);
233
234     gtk_widget_show (button);
235     return button;
236 }
237
238 GtkWidget * _build_double_power_key (MaemoTweaksMceSection *section)
239 {
240     gint i;
241     GtkWidget *button, *selector;
242
243     selector = hildon_touch_selector_new_text ();
244     for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
245     {
246         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
247                                            dpk[i].label);
248     }
249
250     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
251                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
252
253     hildon_button_set_title (HILDON_BUTTON (button),
254                              "Power key: double press");
255     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
256     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
257                                        HILDON_TOUCH_SELECTOR (selector));
258
259     g_signal_connect (G_OBJECT (button), "value-changed",
260                       G_CALLBACK (_value_changed), section);
261
262     gtk_widget_show (button);
263     return button;
264 }
265
266
267 static void
268 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
269 {
270     MaemoTweaksSection *iface;
271     gchar *short_power_key_value;
272     gchar *long_power_key_value;
273     gchar *double_power_key_value;
274     gint i;
275
276     section->short_power_key = _build_short_power_key (section);
277     section->long_power_key = _build_long_power_key (section);
278     section->double_power_key = _build_double_power_key (section);
279
280     section->ini = g_key_file_new ();
281
282     if (!g_key_file_load_from_file (section->ini, MCE,
283                                     G_KEY_FILE_NONE, NULL))
284     {
285         g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
286         return;
287     }
288
289     short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
290                                                    "PowerKeyShortAction",
291                                                    NULL);
292     long_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
293                                                   "PowerKeyLongAction",
294                                                   NULL);
295     double_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
296                                                     "PowerKeyDoubleAction",
297                                                     NULL);
298
299     for (i = 0; i < SHORT_POWER_KEY_N; i++)
300     {
301         if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
302         {
303             hildon_picker_button_set_active
304                 (HILDON_PICKER_BUTTON (section->short_power_key), i);
305         }
306     }
307
308     for (i = 0; i < LONG_POWER_KEY_N; i++)
309     {
310         if (g_strcmp0 (long_power_key_value, lpk[i].value) == 0)
311         {
312             hildon_picker_button_set_active
313                 (HILDON_PICKER_BUTTON (section->long_power_key), i);
314         }
315     }
316
317     for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
318     {
319         if (g_strcmp0 (double_power_key_value, dpk[i].value) == 0)
320         {
321             hildon_picker_button_set_active
322                 (HILDON_PICKER_BUTTON (section->double_power_key), i);
323         }
324     }
325
326     section->value_changed = FALSE;
327
328     iface = MAEMO_TWEAKS_SECTION (section);
329     iface->widget = gtk_vbox_new (FALSE, 0);
330     gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
331                         TRUE, TRUE, 0);
332     gtk_box_pack_start (GTK_BOX (iface->widget), section->long_power_key,
333                         TRUE, TRUE, 0);
334     gtk_box_pack_start (GTK_BOX (iface->widget), section->double_power_key,
335                         TRUE, TRUE, 0);
336 }
337
338 static void
339 maemo_tweaks_mce_section_dispose (GObject *obj)
340 {
341     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
342     if (section->ini)
343     {
344         g_key_file_free (section->ini);
345         section->ini = NULL;
346     }
347
348     G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
349         (obj);
350 }
351
352 static void _save (MaemoTweaksSection *section, gboolean *requires_restart)
353 {
354     gchar *argv[5];
355     gint short_active, long_active, double_active;
356     gint i;
357
358     if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
359         return;
360
361     *requires_restart = TRUE;
362
363     short_active = hildon_picker_button_get_active
364         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
365                                (section)->short_power_key));
366
367     long_active = hildon_picker_button_get_active
368         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
369                                (section)->long_power_key));
370
371     double_active = hildon_picker_button_get_active
372         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
373                                (section)->double_power_key));
374
375     argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
376     argv[1] = g_strdup_printf ("%s", spk[short_active].value);
377     argv[2] = g_strdup_printf ("%s", lpk[long_active].value);
378     argv[3] = g_strdup_printf ("%s", dpk[double_active].value);
379     argv[4] = NULL;
380
381     g_spawn_sync ("/home/user", argv, NULL,
382                   G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
383                   NULL, NULL, NULL, NULL, NULL, NULL);
384
385     for (i = 0; i < (sizeof (argv) / sizeof (gchar *)) - 1; i++)
386         g_free (argv[i]);
387 }
388