input.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
  2. //
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. import Gdk from 'gi://Gdk';
  5. import Gio from 'gi://Gio';
  6. import GLib from 'gi://GLib';
  7. import GObject from 'gi://GObject';
  8. import AtspiController from './atspi.js';
  9. const SESSION_TIMEOUT = 300;
  10. const RemoteSession = GObject.registerClass({
  11. GTypeName: 'GSConnectRemoteSession',
  12. Implements: [Gio.DBusInterface],
  13. Signals: {
  14. 'closed': {
  15. flags: GObject.SignalFlags.RUN_FIRST,
  16. },
  17. },
  18. }, class RemoteSession extends Gio.DBusProxy {
  19. _init(objectPath) {
  20. super._init({
  21. g_bus_type: Gio.BusType.SESSION,
  22. g_name: 'org.gnome.Mutter.RemoteDesktop',
  23. g_object_path: objectPath,
  24. g_interface_name: 'org.gnome.Mutter.RemoteDesktop.Session',
  25. g_flags: Gio.DBusProxyFlags.NONE,
  26. });
  27. this._started = false;
  28. }
  29. vfunc_g_signal(sender_name, signal_name, parameters) {
  30. if (signal_name === 'Closed')
  31. this.emit('closed');
  32. }
  33. _call(name, parameters = null) {
  34. if (!this._started)
  35. return;
  36. // Pass a null callback to allow this call to finish itself
  37. this.call(name, parameters, Gio.DBusCallFlags.NONE, -1, null, null);
  38. }
  39. get session_id() {
  40. try {
  41. return this.get_cached_property('SessionId').unpack();
  42. } catch {
  43. return null;
  44. }
  45. }
  46. async start() {
  47. try {
  48. if (this._started)
  49. return;
  50. // Initialize the proxy, and start the session
  51. await this.init_async(GLib.PRIORITY_DEFAULT, null);
  52. await this.call('Start', null, Gio.DBusCallFlags.NONE, -1, null);
  53. this._started = true;
  54. } catch (e) {
  55. this.destroy();
  56. Gio.DBusError.strip_remote_error(e);
  57. throw e;
  58. }
  59. }
  60. stop() {
  61. if (this._started) {
  62. this._started = false;
  63. // Pass a null callback to allow this call to finish itself
  64. this.call('Stop', null, Gio.DBusCallFlags.NONE, -1, null, null);
  65. }
  66. }
  67. _translateButton(button) {
  68. switch (button) {
  69. case Gdk.BUTTON_PRIMARY:
  70. return 0x110;
  71. case Gdk.BUTTON_MIDDLE:
  72. return 0x112;
  73. case Gdk.BUTTON_SECONDARY:
  74. return 0x111;
  75. case 4:
  76. return 0; // FIXME
  77. case 5:
  78. return 0x10F; // up
  79. }
  80. }
  81. movePointer(dx, dy) {
  82. this._call(
  83. 'NotifyPointerMotionRelative',
  84. GLib.Variant.new('(dd)', [dx, dy])
  85. );
  86. }
  87. pressPointer(button) {
  88. button = this._translateButton(button);
  89. this._call(
  90. 'NotifyPointerButton',
  91. GLib.Variant.new('(ib)', [button, true])
  92. );
  93. }
  94. releasePointer(button) {
  95. button = this._translateButton(button);
  96. this._call(
  97. 'NotifyPointerButton',
  98. GLib.Variant.new('(ib)', [button, false])
  99. );
  100. }
  101. clickPointer(button) {
  102. button = this._translateButton(button);
  103. this._call(
  104. 'NotifyPointerButton',
  105. GLib.Variant.new('(ib)', [button, true])
  106. );
  107. this._call(
  108. 'NotifyPointerButton',
  109. GLib.Variant.new('(ib)', [button, false])
  110. );
  111. }
  112. doubleclickPointer(button) {
  113. this.clickPointer(button);
  114. this.clickPointer(button);
  115. }
  116. scrollPointer(dx, dy) {
  117. if (dy > 0) {
  118. this._call(
  119. 'NotifyPointerAxisDiscrete',
  120. GLib.Variant.new('(ui)', [Gdk.ScrollDirection.UP, 1])
  121. );
  122. } else if (dy < 0) {
  123. this._call(
  124. 'NotifyPointerAxisDiscrete',
  125. GLib.Variant.new('(ui)', [Gdk.ScrollDirection.UP, -1])
  126. );
  127. }
  128. }
  129. /*
  130. * Keyboard Events
  131. */
  132. pressKeysym(keysym) {
  133. this._call(
  134. 'NotifyKeyboardKeysym',
  135. GLib.Variant.new('(ub)', [keysym, true])
  136. );
  137. }
  138. releaseKeysym(keysym) {
  139. this._call(
  140. 'NotifyKeyboardKeysym',
  141. GLib.Variant.new('(ub)', [keysym, false])
  142. );
  143. }
  144. pressreleaseKeysym(keysym) {
  145. this._call(
  146. 'NotifyKeyboardKeysym',
  147. GLib.Variant.new('(ub)', [keysym, true])
  148. );
  149. this._call(
  150. 'NotifyKeyboardKeysym',
  151. GLib.Variant.new('(ub)', [keysym, false])
  152. );
  153. }
  154. /*
  155. * High-level keyboard input
  156. */
  157. pressKey(input, modifiers) {
  158. // Press Modifiers
  159. if (modifiers & Gdk.ModifierType.MOD1_MASK)
  160. this.pressKeysym(Gdk.KEY_Alt_L);
  161. if (modifiers & Gdk.ModifierType.CONTROL_MASK)
  162. this.pressKeysym(Gdk.KEY_Control_L);
  163. if (modifiers & Gdk.ModifierType.SHIFT_MASK)
  164. this.pressKeysym(Gdk.KEY_Shift_L);
  165. if (modifiers & Gdk.ModifierType.SUPER_MASK)
  166. this.pressKeysym(Gdk.KEY_Super_L);
  167. if (typeof input === 'string') {
  168. const keysym = Gdk.unicode_to_keyval(input.codePointAt(0));
  169. this.pressreleaseKeysym(keysym);
  170. } else {
  171. this.pressreleaseKeysym(input);
  172. }
  173. // Release Modifiers
  174. if (modifiers & Gdk.ModifierType.MOD1_MASK)
  175. this.releaseKeysym(Gdk.KEY_Alt_L);
  176. if (modifiers & Gdk.ModifierType.CONTROL_MASK)
  177. this.releaseKeysym(Gdk.KEY_Control_L);
  178. if (modifiers & Gdk.ModifierType.SHIFT_MASK)
  179. this.releaseKeysym(Gdk.KEY_Shift_L);
  180. if (modifiers & Gdk.ModifierType.SUPER_MASK)
  181. this.releaseKeysym(Gdk.KEY_Super_L);
  182. }
  183. destroy() {
  184. if (this.__disposed === undefined) {
  185. this.__disposed = true;
  186. GObject.signal_handlers_destroy(this);
  187. }
  188. }
  189. });
  190. export default class Controller {
  191. constructor() {
  192. this._nameAppearedId = 0;
  193. this._session = null;
  194. this._sessionCloseId = 0;
  195. this._sessionExpiry = 0;
  196. this._sessionExpiryId = 0;
  197. this._sessionStarting = false;
  198. // Watch for the RemoteDesktop portal
  199. this._nameWatcherId = Gio.bus_watch_name(
  200. Gio.BusType.SESSION,
  201. 'org.gnome.Mutter.RemoteDesktop',
  202. Gio.BusNameWatcherFlags.NONE,
  203. this._onNameAppeared.bind(this),
  204. this._onNameVanished.bind(this)
  205. );
  206. }
  207. get connection() {
  208. if (this._connection === undefined)
  209. this._connection = null;
  210. return this._connection;
  211. }
  212. _onNameAppeared(connection, name, name_owner) {
  213. try {
  214. this._connection = connection;
  215. } catch (e) {
  216. logError(e);
  217. }
  218. }
  219. _onNameVanished(connection, name) {
  220. try {
  221. if (this._session !== null)
  222. this._onSessionClosed(this._session);
  223. } catch (e) {
  224. logError(e);
  225. }
  226. }
  227. _onSessionClosed(session) {
  228. // Disconnect from the session
  229. if (this._sessionClosedId > 0) {
  230. session.disconnect(this._sessionClosedId);
  231. this._sessionClosedId = 0;
  232. }
  233. // Destroy the session
  234. session.destroy();
  235. this._session = null;
  236. }
  237. _onSessionExpired() {
  238. // If the session has been used recently, schedule a new expiry
  239. const remainder = Math.floor(this._sessionExpiry - (Date.now() / 1000));
  240. if (remainder > 0) {
  241. this._sessionExpiryId = GLib.timeout_add_seconds(
  242. GLib.PRIORITY_DEFAULT,
  243. remainder,
  244. this._onSessionExpired.bind(this)
  245. );
  246. return GLib.SOURCE_REMOVE;
  247. }
  248. // Otherwise if there's an active session, close it
  249. if (this._session !== null)
  250. this._session.stop();
  251. // Reset the GSource Id
  252. this._sessionExpiryId = 0;
  253. return GLib.SOURCE_REMOVE;
  254. }
  255. async _createRemoteDesktopSession() {
  256. if (this.connection === null)
  257. return Promise.reject(new Error('No DBus connection'));
  258. const reply = await this.connection.call(
  259. 'org.gnome.Mutter.RemoteDesktop',
  260. '/org/gnome/Mutter/RemoteDesktop',
  261. 'org.gnome.Mutter.RemoteDesktop',
  262. 'CreateSession',
  263. null,
  264. null,
  265. Gio.DBusCallFlags.NONE,
  266. -1,
  267. null);
  268. return reply.deepUnpack()[0];
  269. }
  270. async _ensureAdapter() {
  271. // Update the timestamp of the last event
  272. this._sessionExpiry = Math.floor((Date.now() / 1000) + SESSION_TIMEOUT);
  273. // Session is active
  274. if (this._session !== null)
  275. return this._session;
  276. // Mutter's RemoteDesktop is not available, fall back to Atspi
  277. if (this.connection === null) {
  278. debug('Falling back to Atspi');
  279. this._session = new AtspiController();
  280. return this._session;
  281. }
  282. try {
  283. // Mutter is available and there isn't another session starting
  284. if (this._sessionStarting === false) {
  285. this._sessionStarting = true;
  286. debug('Creating Mutter RemoteDesktop session');
  287. // This takes three steps: creating the remote desktop session,
  288. // starting the session, and creating a screencast session for
  289. // the remote desktop session.
  290. const objectPath = await this._createRemoteDesktopSession();
  291. this._session = new RemoteSession(objectPath);
  292. await this._session.start();
  293. // Watch for the session ending
  294. this._sessionClosedId = this._session.connect(
  295. 'closed',
  296. this._onSessionClosed.bind(this)
  297. );
  298. if (this._sessionExpiryId === 0) {
  299. this._sessionExpiryId = GLib.timeout_add_seconds(
  300. GLib.PRIORITY_DEFAULT,
  301. SESSION_TIMEOUT,
  302. this._onSessionExpired.bind(this)
  303. );
  304. }
  305. this._sessionStarting = false;
  306. }
  307. return this._session;
  308. } catch (e) {
  309. logError(e);
  310. if (this._session !== null) {
  311. this._session.destroy();
  312. this._session = null;
  313. }
  314. this._sessionStarting = false;
  315. throw e;
  316. }
  317. }
  318. /*
  319. * Pointer Events
  320. */
  321. movePointer(dx, dy) {
  322. if (dx === 0 && dy === 0)
  323. return;
  324. this._ensureAdapter()
  325. .then(session => session?.movePointer(dx, dy))
  326. .catch(e => debug(e));
  327. }
  328. pressPointer(button) {
  329. this._ensureAdapter()
  330. .then(session => session?.pressPointer(button))
  331. .catch(e => debug(e));
  332. }
  333. releasePointer(button) {
  334. this._ensureAdapter()
  335. .then(session => session?.releasePointer(button))
  336. .catch(e => debug(e));
  337. }
  338. clickPointer(button) {
  339. this._ensureAdapter()
  340. .then(session => session?.clickPointer(button))
  341. .catch(e => debug(e));
  342. }
  343. doubleclickPointer(button) {
  344. this._ensureAdapter()
  345. .then(session => session?.doubleclickPointer(button))
  346. .catch(e => debug(e));
  347. }
  348. scrollPointer(dx, dy) {
  349. if (dx === 0 && dy === 0)
  350. return;
  351. this._ensureAdapter()
  352. .then(session => session?.scrollPointer(dx, dy))
  353. .catch(e => debug(e));
  354. }
  355. /*
  356. * Keyboard Events
  357. */
  358. pressKeysym(keysym) {
  359. this._ensureAdapter()
  360. .then(session => session?.pressKeysym(keysym))
  361. .catch(e => debug(e));
  362. }
  363. releaseKeysym(keysym) {
  364. this._ensureAdapter()
  365. .then(session => session?.releaseKeysym(keysym))
  366. .catch(e => debug(e));
  367. }
  368. pressreleaseKeysym(keysym) {
  369. this._ensureAdapter()
  370. .then(session => session?.pressreleaseKeysym(keysym))
  371. .catch(e => debug(e));
  372. }
  373. /*
  374. * High-level keyboard input
  375. */
  376. pressKeys(input, modifiers) {
  377. this._ensureAdapter()
  378. .then(session => {
  379. if (typeof input === 'string') {
  380. for (let i = 0; i < input.length; i++)
  381. session?.pressKey(input[i], modifiers);
  382. } else {
  383. session?.pressKey(input, modifiers);
  384. }
  385. }).catch(e => debug(e));
  386. }
  387. destroy() {
  388. if (this._session !== null) {
  389. // Disconnect from the session
  390. if (this._sessionClosedId > 0) {
  391. this._session.disconnect(this._sessionClosedId);
  392. this._sessionClosedId = 0;
  393. }
  394. this._session.destroy();
  395. this._session = null;
  396. }
  397. if (this._nameWatcherId > 0) {
  398. Gio.bus_unwatch_name(this._nameWatcherId);
  399. this._nameWatcherId = 0;
  400. }
  401. }
  402. }