1
0
mirror of https://github.com/pavlobu/deskreen.git synced 2025-05-17 07:50:17 -07:00
deskreen/app/main/ipcMainHandlers.ts

175 lines
5.7 KiB
TypeScript

import { Display, ipcMain, BrowserWindow, screen } from 'electron';
import settings from 'electron-settings';
import i18n from '../configs/i18next.config';
import ConnectedDevicesService from '../features/ConnectedDevicesService';
import SharingSession from '../features/SharingSessionService/SharingSession';
import RoomIDService from '../server/RoomIDService';
import getDeskreenGlobal from '../utils/mainProcessHelpers/getDeskreenGlobal';
import signalingServer from '../server';
import Logger from '../utils/LoggerWithFilePrefix';
import { IpcEvents } from './IpcEvents.enum';
import SharingSessionStatusEnum from '../features/SharingSessionService/SharingSessionStatusEnum';
const log = new Logger(__filename);
const v4IPGetter = require('internal-ip').v4;
export default function initIpcMainHandlers(
mainWindow: BrowserWindow,
latestVersion: string,
appVersion: string
) {
ipcMain.on('client-changed-language', async (_, newLangCode) => {
i18n.changeLanguage(newLangCode);
await settings.set('appLanguage', newLangCode);
});
ipcMain.handle('get-signaling-server-port', () => {
if (mainWindow === null) return;
mainWindow.webContents.send('sending-port-from-main', signalingServer.port);
});
ipcMain.handle('get-all-displays', () => {
return screen.getAllDisplays();
});
ipcMain.handle('get-display-size-by-display-id', (_, displayID: string) => {
const display = screen.getAllDisplays().find((d: Display) => {
return `${d.id}` === displayID;
});
if (display) {
return display.size;
}
return undefined;
});
ipcMain.handle('main-window-onbeforeunload', () => {
const deskreenGlobal = getDeskreenGlobal();
deskreenGlobal.connectedDevicesService = new ConnectedDevicesService();
deskreenGlobal.roomIDService = new RoomIDService();
deskreenGlobal.sharingSessionService.sharingSessions.forEach(
(sharingSession: SharingSession) => {
sharingSession.denyConnectionForPartner();
sharingSession.destroy();
}
);
deskreenGlobal.rendererWebrtcHelpersService.helpers.forEach(
(helperWindow) => {
helperWindow.close();
}
);
deskreenGlobal.sharingSessionService.waitingForConnectionSharingSession = null;
deskreenGlobal.rendererWebrtcHelpersService.helpers.clear();
deskreenGlobal.sharingSessionService.sharingSessions.clear();
});
ipcMain.handle('get-latest-version', () => {
return latestVersion;
});
ipcMain.handle('get-current-version', () => {
return appVersion;
});
ipcMain.handle('get-local-lan-ip', async () => {
if (
process.env.RUN_MODE === 'dev' ||
process.env.NODE_ENV === 'production'
) {
const ip = await v4IPGetter();
return ip;
}
return '255.255.255.255';
});
ipcMain.handle(IpcEvents.GetAppPath, () => {
const deskreenGlobal = getDeskreenGlobal();
return deskreenGlobal.appPath;
});
ipcMain.handle(IpcEvents.UnmarkRoomIDAsTaken, (_, roomID) => {
const deskreenGlobal = getDeskreenGlobal();
deskreenGlobal.roomIDService.unmarkRoomIDAsTaken(roomID);
});
function onDeviceConnectedCallback(device: Device): void {
getDeskreenGlobal().connectedDevicesService.setPendingConnectionDevice(
device
);
mainWindow.webContents.send(IpcEvents.SetPendingConnectionDevice, device);
}
ipcMain.handle(IpcEvents.CreateWaitingForConnectionSharingSession, () => {
getDeskreenGlobal()
.sharingSessionService.createWaitingForConnectionSharingSession()
// eslint-disable-next-line promise/always-return
.then((waitingForConnectionSharingSession) => {
waitingForConnectionSharingSession.setOnDeviceConnectedCallback(
onDeviceConnectedCallback
);
})
.catch((e) => log.error(e));
});
ipcMain.handle(IpcEvents.ResetWaitingForConnectionSharingSession, () => {
const sharingSession = getDeskreenGlobal().sharingSessionService
.waitingForConnectionSharingSession;
sharingSession?.disconnectByHostMachineUser();
sharingSession?.destroy();
sharingSession?.setStatus(SharingSessionStatusEnum.NOT_CONNECTED);
getDeskreenGlobal().sharingSessionService.sharingSessions.delete(
sharingSession?.id as string
);
getDeskreenGlobal().sharingSessionService.waitingForConnectionSharingSession = null;
});
ipcMain.handle(IpcEvents.SetDeviceConnectedStatus, () => {
if (
getDeskreenGlobal().sharingSessionService
.waitingForConnectionSharingSession !== null
) {
const sharingSession = getDeskreenGlobal().sharingSessionService
.waitingForConnectionSharingSession;
sharingSession?.setStatus(SharingSessionStatusEnum.CONNECTED);
}
});
ipcMain.handle(
IpcEvents.GetSourceDisplayIDByDesktopCapturerSourceID,
(_, sourceId) => {
return getDeskreenGlobal().desktopCapturerSourcesService.getSourceDisplayIDByDisplayCapturerSourceID(
sourceId
);
}
);
ipcMain.handle(
IpcEvents.DisconnectPeerAndDestroySharingSessionBySessionID,
(_, sessionId) => {
const sharingSession = getDeskreenGlobal().sharingSessionService.sharingSessions.get(
sessionId
);
sharingSession?.disconnectByHostMachineUser();
sharingSession?.destroy();
getDeskreenGlobal().sharingSessionService.sharingSessions.delete(
sessionId
);
}
);
ipcMain.handle(
IpcEvents.GetDesktopCapturerSourceIdBySharingSessionId,
(_, sessionId) => {
return getDeskreenGlobal().sharingSessionService.sharingSessions.get(
sessionId
)?.desktopCapturerSourceID;
}
);
ipcMain.handle(IpcEvents.GetConnectedDevices, () => {
return getDeskreenGlobal().connectedDevicesService.getDevices();
});
}