1 /* vim:set et sts=4 sw=4:
5 * Copyright(c) 2011 Peng Huang <shawn.p.huang@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or(at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
23 class Panel : IBus.PanelService {
24 private class Keybinding {
25 public Keybinding(uint keysym,
26 Gdk.ModifierType modifiers,
29 this.modifiers = modifiers;
30 this.reverse = reverse;
33 public uint keysym { get; set; }
34 public Gdk.ModifierType modifiers { get; set; }
35 public bool reverse { get; set; }
38 private IBus.Bus m_bus;
39 private IBus.Config m_config;
40 private Gtk.StatusIcon m_status_icon;
41 private Gtk.Menu m_ime_menu;
42 private Gtk.Menu m_sys_menu;
43 private IBus.EngineDesc[] m_engines = {};
44 private CandidatePanel m_candidate_panel;
45 private Switcher m_switcher;
46 private PropertyManager m_property_manager;
47 private GLib.Pid m_setup_pid = 0;
48 private Gtk.AboutDialog m_about_dialog;
49 private Gtk.CssProvider m_css_provider;
50 private int m_switcher_delay_time = 400;
51 private const string ACCELERATOR_SWITCH_IME_FOREWARD = "<Control>space";
53 private GLib.List<Keybinding> m_keybindings = new GLib.List<Keybinding>();
55 public Panel(IBus.Bus bus) {
56 GLib.assert(bus.is_connected());
57 // Chain up base class constructor
58 GLib.Object(connection : bus.get_connection(),
59 object_path : "/org/freedesktop/IBus/Panel");
64 m_status_icon = new Gtk.StatusIcon();
65 m_status_icon.set_name("ibus-ui-gtk");
66 m_status_icon.set_title("IBus Panel");
67 m_status_icon.popup_menu.connect(status_icon_popup_menu_cb);
68 m_status_icon.activate.connect(status_icon_activate_cb);
69 m_status_icon.set_from_icon_name("ibus-keyboard");
71 m_candidate_panel = new CandidatePanel();
72 m_candidate_panel.page_up.connect((w) => this.page_up());
73 m_candidate_panel.page_down.connect((w) => this.page_down());
75 m_switcher = new Switcher();
76 // The initial shortcut is "<Control>space"
77 bind_switch_shortcut(null);
79 if (m_switcher_delay_time >= 0) {
80 m_switcher.set_popup_delay_time((uint) m_switcher_delay_time);
83 m_property_manager = new PropertyManager();
84 m_property_manager.property_activate.connect((k, s) => {
85 property_activate(k, s);
92 unbind_switch_shortcut();
95 private void keybinding_manager_bind(KeybindingManager keybinding_manager,
96 string? accelerator) {
97 uint switch_keysym = 0;
98 Gdk.ModifierType switch_modifiers = 0;
99 Gdk.ModifierType reverse_modifier = Gdk.ModifierType.SHIFT_MASK;
100 Keybinding keybinding;
102 Gtk.accelerator_parse(accelerator,
103 out switch_keysym, out switch_modifiers);
105 // Map virtual modifiers to (i.e. Mod2, Mod3, ...)
106 const Gdk.ModifierType VIRTUAL_MODIFIERS = (
107 Gdk.ModifierType.SUPER_MASK |
108 Gdk.ModifierType.HYPER_MASK |
109 Gdk.ModifierType.META_MASK);
110 if ((switch_modifiers & VIRTUAL_MODIFIERS) != 0) {
111 // workaround a bug in gdk vapi vala > 0.18
112 // https://bugzilla.gnome.org/show_bug.cgi?id=677559
114 Gdk.Keymap.get_default().map_virtual_modifiers(
115 ref switch_modifiers);
117 if ((switch_modifiers & Gdk.ModifierType.SUPER_MASK) != 0)
118 switch_modifiers |= Gdk.ModifierType.MOD4_MASK;
119 if ((switch_modifiers & Gdk.ModifierType.HYPER_MASK) != 0)
120 switch_modifiers |= Gdk.ModifierType.MOD4_MASK;
122 switch_modifiers &= ~VIRTUAL_MODIFIERS;
125 if (switch_keysym == 0 && switch_modifiers == 0) {
126 warning("Parse accelerator '%s' failed!", accelerator);
130 keybinding = new Keybinding(switch_keysym,
133 m_keybindings.append(keybinding);
135 keybinding_manager.bind(switch_keysym, switch_modifiers,
136 (e) => handle_engine_switch(e, false));
138 // accelerator already has Shift mask
139 if ((switch_modifiers & reverse_modifier) != 0) {
143 switch_modifiers |= reverse_modifier;
145 keybinding = new Keybinding(switch_keysym,
148 m_keybindings.append(keybinding);
150 keybinding_manager.bind(switch_keysym, switch_modifiers,
151 (e) => handle_engine_switch(e, true));
154 private void bind_switch_shortcut(Variant? variant) {
155 string[] accelerators = {};
156 Variant var_trigger = variant;
158 if (var_trigger == null && m_config != null) {
159 var_trigger = m_config.get_value("general/hotkey",
163 if (var_trigger != null) {
164 accelerators = var_trigger.dup_strv();
166 accelerators += ACCELERATOR_SWITCH_IME_FOREWARD;
169 var keybinding_manager = KeybindingManager.get_instance();
171 foreach (var accelerator in accelerators) {
172 keybinding_manager_bind(keybinding_manager, accelerator);
176 private void unbind_switch_shortcut() {
177 var keybinding_manager = KeybindingManager.get_instance();
179 unowned GLib.List<Keybinding> keybindings = m_keybindings;
181 while (keybindings != null) {
182 Keybinding keybinding = keybindings.data;
184 keybinding_manager.unbind(keybinding.keysym,
185 keybinding.modifiers);
186 keybindings = keybindings.next;
189 m_keybindings = null;
192 private void set_custom_font() {
193 Gdk.Display display = Gdk.Display.get_default();
194 Gdk.Screen screen = (display != null) ?
195 display.get_default_screen() : null;
197 if (screen == null) {
198 warning("Could not open display.");
202 bool use_custom_font = false;
203 GLib.Variant var_use_custom_font = m_config.get_value("panel",
206 if (var_use_custom_font != null) {
207 use_custom_font = var_use_custom_font.get_boolean();
210 if (m_css_provider != null) {
211 Gtk.StyleContext.remove_provider_for_screen(screen,
213 m_css_provider = null;
216 if (use_custom_font == false) {
220 string font_name = null;
221 GLib.Variant var_custom_font = m_config.get_value("panel",
223 if (var_custom_font != null) {
224 font_name = var_custom_font.dup_string();
227 if (font_name == null) {
228 warning("No config panel:custom_font.");
232 string data_format = "GtkLabel { font: %s; }";
233 string data = data_format.printf(font_name);
234 m_css_provider = new Gtk.CssProvider();
237 m_css_provider.load_from_data(data, -1);
238 } catch (GLib.Error e) {
239 warning("Failed css_provider_from_data: %s: %s", font_name,
244 Gtk.StyleContext.add_provider_for_screen(screen,
246 Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
249 private void set_switcher_delay_time(Variant? variant) {
250 Variant var_switcher_delay_time = variant;
252 if (var_switcher_delay_time == null) {
253 var_switcher_delay_time = m_config.get_value("general",
254 "switcher-delay-time");
257 if (var_switcher_delay_time == null) {
261 m_switcher_delay_time = var_switcher_delay_time.get_int32();
263 if (m_switcher_delay_time >= 0) {
264 m_switcher.set_popup_delay_time((uint) m_switcher_delay_time);
268 public void set_config(IBus.Config config) {
269 if (m_config != null) {
270 m_config.value_changed.disconnect(config_value_changed_cb);
271 m_config.watch(null, null);
276 if (m_config != null) {
277 m_config.value_changed.connect(config_value_changed_cb);
278 m_config.watch("general", "preload_engines");
279 m_config.watch("general", "engines_order");
280 m_config.watch("general", "switcher_delay_time");
281 m_config.watch("general/hotkey", "triggers");
282 m_config.watch("panel", "custom_font");
283 m_config.watch("panel", "use_custom_font");
284 update_engines(m_config.get_value("general", "preload_engines"),
285 m_config.get_value("general", "engines_order"));
286 unbind_switch_shortcut();
287 bind_switch_shortcut(null);
288 set_switcher_delay_time(null);
290 update_engines(null, null);
296 private void exec_setxkbmap(IBus.EngineDesc engine) {
297 string layout = engine.get_layout();
298 string variant = engine.get_layout_variant();
299 string option = engine.get_layout_option();
300 string standard_error = null;
302 string[] args = { "setxkbmap" };
304 if (layout != null && layout != "" && layout != "default") {
308 if (variant != null && variant != "" && variant != "default") {
312 if (option != null && option != "" && option != "default") {
313 /*TODO: Need to get the session XKB options */
319 if (args.length == 1) {
324 if (!GLib.Process.spawn_sync(null, args, null,
325 GLib.SpawnFlags.SEARCH_PATH,
329 warning("Switch xkb layout to %s failed.",
330 engine.get_layout());
332 } catch (GLib.SpawnError e) {
333 warning("Execute setxkbmap failed: %s", e.message);
336 if (exit_status != 0) {
337 warning("Execute setxkbmap failed: %s", standard_error ?? "(null)");
341 private void switch_engine(int i, bool force = false) {
342 GLib.assert(i >= 0 && i < m_engines.length);
344 // Do not need switch
345 if (i == 0 && !force)
348 IBus.EngineDesc engine = m_engines[i];
350 if (!m_bus.set_global_engine(engine.get_name())) {
351 warning("Switch engine to %s failed.", engine.get_name());
355 exec_setxkbmap(engine);
358 private void config_value_changed_cb(IBus.Config config,
362 if (section == "general" && name == "preload_engines") {
363 update_engines(variant, null);
367 if (section == "general/hotkey" && name == "triggers") {
368 unbind_switch_shortcut();
369 bind_switch_shortcut(variant);
373 if (section == "panel" && (name == "custom_font" ||
374 name == "use_custom_font")) {
379 if (section == "general" && name == "switcher_delay_time") {
380 set_switcher_delay_time(variant);
385 private void handle_engine_switch(Gdk.Event event, bool revert) {
386 // Do not need switch IME
387 if (m_engines.length <= 1)
390 uint keyval = event.key.keyval;
391 uint modifiers = KeybindingManager.MODIFIER_FILTER & event.key.state;
393 uint primary_modifiers =
394 KeybindingManager.get_primary_modifier(event.key.state);
396 bool pressed = KeybindingManager.primary_modifier_still_pressed(
397 event, primary_modifiers);
400 modifiers &= ~Gdk.ModifierType.SHIFT_MASK;
403 if (pressed && m_switcher_delay_time >= 0) {
404 int i = revert ? m_engines.length - 1 : 1;
405 i = m_switcher.run(keyval, modifiers, event, m_engines, i);
407 debug("switch cancelled");
409 GLib.assert(i < m_engines.length);
413 int i = revert ? m_engines.length - 1 : 1;
418 private void update_engines(GLib.Variant? var_engines,
419 GLib.Variant? var_order) {
420 string[] engine_names = null;
422 if (var_engines != null)
423 engine_names = var_engines.dup_strv();
424 if (engine_names == null || engine_names.length == 0)
425 engine_names = {"xkb:us::eng"};
427 string[] order_names =
428 (var_order != null) ? var_order.dup_strv() : null;
432 foreach (var name in order_names) {
433 if (name in engine_names)
437 foreach (var name in engine_names) {
443 var engines = m_bus.get_engines_by_names(names);
445 if (m_engines.length == 0) {
447 switch_engine(0, true);
449 var current_engine = m_engines[0];
452 for (i = 0; i < m_engines.length; i++) {
453 if (current_engine.get_name() == engines[i].get_name()) {
458 switch_engine(0, true);
463 private void show_setup_dialog() {
464 if (m_setup_pid != 0) {
465 if (Posix.kill(m_setup_pid, Posix.SIGUSR1) == 0)
470 string binary = GLib.Path.build_filename(Config.BINDIR, "ibus-setup");
472 GLib.Process.spawn_async(null,
473 {binary, "ibus-setup"},
475 GLib.SpawnFlags.DO_NOT_REAP_CHILD,
478 } catch (GLib.SpawnError e) {
479 warning("Execute %s failed! %s", binary, e.message);
483 GLib.ChildWatch.add(m_setup_pid, (pid, state) => {
484 if (pid != m_setup_pid)
487 GLib.Process.close_pid(pid);
491 private void show_about_dialog() {
492 if (m_about_dialog == null) {
493 m_about_dialog = new Gtk.AboutDialog();
494 m_about_dialog.set_program_name("IBus");
495 m_about_dialog.set_version(Config.PACKAGE_VERSION);
497 string copyright = _(
498 "Copyright (c) 2007-2012 Peng Huang\n" +
499 "Copyright (c) 2007-2010 Red Hat, Inc.\n");
501 m_about_dialog.set_copyright(copyright);
502 m_about_dialog.set_license("LGPL");
503 m_about_dialog.set_comments(_("IBus is an intelligent input bus for Linux/Unix."));
504 m_about_dialog.set_website("http://code.google.com/p/ibus");
505 m_about_dialog.set_authors({"Peng Huang <shawn.p.huang@gmail.com>"});
506 m_about_dialog.set_documenters({"Peng Huang <shawn.p.huang@gmail.com>"});
507 m_about_dialog.set_translator_credits(_("translator-credits"));
508 m_about_dialog.set_logo_icon_name("ibus");
509 m_about_dialog.set_icon_name("ibus");
512 if (!m_about_dialog.get_visible()) {
513 m_about_dialog.run();
514 m_about_dialog.hide();
516 m_about_dialog.present();
520 private void status_icon_popup_menu_cb(Gtk.StatusIcon status_icon,
522 uint activate_time) {
524 if (m_sys_menu == null) {
525 Gtk.ImageMenuItem item;
526 m_sys_menu = new Gtk.Menu();
528 item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.PREFERENCES, null);
529 item.activate.connect((i) => show_setup_dialog());
530 m_sys_menu.append(item);
532 item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.ABOUT, null);
533 item.activate.connect((i) => show_about_dialog());
534 m_sys_menu.append(item);
536 m_sys_menu.append(new Gtk.SeparatorMenuItem());
538 item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.REFRESH, null);
539 item.set_label(_("Restart"));
540 item.activate.connect((i) => m_bus.exit(true));
541 m_sys_menu.append(item);
543 item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.QUIT, null);
544 item.activate.connect((i) => m_bus.exit(false));
545 m_sys_menu.append(item);
547 m_sys_menu.show_all();
550 m_sys_menu.popup(null,
552 m_status_icon.position_menu,
554 Gtk.get_current_event_time());
557 private void status_icon_activate_cb(Gtk.StatusIcon status_icon) {
558 m_ime_menu = new Gtk.Menu();
560 // Show properties and IME switching menu
561 m_property_manager.create_menu_items(m_ime_menu);
563 m_ime_menu.append(new Gtk.SeparatorMenuItem());
566 foreach (var engine in m_engines) {
567 var language = engine.get_language();
568 var longname = engine.get_longname();
569 var item = new Gtk.ImageMenuItem.with_label(
570 "%s - %s".printf (IBus.get_language_name(language), longname));
571 if (engine.get_icon() != "") {
572 var icon = new IconWidget(engine.get_icon(), Gtk.IconSize.MENU);
573 item.set_image(icon);
575 // Make a copy of engine to workaround a bug in vala.
576 // https://bugzilla.gnome.org/show_bug.cgi?id=628336
578 item.activate.connect((item) => {
579 for (int i = 0; i < m_engines.length; i++) {
580 if (e == m_engines[i]) {
586 m_ime_menu.add(item);
589 m_ime_menu.show_all();
591 // Do not take focuse to avoid some focus related issues.
592 m_ime_menu.set_take_focus(false);
593 m_ime_menu.popup(null,
595 m_status_icon.position_menu,
597 Gtk.get_current_event_time());
600 /* override virtual functions */
601 public override void set_cursor_location(int x, int y,
602 int width, int height) {
603 m_candidate_panel.set_cursor_location(x, y, width, height);
606 public override void focus_in(string input_context_path) {
609 public override void focus_out(string input_context_path) {
612 public override void register_properties(IBus.PropList props) {
613 m_property_manager.set_properties(props);
616 public override void update_property(IBus.Property prop) {
617 m_property_manager.update_property(prop);
620 public override void update_preedit_text(IBus.Text text,
624 m_candidate_panel.set_preedit_text(text, cursor_pos);
626 m_candidate_panel.set_preedit_text(null, 0);
629 public override void hide_preedit_text() {
630 m_candidate_panel.set_preedit_text(null, 0);
633 public override void update_auxiliary_text(IBus.Text text,
635 m_candidate_panel.set_auxiliary_text(visible ? text : null);
638 public override void hide_auxiliary_text() {
639 m_candidate_panel.set_auxiliary_text(null);
642 public override void update_lookup_table(IBus.LookupTable table,
644 m_candidate_panel.set_lookup_table(visible ? table : null);
647 public override void hide_lookup_table() {
648 m_candidate_panel.set_lookup_table(null);
651 public override void state_changed() {
652 var icon_name = "ibus-keyboard";
654 var engine = m_bus.get_global_engine();
656 icon_name = engine.get_icon();
658 if (icon_name[0] == '/')
659 m_status_icon.set_from_file(icon_name);
661 var theme = Gtk.IconTheme.get_default();
662 if (theme.lookup_icon(icon_name, 48, 0) != null) {
663 m_status_icon.set_from_icon_name(icon_name);
665 m_status_icon.set_from_icon_name("ibus-engine");
673 for (i = 0; i < m_engines.length; i++) {
674 if (m_engines[i].get_name() == engine.get_name())
678 // engine is first engine in m_engines.
682 // engine is not in m_engines.
683 if (i >= m_engines.length)
686 for (int j = i; j > 0; j--) {
687 m_engines[j] = m_engines[j - 1];
689 m_engines[0] = engine;
692 foreach(var desc in m_engines) {
693 names += desc.get_name();
695 if (m_config != null)
696 m_config.set_value("general",
698 new GLib.Variant.strv(names));