1 /* gcc -o pager pager.c -lX11 -lXmu -ldockapp */
3 #include <X11/extensions/Xrender.h>
8 #define DOCKAPP_SIZE 56
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;
25 static int desktop_geometry_supported;
26 static int current_desktop_supported;
27 static int active_window_supported;
28 static int moveresize_supported;
35 int error_handler(Display *display, XErrorEvent *event) {
39 switch (event->error_code) {
41 /* The window may have gone away since we queried the window manager. */
45 if (XGetErrorText(display, event->error_code, buffer, sizeof(buffer))) {
46 fprintf(stderr, "%s\n", buffer);
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);
62 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
65 unsigned long num_bytes;
67 XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
70 long get_atom_long(Atom atom, Atom type, Window window) {
73 unsigned long num_items;
75 get_atom_longs(atom, type, window, &data, &num_items);
76 if (num_items) ret = data[0];
82 void check_support() {
84 unsigned long i, num_items;
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;
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;
100 unsigned int width, height, border, depth;
101 unsigned int root_width, root_height;
104 unsigned long i, j, num_states;
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];
115 root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
116 root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
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;
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));
125 get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &data, num_clients);
127 *clients = (client_t **) malloc(sizeof(client_t *) * *num_clients);
129 fprintf(stderr, "Out of memory for clients!\n");
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");
139 (*clients)[i]->window = 0;
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;
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;
149 /* Make sure it isn't hidden or shaded. */
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;
157 if (! draw) continue;
159 (*clients)[i]->window = data[i];
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;
171 void destroy_clients(client_t ***clients, unsigned long num_clients) {
174 if (! *clients) return;
176 for (i = 0; i < num_clients; i++) free((*clients)[i]);
184 gcv.foreground = DAGetColor("darkGray");
185 XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
187 gcv.foreground = DAGetColor("lightGray");
188 gcv.graphics_exposures = False;
190 dockapp_border1GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
192 gcv.foreground = DAGetColor("#222222");
193 dockapp_border2GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
195 gcv.foreground = DAGetColor("#0088ff");
196 active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
198 gcv.foreground = DAGetColor("darkGray");
199 inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
201 gcv.foreground = DAGetColor("white");
202 active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
204 gcv.foreground = DAGetColor("#999999");
205 inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
207 gcv.foreground = DAGetColor("black");
208 window_border1GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
210 gcv.foreground = DAGetColor("#333333");
211 window_border2GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
214 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
216 XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
218 XDrawLine(DADisplay, pixmap, dockapp_border2GC, 0, 0, 0, height - 2);
219 XDrawLine(DADisplay, pixmap, dockapp_border2GC, 1, 0, width - 1, 0);
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);
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;
233 int client_message(Atom message_type, Window window, long data0, long data1, long data2, long data3, long data4) {
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;
249 return XSendEvent(DADisplay, DefaultRootWindow(DADisplay), False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
252 void change(int button, int state, int x, int y) {
254 unsigned long i, num_clients;
255 unsigned long active_desktop, active_window;
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;
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);
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);
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;
278 destroy_clients(&clients, num_clients);
280 if (drag_window && button == 1) {
281 client_message(active_window_atom, drag_window, FromTool, CurrentTime, 0, 0 ,0);
284 client_message(current_desktop_atom, DefaultRootWindow(DADisplay), desktop, 0, 0, 0, 0);
288 void release(int button, int state, int x, int y) {
289 if (button == drag_button) drag_window = 0;
292 void move(int x, int y) {
295 if (! drag_window) return;
296 if (x < 0 || y < 0) return;
297 if (x >= dockapp_width || y >= dockapp_height) return;
299 dest_x = (long) ((double) (x - drag_x) * scale);
300 dest_y = (long) ((double) (y - drag_y) * scale);
302 if (moveresize_supported) {
303 client_message(moveresize_window_atom, drag_window, StaticGravity | (3 << 8) | (FromTool << 12), drag_x, drag_y, 0, 0);
306 XMoveWindow(DADisplay, drag_window, dest_x, dest_y);
313 Window child, active_window;
314 XWindowAttributes attr;
318 unsigned long num_clients;
319 long client_desktop, active_desktop;
324 enumerate_clients(&clients, &num_clients, &active_desktop, &active_window);
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);
331 for (i = 0; i < num_clients; i++) {
332 if (! clients[i]->window) continue;
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;
339 if (clients[i]->rect.y < 0) {
340 clients[i]->rect.height += clients[i]->rect.y;
341 clients[i]->rect.y = 0;
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;
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;
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);
359 XTransform xform = {{
360 { XDoubleToFixed(1.0), 0, 0 },
361 { 0, XDoubleToFixed(1.0), 0 },
362 { 0, 0, XDoubleToFixed(scale) },
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);
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);
378 destroy_clients(&clients, num_clients);
381 XFreePixmap(DADisplay, pixmap);
384 int main(int argc, char **argv) {
386 unsigned int root_width, root_height;
389 DACallbacks callbacks = { 0, change, release, move, 0, 0, page };
390 DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
392 DASetExpectedVersion(20020126);
394 display = XOpenDisplay(0);
396 root_width = DisplayWidth(display, DefaultScreen(display));
397 root_height = DisplayHeight(display, DefaultScreen(display));
398 aspect = (double) root_width / (double) root_height;
400 if (root_width > root_height) {
401 dockapp_width = DOCKAPP_SIZE;
402 dockapp_height = (double) DOCKAPP_SIZE / aspect;
405 dockapp_height = DOCKAPP_SIZE;
406 dockapp_width = (double) DOCKAPP_SIZE / aspect;
409 DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB");
410 DAOpenDisplay(0, argc, argv);
411 DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
413 DASetCallbacks(&callbacks);
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(¤t_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");
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");
434 num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
436 desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
438 else if (desktop-- > num_desktops) {
439 fprintf(stderr, "There are only %d desktops!\n", num_desktops);
443 XSetErrorHandler(error_handler);