forked from phuang/ibus
-
Notifications
You must be signed in to change notification settings - Fork 174
/
main.vala
1026 lines (895 loc) · 32.2 KB
/
main.vala
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim:set et sts=4 sw=4:
*
* ibus - The Input Bus
*
* Copyright(c) 2013 Peng Huang <shawn.p.huang@gmail.com>
* Copyright(c) 2015-2024 Takao Fujiwara <takao.fujiwara1@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
private const string IBUS_SCHEMAS_GENERAL = "org.freedesktop.ibus.general";
private const string IBUS_SCHEMAS_GENERAL_HOTKEY =
"org.freedesktop.ibus.general.hotkey";
private const string IBUS_SCHEMAS_PANEL = "org.freedesktop.ibus.panel";
private const string IBUS_SCHEMAS_PANEL_EMOJI =
"org.freedesktop.ibus.panel.emoji";
private const string SYSTEMD_SESSION_GNOME_FILE =
"org.freedesktop.IBus.session.GNOME.service";
bool name_only = false;
/* system() exists as a public API. */
bool is_system = false;
string cache_file = null;
string engine_id = null;
bool verbose = false;
string daemon_type = null;
string systemd_service_file = null;
GLib.MainLoop loop = null;
class EngineList {
public IBus.EngineDesc[] data = {};
}
private bool is_kde(bool verbose) {
unowned string? desktop = Environment.get_variable("XDG_CURRENT_DESKTOP");
if (desktop == "KDE")
return true;
if (desktop == null || desktop == "(null)")
desktop = Environment.get_variable("XDG_SESSION_DESKTOP");
if (desktop == "plasma" || desktop == "KDE-wayland")
return true;
if (desktop == null) {
if (verbose) {
stderr.printf("XDG_CURRENT_DESKTOP is not exported in your " +
"desktop session.\n");
}
} else if (verbose) {
stderr.printf("Your desktop session \"%s\" is not KDE\n.", desktop);
}
return false;
}
IBus.Bus? get_bus() {
var bus = new IBus.Bus();
if (!bus.is_connected ())
return null;
return bus;
}
private void
name_appeared_handler(GLib.DBusConnection connection,
string name,
string name_owner) {
if (verbose)
stderr.printf("NameAquired %s:%s\n", name, name_owner);
loop.quit();
loop = null;
}
private void
kde_virtual_keyboard_avail_cb(GLib.DBusConnection connection,
string? sender_name,
string object_path,
string interface_name,
string signal_name,
GLib.Variant parameters)
{
if (verbose) {
stderr.printf("%s.%s %s returned.\n",
interface_name, signal_name, object_path);
}
loop.quit();
loop = null;
}
GLib.DBusConnection? get_session_bus(bool verbose) {
loop = new GLib.MainLoop();
assert(loop != null);
GLib.Bus.watch_name (GLib.BusType.SESSION,
DBus.SERVICE_DBUS,
GLib.BusNameWatcherFlags.NONE,
name_appeared_handler,
null);
GLib.DBusConnection? connection = null;
GLib.Bus.get.begin(GLib.BusType.SESSION, null,
(obj, res) => {
try {
connection = GLib.Bus.get.end(res);
if (verbose)
stderr.printf("The session bus is generated.\n");
} catch(GLib.IOError e) {
if (verbose)
stderr.printf("The session bus error: %s\n", e.message);
if (loop != null) {
loop.quit();
loop = null;
}
}
});
loop.run();
if (connection.is_closed()) {
if (verbose)
stderr.printf("The session bus is closed.\n");
return null;
}
return connection;
}
string?
get_ibus_systemd_object_path(GLib.DBusConnection connection,
bool verbose) {
string object_path = null;
assert(systemd_service_file != null);
try {
var variant = connection.call_sync (
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit",
new GLib.Variant("(s)", systemd_service_file),
new GLib.VariantType("(o)"),
GLib.DBusCallFlags.NONE,
-1,
null);
variant.get("(o)", ref object_path);
if (verbose) {
stderr.printf("Succeed to get an object path \"%s\" for IBus " +
"systemd service file \"%s\".\n",
object_path, systemd_service_file);
}
return object_path;
} catch (GLib.Error e) {
if (verbose) {
stderr.printf("IBus systemd service file \"%s\" is not installed " +
"in your system: %s\n",
systemd_service_file, e.message);
}
}
return null;
}
bool
is_running_daemon_via_systemd(GLib.DBusConnection connection,
string object_path,
bool verbose) {
string? state = null;
try {
while (true) {
var variant = connection.call_sync (
"org.freedesktop.systemd1",
object_path,
"org.freedesktop.DBus.Properties",
"Get",
new GLib.Variant("(ss)",
"org.freedesktop.systemd1.Unit",
"ActiveState"),
new GLib.VariantType("(v)"),
GLib.DBusCallFlags.NONE,
-1,
null);
GLib.Variant child = null;
variant.get("(v)", ref child);
state = child.dup_string();
if (verbose) {
stderr.printf("systemd state is \"%s\" for an object " +
"path \"%s\".\n", state, object_path);
}
if (state != "activating")
break;
Posix.sleep(1);
}
} catch (GLib.Error e) {
if (verbose)
stderr.printf("%s\n", e.message);
return false;
}
if (state == "active")
return true;
return false;
}
bool
start_daemon_with_dbus_systemd(GLib.DBusConnection connection,
bool restart,
bool verbose) {
string object_path = null;
string method = "StartUnit";
assert(systemd_service_file != null);
if (restart)
method = "RestartUnit";
try {
var variant = connection.call_sync (
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method,
new GLib.Variant("(ss)", systemd_service_file, "fail"),
new GLib.VariantType("(o)"),
GLib.DBusCallFlags.NONE,
-1,
null);
variant.get("(o)", ref object_path);
if (verbose) {
stderr.printf("Succeed to restart IBus daemon via IBus systemd " +
"service file \"%s\": \"%s\"\n",
systemd_service_file, object_path);
}
return true;
} catch (GLib.Error e) {
if (verbose) {
stderr.printf("Failed to %s IBus daemon via IBus systemd " +
"service file \"%s\": %s\n",
restart ? "restart" : "start",
systemd_service_file, e.message);
}
}
return false;
}
bool
start_daemon_with_dbus_kde(GLib.DBusConnection connection,
bool verbose) {
loop = new GLib.MainLoop();
assert(loop != null);
uint subscripion_id = connection.signal_subscribe(
null,
"org.kde.kwin.VirtualKeyboard",
"availableChanged",
"/VirtualKeyboard",
null,
DBusSignalFlags.NONE,
kde_virtual_keyboard_avail_cb);
string wayland_values = "InputMethod";
var bytes = new GLib.VariantBuilder(new GLib.VariantType("ay"));
for (int i = 0; i < wayland_values.length; i++) {
bytes.add("y", wayland_values.get(i));
}
var bytes2 = new GLib.VariantBuilder(new GLib.VariantType("aay"));
bytes2.add("ay", bytes);
var array = new GLib.VariantBuilder(new GLib.VariantType("a{saay}"));
array.add("{saay}", "Wayland", bytes2);
try {
if (!connection.emit_signal(null,
"/kwinrc",
"org.kde.kconfig.notify",
"ConfigChanged",
new GLib.Variant("(a{saay})", array))) {
if (verbose)
stderr.printf("Failed to emit a KDE D-Bus signal.\n");
connection.signal_unsubscribe(subscripion_id);
if (loop != null) {
loop.quit();
loop = null;
}
return false;
}
} catch (GLib.Error e) {
stderr.printf("%s\n", e.message);
connection.signal_unsubscribe(subscripion_id);
if (loop != null) {
loop.quit();
loop = null;
}
return false;
}
// Wait for "/VirtualKeyboard" on "org.kde.kwin.VirtualKeyboard" signal
// so that "/kwinrc" on "org.kde.kconfig.notify" signal is reached to
// kwin.
loop.run();
connection.signal_unsubscribe(subscripion_id);
return true;
}
int list_engine(string[] argv) {
const OptionEntry[] options = {
{ "name-only", 0, 0, OptionArg.NONE, out name_only,
N_("List engine name only"), null },
{ null }
};
var option = new OptionContext();
option.add_main_entries(options, Config.GETTEXT_PACKAGE);
try {
option.parse(ref argv);
} catch (OptionError e) {
stderr.printf("%s\n", e.message);
return Posix.EXIT_FAILURE;
}
var bus = get_bus();
if (bus == null) {
stderr.printf(_("Can't connect to IBus.\n"));
return Posix.EXIT_FAILURE;
}
var engines = bus.list_engines();
if (name_only) {
foreach (var engine in engines) {
print("%s\n", engine.get_name());
}
return Posix.EXIT_SUCCESS;
}
var map = new HashTable<string, EngineList>(GLib.str_hash, GLib.str_equal);
foreach (var engine in engines) {
var list = map.get(engine.get_language());
if (list == null) {
list = new EngineList();
map.insert(engine.get_language(), list);
}
list.data += engine;
}
foreach (var language in map.get_keys()) {
var list = map.get(language);
print(_("language: %s\n"), IBus.get_language_name(language));
foreach (var engine in list.data) {
print(" %s - %s\n", engine.get_name(), engine.get_longname());
}
}
return Posix.EXIT_SUCCESS;
}
private int exec_setxkbmap(IBus.EngineDesc engine) {
string layout = engine.get_layout();
string variant = engine.get_layout_variant();
string option = engine.get_layout_option();
string standard_error = null;
int exit_status = 0;
string[] args = { "setxkbmap" };
if (layout != null && layout != "" && layout != "default") {
args += "-layout";
args += layout;
}
if (variant != null && variant != "" && variant != "default") {
args += "-variant";
args += variant;
}
if (option != null && option != "" && option != "default") {
/*TODO: Need to get the session XKB options */
args += "-option";
args += "-option";
args += option;
}
if (args.length == 1) {
return Posix.EXIT_FAILURE;
}
try {
if (!GLib.Process.spawn_sync(null, args, null,
GLib.SpawnFlags.SEARCH_PATH,
null, null,
out standard_error,
out exit_status)) {
warning("Switch xkb layout to %s failed.",
engine.get_layout());
return Posix.EXIT_FAILURE;
}
} catch (GLib.SpawnError e) {
warning("Execute setxkbmap failed: %s", e.message);
return Posix.EXIT_FAILURE;
}
if (exit_status != 0) {
warning("Execute setxkbmap failed: %s", standard_error ?? "(null)");
return Posix.EXIT_FAILURE;
}
return Posix.EXIT_SUCCESS;
}
int get_set_engine(string[] argv) {
var bus = get_bus();
string engine = null;
if (argv.length > 1)
engine = argv[1];
if (engine == null) {
var desc = bus.get_global_engine();
if (desc == null) {
stderr.printf(_("No engine is set.\n"));
return Posix.EXIT_FAILURE;
}
print("%s\n", desc.get_name());
return Posix.EXIT_SUCCESS;
}
if(!bus.set_global_engine(engine)) {
stderr.printf(_("Set global engine failed.\n"));
return Posix.EXIT_FAILURE;
}
var desc = bus.get_global_engine();
if (desc == null) {
stderr.printf(_("Get global engine failed.\n"));
return Posix.EXIT_FAILURE;
}
var settings = new GLib.Settings(IBUS_SCHEMAS_GENERAL);
if (!settings.get_boolean("use-system-keyboard-layout"))
return exec_setxkbmap(desc);
return Posix.EXIT_SUCCESS;
}
int message_watch(string[] argv) {
return Posix.EXIT_SUCCESS;
}
bool operate_daemon_in_kde_wayland(GLib.DBusConnection connection,
bool start,
bool verbose) {
if (!is_kde(verbose))
return false;
if (Environment.get_variable("WAYLAND_DISPLAY") == null) {
if (verbose)
stderr.printf("Your KDE session is not Wayland\n.");
return false;
}
var kwinrc = GLib.Path.build_filename(
GLib.Environment.get_user_config_dir(), "kwinrc");
var key_file = new KRcFile();
try {
key_file.load_from_file(kwinrc, GLib.KeyFileFlags.KEEP_COMMENTS);
} catch (GLib.KeyFileError e) {
stderr.printf("Error in %s: %s\n", kwinrc, e.message);
return false;
} catch (GLib.FileError e) {
stderr.printf("Error in %s: %s\n", kwinrc, e.message);
return false;
}
try {
bool is_updated = false;
if (start) {
string ibus_value =
GLib.Path.build_filename(Config.DATADIR,
"applications",
Config.UI_WAYLAND_DESKTOP);
string? value = null;
if (key_file.has_group("Wayland") &&
key_file.has_key("Wayland", "InputMethod[$e]")) {
value = key_file.get_value("Wayland", "InputMethod[$e]");
}
if (value != ibus_value) {
key_file.set_value("Wayland", "InputMethod[$e]", ibus_value);
key_file.save_to_file(kwinrc);
is_updated = true;
}
} else {
if (key_file.has_group("Wayland")) {
var keys = key_file.get_keys("Wayland");
if (key_file.has_key("Wayland", "InputMethod[$e]")) {
if (keys.length == 1)
key_file.remove_group("Wayland");
else
key_file.remove_key("Wayland", "InputMethod[$e]");
key_file.save_to_file(kwinrc);
is_updated = true;
}
}
}
if (verbose) {
if (is_updated) {
stderr.printf("\"InputMethod\" was %s in kwinrc for %s.\n",
start ? "added" : "deleted",
start ? "start" : "exit");
} else {
if (start) {
stderr.printf(
"kwinrc is not updated for start. \"ibus exit\" " +
"command may be needed in case kwin_wayland " +
"restarted.\n");
} else {
stderr.printf("kwinrc is not updated for exit.\n");
}
}
}
} catch (GLib.KeyFileError e) {
stderr.printf("%s\n", e.message);
return false;
} catch (GLib.FileError e) {
stderr.printf("%s\n", e.message);
return false;
}
if (!start_daemon_with_dbus_kde(connection, verbose))
return false;
if (verbose) {
stderr.printf("Succeed to %s ibus-daemon with a " +
"KDE Wayland method.\n", start ? "start" : "exit");
}
return true;
}
bool start_daemon_with_systemd(bool restart, bool verbose) {
GLib.DBusConnection? connection = get_session_bus(verbose);
if (connection == null)
return false;
string? object_path = null;
if (restart) {
object_path = get_ibus_systemd_object_path(connection, verbose);
if (object_path == null)
return false;
if (!is_running_daemon_via_systemd(connection,
object_path,
verbose)) {
return false;
}
}
if (!start_daemon_with_dbus_systemd(connection, restart, verbose))
return false;
// Do not check the systemd state in case of restart because
// the systemd file validation is already done and also stopping
// daemon and starting daemon take time and the state could be
// "inactive" with the time lag.
if (restart)
return true;
object_path = get_ibus_systemd_object_path(connection, verbose);
if (object_path == null)
return false;
if (!is_running_daemon_via_systemd(connection, object_path, verbose))
return false;
return true;
}
int start_daemon_real(string[] argv,
bool restart) {
const OptionEntry[] options = {
{ "type", 0, 0, OptionArg.STRING, out daemon_type,
N_("Start or restart daemon with \"direct\", \"systemd\" or " +
"\"kde-wayland\" TYPE."),
"TYPE" },
{ "service-file", 0, 0, OptionArg.STRING, out systemd_service_file,
N_("Start or restart daemon with SYSTEMD_SERVICE file."),
"SYSTEMD_SERVICE" },
{ "verbose", 0, 0, OptionArg.NONE, out verbose,
N_("Show debug messages."), null },
{ null }
};
var option = new OptionContext();
option.add_main_entries(options, Config.GETTEXT_PACKAGE);
option.set_ignore_unknown_options(true);
try {
option.parse(ref argv);
} catch (OptionError e) {
stderr.printf("%s\n", e.message);
return Posix.EXIT_FAILURE;
}
if (daemon_type != null && daemon_type != "direct" &&
daemon_type != "systemd" && daemon_type != "kde-wayland") {
stderr.printf("type argument must be \"direct\" or \"systemd\" " +
"or \"kde-wayland\"\n");
return Posix.EXIT_FAILURE;
}
if (systemd_service_file == null)
systemd_service_file = SYSTEMD_SESSION_GNOME_FILE;
if (daemon_type == null || daemon_type == "kde-wayland") {
do {
GLib.DBusConnection? connection = get_session_bus(verbose);
if (connection == null)
break;
if (restart) {
if (operate_daemon_in_kde_wayland(connection, false, verbose))
Posix.sleep(3);
else
break;
}
if (operate_daemon_in_kde_wayland(connection, true, verbose))
return Posix.EXIT_SUCCESS;
} while (false);
}
if (daemon_type == null || daemon_type == "systemd") {
if (start_daemon_with_systemd(restart, verbose))
return Posix.EXIT_SUCCESS;
}
if (daemon_type == "systemd" || daemon_type == "kde-wayland")
return Posix.EXIT_FAILURE;
if (restart) {
var bus = get_bus();
if (bus == null) {
stderr.printf(_("Can't connect to IBus.\n"));
return Posix.EXIT_FAILURE;
}
bus.exit(true);
if (verbose) {
stderr.printf("Succeed to restart ibus-daemon with an IBus API " +
"directly.\n");
}
} else {
string startarg = "ibus-daemon";
argv[0] = startarg;
var paths = GLib.Environment.get_variable("PATH").split(":");
foreach (unowned string path in paths) {
var full_path = "%s/%s".printf(path, startarg);
if (GLib.FileUtils.test(full_path, GLib.FileTest.IS_EXECUTABLE)) {
startarg = full_path;
break;
}
}
// When ibus-daemon is launched by GLib.Process.spawn_async(),
// the parent process will be systemd
if (verbose) {
stderr.printf("Running \"%s\" directly as a foreground " +
"process.\n", startarg);
}
Posix.execv(startarg, argv);
}
return Posix.EXIT_SUCCESS;
}
int restart_daemon(string[] argv) {
return start_daemon_real(argv, true);
}
int start_daemon(string[] argv) {
return start_daemon_real(argv, false);
}
int exit_daemon(string[] argv) {
const OptionEntry[] options = {
{ "type", 0, 0, OptionArg.STRING, out daemon_type,
N_("Exit daemon with \"direct\" or \"kde-wayland\" " +
"TYPE."),
"TYPE" },
{ "verbose", 0, 0, OptionArg.NONE, out verbose,
N_("Show debug messages."), null },
{ null }
};
var option = new OptionContext();
option.add_main_entries(options, Config.GETTEXT_PACKAGE);
option.set_ignore_unknown_options(true);
try {
option.parse(ref argv);
} catch (OptionError e) {
stderr.printf("%s\n", e.message);
return Posix.EXIT_FAILURE;
}
if (daemon_type != null && daemon_type != "direct" &&
daemon_type != "kde-wayland") {
stderr.printf("type argument must be \"direct\" or \"kde-wayland\"\n");
return Posix.EXIT_FAILURE;
}
if (daemon_type == null || daemon_type == "kde-wayland") {
GLib.DBusConnection? connection = get_session_bus(verbose);
if (connection != null &&
operate_daemon_in_kde_wayland(connection, false, verbose)) {
return Posix.EXIT_SUCCESS;
}
}
if (daemon_type == "kde-wayland")
return Posix.EXIT_FAILURE;
var bus = get_bus();
if (bus == null) {
stderr.printf(_("Can't connect to IBus.\n"));
return Posix.EXIT_FAILURE;
}
bus.exit(false);
return Posix.EXIT_SUCCESS;
}
int print_version(string[] argv) {
print("IBus %s\n", Config.PACKAGE_VERSION);
return Posix.EXIT_SUCCESS;
}
int read_cache (string[] argv) {
const OptionEntry[] options = {
{ "system", 0, 0, OptionArg.NONE, out is_system,
N_("Read the system registry cache."), null },
{ "file", 0, 0, OptionArg.STRING, out cache_file,
N_("Read the registry cache FILE."), "FILE" },
{ null }
};
var option = new OptionContext();
option.add_main_entries(options, Config.GETTEXT_PACKAGE);
try {
option.parse(ref argv);
} catch (OptionError e) {
stderr.printf("%s\n", e.message);
return Posix.EXIT_FAILURE;
}
var registry = new IBus.Registry();
if (cache_file != null) {
if (!registry.load_cache_file(cache_file)) {
stderr.printf(_("The registry cache is invalid.\n"));
return Posix.EXIT_FAILURE;
}
} else {
if (!registry.load_cache(!is_system)) {
stderr.printf(_("The registry cache is invalid.\n"));
return Posix.EXIT_FAILURE;
}
}
var output = new GLib.StringBuilder();
registry.output(output, 1);
print ("%s\n", output.str);
return Posix.EXIT_SUCCESS;
}
int write_cache (string[] argv) {
const OptionEntry[] options = {
{ "system", 0, 0, OptionArg.NONE, out is_system,
N_("Write the system registry cache."), null },
{ "file", 0, 0, OptionArg.STRING, out cache_file,
N_("Write the registry cache FILE."),
"FILE" },
{ null }
};
var option = new OptionContext();
option.add_main_entries(options, Config.GETTEXT_PACKAGE);
try {
option.parse(ref argv);
} catch (OptionError e) {
stderr.printf("%s\n", e.message);
return Posix.EXIT_FAILURE;
}
var registry = new IBus.Registry();
registry.load();
if (cache_file != null) {
return registry.save_cache_file(cache_file) ?
Posix.EXIT_SUCCESS : Posix.EXIT_FAILURE;
}
return registry.save_cache(!is_system) ?
Posix.EXIT_SUCCESS : Posix.EXIT_FAILURE;
}
int print_address(string[] argv) {
string address = IBus.get_address();
print("%s\n", address != null ? address : "(null)");
return Posix.EXIT_SUCCESS;
}
private int read_config_options(string[] argv) {
const OptionEntry[] options = {
{ "engine-id", 0, 0, OptionArg.STRING, out engine_id,
N_("Use engine schema paths instead of ibus core, " +
"which can be comma-separated values."), "ENGINE_ID" },
{ null }
};
var option = new OptionContext();
option.add_main_entries(options, Config.GETTEXT_PACKAGE);
try {
option.parse(ref argv);
} catch (OptionError e) {
stderr.printf("%s\n", e.message);
return Posix.EXIT_FAILURE;
}
return Posix.EXIT_SUCCESS;
}
private GLib.SList<string> get_ibus_schemas() {
string[] ids = {};
if (engine_id != null) {
ids = engine_id.split(",");
}
GLib.SList<string> ibus_schemas = new GLib.SList<string>();
GLib.SettingsSchemaSource schema_source =
GLib.SettingsSchemaSource.get_default();
string[] list_schemas = {};
schema_source.list_schemas(true, out list_schemas, null);
foreach (string schema in list_schemas) {
if (ids.length != 0) {
foreach (unowned string id in ids) {
if (id == schema ||
schema.has_prefix("org.freedesktop.ibus.engine." + id)) {
ibus_schemas.prepend(schema);
break;
}
}
} else if (schema.has_prefix("org.freedesktop.ibus") &&
!schema.has_prefix("org.freedesktop.ibus.engine")) {
ibus_schemas.prepend(schema);
}
}
if (ibus_schemas.length() == 0) {
printerr("Not found schemas of \"org.freedesktop.ibus\"\n");
return ibus_schemas;
}
ibus_schemas.sort(GLib.strcmp);
return ibus_schemas;
}
int read_config(string[] argv) {
if (read_config_options(argv) == Posix.EXIT_FAILURE)
return Posix.EXIT_FAILURE;
GLib.SList<string> ibus_schemas = get_ibus_schemas();
if (ibus_schemas.length() == 0)
return Posix.EXIT_FAILURE;
GLib.SettingsSchemaSource schema_source =
GLib.SettingsSchemaSource.get_default();
var output = new GLib.StringBuilder();
foreach (string schema in ibus_schemas) {
GLib.SettingsSchema settings_schema = schema_source.lookup(schema,
false);
GLib.Settings settings = new GLib.Settings(schema);
output.append_printf("SCHEMA: %s\n", schema);
foreach (string key in settings_schema.list_keys()) {
GLib.Variant variant = settings.get_value(key);
output.append_printf(" %s: %s\n", key, variant.print(true));
}
}
print("%s", output.str);
return Posix.EXIT_SUCCESS;
}
int reset_config(string[] argv) {
if (read_config_options(argv) == Posix.EXIT_FAILURE)
return Posix.EXIT_FAILURE;
GLib.SList<string> ibus_schemas = get_ibus_schemas();
if (ibus_schemas.length() == 0)
return Posix.EXIT_FAILURE;
print("%s\n", _("Resetting…"));
GLib.SettingsSchemaSource schema_source =
GLib.SettingsSchemaSource.get_default();
foreach (string schema in ibus_schemas) {
GLib.SettingsSchema settings_schema = schema_source.lookup(schema,
false);
GLib.Settings settings = new GLib.Settings(schema);
print("SCHEMA: %s\n", schema);
foreach (string key in settings_schema.list_keys()) {
print(" %s\n", key);
settings.reset(key);
}
}
GLib.Settings.sync();
print("%s\n", _("Done"));
return Posix.EXIT_SUCCESS;
}
#if EMOJI_DICT
int emoji_dialog(string[] argv) {
string cmd = Config.LIBEXECDIR + "/ibus-ui-emojier";
var file = File.new_for_path(cmd);
if (!file.query_exists())
cmd = "../ui/gtk3/ibus-ui-emojier";
argv[0] = cmd;
string[] env = Environ.get();
try {
// Non-blocking
Process.spawn_async(null, argv, env,
SpawnFlags.SEARCH_PATH,
null, null);
} catch (SpawnError e) {
stderr.printf("%s\n", e.message);
return Posix.EXIT_FAILURE;
}
return Posix.EXIT_SUCCESS;
}
#endif
int read_im_module(string[] argv) {
string? im_module = IBusIMModule.im_module_get_id(argv);
if (im_module == null)
return Posix.EXIT_FAILURE;
print("%s\n".printf(im_module));
return Posix.EXIT_SUCCESS;
}
int print_help(string[] argv) {
print_usage(stdout);
return Posix.EXIT_SUCCESS;
}
delegate int EntryFunc(string[] argv);
struct CommandEntry {
unowned string name;
unowned string description;
unowned EntryFunc entry;
}
const CommandEntry commands[] = {
{ "engine", N_("Set or get engine"), get_set_engine },
{ "exit", N_("Exit ibus-daemon"), exit_daemon },
{ "list-engine", N_("Show available engines"), list_engine },
{ "watch", N_("(Not implemented)"), message_watch },
{ "restart", N_("Restart ibus-daemon"), restart_daemon },
{ "start", N_("Start ibus-daemon"), start_daemon },
{ "version", N_("Show version"), print_version },
{ "read-cache", N_("Show the content of registry cache"), read_cache },
{ "write-cache", N_("Create registry cache"), write_cache },
{ "address", N_("Print the D-Bus address of ibus-daemon"), print_address },
{ "read-config", N_("Show the configuration values"), read_config },
{ "reset-config", N_("Reset the configuration values"), reset_config },
#if EMOJI_DICT
{ "emoji", N_("Save emoji on dialog to clipboard"), emoji_dialog },
#endif
{ "im-module", N_("Retrieve im-module value from GTK instance"),
read_im_module },
{ "help", N_("Show this information"), print_help }
};
static string program_name;
void print_usage(FileStream stream) {
stream.printf(_("Usage: %s COMMAND [OPTION...]\n\n"), program_name);
stream.printf(_("Commands:\n"));
for (int i = 0; i < commands.length; i++) {
stream.printf(" %-12s %s\n",
commands[i].name,
GLib.dgettext(null, commands[i].description));
}
}