forked from kohler/xwrits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.c
More file actions
408 lines (347 loc) · 10.9 KB
/
schedule.c
File metadata and controls
408 lines (347 loc) · 10.9 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <config.h>
#include "xwrits.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
static Alarm alarm_sentinel;
/*****************************************************************************/
/* Idle functions */
/* Idea and motivation from xautolock, by Stefan De Troch, detroch@imec.be, and
Michel Eyckmans (MCE), eyckmans@imec.be. However, the algorithm presented
here is a little different from theirs, and therefore probably doesn't
work! */
/* Support for Xidle is *not* included. */
struct timeval register_keystrokes_delay;
static unsigned long created_count;
static unsigned long key_press_selected_count;
int
x_error_handler(Display *d, XErrorEvent *error)
{
if (error->error_code != BadWindow && error->error_code != BadDrawable) {
char buffer[BUFSIZ];
/* avoid recursive calls to this function, in case XGetErrorText
returns an error! Problem reported by Mike Swieton
<swietonm@student.gvsu.edu> */
XSetErrorHandler(old_x_error_handler);
XGetErrorText(d, error->error_code, buffer, BUFSIZ);
fprintf(stderr, "X Error of failed request: %s\n", buffer);
abort();
}
/* Maybe someone created a window then destroyed it immediately!
I don't think there's any way of working around this. */
if (error->error_code == BadWindow) {
Window window = (Window)error->resourceid;
unschedule_data(A_IDLE_SELECT, (void *) window);
}
return 0;
}
void
watch_keystrokes(Port *port, Window w, const struct timeval *now)
{
Display *display = port->display;
Window root, parent, *children;
unsigned i, nchildren;
Alarm *a;
/* Don't pay attention to our own windows */
if (window_to_hand(port, w, 1))
return;
if (XQueryTree(display, w, &root, &parent, &children, &nchildren) == 0)
return; /* the window doesn't exist */
XSelectInput(display, w, SubstructureNotifyMask);
created_count++;
if (verbose)
fprintf(stderr, "Window 0x%x: watching for subwindows\n", (unsigned)w);
/* This code ensures that at least register_keystrokes_delay elapses before
we listen for KeyPress events on the window. We want to wait so we can
make sure the window selects them first. */
a = new_alarm_data(A_IDLE_SELECT, (void *)w, (void *)port);
xwADDTIME(a->timer, *now, register_keystrokes_delay);
schedule(a);
for (i = 0; i < nchildren; i++)
watch_keystrokes(port, children[i], now);
if (children) XFree(children);
}
void
register_keystrokes(Port *port, Window w)
{
/* Before I only selected KeyPress if someone else had selected KeyPress on
the window (indicated by the 'or' of all_event_masks and
do_not_propagate_mask). That seems obviously wrong! What if they only
selected KeyRelease? What if they selected KeyPress later? So I've
removed it. Hopefully this won't cause any strange behavior. */
/* Even though I didn't test long enough to see if it causes strange
behavior, I'm going back to the old method (we select KeyPress only if
someone else could receive it); this is a good idea because there are so
many window manager windows which otherwise we would select events on,
and not selecting events on 'em would seem to help performance. */
XWindowAttributes attr;
XClassHint class;
Window peer;
if (XGetWindowAttributes(port->display, w, &attr) == 0)
return;
if (XGetClassHint(port->display, w, &class) == 0)
return;
/* check if this is an xwrits window */
peer = check_xwrits_window(port, w);
if (peer)
add_peer(port, peer);
if (attr.root == w
|| ((attr.all_event_masks | attr.do_not_propagate_mask)
& (KeyPressMask | KeyReleaseMask))) {
key_press_selected_count++;
XSelectInput(port->display, w, SubstructureNotifyMask | KeyPressMask);
if (verbose)
fprintf(stderr, "Window 0x%x: (%s) listening for keystrokes\n",
(unsigned)w, class.res_class);
} else if (verbose)
fprintf(stderr, "Window 0x%x: (%s) skipping keystrokes\n",
(unsigned)w, class.res_class );
}
/*****************************************************************************/
/* Scheduling and alarm functions */
Alarm *
new_alarm_data(int action, void *data1, void *data2)
{
Alarm *a = xwNEW(Alarm);
a->action = action;
a->data1 = data1;
a->data2 = data2;
a->scheduled = 0;
return a;
}
Alarm *
grab_alarm_data(int action, void *data1, void *data2)
{
Alarm *a;
for (a = alarm_sentinel.next; a != &alarm_sentinel; a = a->next)
if (a->action == action && a->data1 == data1 && a->data2 == data2) {
a->prev->next = a->next;
a->next->prev = a->prev;
a->scheduled = 0;
return a;
}
return 0;
}
void
destroy_alarm(Alarm *a)
{
if (a->scheduled) {
a->prev->next = a->next;
a->next->prev = a->prev;
}
xfree(a);
}
void
init_scheduler(void)
{
alarm_sentinel.next = alarm_sentinel.prev = &alarm_sentinel;
}
void
schedule(Alarm *newalarm)
{
Alarm *a = alarm_sentinel.prev;
assert(!newalarm->scheduled);
while (a != &alarm_sentinel && xwTIMEGT(a->timer, newalarm->timer))
a = a->prev;
newalarm->prev = a;
newalarm->next = a->next;
a->next->prev = newalarm;
a->next = newalarm;
newalarm->scheduled = 1;
}
void
unschedule_data(int actions, void *data1)
{
Alarm *a = alarm_sentinel.next;
while (a != &alarm_sentinel) {
Alarm *n = a->next;
if ((a->action & actions) != 0 && (a->data1 == data1 || data1 == 0)) {
a->prev->next = n;
n->prev = a->prev;
xfree(a);
}
a = n;
}
}
void
looprinter(int i, int ret_val)
{
char desc[16];
if( !verbose )
return;
switch(ret_val) {
case 1:
strcpy(desc, "TRAN_WARN"); break;
case 2:
strcpy(desc, "TRAN_CANCEL"); break;
case 3:
strcpy(desc, "TRAN_FAIL"); break;
case 4:
strcpy(desc, "TRAN_REST"); break;
case 5:
strcpy(desc, "TRAN_LOCK"); break;
case 6:
strcpy(desc, "TRAN_AWAKE"); break;
default:
strcpy(desc, "UNKNOWN"); break;
}
fprintf(stderr, "TRANSITION via loopmaster %d >> %s\n", i, desc);
}
int
loopmaster(Alarmloopfunc alarm_looper, Xloopfunc x_looper)
{
struct timeval timeout, now, *timeoutptr;
fd_set xfds;
int pending, i;
int ret_val = 0;
/* 26 May 1998: Changed logic to avoid race conditions. Now we always flush
the output queue and check if there are any pending X events before
entering select() to wait for data. I wouldn't have noticed this, since
the effects were so transient, if it hadn't been for the --animate option
to gifview, which exercised this code more strenuously. */
xwGETTIME(now);
FD_ZERO(&xfds);
while (1) {
while (1) {
Alarm *a = alarm_sentinel.next;
Hand *h;
Gif_Stream *gfs;
if (a == &alarm_sentinel || xwTIMEGT(a->timer, now))
break;
h = (Hand *)a->data1;
alarm_sentinel.next = a->next;
a->next->prev = &alarm_sentinel;
a->scheduled = 0;
switch (a->action) {
case A_FLASH:
gfs = h->slideshow;
/* cycle through slides */
while (xwTIMEGEQ(now, a->timer)) {
h->slide = (h->slide + 1) % gfs->nimages;
xwADDDELAY(a->timer, a->timer, gfs->images[h->slide]->delay);
/* handle loopcount */
if (h->slide == 0 && gfs->loopcount != 0
&& ++h->loopcount > gfs->loopcount) {
h->slide = gfs->nimages - 1;
goto flash_draw;
}
}
schedule(a);
flash_draw:
if (h->mapped) draw_slide(h);
break;
case A_CLOCK:
draw_all_clocks(&now);
xwADDTIME(a->timer, a->timer, clock_tick);
schedule(a);
break;
case A_AWAKE:
ret_val = TRAN_AWAKE;
break;
case A_IDLE_SELECT:
register_keystrokes((Port *)a->data2, (Window)a->data1);
break;
case A_MOUSE: {
Window root, child;
int root_x, root_y, win_x, win_y;
unsigned mask;
int i;
for (i = 0; i < nports; i++) {
if (ports[i]->master != ports[i])
continue;
XQueryPointer(ports[i]->display, ports[i]->root_window, &root,
&child, &root_x, &root_y, &win_x, &win_y, &mask);
if (root != ports[i]->last_mouse_root
|| root_x < ports[i]->last_mouse_x - mouse_sensitivity
|| root_x > ports[i]->last_mouse_x + mouse_sensitivity
|| root_y < ports[i]->last_mouse_y - mouse_sensitivity
|| root_y > ports[i]->last_mouse_y + mouse_sensitivity) {
XEvent event;
event.type = MotionNotify; /* skeletal MotionNotify event */
if (x_looper && ports[i]->last_mouse_root)
ret_val = x_looper(&event, &now);
ports[i]->last_mouse_root = root;
ports[i]->last_mouse_x = root_x;
ports[i]->last_mouse_y = root_y;
}
}
xwADDTIME(a->timer, a->timer, check_mouse_time);
schedule(a);
break;
}
#ifdef HAVE_XSS
case A_XSS_CHECK: {
static XScreenSaverInfo* mitInfo = 0;
int idle_break = 0;
if (!mitInfo) mitInfo = XScreenSaverAllocInfo ();
for (i = 0; i < nports; i++) {
if (ports[i]->master != ports[i])
continue;
/* xautolock uses QueryInfo therefore I hope it's a performant one :) */
XScreenSaverQueryInfo (ports[i]->display, ports[i]->root_window, mitInfo);
if( mitInfo->idle < check_xss_time.tv_sec * 1000 )
idle_break++;
}
if ( x_looper && idle_break ) { /* we break on ANY port xss idle detection */
XEvent event;
event.type = MotionNotify; /* we use skeletal MotionNotify as a XSS signal */
ret_val = x_looper(&event, &now);
}
// reschedule
xwADDTIME(a->timer, a->timer, check_xss_time);
schedule(a);
break;
}
#endif
default:
if (alarm_looper)
ret_val = alarm_looper(a, &now);
break;
}
if (!a->scheduled) xfree(a);
if (ret_val != 0) {
looprinter(1, ret_val);
return ret_val;
}
}
if (alarm_sentinel.next != &alarm_sentinel) {
timeoutptr = &timeout;
xwSUBTIME(timeout, alarm_sentinel.next->timer, now);
} else
timeoutptr = 0;
for (i = pending = 0; !pending && i < nports; i++)
pending = XPending(ports[i]->display);
if (!pending) {
int result;
xfds = x_socket_set;
result = select(max_x_socket + 1, &xfds, 0, 0, timeoutptr);
}
/* Behave robustly when the system clock is adjusted backwards. The idea:
estimate the duration of the backwards jump and subtract that from
genesis_time. This will compensate for the jump in any new times
returned from xwGETTIME. */
{
struct timeval new_now;
xwGETTIME(new_now);
if (xwTIMEGT(now, new_now)) {
xwSUBTIME(new_now, now, new_now);
xwSUBTIME(genesis_time, genesis_time, new_now);
xwGETTIME(new_now);
}
now = new_now;
}
/* Handle X events. */
for (i = 0; i < nports; i++)
while (XPending(ports[i]->display)) {
XEvent event;
XNextEvent(ports[i]->display, &event);
default_x_processing(&event);
if (x_looper)
ret_val = x_looper(&event, &now);
if (ret_val != 0) {
looprinter(2, ret_val);
return ret_val;
}
}
}
}