2014-02-14 07:35:05 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2014-05-29 09:27:42 -04:00
|
|
|
const Gio = imports.gi.Gio;
|
2014-11-10 05:09:08 -05:00
|
|
|
const GLib = imports.gi.GLib;
|
2014-02-14 07:35:05 -05:00
|
|
|
const Lang = imports.lang;
|
2014-11-10 05:09:08 -05:00
|
|
|
const Mainloop = imports.mainloop;
|
2014-02-14 07:35:05 -05:00
|
|
|
const Signals = imports.signals;
|
|
|
|
|
2017-02-09 21:11:28 -05:00
|
|
|
let IBusCandidatePopup;
|
2014-02-14 07:35:05 -05:00
|
|
|
try {
|
|
|
|
var IBus = imports.gi.IBus;
|
2014-11-27 03:23:01 -05:00
|
|
|
_checkIBusVersion(1, 5, 2);
|
2017-02-09 21:11:28 -05:00
|
|
|
IBusCandidatePopup = imports.ui.ibusCandidatePopup;
|
2014-02-14 07:35:05 -05:00
|
|
|
} catch (e) {
|
|
|
|
var IBus = null;
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ibusManager = null;
|
|
|
|
|
2014-11-27 03:23:01 -05:00
|
|
|
function _checkIBusVersion(requiredMajor, requiredMinor, requiredMicro) {
|
2014-11-10 05:09:08 -05:00
|
|
|
if ((IBus.MAJOR_VERSION > requiredMajor) ||
|
|
|
|
(IBus.MAJOR_VERSION == requiredMajor && IBus.MINOR_VERSION > requiredMinor) ||
|
|
|
|
(IBus.MAJOR_VERSION == requiredMajor && IBus.MINOR_VERSION == requiredMinor &&
|
|
|
|
IBus.MICRO_VERSION >= requiredMicro))
|
|
|
|
return;
|
|
|
|
|
|
|
|
throw "Found IBus version %d.%d.%d but required is %d.%d.%d".
|
|
|
|
format(IBus.MAJOR_VERSION, IBus.MINOR_VERSION, IBus.MINOR_VERSION,
|
|
|
|
requiredMajor, requiredMinor, requiredMicro);
|
|
|
|
}
|
|
|
|
|
2014-02-14 07:35:05 -05:00
|
|
|
function getIBusManager() {
|
|
|
|
if (_ibusManager == null)
|
|
|
|
_ibusManager = new IBusManager();
|
|
|
|
return _ibusManager;
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var IBusManager = new Lang.Class({
|
2014-02-14 07:35:05 -05:00
|
|
|
Name: 'IBusManager',
|
|
|
|
|
2014-06-05 12:47:48 -04:00
|
|
|
// This is the longest we'll keep the keyboard frozen until an input
|
|
|
|
// source is active.
|
|
|
|
_MAX_INPUT_SOURCE_ACTIVATION_TIME: 4000, // ms
|
2014-11-10 05:09:08 -05:00
|
|
|
_PRELOAD_ENGINES_DELAY_TIME: 30, // sec
|
2014-06-05 12:47:48 -04:00
|
|
|
|
2014-02-14 07:35:05 -05:00
|
|
|
_init: function() {
|
|
|
|
if (!IBus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IBus.init();
|
|
|
|
|
|
|
|
this._candidatePopup = new IBusCandidatePopup.CandidatePopup();
|
|
|
|
|
|
|
|
this._panelService = null;
|
|
|
|
this._engines = {};
|
|
|
|
this._ready = false;
|
|
|
|
this._registerPropertiesId = 0;
|
|
|
|
this._currentEngineName = null;
|
2014-11-10 05:09:08 -05:00
|
|
|
this._preloadEnginesId = 0;
|
2014-02-14 07:35:05 -05:00
|
|
|
|
|
|
|
this._ibus = IBus.Bus.new_async();
|
|
|
|
this._ibus.connect('connected', Lang.bind(this, this._onConnected));
|
|
|
|
this._ibus.connect('disconnected', Lang.bind(this, this._clear));
|
|
|
|
// Need to set this to get 'global-engine-changed' emitions
|
|
|
|
this._ibus.set_watch_ibus_signal(true);
|
|
|
|
this._ibus.connect('global-engine-changed', Lang.bind(this, this._engineChanged));
|
2014-05-29 09:27:42 -04:00
|
|
|
|
|
|
|
this._spawn();
|
|
|
|
},
|
|
|
|
|
|
|
|
_spawn: function() {
|
|
|
|
try {
|
|
|
|
Gio.Subprocess.new(['ibus-daemon', '--xim', '--panel', 'disable'],
|
|
|
|
Gio.SubprocessFlags.NONE);
|
|
|
|
} catch(e) {
|
|
|
|
log('Failed to launch ibus-daemon: ' + e.message);
|
|
|
|
}
|
2014-02-14 07:35:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_clear: function() {
|
|
|
|
if (this._panelService)
|
|
|
|
this._panelService.destroy();
|
|
|
|
|
|
|
|
this._panelService = null;
|
|
|
|
this._candidatePopup.setPanelService(null);
|
|
|
|
this._engines = {};
|
|
|
|
this._ready = false;
|
|
|
|
this._registerPropertiesId = 0;
|
|
|
|
this._currentEngineName = null;
|
|
|
|
|
|
|
|
this.emit('ready', false);
|
2014-05-29 09:27:42 -04:00
|
|
|
|
|
|
|
this._spawn();
|
2014-02-14 07:35:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_onConnected: function() {
|
|
|
|
this._ibus.list_engines_async(-1, null, Lang.bind(this, this._initEngines));
|
|
|
|
this._ibus.request_name_async(IBus.SERVICE_PANEL,
|
|
|
|
IBus.BusNameFlag.REPLACE_EXISTING,
|
|
|
|
-1, null,
|
|
|
|
Lang.bind(this, this._initPanelService));
|
|
|
|
},
|
|
|
|
|
|
|
|
_initEngines: function(ibus, result) {
|
|
|
|
let enginesList = this._ibus.list_engines_async_finish(result);
|
|
|
|
if (enginesList) {
|
|
|
|
for (let i = 0; i < enginesList.length; ++i) {
|
|
|
|
let name = enginesList[i].get_name();
|
|
|
|
this._engines[name] = enginesList[i];
|
|
|
|
}
|
|
|
|
this._updateReadiness();
|
|
|
|
} else {
|
|
|
|
this._clear();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_initPanelService: function(ibus, result) {
|
|
|
|
let success = this._ibus.request_name_async_finish(result);
|
|
|
|
if (success) {
|
|
|
|
this._panelService = new IBus.PanelService({ connection: this._ibus.get_connection(),
|
|
|
|
object_path: IBus.PATH_PANEL });
|
|
|
|
this._candidatePopup.setPanelService(this._panelService);
|
|
|
|
this._panelService.connect('update-property', Lang.bind(this, this._updateProperty));
|
2014-11-27 03:23:01 -05:00
|
|
|
try {
|
|
|
|
// IBus versions older than 1.5.10 have a bug which
|
|
|
|
// causes spurious set-content-type emissions when
|
|
|
|
// switching input focus that temporarily lose purpose
|
|
|
|
// and hints defeating its intended semantics and
|
|
|
|
// confusing users. We thus don't use it in that case.
|
|
|
|
_checkIBusVersion(1, 5, 10);
|
|
|
|
this._panelService.connect('set-content-type', Lang.bind(this, this._setContentType));
|
|
|
|
} catch (e) {
|
|
|
|
}
|
2014-02-14 07:35:05 -05:00
|
|
|
// If an engine is already active we need to get its properties
|
|
|
|
this._ibus.get_global_engine_async(-1, null, Lang.bind(this, function(i, result) {
|
|
|
|
let engine;
|
|
|
|
try {
|
|
|
|
engine = this._ibus.get_global_engine_async_finish(result);
|
|
|
|
if (!engine)
|
|
|
|
return;
|
|
|
|
} catch(e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._engineChanged(this._ibus, engine.get_name());
|
|
|
|
}));
|
|
|
|
this._updateReadiness();
|
|
|
|
} else {
|
|
|
|
this._clear();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateReadiness: function() {
|
|
|
|
this._ready = (Object.keys(this._engines).length > 0 &&
|
|
|
|
this._panelService != null);
|
|
|
|
this.emit('ready', this._ready);
|
|
|
|
},
|
|
|
|
|
|
|
|
_engineChanged: function(bus, engineName) {
|
|
|
|
if (!this._ready)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._currentEngineName = engineName;
|
|
|
|
|
|
|
|
if (this._registerPropertiesId != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._registerPropertiesId =
|
|
|
|
this._panelService.connect('register-properties', Lang.bind(this, function(p, props) {
|
|
|
|
if (!props.get(0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._panelService.disconnect(this._registerPropertiesId);
|
|
|
|
this._registerPropertiesId = 0;
|
|
|
|
|
|
|
|
this.emit('properties-registered', this._currentEngineName, props);
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateProperty: function(panel, prop) {
|
|
|
|
this.emit('property-updated', this._currentEngineName, prop);
|
|
|
|
},
|
|
|
|
|
2014-11-27 03:23:01 -05:00
|
|
|
_setContentType: function(panel, purpose, hints) {
|
|
|
|
this.emit('set-content-type', purpose, hints);
|
|
|
|
},
|
|
|
|
|
2014-02-14 07:35:05 -05:00
|
|
|
activateProperty: function(key, state) {
|
|
|
|
this._panelService.property_activate(key, state);
|
|
|
|
},
|
|
|
|
|
|
|
|
getEngineDesc: function(id) {
|
2017-06-12 23:03:17 -04:00
|
|
|
if (!IBus || !this._ready || !this._engines.hasOwnProperty(id))
|
2014-02-14 07:35:05 -05:00
|
|
|
return null;
|
|
|
|
|
|
|
|
return this._engines[id];
|
2014-06-05 12:47:48 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setEngine: function(id, callback) {
|
2014-11-27 03:23:01 -05:00
|
|
|
// Send id even if id == this._currentEngineName
|
|
|
|
// because 'properties-registered' signal can be emitted
|
|
|
|
// while this._ibusSources == null on a lock screen.
|
|
|
|
if (!IBus || !this._ready) {
|
2014-06-05 12:47:48 -04:00
|
|
|
if (callback)
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._ibus.set_global_engine_async(id, this._MAX_INPUT_SOURCE_ACTIVATION_TIME,
|
|
|
|
null, callback);
|
|
|
|
},
|
2014-11-10 05:09:08 -05:00
|
|
|
|
|
|
|
preloadEngines: function(ids) {
|
|
|
|
if (!IBus || !this._ibus || ids.length == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this._preloadEnginesId != 0) {
|
|
|
|
Mainloop.source_remove(this._preloadEnginesId);
|
|
|
|
this._preloadEnginesId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._preloadEnginesId =
|
|
|
|
Mainloop.timeout_add_seconds(this._PRELOAD_ENGINES_DELAY_TIME,
|
|
|
|
Lang.bind(this, function() {
|
|
|
|
this._ibus.preload_engines_async(
|
|
|
|
ids,
|
|
|
|
-1,
|
|
|
|
null,
|
|
|
|
null);
|
|
|
|
this._preloadEnginesId = 0;
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
}));
|
|
|
|
},
|
2014-02-14 07:35:05 -05:00
|
|
|
});
|
|
|
|
Signals.addSignalMethods(IBusManager.prototype);
|