-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
347 lines (327 loc) · 13.8 KB
/
index.ts
File metadata and controls
347 lines (327 loc) · 13.8 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { openStreamDeck, StreamDeck } from "elgato-stream-deck";
import * as path from "path";
import { exec } from "child_process";
import sharp from "sharp";
import * as fs from "fs";
import * as http from "http";
import * as os from "os";
import * as source_map from "source-map-support";
source_map.install();
import { OBSws } from "./obsWs";
const options = {
hostname: "192.168.178.51",
port: 80,
path: "/todos",
method: "GET",
};
const obsWS = new OBSws("ws://localhost:4444", true);
let studioMode = true;
obsWS.on("ConnectionOpened", function () {
obsWS
.send("GetStudioModeEnabled")
.then(function (data: { studioModeEnabled: boolean }) {
studioMode = data.studioModeEnabled;
// data.transitions.forEach(function (transition) {
// console.log(transition);
// });
});
const deck = new Stream();
});
obsWS.on(
"StudioModeStateChanged",
(event: { studioModeEnabled: boolean }) =>
(studioMode = event.studioModeEnabled),
);
class Stream {
deck: StreamDeck;
current_path: string;
config: { [x: string]: any };
constructor() {
this.deck = openStreamDeck();
console.log(os.homedir());
this.current_path = path.resolve(
os.homedir(),
".config/shortcut/config",
);
this.config = JSON.parse(
fs.readFileSync(this.current_path + "/" + "config.json", "utf8"),
);
this.applyConfig(this.current_path);
this.deck.on("down", (button) => {
console.log("button %d down", button);
this.handleDown(button, this.config);
});
this.deck.on("up", function (button) {
console.log("button %d up", button);
});
}
async applyConfig(config_path: string) {
obsWS.clearCallbacks();
console.log(config_path);
this.deck.clearAllKeys();
this.config = JSON.parse(
fs.readFileSync(config_path + "/" + "config.json", "utf8"),
);
for (const button in this.config) {
if (Object.hasOwnProperty.call(this.config, button)) {
const button_config = this.config[button];
// console.log(button_config);
if (Object.hasOwnProperty.call(button_config, "image")) {
this.setImage(
parseInt(button),
button_config["image"],
config_path,
);
} else if (
Object.hasOwnProperty.call(button_config, "text") &&
Object.hasOwnProperty.call(button_config, "color")
) {
console.log("first");
await this.setText(
parseInt(button),
button_config["text"],
button_config["text-size"] || 30,
button_config["color"],
);
} else if (Object.hasOwnProperty.call(button_config, "text")) {
await this.setText(
parseInt(button),
button_config["text"],
button_config["text-size"] || 30,
);
} else if (Object.hasOwnProperty.call(button_config, "color")) {
this.setColor(
parseInt(button),
button_config["color"][0],
button_config["color"][1],
button_config["color"][2],
);
}
if (Object.hasOwnProperty.call(button_config, "color-states")) {
const color_states = button_config["color-states"];
for (const state_name in color_states) {
const state = color_states[state_name];
obsWS.send(state_name).then((data) => {
for (const property in state) {
if (
Object.hasOwnProperty.call(
state,
property,
) &&
Object.hasOwnProperty.call(data, property)
) {
for (const value in state[property]) {
if (
Object.hasOwnProperty.call(
state[property],
value,
) &&
value == String(data[property])
) {
if (
Object.hasOwnProperty.call(
button_config,
"text",
)
) {
this.setText(
parseInt(button),
button_config["text"],
button_config[
"text-size"
] || 30,
state[property][value],
);
} else {
this.setColor(
parseInt(button),
state[property][value][0],
state[property][value][1],
state[property][value][2],
);
}
}
}
}
}
});
}
}
if (
Object.hasOwnProperty.call(button_config, "color-events") &&
Object.hasOwnProperty.call(button_config, "text")
) {
const color_events = button_config["color-events"];
for (const event in color_events) {
const color: [number, number, number] =
color_events[event];
obsWS.on(event, () => {
console.log(`event ${event} came in`);
this.setText(
parseInt(button),
button_config["text"],
button_config["text-size"] || 30,
[color[0], color[1], color[2]],
);
});
}
} else if (
Object.hasOwnProperty.call(button_config, "color-events")
) {
const color_events = button_config["color-events"];
for (const event in color_events) {
const color = color_events[event];
obsWS.on(event, () => {
this.setColor(
parseInt(button),
color[0],
color[1],
color[2],
);
});
}
}
}
}
}
async setImage(index: number, image: string, basedir: string) {
console.log(
`Setting image ${path.resolve(basedir, image)} to ${index}`,
);
const buffer = await sharp(path.resolve(basedir, image))
.flatten() // Eliminate alpha channel, if any.
.resize(this.deck.ICON_SIZE, this.deck.ICON_SIZE) // Scale up/down to the right size, cropping if necessary.
.raw() // Give us uncompressed RGB.
.toBuffer();
this.deck.fillImage(index, buffer);
}
async setText(
index: number,
text: string,
size: number,
color: string | [number, number, number] = "black",
) {
if (typeof color === "object") {
color = `rgb(${color[0]},${color[1]},${color[2]})`;
}
const buffer = await sharp(
Buffer.from(
`<svg>
<rect x="0" y="0" width="64" height="64" fill="${color}" />
<text x="5" y="${25 + size / 2}" font-size="${size}"` +
` font-weight="bold" font-family="mono" fill="#fff">` +
text +
`</text>
</svg>`,
),
)
.flatten() // Eliminate alpha channel, if any.
.resize(this.deck.ICON_SIZE, this.deck.ICON_SIZE) // Scale up/down to the right size, cropping if necessary.
.raw() // Give us uncompressed RGB.
.toBuffer();
this.deck.fillImage(index, buffer);
}
handleDown(button: number, config: { [x: string]: any }) {
if (Object.hasOwnProperty.call(config, button)) {
const button_config = config[String(button)];
if (Object.hasOwnProperty.call(button_config, "folder")) {
console.log(this.current_path);
this.current_path = path.resolve(
this.current_path,
button_config["folder"],
);
console.log(this.current_path);
this.applyConfig(this.current_path);
} else if (Object.hasOwnProperty.call(button_config, "command")) {
const command = button_config["command"];
console.log("executing", command);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
} else if (Object.hasOwnProperty.call(button_config, "key")) {
const key = button_config["key"];
console.log("typing", key);
exec("export DISPLAY=':0.0';xdotool key \"" + key + '"');
} else if (Object.hasOwnProperty.call(button_config, "scene")) {
const scene = button_config["scene"];
if (studioMode)
obsWS.send("SetPreviewScene", {
"scene-name": scene,
});
else
obsWS.send("SetCurrentScene", {
"scene-name": scene,
});
} else if (Object.hasOwnProperty.call(button_config, "request")) {
const request = button_config["request"];
obsWS.send(request);
} else if (
Object.hasOwnProperty.call(button_config, "toggleHide")
) {
const toggleHide = button_config["toggleHide"];
toggleHide["sourceEnabled"] = !toggleHide["sourceEnabled"];
obsWS.send("SetSceneItemRender", {
"scene-name": toggleHide["scene"],
source: toggleHide["source"],
render: toggleHide["sourceEnabled"],
});
} else if (Object.hasOwnProperty.call(button_config, "filter")) {
const filter = button_config["filter"];
filter["filterEnabled"] = !filter["filterEnabled"];
obsWS.send("SetSourceFilterVisibility", {
sourceName: filter["source"],
filterName: filter["filter"],
filterEnabled: filter["filterEnabled"],
});
} else if (
Object.hasOwnProperty.call(button_config, "transition")
) {
obsWS.send("TransitionToProgram");
} else if (Object.hasOwnProperty.call(button_config, "send")) {
const message = button_config["send"];
obsWS.send(message.message, message.args);
} else if (Object.hasOwnProperty.call(button_config, "setColor")) {
const color = button_config["setColor"];
http.request(
Object.assign(options, {
path: `/rgb/${color[0]}/${color[1]}/${color[2]}`,
}),
)
.on("error", console.log)
.end();
}
}
}
setColor(index: number, r: number, g: number, b: number) {
this.deck.fillColor(index, r, g, b);
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
console.log("done");
// process.on("exit", function (code) {
// deck.deck.clearAllKeys();
// });
// process.on("SIGINT", () => {
// console.log("sigint");
// deck.deck.clearAllKeys();
// });
// process.on("SIGUSR1", () => deck.deck.clearAllKeys());
// process.on("SIGUSR2", () => deck.deck.clearAllKeys());
/*
if (key >= 2 && key <= 11) {// numkeys
if (studioMode)
obsWS.send('SetPreviewScene', {
'scene-name': scenes[key - 2].name
});
else
obsWS.send('SetCurrentScene', {
'scene-name': scenes[key - 2].name
});
}
});
*/