-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_api.js
165 lines (147 loc) · 4.09 KB
/
window_api.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
const { exec } = require('child_process');
/**
* @typedef {import('./common.js').Dimension} Dimension
* @typedef {import('./common.js').Position} Position
* @typedef {import('./common.js').WindowGeometry} WindowGeometry
*/
function _handleError(error, stderr, rejectCallback)
{
if (error || stderr) {
let errorText = 'An error occured:\n';
errorText += error.message + ' ' + stderr;
rejectCallback(errorText);
}
}
exports.getCurrentWindowIds = function ()
{
const command = 'xdotool search --onlyvisible --sync --all --name --class --classname "\\S+"';
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
stdout = stdout.trim();
resolve(stdout.split('\n'));
});
});
}
exports.getWindowName = function (id)
{
const command = `xdotool getwindowname ${id}`;
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
resolve(stdout.trim());
});
});
}
exports.getCurrentFocusedWindowId = function ()
{
const command = `xdotool getwindowfocus`;
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
resolve(stdout.trim());
});
});
}
const geometry_regexp = /^Window\s.*\n\s\sPosition:\s(.*),(.*)\s\(.*\n\s\sGeometry:\s(.*)x(.*)/m;
/**
* @param {number} id
* @returns {WindowGeometry}
*/
exports.getWindowGeometry = function (id)
{
const command = `xdotool getwindowgeometry ${id}`;
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
const match = stdout.match(geometry_regexp);
const result = {
position: { left: parseInt(match[1]), top: parseInt(match[2]) },
dimension: { width: parseInt(match[3]), height: parseInt(match[4]) },
}
resolve(result);
});
});
}
exports.isWindowHidden = function (id)
{
const command = `xwininfo -wm -id ${id}`;
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
const stateText = stdout.split('Window type:')[1];
if (!stateText)
resolve(false);
else
resolve(stateText.includes('Hidden') || !stateText.includes('Normal'));
});
});
}
exports.focusNext = async function (id)
{
await raiseWindow(id);
await focusWindow(id);
await activateWindow(id);
}
function focusWindow(id)
{
const command = `xdotool windowfocus --sync ${id}`;
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
resolve();
});
});
}
function raiseWindow(id)
{
const command = `xdotool windowraise ${id}`;
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
resolve();
});
})
}
function activateWindow(id)
{
const command = `xdotool windowactivate --sync ${id}`;
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
resolve();
});
})
}
// tried this als another focusWindow-alternative,
// in order to also get the underlying desktop actived.
// (not a primary problem, but would be good)
// ...however: it does not work!
function clickWindow(id)
{
const command = `xdotool click --window ${id}`;
return new Promise((resolve, reject) =>
{
exec(command, (error, stdout, stderr) =>
{
_handleError(error, stderr, reject);
resolve();
});
})
}