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