Added own_window_type panel
[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         memset(&window, 0, sizeof(struct conky_window));
220 }
221
222 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
223                 char **argv, int argc)
224 {
225         /* There seems to be some problems with setting transparent background
226          * (on fluxbox this time). It doesn't happen always and I don't know why it
227          * happens but I bet the bug is somewhere here. */
228         set_transparent = set_trans;
229         background_colour = back_colour;
230
231 #ifdef OWN_WINDOW
232         if (own_window) {
233                 if (!find_desktop_window(&window.root, &window.desktop)) {
234                         return;
235                 }
236
237                 if (window.type == TYPE_OVERRIDE) {
238
239                         /* An override_redirect True window.
240                          * No WM hints or button processing needed. */
241                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
242                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
243                                 True, 0, 0 };
244
245                         /* Parent is desktop window (which might be a child of root) */
246                         window.window = XCreateWindow(display, window.desktop, window.x,
247                                 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
248                                 CWBackPixel | CWOverrideRedirect, &attrs);
249
250                         XLowerWindow(display, window.window);
251
252                         fprintf(stderr, PACKAGE_NAME": window type - override\n");
253                         fflush(stderr);
254                 } else { /* window.type != TYPE_OVERRIDE */
255
256                         /* A window managed by the window manager.
257                          * Process hints and buttons. */
258                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
259                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
260                                 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
261
262                         XClassHint classHint;
263                         XWMHints wmHint;
264                         Atom xa;
265
266                         if (window.type == TYPE_DOCK) {
267                                 window.x = window.y = 0;
268                         }
269                         /* Parent is root window so WM can take control */
270                         window.window = XCreateWindow(display, window.root, window.x,
271                                 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
272                                 CWBackPixel | CWOverrideRedirect, &attrs);
273
274                         classHint.res_name = window.class_name;
275                         classHint.res_class = classHint.res_name;
276
277                         wmHint.flags = InputHint | StateHint;
278                         /* allow decorated windows to be given input focus by WM */
279                         wmHint.input =
280                                 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
281                         if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
282                                 wmHint.initial_state = WithdrawnState;
283                         } else {
284                                 wmHint.initial_state = NormalState;
285                         }
286
287                         XmbSetWMProperties(display, window.window, window.title, NULL, argv,
288                                 argc, NULL, &wmHint, &classHint);
289
290                         /* Sets an empty WM_PROTOCOLS property */
291                         XSetWMProtocols(display, window.window, NULL, 0);
292
293                         /* Set window type */
294                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
295                                 Atom prop;
296
297                                 switch (window.type) {
298                                         case TYPE_DESKTOP:
299                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
300                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
301                                                 fflush(stderr);
302                                                 break;
303                                         case TYPE_DOCK:
304                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
305                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
306                                                 fflush(stderr);
307                                                 break;
308                                         case TYPE_PANEL:
309                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
310                                                 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
311                                                 fflush(stderr);
312                                                 break;
313                                         case TYPE_NORMAL:
314                                         default:
315                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
316                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
317                                                 fflush(stderr);
318                                                 break;
319                                 }
320                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
321                                         PropModeReplace, (unsigned char *) &prop, 1);
322                         }
323
324                         /* Set desired hints */
325
326                         /* Window decorations */
327                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
328                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
329                                 fflush(stderr); */
330
331                                 xa = ATOM(_MOTIF_WM_HINTS);
332                                 if (xa != None) {
333                                         long prop[5] = { 2, 0, 0, 0, 0 };
334                                         XChangeProperty(display, window.window, xa, xa, 32,
335                                                 PropModeReplace, (unsigned char *) prop, 5);
336                                 }
337                         }
338
339                         /* Below other windows */
340                         if (TEST_HINT(window.hints, HINT_BELOW)) {
341                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
342                                 fflush(stderr); */
343
344                                 xa = ATOM(_WIN_LAYER);
345                                 if (xa != None) {
346                                         long prop = 0;
347
348                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
349                                                 PropModeAppend, (unsigned char *) &prop, 1);
350                                 }
351
352                                 xa = ATOM(_NET_WM_STATE);
353                                 if (xa != None) {
354                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
355
356                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
357                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
358                                 }
359                         }
360
361                         /* Above other windows */
362                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
363                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
364                                 fflush(stderr); */
365
366                                 xa = ATOM(_WIN_LAYER);
367                                 if (xa != None) {
368                                         long prop = 6;
369
370                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
371                                                 PropModeAppend, (unsigned char *) &prop, 1);
372                                 }
373
374                                 xa = ATOM(_NET_WM_STATE);
375                                 if (xa != None) {
376                                         Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
377
378                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
379                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
380                                 }
381                         }
382
383                         /* Sticky */
384                         if (TEST_HINT(window.hints, HINT_STICKY)) {
385                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
386                                 fflush(stderr); */
387
388                                 xa = ATOM(_NET_WM_DESKTOP);
389                                 if (xa != None) {
390                                         CARD32 xa_prop = 0xFFFFFFFF;
391
392                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
393                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
394                                 }
395
396                                 xa = ATOM(_NET_WM_STATE);
397                                 if (xa != None) {
398                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
399
400                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
401                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
402                                 }
403                         }
404
405                         /* Skip taskbar */
406                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
407                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
408                                 fflush(stderr); */
409
410                                 xa = ATOM(_NET_WM_STATE);
411                                 if (xa != None) {
412                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
413
414                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
415                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
416                                 }
417                         }
418
419                         /* Skip pager */
420                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
421                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
422                                 fflush(stderr); */
423
424                                 xa = ATOM(_NET_WM_STATE);
425                                 if (xa != None) {
426                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
427
428                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
429                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
430                                 }
431                         }
432                 } /* else { window.type != TYPE_OVERRIDE */
433
434                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
435                         window.window);
436                 fflush(stderr);
437
438                 XMapWindow(display, window.window);
439
440         } else /* if (own_window) { */
441 #endif
442                 /* root / desktop window */
443         {
444                 XWindowAttributes attrs;
445
446                 if (!window.window) {
447                         window.window = find_desktop_window(&window.root, &window.desktop);
448                 }
449
450                 if (XGetWindowAttributes(display, window.window, &attrs)) {
451                         window.width = attrs.width;
452                         window.height = attrs.height;
453                 }
454
455                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
456         }
457
458         /* Drawable is same as window. This may be changed by double buffering. */
459         window.drawable = window.window;
460
461 #ifdef HAVE_XDBE
462         if (use_xdbe) {
463                 int major, minor;
464
465                 if (!XdbeQueryExtension(display, &major, &minor)) {
466                         use_xdbe = 0;
467                 } else {
468                         window.back_buffer = XdbeAllocateBackBufferName(display,
469                                 window.window, XdbeBackground);
470                         if (window.back_buffer != None) {
471                                 window.drawable = window.back_buffer;
472                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
473                         } else {
474                                 use_xdbe = 0;
475                         }
476                 }
477                 if (!use_xdbe) {
478                         ERR("failed to set up double buffer");
479                 }
480         }
481         if (!use_xdbe) {
482                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
483         }
484 #endif
485 #ifdef IMLIB2
486         {
487                 Visual *visual;
488                 Colormap colourmap;
489                 visual = DefaultVisual(display, DefaultScreen(display));
490                 colourmap = DefaultColormap(display, DefaultScreen(display));
491                 cimlib_init(display, window.drawable, visual, colourmap);
492         }
493 #endif /* IMLIB2 */
494         XFlush(display);
495
496         /* set_transparent_background(window.window);
497          * must be done after double buffer stuff? */
498 #ifdef OWN_WINDOW
499         /* if (own_window) {
500                 set_transparent_background(window.window);
501                 XClearWindow(display, window.window);
502         } */
503 #endif
504
505         XSelectInput(display, window.window, ExposureMask
506 #ifdef OWN_WINDOW
507                 | (own_window ? (StructureNotifyMask | PropertyChangeMask |
508                 ButtonPressMask | ButtonReleaseMask) : 0)
509 #endif
510                 );
511 }
512
513 static Window find_subwindow(Window win, int w, int h)
514 {
515         unsigned int i, j;
516         Window troot, parent, *children;
517         unsigned int n;
518
519         /* search subwindows with same size as display or work area */
520
521         for (i = 0; i < 10; i++) {
522                 XQueryTree(display, win, &troot, &parent, &children, &n);
523
524                 for (j = 0; j < n; j++) {
525                         XWindowAttributes attrs;
526
527                         if (XGetWindowAttributes(display, children[j], &attrs)) {
528                                 /* Window must be mapped and same size as display or
529                                  * work space */
530                                 if (attrs.map_state != 0 && ((attrs.width == display_width
531                                                 && attrs.height == display_height)
532                                                 || (attrs.width == w && attrs.height == h))) {
533                                         win = children[j];
534                                         break;
535                                 }
536                         }
537                 }
538
539                 XFree(children);
540                 if (j == n) {
541                         break;
542                 }
543         }
544
545         return win;
546 }
547
548 long get_x11_color(const char *name)
549 {
550         XColor color;
551
552         color.pixel = 0;
553         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
554                 /* lets check if it's a hex colour with the # missing in front
555                  * if yes, then do something about it */
556                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
557
558                 newname[0] = '#';
559                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
560                 /* now lets try again */
561                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
562                                 &color)) {
563                         ERR("can't parse X color '%s'", name);
564                         return 0xFF00FF;
565                 }
566         }
567         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
568                 ERR("can't allocate X color '%s'", name);
569         }
570
571         return (long) color.pixel;
572 }
573
574 void create_gc(void)
575 {
576         XGCValues values;
577
578         values.graphics_exposures = 0;
579         values.function = GXcopy;
580         window.gc = XCreateGC(display, window.drawable,
581                 GCFunction | GCGraphicsExposures, &values);
582 }
583
584 void update_x11info(void)
585 {
586         struct information *current_info = &info;
587         current_info->x11.monitor.number = XScreenCount(display);
588         current_info->x11.monitor.current = XDefaultScreen(display);
589 }
590
591 /* reserve window manager space */
592 void set_struts(int sidenum)
593 {
594         Atom strut;
595         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
596                 /* reserve space at left, right, top, bottom */
597                 signed long sizes[12] = {0};
598                 int i;
599
600                 /* define strut depth */
601                 switch (sidenum) {
602                         case 0:
603                         {
604                                 /* left side */
605                                 sizes[0] = window.x + window.width;
606                                 break;
607                         }
608                         case 1:
609                         {
610                                 /* right side */
611                                 sizes[1] = display_width - window.x;
612                                 break;
613                         }
614                         case 2:
615                         {
616                                 /* top side */
617                                 sizes[2] = window.y + window.height;
618                                 break;
619                         }
620                         case 3:
621                         {
622                                 /* bottom side */
623                                 sizes[3] = display_height - window.y;
624                                 break;
625                         }
626                 }
627
628                 /* define partial strut length */
629                 if (sidenum <= 1) {
630                         sizes[4 + (sidenum*2)] = window.y;
631                         sizes[5 + (sidenum*2)] = window.y + window.height;
632                 } else if (sidenum <= 3) {
633                         sizes[4 + (sidenum*2)] = window.x;
634                         sizes[5 + (sidenum*2)] = window.x + window.width;
635                 }
636
637                 /* check constraints */
638                 for (i = 0; i < 12; i++) {
639                         if (sizes[i] < 0) {
640                                 sizes[i] = 0;
641                         } else {
642                                 if (i <= 1 || i >= 8) {
643                                         if (sizes[i] > display_width) {
644                                                 sizes[i] = display_width;
645                                         }
646                                 } else {
647                                         if (sizes[i] > display_height) {
648                                                 sizes[i] = display_height;
649                                         }
650                                 }
651                         }
652                 }
653
654                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
655                                 PropModeReplace, (unsigned char *) &sizes, 4);
656
657                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
658                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
659                                         PropModeReplace, (unsigned char *) &sizes, 12);
660                 }
661         }
662 }