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