Remove old,unused desktop-finding code
[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         /* default work area is display */
89         workarea[0] = 0;
90         workarea[1] = 0;
91         workarea[2] = display_width;
92         workarea[3] = display_height;
93 }
94
95 /* Find root window and desktop window.
96  * Return desktop window on success,
97  * and set root and desktop byref return values.
98  * Return 0 on failure. */
99 static Window find_desktop_window(Window *p_root, Window *p_desktop)
100 {
101         Atom type;
102         int format, i;
103         unsigned long nitems, bytes;
104         unsigned int n;
105         Window root = RootWindow(display, screen);
106         Window win = root;
107         Window troot, parent, *children;
108         unsigned char *buf = NULL;
109
110         if (!p_root || !p_desktop) {
111                 return 0;
112         }
113
114         /* some window managers set __SWM_VROOT to some child of root window */
115
116         XQueryTree(display, root, &troot, &parent, &children, &n);
117         for (i = 0; i < (int) n; i++) {
118                 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
119                                 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
120                                 == Success && type == XA_WINDOW) {
121                         win = *(Window *) buf;
122                         XFree(buf);
123                         XFree(children);
124                         fprintf(stderr,
125                                 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
126                                 win);
127                         fflush(stderr);
128                         *p_root = win;
129                         *p_desktop = win;
130                         return win;
131                 }
132
133                 if (buf) {
134                         XFree(buf);
135                         buf = 0;
136                 }
137         }
138         XFree(children);
139
140         /* get subwindows from root */
141         win = find_subwindow(root, -1, -1);
142
143         update_workarea();
144
145         win = find_subwindow(win, workarea[2], workarea[3]);
146
147         if (buf) {
148                 XFree(buf);
149                 buf = 0;
150         }
151
152         if (win != root) {
153                 fprintf(stderr,
154                         PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
155                         win, root);
156         } else {
157                 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
158         }
159
160         fflush(stderr);
161
162         *p_root = root;
163         *p_desktop = win;
164
165         return win;
166 }
167
168 /* sets background to ParentRelative for the Window and all parents */
169 void set_transparent_background(Window win)
170 {
171         static int colour_set = -1;
172
173         if (set_transparent) {
174                 Window parent = win;
175                 unsigned int i;
176
177                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
178                         Window r, *children;
179                         unsigned int n;
180
181                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
182
183                         XQueryTree(display, parent, &r, &parent, &children, &n);
184                         XFree(children);
185                 }
186         } else if (colour_set != background_colour) {
187                 XSetWindowBackground(display, win, background_colour);
188                 colour_set = background_colour;
189         }
190         // XClearWindow(display, win); not sure why this was here
191 }
192
193 void destroy_window(void)
194 {
195         XDestroyWindow(display, window.window);
196         XFreeGC(display, window.gc);
197         memset(&window, 0, sizeof(struct conky_window));
198 }
199
200 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
201                 char **argv, int argc)
202 {
203         /* There seems to be some problems with setting transparent background
204          * (on fluxbox this time). It doesn't happen always and I don't know why it
205          * happens but I bet the bug is somewhere here. */
206         set_transparent = set_trans;
207         background_colour = back_colour;
208
209 #ifdef OWN_WINDOW
210         if (own_window) {
211                 if (!find_desktop_window(&window.root, &window.desktop)) {
212                         return;
213                 }
214
215                 if (window.type == TYPE_OVERRIDE) {
216
217                         /* An override_redirect True window.
218                          * No WM hints or button processing needed. */
219                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
220                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
221                                 True, 0, 0 };
222
223                         /* Parent is desktop window (which might be a child of root) */
224                         window.window = XCreateWindow(display, window.desktop, window.x,
225                                 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
226                                 CWBackPixel | CWOverrideRedirect, &attrs);
227
228                         XLowerWindow(display, window.window);
229
230                         fprintf(stderr, PACKAGE_NAME": window type - override\n");
231                         fflush(stderr);
232                 } else { /* window.type != TYPE_OVERRIDE */
233
234                         /* A window managed by the window manager.
235                          * Process hints and buttons. */
236                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
237                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
238                                 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
239
240                         XClassHint classHint;
241                         XWMHints wmHint;
242                         Atom xa;
243
244                         if (window.type == TYPE_DOCK) {
245                                 window.x = window.y = 0;
246                         }
247                         /* Parent is root window so WM can take control */
248                         window.window = XCreateWindow(display, window.root, window.x,
249                                 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
250                                 CWBackPixel | CWOverrideRedirect, &attrs);
251
252                         classHint.res_name = window.class_name;
253                         classHint.res_class = classHint.res_name;
254
255                         wmHint.flags = InputHint | StateHint;
256                         /* allow decorated windows to be given input focus by WM */
257                         wmHint.input =
258                                 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
259                         if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
260                                 wmHint.initial_state = WithdrawnState;
261                         } else {
262                                 wmHint.initial_state = NormalState;
263                         }
264
265                         XmbSetWMProperties(display, window.window, window.title, NULL, argv,
266                                 argc, NULL, &wmHint, &classHint);
267
268                         /* Sets an empty WM_PROTOCOLS property */
269                         XSetWMProtocols(display, window.window, NULL, 0);
270
271                         /* Set window type */
272                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
273                                 Atom prop;
274
275                                 switch (window.type) {
276                                         case TYPE_DESKTOP:
277                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
278                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
279                                                 fflush(stderr);
280                                                 break;
281                                         case TYPE_DOCK:
282                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
283                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
284                                                 fflush(stderr);
285                                                 break;
286                                         case TYPE_PANEL:
287                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
288                                                 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
289                                                 fflush(stderr);
290                                                 break;
291                                         case TYPE_NORMAL:
292                                         default:
293                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
294                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
295                                                 fflush(stderr);
296                                                 break;
297                                 }
298                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
299                                         PropModeReplace, (unsigned char *) &prop, 1);
300                         }
301
302                         /* Set desired hints */
303
304                         /* Window decorations */
305                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
306                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
307                                 fflush(stderr); */
308
309                                 xa = ATOM(_MOTIF_WM_HINTS);
310                                 if (xa != None) {
311                                         long prop[5] = { 2, 0, 0, 0, 0 };
312                                         XChangeProperty(display, window.window, xa, xa, 32,
313                                                 PropModeReplace, (unsigned char *) prop, 5);
314                                 }
315                         }
316
317                         /* Below other windows */
318                         if (TEST_HINT(window.hints, HINT_BELOW)) {
319                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
320                                 fflush(stderr); */
321
322                                 xa = ATOM(_WIN_LAYER);
323                                 if (xa != None) {
324                                         long prop = 0;
325
326                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
327                                                 PropModeAppend, (unsigned char *) &prop, 1);
328                                 }
329
330                                 xa = ATOM(_NET_WM_STATE);
331                                 if (xa != None) {
332                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
333
334                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
335                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
336                                 }
337                         }
338
339                         /* Above other windows */
340                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
341                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
342                                 fflush(stderr); */
343
344                                 xa = ATOM(_WIN_LAYER);
345                                 if (xa != None) {
346                                         long prop = 6;
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_ABOVE);
355
356                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
357                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
358                                 }
359                         }
360
361                         /* Sticky */
362                         if (TEST_HINT(window.hints, HINT_STICKY)) {
363                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
364                                 fflush(stderr); */
365
366                                 xa = ATOM(_NET_WM_DESKTOP);
367                                 if (xa != None) {
368                                         CARD32 xa_prop = 0xFFFFFFFF;
369
370                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
371                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
372                                 }
373
374                                 xa = ATOM(_NET_WM_STATE);
375                                 if (xa != None) {
376                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
377
378                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
379                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
380                                 }
381                         }
382
383                         /* Skip taskbar */
384                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
385                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
386                                 fflush(stderr); */
387
388                                 xa = ATOM(_NET_WM_STATE);
389                                 if (xa != None) {
390                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
391
392                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
393                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
394                                 }
395                         }
396
397                         /* Skip pager */
398                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
399                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
400                                 fflush(stderr); */
401
402                                 xa = ATOM(_NET_WM_STATE);
403                                 if (xa != None) {
404                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
405
406                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
407                                                 PropModeAppend, (unsigned char *) &xa_prop, 1);
408                                 }
409                         }
410                 } /* else { window.type != TYPE_OVERRIDE */
411
412                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
413                         window.window);
414                 fflush(stderr);
415
416                 XMapWindow(display, window.window);
417
418         } else /* if (own_window) { */
419 #endif
420                 /* root / desktop window */
421         {
422                 XWindowAttributes attrs;
423
424                 if (!window.window) {
425                         window.window = find_desktop_window(&window.root, &window.desktop);
426                 }
427
428                 if (XGetWindowAttributes(display, window.window, &attrs)) {
429                         window.width = attrs.width;
430                         window.height = attrs.height;
431                 }
432
433                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
434         }
435
436         /* Drawable is same as window. This may be changed by double buffering. */
437         window.drawable = window.window;
438
439 #ifdef HAVE_XDBE
440         if (use_xdbe) {
441                 int major, minor;
442
443                 if (!XdbeQueryExtension(display, &major, &minor)) {
444                         use_xdbe = 0;
445                 } else {
446                         window.back_buffer = XdbeAllocateBackBufferName(display,
447                                 window.window, XdbeBackground);
448                         if (window.back_buffer != None) {
449                                 window.drawable = window.back_buffer;
450                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
451                         } else {
452                                 use_xdbe = 0;
453                         }
454                 }
455                 if (!use_xdbe) {
456                         ERR("failed to set up double buffer");
457                 }
458         }
459         if (!use_xdbe) {
460                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
461         }
462 #endif
463         window.visual = DefaultVisual(display, DefaultScreen(display));
464         window.colourmap = DefaultColormap(display, DefaultScreen(display));
465 #ifdef IMLIB2
466         {
467                 cimlib_init(display, window.drawable, window.visual, window.colourmap);
468         }
469 #endif /* IMLIB2 */
470         XFlush(display);
471
472         /* set_transparent_background(window.window);
473          * must be done after double buffer stuff? */
474 #ifdef OWN_WINDOW
475         /* if (own_window) {
476                 set_transparent_background(window.window);
477                 XClearWindow(display, window.window);
478         } */
479 #endif
480
481         XSelectInput(display, window.window, ExposureMask
482 #ifdef OWN_WINDOW
483                 | (own_window ? (StructureNotifyMask | PropertyChangeMask |
484                 ButtonPressMask | ButtonReleaseMask) : 0)
485 #endif
486                 );
487 }
488
489 static Window find_subwindow(Window win, int w, int h)
490 {
491         unsigned int i, j;
492         Window troot, parent, *children;
493         unsigned int n;
494
495         /* search subwindows with same size as display or work area */
496
497         for (i = 0; i < 10; i++) {
498                 XQueryTree(display, win, &troot, &parent, &children, &n);
499
500                 for (j = 0; j < n; j++) {
501                         XWindowAttributes attrs;
502
503                         if (XGetWindowAttributes(display, children[j], &attrs)) {
504                                 /* Window must be mapped and same size as display or
505                                  * work space */
506                                 if (attrs.map_state != 0 && ((attrs.width == display_width
507                                                 && attrs.height == display_height)
508                                                 || (attrs.width == w && attrs.height == h))) {
509                                         win = children[j];
510                                         break;
511                                 }
512                         }
513                 }
514
515                 XFree(children);
516                 if (j == n) {
517                         break;
518                 }
519         }
520
521         return win;
522 }
523
524 long get_x11_color(const char *name)
525 {
526         XColor color;
527
528         color.pixel = 0;
529         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
530                 /* lets check if it's a hex colour with the # missing in front
531                  * if yes, then do something about it */
532                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
533
534                 newname[0] = '#';
535                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
536                 /* now lets try again */
537                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
538                                 &color)) {
539                         ERR("can't parse X color '%s'", name);
540                         return 0xFF00FF;
541                 }
542         }
543         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
544                 ERR("can't allocate X color '%s'", name);
545         }
546
547         return (long) color.pixel;
548 }
549
550 void create_gc(void)
551 {
552         XGCValues values;
553
554         values.graphics_exposures = 0;
555         values.function = GXcopy;
556         window.gc = XCreateGC(display, window.drawable,
557                 GCFunction | GCGraphicsExposures, &values);
558 }
559
560 void update_x11info(void)
561 {
562         Window root;
563         Atom actual_type, atom;
564         int actual_format;
565         unsigned long nitems;
566         unsigned long bytes_after;
567         unsigned char *prop = NULL;
568
569         struct information *current_info = &info;
570         current_info->x11.monitor.number = XScreenCount(display);
571         current_info->x11.monitor.current = XDefaultScreen(display);
572
573         root = RootWindow(display, current_info->x11.monitor.current);
574
575         //Get current desktop number
576         if ((atom = XInternAtom(display, "_NET_CURRENT_DESKTOP", True)) != None) {
577           if ( (XGetWindowProperty( display, root, atom,
578                                     0, 1L, False, XA_CARDINAL,
579                                     &actual_type, &actual_format, &nitems,
580                                     &bytes_after, &prop ) == Success ) &&
581                (actual_type == XA_CARDINAL) &&
582                (nitems == 1L) ) {
583             current_info->x11.desktop.current = prop[0]+1;
584           }
585         }
586
587         //Get total number of available desktops
588         if ((atom = XInternAtom(display, "_NET_NUMBER_OF_DESKTOPS", True)) != None) {
589           if ( (XGetWindowProperty( display, root, atom,
590                                     0, 1L, False, XA_CARDINAL,
591                                     &actual_type, &actual_format, &nitems,
592                                     &bytes_after, &prop ) == Success ) &&
593                (actual_type == XA_CARDINAL) &&
594                (nitems == 1L) ) {
595             current_info->x11.desktop.number = prop[0];
596           }
597         }
598
599         //Get current desktop name
600         if ((atom = XInternAtom(display, "_NET_DESKTOP_NAMES", True)) != None) {
601           if ( (XGetWindowProperty( display, root, atom,
602                                     0, (~0L), False, ATOM(UTF8_STRING),
603                                     &actual_type, &actual_format, &nitems,
604                                     &bytes_after, &prop ) == Success ) &&
605                (actual_type == ATOM(UTF8_STRING)) &&
606                (nitems > 0L) ) {
607             unsigned int i = 0, j = 0;
608             int k = 0;
609             while ( i < nitems ) {
610               if ( prop[i++] == '\0' ) {
611                 if ( ++k == current_info->x11.desktop.current ) {
612                   if(current_info->x11.desktop.name) {
613                     free(current_info->x11.desktop.name);
614                     current_info->x11.desktop.name = NULL;
615                   }
616                   current_info->x11.desktop.name = malloc(i-j);
617                   //desktop names can be empty but should always be not null
618                   strcpy( current_info->x11.desktop.name, (char *)&prop[j] );
619                   break;
620                 }
621                 j = i;
622               }
623             }
624           }
625         }
626
627 }
628
629 /* reserve window manager space */
630 void set_struts(int sidenum)
631 {
632         Atom strut;
633         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
634                 /* reserve space at left, right, top, bottom */
635                 signed long sizes[12] = {0};
636                 int i;
637
638                 /* define strut depth */
639                 switch (sidenum) {
640                         case 0:
641                         {
642                                 /* left side */
643                                 sizes[0] = window.x + window.width;
644                                 break;
645                         }
646                         case 1:
647                         {
648                                 /* right side */
649                                 sizes[1] = display_width - window.x;
650                                 break;
651                         }
652                         case 2:
653                         {
654                                 /* top side */
655                                 sizes[2] = window.y + window.height;
656                                 break;
657                         }
658                         case 3:
659                         {
660                                 /* bottom side */
661                                 sizes[3] = display_height - window.y;
662                                 break;
663                         }
664                 }
665
666                 /* define partial strut length */
667                 if (sidenum <= 1) {
668                         sizes[4 + (sidenum*2)] = window.y;
669                         sizes[5 + (sidenum*2)] = window.y + window.height;
670                 } else if (sidenum <= 3) {
671                         sizes[4 + (sidenum*2)] = window.x;
672                         sizes[5 + (sidenum*2)] = window.x + window.width;
673                 }
674
675                 /* check constraints */
676                 for (i = 0; i < 12; i++) {
677                         if (sizes[i] < 0) {
678                                 sizes[i] = 0;
679                         } else {
680                                 if (i <= 1 || i >= 8) {
681                                         if (sizes[i] > display_width) {
682                                                 sizes[i] = display_width;
683                                         }
684                                 } else {
685                                         if (sizes[i] > display_height) {
686                                                 sizes[i] = display_height;
687                                         }
688                                 }
689                         }
690                 }
691
692                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
693                                 PropModeReplace, (unsigned char *) &sizes, 4);
694
695                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
696                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
697                                         PropModeReplace, (unsigned char *) &sizes, 12);
698                 }
699         }
700 }