You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.ts 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. // @ts-ignore
  2. import $ from 'jquery';
  3. import React from 'react';
  4. import { IStore } from '../app/types';
  5. import { openDialog } from '../base/dialog/actions';
  6. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  7. import { pinParticipant } from '../base/participants/actions';
  8. import {
  9. getParticipantDisplayName,
  10. getPinnedParticipant,
  11. getVirtualScreenshareParticipantByOwnerId
  12. } from '../base/participants/functions';
  13. import { toggleScreensharing } from '../base/tracks/actions';
  14. import { getLocalDesktopTrack } from '../base/tracks/functions';
  15. import { showNotification } from '../notifications/actions';
  16. import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
  17. import { isScreenVideoShared } from '../screen-share/functions';
  18. import {
  19. CAPTURE_EVENTS,
  20. REMOTE_CONTROL_ACTIVE,
  21. SET_CONTROLLED_PARTICIPANT,
  22. SET_CONTROLLER,
  23. SET_RECEIVER_ENABLED,
  24. SET_RECEIVER_TRANSPORT,
  25. SET_REQUESTED_PARTICIPANT
  26. } from './actionTypes';
  27. // eslint-disable-next-line lines-around-comment
  28. // @ts-ignore
  29. import { RemoteControlAuthorizationDialog } from './components';
  30. import {
  31. DISCO_REMOTE_CONTROL_FEATURE,
  32. EVENTS,
  33. PERMISSIONS_ACTIONS,
  34. REMOTE_CONTROL_MESSAGE_NAME,
  35. REQUESTS
  36. } from './constants';
  37. import {
  38. getKey,
  39. getModifiers,
  40. getRemoteConrolEventCaptureArea,
  41. isRemoteControlEnabled,
  42. sendRemoteControlEndpointMessage
  43. } from './functions';
  44. import logger from './logger';
  45. /**
  46. * Listeners.
  47. */
  48. let permissionsReplyListener: Function | undefined,
  49. receiverEndpointMessageListener: Function, stopListener: Function | undefined;
  50. /**
  51. * Signals that the remote control authorization dialog should be displayed.
  52. *
  53. * @param {string} participantId - The id of the participant who is requesting
  54. * the authorization.
  55. * @returns {{
  56. * type: OPEN_DIALOG,
  57. * component: {RemoteControlAuthorizationDialog},
  58. * componentProps: {
  59. * participantId: {string}
  60. * }
  61. * }}
  62. * @public
  63. */
  64. export function openRemoteControlAuthorizationDialog(participantId: string) {
  65. return openDialog(RemoteControlAuthorizationDialog, { participantId });
  66. }
  67. /**
  68. * Sets the remote control active property.
  69. *
  70. * @param {boolean} active - The new value for the active property.
  71. * @returns {Function}
  72. */
  73. export function setRemoteControlActive(active: boolean) {
  74. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  75. const state = getState();
  76. const { active: oldActive } = state['features/remote-control'];
  77. const { conference } = state['features/base/conference'];
  78. if (active !== oldActive) {
  79. dispatch({
  80. type: REMOTE_CONTROL_ACTIVE,
  81. active
  82. });
  83. conference?.setLocalParticipantProperty('remoteControlSessionStatus', active);
  84. }
  85. };
  86. }
  87. /**
  88. * Requests permissions from the remote control receiver side.
  89. *
  90. * @param {string} userId - The user id of the participant that will be
  91. * requested.
  92. * @returns {Function}
  93. */
  94. export function requestRemoteControl(userId: string) {
  95. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  96. const state = getState();
  97. const enabled = isRemoteControlEnabled(state);
  98. if (!enabled) {
  99. return Promise.reject(new Error('Remote control is disabled!'));
  100. }
  101. dispatch(setRemoteControlActive(true));
  102. logger.log(`Requsting remote control permissions from: ${userId}`);
  103. const { conference } = state['features/base/conference'];
  104. permissionsReplyListener = (participant: any, event: any) => {
  105. dispatch(processPermissionRequestReply(participant.getId(), event));
  106. };
  107. conference?.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, permissionsReplyListener);
  108. dispatch({
  109. type: SET_REQUESTED_PARTICIPANT,
  110. requestedParticipant: userId
  111. });
  112. if (!sendRemoteControlEndpointMessage(
  113. conference,
  114. userId,
  115. {
  116. type: EVENTS.permissions,
  117. action: PERMISSIONS_ACTIONS.request
  118. })) {
  119. dispatch(clearRequest());
  120. }
  121. };
  122. }
  123. /**
  124. * Handles permission request replies on the controller side.
  125. *
  126. * @param {string} participantId - The participant that sent the request.
  127. * @param {EndpointMessage} event - The permission request event.
  128. * @returns {Function}
  129. */
  130. export function processPermissionRequestReply(participantId: string, event: any) {
  131. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  132. const state = getState();
  133. const { action, name, type } = event;
  134. const { requestedParticipant } = state['features/remote-control'].controller;
  135. if (isRemoteControlEnabled(state) && name === REMOTE_CONTROL_MESSAGE_NAME && type === EVENTS.permissions
  136. && participantId === requestedParticipant) {
  137. let descriptionKey, permissionGranted = false;
  138. switch (action) {
  139. case PERMISSIONS_ACTIONS.grant: {
  140. dispatch({
  141. type: SET_CONTROLLED_PARTICIPANT,
  142. controlled: participantId
  143. });
  144. logger.log('Remote control permissions granted!', participantId);
  145. logger.log('Starting remote control controller.');
  146. const { conference } = state['features/base/conference'];
  147. stopListener = (participant: any, stopEvent: { name: string; type: string; }) => {
  148. dispatch(handleRemoteControlStoppedEvent(participant.getId(), stopEvent));
  149. };
  150. conference?.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, stopListener);
  151. dispatch(resume());
  152. permissionGranted = true;
  153. descriptionKey = 'dialog.remoteControlAllowedMessage';
  154. break;
  155. }
  156. case PERMISSIONS_ACTIONS.deny:
  157. logger.log('Remote control permissions denied!', participantId);
  158. descriptionKey = 'dialog.remoteControlDeniedMessage';
  159. break;
  160. case PERMISSIONS_ACTIONS.error:
  161. logger.error('Error occurred on receiver side');
  162. descriptionKey = 'dialog.remoteControlErrorMessage';
  163. break;
  164. default:
  165. logger.error('Unknown reply received!');
  166. descriptionKey = 'dialog.remoteControlErrorMessage';
  167. }
  168. dispatch(clearRequest());
  169. if (!permissionGranted) {
  170. dispatch(setRemoteControlActive(false));
  171. }
  172. dispatch(showNotification({
  173. descriptionArguments: { user: getParticipantDisplayName(state, participantId) },
  174. descriptionKey,
  175. titleKey: 'dialog.remoteControlTitle'
  176. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  177. if (permissionGranted) {
  178. // the remote control permissions has been granted
  179. // pin the controlled participant
  180. const pinnedParticipant = getPinnedParticipant(state);
  181. const virtualScreenshareParticipantId = getVirtualScreenshareParticipantByOwnerId(state, participantId);
  182. const pinnedId = pinnedParticipant?.id;
  183. // @ts-ignore
  184. if (virtualScreenshareParticipantId && pinnedId !== virtualScreenshareParticipantId) {
  185. // @ts-ignore
  186. dispatch(pinParticipant(virtualScreenshareParticipantId));
  187. } else if (!virtualScreenshareParticipantId && pinnedId !== participantId) {
  188. dispatch(pinParticipant(participantId));
  189. }
  190. }
  191. } else {
  192. // different message type or another user -> ignoring the message
  193. }
  194. };
  195. }
  196. /**
  197. * Handles remote control stopped.
  198. *
  199. * @param {string} participantId - The ID of the participant that has sent the event.
  200. * @param {EndpointMessage} event - EndpointMessage event from the data channels.
  201. * @property {string} type - The function process only events with name REMOTE_CONTROL_MESSAGE_NAME.
  202. * @returns {void}
  203. */
  204. export function handleRemoteControlStoppedEvent(participantId: Object, event: { name: string; type: string; }) {
  205. return (dispatch: Function, getState: Function) => {
  206. const state = getState();
  207. const { name, type } = event;
  208. const { controlled } = state['features/remote-control'].controller;
  209. if (isRemoteControlEnabled(state) && name === REMOTE_CONTROL_MESSAGE_NAME && type === EVENTS.stop
  210. && participantId === controlled) {
  211. dispatch(stopController());
  212. }
  213. };
  214. }
  215. /**
  216. * Stops processing the mouse and keyboard events. Removes added listeners.
  217. * Enables the keyboard shortcuts. Displays dialog to notify the user that remote control session has ended.
  218. *
  219. * @param {boolean} notifyRemoteParty - If true a endpoint message to the controlled participant will be sent.
  220. * @returns {void}
  221. */
  222. export function stopController(notifyRemoteParty = false) {
  223. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  224. const state = getState();
  225. const { controlled } = state['features/remote-control'].controller;
  226. if (!controlled) {
  227. return;
  228. }
  229. const { conference } = state['features/base/conference'];
  230. if (notifyRemoteParty) {
  231. sendRemoteControlEndpointMessage(conference, controlled, {
  232. type: EVENTS.stop
  233. });
  234. }
  235. logger.log('Stopping remote control controller.');
  236. conference?.off(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, stopListener);
  237. stopListener = undefined;
  238. dispatch(pause());
  239. dispatch({
  240. type: SET_CONTROLLED_PARTICIPANT,
  241. controlled: undefined
  242. });
  243. dispatch(setRemoteControlActive(false));
  244. dispatch(showNotification({
  245. descriptionKey: 'dialog.remoteControlStopMessage',
  246. titleKey: 'dialog.remoteControlTitle'
  247. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  248. };
  249. }
  250. /**
  251. * Clears a pending permission request.
  252. *
  253. * @returns {Function}
  254. */
  255. export function clearRequest() {
  256. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  257. const { conference } = getState()['features/base/conference'];
  258. dispatch({
  259. type: SET_REQUESTED_PARTICIPANT,
  260. requestedParticipant: undefined
  261. });
  262. conference?.off(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, permissionsReplyListener);
  263. permissionsReplyListener = undefined;
  264. };
  265. }
  266. /**
  267. * Sets that transport object that is used by the receiver to communicate with the native part of the remote control
  268. * implementation.
  269. *
  270. * @param {Transport} transport - The transport to be set.
  271. * @returns {{
  272. * type: SET_RECEIVER_TRANSPORT,
  273. * transport: Transport
  274. * }}
  275. */
  276. export function setReceiverTransport(transport?: Object) {
  277. return {
  278. type: SET_RECEIVER_TRANSPORT,
  279. transport
  280. };
  281. }
  282. /**
  283. * Enables the receiver functionality.
  284. *
  285. * @returns {Function}
  286. */
  287. export function enableReceiver() {
  288. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  289. const state = getState();
  290. const { enabled } = state['features/remote-control'].receiver;
  291. if (enabled) {
  292. return;
  293. }
  294. const { connection } = state['features/base/connection'];
  295. const { conference } = state['features/base/conference'];
  296. if (!connection || !conference) {
  297. logger.error('Couldn\'t enable the remote receiver! The connection or conference instance is undefined!');
  298. return;
  299. }
  300. dispatch({
  301. type: SET_RECEIVER_ENABLED,
  302. enabled: true
  303. });
  304. connection.addFeature(DISCO_REMOTE_CONTROL_FEATURE, true);
  305. receiverEndpointMessageListener = (participant: any, message: {
  306. action: string; name: string; type: string; }) => {
  307. dispatch(endpointMessageReceived(participant.getId(), message));
  308. };
  309. conference.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, receiverEndpointMessageListener);
  310. };
  311. }
  312. /**
  313. * Disables the receiver functionality.
  314. *
  315. * @returns {Function}
  316. */
  317. export function disableReceiver() {
  318. return (dispatch: Function, getState: Function) => {
  319. const state = getState();
  320. const { enabled } = state['features/remote-control'].receiver;
  321. if (!enabled) {
  322. return;
  323. }
  324. const { connection } = state['features/base/connection'];
  325. const { conference } = state['features/base/conference'];
  326. if (!connection || !conference) {
  327. logger.error('Couldn\'t enable the remote receiver! The connection or conference instance is undefined!');
  328. return;
  329. }
  330. logger.log('Remote control receiver disabled.');
  331. dispatch({
  332. type: SET_RECEIVER_ENABLED,
  333. enabled: false
  334. });
  335. dispatch(stopReceiver(true));
  336. connection.removeFeature(DISCO_REMOTE_CONTROL_FEATURE);
  337. conference.off(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, receiverEndpointMessageListener);
  338. };
  339. }
  340. /**
  341. * Stops a remote control session on the receiver side.
  342. *
  343. * @param {boolean} [dontNotifyLocalParty] - If true - a notification about stopping
  344. * the remote control won't be displayed.
  345. * @param {boolean} [dontNotifyRemoteParty] - If true a endpoint message to the controller participant will be sent.
  346. * @returns {Function}
  347. */
  348. export function stopReceiver(dontNotifyLocalParty = false, dontNotifyRemoteParty = false) {
  349. return (dispatch: Function, getState: Function) => {
  350. const state = getState();
  351. const { receiver } = state['features/remote-control'];
  352. const { controller, transport } = receiver;
  353. if (!controller) {
  354. return;
  355. }
  356. const { conference } = state['features/base/conference'];
  357. if (!dontNotifyRemoteParty) {
  358. sendRemoteControlEndpointMessage(conference, controller, {
  359. type: EVENTS.stop
  360. });
  361. }
  362. dispatch({
  363. type: SET_CONTROLLER,
  364. controller: undefined
  365. });
  366. transport.sendEvent({
  367. name: REMOTE_CONTROL_MESSAGE_NAME,
  368. type: EVENTS.stop
  369. });
  370. dispatch(setRemoteControlActive(false));
  371. if (!dontNotifyLocalParty) {
  372. dispatch(showNotification({
  373. descriptionKey: 'dialog.remoteControlStopMessage',
  374. titleKey: 'dialog.remoteControlTitle'
  375. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  376. }
  377. };
  378. }
  379. /**
  380. * Handles only remote control endpoint messages.
  381. *
  382. * @param {string} participantId - The controller participant ID.
  383. * @param {Object} message - EndpointMessage from the data channels.
  384. * @param {string} message.name - The function processes only messages with
  385. * name REMOTE_CONTROL_MESSAGE_NAME.
  386. * @returns {Function}
  387. */
  388. export function endpointMessageReceived(participantId: string, message: {
  389. action: string; name: string; type: string; }) {
  390. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  391. const { action, name, type } = message;
  392. if (name !== REMOTE_CONTROL_MESSAGE_NAME) {
  393. return;
  394. }
  395. const state = getState();
  396. const { receiver } = state['features/remote-control'];
  397. const { enabled, transport } = receiver;
  398. if (enabled) {
  399. const { controller } = receiver;
  400. if (!controller && type === EVENTS.permissions && action === PERMISSIONS_ACTIONS.request) {
  401. dispatch(setRemoteControlActive(true));
  402. dispatch(openRemoteControlAuthorizationDialog(participantId));
  403. } else if (controller === participantId) {
  404. if (type === EVENTS.stop) {
  405. dispatch(stopReceiver(false, true));
  406. } else { // forward the message
  407. transport?.sendEvent(message);
  408. }
  409. } // else ignore
  410. } else {
  411. logger.log('Remote control message is ignored because remote control is disabled', message);
  412. }
  413. };
  414. }
  415. /**
  416. * Denies remote control access for user associated with the passed user id.
  417. *
  418. * @param {string} participantId - The id associated with the user who sent the
  419. * request for remote control authorization.
  420. * @returns {Function}
  421. */
  422. export function deny(participantId: string) {
  423. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  424. const state = getState();
  425. const { conference } = state['features/base/conference'];
  426. dispatch(setRemoteControlActive(false));
  427. sendRemoteControlEndpointMessage(conference, participantId, {
  428. type: EVENTS.permissions,
  429. action: PERMISSIONS_ACTIONS.deny
  430. });
  431. };
  432. }
  433. /**
  434. * Sends start remote control request to the native implementation.
  435. *
  436. * @returns {Function}
  437. */
  438. export function sendStartRequest() {
  439. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  440. const state = getState();
  441. const tracks = state['features/base/tracks'];
  442. const track = getLocalDesktopTrack(tracks);
  443. const { sourceId } = track?.jitsiTrack || {};
  444. const { transport } = state['features/remote-control'].receiver;
  445. if (typeof sourceId === 'undefined') {
  446. return Promise.reject(new Error('Cannot identify screen for the remote control session'));
  447. }
  448. return transport?.sendRequest({
  449. name: REMOTE_CONTROL_MESSAGE_NAME,
  450. type: REQUESTS.start,
  451. sourceId
  452. });
  453. };
  454. }
  455. /**
  456. * Grants remote control access to user associated with the passed user id.
  457. *
  458. * @param {string} participantId - The id associated with the user who sent the
  459. * request for remote control authorization.
  460. * @returns {Function}
  461. */
  462. export function grant(participantId: string) {
  463. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  464. dispatch({
  465. type: SET_CONTROLLER,
  466. controller: participantId
  467. });
  468. logger.log(`Remote control permissions granted to: ${participantId}`);
  469. let promise;
  470. const state = getState();
  471. const tracks = state['features/base/tracks'];
  472. const track = getLocalDesktopTrack(tracks);
  473. const isScreenSharing = isScreenVideoShared(state);
  474. const { sourceType } = track?.jitsiTrack || {};
  475. if (isScreenSharing && sourceType === 'screen') {
  476. promise = dispatch(sendStartRequest());
  477. } else {
  478. promise = dispatch(toggleScreensharing(
  479. true,
  480. false,
  481. { desktopSharingSources: [ 'screen' ] }
  482. )) // @ts-ignore
  483. .then(() => dispatch(sendStartRequest()));
  484. }
  485. const { conference } = state['features/base/conference'];
  486. promise
  487. .then(() => sendRemoteControlEndpointMessage(conference, participantId, {
  488. type: EVENTS.permissions,
  489. action: PERMISSIONS_ACTIONS.grant
  490. }))
  491. .catch((error: any) => {
  492. logger.error(error);
  493. sendRemoteControlEndpointMessage(conference, participantId, {
  494. type: EVENTS.permissions,
  495. action: PERMISSIONS_ACTIONS.error
  496. });
  497. dispatch(showNotification({
  498. descriptionKey: 'dialog.startRemoteControlErrorMessage',
  499. titleKey: 'dialog.remoteControlTitle'
  500. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  501. dispatch(stopReceiver(true));
  502. });
  503. };
  504. }
  505. /**
  506. * Handler for mouse click events on the controller side.
  507. *
  508. * @param {string} type - The type of event ("mousedown"/"mouseup").
  509. * @param {Event} event - The mouse event.
  510. * @returns {Function}
  511. */
  512. export function mouseClicked(type: string, event: React.MouseEvent) {
  513. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  514. const state = getState();
  515. const { conference } = state['features/base/conference'];
  516. const { controller } = state['features/remote-control'];
  517. sendRemoteControlEndpointMessage(conference, controller.controlled, {
  518. type,
  519. // @ts-ignore
  520. button: event.which
  521. });
  522. };
  523. }
  524. /**
  525. * Handles mouse moved events on the controller side.
  526. *
  527. * @param {Event} event - The mouse event.
  528. * @returns {Function}
  529. */
  530. export function mouseMoved(event: React.MouseEvent) {
  531. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  532. const area = getRemoteConrolEventCaptureArea();
  533. if (!area) {
  534. return;
  535. }
  536. const position = area.position();
  537. const state = getState();
  538. const { conference } = state['features/base/conference'];
  539. const { controller } = state['features/remote-control'];
  540. sendRemoteControlEndpointMessage(conference, controller.controlled, {
  541. type: EVENTS.mousemove,
  542. x: (event.pageX - position.left) / area.width(),
  543. y: (event.pageY - position.top) / area.height()
  544. });
  545. };
  546. }
  547. /**
  548. * Handles mouse scroll events on the controller side.
  549. *
  550. * @param {Event} event - The mouse event.
  551. * @returns {Function}
  552. */
  553. export function mouseScrolled(event: { deltaX: number; deltaY: number; }) {
  554. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  555. const state = getState();
  556. const { conference } = state['features/base/conference'];
  557. const { controller } = state['features/remote-control'];
  558. sendRemoteControlEndpointMessage(conference, controller.controlled, {
  559. type: EVENTS.mousescroll,
  560. x: event.deltaX,
  561. y: event.deltaY
  562. });
  563. };
  564. }
  565. /**
  566. * Handles key press events on the controller side..
  567. *
  568. * @param {string} type - The type of event ("keydown"/"keyup").
  569. * @param {Event} event - The key event.
  570. * @returns {Function}
  571. */
  572. export function keyPressed(type: string, event: React.KeyboardEvent) {
  573. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  574. const state = getState();
  575. const { conference } = state['features/base/conference'];
  576. const { controller } = state['features/remote-control'];
  577. sendRemoteControlEndpointMessage(conference, controller.controlled, {
  578. type,
  579. key: getKey(event),
  580. modifiers: getModifiers(event)
  581. });
  582. };
  583. }
  584. /**
  585. * Disables the keyboatd shortcuts. Starts collecting remote control
  586. * events. It can be used to resume an active remote control session which
  587. * was paused with the pause action.
  588. *
  589. * @returns {Function}
  590. */
  591. export function resume() {
  592. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  593. const area = getRemoteConrolEventCaptureArea();
  594. const state = getState();
  595. const { controller } = state['features/remote-control'];
  596. const { controlled, isCapturingEvents } = controller;
  597. if (!isRemoteControlEnabled(state) || !area || !controlled || isCapturingEvents) {
  598. return;
  599. }
  600. logger.log('Resuming remote control controller.');
  601. // FIXME: Once the keyboard shortcuts are using react/redux.
  602. APP.keyboardshortcut.enable(false);
  603. area.mousemove((event: React.MouseEvent) => {
  604. dispatch(mouseMoved(event));
  605. });
  606. area.mousedown((event: React.MouseEvent) => dispatch(mouseClicked(EVENTS.mousedown, event)));
  607. area.mouseup((event: React.MouseEvent) => dispatch(mouseClicked(EVENTS.mouseup, event)));
  608. area.dblclick((event: React.MouseEvent) => dispatch(mouseClicked(EVENTS.mousedblclick, event)));
  609. area.contextmenu(() => false);
  610. area[0].onwheel = (event: any) => {
  611. event.preventDefault();
  612. event.stopPropagation();
  613. dispatch(mouseScrolled(event));
  614. return false;
  615. };
  616. $(window).keydown((event: React.KeyboardEvent) => dispatch(keyPressed(EVENTS.keydown, event)));
  617. $(window).keyup((event: React.KeyboardEvent) => dispatch(keyPressed(EVENTS.keyup, event)));
  618. dispatch({
  619. type: CAPTURE_EVENTS,
  620. isCapturingEvents: true
  621. });
  622. };
  623. }
  624. /**
  625. * Pauses the collecting of events and enables the keyboard shortcus. But
  626. * it doesn't removes any other listeners. Basically the remote control
  627. * session will be still active after the pause action, but no events from the
  628. * controller side will be captured and sent. You can resume the collecting
  629. * of the events with the resume action.
  630. *
  631. * @returns {Function}
  632. */
  633. export function pause() {
  634. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  635. const state = getState();
  636. const { controller } = state['features/remote-control'];
  637. const { controlled, isCapturingEvents } = controller;
  638. if (!isRemoteControlEnabled(state) || !controlled || !isCapturingEvents) {
  639. return;
  640. }
  641. logger.log('Pausing remote control controller.');
  642. // FIXME: Once the keyboard shortcuts are using react/redux.
  643. APP.keyboardshortcut.enable(true);
  644. const area = getRemoteConrolEventCaptureArea();
  645. if (area) {
  646. area.off('contextmenu');
  647. area.off('dblclick');
  648. area.off('mousedown');
  649. area.off('mousemove');
  650. area.off('mouseup');
  651. area[0].onwheel = undefined;
  652. }
  653. $(window).off('keydown');
  654. $(window).off('keyup');
  655. dispatch({
  656. type: CAPTURE_EVENTS,
  657. isCapturingEvents: false
  658. });
  659. };
  660. }