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 void setup_atom(Atom *atom, const char *prop) {
19 *atom = XInternAtom(DADisplay, prop, True);
20 if (*atom == None) exit(111);
23 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
26 unsigned long num_bytes;
28 XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
31 long get_atom_long(Atom atom, Atom type, Window window) {
34 unsigned long num_items;
36 get_atom_longs(atom, type, window, &data, &num_items);
37 if (num_items) ret = data[0];
47 gcv.foreground = DAGetColor("darkGray");
48 XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
50 gcv.foreground = DAGetColor("lightGray");
51 gcv.graphics_exposures = False;
53 lightGrayGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
55 gcv.foreground = DAGetColor("#222222");
56 darkGrayGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
58 gcv.foreground = DAGetColor("#bbbbbb");
59 active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
61 gcv.foreground = DAGetColor("#777777");
62 inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
64 gcv.foreground = DAGetColor("#20b2ae");
65 active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
67 gcv.foreground = DAGetColor("#999999");
68 inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
71 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
73 XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
75 XDrawLine(DADisplay, pixmap, darkGrayGC, 0, 0, 0, height - 2);
76 XDrawLine(DADisplay, pixmap, darkGrayGC, 1, 0, width - 1, 0);
78 XDrawLine(DADisplay, pixmap, lightGrayGC, 0, height - 1, width - 1, height - 1);
79 XDrawLine(DADisplay, pixmap, lightGrayGC, width - 1, 1, width - 1, height - 2);
84 Window root = DefaultRootWindow(DADisplay);
86 event.type = ClientMessage;
87 event.xclient.type = ClientMessage;
88 event.xclient.window = root;
89 event.xclient.message_type = current_desktop_atom;
90 event.xclient.format = 32;
91 event.xclient.data.l[0] = desktop;
93 XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event);
97 Window root, dockapp_root, child, active_window;
98 XWindowAttributes attr;
102 unsigned long num_clients, num_states;
103 unsigned int root_width, root_height;
104 unsigned int dockapp_x, dockapp_y, dockapp_width, dockapp_height, dockapp_border, dockapp_depth;
105 unsigned int x, y, width, height, border, depth;
107 long *clients, *states;
108 long client_desktop, active_desktop;
112 /* XXX: Use _NET_DESKTOP_GEOMETRY. */
113 root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
114 root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
116 pixmap = DAMakePixmap();
117 XGetGeometry(DADisplay, pixmap, &dockapp_root, &dockapp_x, &dockapp_y, &dockapp_width, &dockapp_height, &dockapp_border, &dockapp_depth);
118 scale = (double) root_width / (double) dockapp_width;
120 active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root);
121 active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
123 if (active_desktop == desktop) gc = active_desktopGC;
124 else gc = inactive_desktopGC;
125 draw_relief(pixmap, dockapp_width, dockapp_height, gc);
127 get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &clients, &num_clients);
128 for (i = 0; i < num_clients; i++) {
130 client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, clients[i]);
131 if (client_desktop != desktop && client_desktop != -1) continue;
132 if (! XGetGeometry(DADisplay, clients[i], &root, &x, &y, &width, &height, &border, &depth)) continue;
133 if (! XTranslateCoordinates(DADisplay, clients[i], root, x, y, &x, &y, &child)) continue;
134 get_atom_longs(client_state_atom, XA_ATOM, clients[i], &states, &num_states);
135 for (j = 0; j < num_states; j++) {
136 if (states[j] == shaded_state) draw = 0;
137 if (states[j] == skip_pager_state) draw = 0;
138 if (states[j] == hidden_state) draw = 0;
140 if (! draw) continue;
142 rect.x = (double) x / scale;
143 rect.y = (double) y / scale;
144 rect.width = (double) width / scale;
145 rect.height = (double) height / scale;
147 if (clients[i] == active_window) gc = active_windowGC;
148 else gc = inactive_windowGC;
149 XFillRectangle(DADisplay, pixmap, gc, rect.x, rect.y, rect.width, rect.height);
153 XRenderPictureAttributes pa;
154 Pixmap client_pixmap = XCreatePixmap(DADisplay, wins[i], width, height, DefaultDepth(DADisplay, DefaultScreen(DADisplay)));
155 //XCopyArea(DADisplay, wins[i], client_pixmap, DefaultGC(DADisplay, DefaultScreen(DADisplay)), 0, 0, width, height, 0, 0);
156 Picture client_picture = XRenderCreatePicture(DADisplay, client_pixmap, XRenderFindVisualFormat(DADisplay, attr.visual), CPSubwindowMode, &pa);
158 XTransform xform = {{
159 { XDoubleToFixed(1.0), 0, 0 },
160 { 0, XDoubleToFixed(1.0), 0 },
161 { 0, 0, XDoubleToFixed(scale) },
164 XRenderSetPictureFilter(DADisplay, client_picture, FilterBilinear, 0, 0);
165 XRenderSetPictureTransform(DADisplay, client_picture, &xform);
166 XRenderComposite(DADisplay, PictOpSrc, client_picture, None, picture, 0, 0, 0, 0, rect.x, rect.y, rect.width, rect.height);
167 XFreePixmap(DADisplay, client_pixmap);
168 XSync(DADisplay, False);
171 XDrawLine(DADisplay, pixmap, darkGrayGC, rect.x, rect.y, rect.x, rect.y + rect.height - 2);
172 XDrawLine(DADisplay, pixmap, darkGrayGC, rect.x + 1, rect.y, rect.x + rect.width - 1, rect.y);
173 XDrawLine(DADisplay, pixmap, lightGrayGC, rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1);
174 XDrawLine(DADisplay, pixmap, lightGrayGC, rect.x + rect.width - 1, rect.y + 1, rect.x + rect.width - 1, rect.y + rect.height - 2);
178 XFreePixmap(DADisplay, pixmap);
181 int main(int argc, char **argv) {
183 unsigned int root_width, root_height;
184 unsigned int dockapp_width, dockapp_height;
187 DACallbacks callbacks = { 0, change, 0, 0, 0, 0, page };
188 DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
190 DASetExpectedVersion(20020126);
192 display = XOpenDisplay(0);
194 root_width = DisplayWidth(display, DefaultScreen(display));
195 root_height = DisplayHeight(display, DefaultScreen(display));
196 aspect = (double) root_width / (double) root_height;
198 if (root_width > root_height) {
199 dockapp_width = DOCKAPP_SIZE;
200 dockapp_height = (double) DOCKAPP_SIZE / aspect;
203 dockapp_height = DOCKAPP_SIZE;
204 dockapp_width = (double) DOCKAPP_SIZE / aspect;
207 DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB");
208 DAOpenDisplay(0, argc, argv);
209 DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
211 DASetCallbacks(&callbacks);
214 setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
215 setup_atom(¤t_desktop_atom, "_NET_CURRENT_DESKTOP");
216 setup_atom(&client_list_atom, "_NET_CLIENT_LIST_STACKING");
217 setup_atom(&client_desktop_atom, "_NET_WM_DESKTOP");
218 setup_atom(&client_state_atom, "_NET_WM_STATE");
219 setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW");
221 setup_atom(&shaded_state, "_NET_WM_STATE_SHADED");
222 setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER");
223 setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN");
225 num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
227 desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
229 else if (desktop-- > num_desktops) {
230 fprintf(stderr, "There are only %d desktops!\n", num_desktops);