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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/shape.h>
#include <time.h>
#include <math.h>
#include "img/dvdlogo.xbm"
#include "img/dvdlogo_mask.xbm"
_Static_assert(dvdlogo_width == dvdlogo_mask_width);
_Static_assert(dvdlogo_height == dvdlogo_mask_height);
#ifndef M_PI
#define M_PI 3.14159265358979323846 /* pi */
#endif
static unsigned long FILL_COLORS[] = {
0xbe00ffff,
0x00feffff,
0xff8300ff,
0x0026ffff,
0xfffa01ff,
0xff2600ff,
0xff008bff,
0x25ff01ff
};
#define NB_FILL_COLORS (sizeof(FILL_COLORS) / sizeof(unsigned long))
struct {
Display *dpy;
int scr_num;
Screen *scr;
unsigned int depth;
unsigned long wndmask;
XSetWindowAttributes wndattr;
Window rwnd, mywnd;
short refresh_rate;
struct timespec refresh_interval;
Pixmap pm[NB_FILL_COLORS];
GC gc[NB_FILL_COLORS];
Pixmap mask;
} x11;
struct {
double vel;
} param;
struct vec2 {
double x, y;
};
struct {
struct vec2 dir;
struct vec2 p1, p2;
double w, h;
size_t color_ptr;
} state;
static int main_loop (void);
static bool init_x11 (void);
static void deinit_x11 (void);
static Bool MakeAlwaysOnTop(Display* display, Window root, Window mywin);
/*
* --monitor
* --logo
* --width
* --height
*/
int main (const int argc, const char **argv) {
int ec = 0;
param.vel = 300;
state.dir.x = sin(45.0 / 360.0 * M_PI * 2.0);
state.dir.y = cos(45.0 / 360.0 * M_PI * 2.0);
state.p1.x = 0;
state.p1.y = 0;
state.p2.x = state.w = dvdlogo_width;
state.p2.y = state.h = dvdlogo_height;
if (!init_x11()) {
ec = 1;
goto END;
}
// - load xbm
// - init X11
// - Select monitor
// - do the main loop
ec = main_loop();
END:
deinit_x11();
return ec;
}
static void update_screen_info (void) {
XRRScreenConfiguration *conf = XRRGetScreenInfo(x11.dpy, x11.mywnd);
if (conf == NULL) {
return;
}
x11.refresh_rate = XRRConfigCurrentRate(conf);
XRRFreeScreenConfigInfo(conf);
if (x11.refresh_rate <= 0) {
x11.refresh_rate = 10;
}
x11.refresh_interval.tv_sec = 0;
x11.refresh_interval.tv_nsec = 1000000000 / x11.refresh_rate;
}
static void print_l18n_xopen_err (void) {
const char *LANG = getenv("LANG");
const char *msg;
if (strncmp(LANG, "ko", 2) == 0) {
msg = "X 서버 연결 실패. `ssh -X` 옵션으로 X11 포워딩을 활성화 하셨습니까?";
}
else {
msg =
"Could not open X display. "
"Did you enable X11 forwarding with `ssh -X` option?";
}
fprintf(stderr, "%s\n", msg);
}
static bool init_x11 (void) {
XSizeHints hints = { 0, };
XGCValues gcv = { 0, };
x11.dpy = XOpenDisplay(NULL);
if (x11.dpy == NULL) {
print_l18n_xopen_err();
return false;
}
// after this point, X11 will call exit() upon error.
x11.scr_num = DefaultScreen(x11.dpy); // TODO: parameterise
x11.scr = XScreenOfDisplay(x11.dpy, x11.scr_num);
x11.rwnd = RootWindowOfScreen(x11.scr);
x11.depth = DefaultDepth(x11.dpy, x11.scr_num);
x11.wndmask = CWBackingPixel | CWOverrideRedirect;
x11.wndattr.override_redirect = True;
hints.flags = PPosition | PSize;
hints.x = (int)state.p1.x;
hints.y = (int)state.p1.y;
hints.width = state.w;
hints.height = state.h;
x11.mywnd = XCreateWindow(
x11.dpy,
x11.rwnd,
0,
0,
state.w,
state.h,
0,
x11.depth,
InputOutput,
CopyFromParent,
x11.wndmask,
&x11.wndattr);
XSetNormalHints(x11.dpy, x11.mywnd, &hints);
XSelectInput(
x11.dpy,
x11.mywnd,
ExposureMask | VisibilityChangeMask | KeyPressMask);
XMapWindow(x11.dpy, x11.mywnd);
MakeAlwaysOnTop(x11.dpy, x11.rwnd, x11.mywnd);
for (size_t i = 0; i < NB_FILL_COLORS; i += 1) {
x11.pm[i] = XCreatePixmapFromBitmapData(
x11.dpy,
x11.mywnd,
dvdlogo_bits,
dvdlogo_width,
dvdlogo_height,
0x00000000,
FILL_COLORS[i],
x11.depth);
gcv.function = GXcopy;
gcv.fill_style = FillTiled;
gcv.ts_x_origin = 0;
gcv.ts_y_origin = 0;
gcv.tile = x11.pm[i];
x11.gc[i] = XCreateGC(
x11.dpy,
x11.mywnd,
GCFunction | GCTile | GCTileStipXOrigin | GCTileStipYOrigin | GCFillStyle,
&gcv);
}
x11.mask = XCreateBitmapFromData(
x11.dpy,
x11.mywnd,
dvdlogo_mask_bits,
dvdlogo_mask_width,
dvdlogo_mask_height);
XFlush(x11.dpy);
update_screen_info();
return true;
}
static void deinit_x11 (void) {
if (x11.dpy != NULL) {
XCloseDisplay(x11.dpy);
}
}
#define _NET_WM_STATE_REMOVE 0 // remove/unset property
#define _NET_WM_STATE_ADD 1 // add/set property
#define _NET_WM_STATE_TOGGLE 2 // toggle property
static Bool MakeAlwaysOnTop(Display* display, Window root, Window mywin) {
Atom wmStateAbove = XInternAtom( display, "_NET_WM_STATE_ABOVE", 1 );
if( wmStateAbove == None ) {
return False;
}
Atom wmNetWmState = XInternAtom( display, "_NET_WM_STATE", 1 );
if( wmNetWmState == None ) {
return False;
}
// set window always on top hint
if( wmStateAbove != None )
{
XClientMessageEvent xclient;
memset( &xclient, 0, sizeof (xclient) );
//
//window = the respective client window
//message_type = _NET_WM_STATE
//format = 32
//data.l[0] = the action, as listed below
//data.l[1] = first property to alter
//data.l[2] = second property to alter
//data.l[3] = source indication (0-unk,1-normal app,2-pager)
//other data.l[] elements = 0
//
xclient.type = ClientMessage;
xclient.window = mywin; // GDK_WINDOW_XID(window);
xclient.message_type = wmNetWmState; //gdk_x11_get_xatom_by_name_for_display( display, "_NET_WM_STATE" );
xclient.format = 32;
xclient.data.l[0] = _NET_WM_STATE_ADD; // add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
xclient.data.l[1] = wmStateAbove; //gdk_x11_atom_to_xatom_for_display (display, state1);
xclient.data.l[2] = 0; //gdk_x11_atom_to_xatom_for_display (display, state2);
xclient.data.l[3] = 0;
xclient.data.l[4] = 0;
//gdk_wmspec_change_state( FALSE, window,
// gdk_atom_intern_static_string ("_NET_WM_STATE_BELOW"),
// GDK_NONE );
XSendEvent(display,
//mywin - wrong, not app window, send to root window!
root, // <-- DefaultRootWindow( display )
False,
SubstructureRedirectMask | SubstructureNotifyMask,
(XEvent *)&xclient );
return True;
}
return False;
}
static void proc_key (XKeyEvent *evt, bool *loop_flag) {
int len;
char buf[2] = { 0, };
len = XLookupString(evt, buf, 1, NULL, NULL);
if (len > 0) {
switch (buf[0]) {
case 0x1B:
case 'q':
case 'Q':
*loop_flag = false;
break;
}
}
}
static void draw (void) {
XShapeCombineMask(
x11.dpy,
x11.mywnd,
ShapeBounding,
0,
0,
x11.mask,
ShapeSet);
XFillRectangle(
x11.dpy,
x11.mywnd,
x11.gc[state.color_ptr],
0,
0,
state.w,
state.h);
}
static void proc_event (bool *loop_flag, bool *redraw) {
XEvent evt;
while (XPending(x11.dpy)) {
XNextEvent(x11.dpy, &evt);
switch (evt.type) {
case Expose:
*redraw = true;
break;
case KeyPress:
proc_key((XKeyEvent*)&evt, loop_flag);
break;
}
}
}
static int get_geo_nullable (
Display *dpy,
Drawable d,
Window *out_wnd,
int *out_x,
int *out_y,
unsigned int *out_w,
unsigned int *out_h,
unsigned int *out_bw,
unsigned int *out_dph)
{
#define CHK_ASS(l, r) if ((l) != NULL) { *(l) = (r); }
Window wnd;
int x, y;
unsigned int w, h, bw, dph;
int ret;
ret = XGetGeometry(dpy, d, &wnd, &x, &y, &w, &h, &bw, &dph);
if (ret) {
CHK_ASS(out_wnd, wnd);
CHK_ASS(out_x, x);
CHK_ASS(out_y, y);
CHK_ASS(out_w, w);
CHK_ASS(out_h, h);
CHK_ASS(out_bw, bw);
CHK_ASS(out_dph, dph);
}
return ret;
#undef CHK_ASS
}
static void do_move (bool *redraw, const double v_factor) {
double dx, dy;
const unsigned int root_w = XWidthOfScreen(x11.scr);
const unsigned int root_h = XHeightOfScreen(x11.scr);
dx = state.dir.x * param.vel / x11.refresh_rate * v_factor;
dy = state.dir.y * param.vel / x11.refresh_rate * v_factor;
state.p1.x += dx;
state.p1.y += dy;
state.p2.x += dx;
state.p2.y += dy;
XMoveWindow(x11.dpy, x11.mywnd, (int)state.p1.x, (int)state.p1.y);
// north
if (state.p1.y < 0) {
state.p1.y = 0;
state.p2.y = state.h;
state.dir.y = -state.dir.y;
state.color_ptr = (state.color_ptr + 1) % NB_FILL_COLORS;
*redraw = true;
}
// east
if (state.p2.x > root_w) {
state.p2.x = root_w;
state.p1.x = root_w - state.w;
state.dir.x = -state.dir.x;
state.color_ptr = (state.color_ptr + 1) % NB_FILL_COLORS;
*redraw = true;
}
// south
if (state.p2.y > root_h) {
state.p2.y = root_h;
state.p1.y = root_h - state.h;
state.dir.y = -state.dir.y;
state.color_ptr = (state.color_ptr + 1) % NB_FILL_COLORS;
*redraw = true;
}
// west
if (state.p1.x < 0) {
state.p1.x = 0;
state.p2.x = state.w;
state.dir.x = -state.dir.x;
state.color_ptr = (state.color_ptr + 1) % NB_FILL_COLORS;
*redraw = true;
}
}
static void sub_ts (
struct timespec *out,
const struct timespec *a,
const struct timespec *b)
{
if (a->tv_nsec < b->tv_nsec) {
out->tv_sec = a->tv_sec - 1 - b->tv_sec;
out->tv_nsec = 1000000000 + a->tv_nsec - b->tv_nsec;
}
else {
out->tv_sec = a->tv_sec - b->tv_sec;
out->tv_nsec = a->tv_nsec - b->tv_nsec;
}
}
static int cmp_ts (const struct timespec *a, const struct timespec *b) {
if (a->tv_sec < b->tv_sec) {
return -1;
}
else if (a->tv_sec > b->tv_sec) {
return 1;
}
return a->tv_nsec < b->tv_nsec ? -1 : a->tv_nsec > b->tv_nsec ? 1 : 0;
}
static double ts2d (const struct timespec *x) {
return (double)x->tv_nsec / 1000000000 + x->tv_sec;
}
static int main_loop (void) {
bool loop_flag = true;
bool redraw;
struct timespec ts_now;
struct timespec ts_last_tick;
struct timespec ts_delta;
double v_factor = 1.0;
clock_gettime(CLOCK_MONOTONIC, &ts_now);
memcpy(&ts_last_tick, &ts_now, sizeof(struct timespec));
while (true) {
redraw = false;
proc_event(&loop_flag, &redraw);
if (!loop_flag) {
break;
}
if (false) {
fprintf(stderr, "v_factor: %.6lf\n", v_factor);
}
do_move(&redraw, v_factor);
if (redraw) {
draw();
}
if (false) {
// manually introduce network delay for testing
ts_delta.tv_sec = 0;
ts_delta.tv_nsec = 50000000; // 50ms
nanosleep(&ts_delta, NULL);
}
clock_gettime(CLOCK_MONOTONIC, &ts_now);
sub_ts(&ts_delta, &ts_now, &ts_last_tick);
// TODO: to alleviate stuttering,
// do PID control to keep the X11 command queue under certain limit
v_factor = 1.0 + ts2d(&ts_delta) / ts2d(&x11.refresh_interval);
if (cmp_ts(&ts_delta, &x11.refresh_interval) < 0) {
struct timespec ts_sleep;
sub_ts(&ts_sleep, &x11.refresh_interval, &ts_delta);
nanosleep(&ts_sleep, NULL);
}
clock_gettime(CLOCK_MONOTONIC, &ts_last_tick);
}
return 0;
}
|