Implement input source switching
Instead of calling out to gnome-settings-daemon we'll just implement the switching logic ourselves and use mutter APIs that allow this functionality to work both in X sessions and when we're a Wayland compositor. Switching IBus engines is done transparently as well just like g-s-d used to do. https://bugzilla.gnome.org/show_bug.cgi?id=736435
This commit is contained in:
@ -1,11 +1,16 @@
|
||||
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
||||
|
||||
const Gio = imports.gi.Gio;
|
||||
const GLib = imports.gi.GLib;
|
||||
const GnomeDesktop = imports.gi.GnomeDesktop;
|
||||
const Lang = imports.lang;
|
||||
const Meta = imports.gi.Meta;
|
||||
|
||||
const Main = imports.ui.main;
|
||||
|
||||
const DEFAULT_LOCALE = 'en_US';
|
||||
const DEFAULT_LAYOUT = 'us';
|
||||
const DEFAULT_VARIANT = '';
|
||||
|
||||
let _xkbInfo = null;
|
||||
|
||||
function getXkbInfo() {
|
||||
@ -36,36 +41,113 @@ function holdKeyboard() {
|
||||
const KeyboardManager = new Lang.Class({
|
||||
Name: 'KeyboardManager',
|
||||
|
||||
// This is the longest we'll keep the keyboard frozen until an input
|
||||
// source is active.
|
||||
_MAX_INPUT_SOURCE_ACTIVATION_TIME: 4000, // ms
|
||||
|
||||
_BUS_NAME: 'org.gnome.SettingsDaemon.Keyboard',
|
||||
_OBJECT_PATH: '/org/gnome/SettingsDaemon/Keyboard',
|
||||
|
||||
_INTERFACE: '\
|
||||
<node> \
|
||||
<interface name="org.gnome.SettingsDaemon.Keyboard"> \
|
||||
<method name="SetInputSource"> \
|
||||
<arg type="u" direction="in" /> \
|
||||
</method> \
|
||||
</interface> \
|
||||
</node>',
|
||||
// The XKB protocol doesn't allow for more that 4 layouts in a
|
||||
// keymap. Wayland doesn't impose this limit and libxkbcommon can
|
||||
// handle up to 32 layouts but since we need to support X clients
|
||||
// even as a Wayland compositor, we can't bump this.
|
||||
MAX_LAYOUTS_PER_GROUP: 4,
|
||||
|
||||
_init: function() {
|
||||
let Proxy = Gio.DBusProxy.makeProxyWrapper(this._INTERFACE);
|
||||
this._proxy = new Proxy(Gio.DBus.session,
|
||||
this._BUS_NAME,
|
||||
this._OBJECT_PATH,
|
||||
function(proxy, error) {
|
||||
if (error)
|
||||
log(error.message);
|
||||
});
|
||||
this._proxy.g_default_timeout = this._MAX_INPUT_SOURCE_ACTIVATION_TIME;
|
||||
this._xkbInfo = getXkbInfo();
|
||||
this._current = null;
|
||||
this._localeLayoutInfo = this._getLocaleLayout();
|
||||
this._layoutInfos = {};
|
||||
},
|
||||
|
||||
SetInputSource: function(is) {
|
||||
holdKeyboard();
|
||||
this._proxy.SetInputSourceRemote(is.index, releaseKeyboard);
|
||||
_applyLayoutGroup: function(group) {
|
||||
let options = this._buildOptionsString();
|
||||
let [layouts, variants] = this._buildGroupStrings(group);
|
||||
Meta.get_backend().set_keymap(layouts, variants, options);
|
||||
},
|
||||
|
||||
_applyLayoutGroupIndex: function(idx) {
|
||||
Meta.get_backend().lock_layout_group(idx);
|
||||
},
|
||||
|
||||
apply: function(id) {
|
||||
let info = this._layoutInfos[id];
|
||||
if (!info)
|
||||
return;
|
||||
|
||||
if (this._current && this._current.group == info.group) {
|
||||
if (this._current.groupIndex != info.groupIndex)
|
||||
this._applyLayoutGroupIndex(info.groupIndex);
|
||||
} else {
|
||||
this._applyLayoutGroup(info.group);
|
||||
this._applyLayoutGroupIndex(info.groupIndex);
|
||||
}
|
||||
|
||||
this._current = info;
|
||||
},
|
||||
|
||||
reapply: function() {
|
||||
if (!this._current)
|
||||
return;
|
||||
|
||||
this._applyLayoutGroup(this._current.group);
|
||||
this._applyLayoutGroupIndex(this._current.groupIndex);
|
||||
},
|
||||
|
||||
setUserLayouts: function(ids) {
|
||||
this._current = null;
|
||||
this._layoutInfos = {};
|
||||
|
||||
for (let i = 0; i < ids.length; ++i) {
|
||||
let [found, , , _layout, _variant] = this._xkbInfo.get_layout_info(ids[i]);
|
||||
if (found)
|
||||
this._layoutInfos[ids[i]] = { id: ids[i], layout: _layout, variant: _variant };
|
||||
}
|
||||
|
||||
let i = 0;
|
||||
let group = [];
|
||||
for (let id in this._layoutInfos) {
|
||||
// We need to leave one slot on each group free so that we
|
||||
// can add a layout containing the symbols for the
|
||||
// language used in UI strings to ensure that toolkits can
|
||||
// handle mnemonics like Alt+Ф even if the user is
|
||||
// actually typing in a different layout.
|
||||
let groupIndex = i % (this.MAX_LAYOUTS_PER_GROUP - 1);
|
||||
if (groupIndex == 0)
|
||||
group = [];
|
||||
|
||||
let info = this._layoutInfos[id];
|
||||
group[groupIndex] = info;
|
||||
info.group = group;
|
||||
info.groupIndex = groupIndex;
|
||||
|
||||
i += 1;
|
||||
}
|
||||
},
|
||||
|
||||
_getLocaleLayout: function() {
|
||||
let locale = GLib.get_language_names()[0];
|
||||
if (locale.indexOf('_') == -1)
|
||||
locale = DEFAULT_LOCALE;
|
||||
|
||||
let [found, , id] = GnomeDesktop.get_input_source_from_locale(locale);
|
||||
if (!found)
|
||||
[, , id] = GnomeDesktop.get_input_source_from_locale(DEFAULT_LOCALE);
|
||||
|
||||
let [found, , , _layout, _variant] = this._xkbInfo.get_layout_info(id);
|
||||
if (found)
|
||||
return { layout: _layout, variant: _variant };
|
||||
else
|
||||
return { layout: DEFAULT_LAYOUT, variant: DEFAULT_VARIANT };
|
||||
},
|
||||
|
||||
_buildGroupStrings: function(_group) {
|
||||
let group = _group.concat(this._localeLayoutInfo);
|
||||
let layouts = group.map(function(g) { return g.layout; }).join(',');
|
||||
let variants = group.map(function(g) { return g.variant; }).join(',');
|
||||
return [layouts, variants];
|
||||
},
|
||||
|
||||
setKeyboardOptions: function(options) {
|
||||
this._xkbOptions = options;
|
||||
},
|
||||
|
||||
_buildOptionsString: function() {
|
||||
let options = this._xkbOptions.join(',');
|
||||
return options;
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user