-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPWMC.php
More file actions
292 lines (246 loc) · 7.99 KB
/
Copy pathPWMC.php
File metadata and controls
292 lines (246 loc) · 7.99 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
<?php
abstract class PWMC{
const DATA_FILE_NAME = 'PWMCNames';
const COMM_MAP = [
'firefox' => 'Firefox'
];
protected static $windows = [];
protected static $blockWait = [];
protected static $commWait = [];
protected static $searchWait = [];
protected static $scanning = false;
protected static $timeStack = [];
protected static $timeout = 10000;
protected static $timeoutStamp = 0;
protected static $waiting = false;
private static $monitors = null;
protected $wid;
protected $pid;
protected $title;
protected $comm;
protected $state = 0;
public function __construct(string $cmd){
self::setup();
}
public static function millitime(){
return round(microtime(true)*1000);
}
public static function arrayPluck(array &$array, $key){
if(!isset($array[$key])) return null;
$return = $array[$key];
unset($array[$key]);
return $return;
}
protected static function scan(){
if(self::$scanning) return;
self::$scanning = true;
$data = shell_exec('wmctrl -lp');
preg_match_all('/^(\S+)\s+\S+\s+(\S+)\s+\S+(.+)?$/m', $data, $matches, PREG_SET_ORDER);
$found = [];
$blocked = [];
$old = self::$windows;
$new = [];
foreach($matches as $match){
$info = ['wid' => intval($match[1], 16), 'pid' => intval($match[2], 10), 'title' => trim($match[3]) ?? ''];
// Test if window already excists
$win = self::arrayPluck($old, $info['wid']);
if($win !== null && $win->state == Window::STATE_CLOSED) continue;
if($win === null) $info['comm'] = self::getComm($info['pid']);
// Test if window should be blocked
if($win === null){
$matchedBlocks = [];
foreach(self::$blockWait as $block){
if(self::testFilter($info, $block[0])) $matchedBlocks[] = $block;
}
if(!empty($matchedBlocks)){
$win = self::newWindow($info['comm']);
$blocked[] = $win;
foreach($matchedBlocks as $block){
if($block[1] !== null) $win->onReady($block[1], array_merge([$win], $block[2]));
}
}
}
// If win is still null treat as new
if($win === null){
// Test if window has been created with new Window()
$win = self::arrayPluck(self::$commWait, $info['comm']);
// Test if a search is waiting for window
if($win === null){
foreach(self::$searchWait as $key => $wait){
if(self::testFilter($info, $wait[0])){
$win = $wait[1];
unset(self::$searchWait[$key]);
break;
}
}
}
// Window apears to be unhandled by script
if($win === null) $win = self::newWindow($info['comm']);
$new[] = $win;
}
foreach($info as $k => $v) $win->$k = $v;
if($win->state == Window::STATE_WAITING) self::$timeoutStamp = max((self::millitime() + self::$timeout), self::$timeoutStamp);
$win->state = Window::STATE_READY;
$found[$win->wid] = $win;
}
foreach($old as $v) $v->state = Window::STATE_CLOSED;
self::$windows = $found;
foreach(array_merge($blocked, $new) as $win) $win->runStack();
self::$scanning = false;
}
private static function newWindow($comm){
static $reflections = [];
$class = self::COMM_MAP[$comm] ?? 'Window';
if(!isset($reflections[$class])) $reflections[$class] = new ReflectionClass($class);
return $reflections[$class]->newInstanceWithoutConstructor();
}
private static function testFilter($data, array $filter){
foreach($filter as $prop => $value){
switch($prop){
case 'title';
$test = is_object($data) ? $data->title : $data['title'];
if(!preg_match('/'.$value.'/', $test)) return false;
break;
case 'pid': case 'wid':
$value = (int)$value;
case 'comm':
$test = is_object($data) ? $data->$prop : $data[$prop];
if($value !== $test) return false;
break;
default:
trigger_error('Property '.$prop.' can\'t be used in filter', E_USER_WARNING);
}
}
return true;
}
public static function block(array $filter, callable $callback = null, array $args = []){
self::setup();
self::$blockWait[] = [$filter, $callback, $args];
}
public static function search(array $filter, bool $wait = false){
self::scan();
foreach(self::$windows as $win){
if(self::testFilter($win, $filter)) return $win;
}
if(!$wait) return null;
$win = self::newWindow(isset($filter['comm']) ?? '');
self::$searchWait[] = [$filter, $win];
return $win;
}
public static function get(string $name){
self::setup();
$names = self::handleNames();
return self::$windows[$names[$name]] ?? null;
}
public static function getNames(){
self::setup();
$names = self::handleNames();
foreach($names as $k => $v) $names[$k] = self::$windows[$v];
return $names;
}
protected static function handleNames(string $name = null, int $wid = 0){
static $warned = false;
$lock = !is_null($name) ? LOCK_EX : LOCK_SH;
$file = rtrim(sys_get_temp_dir(), '/').'/'.self::DATA_FILE_NAME;
$pointer = fopen($file, 'c+');
if(!flock($pointer, $lock) && !$warned){
trigger_error('PWMC failed to get lock for data file and is running in non threadsafe mode', E_USER_WARNING);
$warned = true;
}
$data = fgets($pointer);
$data = $data == '' ? [] : unserialize(trim($data));
$names = [];
self::scan();
foreach($data as $k => $v){
if(isset(self::$windows[$v]) && $v != $wid) $names[$k] = $v;
}
if(!is_null($name)){
$names[$name] = $wid;
rewind($pointer);
ftruncate($pointer, 0);
fwrite($pointer, serialize($names));
fflush($pointer);
}
flock($pointer, LOCK_UN);
fclose($pointer);
return $names;
}
protected abstract function runStack();
public static function getComm(int $PID){
$comm = trim(shell_exec('ps -o comm --no-headers -p '.$PID));
if(substr($comm, -1) == '-'){
$test = trim(shell_exec('which '.$comm));
if($test == '') $comm = substr($comm, 0, -1);
}
return $comm;
}
public static function start(string $cmd){
return (int)trim(shell_exec('('.$cmd.' >/dev/null 2>&1 & echo $!)'));
}
public static function getMonitors(){
if(self::$monitors === null){
$data = shell_exec('xrandr --query | grep connected | grep disconnected -v');
preg_match_all('/\s(\d+)x(\d+)\+(\d+)\+(\d+)\s/', $data, self::$monitors, PREG_SET_ORDER);
foreach(self::$monitors as &$monitor) array_shift($monitor);
usort(self::$monitors, function($a, $b){
if($a[3] != $b[3]){
$diff = $a[3] - $b[3];
$border = ($a[3] < $b[3] ? $a[1] : $b[1]) / 2;
if(abs($diff) > $border) return $diff;
}
return $a[2] <=> $b[2];
});
}
return self::$monitors;
}
public static function doin(callable $callback, int $time, array $args = []){
self::setup();
$stamp = self::millitime() + $time;
self::$timeoutStamp = max(($stamp + self::$timeout), self::$timeoutStamp);
self::$timeStack[] = [$stamp, $callback, $args];
}
private static function testWait(){
self::scan();
$wait = false;
foreach(self::$commWait as $win){
if($win->state == Window::STATE_WAITING){
$wait = true;
break;
}
}
if(!$wait){
foreach(self::$searchWait as $win){
if($win[1]->state == Window::STATE_WAITING){
$wait = true;
break;
}
}
}
self::$waiting = $wait;
if($wait) self::$timeStack[] = [self::millitime() + 250, [self::class, 'testWait'], []];
}
private static function setup(){
static $hasRun = false;
if($hasRun) return;
$hasRun = true;
self::scan();
self::$timeoutStamp = self::millitime() + self::$timeout;
register_shutdown_function(function(){
self::testWait();
$count = 0;
while(!empty(self::$timeStack) && self::millitime() < self::$timeoutStamp){
if($count != count(self::$timeStack)){
usort(self::$timeStack, function($a1, $a2){return $a1[0] <=> $a2[0];});
$count = count(self::$timeStack);
}
$action = array_shift(self::$timeStack);
$count--;
$wait = $action[0] - self::millitime();
if($wait > 0) usleep($wait * 1000);
call_user_func_array($action[1], $action[2]);
if(!self::$waiting && (!is_array($action[1]) || $action[1][0] !== self::class || $action[1][1] != 'testWait')) self::testWait();
}
if(!empty(self::$timeStack)) trigger_error('PWMC timeouted without compleating all commands', E_USER_WARNING);
});
}
}