-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.c
560 lines (472 loc) · 12.4 KB
/
ui.c
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* ui.c
*
* General UI code; basically, anything that has visible effects is
* implemented here.
*
* Copyright (c) 2008 Paulo Matias
* Copyright (c) 2004 Matthew Reppert
*
* Use of this code is permitted under the code of the GNU General
* Public License version 2 (and only version 2).
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
/*#include <X11/xpm.h>*/
#include <xembed.h>
#include "trace.h"
#include "systray.h"
#include "ui.h"
#include "hidden_list.h"
#include "configuration_files.h"
Display *main_disp;
Window main_wind, sel_wind, icon_wind, draw_wind;
char *display_string = NULL;
/*char *geometry_string = "64x64+0+0";*/
int width, height, pos_x, pos_y;
/*Pixmap bg_pixmap;*/
char *bg_data;
/*int wmaker = 1;*/
int iconsize = 14;
int loop_program = 1;
/*static GC main_gc;*/
/* kludge! */
int skip_next_leave_event=0;
int skip_next_enter_event=0;
#if 0
XRectangle active_area = {
.x = 0, .y = 0, .width = iconsize, .height = iconsize
};
static XRectangle left_button_area = {
.x = 0, .y = 0, .width = 0, .height = 0
};
static XRectangle right_button_area = {
.x = 16, .y = 0, .width = 0, .height = 0
};
#endif
/*
* wmsystray_resize
*
* This will resize the systray main window and set its SizeHints.
*/
void wmsystray_resize(int width, int height) {
XSizeHints *size_hints;
/* Unmap windows to be sure the Window Manager will be notified. */
XUnmapWindow (main_disp, icon_wind);
XUnmapWindow (main_disp, main_wind);
XFlush (main_disp);
/* Set the size hints and resize this stuff! */
size_hints = XAllocSizeHints();
size_hints->flags = PSize | PMinSize | PMaxSize;
size_hints->min_width = size_hints->max_width = size_hints->width = width;
size_hints->min_height = size_hints->max_height = size_hints->height = height;
XSetWMNormalHints (main_disp, icon_wind, size_hints);
XResizeWindow (main_disp, icon_wind, width, height);
XSetWMNormalHints (main_disp, main_wind, size_hints);
XResizeWindow (main_disp, main_wind, width, height);
/* Remap windows so the user can see them. */
XMapWindow (main_disp, main_wind);
XMapSubwindows (main_disp, icon_wind);
XFlush (main_disp);
XFree (size_hints);
}
/*
* init_ui
*
* Create the main window and get it ready for use.
*/
int init_ui(char *app_name, int argc, char **argv) {
XWMHints *wm_hints;
/*XSizeHints *size_hints;*/
XClassHint *class_hints;
int border_width;
/*int status;*/
TRACE((stderr, "ENTERING: init_ui\n"));
border_width = 0;
/*
* Try to open a connection to an X server.
*/
main_disp = XOpenDisplay(display_string);
if (main_disp == NULL) {
TRACE((stderr, "Couldn't open display %s!\n",
display_string ? display_string : ":0.0"));
return -1;
}
#if 0
size_hints = XAllocSizeHints();
XWMGeometry (main_disp, DefaultScreen(main_disp), geometry_string,
NULL, border_width, size_hints,
&size_hints->x, &size_hints->y,
&size_hints->width, &size_hints->height, &status);
#endif
/*
* Create our windows.
*/
main_wind = XCreateSimpleWindow (main_disp,
DefaultRootWindow (main_disp),
0, 0,
iconsize, iconsize,
0, 0, 0);
icon_wind = XCreateSimpleWindow (main_disp,
DefaultRootWindow (main_disp),
0, 0,
iconsize, iconsize,
0, 0, 0);
/*XFree(size_hints);*/
draw_wind = icon_wind;
#if 0
if (!wmaker)
draw_wind = main_wind;
#endif
sel_wind = XCreateSimpleWindow (main_disp, DefaultRootWindow(main_disp),
-1, -1, 1, 1, 0, 0, 0);
/*
* Set window manager hints.
*/
class_hints = XAllocClassHint();
class_hints->res_class = "WMSYSTRAY";
class_hints->res_name = app_name;
XSetClassHint (main_disp, main_wind, class_hints);
XFree (class_hints);
/*
* The icon window and SetCommand hints are necessary for icon
* window and dockapp stuff to work properly with Window Maker.
*/
wm_hints = XAllocWMHints();
wm_hints->flags = WindowGroupHint | IconWindowHint | StateHint;
wm_hints->window_group = main_wind;
wm_hints->icon_window = icon_wind;
wm_hints->initial_state = WithdrawnState;
XSetWMHints (main_disp, main_wind, wm_hints);
XFree (wm_hints);
XSetCommand (main_disp, main_wind, argv, argc);
/*
* Display windows.
*/
XMapWindow (main_disp, main_wind);
XMapSubwindows (main_disp, icon_wind);
XFlush (main_disp);
/*
* Resize the window to its default size.
*/
wmsystray_resize(iconsize, iconsize);
/*
* Set the icon window background.
*/
#if 0
status=XpmCreatePixmapFromData (main_disp, DefaultRootWindow(main_disp),
wmsystray_xpm, &bg_pixmap, NULL, NULL);
if (status != XpmSuccess || status != 0)
TRACE((stderr, "XPM loading didn't work\n"));
/* XSetWindowBackgroundPixmap (main_disp, icon_wind, bg_pixmap); */
#endif
XSetWindowBackgroundPixmap (main_disp, draw_wind, ParentRelative);
XClearWindow (main_disp, draw_wind);
XFlush (main_disp);
/*draw_ui_elements();*/
/*
* Select input events we're interested in.
*/
XSelectInput(main_disp, main_wind, VisibilityChangeMask |
StructureNotifyMask |
ExposureMask |
PropertyChangeMask |
/*
ButtonPressMask |
ButtonReleaseMask |
*/
EnterWindowMask|
LeaveWindowMask|
PointerMotionMask|
KeyPressMask |
KeyReleaseMask);
XSelectInput(main_disp, icon_wind, VisibilityChangeMask |
StructureNotifyMask |
ExposureMask |
PropertyChangeMask |
/*
ButtonPressMask |
ButtonReleaseMask |
*/
EnterWindowMask|
LeaveWindowMask|
PointerMotionMask|
KeyPressMask |
KeyReleaseMask);
TRACE((stderr, "LEAVING: init_ui\n"));
return 0;
}
/*
* cleanup_ui
*
* Clean up the main window and free related resources.
*/
void cleanup_ui() {
TRACE((stderr, "ENTERING: cleanup_ui\n"));
XUnmapWindow (main_disp, main_wind);
XDestroyWindow (main_disp, main_wind);
XDestroyWindow (main_disp, icon_wind);
XDestroyWindow (main_disp, sel_wind);
XCloseDisplay (main_disp);
TRACE((stderr, "LEAVING: cleanup_ui\n"));
}
#if 0
/*
* draw_ui_elements
*
* Draw the systray area and buttons.
*/
void draw_ui_elements() {
XCopyArea (main_disp, bg_pixmap, draw_wind,
DefaultGC(main_disp, DefaultScreen(main_disp)),
active_area.x - 1, active_area.y - 1,
active_area.width + 2, active_area.height + 2,
active_area.x - 1, active_area.y - 1);
XCopyArea (main_disp, bg_pixmap, draw_wind,
DefaultGC(main_disp, DefaultScreen(main_disp)),
left_button_area.x, left_button_area.y,
left_button_area.width, left_button_area.height,
left_button_area.x - 1, left_button_area.y - 1);
XCopyArea (main_disp, bg_pixmap, draw_wind,
DefaultGC(main_disp, DefaultScreen(main_disp)),
right_button_area.x, right_button_area.y,
right_button_area.width, right_button_area.height,
right_button_area.x - 1, right_button_area.y - 1);
XFlush (main_disp);
}
#endif
/*
* point_is_in_rect
*
* Determines whether a point is in a rectangle.
*/
int point_is_in_rect (int x, int y, XRectangle *rect) {
int ret = 0;
int x2, y2;
x2 = rect->x + rect->width;
y2 = rect->y + rect->height;
if (x >= rect->x && y >= rect->y && x <= x2 && y <= y2)
ret = 1;
return ret;
}
void print_item_info(const char* fname)
{
struct list_head *n;
struct systray_item *item;
XClassHint class_hint;
FILE *fptr;
fptr = fopen(fname, "w");
list_for_each (n, &systray_list) {
item = list_entry (n, struct systray_item, systray_list);
XGetClassHint(main_disp, item->window_id, &class_hint);
fprintf(fptr, "%s\n", class_hint.res_name);
XFree(class_hint.res_name);
XFree(class_hint.res_class);
}
fflush(fptr);
fclose(fptr);
}
/*
* wmsystray_handle_signal
*
* Handle UNIX signals.
*/
void wmsystray_handle_signal (int signum) {
switch (signum) {
case SIGUSR1:
print_item_info("/tmp/trayion-icon-list.txt");
break;
case SIGUSR2:
reload_config_files();
repaint_systray(0);
printf("reloading config files.\n");
break;
case SIGINT:
case SIGTERM:
loop_program = 0;
break;
}
}
void handle_enter_event()
{
if (!skip_next_enter_event){
show_hidden = 1;
skip_next_leave_event = 1;
repaint_systray(0);
} else {
skip_next_enter_event = 0;
}
}
void handle_leave_event()
{
if (!skip_next_leave_event){
show_hidden=0;
repaint_systray(0);
} else {
skip_next_leave_event = 0;
skip_next_enter_event = 1;
}
}
void check_pointer_inside_tray_kludge()
{
Window root_return, child_return;
int pointer_root_x, pointer_root_y, window_x, window_y;
unsigned int pointer_mask;
static int iteration=0;
XWindowAttributes attrib;
iteration++;
if (iteration%100==0) {
XQueryPointer(main_disp, icon_wind, &root_return, &child_return,
&pointer_root_x, &pointer_root_y,
&window_x, &window_y, &pointer_mask);
XGetWindowAttributes(main_disp, main_wind, &attrib);
if(!child_return && show_hidden && !skip_next_leave_event){
show_hidden = 0;
XSync(main_disp, False);
repaint_systray(0);
}
}
}
/*
* wmsystray_event_loop
*
* Handle X events.
*/
void wmsystray_event_loop() {
XEvent ev;
XWindowAttributes attrib;
#if 0
Window cover;
struct list_head *n;
#endif
struct systray_item *item;
while (loop_program) {
while (XPending(main_disp)) {
XNextEvent(main_disp, &ev);
/* TRACE((stderr, "XEVENT %x\n", ev.type)); */
/*
* There are several XEMBED events that clients may send
* to us.
*/
if (xembed_event_is_xembed_event(main_disp, &ev)) {
switch (ev.xclient.data.l[1]) {
case XEMBED_REQUEST_FOCUS:
xembed_focus_in(main_disp,
ev.xclient.window,
XEMBED_FOCUS_CURRENT);
break;
case XEMBED_FOCUS_NEXT:
break;
case XEMBED_FOCUS_PREV:
break;
case XEMBED_REGISTER_ACCELERATOR:
break;
case XEMBED_UNREGISTER_ACCELERATOR:
break;
}
continue;
}
if (event_is_systray_event(&ev)) {
handle_systray_event (&ev);
continue;
}
switch (ev.type) {
case MapRequest:
item = find_systray_item(ev.xmaprequest.window);
if (item) {
TRACE((stderr, "MRAISING: %X\n",
(unsigned int)item->window_id));
XMapRaised (main_disp, item->window_id);
xembed_window_activate (main_disp,
item->window_id);
/*
xembed_focus_in (main_disp,
item->window_id, 1);
*/
}
break;
case Expose:
/*draw_ui_elements();*/
break;
case PropertyNotify:
if (ev.xproperty.window == main_wind)
break;
TRACE((stderr, "Prop %x\n", (unsigned int)ev.xproperty.window));
item = find_systray_item (ev.xproperty.window);
if (item && systray_property_update (item) == 0)
break;
break;
case ConfigureNotify:
if (ev.xany.window == main_wind ||
ev.xany.window == icon_wind)
break;
/*
* Don't let icons resize themselves to other
* than iconsize x iconsize.
*/
XGetWindowAttributes (main_disp,
ev.xproperty.window,
&attrib);
TRACE((stderr, "Window %x trying to resize to %dx%d.\n", (unsigned int)ev.xproperty.window, attrib.width, attrib.height));
if ((attrib.height != iconsize)){
int ww;
ww = scale_item_width(attrib.width, attrib.height, iconsize);
XResizeWindow (main_disp,
ev.xproperty.window,
ww, iconsize);
wmsystray_resize(systray_total_width(), 14);
repaint_systray(0);
}
/*
cover = XCreateSimpleWindow (main_disp,
draw_wind,
0, 0,
64, 64,
0, 0, 0);
XMapWindow (main_disp, cover);
XDestroyWindow (main_disp, cover);
*/
break;
case ReparentNotify:
case UnmapNotify:
case DestroyNotify:
if (ev.xreparent.parent == main_wind ||
ev.xreparent.parent == icon_wind)
break;
item = find_systray_item (ev.xreparent.window);
if (item) {
TRACE ((stderr, "\tRemove %x\n",
(unsigned int)item->window_id));
systray_item_count--;
list_del (&item->systray_list);
repaint_systray(0);
}
break;
case KeyPress:
case KeyRelease:
/* systray_forward_event(&ev); */
break;
case FocusIn:
/* systray_focus_in(&ev); */
break;
case FocusOut:
/* systray_focus_out(&ev); */
break;
case EnterNotify:
handle_enter_event();
break;
case LeaveNotify:
handle_leave_event();
break;
case MotionNotify:
break;
}
}
usleep(100000L);
check_pointer_inside_tray_kludge();
}
}