Use _NET_WM properties
[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 void setup_atom(Atom *atom, const char *prop) {
19   *atom = XInternAtom(DADisplay, prop, True);
20   if (*atom == None) exit(111);
21 }
22
23 void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) {
24   Atom actual;
25   int format;
26   unsigned long num_bytes;
27
28   XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data);
29 }
30
31 long get_atom_long(Atom atom, Atom type, Window window) {
32   long *data;
33   long ret = 0;
34   unsigned long num_items;
35
36   get_atom_longs(atom, type, window, &data, &num_items);
37   if (num_items) ret = data[0];
38   XFree(data);
39
40   return ret;
41 }
42
43 void setup_GCs() {
44   XGCValues gcv;
45
46   /* GC's */
47   gcv.foreground = DAGetColor("darkGray");
48   XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
49
50   gcv.foreground = DAGetColor("lightGray");
51   gcv.graphics_exposures = False;
52
53   lightGrayGC   = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
54
55   gcv.foreground = DAGetColor("#222222");
56   darkGrayGC    =  XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
57
58   gcv.foreground = DAGetColor("#bbbbbb");
59   active_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
60
61   gcv.foreground = DAGetColor("#777777");
62   inactive_desktopGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
63
64   gcv.foreground = DAGetColor("#20b2ae");
65   active_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
66
67   gcv.foreground = DAGetColor("#999999");
68   inactive_windowGC = XCreateGC(DADisplay, DAWindow, GCForeground|GCGraphicsExposures, &gcv);
69 }
70
71 void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desktopGC) {
72   /* Drawing */
73   XFillRectangle(DADisplay, pixmap, desktopGC, 0, 0, width, height);
74
75   XDrawLine(DADisplay, pixmap, darkGrayGC, 0, 0, 0, height - 2);
76   XDrawLine(DADisplay, pixmap, darkGrayGC, 1, 0, width - 1, 0);
77
78   XDrawLine(DADisplay, pixmap, lightGrayGC, 0, height - 1, width - 1, height - 1);
79   XDrawLine(DADisplay, pixmap, lightGrayGC, width - 1, 1, width - 1, height - 2);
80 }
81
82 void change() {
83   XEvent event;
84   Window root = DefaultRootWindow(DADisplay);
85
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;
92
93   XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event);
94 }
95
96 void page() {
97   Window root, dockapp_root, child, active_window;
98   XWindowAttributes attr;
99   Pixmap pixmap;
100   DARect rect;
101   long i, j;
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;
106   double scale;
107   long *clients, *states;
108   long client_desktop, active_desktop;
109   int active;
110   GC gc;
111
112   /* XXX: Use _NET_DESKTOP_GEOMETRY. */
113   root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
114   root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
115
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;
119
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));
122
123   if (active_desktop == desktop) gc = active_desktopGC;
124   else gc = inactive_desktopGC;
125   draw_relief(pixmap, dockapp_width, dockapp_height, gc);
126
127   get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &clients, &num_clients);
128   for (i = 0; i < num_clients; i++) {
129     int draw = 1;
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;
139     }
140     if (! draw) continue;
141
142     rect.x = (double) x / scale;
143     rect.y = (double) y / scale;
144     rect.width = (double) width / scale;
145     rect.height = (double) height / scale;
146
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);
150
151     /* Thumbnail. */
152 #if 0
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);
157
158       XTransform xform = {{
159         { XDoubleToFixed(1.0), 0, 0 },
160         { 0, XDoubleToFixed(1.0), 0 },
161         { 0, 0, XDoubleToFixed(scale) },
162       }};
163
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);
169 #endif
170
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);
175   }
176
177   DASetPixmap(pixmap);
178   XFreePixmap(DADisplay, pixmap);
179 }
180
181 int main(int argc, char **argv) {
182   Display *display;
183   unsigned int root_width, root_height;
184   unsigned int dockapp_width, dockapp_height;
185   long num_desktops;
186   double aspect;
187   DACallbacks callbacks = { 0, change, 0, 0, 0, 0, page };
188   DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
189
190   DASetExpectedVersion(20020126);
191
192   display = XOpenDisplay(0);
193
194   root_width = DisplayWidth(display, DefaultScreen(display));
195   root_height = DisplayHeight(display, DefaultScreen(display));
196   aspect = (double) root_width / (double) root_height;
197
198   if (root_width > root_height) {
199     dockapp_width = DOCKAPP_SIZE;
200     dockapp_height = (double) DOCKAPP_SIZE / aspect;
201   }
202   else {
203     dockapp_height = DOCKAPP_SIZE;
204     dockapp_width = (double) DOCKAPP_SIZE / aspect;
205   }
206
207   DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB");
208   DAOpenDisplay(0, argc, argv);
209   DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
210
211   DASetCallbacks(&callbacks);
212   DASetTimeout(SPEED);
213
214   setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS");
215   setup_atom(&current_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");
220
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");
224
225   num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
226   if (desktop < 1) {
227     desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
228   }
229   else if (desktop-- > num_desktops) {
230     fprintf(stderr, "There are only %d desktops!\n", num_desktops);
231     exit(111);
232   }
233
234   setup_GCs();
235   DAShow();
236   DAEventLoop();
237 }