f5f05c09241f2b6c597b10dd040a782495e53411
[pager.git] / pager.c
1 /* gcc -o pager pager.c -lX11 -lXmu -ldockapp */
2 #if 0
3 #include <X11/extensions/Xrender.h>
4 #endif
5 #include <X11/Xatom.h>
6 #include <dockapp.h>
7
8 #define DOCKAPP_SIZE 56
9 #define SPEED 100
10
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;
15
16 int error_handler(Display *display, XErrorEvent *event) {
17   char buffer[512];
18   int length;
19
20   switch (event->error_code) {
21     case BadWindow:
22       /* The window may have gone away since we queried the window manager. */
23     break;
24
25     default:
26       if (XGetErrorText(display, event->error_code, buffer, sizeof(buffer))) {
27         fprintf(stderr, "%s\n", buffer);
28         exit(100);
29       }
30     break;
31   }
32
33   return 0;
34 }
35
36 void setup_atom(Atom *atom, const char *prop) {
37   *atom = XInternAtom(DADisplay, prop, True);
38   if (*atom == None) exit(111);
39 }
40
41 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
42   Atom actual;
43   int format;
44   unsigned long num_bytes;
45
46   XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
47 }
48
49 long get_atom_long(Atom atom, Atom type, Window window) {
50   long *data;
51   long ret = 0;
52   unsigned long num_items;
53
54   get_atom_longs(atom, type, window, &data, &num_items);
55   if (num_items) ret = data[0];
56   XFree(data);
57
58   return ret;
59 }
60
61 void setup_GCs() {
62   XGCValues gcv;
63
64   /* GC's */
65   gcv.foreground = DAGetColor("darkGray");
66   XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
67
68   gcv.foreground = DAGetColor("lightGray");
69   gcv.graphics_exposures = False;
70
71   dockapp_border1GC     = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
72
73   gcv.foreground = DAGetColor("#222222");
74   dockapp_border2GC     =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
75
76   gcv.foreground = DAGetColor("#0088ff");
77   active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
78
79   gcv.foreground = DAGetColor("darkGray");
80   inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
81
82   gcv.foreground = DAGetColor("white");
83   active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
84
85   gcv.foreground = DAGetColor("#999999");
86   inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
87
88   gcv.foreground = DAGetColor("black");
89   window_border1GC      =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
90
91   gcv.foreground = DAGetColor("#333333");
92   window_border2GC      =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
93 }
94
95 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
96   /* Drawing */
97   XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
98
99   XDrawLine(DADisplay, pixmap, dockapp_border2GC, 0, 0, 0, height - 2);
100   XDrawLine(DADisplay, pixmap, dockapp_border2GC, 1, 0, width - 1, 0);
101
102   XDrawLine(DADisplay, pixmap, dockapp_border1GC, 0, height - 1, width - 1, height - 1);
103   XDrawLine(DADisplay, pixmap, dockapp_border1GC, width - 1, 1, width - 1, height - 2);
104 }
105
106 void change() {
107   XEvent event;
108   Window root = DefaultRootWindow(DADisplay);
109
110   event.type = ClientMessage;
111   event.xclient.type = ClientMessage;
112   event.xclient.window = root;
113   event.xclient.message_type = current_desktop_atom;
114   event.xclient.format = 32;
115   event.xclient.data.l[0] = desktop;
116
117   XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event);
118 }
119
120 void page() {
121   Window root, dockapp_root, child, active_window;
122   XWindowAttributes attr;
123   Pixmap pixmap;
124   DARect rect;
125   long i, j;
126   unsigned long num_clients, num_states;
127   unsigned int root_width, root_height;
128   unsigned int dockapp_x, dockapp_y, dockapp_width, dockapp_height, dockapp_border, dockapp_depth;
129   unsigned int x, y, width, height, border, depth;
130   double scale;
131   long *clients, *states;
132   long client_desktop, active_desktop;
133   int active;
134   GC gc;
135
136   /* XXX: Use _NET_DESKTOP_GEOMETRY. */
137   root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
138   root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
139
140   pixmap = DAMakePixmap();
141   XGetGeometry(DADisplay, pixmap, &dockapp_root, &dockapp_x, &dockapp_y, &dockapp_width, &dockapp_height, &dockapp_border, &dockapp_depth);
142   scale = (double) root_width / (double) dockapp_width;
143
144   active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root);
145   active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
146
147   if (active_desktop == desktop) gc = active_desktopGC;
148   else gc = inactive_desktopGC;
149   draw_relief(pixmap, dockapp_width, dockapp_height, gc);
150
151   get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &clients, &num_clients);
152   for (i = 0; i < num_clients; i++) {
153     int draw = 1;
154     client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, clients[i]);
155     if (client_desktop != desktop && client_desktop != -1) continue;
156     if (! XGetGeometry(DADisplay, clients[i], &root, &x, &y, &width, &height, &border, &depth)) continue;
157     if (! XTranslateCoordinates(DADisplay, clients[i], root, x, y, &x, &y, &child)) continue;
158     get_atom_longs(client_state_atom, XA_ATOM, clients[i], &states, &num_states);
159     for (j = 0; j < num_states; j++) {
160       if (states[j] == shaded_state) draw = 0;
161       if (states[j] == skip_pager_state) draw = 0;
162       if (states[j] == hidden_state) draw = 0;
163     }
164     if (! draw) continue;
165
166     rect.x = (double) x / scale;
167     rect.y = (double) y / scale;
168     rect.width = (double) width / scale;
169     rect.height = (double) height / scale;
170
171     if (rect.width < 2 || rect.height < 2) gc = window_border1GC;
172     else if (clients[i] == active_window) gc = active_windowGC;
173     else gc = inactive_windowGC;
174     XFillRectangle(DADisplay, pixmap, gc, rect.x, rect.y, rect.width, rect.height);
175     if (rect.width < 2 || rect.height < 2) continue;
176
177     /* Thumbnail. */
178 #if 0
179       XRenderPictureAttributes pa;
180       Pixmap client_pixmap = XCreatePixmap(DADisplay, wins[i], width, height, DefaultDepth(DADisplay, DefaultScreen(DADisplay)));
181       //XCopyArea(DADisplay, wins[i], client_pixmap, DefaultGC(DADisplay, DefaultScreen(DADisplay)), 0, 0, width, height, 0, 0);
182       Picture client_picture = XRenderCreatePicture(DADisplay, client_pixmap, XRenderFindVisualFormat(DADisplay, attr.visual), CPSubwindowMode, &pa);
183
184       XTransform xform = {{
185         { XDoubleToFixed(1.0), 0, 0 },
186         { 0, XDoubleToFixed(1.0), 0 },
187         { 0, 0, XDoubleToFixed(scale) },
188       }};
189
190       XRenderSetPictureFilter(DADisplay, client_picture, FilterBilinear, 0, 0);
191       XRenderSetPictureTransform(DADisplay, client_picture, &xform);
192       XRenderComposite(DADisplay, PictOpSrc, client_picture, None, picture, 0, 0, 0, 0, rect.x, rect.y, rect.width, rect.height);
193       XFreePixmap(DADisplay, client_pixmap);
194       XSync(DADisplay, False);
195 #endif
196
197     XDrawLine(DADisplay, pixmap, window_border1GC, rect.x, rect.y, rect.x, rect.y + rect.height - 2);
198     XDrawLine(DADisplay, pixmap, window_border1GC, rect.x + 1, rect.y, rect.x + rect.width - 1, rect.y);
199     XDrawLine(DADisplay, pixmap, window_border2GC, rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1);
200     XDrawLine(DADisplay, pixmap, window_border2GC, rect.x + rect.width - 1, rect.y + 1, rect.x + rect.width - 1, rect.y + rect.height - 2);
201   }
202
203   DASetPixmap(pixmap);
204   XFreePixmap(DADisplay, pixmap);
205 }
206
207 int main(int argc, char **argv) {
208   Display *display;
209   unsigned int root_width, root_height;
210   unsigned int dockapp_width, dockapp_height;
211   long num_desktops;
212   double aspect;
213   DACallbacks callbacks = { 0, change, 0, 0, 0, 0, page };
214   DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
215
216   DASetExpectedVersion(20020126);
217
218   display = XOpenDisplay(0);
219
220   root_width = DisplayWidth(display, DefaultScreen(display));
221   root_height = DisplayHeight(display, DefaultScreen(display));
222   aspect = (double) root_width / (double) root_height;
223
224   if (root_width > root_height) {
225     dockapp_width = DOCKAPP_SIZE;
226     dockapp_height = (double) DOCKAPP_SIZE / aspect;
227   }
228   else {
229     dockapp_height = DOCKAPP_SIZE;
230     dockapp_width = (double) DOCKAPP_SIZE / aspect;
231   }
232
233   DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB");
234   DAOpenDisplay(0, argc, argv);
235   DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
236
237   DASetCallbacks(&callbacks);
238   DASetTimeout(SPEED);
239
240   setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
241   setup_atom(&current_desktop_atom, "_NET_CURRENT_DESKTOP");
242   setup_atom(&client_list_atom, "_NET_CLIENT_LIST_STACKING");
243   setup_atom(&client_desktop_atom, "_NET_WM_DESKTOP");
244   setup_atom(&client_state_atom, "_NET_WM_STATE");
245   setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW");
246
247   setup_atom(&shaded_state, "_NET_WM_STATE_SHADED");
248   setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER");
249   setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN");
250
251   num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
252   if (desktop < 1) {
253     desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
254   }
255   else if (desktop-- > num_desktops) {
256     fprintf(stderr, "There are only %d desktops!\n", num_desktops);
257     exit(111);
258   }
259
260   XSetErrorHandler(error_handler);
261   setup_GCs();
262   DAShow();
263   DAEventLoop();
264 }