diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile.am | 7 | ||||
-rw-r--r-- | lib/ibus-menu.vala | 26 | ||||
-rw-r--r-- | lib/indicator-menu.vala | 83 | ||||
-rw-r--r-- | lib/main.vala | 329 | ||||
-rw-r--r-- | lib/source.vala | 153 |
5 files changed, 403 insertions, 195 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index c57725c1..d45b98dd 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -22,11 +22,12 @@ indicator_keyboard_service_VALAFLAGS = $(AM_VALAFLAGS) \ --pkg pangocairo \ --pkg gtk+-3.0 \ --pkg GDesktopEnums-3.0 \ - --pkg gnome-desktop-3.0 \ + --pkg GnomeDesktop-3.0 \ --pkg Xkl-1.0 \ --pkg Gkbd-3.0 \ --pkg ibus-1.0 \ - --pkg accountsservice \ + --pkg Fcitx-1.0 \ + --pkg AccountsService-1.0 \ --pkg liblightdm-gobject-1 indicator_keyboard_service_CFLAGS = $(AM_CFLAGS) \ $(GEE_CFLAGS) \ @@ -36,6 +37,7 @@ indicator_keyboard_service_CFLAGS = $(AM_CFLAGS) \ $(LIBXKLAVIER_CFLAGS) \ $(LIBGNOMEKBD_CFLAGS) \ $(IBUS_CFLAGS) \ + $(FCITX_GCLIENT_CFLAGS) \ $(ACCOUNTSSERVICE_CFLAGS) \ $(LIGHTDM_CFLAGS) \ $(COVERAGE_CFLAGS) @@ -47,6 +49,7 @@ indicator_keyboard_service_LDFLAGS = $(AM_LDFLAGS) \ $(LIBXKLAVIER_LIBS) \ $(LIBGNOMEKBD_LIBS) \ $(IBUS_LIBS) \ + $(FCITX_GCLIENT_LIBS) \ $(ACCOUNTSSERVICE_LIBS) \ $(LIGHTDM_LIBS) \ $(COVERAGE_LDFLAGS) diff --git a/lib/ibus-menu.vala b/lib/ibus-menu.vala index c0862ac3..a240f00b 100644 --- a/lib/ibus-menu.vala +++ b/lib/ibus-menu.vala @@ -128,10 +128,6 @@ public class Indicator.Keyboard.IBusMenu : MenuModel { var state = new Variant.boolean (property.state == IBus.PropState.CHECKED); var action = new SimpleAction.stateful (name, null, state); - action.activate.connect ((parameter) => { - action.change_state (new Variant.boolean (!action.get_state ().get_boolean ())); - }); - action.change_state.connect ((value) => { if (value != null) { action.set_state ((!) value); @@ -154,26 +150,26 @@ public class Indicator.Keyboard.IBusMenu : MenuModel { /* Create a single action for all radio properties. */ if (action_map != null && radio_name == null) { radio_counter++; - radio_name = @"-private-radio-$radio_counter"; - radio_action = new SimpleAction.stateful ((!) radio_name, VariantType.STRING, new Variant.string ("")); - ((!) radio_action).activate.connect ((parameter) => { - ((!) radio_action).change_state (parameter); - }); + var name = @"-private-radio-$radio_counter"; + var action = new SimpleAction.stateful (name, VariantType.STRING, new Variant.string ("")); - ((!) radio_action).change_state.connect ((value) => { + action.change_state.connect ((value) => { if (value != null) { var key = ((!) value).get_string (); if (radio_properties.has_key (key)) { - ((!) radio_action).set_state ((!) value); + action.set_state ((!) value); activate (radio_properties[key], IBus.PropState.CHECKED); } } }); - ((!) action_map).add_action ((!) radio_action); - names.add ((!) radio_name); + ((!) action_map).add_action (action); + names.add (name); + + radio_name = name; + radio_action = action; } radio_properties[property.key] = property; @@ -195,7 +191,7 @@ public class Indicator.Keyboard.IBusMenu : MenuModel { private void append_menu_property (IBus.Property property) { if (property.prop_type == IBus.PropType.MENU) { - var submenu = new IBusMenu (action_map, ((!) property).sub_props); + var submenu = new IBusMenu (action_map, property.sub_props); submenu.activate.connect ((property, state) => { activate (property, state); }); menu.append_submenu (get_label (property), submenu); } @@ -295,7 +291,7 @@ public class Indicator.Keyboard.IBusMenu : MenuModel { return menu.get_item_link (item_index, link); } - public override void get_item_links (int item_index, out HashTable<string, MenuModel>? links) { + public override void get_item_links (int item_index, out HashTable<string, MenuModel> links) { menu.get_item_links (item_index, out links); } diff --git a/lib/indicator-menu.vala b/lib/indicator-menu.vala index 8e5661e2..96bf64d6 100644 --- a/lib/indicator-menu.vala +++ b/lib/indicator-menu.vala @@ -19,10 +19,11 @@ public class Indicator.Keyboard.IndicatorMenu : MenuModel { public enum Options { - NONE = 0x0, - DCONF = 0x1, - IBUS = 0x2, - SETTINGS = 0x4 + NONE = 0, + DCONF = 1 << 0, + XKB = 1 << 1, + IBUS = 1 << 2, + SETTINGS = 1 << 3 } private Options options; @@ -32,42 +33,45 @@ public class Indicator.Keyboard.IndicatorMenu : MenuModel { private IBusMenu properties_section; public IndicatorMenu (ActionMap? action_map = null, Options options = Options.NONE) { - var submenu = new Menu (); + this.options = options; + indicator_menu = new Menu (); sources_section = new Menu (); - submenu.append_section (null, sources_section); - if ((options & Options.IBUS) != Options.NONE) { - properties_section = new IBusMenu (action_map); - properties_section.activate.connect ((property, state) => { activate (property, state); }); - submenu.append_section (null, properties_section); - } + if ((options & ~Options.DCONF) != Options.NONE) { + var submenu = new Menu (); - if ((options & Options.SETTINGS) != Options.NONE) { - var settings_section = new Menu (); - settings_section.append (_ ("Character Map"), "indicator.map"); - settings_section.append (_ ("Keyboard Layout Chart"), "indicator.chart"); - settings_section.append (_ ("Text Entry Settings..."), "indicator.settings"); - submenu.append_section (null, settings_section); - } + submenu.append_section (null, sources_section); - var indicator = new MenuItem.submenu (null, submenu); - indicator.set_detailed_action ("indicator.indicator"); - indicator.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); - - /* We need special mouse actions on the lock screen. */ - if ((options & Options.DCONF) != Options.NONE) { - indicator.set_attribute ("x-canonical-secondary-action", "s", "indicator.next"); - indicator.set_attribute ("x-canonical-scroll-action", "s", "indicator.scroll"); - } else { - indicator.set_attribute ("x-canonical-secondary-action", "s", "indicator.locked_next"); - indicator.set_attribute ("x-canonical-scroll-action", "s", "indicator.locked_scroll"); - } + if (Options.IBUS in options) { + properties_section = new IBusMenu (action_map); + properties_section.activate.connect ((property, state) => { activate (property, state); }); + submenu.append_section (null, properties_section); + } - indicator_menu = new Menu (); - indicator_menu.append_item (indicator); + if (Options.SETTINGS in options) { + var settings_section = new Menu (); + settings_section.append (_ ("Character Map"), "indicator.map"); + settings_section.append (_ ("Keyboard Layout Chart"), "indicator.chart"); + settings_section.append (_ ("Text Entry Settings..."), "indicator.settings"); + submenu.append_section (null, settings_section); + } - this.options = options; + var indicator = new MenuItem.submenu (null, submenu); + indicator.set_detailed_action ("indicator.indicator"); + indicator.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); + + /* We need special mouse actions on the lock screen. */ + if (Options.DCONF in options) { + indicator.set_attribute ("x-canonical-secondary-action", "s", "indicator.next"); + indicator.set_attribute ("x-canonical-scroll-action", "s", "indicator.scroll"); + } else { + indicator.set_attribute ("x-canonical-secondary-action", "s", "indicator.locked_next"); + indicator.set_attribute ("x-canonical-scroll-action", "s", "indicator.locked_scroll"); + } + + indicator_menu.append_item (indicator); + } } public signal void activate (IBus.Property property, IBus.PropState state); @@ -76,10 +80,13 @@ public class Indicator.Keyboard.IndicatorMenu : MenuModel { sources_section.remove_all (); for (var i = 0; i < sources.length; i++) { - if (!sources[i].is_ibus || (options & Options.IBUS) != Options.NONE) { + var visible = (sources[i].is_xkb && Options.XKB in options) || + (sources[i].is_ibus && Options.IBUS in options); + + if (visible) { string action; - if ((options & Options.DCONF) != Options.NONE) { + if (Options.DCONF in options) { action = "indicator.current"; } else { action = "indicator.active"; @@ -99,13 +106,13 @@ public class Indicator.Keyboard.IndicatorMenu : MenuModel { } public void set_properties (IBus.PropList properties) { - if ((options & Options.IBUS) != Options.NONE) { + if (Options.IBUS in options) { properties_section.set_properties (properties); } } public void update_property (IBus.Property property) { - if ((options & Options.IBUS) != Options.NONE) { + if (Options.IBUS in options) { properties_section.update_property (property); } } @@ -122,7 +129,7 @@ public class Indicator.Keyboard.IndicatorMenu : MenuModel { indicator_menu.get_item_attributes (item_index, out attributes); } - public override void get_item_links (int item_index, out HashTable<string, MenuModel>? links) { + public override void get_item_links (int item_index, out HashTable<string, MenuModel> links) { indicator_menu.get_item_links (item_index, out links); } diff --git a/lib/main.vala b/lib/main.vala index 1cb3896c..d3e063d1 100644 --- a/lib/main.vala +++ b/lib/main.vala @@ -33,7 +33,7 @@ public class Indicator.Keyboard.Service : Object { private SList<Act.User> users; private WindowStack? window_stack; - private Gee.HashMap<uint, uint>? window_sources; + private Gee.HashMap<uint, Source>? window_sources; private uint focused_window_id; private IBus.Bus? ibus; @@ -41,6 +41,9 @@ public class Indicator.Keyboard.Service : Object { private ulong ibus_connected_id; private uint panel_timeout; + private Fcitx.InputMethod? fcitx; + private bool fcitx_initialized; + private Source[]? sources; private SimpleActionGroup? action_group; @@ -118,11 +121,13 @@ public class Indicator.Keyboard.Service : Object { handle_unity_name_appeared, handle_unity_name_vanished); - Bus.watch_name (BusType.SESSION, - "com.canonical.Unity.WindowStack", - BusNameWatcherFlags.NONE, - handle_window_stack_name_appeared, - handle_window_stack_name_vanished); + if (!is_fcitx_active ()) { + Bus.watch_name (BusType.SESSION, + "com.canonical.Unity.WindowStack", + BusNameWatcherFlags.NONE, + handle_window_stack_name_appeared, + handle_window_stack_name_vanished); + } } indicator_settings = new Settings ("com.canonical.indicator.keyboard"); @@ -146,11 +151,33 @@ public class Indicator.Keyboard.Service : Object { } [DBus (visible = false)] + private static bool is_ibus_active () { + if (is_login_user ()) { + return false; + } + + var module = Environment.get_variable ("GTK_IM_MODULE"); + return module != null && (!) module == "ibus"; + } + + [DBus (visible = false)] + private static bool is_fcitx_active () { + if (is_login_user ()) { + return false; + } + + var module = Environment.get_variable ("GTK_IM_MODULE"); + return module != null && (!) module == "fcitx"; + } + + [DBus (visible = false)] private IBus.Bus get_ibus () { if (ibus == null) { IBus.init (); - ibus = new IBus.Bus (); - ((!) ibus).connected.connect (() => { + + var proxy = new IBus.Bus (); + + proxy.connected.connect (() => { if (desktop_menu != null) { get_desktop_menu ().set_sources (get_sources ()); } @@ -167,6 +194,8 @@ public class Indicator.Keyboard.Service : Object { update_indicator_action (); } }); + + ibus = proxy; } return (!) ibus; @@ -180,9 +209,9 @@ public class Indicator.Keyboard.Service : Object { var path = "/org/freedesktop/IBus/Panel"; try { - ibus_panel = connection.get_proxy_sync (name, path); + var proxy = connection.get_proxy_sync<IBusPanel> (name, path); - ((!) ibus_panel).properties_registered.connect ((variant) => { + proxy.properties_registered.connect ((variant) => { var properties = new IBus.PropList (); properties.deserialize (variant); @@ -190,7 +219,7 @@ public class Indicator.Keyboard.Service : Object { handle_properties_registered ((!) (properties as IBus.PropList)); } }); - ((!) ibus_panel).property_updated.connect ((variant) => { + proxy.property_updated.connect ((variant) => { var type = IBus.PropType.NORMAL; var state = IBus.PropState.INCONSISTENT; var text = new IBus.Text.from_static_string (""); @@ -201,6 +230,8 @@ public class Indicator.Keyboard.Service : Object { handle_property_updated ((!) (property as IBus.Property)); } }); + + ibus_panel = proxy; } catch (IOError error) { warning ("error: %s", error.message); } @@ -210,10 +241,30 @@ public class Indicator.Keyboard.Service : Object { } [DBus (visible = false)] + private Fcitx.InputMethod? get_fcitx () { + if (!fcitx_initialized) { + fcitx_initialized = true; + + if (is_fcitx_active ()) { + try { + var proxy = new Fcitx.InputMethod (BusType.SESSION, DBusProxyFlags.NONE, 0); + proxy.notify["current-im"].connect ((pspec) => { handle_changed_current ("current"); }); + fcitx = proxy; + } catch (Error error) { + warning ("error: %s", error.message); + } + } + } + + return fcitx; + } + + [DBus (visible = false)] public void up () { if (loop == null) { - loop = new MainLoop (); - ((!) loop).run (); + var main_loop = new MainLoop (); + loop = main_loop; + main_loop.run (); } } @@ -254,17 +305,13 @@ public class Indicator.Keyboard.Service : Object { Act.User? user = manager.get_user ((!) greeter_user); if (user != null && ((!) user).is_loaded) { - VariantIter outer; - VariantIter inner; - - var sources = ((!) user).input_sources; - sources.get ("aa{ss}", out outer); + foreach (var outer in ((!) user).input_sources) { + foreach (var inner in (!) outer) { + unowned string key; + unowned string value; - while (outer.next ("a{ss}", out inner)) { - unowned string key; - unowned string value; + ((!) inner).get ("{&s&s}", out key, out value); - while (inner.next ("{&s&s}", out key, out value)) { if (key == "xkb") { source = value; break; @@ -289,9 +336,7 @@ public class Indicator.Keyboard.Service : Object { } if (layouts.length > 0) { - source = layouts[0]; - source = ((!) source).replace (" ", "+"); - source = ((!) source).replace ("\t", "+"); + source = layouts[0].replace (" ", "+").replace ("\t", "+"); } } } @@ -340,7 +385,7 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] private void migrate_keyboard_layouts () { if (is_login_user ()) { - lightdm_current = source_settings.get_uint ("current"); + lightdm_current = get_current (); var manager = Act.UserManager.get_default (); @@ -450,17 +495,13 @@ public class Indicator.Keyboard.Service : Object { if (user.is_loaded) { var done = false; - VariantIter outer; - VariantIter inner; - - var sources = user.input_sources; - sources.get ("aa{ss}", out outer); + foreach (var outer in user.input_sources) { + foreach (var inner in (!) outer) { + unowned string key; + unowned string source; - while (outer.next ("a{ss}", out inner)) { - unowned string key; - unowned string source; + ((!) inner).get ("{&s&s}", out key, out source); - while (inner.next ("{&s&s}", out key, out source)) { if (key == "xkb") { done = true; @@ -548,7 +589,7 @@ public class Indicator.Keyboard.Service : Object { private void update_login_layout () { if (is_login_user ()) { unowned List<LightDM.Layout> layouts = LightDM.get_layouts (); - var current = source_settings.get_uint ("current"); + var current = get_current (); if (current < get_sources ().length) { var source = get_sources ()[current]; @@ -594,10 +635,12 @@ public class Indicator.Keyboard.Service : Object { warning ("error: %s", error.message); } - window_sources = new Gee.HashMap<uint, uint> (); + window_sources = new Gee.HashMap<uint, Source> (); + ((!) window_stack).window_destroyed.connect (handle_window_destroyed); ((!) window_stack).focused_window_changed.connect (handle_focused_window_changed); } else { ((!) window_stack).focused_window_changed.disconnect (handle_focused_window_changed); + ((!) window_stack).window_destroyed.disconnect (handle_window_destroyed); window_sources = null; } } @@ -610,22 +653,49 @@ public class Indicator.Keyboard.Service : Object { } [DBus (visible = false)] + private void handle_window_destroyed (uint window_id, string app_id) { + ((!) window_sources).unset (window_id); + } + + [DBus (visible = false)] private void handle_focused_window_changed (uint window_id, string app_id, uint stage) { - var old_current = source_settings.get_uint ("current"); + var sources = get_sources (); + var old_current = get_current (); - ((!) window_sources)[focused_window_id] = old_current; + if (old_current < sources.length) { + ((!) window_sources)[focused_window_id] = sources[old_current]; + } if (!(((!) window_sources).has_key (window_id))) { var default_group = per_window_settings.get_int ("default-group"); - if (default_group >= 0 && default_group != old_current) { - source_settings.set_uint ("current", (uint) default_group); + if (default_group >= 0) { + for (var offset = 0; offset < sources.length; offset++) { + var current = (default_group + offset) % sources.length; + var source = sources[current]; + + if (source.is_xkb || + (source.is_ibus && is_ibus_active ()) || + (source.is_fcitx && is_fcitx_active ())) { + if (current != old_current) { + source_settings.set_uint ("current", current); + } + + break; + } + } } } else { - var current = ((!) window_sources)[window_id]; + var source = ((!) window_sources)[window_id]; + + for (var current = 0; current < sources.length; current++) { + if (sources[current] == source) { + if (current != old_current) { + source_settings.set_uint ("current", current); + } - if (current != old_current) { - source_settings.set_uint ("current", current); + break; + } } } @@ -633,6 +703,44 @@ public class Indicator.Keyboard.Service : Object { } [DBus (visible = false)] + private uint get_current () { + if (is_fcitx_active () && get_fcitx () != null) { + string? engine = ((!) get_fcitx ()).current_im; + + if (engine != null) { + var is_xkb = ((!) engine).has_prefix ("fcitx-keyboard-"); + var type = is_xkb ? "xkb" : "fcitx"; + var name = (!) engine; + + if (is_xkb) { + name = name.substring ("fcitx-keyboard-".length); + var index = name.index_of ("-"); + if (index >= 0) { + name.data[index] = '+'; + } + } + + var i = 0; + + foreach (var pair in source_settings.get_value ("sources")) { + unowned string source_type; + unowned string source_name; + + ((!) pair).get ("(&s&s)", out source_type, out source_name); + + if (source_name == name && source_type == type) { + return i; + } + + i++; + } + } + } + + return source_settings.get_uint ("current"); + } + + [DBus (visible = false)] private Source[] get_sources () { if (sources == null) { var array = source_settings.get_value ("sources"); @@ -743,7 +851,7 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] private void update_active_action () { if (active_action != null) { - ((!) active_action).set_state (source_settings.get_value ("current")); + ((!) active_action).set_state (new Variant.uint32 (get_current ())); update_indicator_action (); } } @@ -751,10 +859,9 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] private Action get_active_action () { if (active_action == null) { - var current = source_settings.get_value ("current"); - active_action = new SimpleAction.stateful ("active", VariantType.UINT32, current); - ((!) active_action).activate.connect ((parameter) => { ((!) active_action).change_state (parameter); }); - ((!) active_action).change_state.connect (handle_changed_active); + var action = new SimpleAction.stateful ("active", VariantType.UINT32, new Variant.uint32 (get_current ())); + action.change_state.connect (handle_changed_active); + active_action = action; } return (!) active_action; @@ -768,13 +875,45 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] private void handle_scroll_wheel (Variant? parameter) { if (parameter != null) { - var sources = source_settings.get_value ("sources"); - var current = source_settings.get_uint ("current"); - var length = (int) sources.n_children (); + var old_current = get_current (); + var sources = get_sources (); + var length = 0; + + foreach (var source in sources) { + if (source.is_xkb || + (source.is_ibus && is_ibus_active ()) || + (source.is_fcitx && is_fcitx_active ())) { + length++; + } + } + + if (length > 1) { + var current = old_current; + var offset = -((!) parameter).get_int32 () % length; + var jump = 1; + + if (offset < 0) { + offset = -offset; + jump = sources.length - jump; + } + + /* + * We need to cycle through offset valid input sources, skipping those that aren't + * valid for this session (i.e. skipping Fcitx ones if IBus is active and vice-versa. + * jump is the direction we need to cycle in, which is 1 if we want to cycle forward + * and -1 (mod sources.length) if we want to cycle backward. + */ + + for (; offset > 0; offset--) { + do { + current = (current + jump) % sources.length; + } while ((sources[current].is_ibus && !is_ibus_active ()) || + (sources[current].is_fcitx && !is_fcitx_active ())); + } - if (length > 0) { - var offset = ((!) parameter).get_int32 () % length; - source_settings.set_uint ("current", (current + (length - offset)) % length); + if (current != old_current) { + source_settings.set_uint ("current", current); + } } } } @@ -788,30 +927,30 @@ public class Indicator.Keyboard.Service : Object { private void handle_scroll_wheel_when_locked (Variant? parameter) { if (parameter != null) { var sources = get_sources (); - var non_ibus_length = 0; + var xkb_length = 0; - /* Figure out how many non-IBus sources we have. */ + /* Figure out how many Xkb sources we have. */ foreach (var source in sources) { - if (!source.is_ibus) { - non_ibus_length++; + if (source.is_xkb) { + xkb_length++; } } - if (non_ibus_length > 1) { + if (xkb_length > 1) { var active_action = get_active_action (); var active = active_action.get_state ().get_uint32 (); - var offset = -((!) parameter).get_int32 () % non_ibus_length; + var offset = -((!) parameter).get_int32 () % xkb_length; - /* Make offset positive modulo non_ibus_length. */ + /* Make offset positive modulo xkb_length. */ if (offset < 0) { - offset += non_ibus_length; + offset += xkb_length; } - /* We need to cycle through non-IBus sources only. */ + /* We need to cycle through Xkb sources only. */ while (offset > 0) { do { active = (active + 1) % sources.length; - } while (sources[active].is_ibus); + } while (!sources[active].is_xkb); offset--; } @@ -881,13 +1020,20 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] public IndicatorMenu get_desktop_menu () { if (desktop_menu == null) { - var options = IndicatorMenu.Options.DCONF - | IndicatorMenu.Options.IBUS - | IndicatorMenu.Options.SETTINGS; + var options = IndicatorMenu.Options.DCONF; + + if (!is_fcitx_active ()) { + options |= IndicatorMenu.Options.XKB | IndicatorMenu.Options.SETTINGS; + + if (is_ibus_active ()) { + options |= IndicatorMenu.Options.IBUS; + } + } - desktop_menu = new IndicatorMenu (get_action_group (), options); - ((!) desktop_menu).set_sources (get_sources ()); - ((!) desktop_menu).activate.connect ((property, state) => { + var menu = new IndicatorMenu (get_action_group (), options); + + menu.set_sources (get_sources ()); + menu.activate.connect ((property, state) => { var panel = get_ibus_panel (); if (panel != null) { @@ -898,6 +1044,8 @@ public class Indicator.Keyboard.Service : Object { } } }); + + desktop_menu = menu; } return (!) desktop_menu; @@ -906,10 +1054,12 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] public IndicatorMenu get_desktop_greeter_menu () { if (desktop_greeter_menu == null) { - var options = IndicatorMenu.Options.DCONF; + var options = IndicatorMenu.Options.DCONF | + IndicatorMenu.Options.XKB; - desktop_greeter_menu = new IndicatorMenu (get_action_group (), options); - ((!) desktop_greeter_menu).set_sources (get_sources ()); + var menu = new IndicatorMenu (get_action_group (), options); + menu.set_sources (get_sources ()); + desktop_greeter_menu = menu; } return (!) desktop_greeter_menu; @@ -918,10 +1068,11 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] public IndicatorMenu get_desktop_lockscreen_menu () { if (desktop_lockscreen_menu == null) { - var options = IndicatorMenu.Options.NONE; + var options = IndicatorMenu.Options.XKB; - desktop_lockscreen_menu = new IndicatorMenu (get_action_group (), options); - ((!) desktop_lockscreen_menu).set_sources (get_sources ()); + var menu = new IndicatorMenu (get_action_group (), options); + menu.set_sources (get_sources ()); + desktop_lockscreen_menu = menu; } return (!) desktop_lockscreen_menu; @@ -965,7 +1116,7 @@ public class Indicator.Keyboard.Service : Object { string? variant = null; var sources = get_sources (); - var current = source_settings.get_uint ("current"); + var current = get_current (); if (current < sources.length) { layout = sources[current].layout; @@ -1004,8 +1155,9 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] private void handle_unity_greeter_name_appeared (DBusConnection connection, string name, string name_owner) { try { - unity_greeter = Bus.get_proxy_sync (BusType.SESSION, name, "/list"); - ((!) unity_greeter).entry_selected.connect (handle_entry_selected); + var greeter = Bus.get_proxy_sync<UnityGreeter> (BusType.SESSION, name, "/list"); + greeter.entry_selected.connect (handle_entry_selected); + unity_greeter = greeter; } catch (IOError error) { warning ("error: %s", error.message); } @@ -1033,16 +1185,17 @@ public class Indicator.Keyboard.Service : Object { [DBus (visible = false)] private void handle_unity_name_appeared (DBusConnection connection, string name, string name_owner) { try { - unity_session = Bus.get_proxy_sync (BusType.SESSION, name, "/com/canonical/Unity/Session"); - ((!) unity_session).locked.connect (() => { + var session = Bus.get_proxy_sync<UnitySession> (BusType.SESSION, name, "/com/canonical/Unity/Session"); + + session.locked.connect (() => { var sources = get_sources (); if (sources.length > 0) { - var current = source_settings.get_uint ("current"); + var current = get_current (); - if (current < sources.length && sources[current].is_ibus) { + if (current < sources.length && !sources[current].is_xkb) { for (var i = 0; i < sources.length; i++) { - if (!sources[i].is_ibus) { + if (sources[i].is_xkb) { get_active_action ().change_state (new Variant.uint32 (i)); break; } @@ -1050,9 +1203,11 @@ public class Indicator.Keyboard.Service : Object { } } }); - ((!) unity_session).unlocked.connect (() => { - get_active_action ().change_state (source_settings.get_value ("current")); + session.unlocked.connect (() => { + get_active_action ().change_state (new Variant.uint32 (get_current ())); }); + + unity_session = session; } catch (IOError error) { warning ("error: %s", error.message); } diff --git a/lib/source.vala b/lib/source.vala index 5fe7157d..b7d7a971 100644 --- a/lib/source.vala +++ b/lib/source.vala @@ -19,10 +19,12 @@ public class Indicator.Keyboard.Source : Object { private static Gnome.XkbInfo? xkb_info; - private static IBus.Bus? bus; + private static IBus.Bus? ibus_bus; + private static Fcitx.InputMethod? fcitx_proxy; private string? xkb; private string? ibus; + private string? fcitx; private string? _name; private string? _short_name; @@ -77,6 +79,10 @@ public class Indicator.Keyboard.Source : Object { get { return ibus != null; } } + public bool is_fcitx { + get { return fcitx != null; } + } + public Source (Variant variant, bool use_gtk = false) { Object (use_gtk: use_gtk); @@ -90,19 +96,22 @@ public class Indicator.Keyboard.Source : Object { xkb = name; } else if (type == "ibus") { ibus = name; + } else if (type == "fcitx") { + fcitx = name; } } else if (variant.is_of_type (new VariantType ("a{ss}"))) { - VariantIter iter; - unowned string key; - unowned string value; + foreach (var pair in variant) { + unowned string key; + unowned string value; - variant.get ("a{ss}", out iter); + ((!) pair).get ("{&s&s}", out key, out value); - while (iter.next ("{&s&s}", out key, out value)) { if (key == "xkb") { xkb = value; } else if (key == "ibus") { ibus = value; + } else if (key == "fcitx") { + fcitx = value; } } } @@ -116,13 +125,21 @@ public class Indicator.Keyboard.Source : Object { return (!) xkb_info; } - private static IBus.Bus get_bus () { - if (bus == null) { + private static IBus.Bus get_ibus_bus () { + if (ibus_bus == null) { IBus.init (); - bus = new IBus.Bus (); + ibus_bus = new IBus.Bus (); } - return (!) bus; + return (!) ibus_bus; + } + + private static Fcitx.InputMethod get_fcitx_proxy () throws Error { + if (fcitx_proxy == null) { + fcitx_proxy = new Fcitx.InputMethod (BusType.SESSION, DBusProxyFlags.NONE, 0); + } + + return (!) fcitx_proxy; } private IBus.EngineDesc? get_engine () { @@ -132,7 +149,7 @@ public class Indicator.Keyboard.Source : Object { var names = new string[2]; names[0] = (!) ibus; - var engines = get_bus ().get_engines_by_names (names); + var engines = get_ibus_bus ().get_engines_by_names (names); if (engines.length > 0) { engine = engines[0]; @@ -145,31 +162,7 @@ public class Indicator.Keyboard.Source : Object { protected virtual string? _get_name () { string? name = null; - var engine = get_engine (); - - if (engine != null) { - string? language = ((!) engine).get_language (); - string? display_name = ((!) engine).get_longname (); - var has_language = language != null && ((!) language).get_char () != '\0'; - var has_display_name = display_name != null && ((!) display_name).get_char () != '\0'; - - if (has_language) { - language = Xkl.get_language_name ((!) language); - has_language = language != null && ((!) language).get_char () != '\0'; - } - - if (has_language && has_display_name) { - name = @"$((!) language) ($((!) display_name))"; - } else if (has_language) { - name = language; - } else if (has_display_name) { - name = display_name; - } - } - - var has_name = name != null && ((!) name).get_char () != '\0'; - - if (!has_name && xkb != null) { + if (xkb != null) { string? display_name = null; string? layout = null; @@ -194,14 +187,53 @@ public class Indicator.Keyboard.Source : Object { name = country; } } - } - if (name == null || ((!) name).get_char () == '\0') { - if (ibus != null) { - name = ibus; - } else if (xkb != null) { + if (name == null || ((!) name).get_char () == '\0') { name = xkb; } + } else if (ibus != null) { + var engine = get_engine (); + + if (engine != null) { + string? language = ((!) engine).get_language (); + string? display_name = ((!) engine).get_longname (); + var has_language = language != null && ((!) language).get_char () != '\0'; + var has_display_name = display_name != null && ((!) display_name).get_char () != '\0'; + + if (has_language) { + language = Xkl.get_language_name ((!) language); + has_language = language != null && ((!) language).get_char () != '\0'; + } + + if (has_language && has_display_name) { + name = @"$((!) language) ($((!) display_name))"; + } else if (has_language) { + name = language; + } else if (has_display_name) { + name = display_name; + } + } + + if (name == null || ((!) name).get_char () == '\0') { + name = ibus; + } + } else if (fcitx != null) { + try { + var input_methods = get_fcitx_proxy ().get_imlist_nofree (); + + for (var i = 0; i < input_methods.length; i++) { + if (input_methods.get (i).unique_name == (!) fcitx) { + name = input_methods.get (i).name; + break; + } + } + } catch (Error error) { + warning ("error: %s", error.message); + } + + if (name == null || ((!) name).get_char () == '\0') { + name = fcitx; + } } return name; @@ -212,23 +244,36 @@ public class Indicator.Keyboard.Source : Object { if (xkb != null) { get_xkb_info ().get_layout_info ((!) xkb, null, out short_name, null, null); - } - - var has_short_name = short_name != null && ((!) short_name).get_char () != '\0'; - if (!has_short_name) { + if (short_name == null || ((!) short_name).get_char () == '\0') { + short_name = xkb; + } + } else if (ibus != null) { var engine = get_engine (); if (engine != null) { short_name = ((!) engine).get_name (); } - } - if (short_name == null || ((!) short_name).get_char () == '\0') { - if (ibus != null) { + if (short_name == null || ((!) short_name).get_char () == '\0') { short_name = ibus; - } else if (xkb != null) { - short_name = xkb; + } + } else if (fcitx != null) { + try { + var input_methods = get_fcitx_proxy ().get_imlist_nofree (); + + for (var i = 0; i < input_methods.length; i++) { + if (input_methods.get (i).unique_name == (!) fcitx) { + short_name = input_methods.get (i).langcode; + break; + } + } + } catch (Error error) { + warning ("error: %s", error.message); + } + + if (short_name == null || ((!) short_name).get_char () == '\0') { + short_name = fcitx; } } @@ -290,12 +335,14 @@ public class Indicator.Keyboard.Source : Object { Gdk.Screen? screen = Gdk.Screen.get_default (); if (screen != null) { - context = new Gtk.StyleContext (); - ((!) context).set_screen ((!) screen); + var style_context = new Gtk.StyleContext (); + style_context.set_screen ((!) screen); var path = new Gtk.WidgetPath (); path.append_type (typeof (Gtk.MenuItem)); - ((!) context).set_path (path); + style_context.set_path (path); + + context = style_context; } } |