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.js 25KB

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