a8f0e8d01d7e546caad8d0502453a059bbe2d916
[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 #define ACTIVE_DESKTOP 1
11 #define ACTIVE_WINDOW 2
12
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;
17
18 int error_handler(Display *display, XErrorEvent *event) {
19   char buffer[512];
20   int length;
21
22   switch (event->error_code) {
23     case BadWindow:
24       /* The window may have gone away since we queried the window manager. */
25     break;
26
27     default:
28       if (XGetErrorText(display, event->error_code, buffer, sizeof(buffer))) {
29         fprintf(stderr, "%s\n", buffer);
30         exit(100);
31       }
32     break;
33   }
34
35   return 0;
36 }
37
38 void setup_atom(Atom *atom, const char *prop) {
39   *atom = XInternAtom(DADisplay, prop, True);
40   if (*atom == None) exit(111);
41 }
42
43 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
44   Atom actual;
45   int format;
46   unsigned long num_bytes;
47
48   XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
49 }
50
51 long get_atom_long(Atom atom, Atom type, Window window) {
52   long *data;
53   long ret = 0;
54   unsigned long num_items;
55
56   get_atom_longs(atom, type, window, &data, &num_items);
57   if (num_items) ret = data[0];
58   XFree(data);
59
60   return ret;
61 }
62
63 void setup_GCs() {
64   XGCValues gcv;
65
66   /* GC's */
67   gcv.foreground = DAGetColor("darkGray");
68   XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
69
70   gcv.foreground = DAGetColor("lightGray");
71   gcv.graphics_exposures = False;
72
73   lightGrayGC   = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
74
75   gcv.foreground = DAGetColor("#222222");
76   darkGrayGC    =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
77
78   gcv.foreground = DAGetColor("#bbbbbb");
79   active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
80
81   gcv.foreground = DAGetColor("#777777");
82   inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
83
84   gcv.foreground = DAGetColor("#20b2ae");
85   active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
86
87   gcv.foreground = DAGetColor("#999999");
88   inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
89 }
90
91 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
92   /* Drawing */
93   XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
94
95   XDrawLine(DADisplay, pixmap, darkGrayGC, 0, 0, 0, height - 2);
96   XDrawLine(DADisplay, pixmap, darkGrayGC, 1, 0, width - 1, 0);
97
98   XDrawLine(DADisplay, pixmap, lightGrayGC, 0, height - 1, width - 1, height - 1);
99   XDrawLine(DADisplay, pixmap, lightGrayGC, width - 1, 1, width - 1, height - 2);
100 }
101
102 void change() {
103   XEvent event;
104   Window root = DefaultRootWindow(DADisplay);
105
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;
112
113   XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event);
114 }
115
116 void page() {
117   Window root, dockapp_root, child, active_window;
118   XWindowAttributes attr;
119   Pixmap pixmap;
120   DARect rect;
121   long i, j;
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;
126   double scale;
127   long *clients, *states;
128   long client_desktop, active_desktop;
129   int active;
130   GC gc;
131
132   /* XXX: Use _NET_DESKTOP_GEOMETRY. */
133   root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
134   root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
135
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;
139
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));
142
143   if (active_desktop == desktop) gc = active_desktopGC;
144   else gc = inactive_desktopGC;
145   draw_relief(pixmap, dockapp_width, dockapp_height, gc);
146
147   get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &clients, &num_clients);
148   for (i = 0; i < num_clients; i++) {
149     int draw = 1;
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;
159     }
160     if (! draw) continue;
161
162     rect.x = (double) x / scale;
163     rect.y = (double) y / scale;
164     rect.width = (double) width / scale;
165     rect.height = (double) height / scale;
166
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);
170
171     /* Thumbnail. */
172 #if 0
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);
177
178       XTransform xform = {{
179         { XDoubleToFixed(1.0), 0, 0 },
180         { 0, XDoubleToFixed(1.0), 0 },
181         { 0, 0, XDoubleToFixed(scale) },
182       }};
183
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);
189 #endif
190
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);
195   }
196
197   DASetPixmap(pixmap);
198   XFreePixmap(DADisplay, pixmap);
199 }
200
201 int main(int argc, char **argv) {
202   Display *display;
203   unsigned int root_width, root_height;
204   unsigned int dockapp_width, dockapp_height;
205   long num_desktops;
206   double aspect;
207   DACallbacks callbacks = { 0, change, 0, 0, 0, 0, page };
208   DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
209
210   DASetExpectedVersion(20020126);
211
212   display = XOpenDisplay(0);
213
214   root_width = DisplayWidth(display, DefaultScreen(display));
215   root_height = DisplayHeight(display, DefaultScreen(display));
216   aspect = (double) root_width / (double) root_height;
217
218   if (root_width > root_height) {
219     dockapp_width = DOCKAPP_SIZE;
220     dockapp_height = (double) DOCKAPP_SIZE / aspect;
221   }
222   else {
223     dockapp_height = DOCKAPP_SIZE;
224     dockapp_width = (double) DOCKAPP_SIZE / aspect;
225   }
226
227   DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB");
228   DAOpenDisplay(0, argc, argv);
229   DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
230
231   DASetCallbacks(&callbacks);
232   DASetTimeout(SPEED);
233
234   setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
235   setup_atom(&current_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");
240
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");
244
245   num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
246   if (desktop < 1) {
247     desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
248   }
249   else if (desktop-- > num_desktops) {
250     fprintf(stderr, "There are only %d desktops!\n", num_desktops);
251     exit(111);
252   }
253
254   XSetErrorHandler(error_handler);
255   setup_GCs();
256   DAShow();
257   DAEventLoop();
258 }