1 /* gcc -o pager pager.c -lX11 -lXmu -ldockapp */
3 #include <X11/extensions/Xrender.h>
8 #define DOCKAPP_SIZE 56
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;
21 int error_handler(Display *display, XErrorEvent *event) {
25 switch (event->error_code) {
27 /* The window may have gone away since we queried the window manager. */
31 if (XGetErrorText(display, event->error_code, buffer, sizeof(buffer))) {
32 fprintf(stderr, "%s\n", buffer);
41 void setup_atom(Atom *atom, const char *prop) {
42 *atom = XInternAtom(DADisplay, prop, True);
43 if (*atom == None) exit(111);
46 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
49 unsigned long num_bytes;
51 XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
54 long get_atom_long(Atom atom, Atom type, Window window) {
57 unsigned long num_items;
59 get_atom_longs(atom, type, window, &data, &num_items);
60 if (num_items) ret = data[0];
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;
75 unsigned long i, j, num_states;
79 /* XXX: Use _NET_DESKTOP_GEOMETRY. */
80 root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
81 root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
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;
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));
89 get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &data, num_clients);
91 *clients = (client_t **) malloc(sizeof(client_t *) * *num_clients);
93 fprintf(stderr, "Out of memory for clients!\n");
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");
103 (*clients)[i]->window = 0;
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;
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;
113 /* Make sure it isn't hidden or shaded. */
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;
121 if (! draw) continue;
123 (*clients)[i]->window = data[i];
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;
133 void destroy_clients(client_t ***clients, unsigned long num_clients) {
136 if (! *clients) return;
138 for (i = 0; i < num_clients; i++) free((*clients)[i]);
146 gcv.foreground = DAGetColor("darkGray");
147 XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
149 gcv.foreground = DAGetColor("lightGray");
150 gcv.graphics_exposures = False;
152 dockapp_border1GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
154 gcv.foreground = DAGetColor("#222222");
155 dockapp_border2GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
157 gcv.foreground = DAGetColor("#0088ff");
158 active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
160 gcv.foreground = DAGetColor("darkGray");
161 inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
163 gcv.foreground = DAGetColor("white");
164 active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
166 gcv.foreground = DAGetColor("#999999");
167 inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
169 gcv.foreground = DAGetColor("black");
170 window_border1GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
172 gcv.foreground = DAGetColor("#333333");
173 window_border2GC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
176 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
178 XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
180 XDrawLine(DADisplay, pixmap, dockapp_border2GC, 0, 0, 0, height - 2);
181 XDrawLine(DADisplay, pixmap, dockapp_border2GC, 1, 0, width - 1, 0);
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);
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;
195 void change(int button, int state, int x, int y) {
197 unsigned long i, num_clients;
198 unsigned long active_desktop, active_window;
199 unsigned int dockapp_width, dockapp_height;
201 Window root = DefaultRootWindow(DADisplay);
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;
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;
214 /* XXX: Use NET_RESTACK_WINDOW instead. */
215 if (button == 3 || button == 4) {
216 XRaiseWindow(DADisplay, clients[i]->window);
218 else if (button == 2 || button == 5) {
219 XLowerWindow(DADisplay, clients[i]->window);
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;
230 destroy_clients(&clients, num_clients);
232 if (! event.xclient.message_type) {
233 event.xclient.message_type = current_desktop_atom;
234 event.xclient.data.l[0] = desktop;
237 XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event);
241 Window child, active_window;
242 XWindowAttributes attr;
246 unsigned long num_clients;
247 long client_desktop, active_desktop;
251 unsigned int dockapp_width, dockapp_height;
253 enumerate_clients(&clients, &num_clients, &active_desktop, &active_window, &dockapp_width, &dockapp_height);
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);
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;
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);
275 XTransform xform = {{
276 { XDoubleToFixed(1.0), 0, 0 },
277 { 0, XDoubleToFixed(1.0), 0 },
278 { 0, 0, XDoubleToFixed(scale) },
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);
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);
294 destroy_clients(&clients, num_clients);
297 XFreePixmap(DADisplay, pixmap);
300 int main(int argc, char **argv) {
302 unsigned int root_width, root_height;
303 unsigned int dockapp_width, dockapp_height;
306 DACallbacks callbacks = { 0, change, 0, 0, 0, 0, page };
307 DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
309 DASetExpectedVersion(20020126);
311 display = XOpenDisplay(0);
313 root_width = DisplayWidth(display, DefaultScreen(display));
314 root_height = DisplayHeight(display, DefaultScreen(display));
315 aspect = (double) root_width / (double) root_height;
317 if (root_width > root_height) {
318 dockapp_width = DOCKAPP_SIZE;
319 dockapp_height = (double) DOCKAPP_SIZE / aspect;
322 dockapp_height = DOCKAPP_SIZE;
323 dockapp_width = (double) DOCKAPP_SIZE / aspect;
326 DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB");
327 DAOpenDisplay(0, argc, argv);
328 DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
330 DASetCallbacks(&callbacks);
333 setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
334 setup_atom(¤t_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");
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");
344 num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
346 desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
348 else if (desktop-- > num_desktops) {
349 fprintf(stderr, "There are only %d desktops!\n", num_desktops);
353 XSetErrorHandler(error_handler);