A whole bunch of changes, mostly Lua related.
[monky] / src / x11.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27
28 #include "config.h"
29 #include "conky.h"
30 #include "logging.h"
31 #include "common.h"
32
33 #include "x11.h"
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xmd.h>
37 #include <X11/Xutil.h>
38 #ifdef IMLIB2
39 #include "imlib2.h"
40 #endif /* IMLIB2 */
41
42 #ifdef XFT
43 #include <X11/Xft/Xft.h>
44 int use_xft = 0;
45 #endif
46
47 #ifdef HAVE_XDBE
48 int use_xdbe;
49 #endif
50
51 /* some basic X11 stuff */
52 Display *display = NULL;
53 int display_width;
54 int display_height;
55 int screen;
56 static int set_transparent;
57 static int background_colour;
58
59 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
60 int workarea[4];
61
62 /* Window stuff */
63 struct conky_window window;
64
65 /* local prototypes */
66 static void update_workarea(void);
67 static Window find_desktop_window(Window *p_root, Window *p_desktop);
68 static Window find_subwindow(Window win, int w, int h);
69
70 /* X11 initializer */
71 void init_X11(const char *disp)
72 {
73         if (!display) {
74                 if ((display = XOpenDisplay(disp)) == NULL) {
75                         CRIT_ERR("can't open display: %s", XDisplayName(0));
76                 }
77         }
78
79         screen = DefaultScreen(display);
80         display_width = DisplayWidth(display, screen);
81         display_height = DisplayHeight(display, screen);
82
83         update_workarea();
84 }
85
86 static void update_workarea(void)
87 {
88         Window root = RootWindow(display, screen);
89         unsigned long nitems, bytes;
90         unsigned char *buf = NULL;
91         Atom type;
92         int format;
93
94         /* default work area is display */
95         workarea[0] = 0;
96         workarea[1] = 0;
97         workarea[2] = display_width;
98         workarea[3] = display_height;
99
100         /* get current desktop */
101         if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP), 0, 1,
102                         False, XA_CARDINAL, &type, &format, &nitems, &bytes, &buf)
103                         == Success && type == XA_CARDINAL && nitems > 0) {
104
105                 // Currently unused
106                 /* long desktop = *(long *) buf; */
107
108                 XFree(buf);
109                 buf = 0;
110         }
111
112         if (buf) {
113                 XFree(buf);
114                 buf = 0;
115         }
116 }
117
118 /* Find root window and desktop window.
119  * Return desktop window on success,
120  * and set root and desktop byref return values.
121  * Return 0 on failure. */
122 static Window find_desktop_window(Window *p_root, Window *p_desktop)
123 {
124         Atom type;
125         int format, i;
126         unsigned long nitems, bytes;
127         unsigned int n;
128         Window root = RootWindow(display, screen);
129         Window win = root;
130         Window troot, parent, *children;
131         unsigned char *buf = NULL;
132
133         if (!p_root || !p_desktop) {
134                 return 0;
135         }
136
137         /* some window managers set __SWM_VROOT to some child of root window */
138
139         XQueryTree(display, root, &troot, &parent, &children, &n);
140         for (i = 0; i < (int) n; i++) {
141                 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
142                                 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
143                                 == Success && type == XA_WINDOW) {
144                         win = *(Window *) buf;
145                         XFree(buf);
146                         XFree(children);
147                         fprintf(stderr,
148                                 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
149                                 win);
150                         fflush(stderr);
151                         *p_root = win;
152                         *p_desktop = win;
153                         return win;
154                 }
155
156                 if (buf) {
157                         XFree(buf);
158                         buf = 0;
159                 }
160         }
161         XFree(children);
162
163         /* get subwindows from root */
164         win = find_subwindow(root, -1, -1);
165
166         update_workarea();
167
168         win = find_subwindow(win, workarea[2], workarea[3]);
169
170         if (buf) {
171                 XFree(buf);
172                 buf = 0;
173         }
174
175         if (win != root) {
176                 fprintf(stderr,
177                         PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
178                         win, root);
179         } else {
180                 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
181         }
182
183         fflush(stderr);
184
185         *p_root = root;
186         *p_desktop = win;
187
188         return win;
189 }
190
191 /* sets background to ParentRelative for the Window and all parents */
192 void set_transparent_background(Window win)
193 {
194         static int colour_set = -1;
195
196         if (set_transparent) {
197                 Window parent = win;
198                 unsigned int i;
199
200                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
201                         Window r, *children;
202                         unsigned int n;
203
204                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
205
206                         XQueryTree(display, parent, &r, &parent, &children, &n);
207                         XFree(children);
208                 }
209         } else if (colour_set != background_colour) {
210                 XSetWindowBackground(display, win, background_colour);
211                 colour_set = background_colour;
212         }
213         // XClearWindow(display, win); not sure why this was here
214 }
215
216 void destroy_window(void)
217 {
218         XDestroyWindow(display, window.window);
219         XFreeGC(display, window.gc);
220         memset(&window, 0, sizeof(struct conky_window));
221 }
222
223 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
224                 char **argv, int argc)
225 {
226         /* There seems to be some problems with setting transparent background
227          * (on fluxbox this time). It doesn't happen always and I don't know why it
228          * happens but I bet the bug is somewhere here. */
229         set_transparent = set_trans;
230         background_colour = back_colour;
231
232 #ifdef OWN_WINDOW
233         if (own_window) {
234                 if (!find_desktop_window(&window.root, &window.desktop)) {
235                         return;
236                 }
237
238                 if (window.type == TYPE_OVERRIDE) {
239
240                         /* An override_redirect True window.
241                          * No WM hints or button processing needed. */
242                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
243                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
244                                 True, 0, 0 };
245
246                         /* Parent is desktop window (which might be a child of root) */
247                         window.window = XCreateWindow(display, window.desktop, window.x,
248                                 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
249                                 CWBackPixel | CWOverrideRedirect, &attrs);
250
251                         XLowerWindow(display, window.window);
252
253                         fprintf(stderr, PACKAGE_NAME": window type - override\n");
254                         fflush(stderr);
255                 } else { /* window.type != TYPE_OVERRIDE */
256
257                         /* A window managed by the window manager.
258                          * Process hints and buttons. */
259                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
260                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
261                                 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
262
263                         XClassHint classHint;
264                         XWMHints wmHint;
265                         Atom xa;
266
267                         if (window.type == TYPE_DOCK) {
268                                 window.x = window.y = 0;
269                         }
270                         /* Parent is root window so WM can take control */
271                         window.window = XCreateWindow(display, window.root, window.x,
272                                 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
273                                 CWBackPixel | CWOverrideRedirect, &attrs);
274
275                         classHint.res_name = window.class_name;
276                         classHint.res_class = classHint.res_name;
277
278                         wmHint.flags = InputHint | StateHint;
279                         /* allow decorated windows to be given input focus by WM */
280                         wmHint.input =
281                                 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
282                         if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
283                                 wmHint.initial_state = WithdrawnState;
284                         } else {
285                                 wmHint.initial_state = NormalState;
286                         }
287
288                         XmbSetWMProperties(display, window.window, window.title, NULL, argv,
289                                 argc, NULL, &wmHint, &classHint);
290
291                         /* Sets an empty WM_PROTOCOLS property */
292                         XSetWMProtocols(display, window.window, NULL, 0);
293
294                         /* Set window type */
295                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
296                                 Atom prop;
297
298                                 switch (window.type) {
299                                         case TYPE_DESKTOP:
300                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
301                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
302                                                 fflush(stderr);
303                                                 break;
304                                         case TYPE_DOCK:
305                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
306                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
307                                                 fflush(stderr);
308                                                 break;
309                                         case TYPE_PANEL:
310                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
311                                                 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
312                                                 fflush(stderr);
313                                                 break;
314                                         case TYPE_NORMAL:
315                                         default:
316                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
317                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
318                                                 fflush(stderr);
319                                                 break;
320                                 }
321                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
322                                         PropModeReplace, (unsigned char *) &prop, 1);
323                         }
324
325                         /* Set desired hints */
326
327                         /* Window decorations */
328                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
329                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
330                                 fflush(stderr); */
331
332                                 xa = ATOM(_MOTIF_WM_HINTS);
333                                 if (xa != None) {
334                                         long prop[5] = { 2, 0, 0, 0, 0 };
335                                         XChangeProperty(display, window.window, xa, xa, 32,
336                                                 PropModeReplace, (unsigned char *) prop, 5);
337                                 }
338                         }
339
340                         /* Below other windows */
341                         if (TEST_HINT(window.hints, HINT_BELOW)) {
342                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
343                                 fflush(stderr); */
344
345                                 xa = ATOM(_WIN_LAYER);
346                                 if (xa != None) {
347                                         long prop = 0;
348
349                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
350                                                 PropModeAppend, (unsigned char *) &prop, 1);
351                                 }
352
353                                 xa = ATOM(_NET_WM_STATE);
354                                 if (xa != None) {
355                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
356
357                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
358                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
359                                 }
360                         }
361
362                         /* Above other windows */
363                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
364                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
365                                 fflush(stderr); */
366
367                                 xa = ATOM(_WIN_LAYER);
368                                 if (xa != None) {
369                                         long prop = 6;
370
371                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
372                                                 PropModeAppend, (unsigned char *) &prop, 1);
373                                 }
374
375                                 xa = ATOM(_NET_WM_STATE);
376                                 if (xa != None) {
377                                         Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
378
379                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
380                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
381                                 }
382                         }
383
384                         /* Sticky */
385                         if (TEST_HINT(window.hints, HINT_STICKY)) {
386                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
387                                 fflush(stderr); */
388
389                                 xa = ATOM(_NET_WM_DESKTOP);
390                                 if (xa != None) {
391                                         CARD32 xa_prop = 0xFFFFFFFF;
392
393                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
394                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
395                                 }
396
397                                 xa = ATOM(_NET_WM_STATE);
398                                 if (xa != None) {
399                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
400
401                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
402                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
403                                 }
404                         }
405
406                         /* Skip taskbar */
407                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
408                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
409                                 fflush(stderr); */
410
411                                 xa = ATOM(_NET_WM_STATE);
412                                 if (xa != None) {
413                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
414
415                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
416                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
417                                 }
418                         }
419
420                         /* Skip pager */
421                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
422                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
423                                 fflush(stderr); */
424
425                                 xa = ATOM(_NET_WM_STATE);
426                                 if (xa != None) {
427                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
428
429                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
430                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
431                                 }
432                         }
433                 } /* else { window.type != TYPE_OVERRIDE */
434
435                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
436                         window.window);
437                 fflush(stderr);
438
439                 XMapWindow(display, window.window);
440
441         } else /* if (own_window) { */
442 #endif
443                 /* root / desktop window */
444         {
445                 XWindowAttributes attrs;
446
447                 if (!window.window) {
448                         window.window = find_desktop_window(&window.root, &window.desktop);
449                 }
450
451                 if (XGetWindowAttributes(display, window.window, &attrs)) {
452                         window.width = attrs.width;
453                         window.height = attrs.height;
454                 }
455
456                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
457         }
458
459         /* Drawable is same as window. This may be changed by double buffering. */
460         window.drawable = window.window;
461
462 #ifdef HAVE_XDBE
463         if (use_xdbe) {
464                 int major, minor;
465
466                 if (!XdbeQueryExtension(display, &major, &minor)) {
467                         use_xdbe = 0;
468                 } else {
469                         window.back_buffer = XdbeAllocateBackBufferName(display,
470                                 window.window, XdbeBackground);
471                         if (window.back_buffer != None) {
472                                 window.drawable = window.back_buffer;
473                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
474                         } else {
475                                 use_xdbe = 0;
476                         }
477                 }
478                 if (!use_xdbe) {
479                         ERR("failed to set up double buffer");
480                 }
481         }
482         if (!use_xdbe) {
483                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
484         }
485 #endif
486         window.visual = DefaultVisual(display, DefaultScreen(display));
487         window.colourmap = DefaultColormap(display, DefaultScreen(display));
488 #ifdef IMLIB2
489         {
490                 cimlib_init(display, window.drawable, window.visual, window.colourmap);
491         }
492 #endif /* IMLIB2 */
493         XFlush(display);
494
495         /* set_transparent_background(window.window);
496          * must be done after double buffer stuff? */
497 #ifdef OWN_WINDOW
498         /* if (own_window) {
499                 set_transparent_background(window.window);
500                 XClearWindow(display, window.window);
501         } */
502 #endif
503
504         XSelectInput(display, window.window, ExposureMask
505 #ifdef OWN_WINDOW
506                 | (own_window ? (StructureNotifyMask | PropertyChangeMask |
507                 ButtonPressMask | ButtonReleaseMask) : 0)
508 #endif
509                 );
510 }
511
512 static Window find_subwindow(Window win, int w, int h)
513 {
514         unsigned int i, j;
515         Window troot, parent, *children;
516         unsigned int n;
517
518         /* search subwindows with same size as display or work area */
519
520         for (i = 0; i < 10; i++) {
521                 XQueryTree(display, win, &troot, &parent, &children, &n);
522
523                 for (j = 0; j < n; j++) {
524                         XWindowAttributes attrs;
525
526                         if (XGetWindowAttributes(display, children[j], &attrs)) {
527                                 /* Window must be mapped and same size as display or
528                                  * work space */
529                                 if (attrs.map_state != 0 && ((attrs.width == display_width
530                                                 && attrs.height == display_height)
531                                                 || (attrs.width == w && attrs.height == h))) {
532                                         win = children[j];
533                                         break;
534                                 }
535                         }
536                 }
537
538                 XFree(children);
539                 if (j == n) {
540                         break;
541                 }
542         }
543
544         return win;
545 }
546
547 long get_x11_color(const char *name)
548 {
549         XColor color;
550
551         color.pixel = 0;
552         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
553                 /* lets check if it's a hex colour with the # missing in front
554                  * if yes, then do something about it */
555                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
556
557                 newname[0] = '#';
558                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
559                 /* now lets try again */
560                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
561                                 &color)) {
562                         ERR("can't parse X color '%s'", name);
563                         return 0xFF00FF;
564                 }
565         }
566         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
567                 ERR("can't allocate X color '%s'", name);
568         }
569
570         return (long) color.pixel;
571 }
572
573 void create_gc(void)
574 {
575         XGCValues values;
576
577         values.graphics_exposures = 0;
578         values.function = GXcopy;
579         window.gc = XCreateGC(display, window.drawable,
580                 GCFunction | GCGraphicsExposures, &values);
581 }
582
583 void update_x11info(void)
584 {
585         struct information *current_info = &info;
586         current_info->x11.monitor.number = XScreenCount(display);
587         current_info->x11.monitor.current = XDefaultScreen(display);
588 }
589
590 /* reserve window manager space */
591 void set_struts(int sidenum)
592 {
593         Atom strut;
594         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
595                 /* reserve space at left, right, top, bottom */
596                 signed long sizes[12] = {0};
597                 int i;
598
599                 /* define strut depth */
600                 switch (sidenum) {
601                         case 0:
602                         {
603                                 /* left side */
604                                 sizes[0] = window.x + window.width;
605                                 break;
606                         }
607                         case 1:
608                         {
609                                 /* right side */
610                                 sizes[1] = display_width - window.x;
611                                 break;
612                         }
613                         case 2:
614                         {
615                                 /* top side */
616                                 sizes[2] = window.y + window.height;
617                                 break;
618                         }
619                         case 3:
620                         {
621                                 /* bottom side */
622                                 sizes[3] = display_height - window.y;
623                                 break;
624                         }
625                 }
626
627                 /* define partial strut length */
628                 if (sidenum <= 1) {
629                         sizes[4 + (sidenum*2)] = window.y;
630                         sizes[5 + (sidenum*2)] = window.y + window.height;
631                 } else if (sidenum <= 3) {
632                         sizes[4 + (sidenum*2)] = window.x;
633                         sizes[5 + (sidenum*2)] = window.x + window.width;
634                 }
635
636                 /* check constraints */
637                 for (i = 0; i < 12; i++) {
638                         if (sizes[i] < 0) {
639                                 sizes[i] = 0;
640                         } else {
641                                 if (i <= 1 || i >= 8) {
642                                         if (sizes[i] > display_width) {
643                                                 sizes[i] = display_width;
644                                         }
645                                 } else {
646                                         if (sizes[i] > display_height) {
647                                                 sizes[i] = display_height;
648                                         }
649                                 }
650                         }
651                 }
652
653                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
654                                 PropModeReplace, (unsigned char *) &sizes, 4);
655
656                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
657                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
658                                         PropModeReplace, (unsigned char *) &sizes, 12);
659                 }
660         }
661 }