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