status/keyboard: Factor out a KeyboardManager class

This code will grow in a forthcoming patch so let's move it out.

https://bugzilla.gnome.org/show_bug.cgi?id=736435
This commit is contained in:
Rui Matos 2014-02-14 16:11:28 +01:00
parent 58aabfcf5b
commit 8e560f98d1
3 changed files with 79 additions and 43 deletions

View File

@ -18,6 +18,7 @@
<file>misc/history.js</file> <file>misc/history.js</file>
<file>misc/ibusManager.js</file> <file>misc/ibusManager.js</file>
<file>misc/jsParse.js</file> <file>misc/jsParse.js</file>
<file>misc/keyboardManager.js</file>
<file>misc/loginManager.js</file> <file>misc/loginManager.js</file>
<file>misc/modemManager.js</file> <file>misc/modemManager.js</file>
<file>misc/objectManager.js</file> <file>misc/objectManager.js</file>

View File

@ -0,0 +1,71 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Gio = imports.gi.Gio;
const GnomeDesktop = imports.gi.GnomeDesktop;
const Lang = imports.lang;
const Main = imports.ui.main;
let _xkbInfo = null;
function getXkbInfo() {
if (_xkbInfo == null)
_xkbInfo = new GnomeDesktop.XkbInfo();
return _xkbInfo;
}
let _keyboardManager = null;
function getKeyboardManager() {
if (_keyboardManager == null)
_keyboardManager = new KeyboardManager();
return _keyboardManager;
}
function releaseKeyboard() {
if (Main.modalCount > 0)
global.display.unfreeze_keyboard(global.get_current_time());
else
global.display.ungrab_keyboard(global.get_current_time());
}
function holdKeyboard() {
global.freeze_keyboard(global.get_current_time());
}
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>',
_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;
},
SetInputSource: function(is) {
holdKeyboard();
this._proxy.SetInputSourceRemote(is.index, releaseKeyboard);
}
});

View File

