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