1
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
3
const Caribou = imports.gi.Caribou;
4
const Clutter = imports.gi.Clutter;
5
const DBus = imports.dbus;
6
const Gdk = imports.gi.Gdk;
7
const Gio = imports.gi.Gio;
8
const GLib = imports.gi.GLib;
9
const Lang = imports.lang;
10
const Shell = imports.gi.Shell;
11
const St = imports.gi.St;
13
const BoxPointer = imports.ui.boxpointer;
14
const Main = imports.ui.main;
15
const MessageTray = imports.ui.messageTray;
17
const KEYBOARD_SCHEMA = 'org.gnome.shell.keyboard';
18
const KEYBOARD_TYPE = 'keyboard-type';
20
const A11Y_APPLICATIONS_SCHEMA = 'org.gnome.desktop.a11y.applications';
21
const SHOW_KEYBOARD = 'screen-keyboard-enabled';
23
// Key constants taken from Antler
24
// FIXME: ought to be moved into libcaribou
26
'BackSpace': '\u232b',
29
'Caribou_Prefs': '\u2328',
30
'Caribou_ShiftUp': '\u2b06',
31
'Caribou_ShiftDown': '\u2b07',
32
'Caribou_Emoticons': '\u263a',
33
'Caribou_Symbols': '123',
34
'Caribou_Symbols_More': '{#*',
35
'Caribou_Alpha': 'Abc',
42
const CaribouKeyboardIface = <interface name='org.gnome.Caribou.Keyboard'>
44
<arg type='u' direction='in' />
47
<arg type='u' direction='in' />
49
<method name='SetCursorLocation'>
50
<arg type='i' direction='in' />
51
<arg type='i' direction='in' />
52
<arg type='i' direction='in' />
53
<arg type='i' direction='in' />
55
<method name='SetEntryLocation'>
56
<arg type='i' direction='in' />
57
<arg type='i' direction='in' />
58
<arg type='i' direction='in' />
59
<arg type='i' direction='in' />
61
<property name='Name' access='read' type='s' />
64
const Key = new Lang.Class({
67
_init : function(key) {
70
this.actor = this._makeKey();
72
this._extended_keys = this._key.get_extended_keys();
73
this._extended_keyboard = null;
75
if (this._key.name == 'Control_L' || this._key.name == 'Alt_L')
76
this._key.latch = true;
78
this._key.connect('key-pressed', Lang.bind(this, function ()
79
{ this.actor.checked = true }));
80
this._key.connect('key-released', Lang.bind(this, function ()
81
{ this.actor.checked = false; }));
83
if (this._extended_keys.length > 0) {
84
this._grabbed = false;
85
this._eventCaptureId = 0;
86
this._key.connect('notify::show-subkeys', Lang.bind(this, this._onShowSubkeysChanged));
87
this._boxPointer = new BoxPointer.BoxPointer(St.Side.BOTTOM,
90
x_align: St.Align.START });
91
// Adds style to existing keyboard style to avoid repetition
92
this._boxPointer.actor.add_style_class_name('keyboard-subkeys');
93
this._getExtendedKeys();
94
this.actor._extended_keys = this._extended_keyboard;
95
this._boxPointer.actor.hide();
96
Main.layoutManager.addChrome(this._boxPointer.actor);
100
_makeKey: function () {
101
let label = this._key.name;
103
if (label.length > 1) {
104
let pretty = PRETTY_KEYS[label];
108
label = this._getUnichar(this._key);
111
label = GLib.markup_escape_text(label, -1);
112
let button = new St.Button ({ label: label,
113
style_class: 'keyboard-key' });
115
button.key_width = this._key.width;
116
button.connect('button-press-event', Lang.bind(this, function () { this._key.press(); }));
117
button.connect('button-release-event', Lang.bind(this, function () { this._key.release(); }));
122
_getUnichar: function(key) {
123
let keyval = key.keyval;
124
let unichar = Gdk.keyval_to_unicode(keyval);
126
return String.fromCharCode(unichar);
132
_getExtendedKeys: function () {
133
this._extended_keyboard = new St.BoxLayout({ style_class: 'keyboard-layout',
135
for (let i = 0; i < this._extended_keys.length; ++i) {
136
let extended_key = this._extended_keys[i];
137
let label = this._getUnichar(extended_key);
138
let key = new St.Button({ label: label, style_class: 'keyboard-key' });
139
key.extended_key = extended_key;
140
key.connect('button-press-event', Lang.bind(this, function () { extended_key.press(); }));
141
key.connect('button-release-event', Lang.bind(this, function () { extended_key.release(); }));
142
this._extended_keyboard.add(key);
144
this._boxPointer.bin.add_actor(this._extended_keyboard);
147
_onEventCapture: function (actor, event) {
148
let source = event.get_source();
149
let type = event.type();
151
if ((type == Clutter.EventType.BUTTON_PRESS ||
152
type == Clutter.EventType.BUTTON_RELEASE) &&
153
this._extended_keyboard.contains(source)) {
154
source.extended_key.press();
155
source.extended_key.release();
158
if (type == Clutter.EventType.BUTTON_PRESS) {
159
this._boxPointer.actor.hide();
166
_ungrab: function () {
167
global.stage.disconnect(this._eventCaptureId);
168
this._eventCaptureId = 0;
169
this._grabbed = false;
170
Main.popModal(this.actor);
173
_onShowSubkeysChanged: function () {
174
if (this._key.show_subkeys) {
175
this.actor.fake_release();
176
this._boxPointer.actor.raise_top();
177
this._boxPointer.setPosition(this.actor, 0.5);
178
this._boxPointer.show(BoxPointer.PopupAnimation.FULL);
179
this.actor.set_hover(false);
180
if (!this._grabbed) {
181
Main.pushModal(this.actor);
182
this._eventCaptureId = global.stage.connect('captured-event', Lang.bind(this, this._onEventCapture));
183
this._grabbed = true;
189
this._boxPointer.hide(BoxPointer.PopupAnimation.FULL);
194
const Keyboard = new Lang.Class({
195
// HACK: we can't set Name, because it collides with Name dbus property
199
this._impl = Gio.DBusExportedObject.wrapJSObject(CaribouKeyboardIface, this);
200
this._impl.export(Gio.DBus.session, '/org/gnome/Caribou/Keyboard');
203
this._focusInTray = false;
204
this._focusInExtendedKeys = false;
206
this._timestamp = global.display.get_current_time_roundtrip();
207
Main.layoutManager.connect('monitors-changed', Lang.bind(this, this._redraw));
209
this._keyboardSettings = new Gio.Settings({ schema: KEYBOARD_SCHEMA });
210
this._keyboardSettings.connect('changed', Lang.bind(this, this._settingsChanged));
211
this._a11yApplicationsSettings = new Gio.Settings({ schema: A11Y_APPLICATIONS_SCHEMA });
212
this._a11yApplicationsSettings.connect('changed', Lang.bind(this, this._settingsChanged));
213
this._settingsChanged();
220
_settingsChanged: function (settings, key) {
221
this._enableKeyboard = this._a11yApplicationsSettings.get_boolean(SHOW_KEYBOARD);
222
if (!this._enableKeyboard && !this._keyboard)
224
if (this._enableKeyboard && this._keyboard &&
225
this._keyboard.keyboard_type == this._keyboardSettings.get_string(KEYBOARD_TYPE))
229
this._destroyKeyboard();
231
if (this._enableKeyboard) {
232
// If we've been called because the setting actually just
233
// changed to true (as opposed to being called from
234
// this._init()), then we want to pop up the keyboard.
235
let showKeyboard = (settings != null);
237
// However, caribou-gtk-module or this._onKeyFocusChanged
238
// will probably immediately tell us to hide it, so we
239
// have to fake things out so we'll ignore that request.
241
this._timestamp = global.display.get_current_time_roundtrip() + 1;
242
this._setupKeyboard(showKeyboard);
244
Main.layoutManager.hideKeyboard(true);
247
_destroyKeyboard: function() {
248
if (this._keyboardNotifyId)
249
this._keyboard.disconnect(this._keyboardNotifyId);
250
if (this._focusNotifyId)
251
global.stage.disconnect(this._focusNotifyId);
252
this._keyboard = null;
253
this.actor.destroy();
256
this._destroySource();
259
_setupKeyboard: function(show) {
260
this.actor = new St.BoxLayout({ name: 'keyboard', vertical: true, reactive: true });
261
Main.layoutManager.keyboardBox.add_actor(this.actor);
262
Main.layoutManager.trackChrome(this.actor);
264
this._keyboard = new Caribou.KeyboardModel({ keyboard_type: this._keyboardSettings.get_string(KEYBOARD_TYPE) });
266
this._current_page = null;
268
// Initialize keyboard key measurements
269
this._numOfHorizKeys = 0;
270
this._numOfVertKeys = 0;
274
// Keys should be layout according to the group, not the
275
// locale; as Caribou already provides the expected layout,
276
// this means enforcing LTR for all locales.
277
this.actor.text_direction = Clutter.TextDirection.LTR;
279
this._keyboardNotifyId = this._keyboard.connect('notify::active-group', Lang.bind(this, this._onGroupChanged));
280
this._focusNotifyId = global.stage.connect('notify::key-focus', Lang.bind(this, this._onKeyFocusChanged));
285
this._createSource();
288
_onKeyFocusChanged: function () {
289
let focus = global.stage.key_focus;
291
// Showing an extended key popup and clicking a key from the extended keys
292
// will grab focus, but ignore that
293
let extendedKeysWereFocused = this._focusInExtendedKeys;
294
this._focusInExtendedKeys = focus && (focus._extended_keys || focus.extended_key);
295
if (this._focusInExtendedKeys || extendedKeysWereFocused)
298
// Ignore focus changes caused by message tray showing/hiding
299
let trayWasFocused = this._focusInTray;
300
this._focusInTray = (focus && Main.messageTray.actor.contains(focus));
301
if (this._focusInTray || trayWasFocused)
304
let time = global.get_current_time();
305
if (focus instanceof Clutter.Text)
311
_addKeys: function () {
312
let groups = this._keyboard.get_groups();
313
for (let i = 0; i < groups.length; ++i) {
314
let gname = groups[i];
315
let group = this._keyboard.get_group(gname);
316
group.connect('notify::active-level', Lang.bind(this, this._onLevelChanged));
318
let levels = group.get_levels();
319
for (let j = 0; j < levels.length; ++j) {
320
let lname = levels[j];
321
let level = group.get_level(lname);
322
let layout = new St.BoxLayout({ style_class: 'keyboard-layout',
324
this._loadRows(level, layout);
325
layers[lname] = layout;
326
this.actor.add(layout, { x_fill: false });
330
this._groups[gname] = layers;
333
this._setActiveLayer();
336
_getTrayIcon: function () {
337
let trayButton = new St.Button ({ label: _("tray"),
338
style_class: 'keyboard-key' });
339
trayButton.key_width = 1;
340
trayButton.connect('button-press-event', Lang.bind(this, function () {
341
Main.messageTray.toggle();
344
Main.overview.connect('showing', Lang.bind(this, function () {
345
trayButton.reactive = false;
346
trayButton.add_style_pseudo_class('grayed');
348
Main.overview.connect('hiding', Lang.bind(this, function () {
349
trayButton.reactive = true;
350
trayButton.remove_style_pseudo_class('grayed');
352
Main.sessionMode.connect('updated', Lang.bind(this, function() {
353
trayButton.reactive = !Main.sessionMode.isLocked;
354
if (Main.sessionMode.isLocked)
355
trayButton.add_style_pseudo_class('grayed');
357
trayButton.remove_style_pseudo_class('grayed');
363
_addRows : function (keys, layout) {
364
let keyboard_row = new St.BoxLayout();
365
for (let i = 0; i < keys.length; ++i) {
366
let children = keys[i].get_children();
367
let right_box = new St.BoxLayout({ style_class: 'keyboard-row' });
368
let left_box = new St.BoxLayout({ style_class: 'keyboard-row' });
369
for (let j = 0; j < children.length; ++j) {
370
if (this._numOfHorizKeys == 0)
371
this._numOfHorizKeys = children.length;
372
let key = children[j];
373
let button = new Key(key);
375
if (key.align == 'right')
376
right_box.add(button.actor);
378
left_box.add(button.actor);
379
if (key.name == 'Caribou_Prefs') {
380
key.connect('key-released', Lang.bind(this, this.hide));
382
// Add new key for hiding message tray
383
right_box.add(this._getTrayIcon());
386
keyboard_row.add(left_box, { expand: true, x_fill: false, x_align: St.Align.START });
387
keyboard_row.add(right_box, { expand: true, x_fill: false, x_align: St.Align.END });
389
layout.add(keyboard_row);
392
_loadRows : function (level, layout) {
393
let rows = level.get_rows();
394
for (let i = 0; i < rows.length; ++i) {
396
if (this._numOfVertKeys == 0)
397
this._numOfVertKeys = rows.length;
398
this._addRows(row.get_columns(), layout);
403
_redraw: function () {
404
if (!this._enableKeyboard)
407
let monitor = Main.layoutManager.bottomMonitor;
408
let maxHeight = monitor.height / 3;
409
this.actor.width = monitor.width;
411
let layout = this._current_page;
412
let verticalSpacing = layout.get_theme_node().get_length('spacing');
413
let padding = layout.get_theme_node().get_length('padding');
415
let box = layout.get_children()[0].get_children()[0];
416
let horizontalSpacing = box.get_theme_node().get_length('spacing');
417
let allHorizontalSpacing = (this._numOfHorizKeys - 1) * horizontalSpacing;
418
let keyWidth = Math.floor((this.actor.width - allHorizontalSpacing - 2 * padding) / this._numOfHorizKeys);
420
let allVerticalSpacing = (this._numOfVertKeys - 1) * verticalSpacing;
421
let keyHeight = Math.floor((maxHeight - allVerticalSpacing - 2 * padding) / this._numOfVertKeys);
423
let keySize = Math.min(keyWidth, keyHeight);
424
this.actor.height = keySize * this._numOfVertKeys + allVerticalSpacing + 2 * padding;
426
let rows = this._current_page.get_children();
427
for (let i = 0; i < rows.length; ++i) {
428
let keyboard_row = rows[i];
429
let boxes = keyboard_row.get_children();
430
for (let j = 0; j < boxes.length; ++j) {
431
let keys = boxes[j].get_children();
432
for (let k = 0; k < keys.length; ++k) {
434
child.width = keySize * child.key_width;
435
child.height = keySize;
436
if (child._extended_keys) {
437
let extended_keys = child._extended_keys.get_children();
438
for (let n = 0; n < extended_keys.length; ++n) {
439
let extended_key = extended_keys[n];
440
extended_key.width = keySize;
441
extended_key.height = keySize;
449
_onLevelChanged: function () {
450
this._setActiveLayer();
454
_onGroupChanged: function () {
455
this._setActiveLayer();
459
_setActiveLayer: function () {
460
let active_group_name = this._keyboard.active_group;
461
let active_group = this._keyboard.get_group(active_group_name);
462
let active_level = active_group.active_level;
463
let layers = this._groups[active_group_name];
465
if (this._current_page != null) {
466
this._current_page.hide();
469
this._current_page = layers[active_level];
470
this._current_page.show();
473
_createSource: function () {
474
if (this._source == null) {
475
this._source = new KeyboardSource(this);
476
this._source.setTransient(true);
477
Main.messageTray.add(this._source);
481
_destroySource: function () {
483
this._source.destroy();
488
shouldTakeEvent: function(event) {
489
let actor = event.get_source();
490
return Main.layoutManager.keyboardBox.contains(actor) ||
491
actor._extended_keys || actor.extended_key;
497
Main.layoutManager.showKeyboard();
498
this._destroySource();
502
Main.layoutManager.hideKeyboard();
503
this._createSource();
506
_moveTemporarily: function () {
507
let currentWindow = global.screen.get_display().focus_window;
508
let rect = currentWindow.get_outer_rect();
511
let newY = 3 * this.actor.height / 2;
512
currentWindow.move_frame(true, newX, newY);
515
_setLocation: function (x, y) {
516
if (y >= 2 * this.actor.height)
517
this._moveTemporarily();
520
// _compareTimestamp:
522
// Compare two timestamps taking into account
524
_compareTimestamp: function(one, two) {
527
if (one == Clutter.CURRENT_TIME)
529
if (two == Clutter.CURRENT_TIME)
535
Show: function(timestamp) {
536
if (!this._enableKeyboard)
539
if (this._compareTimestamp(timestamp, this._timestamp) < 0)
542
if (timestamp != Clutter.CURRENT_TIME)
543
this._timestamp = timestamp;
547
Hide: function(timestamp) {
548
if (!this._enableKeyboard)
551
if (this._compareTimestamp(timestamp, this._timestamp) < 0)
554
if (timestamp != Clutter.CURRENT_TIME)
555
this._timestamp = timestamp;
559
SetCursorLocation: function(x, y, w, h) {
560
if (!this._enableKeyboard)
563
// this._setLocation(x, y);
566
SetEntryLocation: function(x, y, w, h) {
567
if (!this._enableKeyboard)
570
// this._setLocation(x, y);
574
return 'gnome-shell';
578
const KeyboardSource = new Lang.Class({
579
Name: 'KeyboardSource',
580
Extends: MessageTray.Source,
582
_init: function(keyboard) {
583
this._keyboard = keyboard;
584
this.parent(_("Keyboard"), 'input-keyboard-symbolic');
585
this.keepTrayOnSummaryClick = true;
588
handleSummaryClick: function() {
589
let event = Clutter.get_current_event();
590
if (event.type() != Clutter.EventType.BUTTON_RELEASE)
598
this._keyboard.show();