/
SwitcherButton.c
executable file
·496 lines (382 loc) · 16.2 KB
/
SwitcherButton.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "SwitcherButton.h"
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
#include <gconf/gconf-client.h>
#include <libosso.h>
#include "TabletModel.h"
#define GCONF_DIR_2008 "/apps/osso/inputmethod/hildon-im-languages"
#define GCONF_CURRENT_2008 "/apps/osso/inputmethod/hildon-im-languages/current"
#define GCONF_LANG0_2008 "/apps/osso/inputmethod/hildon-im-languages/language-0"
#define GCONF_LANG1_2008 "/apps/osso/inputmethod/hildon-im-languages/language-1"
#define GCONF_DIR_2006 "/apps/osso/inputmethod"
#define GCONF_CURRENT_2006 "/apps/osso/inputmethod/current_language"
#define GCONF_LANG0_2006 "/apps/osso/inputmethod/primary_language"
#define GCONF_LANG1_2006 "/apps/osso/inputmethod/secondary_language"
#define GCONF_LOCK_DIR "/system/osso/af"
#define GCONF_LOCK_ENTRY "/system/osso/af/keyboard-attached"
#define CMD_BLOCK_2006_1 "maemo-gtk-im-switch osso-input-method"
#define CMD_BLOCK_2006_2 "maemo-gtk-im-switch xim"
#define CMD_BLOCK_2006_3 "maemo-gtk-im-switch default"
#define SWITCHER_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), SWITCHER_BUTTON_TYPE, SwitcherButtonPriv))
struct _SwitcherButtonPriv
{
TabletModel model;
osso_context_t* osso;
int current;
gboolean locked;
GtkIconTheme *theme;
GdkPixbuf *icon0;
GdkPixbuf *icon1;
GdkPixbuf *lock;
GConfClient *gconfClient;
guint gconfNotifyCurrent;
guint gconfNotifyLang0;
guint gconfNotifyLang1;
guint gconfNotifyLock;
GtkWidget *menu;
GtkWidget *menuItemCopy;
GtkWidget *menuItemBlock;
};
G_DEFINE_TYPE(SwitcherButton, switcher_button, GTK_TYPE_BUTTON);
void switcher_button_dispose(GObject*);
void switcher_button_finalize(GObject*);
void switcher_button_size_request(GtkWidget *self, GtkRequisition *requisition);
// Private methods
void switcher_button_update_icon(SwitcherButton *self);
void switcher_button_load_icon(SwitcherButton *self, gchar *langName, GdkPixbuf **icon, GtkIconTheme *theme);
void switcher_button_update_languages(SwitcherButton *self);
void switcher_button_on_current_changed(GConfClient *client, guint cnxn_id, GConfEntry *entry, gpointer userData);
void switcher_button_on_lock_changed(GConfClient *client, guint cnxn_id, GConfEntry *entry, gpointer userData);
void switcher_button_on_language_changed(GConfClient *client, guint cnxn_id, GConfEntry *entry, gpointer userData);
void switcher_button_on_click(SwitcherButton *self, gpointer data);
void switcher_button_on_tap_and_hold(SwitcherButton *self, gpointer data);
void switcher_button_copy_to_clipboard(SwitcherButton *self);
void switcher_button_block_keyboard(SwitcherButton *self, gboolean block);
void switcher_button_switch_layout(SwitcherButton *self, int langN);
void switcher_button_on_copy_menu(GtkMenuItem* menuItem, gpointer data);
void switcher_button_on_block_menu(GtkMenuItem* menuItem, gpointer data);
gboolean switcher_button_on_expose(SwitcherButton *self, GdkEventExpose *event, gpointer data);
void switcher_button_class_init(SwitcherButtonClass *klass)
{
GObjectClass *gobjectClass = G_OBJECT_CLASS(klass);
gobjectClass->dispose = switcher_button_dispose;
gobjectClass->finalize= switcher_button_finalize;
GtkWidgetClass *widgetClass = GTK_WIDGET_CLASS(klass);
widgetClass->size_request = switcher_button_size_request;
g_type_class_add_private(klass, sizeof(SwitcherButtonPriv));
}
void switcher_button_init(SwitcherButton *self)
{
self->priv = SWITCHER_BUTTON_GET_PRIVATE(self);
self->priv->osso = osso_initialize("layoutswitcher", VERSION, 0, 0);
self->priv->model = tabletModel();
switch (self->priv->model)
{
case N800:
printf("800\n");
break;
case N810:
printf("810\n");
break;
case N900:
printf("900\n");
break;
case N770:
printf("770\n");
break;
}
self->priv->locked = 0;
self->priv->theme = gtk_icon_theme_get_default();
self->priv->lock = gtk_icon_theme_load_icon(self->priv->theme, "layoutswitcher-lock", 40, (GtkIconLookupFlags)0, 0);
self->priv->icon0 = 0;
self->priv->icon1 = 0;
self->priv->gconfClient = gconf_client_get_default();
if (self->priv->model == N800 || self->priv->model == N810)
{
gconf_client_add_dir(self->priv->gconfClient, GCONF_DIR_2008, GCONF_CLIENT_PRELOAD_NONE, 0);
self->priv->gconfNotifyCurrent = gconf_client_notify_add(self->priv->gconfClient,
GCONF_CURRENT_2008, switcher_button_on_current_changed, self, 0, 0);
self->priv->gconfNotifyLang0 = gconf_client_notify_add(self->priv->gconfClient,
GCONF_LANG0_2008, switcher_button_on_language_changed, self, 0, 0);
self->priv->gconfNotifyLang1 = gconf_client_notify_add(self->priv->gconfClient,
GCONF_LANG1_2008, switcher_button_on_language_changed, self, 0, 0);
}
else if (self->priv->model == N770)
{
gconf_client_add_dir(self->priv->gconfClient, GCONF_DIR_2006, GCONF_CLIENT_PRELOAD_NONE, 0);
self->priv->gconfNotifyCurrent = gconf_client_notify_add(self->priv->gconfClient,
GCONF_CURRENT_2006, switcher_button_on_current_changed, self, 0, 0);
self->priv->gconfNotifyLang0 = gconf_client_notify_add(self->priv->gconfClient,
GCONF_LANG0_2006, switcher_button_on_language_changed, self, 0, 0);
self->priv->gconfNotifyLang1 = gconf_client_notify_add(self->priv->gconfClient,
GCONF_LANG1_2006, switcher_button_on_language_changed, self, 0, 0);
}
gconf_client_add_dir(self->priv->gconfClient, GCONF_LOCK_DIR, GCONF_CLIENT_PRELOAD_NONE, 0);
self->priv->gconfNotifyLock = gconf_client_notify_add(self->priv->gconfClient,
GCONF_LOCK_ENTRY, switcher_button_on_lock_changed, self, 0, 0);
self->priv->menu = gtk_menu_new();
self->priv->menuItemCopy = gtk_menu_item_new_with_label("Copy text");
self->priv->menuItemBlock= gtk_check_menu_item_new_with_label("Block keyboard");
g_signal_connect(G_OBJECT(self->priv->menuItemCopy), "activate", G_CALLBACK(switcher_button_on_copy_menu), self);
g_signal_connect(G_OBJECT(self->priv->menuItemBlock), "activate", G_CALLBACK(switcher_button_on_block_menu), self);
gtk_menu_shell_append(GTK_MENU_SHELL(self->priv->menu), self->priv->menuItemCopy);
gtk_menu_shell_append(GTK_MENU_SHELL(self->priv->menu), self->priv->menuItemBlock);
gtk_widget_show_all(self->priv->menu);
g_signal_connect(G_OBJECT(self), "clicked", G_CALLBACK(switcher_button_on_click), 0);
if (self->priv->model == N770)
{
g_signal_connect(G_OBJECT(self), "tap-and-hold", G_CALLBACK(switcher_button_on_tap_and_hold), 0);
gtk_widget_tap_and_hold_setup(GTK_WIDGET(self), 0, 0, 0);
}
else
{
// g_signal_connect(G_OBJECT(self), "tap-and-hold", G_CALLBACK(switcher_button_on_tap_and_hold), 0);
gtk_widget_tap_and_hold_setup(GTK_WIDGET(self), self->priv->menu, 0, 0);
}
g_signal_connect_after(G_OBJECT(self), "expose-event", G_CALLBACK(switcher_button_on_expose), 0);
switcher_button_update_languages(self);
}
void switcher_button_dispose(GObject *gobject)
{
SwitcherButton *self = SWITCHER_BUTTON(gobject);
if (self->priv->gconfClient)
{
gconf_client_notify_remove(self->priv->gconfClient, self->priv->gconfNotifyCurrent);
gconf_client_notify_remove(self->priv->gconfClient, self->priv->gconfNotifyLang0);
gconf_client_notify_remove(self->priv->gconfClient, self->priv->gconfNotifyLang1);
if (self->priv->model == N800 || self->priv->model == N810)
gconf_client_remove_dir(self->priv->gconfClient, GCONF_DIR_2008, 0);
else if (self->priv->model == N770)
gconf_client_remove_dir(self->priv->gconfClient, GCONF_DIR_2006, 0);
gconf_client_notify_remove(self->priv->gconfClient, self->priv->gconfNotifyLock);
gconf_client_remove_dir(self->priv->gconfClient, GCONF_LOCK_DIR, 0);
g_object_unref(self->priv->gconfClient);
self->priv->gconfClient = 0;
}
if (self->priv->menu)
{
g_object_unref(self->priv->menu);
g_object_unref(self->priv->menuItemCopy);
g_object_unref(self->priv->menuItemBlock);
self->priv->menu = 0;
}
if (self->priv->osso)
{
osso_deinitialize(self->priv->osso);
self->priv->osso = 0;
}
G_OBJECT_CLASS(switcher_button_parent_class)->dispose(gobject);
}
void switcher_button_finalize(GObject *gobject)
{
SwitcherButton *self = SWITCHER_BUTTON(gobject);
G_OBJECT_CLASS(switcher_button_parent_class)->finalize(gobject);
}
GtkWidget* switcher_button_new()
{
return GTK_WIDGET(g_object_new(SWITCHER_BUTTON_TYPE, NULL));
}
//////////////////////////////////////////////////
void switcher_button_update_icon(SwitcherButton *self)
{
if (self->priv->model == N800 || self->priv->model == N810)
self->priv->current = gconf_client_get_int(self->priv->gconfClient, GCONF_CURRENT_2008, 0);
else if (self->priv->model == N770)
self->priv->current = gconf_client_get_bool(self->priv->gconfClient, GCONF_CURRENT_2006, 0) ? 0 : 1;
self->priv->locked = gconf_client_get_bool(self->priv->gconfClient, GCONF_LOCK_ENTRY, 0) ? 1 : 0;
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(self->priv->menuItemBlock), self->priv->locked != 0);
gtk_widget_queue_draw(GTK_WIDGET(self));
}
void switcher_button_load_icon(SwitcherButton *self, gchar* langName, GdkPixbuf **icon, GtkIconTheme *theme)
{
char iconName[50];
if (langName == 0)
strcpy(iconName, "layoutswitcher-unk");
else
{
if (strcmp(langName, "") == 0)
strcpy(iconName, "layoutswitcher-unk");
else
{
strcpy(iconName, "layoutswitcher-");
strcat(iconName, langName);
}
g_free(langName);
}
if (*icon != 0)
g_object_unref(G_OBJECT(*icon));
*icon = gtk_icon_theme_load_icon(theme, iconName, 40, (GtkIconLookupFlags)0, 0);
}
void switcher_button_update_languages(SwitcherButton *self)
{
if (self->priv->model == N800 || self->priv->model == N810)
{
gchar* lang0 = gconf_client_get_string(self->priv->gconfClient, GCONF_LANG0_2008, 0);
gchar* lang1 = gconf_client_get_string(self->priv->gconfClient, GCONF_LANG1_2008, 0);
switcher_button_load_icon(self, lang0, &self->priv->icon0, self->priv->theme);
switcher_button_load_icon(self, lang1, &self->priv->icon1, self->priv->theme);
}
else if (self->priv->model == N770)
{
gchar* lang0 = gconf_client_get_string(self->priv->gconfClient, GCONF_LANG0_2006, 0);
gchar* lang1 = gconf_client_get_string(self->priv->gconfClient, GCONF_LANG1_2006, 0);
switcher_button_load_icon(self, lang0, &self->priv->icon0, self->priv->theme);
switcher_button_load_icon(self, lang1, &self->priv->icon1, self->priv->theme);
}
else
{
self->priv->icon0 = 0;
self->priv->icon1 = 0;
}
switcher_button_update_icon(self);
}
//////////////////////////////////////////////////
void switcher_button_on_current_changed(GConfClient *client, guint cnxn_id, GConfEntry *entry, gpointer userData)
{
switcher_button_update_icon(SWITCHER_BUTTON(userData));
}
void switcher_button_on_lock_changed(GConfClient *client, guint cnxn_id, GConfEntry *entry, gpointer userData)
{
switcher_button_update_icon(SWITCHER_BUTTON(userData));
}
void switcher_button_on_language_changed(GConfClient *client, guint cnxn_id, GConfEntry *entry, gpointer userData)
{
switcher_button_update_languages(SWITCHER_BUTTON(userData));
}
//////////////////////////////////////////////////
void switcher_button_on_selection_requested(GtkClipboard *selection, const gchar* text, gpointer userData)
{
GtkClipboard *clipboard = gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE));
if (text != NULL)
{
gtk_clipboard_set_text(clipboard, text, -1);
SwitcherButton* self = SWITCHER_BUTTON(userData);
osso_system_note_infoprint(self->priv->osso, "Copied", 0);
}
}
void switcher_button_copy_to_clipboard(SwitcherButton *self)
{
GtkClipboard *selection = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
gtk_clipboard_request_text(selection, switcher_button_on_selection_requested, self);
}
void switcher_button_block_keyboard(SwitcherButton *self, gboolean block)
{
if (self->priv->locked == block)
return;
self->priv->locked = block;
if (self->priv->model == N800 || self->priv->model == N810)
{
gconf_client_set_bool(self->priv->gconfClient, GCONF_LOCK_ENTRY, self->priv->locked, 0);
}
if (self->priv->model == N770)
{
/*
switch (self->priv->locked)
{
case 0:
g_spawn_command_line_async(CMD_BLOCK_2006_1, 0);
break;
case 1:
g_spawn_command_line_async(CMD_BLOCK_2006_2, 0);
break;
case 2:
g_spawn_command_line_async(CMD_BLOCK_2006_3, 0);
break;
}
*/
if (self->priv->locked)
g_spawn_command_line_async(CMD_BLOCK_2006_2, 0);
else
g_spawn_command_line_async(CMD_BLOCK_2006_1, 0);
}
if (self->priv->locked)
osso_system_note_infoprint(self->priv->osso, "Keyboard blocked", 0);
else
osso_system_note_infoprint(self->priv->osso, "Keyboard released", 0);
}
void switcher_button_switch_layout(SwitcherButton *self, int langN)
{
if (self->priv->current == langN)
return;
if (self->priv->model == N810)
{
// Simulate Ctrl+Chr sequence
GdkDisplay* gdkDisplay = gdk_display_get_default();
if (gdkDisplay == 0) return;
Display *dpy = (Display*)gdk_x11_display_get_xdisplay(gdkDisplay);
if (dpy == 0)
return;
KeySym Control_R_Sym = XStringToKeysym("Control_R");
KeySym Multi_key_Sym = XStringToKeysym("Multi_key");
if (Control_R_Sym == 0 || Multi_key_Sym == 0)
return;
KeyCode Control_R = XKeysymToKeycode(dpy, Control_R_Sym);
KeyCode Multi_key = XKeysymToKeycode(dpy, Multi_key_Sym);
if (Control_R == 0 || Multi_key == 0)
return;
XTestFakeKeyEvent(dpy, Control_R, True, CurrentTime);
XTestFakeKeyEvent(dpy, Multi_key, True, CurrentTime);
XTestFakeKeyEvent(dpy, Multi_key, False, CurrentTime);
XTestFakeKeyEvent(dpy, Control_R, False, CurrentTime);
}
if (self->priv->model == N800)
gconf_client_set_int(self->priv->gconfClient, GCONF_CURRENT_2008, langN, 0);
if (self->priv->model == N770)
gconf_client_set_bool(self->priv->gconfClient, GCONF_CURRENT_2006, langN == 0, 0);
if (self->priv->model == N900)
{
}
}
void switcher_button_on_click(SwitcherButton *self, gpointer data)
{
int newLayout = self->priv->current == 0 ? 1 : 0;
switcher_button_switch_layout(self, newLayout);
}
void switcher_button_on_tap_and_hold(SwitcherButton *self, gpointer data)
{
switcher_button_copy_to_clipboard(self);
}
void switcher_button_on_copy_menu(GtkMenuItem* menuItem, gpointer userData)
{
SwitcherButton* self = SWITCHER_BUTTON(userData);
switcher_button_copy_to_clipboard(self);
}
void switcher_button_on_block_menu(GtkMenuItem* menuItem, gpointer userData)
{
SwitcherButton* self = SWITCHER_BUTTON(userData);
switcher_button_block_keyboard(
self,
GTK_CHECK_MENU_ITEM(self->priv->menuItemBlock)->active
);
}
//////////////////////////////////////////////////
gboolean switcher_button_on_expose(SwitcherButton *self, GdkEventExpose *event, gpointer data)
{
GdkGC *gc = gdk_gc_new(GTK_WIDGET(self)->window);
GdkPixbuf *icon = self->priv->current == 0 ? self->priv->icon0 : self->priv->icon1;
if (icon == 0)
{
return TRUE;
}
int x = GTK_WIDGET(self)->allocation.x + (GTK_WIDGET(self)->allocation.width - gdk_pixbuf_get_width(icon)) / 2;
int y = GTK_WIDGET(self)->allocation.y + (GTK_WIDGET(self)->allocation.height - gdk_pixbuf_get_height(icon)) / 2;
gdk_draw_pixbuf(GTK_WIDGET(self)->window, gc,
icon,
0, 0, x, y, -1, -1,
GDK_RGB_DITHER_NORMAL, 0, 0);
if (self->priv->locked)
{
gdk_draw_pixbuf(GTK_WIDGET(self)->window, gc,
self->priv->lock,
0, 0, x, y, -1, -1,
GDK_RGB_DITHER_NORMAL, 0, 0);
}
g_object_unref(gc);
return TRUE;
}
//////////////////////////////////////////////////
void switcher_button_size_request(GtkWidget *widget, GtkRequisition *requisition)
{
requisition->width = 40;
requisition->height = 40;
}