num_chars not used (thanks pkovacs)
[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 #ifdef OWN_WINDOW
190         if (own_window) {
191
192                 /* looks like root pixmap isn't needed for anything */
193                 {
194                         XSetWindowAttributes attrs;
195                         XClassHint class_hints;
196
197                         /* just test color
198                         attrs.background_pixel = get_x11_color("green");
199                         */
200
201                         window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent,        /* depth */
202                                                       CopyFromParent,   /* class */
203                                                       CopyFromParent,   /* visual */
204                                                       CWBackPixel, &attrs);
205 /*                      XWMHints wmhints;
206
207                         this doesn't work properly
208
209                         wmhints.flags = StateHint;
210                         wmhints.initial_state = WithdrawnState;
211                         XSetWMHints(display, window.window, &wmhints);*/
212
213
214
215                         class_hints.res_class = wm_class_name;
216                         class_hints.res_name = wm_class_name;
217                         XSetClassHint(display, window.window,
218                                       &class_hints);
219
220                         /*set_transparent_background(window.window);*/
221                         window_name = (char *) malloc(strlen(WINDOW_NAME_FMT) + strlen(nodename)+1);
222                         sprintf(window_name, WINDOW_NAME_FMT, nodename);
223                         XStoreName(display, window.window, window_name);
224                         free(window_name);
225                         XClearWindow(display, window.window);
226
227                         if (!fixed_pos)
228                                 XMoveWindow(display, window.window, window.x,
229                         window.y);
230                 }
231
232                 {
233                         /* turn off decorations */
234                         Atom a =
235                             XInternAtom(display, "_MOTIF_WM_HINTS", True);
236                         if (a != None) {
237                                 long prop[5] = { 2, 0, 0, 0, 0 };
238                                 XChangeProperty(display, window.window, a,
239                                                 a, 32, PropModeReplace,
240                                                 (unsigned char *) prop, 5);
241                         }
242
243                         /* set window sticky (to all desktops) */
244                         a = XInternAtom(display, "_NET_WM_DESKTOP", True);
245                         if (a != None) {
246                                 long prop = 0xFFFFFFFF;
247                                 XChangeProperty(display, window.window, a,
248                                                 XA_CARDINAL, 32,
249                                                 PropModeReplace,
250                                                 (unsigned char *) &prop,
251                                                 1);
252                         }
253                         if(l) {
254                         /* make sure the layer is on the bottom */
255          a = XInternAtom(display, "_WIN_LAYER", True);
256          if (a != None) {
257             long prop = 0;
258             XChangeProperty(display, window.window, a,
259             XA_CARDINAL, 32,
260             PropModeReplace,
261             (unsigned char *) &prop, 1);
262          }
263                         }
264                 }
265
266                 XMapWindow(display, window.window);
267         } else
268 #endif
269                 /* root / desktop window */
270         {
271                 XWindowAttributes attrs;
272
273                 if (!window.window)
274                         window.window = find_window_to_draw();
275
276                 if (XGetWindowAttributes(display, window.window, &attrs)) {
277                         window.width = attrs.width;
278                         window.height = attrs.height;
279                 }
280         }
281
282         /* Drawable is same as window. This may be changed by double buffering. */
283         window.drawable = window.window;
284
285 #ifdef XDBE
286         if (use_xdbe) {
287                 int major, minor;
288                 if (!XdbeQueryExtension(display, &major, &minor)) {
289                         use_xdbe = 0;
290                 } else {
291                         window.back_buffer =
292                             XdbeAllocateBackBufferName(display,
293                                                        window.window,
294                                                        XdbeBackground);
295                         if (window.back_buffer != None) {
296                                 window.drawable = window.back_buffer;
297                                 fprintf(stderr,
298                                         "Conky: drawing to double buffer\n");
299                         } else
300                                 use_xdbe = 0;
301                 }
302                 if (!use_xdbe)
303                         ERR("failed to set up double buffer");
304         }
305         if (!use_xdbe)
306                 fprintf(stderr, "Conky: drawing to single buffer\n");
307 #endif
308
309         XFlush(display);
310
311         /*set_transparent_background(window.window); must be done after double buffer stuff? */
312 #ifdef OWN_WINDOW
313         /*if (own_window) {
314         set_transparent_background(window.window);
315                 XClearWindow(display, window.window);
316 }*/
317 #endif
318
319         XSelectInput(display, window.window, ExposureMask
320 #ifdef OWN_WINDOW
321                      | (own_window
322                         ? (StructureNotifyMask | PropertyChangeMask) : 0)
323 #endif
324             );
325 }
326
327 static Window find_subwindow(Window win, int w, int h)
328 {
329         unsigned int i, j;
330         Window troot, parent, *children;
331         unsigned int n;
332
333         /* search subwindows with same size as display or work area */
334
335         for (i = 0; i < 10; i++) {
336                 XQueryTree(display, win, &troot, &parent, &children, &n);
337
338                 for (j = 0; j < n; j++) {
339                         XWindowAttributes attrs;
340
341                         if (XGetWindowAttributes
342                             (display, children[j], &attrs)) {
343                                 /* Window must be mapped and same size as display or work space */
344                                 if (attrs.map_state != 0 &&
345                                     ((attrs.width == display_width
346                                       && attrs.height == display_height)
347                                      || (attrs.width == w
348                                          && attrs.height == h))) {
349                                         win = children[j];
350                                         break;
351                                 }
352                         }
353                 }
354
355                 XFree(children);
356                 if (j == n)
357                         break;
358         }
359
360         return win;
361 }
362
363 long get_x11_color(const char *name)
364 {
365         XColor color;
366         color.pixel = 0;
367         if (!XParseColor
368             (display, DefaultColormap(display, screen), name, &color)) {
369                 ERR("can't parse X color '%s'", name);
370                 return 0xFF00FF;
371         }
372         if (!XAllocColor
373             (display, DefaultColormap(display, screen), &color))
374                 ERR("can't allocate X color '%s'", name);
375
376         return (long) color.pixel;
377 }
378
379 void create_gc()
380 {
381         XGCValues values;
382         values.graphics_exposures = 0;
383         values.function = GXcopy;
384         window.gc = XCreateGC(display, window.drawable,
385                               GCFunction | GCGraphicsExposures, &values);
386 }
387
388 #endif /* X11 */