private void RegisterKeyboards(IDictionary <string, uint> keyboards, UnityKeyboardRetrievingHelper.IbusKeyboardEntry _)
        {
            if (keyboards.Count <= 0)
            {
                return;
            }

            Dictionary <string, IbusKeyboardDescription> curKeyboards = KeyboardController.Instance.Keyboards.OfType <IbusKeyboardDescription>().ToDictionary(kd => kd.Id);
            var missingLayouts = new List <string>(keyboards.Keys);

            foreach (var ibusKeyboard in GetAllIBusKeyboards())
            {
                if (keyboards.ContainsKey(ibusKeyboard.LongName))
                {
                    missingLayouts.Remove(ibusKeyboard.LongName);
                    string id = string.Format("{0}_{1}", ibusKeyboard.Language, ibusKeyboard.LongName);
                    IbusKeyboardDescription keyboard;
                    if (curKeyboards.TryGetValue(id, out keyboard))
                    {
                        if (!keyboard.IsAvailable)
                        {
                            keyboard.SetIsAvailable(true);
                            keyboard.IBusKeyboardEngine = ibusKeyboard;
                        }
                        curKeyboards.Remove(id);
                    }
                    else
                    {
                        keyboard = new IbusKeyboardDescription(id, ibusKeyboard, SwitchingAdaptor);
                        KeyboardController.Instance.Keyboards.Add(keyboard);
                    }
                    keyboard.SystemIndex = keyboards[ibusKeyboard.LongName];
                }
            }

            foreach (IbusKeyboardDescription existingKeyboard in curKeyboards.Values)
            {
                existingKeyboard.SetIsAvailable(false);
            }

            foreach (var layout in missingLayouts)
            {
                Console.WriteLine("{0}: Didn't find {1}", GetType().Name, layout);
            }
        }
        private void RegisterKeyboards(IDictionary <string, uint> keyboards,
                                       UnityKeyboardRetrievingHelper.IbusKeyboardEntry _)
        {
            if (keyboards.Count <= 0)
            {
                return;
            }

            var configRegistry = XklConfigRegistry.Create(XklEngine);
            var layouts        = configRegistry.Layouts;
            Dictionary <string, XkbKeyboardDescription> curKeyboards = KeyboardController.Instance.Keyboards.OfType <XkbKeyboardDescription>().ToDictionary(kd => kd.Id);

            foreach (var kvp in layouts)
            {
                foreach (var layout in kvp.Value)
                {
                    uint index;
                    // Custom keyboards may omit defining a country code.  Try to survive such cases.
                    string codeToMatch;
                    if (layout.CountryCode == null)
                    {
                        codeToMatch = layout.LanguageCode.ToLowerInvariant();
                    }
                    else
                    {
                        codeToMatch = layout.CountryCode.ToLowerInvariant();
                    }
                    if ((keyboards.TryGetValue(layout.LayoutId, out index) && (layout.LayoutId == codeToMatch)) ||
                        keyboards.TryGetValue(string.Format("{0}+{1}", codeToMatch, layout.LayoutId), out index))
                    {
                        AddKeyboardForLayout(curKeyboards, layout, index, SwitchingAdaptor);
                    }
                }
            }

            foreach (XkbKeyboardDescription existingKeyboard in curKeyboards.Values)
            {
                existingKeyboard.SetIsAvailable(false);
            }
        }
Exemplo n.º 3
0
        private void RegisterKeyboards(IDictionary <string, uint> installedKeyboards,
                                       UnityKeyboardRetrievingHelper.IbusKeyboardEntry firstKeyboard)
        {
            if (installedKeyboards.Count <= 0)
            {
                return;
            }

            // The list of keyboards we knew before registering the new keyboards
            var priorKeyboardsList =
                KeyboardController.Instance.Keyboards.OfType <IbusKeyboardDescription>().ToDictionary(kd => kd.Id);
            // The list of keyboards that are installed on this system but we haven't encountered yet
            var missingLayouts = new List <string>(installedKeyboards.Keys);

            // The list of keyboards we registered
            var addedKeyboards = new List <string>();

            foreach (var ibusKeyboard in GetAllIBusKeyboards())
            {
                Type   type;
                string layout;

                // ENHANCE: Keyman keyboards contain the path, possibly with username. We should
                // make that more fault tolerant so that we find that keyboard in curKeyboards
                // even when running under a different user or installed globally/locally.
                var id = $"{ibusKeyboard.Language}_{ibusKeyboard.LongName}";

                if (ibusKeyboard.LongName.StartsWith("xkb"))
                {
                    type   = typeof(IbusXkbKeyboardDescription);
                    layout = string.IsNullOrEmpty(ibusKeyboard.LayoutVariant)
                                                        ? ibusKeyboard.Layout
                                                        : $"{ibusKeyboard.Layout}+{ibusKeyboard.LayoutVariant}";
                }
                else
                {
                    type   = typeof(IbusKeyboardDescription);
                    layout = ibusKeyboard.LongName;
                }

                // Don't add a keyboard that is not installed locally
                if (!IsKeyboardAvailable(installedKeyboards, layout))
                {
                    continue;
                }

                // Don't add same keyboard twice
                if (addedKeyboards.Contains(layout))
                {
                    continue;
                }

                // we found this keyboard, so remove it from list
                missingLayouts.Remove(layout);

                IbusKeyboardDescription keyboard;
                if (priorKeyboardsList.TryGetValue(id, out keyboard))
                {
                    if (!keyboard.IsAvailable)
                    {
                        keyboard.SetIsAvailable(true);
                        keyboard.IBusKeyboardEngine = ibusKeyboard;
                    }

                    // Remove this keyboard from list so that we don't set it inactive
                    priorKeyboardsList.Remove(id);
                }
                else
                {
                    // Add keyboard to keyboards list
                    keyboard = Activator.CreateInstance(type, id, ibusKeyboard,
                                                        SwitchingAdaptor) as IbusKeyboardDescription;
                    KeyboardController.Instance.Keyboards.Add(keyboard);
                    addedKeyboards.Add(layout);
                }

                keyboard.SystemIndex = installedKeyboards[layout];

                if (firstKeyboard.Layout == layout)
                {
                    ((GnomeShellIbusKeyboardSwitchingAdaptor)SwitchingAdaptor).SetDefaultKeyboard(
                        keyboard);
                }
            }

            // All keyboards that we knew before (e.g. from writing systems) and didn't encounter
            // are not available on this system
            foreach (var existingKeyboard in priorKeyboardsList.Values)
            {
                existingKeyboard.SetIsAvailable(false);
            }

            // output a warning for all keyboards that were in installedKeyboards but that we couldn't
            // process because they didn't have the properties we expected.
            foreach (var layout in missingLayouts)
            {
                Console.WriteLine($"{GetType().Name}: Didn't find {layout}");
            }
        }