-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
InputManipulator.js
166 lines (131 loc) · 6.68 KB
/
InputManipulator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//////////////////////////////////////////////////////////////////////////////////////////
// ___ _ ___ //
// | | \/ | ) | | //
// O- |- | | - | | |- -O //
// | |_ | | | |_ //
// //
//////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: Simon Schneegans <code@simonschneegans.de>
// SPDX-License-Identifier: MIT
'use strict';
import Clutter from 'gi://Clutter';
import {NameToKeysym} from './keysyms.js';
//////////////////////////////////////////////////////////////////////////////////////////
// An instance of this class can be used to create faked input events. You can use it //
// to move the mouse pointer or to press accelerator key strokes. //
//////////////////////////////////////////////////////////////////////////////////////////
export default class InputManipulator {
// ------------------------------------------------------------ constructor / destructor
constructor() {
// get_default_seat() is available since GNOME Shell 3.36.
if (Clutter.get_default_backend().get_default_seat) {
const dev = Clutter.get_default_backend().get_default_seat();
this._mouse = dev.create_virtual_device(Clutter.InputDeviceType.POINTER_DEVICE);
this._keyboard = dev.create_virtual_device(Clutter.InputDeviceType.KEYBOARD_DEVICE);
} else {
const dev = Clutter.DeviceManager.get_default();
this._mouse = dev.create_virtual_device(Clutter.InputDeviceType.POINTER_DEVICE);
this._keyboard = dev.create_virtual_device(Clutter.InputDeviceType.KEYBOARD_DEVICE);
}
}
// -------------------------------------------------------------------- public interface
// Warps the mouse pointer to the specified position.
warpPointer(x, y) {
this._mouse.notify_absolute_motion(0, x, y);
}
// Simulates the activation of a given accelerator. The string can be anything accepted
// by Gtk.accelerator_parse(). That is, for example, "<Control>a" or "<Shift><Alt>F1".
activateAccelerator(string) {
// First we release any currently pressed modifiers.
const currentMods = global.get_pointer()[2];
this._releaseModifiers(currentMods);
// Now parse the string and press the buttons accordingly.
const [keyval, mods] = this._parseAccelerator(string);
this._pressModifiers(mods);
this._keyboard.notify_keyval(0, keyval, Clutter.KeyState.PRESSED);
this._keyboard.notify_keyval(0, keyval, Clutter.KeyState.RELEASED);
this._releaseModifiers(mods);
// Finally we re-press the modifiers which were pressed before.
this._pressModifiers(currentMods);
}
// ----------------------------------------------------------------------- private stuff
// Helper method which 'releases' the desired modifier keys.
_releaseModifiers(modifiers) {
// Since we do not know whether left or right version of each key is pressed, we
// release both...
if (modifiers & Clutter.ModifierType.CONTROL_MASK) {
this._keyboard.notify_keyval(0, Clutter.KEY_Control_L, Clutter.KeyState.RELEASED);
this._keyboard.notify_keyval(0, Clutter.KEY_Control_R, Clutter.KeyState.RELEASED);
}
if (modifiers & Clutter.ModifierType.SHIFT_MASK) {
this._keyboard.notify_keyval(0, Clutter.KEY_Shift_L, Clutter.KeyState.RELEASED);
this._keyboard.notify_keyval(0, Clutter.KEY_Shift_R, Clutter.KeyState.RELEASED);
}
if (modifiers & Clutter.ModifierType.MOD1_MASK) {
this._keyboard.notify_keyval(0, Clutter.KEY_Alt_L, Clutter.KeyState.RELEASED);
this._keyboard.notify_keyval(0, Clutter.KEY_Alt_R, Clutter.KeyState.RELEASED);
}
if ((modifiers & Clutter.ModifierType.MOD4_MASK) ||
(modifiers & Clutter.ModifierType.SUPER_MASK)) {
this._keyboard.notify_keyval(0, Clutter.KEY_Super_L, Clutter.KeyState.RELEASED);
this._keyboard.notify_keyval(0, Clutter.KEY_Super_R, Clutter.KeyState.RELEASED);
}
}
// Helper method which 'presses' the desired modifier keys.
_pressModifiers(modifiers) {
if (modifiers & Clutter.ModifierType.CONTROL_MASK) {
this._keyboard.notify_keyval(0, Clutter.KEY_Control_L, Clutter.KeyState.PRESSED);
}
if (modifiers & Clutter.ModifierType.SHIFT_MASK) {
this._keyboard.notify_keyval(0, Clutter.KEY_Shift_L, Clutter.KeyState.PRESSED);
}
if (modifiers & Clutter.ModifierType.MOD1_MASK) {
this._keyboard.notify_keyval(0, Clutter.KEY_Alt_L, Clutter.KeyState.PRESSED);
}
if ((modifiers & Clutter.ModifierType.MOD4_MASK) ||
(modifiers & Clutter.ModifierType.SUPER_MASK)) {
this._keyboard.notify_keyval(0, Clutter.KEY_Super_L, Clutter.KeyState.PRESSED);
}
}
// Parses the given string and returns the keyval and modifiers. This method attempts is
// used as a drop-in replacement for Gtk.accelerator_parse() which we cannot use since
// we do not have access to the Gtk library.
_parseAccelerator(accelerator) {
const accelerators = [
{
names: ['<control>', '<ctrl>', '<ctl>', '<primary>'],
mask: Clutter.ModifierType.CONTROL_MASK
},
{names: ['<shift>', '<shft>'], mask: Clutter.ModifierType.SHIFT_MASK},
{names: ['<alt>', '<mod1>'], mask: Clutter.ModifierType.MOD1_MASK},
{names: ['<meta>'], mask: Clutter.ModifierType.META_MASK},
{names: ['<super>'], mask: Clutter.ModifierType.SUPER_MASK},
{names: ['<hyper>'], mask: Clutter.ModifierType.HYPER_MASK},
{names: ['<mod2>'], mask: Clutter.ModifierType.MOD2_MASK},
{names: ['<mod3>'], mask: Clutter.ModifierType.MOD3_MASK},
{names: ['<mod4>'], mask: Clutter.ModifierType.MOD4_MASK},
{names: ['<mod5>'], mask: Clutter.ModifierType.MOD5_MASK},
];
const acceleratorLower = accelerator.toLowerCase();
let mods = 0;
for (const accelerator of accelerators) {
for (const name of accelerator.names) {
if (acceleratorLower.includes(name)) {
mods |= accelerator.mask;
}
}
}
// Remove all modifiers from the string.
const key = accelerator.replace(/<[^>]+>/g, '');
// Try to convert the remaining string to a keyval.
let keyval = NameToKeysym[key];
// Some keys have a XF86 prefix.
if (keyval === undefined) {
keyval = NameToKeysym['XF86' + key];
}
if (keyval === undefined) {
throw new Error(`Could not parse accelerator: ${accelerator}`);
}
return [keyval, mods];
}
};