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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
[DBus (name = "com.canonical.indicator.keyboard")]
public class Indicator.Keyboard.Service : Object {
private MainLoop loop;
private Settings indicator_settings;
private Settings source_settings;
private Gnome.XkbInfo xkb_info;
private IBus.Bus ibus;
private SimpleActionGroup action_group;
private SimpleAction indicator_action;
private MenuModel menu_model;
private Menu sources_menu;
private Icon[] icons;
private string[] icon_strings;
private int[] icon_string_uniques;
private uint[] icon_string_subscripts;
[DBus (visible = false)]
public Service (bool force) {
Bus.own_name (BusType.SESSION,
"com.canonical.indicator.keyboard",
BusNameOwnerFlags.ALLOW_REPLACEMENT | (force ? BusNameOwnerFlags.REPLACE : 0),
this.handle_bus_acquired,
null,
this.handle_name_lost);
this.indicator_settings = new Settings ("com.canonical.indicator.keyboard");
this.indicator_settings.changed["visible"].connect (this.handle_changed_visible);
this.source_settings = new Settings ("org.gnome.desktop.input-sources");
this.source_settings.changed["current"].connect (this.handle_changed_current);
this.source_settings.changed["sources"].connect (this.handle_changed_sources);
this.xkb_info = new Gnome.XkbInfo ();
this.loop = new MainLoop ();
this.loop.run ();
}
[DBus (visible = false)]
private Gtk.StyleContext get_style_context () {
var context = new Gtk.StyleContext ();
context.set_screen (Gdk.Screen.get_default ());
var path = new Gtk.WidgetPath ();
path.append_type (typeof (Gtk.MenuItem));
context.set_path (path);
return context;
}
[DBus (visible = false)]
protected virtual Icon create_icon (string text, uint subscript) {
const int W = 20;
const int H = 20;
const double R = 2.0;
const double TEXT_SIZE = 12.0;
const double SUBSCRIPT_SIZE = 8.0;
Pango.FontDescription description;
var style = get_style_context ();
var colour = style.get_color (Gtk.StateFlags.NORMAL);
style.get (Gtk.StateFlags.NORMAL, Gtk.STYLE_PROPERTY_FONT, out description);
var surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, W, H);
var context = new Cairo.Context (surface);
context.new_sub_path ();
context.arc (R, R, R, Math.PI, -0.5 * Math.PI);
context.arc (W - R, R, R, -0.5 * Math.PI, 0);
context.arc (W - R, H - R, R, 0, 0.5 * Math.PI);
context.arc (R, H - R, R, 0.5 * Math.PI, Math.PI);
context.close_path ();
context.set_source_rgba (colour.red, colour.green, colour.blue, colour.alpha);
context.fill ();
context.set_operator (Cairo.Operator.CLEAR);
var text_layout = Pango.cairo_create_layout (context);
text_layout.set_alignment (Pango.Alignment.CENTER);
description.set_absolute_size (Pango.units_from_double (TEXT_SIZE));
text_layout.set_font_description (description);
text_layout.set_text (text, -1);
Pango.cairo_update_layout (context, text_layout);
int text_width;
int text_height;
text_layout.get_pixel_size (out text_width, out text_height);
if (subscript > 0) {
var subscript_layout = Pango.cairo_create_layout (context);
subscript_layout.set_alignment (Pango.Alignment.CENTER);
description.set_absolute_size (Pango.units_from_double (SUBSCRIPT_SIZE));
subscript_layout.set_font_description (description);
subscript_layout.set_text (@"$subscript", -1);
Pango.cairo_update_layout (context, subscript_layout);
int subscript_width;
int subscript_height;
subscript_layout.get_pixel_size (out subscript_width, out subscript_height);
context.identity_matrix ();
context.translate ((W - (text_width + subscript_width)) / 2, (H - text_height) / 2);
Pango.cairo_layout_path (context, text_layout);
context.fill ();
context.identity_matrix ();
context.translate ((W + (text_width - subscript_width)) / 2, (H + text_height) / 2 - subscript_height);
Pango.cairo_layout_path (context, subscript_layout);
context.fill ();
} else {
context.identity_matrix ();
context.translate ((W - text_width) / 2, (H - text_height) / 2);
Pango.cairo_layout_path (context, text_layout);
context.fill ();
}
var buffer = new ByteArray ();
surface.write_to_png_stream ((data) => {
buffer.append (data);
return Cairo.Status.SUCCESS;
});
return new BytesIcon (ByteArray.free_to_bytes ((owned) buffer));
}
[DBus (visible = false)]
private string get_icon_string (uint index) {
string icon_string = null;
Variant array = null;
if (this.icon_strings == null) {
this.source_settings.get ("sources", "@a(ss)", out array);
this.icon_strings = new string[array.n_children ()];
}
if (index < this.icon_strings.length) {
icon_string = this.icon_strings[index];
if (icon_string == null) {
if (array == null) {
this.source_settings.get ("sources", "@a(ss)", out array);
}
string type;
string name;
array.get_child (index, "(ss)", out type, out name);
if (type == "xkb") {
string display_name;
string layout_name;
this.xkb_info.get_layout_info (name, out display_name, null, out layout_name, null);
if (display_name == null) {
display_name = get_display_name (layout_name);
}
this.icon_strings[index] = get_abbreviation (display_name);
icon_string = this.icon_strings[index];
}
}
}
return icon_string;
}
[DBus (visible = false)]
private bool is_icon_string_unique (uint index) {
bool icon_string_unique = true;
Variant array = null;
if (this.icon_string_uniques == null) {
this.source_settings.get ("sources", "@a(ss)", out array);
this.icon_string_uniques = new int[array.n_children ()];
for (var i = 0; i < this.icon_string_uniques.length; i++) {
this.icon_string_uniques[i] = -1;
}
}
if (index < this.icon_string_uniques.length) {
if (this.icon_string_uniques[index] == -1) {
this.icon_string_uniques[index] = 1;
var icon_string = get_icon_string (index);
for (var i = 0; i < this.icon_string_uniques.length && this.icon_string_uniques[index] == 1; i++) {
if (i != index && get_icon_string (i) == icon_string) {
this.icon_string_uniques[index] = 0;
}
}
}
icon_string_unique = this.icon_string_uniques[index] != 0;
}
return icon_string_unique;
}
[DBus (visible = false)]
private uint get_icon_string_subscript (uint index) {
uint icon_string_subscript = 0;
Variant array = null;
if (this.icon_string_subscripts == null) {
this.source_settings.get ("sources", "@a(ss)", out array);
this.icon_string_subscripts = new uint[array.n_children ()];
}
if (index < this.icon_string_subscripts.length) {
icon_string_subscript = this.icon_string_subscripts[index];
if (icon_string_subscript == 0) {
this.icon_string_subscripts[index] = 1;
for (var i = (int) index - 1; i >= 0 && this.icon_string_subscripts[index] == 1; i--) {
if (get_icon_string (i) == get_icon_string (index)) {
this.icon_string_subscripts[index] = get_icon_string_subscript (i) + 1;
}
}
icon_string_subscript = this.icon_string_subscripts[index];
}
}
return icon_string_subscript;
}
[DBus (visible = false)]
private Icon get_icon (uint index) {
Icon icon = null;
Variant array = null;
if (this.icons == null) {
this.source_settings.get ("sources", "@a(ss)", out array);
this.icons = new Icon[array.n_children ()];
}
if (index < this.icons.length) {
icon = this.icons[index];
if (icon == null) {
if (array == null) {
this.source_settings.get ("sources", "@a(ss)", out array);
}
string type;
string name;
array.get_child (index, "(ss)", out type, out name);
if (type == "xkb") {
var icon_string = get_icon_string (index);
var icon_unique = is_icon_string_unique (index);
var icon_subscript = get_icon_string_subscript (index);
string icon_name;
if (icon_unique) {
icon_name = @"indicator-keyboard-$icon_string";
} else {
icon_name = @"indicator-keyboard-$icon_string-$icon_subscript";
}
var icon_theme = Gtk.IconTheme.get_default ();
var icon_info = icon_theme.lookup_icon (icon_name, 22, 0);
if (icon_info != null) {
try {
this.icons[index] = Icon.new_for_string (icon_info.get_filename ());
} catch (Error error) {
this.icons[index] = null;
}
}
if (this.icons[index] == null) {
if (icon_unique) {
this.icons[index] = create_icon (get_icon_string (index), 0);
} else {
this.icons[index] = create_icon (get_icon_string (index), get_icon_string_subscript (index));
}
}
} else if (type == "ibus") {
var ibus = get_ibus ();
string[] names = { name, null };
var engines = ibus.get_engines_by_names (names);
var engine = engines[0];
try {
this.icons[index] = Icon.new_for_string (engine.get_icon ());
} catch {
warn_if_reached ();
}
}
icon = this.icons[index];
}
}
return icon;
}
[DBus (visible = false)]
protected virtual SimpleActionGroup create_action_group (Action root_action) {
var group = new SimpleActionGroup ();
group.insert (root_action);
group.insert (this.source_settings.create_action ("current"));
var action = new SimpleAction ("map", null);
action.activate.connect (this.handle_activate_map);
group.insert (action);
action = new SimpleAction ("chart", null);
action.activate.connect (this.handle_activate_chart);
group.insert (action);
action = new SimpleAction ("settings", null);
action.activate.connect (this.handle_activate_settings);
group.insert (action);
return group;
}
[DBus (visible = false)]
private void update_indicator_action () {
var visible = this.indicator_settings.get_boolean ("visible");
var current = this.source_settings.get_uint ("current");
var icon = get_icon (current);
Variant state;
if (icon != null) {
state = new Variant.parsed ("{ 'visible' : <%b>, 'icon' : %v }", visible, icon.serialize ());
} else {
state = new Variant.parsed ("{ 'visible' : <%b> }", visible);
}
get_indicator_action ().set_state (state);
}
[DBus (visible = false)]
private SimpleAction get_indicator_action () {
if (this.indicator_action == null) {
var state = new Variant.parsed ("{ 'visible' : <false> }");
this.indicator_action = new SimpleAction.stateful ("indicator", null, state);
update_indicator_action ();
}
return this.indicator_action;
}
[DBus (visible = false)]
public SimpleActionGroup get_action_group () {
if (this.action_group == null) {
this.action_group = create_action_group (get_indicator_action ());
}
return this.action_group;
}
[DBus (visible = false)]
protected virtual MenuModel create_menu_model (MenuModel section_menu) {
var menu = new Menu ();
var submenu = new Menu ();
submenu.append_section (null, section_menu);
var section = new Menu ();
section.append (_ ("Character Map"), "indicator.map");
section.append (_ ("Keyboard Layout Chart"), "indicator.chart");
section.append (_ ("Text Entry Settings..."), "indicator.settings");
submenu.append_section (null, section);
var indicator = new MenuItem.submenu ("x", submenu);
indicator.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root");
indicator.set_detailed_action ("indicator.indicator");
menu.append_item (indicator);
return menu;
}
[DBus (visible = false)]
private IBus.Bus get_ibus () {
if (this.ibus == null) {
IBus.init ();
this.ibus = new IBus.Bus ();
}
return this.ibus;
}
[DBus (visible = false)]
private void update_sources_menu () {
if (this.sources_menu != null) {
var menu = get_sources_menu ();
while (menu.get_n_items () > 0)
menu.remove (0);
VariantIter iter;
string type;
string name;
this.source_settings.get ("sources", "a(ss)", out iter);
for (var i = 0; iter.next ("(ss)", out type, out name); i++) {
if (type == "xkb") {
string display_name;
string layout_name;
this.xkb_info.get_layout_info (name, out display_name, null, out layout_name, null);
if (display_name == null) {
name = get_display_name (layout_name);
} else {
name = display_name;
}
}
else if (type == "ibus") {
var ibus = get_ibus ();
string[] names = { name, null };
var engines = ibus.get_engines_by_names (names);
var engine = engines[0];
var language = engine.get_language ();
var display_name = engine.get_longname ();
if (language != null) {
language = Xkl.get_language_name (language);
}
if (language != null && display_name != null) {
name = @"$language ($display_name)";
} else if (language != null) {
name = language;
} else if (display_name != null) {
name = display_name;
}
}
var menu_item = new MenuItem (name, "indicator.current");
menu_item.set_attribute (Menu.ATTRIBUTE_TARGET, "u", i);
menu_item.set_icon (get_icon (i));
menu.append_item (menu_item);
}
} else {
get_sources_menu ();
}
}
[DBus (visible = false)]
private Menu get_sources_menu () {
if (this.sources_menu == null) {
this.sources_menu = new Menu ();
update_sources_menu ();
}
return this.sources_menu;
}
[DBus (visible = false)]
public MenuModel get_menu_model () {
if (this.menu_model == null) {
this.menu_model = create_menu_model (get_sources_menu ());
}
return this.menu_model;
}
[DBus (visible = false)]
private void handle_changed_visible (string key) {
update_indicator_action ();
}
[DBus (visible = false)]
private void handle_changed_current (string key) {
update_indicator_action ();
}
[DBus (visible = false)]
private void handle_changed_sources (string key) {
this.icon_string_subscripts = null;
this.icon_string_uniques = null;
this.icon_strings = null;
this.icons = null;
update_sources_menu ();
update_indicator_action ();
}
[DBus (visible = false)]
private void handle_activate_map (Variant? parameter) {
try {
Process.spawn_command_line_async ("gucharmap");
} catch {
warn_if_reached ();
}
}
[DBus (visible = false)]
private void handle_activate_chart (Variant? parameter) {
var layout = "us";
string variant = null;
var current = this.source_settings.get_uint ("current");
Variant array;
this.source_settings.get ("sources", "@a(ss)", out array);
if (current < array.n_children ()) {
string type;
string name;
array.get_child (current, "(ss)", out type, out name);
if (type == "xkb") {
this.xkb_info.get_layout_info (name, null, null, out layout, out variant);
} else if (type == "ibus") {
var ibus = get_ibus ();
string[] names = { name, null };
var engines = ibus.get_engines_by_names (names);
var engine = engines[0];
layout = engine.get_layout ();
variant = engine.get_layout_variant ();
}
}
try {
string command;
if (variant != null) {
command = @"gkbd-keyboard-display -l \"$layout\t$variant\"";
} else {
command = @"gkbd-keyboard-display -l $layout";
}
Process.spawn_command_line_async (command);
} catch {
warn_if_reached ();
}
}
[DBus (visible = false)]
private void handle_activate_settings (Variant? parameter) {
try {
Process.spawn_command_line_async ("gnome-control-center region layouts");
} catch {
warn_if_reached ();
}
}
[DBus (visible = false)]
private void handle_bus_acquired (DBusConnection connection, string name) {
try {
connection.export_action_group ("/com/canonical/indicator/keyboard", get_action_group ());
connection.export_menu_model ("/com/canonical/indicator/keyboard/desktop", get_menu_model ());
} catch {
warn_if_reached ();
}
}
[DBus (visible = false)]
private void handle_name_lost (DBusConnection connection, string name) {
this.loop.quit ();
this.loop = null;
}
[DBus (visible = false)]
public static int main (string[] args) {
Gtk.init (ref args);
new Service ("--force" in args);
return 0;
}
}
|