input.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 (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. 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. 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. this._session = new AtspiController();
  281. // Mutter is available and there isn't another session starting
  282. } else if (this._sessionStarting === false) {
  283. this._sessionStarting = true;
  284. debug('Creating Mutter RemoteDesktop session');
  285. // This takes three steps: creating the remote desktop session,
  286. // starting the session, and creating a screencast session for
  287. // the remote desktop session.
  288. const objectPath = await this._createRemoteDesktopSession();
  289. this._session = new RemoteSession(objectPath);
  290. await this._session.start();
  291. // Watch for the session ending
  292. this._sessionClosedId = this._session.connect(
  293. 'closed',
  294. this._onSessionClosed.bind(this)
  295. );
  296. if (this._sessionExpiryId === 0) {
  297. this._sessionExpiryId = GLib.timeout_add_seconds(
  298. GLib.PRIORITY_DEFAULT,
  299. SESSION_TIMEOUT,
  300. this._onSessionExpired.bind(this)
  301. );
  302. }
  303. this._sessionStarting = false;
  304. }
  305. } catch (e) {
  306. logError(e);
  307. if (this._session !== null) {
  308. this._session.destroy();
  309. this._session = null;
  310. }
  311. this._sessionStarting = false;
  312. }
  313. }
  314. /*
  315. * Pointer Events
  316. */
  317. movePointer(dx, dy) {
  318. try {
  319. if (dx === 0 && dy === 0)
  320. return;
  321. this._ensureAdapter();
  322. this._session.movePointer(dx, dy);
  323. } catch (e) {
  324. debug(e);
  325. }
  326. }
  327. pressPointer(button) {
  328. try {
  329. this._ensureAdapter();
  330. this._session.pressPointer(button);
  331. } catch (e) {
  332. debug(e);
  333. }
  334. }
  335. releasePointer(button) {
  336. try {
  337. this._ensureAdapter();
  338. this._session.releasePointer(button);
  339. } catch (e) {
  340. debug(e);
  341. }
  342. }
  343. clickPointer(button) {
  344. try {
  345. this._ensureAdapter();
  346. this._session.clickPointer(button);
  347. } catch (e) {
  348. debug(e);
  349. }
  350. }
  351. doubleclickPointer(button) {
  352. try {
  353. this._ensureAdapter();
  354. this._session.doubleclickPointer(button);
  355. } catch (e) {
  356. debug(e);
  357. }
  358. }
  359. scrollPointer(dx, dy) {
  360. if (dx === 0 && dy === 0)
  361. return;
  362. try {
  363. this._ensureAdapter();
  364. this._session.scrollPointer(dx, dy);
  365. } catch (e) {
  366. debug(e);
  367. }
  368. }
  369. /*
  370. * Keyboard Events
  371. */
  372. pressKeysym(keysym) {
  373. try {
  374. this._ensureAdapter();
  375. this._session.pressKeysym(keysym);
  376. } catch (e) {
  377. debug(e);
  378. }
  379. }
  380. releaseKeysym(keysym) {
  381. try {
  382. this._ensureAdapter();
  383. this._session.releaseKeysym(keysym);
  384. } catch (e) {
  385. debug(e);
  386. }
  387. }
  388. pressreleaseKeysym(keysym) {
  389. try {
  390. this._ensureAdapter();
  391. this._session.pressreleaseKeysym(keysym);
  392. } catch (e) {
  393. debug(e);
  394. }
  395. }
  396. /*
  397. * High-level keyboard input
  398. */
  399. pressKeys(input, modifiers) {
  400. try {
  401. this._ensureAdapter();
  402. if (typeof input === 'string') {
  403. for (let i = 0; i < input.length; i++)
  404. this._session.pressKey(input[i], modifiers);
  405. } else {
  406. this._session.pressKey(input, modifiers);
  407. }
  408. } catch (e) {
  409. debug(e);
  410. }
  411. }
  412. destroy() {
  413. if (this._session !== null) {
  414. // Disconnect from the session
  415. if (this._sessionClosedId > 0) {
  416. this._session.disconnect(this._sessionClosedId);
  417. this._sessionClosedId = 0;
  418. }
  419. this._session.destroy();
  420. this._session = null;
  421. }
  422. if (this._nameWatcherId > 0) {
  423. Gio.bus_unwatch_name(this._nameWatcherId);
  424. this._nameWatcherId = 0;
  425. }
  426. }
  427. }