This repository has been archived by the owner on May 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
258 lines (238 loc) · 7.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// This code is a part of MagicCap which is a MPL-2.0 licensed project.
// Copyright (C) Jake Gealer <jake@gealer.email> 2019.
// Copyright (C) Rhys O'Kane <SunburntRock89@gmail.com> 2019.
// Defines the required imports.
const { ipcMain, BrowserWindow, app } = require("electron");
const uuidv4 = require('uuid/v4');
const os = require("os");
const path = require("path");
const asyncChildProcess = require("async-child-process");
const { spawn } = require("child_process");
const { get } = require("chainfetch");
const express = require("express");
// Defines all of the screenshots.
let screenshots = [];
// Defines the platform.
const platform = os.platform();
let fullPlatform = platform;
if (platform === "win32") {
fullPlatform += ".exe";
}
// Defines the HTTP servers.
const LOWEST_PORT = 63000;
const HIGHEST_PORT = 63999;
const port = Math.floor(Math.random() * (+HIGHEST_PORT - +LOWEST_PORT)) + +LOWEST_PORT;
const screenshotServer = spawn(`${__dirname}${path.sep}bin${path.sep}screenshot-display-${fullPlatform}`, [`${port}`]);
let screenshotServerKey;
screenshotServer.stdout.on("data", key => {
if (!screenshotServerKey) {
screenshotServerKey = key.toString();
}
})
const freezeServerPort = Math.floor(Math.random() * (+HIGHEST_PORT - +LOWEST_PORT)) + +LOWEST_PORT;
const freezeServer = express();
freezeServer.get("/", (req, res) => {
const key = req.query.key;
if (key !== screenshotServerKey) {
res.status(403);
res.send("Invalid key.");
} else {
const display = parseInt(req.query.display);
res.contentType("png");
res.end(screenshots[display]);
}
});
freezeServer.listen(freezeServerPort, "127.0.0.1");
// Spawns all browser windows.
const spawnWindows = displays => {
const windows = [];
for (let i of displays) {
let win = new BrowserWindow({
frame: false,
alwaysOnTop: true,
show: false,
width: i.bounds.width,
height: i.bounds.height,
webPreferences: {
nodeIntegration: true,
},
backgroundColor: "#000000",
transparent: true,
offscreen: true,
})
win.setVisibleOnAllWorkspaces(true)
win.setPosition(i.bounds.x, i.bounds.y)
win.setMovable(false)
windows.push(win)
}
return windows;
};
// Gets the displays in order.
const getOrderedDisplays = () => {
const electronScreen = require("electron").screen;
return electronScreen.getAllDisplays().sort((a, b) => {
let sub = a.bounds.x - b.bounds.x;
if (sub === 0) {
if (a.bounds.y > b.bounds.y) {
sub -= 1;
} else {
sub += 1;
}
}
return sub;
});
};
// Defines all of the spawned windows.
let spawnedWindows;
const appPrep = () => {
if (platform !== "linux") {
const spawnEm = () => {
const displays = getOrderedDisplays();
spawnedWindows = spawnWindows(displays);
};
spawnEm();
const electronScreen = require("electron").screen;
electronScreen.on("display-added", spawnEm);
electronScreen.on("display-removed", spawnEm);
}
};
app.on("ready", appPrep);
// Gets the values of a object.
const values = item => {
const x = [];
for (const i in item) {
x.push(item[i]);
}
return x;
};
// Defines if the selector is active.
let selectorActive = false;
// Opens the region selector.
module.exports = async buttons => {
if (selectorActive) {
return;
}
const electronScreen = require("electron").screen;
const displays = getOrderedDisplays();
let primaryId = 0;
const x = electronScreen.getPrimaryDisplay().id;
for (const display of displays) {
if (display.id === x) {
primary = display;
break;
}
primaryId += 1;
}
const activeWindows = [];
if (os.platform() === "darwin") {
const { stdout } = await asyncChildProcess.execAsync(`"${__dirname}${path.sep}bin${path.sep}get-visible-windows-darwin"`);
const windowsSplit = stdout.trim().split("\n");
for (const window of windowsSplit) {
const intRectangleParts = [];
for (textRectangle of window.split(" ")) {
intRectangleParts.push(parseInt(textRectangle));
}
activeWindows.push({
x: intRectangleParts[0],
y: intRectangleParts[1],
width: intRectangleParts[2],
height: intRectangleParts[3],
});
}
}
const displayPromise = async display => {
const data = await get(`http://127.0.0.1:${port}/?key=${screenshotServerKey}&display=${display}`).toBuffer();
return data.body;
}
const promises = [];
// Shoves everything in the background.
(() => {
for (const displayId in displays) {
const promise = displayPromise(displayId);
promise;
promises.push(promise);
}
})();
screenshots = await Promise.all(promises);
let screens;
if (!spawnedWindows) {
screens = spawnWindows(displays);
} else {
screens = spawnedWindows;
}
const uuidDisplayMap = {};
for (const screenNumber in screens) {
const screen = screens[screenNumber];
screen.loadURL(`file://${__dirname}/selector.html#${screenNumber}`);
const uuid = uuidv4();
uuidDisplayMap[screenNumber] = uuid;
await ipcMain.once(`screen-${screenNumber}-load`, async() => {
await screen.webContents.send("load-reply", {
mainDisplay: screenNumber == primaryId,
key: screenshotServerKey,
port: freezeServerPort,
buttons: buttons,
displayNumber: screenNumber,
uuid: uuid,
bounds: displays[screenNumber].bounds,
activeWindows: activeWindows,
});
});
setTimeout(() => {
screen.show();
screen.setFullScreen(true);
}, 150);
ipcMain.on(`${uuid}-event-send`, (_, args) => {
for (const browser of screens) {
browser.webContents.send("event-recv", {
type: args.type,
display: screenNumber,
args: args.args,
});
}
});
}
selectorActive = true;
const r = await new Promise(res => {
ipcMain.once("screen-close", async(_, args) => {
for (const uuid of values(uuidDisplayMap)) {
await ipcMain.removeAllListeners(`${uuid}-event-send`);
}
await ipcMain.removeAllListeners("event-recv");
selectorActive = false;
const these = screens;
for (const screen of these) {
await screen.setAutoHideMenuBar(false);
await screen.setSize(0, 0);
screen.close();
}
screens = [];
if (args === undefined) {
res(null);
} else {
res({
start: {
x: args.startX,
y: args.startY,
pageX: args.startPageX,
pageY: args.startPageY,
},
end: {
x: args.endX,
y: args.endY,
pageX: args.endPageX,
pageY: args.endPageY,
},
display: args.display,
screenshots: screenshots,
activeWindows: activeWindows,
selections: args.selections,
width: args.width,
height: args.height,
})
}
})
});
appPrep();
return r;
};