fix stale static process pointer in top.c and add nodename to X window
[monky] / src / x11.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * This program is licensed under BSD license, read COPYING
5  *
6  *  $Id$
7  */
8
9
10 #include "conky.h"
11
12 #ifdef X11
13 #include <X11/Xlib.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xutil.h>
16 #ifdef XFT
17 #include <X11/Xft/Xft.h>
18 #endif
19
20 #include <stdio.h>
21
22 #ifdef XDBE
23 int use_xdbe;
24 #endif
25
26 #ifdef XFT
27 int use_xft = 0;
28 #endif
29
30 #define WINDOW_NAME_FMT "%s - conky" 
31
32 /* some basic X11 stuff */
33 Display *display;
34 int display_width;
35 int display_height;
36 int screen;
37 static int set_transparent;
38 static int background_colour;
39
40 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
41 int workarea[4];
42
43 /* Window stuff */
44 struct conky_window window;
45
46 /* local prototypes */
47 static void update_workarea();
48 static Window find_window_to_draw();
49 static Window find_subwindow(Window win, int w, int h);
50
51 /* X11 initializer */
52 void init_X11()
53 {
54         if ((display = XOpenDisplay(0)) == NULL)
55                 CRIT_ERR("can't open display: %s", XDisplayName(0));
56
57         screen = DefaultScreen(display);
58         display_width = DisplayWidth(display, screen);
59         display_height = DisplayHeight(display, screen);
60
61         update_workarea();
62 }
63
64 static void update_workarea()
65 {
66         Window root = RootWindow(display, screen);
67         unsigned long nitems, bytes;
68         unsigned char *buf = NULL;
69         Atom type;
70         int format;
71
72         /* default work area is display */
73         workarea[0] = 0;
74         workarea[1] = 0;
75         workarea[2] = display_width;
76         workarea[3] = display_height;
77
78         /* get current desktop */
79         if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP),
80                                0, 1, False, XA_CARDINAL, &type, &format,
81                                &nitems, &bytes, &buf) == Success
82             && type == XA_CARDINAL && nitems > 0) {
83
84                 //Currently unused 
85                 /*  long desktop = * (long *) buf; */
86
87                 XFree(buf);
88                 buf = 0;
89
90         }
91
92         if (buf) {
93                 XFree(buf);
94                 buf = 0;
95         }
96 }
97
98 static Window find_window_to_draw()
99 {
100         Atom type;
101         int format, i;
102         unsigned long nitems, bytes;
103         unsigned int n;
104         Window root = RootWindow(display, screen);
105         Window win = root;
106         Window troot, parent, *children;
107         unsigned char *buf = NULL;
108
109         /* some window managers set __SWM_VROOT to some child of root window */
110
111         XQueryTree(display, root, &troot, &parent, &children, &n);
112         for (i = 0; i < (int) n; i++) {
113                 if (XGetWindowProperty
114                     (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
115                      XA_WINDOW, &type, &format, &nitems, &bytes,
116                      &buf) == Success && type == XA_WINDOW) {
117                         win = *(Window *) buf;
118                         XFree(buf);
119                         XFree(children);
120                         fprintf(stderr,
121                                 "Conky: drawing to window from __SWM_VROOT property\n");
122                         return win;
123                 }
124
125                 if (buf) {
126                         XFree(buf);
127                         buf = 0;
128                 }
129         }
130         XFree(children);
131
132         /* get subwindows from root */
133         win = find_subwindow(root, -1, -1);
134
135         update_workarea();
136
137         win = find_subwindow(win, workarea[2], workarea[3]);
138
139         if (buf) {
140                 XFree(buf);
141                 buf = 0;
142         }
143
144         if (win != root)
145                 fprintf(stderr,
146                         "Conky: drawing to subwindow of root window (%lx)\n",
147                         win);
148         else
149                 fprintf(stderr, "Conky: drawing to root window\n");
150
151         return win;
152 }
153
154 /* sets background to ParentRelative for the Window and all parents */
155 inline void set_transparent_background(Window win)
156 {
157         static int colour_set = -1;
158         if (set_transparent) {
159                 Window parent = win;
160                 unsigned int i;
161                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
162                         Window r, *children;
163                         unsigned int n;
164                         
165                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
166         
167                         XQueryTree(display, parent, &r, &parent, &children, &n);
168                         XFree(children);
169                         }
170         } else if (colour_set != background_colour) {
171                 XSetWindowBackground(display, win, background_colour);
172                 colour_set = background_colour;
173 }
174         //XClearWindow(display, win); not sure why this was here
175 }
176
177 #if defined OWN_WINDOW
178 void init_window(int own_window, char* wm_class_name, int w, int h, int l, int fixed_pos, int set_trans, int back_colour, char * nodename)
179 #else
180 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour, char * nodename)
181 #endif
182 {
183         /* There seems to be some problems with setting transparent background (on
184          * fluxbox this time). It doesn't happen always and I don't know why it
185          * happens but I bet the bug is somewhere here. */
186         set_transparent = set_trans;
187         background_colour = back_colour;
188         char * window_name = NULL;
189         int num_chars = 0;
190 #ifdef OWN_WINDOW
191         if (own_window) {
192
193                 /* looks like root pixmap isn't needed for anything */
194                 {
195                         XSetWindowAttributes attrs;
196                         XClassHint class_hints;
197
198                         /* just test color
199                         attrs.background_pixel = get_x11_color("green");
200                         */
201
202                         window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent,        /* depth */
203                                                       CopyFromParent,   /* class */
204                                                       CopyFromParent,   /* visual */
205                                                       CWBackPixel, &attrs);
206 /*                      XWMHints wmhints;
207
208                         this doesn't work properly
209
210                         wmhints.flags = StateHint;
211                         wmhints.initial_state = WithdrawnState;
212                         XSetWMHints(display, window.window, &wmhints);*/
213
214
215
216                         class_hints.res_class = wm_class_name;
217                         class_hints.res_name = wm_class_name;
218                         XSetClassHint(display, window.window,
219                                       &class_hints);
220
221                         /*set_transparent_background(window.window);*/
222                         window_name = (char *) malloc(strlen(WINDOW_NAME_FMT) + strlen(nodename)+1);
223                         sprintf(window_name, WINDOW_NAME_FMT, nodename);
224                         XStoreName(display, window.window, window_name);
225                         free(window_name);
226                         XClearWindow(display, window.window);
227
228                         if (!fixed_pos)
229                                 XMoveWindow(display, window.window, window.x,
230                         window.y);
231                 }
232
233                 {
234                         /* turn off decorations */
235                         Atom a =
236                             XInternAtom(display, "_MOTIF_WM_HINTS", True);
237                         if (a != None) {
238                                 long prop[5] = { 2, 0, 0, 0, 0 };
239                                 XChangeProperty(display, window.window, a,
240                                                 a, 32, PropModeReplace,
241                                                 (unsigned char *) prop, 5);
242                         }
243
244                         /* set window sticky (to all desktops) */
245                         a = XInternAtom(display, "_NET_WM_DESKTOP", True);
246                         if (a != None) {
247                                 long prop = 0xFFFFFFFF;
248                                 XChangeProperty(display, window.window, a,
249                                                 XA_CARDINAL, 32,
250                                                 PropModeReplace,
251                                                 (unsigned char *) &prop,
252                                                 1);
253                         }
254                         if(l) {
255                         /* make sure the layer is on the bottom */
256          a = XInternAtom(display, "_WIN_LAYER", True);
257          if (a != None) {
258             long prop = 0;
259             XChangeProperty(display, window.window, a,
260             XA_CARDINAL, 32,
261             PropModeReplace,
262             (unsigned char *) &prop, 1);
263          }
264                         }
265                 }
266
267                 XMapWindow(display, window.window);
268         } else
269 #endif
270                 /* root / desktop window */
271         {
272                 XWindowAttributes attrs;
273
274                 if (!window.window)
275                         window.window = find_window_to_draw();
276
277                 if (XGetWindowAttributes(display, window.window, &attrs)) {
278                         window.width = attrs.width;
279                         window.height = attrs.height;
280                 }
281         }
282
283         /* Drawable is same as window. This may be changed by double buffering. */
284         window.drawable = window.window;
285
286 #ifdef XDBE
287         if (use_xdbe) {
288                 int major, minor;
289                 if (!XdbeQueryExtension(display, &major, &minor)) {
290                         use_xdbe = 0;
291                 } else {
292                         window.back_buffer =
293                             XdbeAllocateBackBufferName(display,
294                                                        window.window,
295                                                        XdbeBackground);
296                         if (window.back_buffer != None) {
297                                 window.drawable = window.back_buffer;
298                                 fprintf(stderr,
299                                         "Conky: drawing to double buffer\n");
300                         } else
301                                 use_xdbe = 0;
302                 }
303                 if (!use_xdbe)
304                         ERR("failed to set up double buffer");
305         }
306         if (!use_xdbe)
307                 fprintf(stderr, "Conky: drawing to single buffer\n");
308 #endif
309
310         XFlush(display);
311
312         /*set_transparent_background(window.window); must be done after double buffer stuff? */
313 #ifdef OWN_WINDOW
314         /*if (own_window) {
315         set_transparent_background(window.window);
316                 XClearWindow(display, window.window);
317 }*/
318 #endif
319
320         XSelectInput(display, window.window, ExposureMask
321 #ifdef OWN_WINDOW
322                      | (own_window
323                         ? (StructureNotifyMask | PropertyChangeMask) : 0)
324 #endif
325             );
326 }
327
328 static Window find_subwindow(Window win, int w, int h)
329 {
330         unsigned int i, j;
331         Window troot, parent, *children;
332         unsigned int n;
333
334         /* search subwindows with same size as display or work area */
335
336         for (i = 0; i < 10; i++) {
337                 XQueryTree(display, win, &troot, &parent, &children, &n);
338
339                 for (j = 0; j < n; j++) {
340                         XWindowAttributes attrs;
341
342                         if (XGetWindowAttributes
343                             (display, children[j], &attrs)) {
344                                 /* Window must be mapped and same size as display or work space */
345                                 if (attrs.map_state != 0 &&
346                                     ((attrs.width == display_width
347                                       && attrs.height == display_height)
348                                      || (attrs.width == w
349                                          && attrs.height == h))) {
350                                         win = children[j];
351                                         break;
352                                 }
353                         }
354                 }
355
356                 XFree(children);
357                 if (j == n)
358                         break;
359         }
360
361         return win;
362 }
363
364 long get_x11_color(const char *name)
365 {
366         XColor color;
367         color.pixel = 0;
368         if (!XParseColor
369             (display, DefaultColormap(display, screen), name, &color)) {
370                 ERR("can't parse X color '%s'", name);
371                 return 0xFF00FF;
372         }
373         if (!XAllocColor
374             (display, DefaultColormap(display, screen), &color))
375                 ERR("can't allocate X color '%s'", name);
376
377         return (long) color.pixel;
378 }
379
380 void create_gc()
381 {
382         XGCValues values;
383         values.graphics_exposures = 0;
384         values.function = GXcopy;
385         window.gc = XCreateGC(display, window.drawable,
386                               GCFunction | GCGraphicsExposures, &values);
387 }
388
389 #endif /* X11 */