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