1 /* gcc -o pager pager.c -lX11 -lXmu -ldockapp */
6 #define DOCKAPP_SIZE 56
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;
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;
33 int error_handler(Display *display, XErrorEvent *event) {
36 switch (event->error_code) {
38 /* The window may have gone away since we queried the window manager. */
42 if (XGetErrorText(display, event->error_code, buffer, sizeof(buffer))) {
43 fprintf(stderr, "%s\n", buffer);
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);
59 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
62 unsigned long num_bytes;
64 XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
67 long get_atom_long(Atom atom, Atom type, Window window) {
70 unsigned long num_items;
72 get_atom_longs(atom, type, window, &data, &num_items);
73 if (num_items) ret = data[0];
79 void check_support() {
81 unsigned long i, num_items;
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] == client_list_atom) client_list_supported = 1;
88 if (data[i] == active_window_atom) active_window_supported = 1;
89 if (data[i] == moveresize_window_atom) moveresize_supported = 1;
95 void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned long *active_desktop, unsigned long *active_window) {
96 Window root, qroot, qparent, child, *wins, client;
98 unsigned int width, height, border, depth;
99 unsigned int root_width, root_height;
102 unsigned long i, j, num_states;
106 if (desktop_geometry_supported) {
107 get_atom_longs(desktop_geometry_atom, XA_CARDINAL, DefaultRootWindow(DADisplay), &states, &num_states);
108 root_width = states[0];
109 root_height = states[1];
113 root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
114 root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
120 if (! XGetWindowAttributes(DADisplay, DAWindow, &dockapp_attr)) return;
121 scale = (double) root_width / (double) dockapp_attr.width;
122 aspect = (double) root_height / (double) dockapp_attr.height;
124 *active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_attr.root);
125 *active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
127 if (client_list_supported) {
128 get_atom_longs(client_list_atom, XA_WINDOW, dockapp_attr.root, &data, num_clients);
131 XQueryTree(DADisplay, dockapp_attr.root, &qroot, &qparent, &wins, (unsigned int *) num_clients);
134 *clients = (client_t **) malloc(sizeof(client_t *) * *num_clients);
136 fprintf(stderr, "Out of memory for clients!\n");
140 for (i = 0; i < *num_clients; i++) {
141 (*clients)[i] = (client_t *) malloc(sizeof(client_t));
142 if (! (*clients)[i]) {
143 fprintf(stderr, "Out of memory for client!\n");
146 (*clients)[i]->window = 0;
148 if (client_list_supported) client = data[i];
149 else client = XmuClientWindow(DADisplay, wins[i]);
151 /* Don't page the pager. */
152 if (client == DAWindow) continue;
154 /* Check the window is on our desktop (or all desktops). */
155 client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, client);
157 if (client_desktop != desktop && client_desktop != -1) continue;
160 if (client_desktop != *active_desktop && client_desktop != -1) continue;
163 /* Try to get its dimensions. */
164 if (! XGetGeometry(DADisplay, client, &root, &x, &y, &width, &height, &border, &depth)) continue;
165 if (! XTranslateCoordinates(DADisplay, client, root, x, y, &x, &y, &child)) continue;
167 /* Make sure it isn't hidden or shaded. */
169 get_atom_longs(client_state_atom, XA_ATOM, client, &states, &num_states);
170 for (j = 0; j < num_states; j++) {
171 if (states[j] == shaded_state) height = 3.0 * scale;
172 if (states[j] == skip_pager_state) draw = 0;
173 if (states[j] == hidden_state) draw = 0;
175 if (! draw) continue;
177 (*clients)[i]->window = client;
180 (*clients)[i]->rect.x = (double) x / scale;
181 (*clients)[i]->rect.y = (double) y / aspect;
182 (*clients)[i]->rect.width = (double) width / scale;
183 (*clients)[i]->rect.height = (double) height / aspect;
186 if (client_list_supported) XFree(data);
190 void destroy_clients(client_t ***clients, unsigned long num_clients) {
193 if (! *clients) return;
195 for (i = 0; i < num_clients; i++) if ((*clients)[i]) free((*clients)[i]);
203 gcv.foreground = DAGetColor("darkGray");
204 XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
206 gcv.foreground = DAGetColor("lightGray");
207 gcv.graphics_exposures = False;
209 dockapp_border1GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
211 gcv.foreground = DAGetColor("#222222");
212 dockapp_border2GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
214 gcv.foreground = DAGetColor("#0088ff");
215 active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
217 gcv.foreground = DAGetColor("darkGray");
218 inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
220 gcv.foreground = DAGetColor("white");
221 active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
223 gcv.foreground = DAGetColor("#999999");
224 inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
226 gcv.foreground = DAGetColor("black");
227 window_border1GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
229 gcv.foreground = DAGetColor("#333333");
230 window_border2GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
233 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
235 XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
237 XDrawLine(DADisplay, pixmap, dockapp_border2GC, 0, 0, 0, height - 2);
238 XDrawLine(DADisplay, pixmap, dockapp_border2GC, 1, 0, width - 1, 0);
240 XDrawLine(DADisplay, pixmap, dockapp_border1GC, 0, height - 1, width - 1, height - 1);
241 XDrawLine(DADisplay, pixmap, dockapp_border1GC, width - 1, 1, width - 1, height - 2);
244 int clicked(DARect *rect, int x, int y) {
245 if (rect->x > x) return 0;
246 if (rect->y > y) return 0;
247 if (rect->x + rect->width < x) return 0;
248 if (rect->y + rect->height < y) return 0;
252 int client_message(Atom message_type, Window window, long data0, long data1, long data2, long data3, long data4) {
255 memset(&event, 0, sizeof(event));
256 event.type = ClientMessage;
257 event.xclient.type = ClientMessage;
258 event.xclient.send_event = True;
259 event.xclient.window = window;
260 event.xclient.message_type = message_type;
261 event.xclient.format = 32;
262 event.xclient.data.l[0] = data0;
263 event.xclient.data.l[1] = data1;
264 event.xclient.data.l[2] = data2;
265 event.xclient.data.l[3] = data3;
266 event.xclient.data.l[4] = data4;
268 return XSendEvent(DADisplay, DefaultRootWindow(DADisplay), False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
271 void change(int button, int state, int x, int y) {
273 unsigned long i, num_clients;
274 unsigned long active_desktop, active_window;
276 enumerate_clients(&clients, &num_clients, &active_desktop, &active_window);
277 for (i = num_clients - 1; i > 0; i--) {
278 if (! clients[i]->window) continue;
279 if (! clicked(&clients[i]->rect, x, y)) continue;
281 drag_window = (Window) clients[i]->window;
282 drag_button = button;
283 drag_x = x - clients[i]->rect.x;
284 drag_y = y - clients[i]->rect.y;
287 destroy_clients(&clients, num_clients);
289 if (drag_window && button == 1) {
290 client_message(active_window_atom, drag_window, FromTool, CurrentTime, 0, 0 ,0);
293 client_message(current_desktop_atom, DefaultRootWindow(DADisplay), desktop, 0, 0, 0, 0);
297 void release(int button, int state, int x, int y) {
298 if (button == drag_button) drag_window = 0;
301 void draw_window(Pixmap pixmap, GC gc, client_t *client) {
302 /* Draw a blob for the window. */
303 XFillRectangle(DADisplay, pixmap, gc, client->rect.x, client->rect.y, client->rect.width, client->rect.height);
304 if (client->rect.width < 2 || client->rect.height < 2) return;
306 /* Draw a border around the window. */
307 XDrawLine(DADisplay, pixmap, window_border1GC, client->rect.x, client->rect.y, client->rect.x, client->rect.y + client->rect.height - 2);
308 XDrawLine(DADisplay, pixmap, window_border1GC, client->rect.x + 1, client->rect.y, client->rect.x + client->rect.width - 1, client->rect.y);
309 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);
310 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);
312 if (dockapp_attr.width <= DOCKAPP_SIZE * 2) return;
313 if (dockapp_attr.height <= DOCKAPP_SIZE * 2) return;
315 /* Draw a drop shadow. */
316 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);
317 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);
321 Window active_window;
324 unsigned long num_clients;
325 unsigned long active_desktop;
329 enumerate_clients(&clients, &num_clients, &active_desktop, &active_window);
331 pixmap = XCreatePixmap(DADisplay, DAWindow, dockapp_attr.width, dockapp_attr.height, dockapp_attr.depth);
332 if (active_desktop == desktop || desktop < 0) gc = active_desktopGC;
333 else gc = inactive_desktopGC;
334 draw_relief(pixmap, dockapp_attr.width, dockapp_attr.height, gc);
336 for (i = 0; i < num_clients; i++) {
337 if (! clients[i]->window) continue;
339 /* Draw windows which are partially off-screen. */
340 if (clients[i]->rect.x < 0) {
341 clients[i]->rect.width += clients[i]->rect.x;
342 clients[i]->rect.x = 0;
344 if (clients[i]->rect.y < 0) {
345 clients[i]->rect.height += clients[i]->rect.y;
346 clients[i]->rect.y = 0;
349 /* Draw small windows. */
350 if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) gc = window_border1GC;
351 else if ((long) clients[i]->window == active_window) gc = active_windowGC;
352 else gc = inactive_windowGC;
354 draw_window(pixmap, gc, clients[i]);
357 destroy_clients(&clients, num_clients);
360 XFreePixmap(DADisplay, pixmap);
363 void move(int x, int y) {
366 if (! drag_window) return;
367 if (x < 0 || y < 0) return;
368 if (x >= dockapp_attr.width || y >= dockapp_attr.height) return;
370 dest_x = (long) ((double) (x - drag_x) * scale);
371 dest_y = (long) ((double) (y - drag_y) * scale);
373 if (moveresize_supported) {
374 client_message(moveresize_window_atom, drag_window, StaticGravity | (3 << 8) | (FromTool << 12), drag_x, drag_y, 0, 0);
377 XMoveWindow(DADisplay, drag_window, dest_x, dest_y);
383 int main(int argc, char **argv) {
385 unsigned int root_width, root_height;
387 DACallbacks callbacks = { 0, change, release, move, 0, 0, page };
388 DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
390 DASetExpectedVersion(20020126);
392 display = XOpenDisplay(0);
394 root_width = DisplayWidth(display, DefaultScreen(display));
395 root_height = DisplayHeight(display, DefaultScreen(display));
396 aspect = (double) root_width / (double) root_height;
398 if (root_width > root_height) {
399 dockapp_attr.width = DOCKAPP_SIZE;
400 dockapp_attr.height = (double) DOCKAPP_SIZE / aspect;
403 dockapp_attr.height = DOCKAPP_SIZE;
404 dockapp_attr.width = (double) DOCKAPP_SIZE / aspect;
407 DAParseArguments(argc, argv, &options, 1, "OPTIONS", "pager");
408 DAOpenDisplay(0, argc, argv);
409 DACreateIcon("pager", dockapp_attr.width, dockapp_attr.height, argc, argv);
411 DASetCallbacks(&callbacks);
414 setup_atom(&supported_atom, "_NET_SUPPORTED");
415 setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
416 setup_atom(&desktop_geometry_atom, "_NET_DESKTOP_GEOMETRY");
417 setup_atom(¤t_desktop_atom, "_NET_CURRENT_DESKTOP");
418 setup_atom(&client_list_atom, "_NET_CLIENT_LIST_STACKING");
419 setup_atom(&client_desktop_atom, "_NET_WM_DESKTOP");
420 setup_atom(&client_state_atom, "_NET_WM_STATE");
421 setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW");
422 setup_atom(&moveresize_window_atom, "_NET_MOVERESIZE_WINDOW");
424 setup_atom(&shaded_state, "_NET_WM_STATE_SHADED");
425 setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER");
426 setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN");
430 num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
432 desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
434 else if (desktop < 0) desktop = -1;
435 else if (desktop-- > num_desktops) {
436 fprintf(stderr, "There are only %ld desktops!\n", num_desktops);
440 XSetErrorHandler(error_handler);