1 /* gcc -o pager pager.c -lX11 -lXmu -ldockapp */
3 #include <X11/extensions/Xrender.h>
8 #define DOCKAPP_SIZE 56
10 #define ACTIVE_DESKTOP 1
11 #define ACTIVE_WINDOW 2
13 static GC lightGrayGC, darkGrayGC, active_desktopGC, inactive_desktopGC, active_windowGC, inactive_windowGC;
14 static Atom num_desktops_atom, current_desktop_atom, client_list_atom, client_desktop_atom, client_state_atom, active_window_atom;
15 static Atom shaded_state, skip_pager_state, hidden_state;
16 static long desktop = -1;
18 int error_handler(Display *display, XErrorEvent *event) {
22 switch (event->error_code) {
24 /* The window may have gone away since we queried the window manager. */
28 if (XGetErrorText(display, event->error_code, buffer, sizeof(buffer))) {
29 fprintf(stderr, "%s\n", buffer);
38 void setup_atom(Atom *atom, const char *prop) {
39 *atom = XInternAtom(DADisplay, prop, True);
40 if (*atom == None) exit(111);
43 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
46 unsigned long num_bytes;
48 XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
51 long get_atom_long(Atom atom, Atom type, Window window) {
54 unsigned long num_items;
56 get_atom_longs(atom, type, window, &data, &num_items);
57 if (num_items) ret = data[0];
67 gcv.foreground = DAGetColor("darkGray");
68 XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
70 gcv.foreground = DAGetColor("lightGray");
71 gcv.graphics_exposures = False;
73 lightGrayGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
75 gcv.foreground = DAGetColor("#222222");
76 darkGrayGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
78 gcv.foreground = DAGetColor("#bbbbbb");
79 active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
81 gcv.foreground = DAGetColor("#777777");
82 inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
84 gcv.foreground = DAGetColor("#20b2ae");
85 active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
87 gcv.foreground = DAGetColor("#999999");
88 inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
91 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
93 XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
95 XDrawLine(DADisplay, pixmap, darkGrayGC, 0, 0, 0, height - 2);
96 XDrawLine(DADisplay, pixmap, darkGrayGC, 1, 0, width - 1, 0);
98 XDrawLine(DADisplay, pixmap, lightGrayGC, 0, height - 1, width - 1, height - 1);
99 XDrawLine(DADisplay, pixmap, lightGrayGC, width - 1, 1, width - 1, height - 2);
104 Window root = DefaultRootWindow(DADisplay);
106 event.type = ClientMessage;
107 event.xclient.type = ClientMessage;
108 event.xclient.window = root;
109 event.xclient.message_type = current_desktop_atom;
110 event.xclient.format = 32;
111 event.xclient.data.l[0] = desktop;
113 XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event);
117 Window root, dockapp_root, child, active_window;
118 XWindowAttributes attr;
122 unsigned long num_clients, num_states;
123 unsigned int root_width, root_height;
124 unsigned int dockapp_x, dockapp_y, dockapp_width, dockapp_height, dockapp_border, dockapp_depth;
125 unsigned int x, y, width, height, border, depth;
127 long *clients, *states;
128 long client_desktop, active_desktop;
132 /* XXX: Use _NET_DESKTOP_GEOMETRY. */
133 root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
134 root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
136 pixmap = DAMakePixmap();
137 XGetGeometry(DADisplay, pixmap, &dockapp_root, &dockapp_x, &dockapp_y, &dockapp_width, &dockapp_height, &dockapp_border, &dockapp_depth);
138 scale = (double) root_width / (double) dockapp_width;
140 active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root);
141 active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
143 if (active_desktop == desktop) gc = active_desktopGC;
144 else gc = inactive_desktopGC;
145 draw_relief(pixmap, dockapp_width, dockapp_height, gc);
147 get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &clients, &num_clients);
148 for (i = 0; i < num_clients; i++) {
150 client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, clients[i]);
151 if (client_desktop != desktop && client_desktop != -1) continue;
152 if (! XGetGeometry(DADisplay, clients[i], &root, &x, &y, &width, &height, &border, &depth)) continue;
153 if (! XTranslateCoordinates(DADisplay, clients[i], root, x, y, &x, &y, &child)) continue;
154 get_atom_longs(client_state_atom, XA_ATOM, clients[i], &states, &num_states);
155 for (j = 0; j < num_states; j++) {
156 if (states[j] == shaded_state) draw = 0;
157 if (states[j] == skip_pager_state) draw = 0;
158 if (states[j] == hidden_state) draw = 0;
160 if (! draw) continue;
162 rect.x = (double) x / scale;
163 rect.y = (double) y / scale;
164 rect.width = (double) width / scale;
165 rect.height = (double) height / scale;
167 if (clients[i] == active_window) gc = active_windowGC;
168 else gc = inactive_windowGC;
169 XFillRectangle(DADisplay, pixmap, gc, rect.x, rect.y, rect.width, rect.height);
173 XRenderPictureAttributes pa;
174 Pixmap client_pixmap = XCreatePixmap(DADisplay, wins[i], width, height, DefaultDepth(DADisplay, DefaultScreen(DADisplay)));
175 //XCopyArea(DADisplay, wins[i], client_pixmap, DefaultGC(DADisplay, DefaultScreen(DADisplay)), 0, 0, width, height, 0, 0);
176 Picture client_picture = XRenderCreatePicture(DADisplay, client_pixmap, XRenderFindVisualFormat(DADisplay, attr.visual), CPSubwindowMode, &pa);
178 XTransform xform = {{
179 { XDoubleToFixed(1.0), 0, 0 },
180 { 0, XDoubleToFixed(1.0), 0 },
181 { 0, 0, XDoubleToFixed(scale) },
184 XRenderSetPictureFilter(DADisplay, client_picture, FilterBilinear, 0, 0);
185 XRenderSetPictureTransform(DADisplay, client_picture, &xform);
186 XRenderComposite(DADisplay, PictOpSrc, client_picture, None, picture, 0, 0, 0, 0, rect.x, rect.y, rect.width, rect.height);
187 XFreePixmap(DADisplay, client_pixmap);
188 XSync(DADisplay, False);
191 XDrawLine(DADisplay, pixmap, darkGrayGC, rect.x, rect.y, rect.x, rect.y + rect.height - 2);
192 XDrawLine(DADisplay, pixmap, darkGrayGC, rect.x + 1, rect.y, rect.x + rect.width - 1, rect.y);
193 XDrawLine(DADisplay, pixmap, lightGrayGC, rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1);
194 XDrawLine(DADisplay, pixmap, lightGrayGC, rect.x + rect.width - 1, rect.y + 1, rect.x + rect.width - 1, rect.y + rect.height - 2);
198 XFreePixmap(DADisplay, pixmap);
201 int main(int argc, char **argv) {
203 unsigned int root_width, root_height;
204 unsigned int dockapp_width, dockapp_height;
207 DACallbacks callbacks = { 0, change, 0, 0, 0, 0, page };
208 DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
210 DASetExpectedVersion(20020126);
212 display = XOpenDisplay(0);
214 root_width = DisplayWidth(display, DefaultScreen(display));
215 root_height = DisplayHeight(display, DefaultScreen(display));
216 aspect = (double) root_width / (double) root_height;
218 if (root_width > root_height) {
219 dockapp_width = DOCKAPP_SIZE;
220 dockapp_height = (double) DOCKAPP_SIZE / aspect;
223 dockapp_height = DOCKAPP_SIZE;
224 dockapp_width = (double) DOCKAPP_SIZE / aspect;
227 DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB");
228 DAOpenDisplay(0, argc, argv);
229 DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
231 DASetCallbacks(&callbacks);
234 setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
235 setup_atom(¤t_desktop_atom, "_NET_CURRENT_DESKTOP");
236 setup_atom(&client_list_atom, "_NET_CLIENT_LIST_STACKING");
237 setup_atom(&client_desktop_atom, "_NET_WM_DESKTOP");
238 setup_atom(&client_state_atom, "_NET_WM_STATE");
239 setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW");
241 setup_atom(&shaded_state, "_NET_WM_STATE_SHADED");
242 setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER");
243 setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN");
245 num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
247 desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
249 else if (desktop-- > num_desktops) {
250 fprintf(stderr, "There are only %d desktops!\n", num_desktops);
254 XSetErrorHandler(error_handler);