86fd07a9582513bb7535a991a9d9cd9fcbb4d4c1
[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
11 static GC dockapp_border1GC, dockapp_border2GC, window_border1GC, window_border2GC, active_desktopGC, inactive_desktopGC, active_windowGC, inactive_windowGC;
12 static Atom num_desktops_atom, current_desktop_atom, client_list_atom, client_desktop_atom, client_state_atom, active_window_atom;
13 static Atom shaded_state, skip_pager_state, hidden_state;
14 static long desktop = -1;
15
16 typedef struct {
17   Window *window;
18   DARect rect;
19 } client_t;
20
21 int error_handler(Display *display, XErrorEvent *event) {
22   char buffer[512];
23   int length;
24
25   switch (event->error_code) {
26     case BadWindow:
27       /* The window may have gone away since we queried the window manager. */
28     break;
29
30     default:
31       if (XGetErrorText(display, event->error_code, buffer, sizeof(buffer))) {
32         fprintf(stderr, "%s\n", buffer);
33         exit(100);
34       }
35     break;
36   }
37
38   return 0;
39 }
40
41 void setup_atom(Atom *atom, const char *prop) {
42   *atom = XInternAtom(DADisplay, prop, True);
43   if (*atom == None) exit(111);
44 }
45
46 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
47   Atom actual;
48   int format;
49   unsigned long num_bytes;
50
51   XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
52 }
53
54 long get_atom_long(Atom atom, Atom type, Window window) {
55   long *data;
56   long ret = 0;
57   unsigned long num_items;
58
59   get_atom_longs(atom, type, window, &data, &num_items);
60   if (num_items) ret = data[0];
61   XFree(data);
62
63   return ret;
64 }
65
66 /* XXX: enumerate_clients_and_get_a_bunch_of_other_values() */
67 void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned long *active_desktop, unsigned long *active_window, unsigned int *dockapp_width, unsigned int *dockapp_height) {
68   Window root, dockapp_root, child;
69   unsigned int x, y, width, height, border, depth;
70   unsigned int root_width, root_height;
71   unsigned int dockapp_x, dockapp_y, dockapp_border, dockapp_depth;
72   double scale;
73   long *data;
74   long *states;
75   unsigned long i, j, num_states;
76   long client_desktop;
77   int draw;
78
79   /* XXX: Use _NET_DESKTOP_GEOMETRY. */
80   root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
81   root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
82
83   XGetGeometry(DADisplay, DAWindow, &dockapp_root, &dockapp_x, &dockapp_y, dockapp_width, dockapp_height, &dockapp_border, &dockapp_depth);
84   scale = (double) root_width / (double) *dockapp_width;
85
86   *active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root);
87   *active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
88
89   get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &data, num_clients);
90
91   *clients = (client_t **) malloc(sizeof(client_t *) * *num_clients);
92   if (! *clients) {
93     fprintf(stderr, "Out of memory for clients!\n");
94     exit(111);
95   }
96
97   for (i = 0; i < *num_clients; i++) {
98     (*clients)[i] = (client_t *) malloc(sizeof(client_t));
99     if (! (*clients)[i]) {
100       fprintf(stderr, "Out of memory for client!\n");
101       exit(111);
102     }
103     (*clients)[i]->window = 0;
104
105     /* Check the window is on our desktop (or all desktops). */
106     client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, data[i]);
107     if (client_desktop != desktop && client_desktop != -1) continue;
108
109     /* Try to get its dimensions. */
110     if (! XGetGeometry(DADisplay, data[i], &root, &x, &y, &width, &height, &border, &depth)) continue;
111     if (! XTranslateCoordinates(DADisplay, data[i], root, x, y, &x, &y, &child)) continue;
112
113     /* Make sure it isn't hidden or shaded. */
114     draw = 1;
115     get_atom_longs(client_state_atom, XA_ATOM, data[i], &states, &num_states);
116     for (j = 0; j < num_states; j++) {
117       if (states[j] == shaded_state) draw = 0;
118       if (states[j] == skip_pager_state) draw = 0;
119       if (states[j] == hidden_state) draw = 0;
120     }
121     if (! draw) continue;
122
123     (*clients)[i]->window = data[i];
124
125     /* Scale it. */
126     (*clients)[i]->rect.x = (double) x / scale;
127     (*clients)[i]->rect.y = (double) y / scale;
128     (*clients)[i]->rect.width = (double) width / scale;
129     (*clients)[i]->rect.height = (double) height / scale;
130   }
131 }
132
133 void destroy_clients(client_t ***clients, unsigned long num_clients) {
134   unsigned long i;
135
136   if (! *clients) return;
137
138   for (i = 0; i < num_clients; i++) free((*clients)[i]);
139   free(*clients);
140 }
141
142 void setup_GCs() {
143   XGCValues gcv;
144
145   /* GC's */
146   gcv.foreground = DAGetColor("darkGray");
147   XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
148
149   gcv.foreground = DAGetColor("lightGray");
150   gcv.graphics_exposures = False;
151
152   dockapp_border1GC     = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
153
154   gcv.foreground = DAGetColor("#222222");
155   dockapp_border2GC     =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
156
157   gcv.foreground = DAGetColor("#0088ff");
158   active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
159
160   gcv.foreground = DAGetColor("darkGray");
161   inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
162
163   gcv.foreground = DAGetColor("white");
164   active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
165
166   gcv.foreground = DAGetColor("#999999");
167   inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
168
169   gcv.foreground = DAGetColor("black");
170   window_border1GC      =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
171
172   gcv.foreground = DAGetColor("#333333");
173   window_border2GC      =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
174 }
175
176 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
177   /* Drawing */
178   XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
179
180   XDrawLine(DADisplay, pixmap, dockapp_border2GC, 0, 0, 0, height - 2);
181   XDrawLine(DADisplay, pixmap, dockapp_border2GC, 1, 0, width - 1, 0);
182
183   XDrawLine(DADisplay, pixmap, dockapp_border1GC, 0, height - 1, width - 1, height - 1);
184   XDrawLine(DADisplay, pixmap, dockapp_border1GC, width - 1, 1, width - 1, height - 2);
185 }
186
187 int clicked(DARect *rect, int x, int y) {
188   if (rect->x > x) return 0;
189   if (rect->y > y) return 0;
190   if (rect->x + rect->width < x) return 0;
191   if (rect->y + rect->height < y) return 0;
192   return 1;
193 }
194
195 void change(int button, int state, int x, int y) {
196   client_t **clients;
197   unsigned long i, num_clients;
198   unsigned long active_desktop, active_window;
199   unsigned int dockapp_width, dockapp_height;
200   XEvent event;
201   Window root = DefaultRootWindow(DADisplay);
202
203   event.type = ClientMessage;
204   event.xclient.type = ClientMessage;
205   event.xclient.window = root;
206   event.xclient.message_type = 0;
207   event.xclient.format = 32;
208
209   enumerate_clients(&clients, &num_clients, &active_desktop, &active_window, &dockapp_width, &dockapp_height);
210   for (i = num_clients - 1; i > 0; i--) {
211     if (! clients[i]->window) continue;
212     if (! clicked(&clients[i]->rect, x, y)) continue;
213
214     /* XXX: Use NET_RESTACK_WINDOW instead. */
215     if (button == 3 || button == 4) {
216       XRaiseWindow(DADisplay, clients[i]->window);
217     }
218     else if (button == 2 || button == 5) {
219       XLowerWindow(DADisplay, clients[i]->window);
220     }
221     else {
222       event.xclient.window = clients[i]->window;
223       event.xclient.message_type = active_window_atom;
224       event.xclient.data.l[0] = 2;
225       event.xclient.data.l[1] = 0; /* XXX: NET_WM_USER_TIME */
226       event.xclient.data.l[2] = 0;
227     }
228     break;
229   }
230   destroy_clients(&clients, num_clients);
231
232   if (! event.xclient.message_type) {
233     event.xclient.message_type = current_desktop_atom;
234     event.xclient.data.l[0] = desktop;
235   }
236
237   XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event);
238 }
239
240 void page() {
241   Window child, active_window;
242   XWindowAttributes attr;
243   Pixmap pixmap;
244   DARect rect;
245   long i, j;
246   unsigned long num_clients;
247   long client_desktop, active_desktop;
248   int active;
249   GC gc;
250   client_t **clients;
251   unsigned int dockapp_width, dockapp_height;
252
253   enumerate_clients(&clients, &num_clients, &active_desktop, &active_window, &dockapp_width, &dockapp_height);
254
255   pixmap = DAMakePixmap();
256   if (active_desktop == desktop) gc = active_desktopGC;
257   else gc = inactive_desktopGC;
258   draw_relief(pixmap, dockapp_width, dockapp_height, gc);
259
260   for (i = 0; i < num_clients; i++) {
261     if (! clients[i]->window) continue;
262     if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) gc = window_border1GC;
263     else if (clients[i]->window == active_window) gc = active_windowGC;
264     else gc = inactive_windowGC;
265     XFillRectangle(DADisplay, pixmap, gc, clients[i]->rect.x, clients[i]->rect.y, clients[i]->rect.width, clients[i]->rect.height);
266     if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) continue;
267
268     /* Thumbnail. */
269 #if 0
270       XRenderPictureAttributes pa;
271       Pixmap client_pixmap = XCreatePixmap(DADisplay, wins[i], width, height, DefaultDepth(DADisplay, DefaultScreen(DADisplay)));
272       //XCopyArea(DADisplay, wins[i], client_pixmap, DefaultGC(DADisplay, DefaultScreen(DADisplay)), 0, 0, width, height, 0, 0);
273       Picture client_picture = XRenderCreatePicture(DADisplay, client_pixmap, XRenderFindVisualFormat(DADisplay, attr.visual), CPSubwindowMode, &pa);
274
275       XTransform xform = {{
276         { XDoubleToFixed(1.0), 0, 0 },
277         { 0, XDoubleToFixed(1.0), 0 },
278         { 0, 0, XDoubleToFixed(scale) },
279       }};
280
281       XRenderSetPictureFilter(DADisplay, client_picture, FilterBilinear, 0, 0);
282       XRenderSetPictureTransform(DADisplay, client_picture, &xform);
283       XRenderComposite(DADisplay, PictOpSrc, client_picture, None, picture, 0, 0, 0, 0, rect.x, rect.y, rect.width, rect.height);
284       XFreePixmap(DADisplay, client_pixmap);
285       XSync(DADisplay, False);
286 #endif
287
288     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);
289     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);
290     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);
291     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);
292   }
293
294   destroy_clients(&clients, num_clients);
295
296   DASetPixmap(pixmap);
297   XFreePixmap(DADisplay, pixmap);
298 }
299
300 int main(int argc, char **argv) {
301   Display *display;
302   unsigned int root_width, root_height;
303   unsigned int dockapp_width, dockapp_height;
304   long num_desktops;
305   double aspect;
306   DACallbacks callbacks = { 0, change, 0, 0, 0, 0, page };
307   DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
308
309   DASetExpectedVersion(20020126);
310
311   display = XOpenDisplay(0);
312
313   root_width = DisplayWidth(display, DefaultScreen(display));
314   root_height = DisplayHeight(display, DefaultScreen(display));
315   aspect = (double) root_width / (double) root_height;
316
317   if (root_width > root_height) {
318     dockapp_width = DOCKAPP_SIZE;
319     dockapp_height = (double) DOCKAPP_SIZE / aspect;
320   }
321   else {
322     dockapp_height = DOCKAPP_SIZE;
323     dockapp_width = (double) DOCKAPP_SIZE / aspect;
324   }
325
326   DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB");
327   DAOpenDisplay(0, argc, argv);
328   DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
329
330   DASetCallbacks(&callbacks);
331   DASetTimeout(SPEED);
332
333   setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
334   setup_atom(&current_desktop_atom, "_NET_CURRENT_DESKTOP");
335   setup_atom(&client_list_atom, "_NET_CLIENT_LIST_STACKING");
336   setup_atom(&client_desktop_atom, "_NET_WM_DESKTOP");
337   setup_atom(&client_state_atom, "_NET_WM_STATE");
338   setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW");
339
340   setup_atom(&shaded_state, "_NET_WM_STATE_SHADED");
341   setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER");
342   setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN");
343
344   num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
345   if (desktop < 1) {
346     desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
347   }
348   else if (desktop-- > num_desktops) {
349     fprintf(stderr, "There are only %d desktops!\n", num_desktops);
350     exit(111);
351   }
352
353   XSetErrorHandler(error_handler);
354   setup_GCs();
355   DAShow();
356   DAEventLoop();
357 }