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 above_state, below_state, shaded_state, skip_pager_state, hidden_state;
15 static long 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;
23 static int desktop_geometry_supported;
24 static int current_desktop_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] == active_window_atom) active_window_supported = 1;
88 if (data[i] == moveresize_window_atom) moveresize_supported = 1;
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;
97 unsigned int width, height, border, depth;
98 unsigned int root_width, root_height;
101 unsigned long i, j, num_states;
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[0];
112 root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
113 root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
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;
119 *active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root);
120 *active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
122 get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &data, num_clients);
124 *clients = (client_t **) malloc(sizeof(client_t *) * *num_clients);
126 fprintf(stderr, "Out of memory for clients!\n");
130 for (i = 0; i < *num_clients; i++) {
131 (*clients)[i] = (client_t *) malloc(sizeof(client_t));
132 if (! (*clients)[i]) {
133 fprintf(stderr, "Out of memory for client!\n");
136 (*clients)[i]->window = 0;
138 /* Check the window is on our desktop (or all desktops). */
139 client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, data[i]);
140 if (client_desktop != desktop && client_desktop != -1) continue;
142 /* Try to get its dimensions. */
143 if (! XGetGeometry(DADisplay, data[i], &root, &x, &y, &width, &height, &border, &depth)) continue;
144 if (! XTranslateCoordinates(DADisplay, data[i], root, x, y, &x, &y, &child)) continue;
146 /* Make sure it isn't hidden or shaded. */
148 get_atom_longs(client_state_atom, XA_ATOM, data[i], &states, &num_states);
149 for (j = 0; j < num_states; j++) {
150 if (states[j] == shaded_state) draw = 0;
151 if (states[j] == skip_pager_state) draw = 0;
152 if (states[j] == hidden_state) draw = 0;
154 if (! draw) continue;
156 (*clients)[i]->window = data[i];
159 (*clients)[i]->rect.x = (double) x / scale;
160 (*clients)[i]->rect.y = (double) y / scale;
161 (*clients)[i]->rect.width = (double) width / scale;
162 (*clients)[i]->rect.height = (double) height / scale;
168 void destroy_clients(client_t ***clients, unsigned long num_clients) {
171 if (! *clients) return;
173 for (i = 0; i < num_clients; i++) free((*clients)[i]);
181 gcv.foreground = DAGetColor("darkGray");
182 XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
184 gcv.foreground = DAGetColor("lightGray");
185 gcv.graphics_exposures = False;
187 dockapp_border1GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
189 gcv.foreground = DAGetColor("#222222");
190 dockapp_border2GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
192 gcv.foreground = DAGetColor("#0088ff");
193 active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
195 gcv.foreground = DAGetColor("darkGray");
196 inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
198 gcv.foreground = DAGetColor("white");
199 active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
201 gcv.foreground = DAGetColor("#999999");
202 inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
204 gcv.foreground = DAGetColor("black");
205 window_border1GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
207 gcv.foreground = DAGetColor("#333333");
208 window_border2GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
211 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
213 XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
215 XDrawLine(DADisplay, pixmap, dockapp_border2GC, 0, 0, 0, height - 2);
216 XDrawLine(DADisplay, pixmap, dockapp_border2GC, 1, 0, width - 1, 0);
218 XDrawLine(DADisplay, pixmap, dockapp_border1GC, 0, height - 1, width - 1, height - 1);
219 XDrawLine(DADisplay, pixmap, dockapp_border1GC, width - 1, 1, width - 1, height - 2);
222 int clicked(DARect *rect, int x, int y) {
223 if (rect->x > x) return 0;
224 if (rect->y > y) return 0;
225 if (rect->x + rect->width < x) return 0;
226 if (rect->y + rect->height < y) return 0;
230 int client_message(Atom message_type, Window window, long data0, long data1, long data2, long data3, long data4) {
233 memset(&event, 0, sizeof(event));
234 event.type = ClientMessage;
235 event.xclient.type = ClientMessage;
236 event.xclient.send_event = True;
237 event.xclient.window = window;
238 event.xclient.message_type = message_type;
239 event.xclient.format = 32;
240 event.xclient.data.l[0] = data0;
241 event.xclient.data.l[1] = data1;
242 event.xclient.data.l[2] = data2;
243 event.xclient.data.l[3] = data3;
244 event.xclient.data.l[4] = data4;
246 return XSendEvent(DADisplay, DefaultRootWindow(DADisplay), False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
249 void change(int button, int state, int x, int y) {
251 unsigned long i, num_clients;
252 unsigned long active_desktop, active_window;
254 enumerate_clients(&clients, &num_clients, &active_desktop, &active_window);
255 for (i = num_clients - 1; i > 0; i--) {
256 if (! clients[i]->window) continue;
257 if (! clicked(&clients[i]->rect, x, y)) continue;
259 drag_window = (Window) clients[i]->window;
260 drag_button = button;
261 drag_x = x - clients[i]->rect.x;
262 drag_y = y - clients[i]->rect.y;
265 destroy_clients(&clients, num_clients);
267 if (drag_window && button == 1) {
268 client_message(active_window_atom, drag_window, FromTool, CurrentTime, 0, 0 ,0);
271 client_message(current_desktop_atom, DefaultRootWindow(DADisplay), desktop, 0, 0, 0, 0);
275 void release(int button, int state, int x, int y) {
276 if (button == drag_button) drag_window = 0;
280 Window active_window;
283 unsigned long num_clients;
284 unsigned long active_desktop;
288 enumerate_clients(&clients, &num_clients, &active_desktop, &active_window);
290 pixmap = DAMakePixmap();
291 if (active_desktop == desktop) gc = active_desktopGC;
292 else gc = inactive_desktopGC;
293 draw_relief(pixmap, dockapp_width, dockapp_height, gc);
295 for (i = 0; i < num_clients; i++) {
296 if (! clients[i]->window) continue;
298 /* Draw windows which are partially off-screen. */
299 if (clients[i]->rect.x < 0) {
300 clients[i]->rect.width += clients[i]->rect.x;
301 clients[i]->rect.x = 0;
303 if (clients[i]->rect.y < 0) {
304 clients[i]->rect.height += clients[i]->rect.y;
305 clients[i]->rect.y = 0;
308 /* Draw small windows. */
309 if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) gc = window_border1GC;
310 else if ((long) clients[i]->window == active_window) gc = active_windowGC;
311 else gc = inactive_windowGC;
313 XFillRectangle(DADisplay, pixmap, gc, clients[i]->rect.x, clients[i]->rect.y, clients[i]->rect.width, clients[i]->rect.height);
314 if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) continue;
316 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);
317 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);
318 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);
319 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);
322 destroy_clients(&clients, num_clients);
325 XFreePixmap(DADisplay, pixmap);
328 void move(int x, int y) {
331 if (! drag_window) return;
332 if (x < 0 || y < 0) return;
333 if (x >= dockapp_width || y >= dockapp_height) return;
335 dest_x = (long) ((double) (x - drag_x) * scale);
336 dest_y = (long) ((double) (y - drag_y) * scale);
338 if (moveresize_supported) {
339 client_message(moveresize_window_atom, drag_window, StaticGravity | (3 << 8) | (FromTool << 12), drag_x, drag_y, 0, 0);
342 XMoveWindow(DADisplay, drag_window, dest_x, dest_y);
348 int main(int argc, char **argv) {
350 unsigned int root_width, root_height;
353 DACallbacks callbacks = { 0, change, release, move, 0, 0, page };
354 DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
356 DASetExpectedVersion(20020126);
358 display = XOpenDisplay(0);
360 root_width = DisplayWidth(display, DefaultScreen(display));
361 root_height = DisplayHeight(display, DefaultScreen(display));
362 aspect = (double) root_width / (double) root_height;
364 if (root_width > root_height) {
365 dockapp_width = DOCKAPP_SIZE;
366 dockapp_height = (double) DOCKAPP_SIZE / aspect;
369 dockapp_height = DOCKAPP_SIZE;
370 dockapp_width = (double) DOCKAPP_SIZE / aspect;
373 DAParseArguments(argc, argv, &options, 1, "OPTIONS", "pager");
374 DAOpenDisplay(0, argc, argv);
375 DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
377 DASetCallbacks(&callbacks);
380 setup_atom(&supported_atom, "_NET_SUPPORTED");
381 setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
382 setup_atom(&desktop_geometry_atom, "_NET_DESKTOP_GEOMETRY");
383 setup_atom(¤t_desktop_atom, "_NET_CURRENT_DESKTOP");
384 setup_atom(&client_list_atom, "_NET_CLIENT_LIST_STACKING");
385 setup_atom(&client_desktop_atom, "_NET_WM_DESKTOP");
386 setup_atom(&client_state_atom, "_NET_WM_STATE");
387 setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW");
388 setup_atom(&moveresize_window_atom, "_NET_MOVERESIZE_WINDOW");
390 setup_atom(&above_state, "_NET_WM_STATE_ABOVE");
391 setup_atom(&below_state, "_NET_WM_STATE_BELOW");
392 setup_atom(&shaded_state, "_NET_WM_STATE_SHADED");
393 setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER");
394 setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN");
398 num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
400 desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
402 else if (desktop-- > num_desktops) {
403 fprintf(stderr, "There are only %ld desktops!\n", num_desktops);
407 XSetErrorHandler(error_handler);