ee59e68c677de5b79b1089b0711aaf7a36571475
[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-2008 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
39 #ifdef XFT
40 #include <X11/Xft/Xft.h>
41 int use_xft = 0;
42 #endif
43
44 #ifdef HAVE_XDBE
45 int use_xdbe;
46 #endif
47
48 /* some basic X11 stuff */
49 Display *display;
50 int display_width;
51 int display_height;
52 int screen;
53 static int set_transparent;
54 static int background_colour;
55
56 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
57 int workarea[4];
58
59 /* Window stuff */
60 struct conky_window window;
61
62 /* local prototypes */
63 static void update_workarea(void);
64 static Window find_desktop_window(Window *p_root, Window *p_desktop);
65 static Window find_subwindow(Window win, int w, int h);
66
67 /* X11 initializer */
68 void init_X11(void)
69 {
70         if ((display = XOpenDisplay(0)) == NULL) {
71                 CRIT_ERR("can't open display: %s", XDisplayName(0));
72         }
73
74         screen = DefaultScreen(display);
75         display_width = DisplayWidth(display, screen);
76         display_height = DisplayHeight(display, screen);
77
78         update_workarea();
79 }
80
81 static void update_workarea(void)
82 {
83         Window root = RootWindow(display, screen);
84         unsigned long nitems, bytes;
85         unsigned char *buf = NULL;
86         Atom type;
87         int format;
88
89         /* default work area is display */
90         workarea[0] = 0;
91         workarea[1] = 0;
92         workarea[2] = display_width;
93         workarea[3] = display_height;
94
95         /* get current desktop */
96         if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP), 0, 1,
97                         False, XA_CARDINAL, &type, &format, &nitems, &bytes, &buf)
98                         == Success && type == XA_CARDINAL && nitems > 0) {
99
100                 // Currently unused
101                 /* long desktop = *(long *) buf; */
102
103                 XFree(buf);
104                 buf = 0;
105         }
106
107         if (buf) {
108                 XFree(buf);
109                 buf = 0;
110         }
111 }
112
113 /* Find root window and desktop window.
114  * Return desktop window on success,
115  * and set root and desktop byref return values.
116  * Return 0 on failure. */
117 static Window find_desktop_window(Window *p_root, Window *p_desktop)
118 {
119         Atom type;
120         int format, i;
121         unsigned long nitems, bytes;
122         unsigned int n;
123         Window root = RootWindow(display, screen);
124         Window win = root;
125         Window troot, parent, *children;
126         unsigned char *buf = NULL;
127
128         if (!p_root || !p_desktop) {
129                 return 0;
130         }
131
132         /* some window managers set __SWM_VROOT to some child of root window */
133
134         XQueryTree(display, root, &troot, &parent, &children, &n);
135         for (i = 0; i < (int) n; i++) {
136                 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
137                                 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
138                                 == Success && type == XA_WINDOW) {
139                         win = *(Window *) buf;
140                         XFree(buf);
141                         XFree(children);
142                         fprintf(stderr,
143                                 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
144                                 win);
145                         fflush(stderr);
146                         *p_root = win;
147                         *p_desktop = win;
148                         return win;
149                 }
150
151                 if (buf) {
152                         XFree(buf);
153                         buf = 0;
154                 }
155         }
156         XFree(children);
157
158         /* get subwindows from root */
159         win = find_subwindow(root, -1, -1);
160
161         update_workarea();
162
163         win = find_subwindow(win, workarea[2], workarea[3]);
164
165         if (buf) {
166                 XFree(buf);
167                 buf = 0;
168         }
169
170         if (win != root) {
171                 fprintf(stderr,
172                         PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
173                         win, root);
174         } else {
175                 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
176         }
177
178         fflush(stderr);
179
180         *p_root = root;
181         *p_desktop = win;
182
183         return win;
184 }
185
186 /* sets background to ParentRelative for the Window and all parents */
187 void set_transparent_background(Window win)
188 {
189         static int colour_set = -1;
190
191         if (set_transparent) {
192                 Window parent = win;
193                 unsigned int i;
194
195                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
196                         Window r, *children;
197                         unsigned int n;
198
199                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
200
201                         XQueryTree(display, parent, &r, &parent, &children, &n);
202                         XFree(children);
203                 }
204         } else if (colour_set != background_colour) {
205                 XSetWindowBackground(display, win, background_colour);
206                 colour_set = background_colour;
207         }
208         // XClearWindow(display, win); not sure why this was here
209 }
210
211 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
212                 char **argv, int argc)
213 {
214         /* There seems to be some problems with setting transparent background
215          * (on fluxbox this time). It doesn't happen always and I don't know why it
216          * happens but I bet the bug is somewhere here. */
217         set_transparent = set_trans;
218         background_colour = back_colour;
219
220 #ifdef OWN_WINDOW
221         if (own_window) {
222
223                 if (!find_desktop_window(&window.root, &window.desktop)) {
224                         return;
225                 }
226
227                 if (window.type == TYPE_OVERRIDE) {
228
229                         /* An override_redirect True window.
230                          * No WM hints or button processing needed. */
231                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
232                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
233                                 True, 0, 0 };
234
235                         /* Parent is desktop window (which might be a child of root) */
236                         window.window = XCreateWindow(display, window.desktop, window.x,
237                                 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
238                                 CWBackPixel | CWOverrideRedirect, &attrs);
239
240                         XLowerWindow(display, window.window);
241
242                         fprintf(stderr, PACKAGE_NAME": window type - override\n");
243                         fflush(stderr);
244                 } else { /* window.type != TYPE_OVERRIDE */
245
246                         /* A window managed by the window manager.
247                          * Process hints and buttons. */
248                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
249                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
250                                 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
251
252                         XClassHint classHint;
253                         XWMHints wmHint;
254                         Atom xa;
255
256                         if (window.type == TYPE_DOCK) {
257                                 window.x = window.y = 0;
258                         }
259                         /* Parent is root window so WM can take control */
260                         window.window = XCreateWindow(display, window.root, window.x,
261                                 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
262                                 CWBackPixel | CWOverrideRedirect, &attrs);
263
264                         classHint.res_name = window.class_name;
265                         classHint.res_class = classHint.res_name;
266
267                         wmHint.flags = InputHint | StateHint;
268                         /* allow decorated windows to be given input focus by WM */
269                         wmHint.input =
270                                 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
271                         wmHint.initial_state = ((window.type == TYPE_DOCK) ?
272                                                 WithdrawnState : NormalState);
273
274                         XmbSetWMProperties(display, window.window, window.title, NULL, argv,
275                                 argc, NULL, &wmHint, &classHint);
276
277                         /* Sets an empty WM_PROTOCOLS property */
278                         XSetWMProtocols(display, window.window, NULL, 0);
279
280                         /* Set window type */
281                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
282                                 Atom prop;
283
284                                 switch (window.type) {
285                                         case TYPE_DESKTOP:
286                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
287                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
288                                                 fflush(stderr);
289                                                 break;
290                                         case TYPE_DOCK:
291                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
292                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
293                                                 fflush(stderr);
294                                                 break;
295                                         case TYPE_NORMAL:
296                                         default:
297                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
298                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
299                                                 fflush(stderr);
300                                                 break;
301                                 }
302                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
303                                         PropModeReplace, (unsigned char *) &prop, 1);
304                         }
305
306                         /* Set desired hints */
307
308                         /* Window decorations */
309                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
310                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
311                                 fflush(stderr); */
312
313                                 xa = ATOM(_MOTIF_WM_HINTS);
314                                 if (xa != None) {
315                                         long prop[5] = { 2, 0, 0, 0, 0 };
316                                         XChangeProperty(display, window.window, xa, xa, 32,
317                                                 PropModeReplace, (unsigned char *) prop, 5);
318                                 }
319                         }
320
321                         /* Below other windows */
322                         if (TEST_HINT(window.hints, HINT_BELOW)) {
323                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
324                                 fflush(stderr); */
325
326                                 xa = ATOM(_WIN_LAYER);
327                                 if (xa != None) {
328                                         long prop = 0;
329
330                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
331                                                 PropModeAppend, (unsigned char *) &prop, 1);
332                                 }
333
334                                 xa = ATOM(_NET_WM_STATE);
335                                 if (xa != None) {
336                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
337
338                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
339                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
340                                 }
341                         }
342
343                         /* Above other windows */
344                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
345                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
346                                 fflush(stderr); */
347
348                                 xa = ATOM(_WIN_LAYER);
349                                 if (xa != None) {
350                                         long prop = 6;
351
352                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
353                                                 PropModeAppend, (unsigned char *) &prop, 1);
354                                 }
355
356                                 xa = ATOM(_NET_WM_STATE);
357                                 if (xa != None) {
358                                         Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
359
360                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
361                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
362                                 }
363                         }
364
365                         /* Sticky */
366                         if (TEST_HINT(window.hints, HINT_STICKY)) {
367                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
368                                 fflush(stderr); */
369
370                                 xa = ATOM(_NET_WM_DESKTOP);
371                                 if (xa != None) {
372                                         CARD32 xa_prop = 0xFFFFFFFF;
373
374                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
375                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
376                                 }
377
378                                 xa = ATOM(_NET_WM_STATE);
379                                 if (xa != None) {
380                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
381
382                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
383                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
384                                 }
385                         }
386
387                         /* Skip taskbar */
388                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
389                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
390                                 fflush(stderr); */
391
392                                 xa = ATOM(_NET_WM_STATE);
393                                 if (xa != None) {
394                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
395
396                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
397                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
398                                 }
399                         }
400
401                         /* Skip pager */
402                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
403                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
404                                 fflush(stderr); */
405
406                                 xa = ATOM(_NET_WM_STATE);
407                                 if (xa != None) {
408                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
409
410                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
411                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
412                                 }
413                         }
414                 } /* else { window.type != TYPE_OVERRIDE */
415
416                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
417                         window.window);
418                 fflush(stderr);
419
420                 XMapWindow(display, window.window);
421         } else /* if (own_window) { */
422 #endif
423         /* root / desktop window */
424         {
425                 XWindowAttributes attrs;
426
427                 if (!window.window) {
428                         window.window = find_desktop_window(&window.root, &window.desktop);
429                 }
430
431                 if (XGetWindowAttributes(display, window.window, &attrs)) {
432                         window.width = attrs.width;
433                         window.height = attrs.height;
434                 }
435
436                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
437         }
438
439         /* Drawable is same as window. This may be changed by double buffering. */
440         window.drawable = window.window;
441
442 #ifdef HAVE_XDBE
443         if (use_xdbe) {
444                 int major, minor;
445
446                 if (!XdbeQueryExtension(display, &major, &minor)) {
447                         use_xdbe = 0;
448                 } else {
449                         window.back_buffer = XdbeAllocateBackBufferName(display,
450                                 window.window, XdbeBackground);
451                         if (window.back_buffer != None) {
452                                 window.drawable = window.back_buffer;
453                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
454                         } else {
455                                 use_xdbe = 0;
456                         }
457                 }
458                 if (!use_xdbe) {
459                         ERR("failed to set up double buffer");
460                 }
461         }
462         if (!use_xdbe) {
463                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
464         }
465 #endif
466
467         XFlush(display);
468
469         /* set_transparent_background(window.window);
470          * must be done after double buffer stuff? */
471 #ifdef OWN_WINDOW
472         /* if (own_window) {
473                 set_transparent_background(window.window);
474                 XClearWindow(display, window.window);
475         } */
476 #endif
477
478 #ifdef OWN_WINDOW
479         XSelectInput(display, window.window, ExposureMask |
480                 (own_window ? (StructureNotifyMask | PropertyChangeMask |
481                 ButtonPressMask | ButtonReleaseMask) : 0));
482 #else
483         XSelectInput(display, window.window, ExposureMask);
484 #endif
485 }
486
487 static Window find_subwindow(Window win, int w, int h)
488 {
489         unsigned int i, j;
490         Window troot, parent, *children;
491         unsigned int n;
492
493         /* search subwindows with same size as display or work area */
494
495         for (i = 0; i < 10; i++) {
496                 XQueryTree(display, win, &troot, &parent, &children, &n);
497
498                 for (j = 0; j < n; j++) {
499                         XWindowAttributes attrs;
500
501                         if (XGetWindowAttributes(display, children[j], &attrs)) {
502                                 /* Window must be mapped and same size as display or
503                                  * work space */
504                                 if (attrs.map_state != 0 && ((attrs.width == display_width
505                                                 && attrs.height == display_height)
506                                                 || (attrs.width == w && attrs.height == h))) {
507                                         win = children[j];
508                                         break;
509                                 }
510                         }
511                 }
512
513                 XFree(children);
514                 if (j == n) {
515                         break;
516                 }
517         }
518
519         return win;
520 }
521
522 long get_x11_color(const char *name)
523 {
524         XColor color;
525
526         color.pixel = 0;
527         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
528                 /* lets check if it's a hex colour with the # missing in front
529                  * if yes, then do something about it */
530                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
531
532                 newname[0] = '#';
533                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
534                 /* now lets try again */
535                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
536                                 &color)) {
537                         ERR("can't parse X color '%s'", name);
538                         return 0xFF00FF;
539                 }
540         }
541         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
542                 ERR("can't allocate X color '%s'", name);
543         }
544
545         return (long) color.pixel;
546 }
547
548 void create_gc(void)
549 {
550         XGCValues values;
551
552         values.graphics_exposures = 0;
553         values.function = GXcopy;
554         window.gc = XCreateGC(display, window.drawable,
555                 GCFunction | GCGraphicsExposures, &values);
556 }
557
558 void update_x11info(void)
559 {
560         struct information *current_info = &info;
561         current_info->x11.monitor.number = XScreenCount(display);
562         current_info->x11.monitor.current = XDefaultScreen(display);
563 }