c3510491bf04c7b7f9168dd3fc173c606495d715
[pager.git] / pager.c
1 /* gcc -o pager pager.c -lX11 -lXmu -ldockapp */
2 #include <X11/Xatom.h>
3 #include <dockapp.h>
4 #include <string.h>
5
6 #define DOCKAPP_SIZE 56
7 #define SPEED 100
8 #ifndef FromTool
9 #define FromTool 2
10 #endif
11
12 static GC dockapp_border1GC, dockapp_border2GC, window_border1GC, window_border2GC, active_desktopGC, inactive_desktopGC, active_windowGC, inactive_windowGC;
13 static Atom supported_atom, num_desktops_atom, desktop_geometry_atom, current_desktop_atom, client_list_atom, client_desktop_atom, client_state_atom, active_window_atom, moveresize_window_atom;
14 static Atom shaded_state, skip_pager_state, hidden_state;
15 static int desktop = -1;
16 static Window drag_window;
17 static int drag_x, drag_y;
18 static int drag_button;
19 static int dockapp_x, dockapp_y;
20 static unsigned int dockapp_width, dockapp_height, dockapp_border, dockapp_depth;
21 static double aspect, scale;
22
23 static int desktop_geometry_supported;
24 static int current_desktop_supported;
25 static int client_list_supported;
26 static int active_window_supported;
27 static int moveresize_supported;
28
29 typedef struct {
30   Window *window;
31   DARect rect;
32 } client_t;
33
34 int error_handler(Display *display, XErrorEvent *event) {
35   char buffer[512];
36
37   switch (event->error_code) {
38     case BadWindow:
39       /* The window may have gone away since we queried the window manager. */
40     break;
41
42     default:
43       if (XGetErrorText(display, event->error_code, buffer, sizeof(buffer))) {
44         fprintf(stderr, "%s\n", buffer);
45         exit(100);
46       }
47     break;
48   }
49
50   return 0;
51 }
52
53 void setup_atom(Atom *atom, const char *prop) {
54   *atom = XInternAtom(DADisplay, prop, True);
55   if (*atom != None) return;
56   fprintf(stderr, "Unknown atom %s!\n", prop);
57   exit(111);
58 }
59
60 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
61   Atom actual;
62   int format;
63   unsigned long num_bytes;
64
65   XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
66 }
67
68 long get_atom_long(Atom atom, Atom type, Window window) {
69   long *data;
70   long ret = 0;
71   unsigned long num_items;
72
73   get_atom_longs(atom, type, window, &data, &num_items);
74   if (num_items) ret = data[0];
75   XFree(data);
76
77   return ret;
78 }
79
80 void check_support() {
81   long *data;
82   unsigned long i, num_items;
83
84   get_atom_longs(supported_atom, XA_ATOM, DefaultRootWindow(DADisplay), &data, &num_items);
85   for (i = 0; i < num_items; i++) {
86     if (data[i] == desktop_geometry_atom) desktop_geometry_supported = 1;
87     if (data[i] == current_desktop_atom) current_desktop_supported = 1;
88     if (data[i] == client_list_atom) client_list_supported = 1;
89     if (data[i] == active_window_atom) active_window_supported = 1;
90     if (data[i] == moveresize_window_atom) moveresize_supported = 1;
91   }
92
93   XFree(data);
94 }
95
96 void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned long *active_desktop, unsigned long *active_window) {
97   Window root, qroot, qparent, dockapp_root, child, *wins, client;
98   int x, y;
99   unsigned int width, height, border, depth;
100   unsigned int root_width, root_height;
101   long *data;
102   long *states;
103   unsigned long i, j, num_states;
104   long client_desktop;
105   int draw;
106
107   if (desktop_geometry_supported) {
108     get_atom_longs(desktop_geometry_atom, XA_CARDINAL, DefaultRootWindow(DADisplay), &states, &num_states);
109     root_width = states[0];
110     root_height = states[1];
111     XFree(states);
112   }
113   else {
114     root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
115     root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
116   }
117
118   XGetGeometry(DADisplay, DAWindow, &dockapp_root, &dockapp_x, &dockapp_y, &dockapp_width, &dockapp_height, &dockapp_border, &dockapp_depth);
119   scale = (double) root_width / (double) dockapp_width;
120   aspect = (double) root_height / (double) dockapp_height;
121
122   *active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root);
123   *active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
124
125   if (client_list_supported) {
126     get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &data, num_clients);
127   }
128   else {
129     XQueryTree(DADisplay, dockapp_root, &qroot, &qparent, &wins, (unsigned int *) num_clients);
130   }
131
132   *clients = (client_t **) malloc(sizeof(client_t *) * *num_clients);
133   if (! *clients) {
134     fprintf(stderr, "Out of memory for clients!\n");
135     exit(111);
136   }
137
138   for (i = 0; i < *num_clients; i++) {
139     (*clients)[i] = (client_t *) malloc(sizeof(client_t));
140     if (! (*clients)[i]) {
141       fprintf(stderr, "Out of memory for client!\n");
142       exit(111);
143     }
144     (*clients)[i]->window = 0;
145
146     if (client_list_supported) client = data[i];
147     else client = XmuClientWindow(DADisplay, wins[i]);
148
149     /* Don't page the pager. */
150     if (client == DAWindow) continue;
151
152     /* Check the window is on our desktop (or all desktops). */
153     client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, client);
154     if (desktop > -1) {
155       if (client_desktop != desktop && client_desktop != -1) continue;
156     }
157     else {
158       if (client_desktop != *active_desktop && client_desktop != -1) continue;
159     }
160
161     /* Try to get its dimensions. */
162     if (! XGetGeometry(DADisplay, client, &root, &x, &y, &width, &height, &border, &depth)) continue;
163     if (! XTranslateCoordinates(DADisplay, client, root, x, y, &x, &y, &child)) continue;
164
165     /* Make sure it isn't hidden or shaded. */
166     draw = 1;
167     get_atom_longs(client_state_atom, XA_ATOM, client, &states, &num_states);
168     for (j = 0; j < num_states; j++) {
169       if (states[j] == shaded_state) height = 3.0 * scale;
170       if (states[j] == skip_pager_state) draw = 0;
171       if (states[j] == hidden_state) draw = 0;
172     }
173     if (! draw) continue;
174
175     (*clients)[i]->window = client;
176
177     /* Scale it. */
178     (*clients)[i]->rect.x = (double) x / scale;
179     (*clients)[i]->rect.y = (double) y / aspect;
180     (*clients)[i]->rect.width = (double) width / scale;
181     (*clients)[i]->rect.height = (double) height / aspect;
182   }
183
184   if (client_list_supported) XFree(data);
185   else XFree(wins);
186 }
187
188 void destroy_clients(client_t ***clients, unsigned long num_clients) {
189   unsigned long i;
190
191   if (! *clients) return;
192
193   for (i = 0; i < num_clients; i++) free((*clients)[i]);
194   free(*clients);
195 }
196
197 void setup_GCs() {
198   XGCValues gcv;
199
200   /* GC's */
201   gcv.foreground = DAGetColor("darkGray");
202   XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
203
204   gcv.foreground = DAGetColor("lightGray");
205   gcv.graphics_exposures = False;
206
207   dockapp_border1GC     = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
208
209   gcv.foreground = DAGetColor("#222222");
210   dockapp_border2GC     =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
211
212   gcv.foreground = DAGetColor("#0088ff");
213   active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
214
215   gcv.foreground = DAGetColor("darkGray");
216   inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
217
218   gcv.foreground = DAGetColor("white");
219   active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
220
221   gcv.foreground = DAGetColor("#999999");
222   inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
223
224   gcv.foreground = DAGetColor("black");
225   window_border1GC      =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
226
227   gcv.foreground = DAGetColor("#333333");
228   window_border2GC      =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
229 }
230
231 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
232   /* Drawing */
233   XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
234
235   XDrawLine(DADisplay, pixmap, dockapp_border2GC, 0, 0, 0, height - 2);
236   XDrawLine(DADisplay, pixmap, dockapp_border2GC, 1, 0, width - 1, 0);
237
238   XDrawLine(DADisplay, pixmap, dockapp_border1GC, 0, height - 1, width - 1, height - 1);
239   XDrawLine(DADisplay, pixmap, dockapp_border1GC, width - 1, 1, width - 1, height - 2);
240 }
241
242 int clicked(DARect *rect, int x, int y) {
243   if (rect->x > x) return 0;
244   if (rect->y > y) return 0;
245   if (rect->x + rect->width < x) return 0;
246   if (rect->y + rect->height < y) return 0;
247   return 1;
248 }
249
250 int client_message(Atom message_type, Window window, long data0, long data1, long data2, long data3, long data4) {
251   XEvent event;
252
253   memset(&event, 0, sizeof(event));
254   event.type = ClientMessage;
255   event.xclient.type = ClientMessage;
256   event.xclient.send_event = True;
257   event.xclient.window = window;
258   event.xclient.message_type = message_type;
259   event.xclient.format = 32;
260   event.xclient.data.l[0] = data0;
261   event.xclient.data.l[1] = data1;
262   event.xclient.data.l[2] = data2;
263   event.xclient.data.l[3] = data3;
264   event.xclient.data.l[4] = data4;
265
266   return XSendEvent(DADisplay, DefaultRootWindow(DADisplay), False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
267 }
268
269 void change(int button, int state, int x, int y) {
270   client_t **clients;
271   unsigned long i, num_clients;
272   unsigned long active_desktop, active_window;
273
274   enumerate_clients(&clients, &num_clients, &active_desktop, &active_window);
275   for (i = num_clients - 1; i > 0; i--) {
276     if (! clients[i]->window) continue;
277     if (! clicked(&clients[i]->rect, x, y)) continue;
278
279     drag_window = (Window) clients[i]->window;
280     drag_button = button;
281     drag_x = x - clients[i]->rect.x;
282     drag_y = y - clients[i]->rect.y;
283     break;
284   }
285   destroy_clients(&clients, num_clients);
286
287   if (drag_window && button == 1) {
288     client_message(active_window_atom, drag_window, FromTool, CurrentTime, 0, 0 ,0);
289   }
290   else {
291     client_message(current_desktop_atom, DefaultRootWindow(DADisplay), desktop, 0, 0, 0, 0);
292   }
293 }
294
295 void release(int button, int state, int x, int y) {
296   if (button == drag_button) drag_window = 0;
297 }
298
299 void page() {
300   Window active_window;
301   Pixmap pixmap;
302   long i;
303   unsigned long num_clients;
304   unsigned long active_desktop;
305   GC gc;
306   client_t **clients;
307
308   enumerate_clients(&clients, &num_clients, &active_desktop, &active_window);
309
310   pixmap = XCreatePixmap(DADisplay, DAWindow, dockapp_width, dockapp_height, dockapp_depth);
311   if (active_desktop == desktop || desktop < 0) gc = active_desktopGC;
312   else gc = inactive_desktopGC;
313   draw_relief(pixmap, dockapp_width, dockapp_height, gc);
314
315   for (i = 0; i < num_clients; i++) {
316     if (! clients[i]->window) continue;
317
318     /* Draw windows which are partially off-screen. */
319     if (clients[i]->rect.x < 0) {
320       clients[i]->rect.width += clients[i]->rect.x;
321       clients[i]->rect.x = 0;
322     }
323     if (clients[i]->rect.y < 0) {
324       clients[i]->rect.height += clients[i]->rect.y;
325       clients[i]->rect.y = 0;
326     }
327
328     /* Draw small windows. */
329     if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) gc = window_border1GC;
330     else if ((long) clients[i]->window == active_window) gc = active_windowGC;
331     else gc = inactive_windowGC;
332
333     XFillRectangle(DADisplay, pixmap, gc, clients[i]->rect.x, clients[i]->rect.y, clients[i]->rect.width, clients[i]->rect.height);
334     if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) continue;
335
336     XDrawLine(DADisplay, pixmap, window_border1GC, clients[i]->rect.x, clients[i]->rect.y, clients[i]->rect.x, clients[i]->rect.y + clients[i]->rect.height - 2);
337     XDrawLine(DADisplay, pixmap, window_border1GC, clients[i]->rect.x + 1, clients[i]->rect.y, clients[i]->rect.x + clients[i]->rect.width - 1, clients[i]->rect.y);
338     XDrawLine(DADisplay, pixmap, window_border2GC, clients[i]->rect.x, clients[i]->rect.y + clients[i]->rect.height - 1, clients[i]->rect.x + clients[i]->rect.width - 1, clients[i]->rect.y + clients[i]->rect.height - 1);
339     XDrawLine(DADisplay, pixmap, window_border2GC, clients[i]->rect.x + clients[i]->rect.width - 1, clients[i]->rect.y + 1, clients[i]->rect.x + clients[i]->rect.width - 1, clients[i]->rect.y + clients[i]->rect.height - 2);
340   }
341
342   destroy_clients(&clients, num_clients);
343
344   DASetPixmap(pixmap);
345   XFreePixmap(DADisplay, pixmap);
346 }
347
348 void move(int x, int y) {
349   long dest_x, dest_y;
350
351   if (! drag_window) return;
352   if (x < 0 || y < 0) return;
353   if (x >= dockapp_width || y >= dockapp_height) return;
354
355   dest_x = (long) ((double) (x - drag_x) * scale);
356   dest_y = (long) ((double) (y - drag_y) * scale);
357
358   if (moveresize_supported) {
359     client_message(moveresize_window_atom, drag_window, StaticGravity | (3 << 8) | (FromTool << 12), drag_x, drag_y, 0, 0);
360   }
361   else {
362     XMoveWindow(DADisplay, drag_window, dest_x, dest_y);
363   }
364
365   page();
366 }
367
368 int main(int argc, char **argv) {
369   Display *display;
370   unsigned int root_width, root_height;
371   long num_desktops;
372   DACallbacks callbacks = { 0, change, release, move, 0, 0, page };
373   DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
374
375   DASetExpectedVersion(20020126);
376
377   display = XOpenDisplay(0);
378
379   root_width = DisplayWidth(display, DefaultScreen(display));
380   root_height = DisplayHeight(display, DefaultScreen(display));
381   aspect = (double) root_width / (double) root_height;
382
383   if (root_width > root_height) {
384     dockapp_width = DOCKAPP_SIZE;
385     dockapp_height = (double) DOCKAPP_SIZE / aspect;
386   }
387   else {
388     dockapp_height = DOCKAPP_SIZE;
389     dockapp_width = (double) DOCKAPP_SIZE / aspect;
390   }
391
392   DAParseArguments(argc, argv, &options, 1, "OPTIONS", "pager");
393   DAOpenDisplay(0, argc, argv);
394   DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
395
396   DASetCallbacks(&callbacks);
397   DASetTimeout(SPEED);
398
399   setup_atom(&supported_atom, "_NET_SUPPORTED");
400   setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
401   setup_atom(&desktop_geometry_atom, "_NET_DESKTOP_GEOMETRY");
402   setup_atom(&current_desktop_atom, "_NET_CURRENT_DESKTOP");
403   setup_atom(&client_list_atom, "_NET_CLIENT_LIST_STACKING");
404   setup_atom(&client_desktop_atom, "_NET_WM_DESKTOP");
405   setup_atom(&client_state_atom, "_NET_WM_STATE");
406   setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW");
407   setup_atom(&moveresize_window_atom, "_NET_MOVERESIZE_WINDOW");
408
409   setup_atom(&shaded_state, "_NET_WM_STATE_SHADED");
410   setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER");
411   setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN");
412
413   check_support();
414
415   num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
416   if (desktop == 0) {
417     desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
418   }
419   else if (desktop < 0) desktop = -1;
420   else if (desktop-- > num_desktops) {
421     fprintf(stderr, "There are only %ld desktops!\n", num_desktops);
422     exit(111);
423   }
424
425   XSetErrorHandler(error_handler);
426   setup_GCs();
427   DAShow();
428   DAEventLoop();
429
430   exit(0);
431 }