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

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