input.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
  2. //
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. 'use strict';
  5. const Gdk = imports.gi.Gdk;
  6. const Gio = imports.gi.Gio;
  7. const GLib = imports.gi.GLib;
  8. const GObject = imports.gi.GObject;
  9. const SESSION_TIMEOUT = 15;
  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 (e) {
  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. 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. try {
  272. // Update the timestamp of the last event
  273. this._sessionExpiry = Math.floor((Date.now() / 1000) + SESSION_TIMEOUT);
  274. // Session is active
  275. if (this._session !== null)
  276. return;
  277. // Mutter's RemoteDesktop is not available, fall back to Atspi
  278. if (this.connection === null) {
  279. debug('Falling back to Atspi');
  280. const fallback = imports.service.components.atspi;
  281. this._session = new fallback.Controller();
  282. // Mutter is available and there isn't another session starting
  283. } else if (this._sessionStarting === false) {
  284. this._sessionStarting = true;
  285. debug('Creating Mutter RemoteDesktop session');
  286. // This takes three steps: creating the remote desktop session,
  287. // starting the session, and creating a screencast session for
  288. // the remote desktop session.
  289. const objectPath = await this._createRemoteDesktopSession();
  290. this._session = new RemoteSession(objectPath);
  291. await this._session.start();
  292. // Watch for the session ending
  293. this._sessionClosedId = this._session.connect(
  294. 'closed',
  295. this._onSessionClosed.bind(this)
  296. );
  297. if (this._sessionExpiryId === 0) {
  298. this._sessionExpiryId = GLib.timeout_add_seconds(
  299. GLib.PRIORITY_DEFAULT,
  300. SESSION_TIMEOUT,
  301. this._onSessionExpired.bind(this)
  302. );
  303. }
  304. this._sessionStarting = false;
  305. }
  306. } catch (e) {
  307. logError(e);
  308. if (this._session !== null) {
  309. this._session.destroy();
  310. this._session = null;
  311. }
  312. this._sessionStarting = false;
  313. }
  314. }
  315. /*
  316. * Pointer Events
  317. */
  318. movePointer(dx, dy) {
  319. try {
  320. if (dx === 0 && dy === 0)
  321. return;
  322. this._ensureAdapter();
  323. this._session.movePointer(dx, dy);
  324. } catch (e) {
  325. debug(e);
  326. }
  327. }
  328. pressPointer(button) {
  329. try {
  330. this._ensureAdapter();
  331. this._session.pressPointer(button);
  332. } catch (e) {
  333. debug(e);
  334. }
  335. }
  336. releasePointer(button) {
  337. try {
  338. this._ensureAdapter();
  339. this._session.releasePointer(button);
  340. } catch (e) {
  341. debug(e);
  342. }
  343. }
  344. clickPointer(button) {
  345. try {
  346. this._ensureAdapter();
  347. this._session.clickPointer(button);
  348. } catch (e) {
  349. debug(e);
  350. }
  351. }
  352. doubleclickPointer(button) {
  353. try {
  354. this._ensureAdapter();
  355. this._session.doubleclickPointer(button);
  356. } catch (e) {
  357. debug(e);
  358. }
  359. }
  360. scrollPointer(dx, dy) {
  361. if (dx === 0 && dy === 0)
  362. return;
  363. try {
  364. this._ensureAdapter();
  365. this._session.scrollPointer(dx, dy);
  366. } catch (e) {
  367. debug(e);
  368. }
  369. }
  370. /*
  371. * Keyboard Events
  372. */
  373. pressKeysym(keysym) {
  374. try {
  375. this._ensureAdapter();
  376. this._session.pressKeysym(keysym);
  377. } catch (e) {
  378. debug(e);
  379. }
  380. }
  381. releaseKeysym(keysym) {
  382. try {
  383. this._ensureAdapter();
  384. this._session.releaseKeysym(keysym);
  385. } catch (e) {
  386. debug(e);
  387. }
  388. }
  389. pressreleaseKeysym(keysym) {
  390. try {
  391. this._ensureAdapter();
  392. this._session.pressreleaseKeysym(keysym);
  393. } catch (e) {
  394. debug(e);
  395. }
  396. }
  397. /*
  398. * High-level keyboard input
  399. */
  400. pressKeys(input, modifiers) {
  401. try {
  402. this._ensureAdapter();
  403. if (typeof input === 'string') {
  404. for (let i = 0; i < input.length; i++)
  405. this._session.pressKey(input[i], modifiers);
  406. } else {
  407. this._session.pressKey(input, modifiers);
  408. }
  409. } catch (e) {
  410. debug(e);
  411. }
  412. }
  413. destroy() {
  414. if (this._session !== null) {
  415. // Disconnect from the session
  416. if (this._sessionClosedId > 0) {
  417. this._session.disconnect(this._sessionClosedId);
  418. this._sessionClosedId = 0;
  419. }
  420. this._session.destroy();
  421. this._session = null;
  422. }
  423. if (this._nameWatcherId > 0) {
  424. Gio.bus_unwatch_name(this._nameWatcherId);
  425. this._nameWatcherId = 0;
  426. }
  427. }
  428. }
  429. /**
  430. * The service class for this component
  431. */
  432. var Component = Controller;