-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (54 loc) · 1.73 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
const i3wm = require("i3wm");
var player = require("play-sound")((opts = { player: "aplay" }));
const path = require("path");
const fs = require("fs");
function hash(n) {
return Math.floor((Math.sin(n) + 1.0) * 43758.5453123);
}
let sounds = [];
fs.readdir(path.join(__dirname, "sounds"), function(err, files) {
if (err) throw err;
sounds = files;
console.log(files);
});
function click(x) {
let i = hash(x) % sounds.length;
player.play(
path.join(__dirname, "sounds", sounds[i]),
{ timeout: 300 },
function(err) {
console.log(sounds[i]);
if (err) throw err;
}
);
}
i3wm.Client.connect().then(client => {
console.log("Connected to i3");
client.subscribe("window", "workspace");
client.on("workspace", msg => {
let { width, height } = msg.current.rect;
click(width * height * 3);
});
client.on("window", msg => {
let { width, height } = msg.container.rect;
if (["focus", "move", "fullscreen_mode"].indexOf(msg.change) > -1) {
click(width * height);
}
});
});
/// Some cleanup stuff, maybe not needed:
function exitHandler(options, exitCode) {
i3wm.Client.disconnect(client);
if (options.cleanup) console.log("clean");
if (exitCode || exitCode === 0) console.log(exitCode);
if (options.exit) process.exit();
}
//do something when app is closing
process.on("exit", exitHandler.bind(null, { cleanup: true }));
//catches ctrl+c event
process.on("SIGINT", exitHandler.bind(null, { exit: true }));
// catches "kill pid" (for example: nodemon restart)
process.on("SIGUSR1", exitHandler.bind(null, { exit: true }));
process.on("SIGUSR2", exitHandler.bind(null, { exit: true }));
//catches uncaught exceptions
process.on("uncaughtException", exitHandler.bind(null, { exit: true }));