@ -2,8 +2,6 @@
const Clutter = imports.gi.Clutter; const Clutter = imports.gi.Clutter;
const Gio = imports.gi.Gio; const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GnomeDesktop = imports.gi.GnomeDesktop;
const Lang = imports.lang; const Lang = imports.lang;
const Meta = imports.gi.Meta; const Meta = imports.gi.Meta;
const Shell = imports.gi.Shell; const Shell = imports.gi.Shell;
@ -11,15 +9,15 @@ const Signals = imports.signals;
const St = imports.gi.St; const St = imports.gi.St;
const Gettext = imports.gettext; const Gettext = imports.gettext;
const IBus = imports.misc.ibusManager.IBus;
const IBusManager = imports.misc.ibusManager;
const KeyboardManager = imports.misc.keyboardManager;
const Main = imports.ui.main; const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu; const PopupMenu = imports.ui.popupMenu;
const PanelMenu = imports.ui.panelMenu; const PanelMenu = imports.ui.panelMenu;
const SwitcherPopup = imports.ui.switcherPopup; const SwitcherPopup = imports.ui.switcherPopup;
const Util = imports.misc.util; const Util = imports.misc.util;
const IBus = imports.misc.ibusManager.IBus;
const IBusManager = imports.misc.ibusManager;
const DESKTOP_INPUT_SOURCES_SCHEMA = 'org.gnome.desktop.input-sources'; const DESKTOP_INPUT_SOURCES_SCHEMA = 'org.gnome.desktop.input-sources';
const KEY_CURRENT_INPUT_SOURCE = 'current'; const KEY_CURRENT_INPUT_SOURCE = 'current';
const KEY_INPUT_SOURCES = 'sources'; const KEY_INPUT_SOURCES = 'sources';
@ -27,34 +25,6 @@ const KEY_INPUT_SOURCES = 'sources';
const INPUT_SOURCE_TYPE_XKB = 'xkb'; const INPUT_SOURCE_TYPE_XKB = 'xkb';
const INPUT_SOURCE_TYPE_IBUS = 'ibus'; const INPUT_SOURCE_TYPE_IBUS = 'ibus';
// This is the longest we'll keep the keyboard frozen until an input
// source is active.
const MAX_INPUT_SOURCE_ACTIVATION_TIME = 4000; // ms
const BUS_NAME = 'org.gnome.SettingsDaemon.Keyboard';
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Keyboard';
const KeyboardManagerInterface = '<node> \
<interface name="org.gnome.SettingsDaemon.Keyboard"> \
<method name="SetInputSource"> \
<arg type="u" direction="in" /> \
</method> \
</interface> \
</node>';
const KeyboardManagerProxy = Gio.DBusProxy.makeProxyWrapper(KeyboardManagerInterface);
function releaseKeyboard() {
if (Main.modalCount > 0)
global.display.unfreeze_keyboard(global.get_current_time());
else
global.display.ungrab_keyboard(global.get_current_time());
}
function holdKeyboard() {
global.freeze_keyboard(global.get_current_time());
}
const LayoutMenuItem = new Lang.Class({ const LayoutMenuItem = new Lang.Class({
Name: 'LayoutMenuItem', Name: 'LayoutMenuItem',
Extends: PopupMenu.PopupBaseMenuItem, Extends: PopupMenu.PopupBaseMenuItem,
@ -225,7 +195,7 @@ const InputSourceIndicator = new Lang.Class({
this._settings.connect('changed::' + KEY_CURRENT_INPUT_SOURCE, Lang.bind(this, this._currentInputSourceChanged)); this._settings.connect('changed::' + KEY_CURRENT_INPUT_SOURCE, Lang.bind(this, this._currentInputSourceChanged));
this._settings.connect('changed::' + KEY_INPUT_SOURCES, Lang.bind(this, this._inputSourcesChanged)); this._settings.connect('changed::' + KEY_INPUT_SOURCES, Lang.bind(this, this._inputSourcesChanged));
this._xkbInfo = new GnomeDesktop.XkbInfo(); this._xkbInfo = KeyboardManager.getXkbInfo();
this._propSeparator = new PopupMenu.PopupSeparatorMenuItem(); this._propSeparator = new PopupMenu.PopupSeparatorMenuItem();
this.menu.addMenuItem(this._propSeparator); this.menu.addMenuItem(this._propSeparator);
@ -240,12 +210,7 @@ const InputSourceIndicator = new Lang.Class({
this._ibusManager.connect('property-updated', Lang.bind(this, this._ibusPropertyUpdated)); this._ibusManager.connect('property-updated', Lang.bind(this, this._ibusPropertyUpdated));
this._inputSourcesChanged(); this._inputSourcesChanged();
this._keyboardManager = new KeyboardManagerProxy(Gio.DBus.session, BUS_NAME, OBJECT_PATH, this._keyboardManager = KeyboardManager.getKeyboardManager();
function(proxy, error) {
if (error)
log(error.message);
});
this._keyboardManager.g_default_timeout = MAX_INPUT_SOURCE_ACTIVATION_TIME;
global.display.connect('modifiers-accelerator-activated', Lang.bind(this, this._modifiersSwitcher)); global.display.connect('modifiers-accelerator-activated', Lang.bind(this, this._modifiersSwitcher));
@ -283,7 +248,7 @@ const InputSourceIndicator = new Lang.Class({
_modifiersSwitcher: function() { _modifiersSwitcher: function() {
let sourceIndexes = Object.keys(this._inputSources); let sourceIndexes = Object.keys(this._inputSources);
if (sourceIndexes.length == 0) { if (sourceIndexes.length == 0) {
releaseKeyboard(); KeyboardManager.releaseKeyboard();
return true; return true;
} }
@ -406,8 +371,7 @@ const InputSourceIndicator = new Lang.Class({
let is = new InputSource(type, id, displayName, shortName, i); let is = new InputSource(type, id, displayName, shortName, i);
is.connect('activate', Lang.bind(this, function() { is.connect('activate', Lang.bind(this, function() {
holdKeyboard(); this._keyboardManager.SetInputSource(is);
this._keyboardManager.SetInputSourceRemote(is.index, releaseKeyboard);
})); }));
if (!(is.shortName in inputSourcesByShortName)) if (!(is.shortName in inputSourcesByShortName))