Stability
[pager.git] / pager.c
diff --git a/pager.c b/pager.c
index 6c60e09..e18a3d1 100644 (file)
--- a/pager.c
+++ b/pager.c
 
 static GC dockapp_border1GC, dockapp_border2GC, window_border1GC, window_border2GC, active_desktopGC, inactive_desktopGC, active_windowGC, inactive_windowGC;
 static Atom supported_atom, num_desktops_atom, desktop_geometry_atom, current_desktop_atom, client_list_atom, client_desktop_atom, client_state_atom, active_window_atom, moveresize_window_atom;
-static Atom above_state, below_state, shaded_state, skip_pager_state, hidden_state;
-static long desktop = -1;
+static Atom shaded_state, skip_pager_state, hidden_state;
+static int desktop = -1;
 static Window drag_window;
 static int drag_x, drag_y;
 static int drag_button;
-static int dockapp_x, dockapp_y;
-static unsigned int dockapp_width, dockapp_height, dockapp_border, dockapp_depth;
-static double scale;
+static XWindowAttributes dockapp_attr;
+static double aspect, scale;
 
 static int desktop_geometry_supported;
 static int current_desktop_supported;
+static int client_list_supported;
 static int active_window_supported;
 static int moveresize_supported;
 
@@ -84,6 +84,7 @@ void check_support() {
   for (i = 0; i < num_items; i++) {
     if (data[i] == desktop_geometry_atom) desktop_geometry_supported = 1;
     if (data[i] == current_desktop_atom) current_desktop_supported = 1;
+    if (data[i] == client_list_atom) client_list_supported = 1;
     if (data[i] == active_window_atom) active_window_supported = 1;
     if (data[i] == moveresize_window_atom) moveresize_supported = 1;
   }
@@ -92,7 +93,7 @@ void check_support() {
 }
 
 void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned long *active_desktop, unsigned long *active_window) {
-  Window root, dockapp_root, child;
+  Window root, qroot, qparent, child, *wins, client;
   int x, y;
   unsigned int width, height, border, depth;
   unsigned int root_width, root_height;
@@ -105,7 +106,7 @@ void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned
   if (desktop_geometry_supported) {
     get_atom_longs(desktop_geometry_atom, XA_CARDINAL, DefaultRootWindow(DADisplay), &states, &num_states);
     root_width = states[0];
-    root_height = states[0];
+    root_height = states[1];
     XFree(states);
   }
   else {
@@ -113,13 +114,22 @@ void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned
     root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay));
   }
 
-  XGetGeometry(DADisplay, DAWindow, &dockapp_root, &dockapp_x, &dockapp_y, &dockapp_width, &dockapp_height, &dockapp_border, &dockapp_depth);
-  scale = (double) root_width / (double) dockapp_width;
+  *num_clients = 0;
+  *clients = 0;
 
-  *active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root);
+  if (! XGetWindowAttributes(DADisplay, DAWindow, &dockapp_attr)) return;
+  scale = (double) root_width / (double) dockapp_attr.width;
+  aspect = (double) root_height / (double) dockapp_attr.height;
+
+  *active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_attr.root);
   *active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
 
-  get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &data, num_clients);
+  if (client_list_supported) {
+    get_atom_longs(client_list_atom, XA_WINDOW, dockapp_attr.root, &data, num_clients);
+  }
+  else {
+    XQueryTree(DADisplay, dockapp_attr.root, &qroot, &qparent, &wins, (unsigned int *) num_clients);
+  }
 
   *clients = (client_t **) malloc(sizeof(client_t *) * *num_clients);
   if (! *clients) {
@@ -135,34 +145,46 @@ void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned
     }
     (*clients)[i]->window = 0;
 
+    if (client_list_supported) client = data[i];
+    else client = XmuClientWindow(DADisplay, wins[i]);
+
+    /* Don't page the pager. */
+    if (client == DAWindow) continue;
+
     /* Check the window is on our desktop (or all desktops). */
-    client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, data[i]);
-    if (client_desktop != desktop && client_desktop != -1) continue;
+    client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, client);
+    if (desktop > -1) {
+      if (client_desktop != desktop && client_desktop != -1) continue;
+    }
+    else {
+      if (client_desktop != *active_desktop && client_desktop != -1) continue;
+    }
 
     /* Try to get its dimensions. */
-    if (! XGetGeometry(DADisplay, data[i], &root, &x, &y, &width, &height, &border, &depth)) continue;
-    if (! XTranslateCoordinates(DADisplay, data[i], root, x, y, &x, &y, &child)) continue;
+    if (! XGetGeometry(DADisplay, client, &root, &x, &y, &width, &height, &border, &depth)) continue;
+    if (! XTranslateCoordinates(DADisplay, client, root, x, y, &x, &y, &child)) continue;
 
     /* Make sure it isn't hidden or shaded. */
     draw = 1;
-    get_atom_longs(client_state_atom, XA_ATOM, data[i], &states, &num_states);
+    get_atom_longs(client_state_atom, XA_ATOM, client, &states, &num_states);
     for (j = 0; j < num_states; j++) {
-      if (states[j] == shaded_state) draw = 0;
+      if (states[j] == shaded_state) height = 3.0 * scale;
       if (states[j] == skip_pager_state) draw = 0;
       if (states[j] == hidden_state) draw = 0;
     }
     if (! draw) continue;
 
-    (*clients)[i]->window = data[i];
+    (*clients)[i]->window = client;
 
     /* Scale it. */
     (*clients)[i]->rect.x = (double) x / scale;
-    (*clients)[i]->rect.y = (double) y / scale;
+    (*clients)[i]->rect.y = (double) y / aspect;
     (*clients)[i]->rect.width = (double) width / scale;
-    (*clients)[i]->rect.height = (double) height / scale;
+    (*clients)[i]->rect.height = (double) height / aspect;
   }
 
-  XFree(data);
+  if (client_list_supported) XFree(data);
+  else XFree(wins);
 }
 
 void destroy_clients(client_t ***clients, unsigned long num_clients) {
@@ -170,7 +192,7 @@ void destroy_clients(client_t ***clients, unsigned long num_clients) {
 
   if (! *clients) return;
 
-  for (i = 0; i < num_clients; i++) free((*clients)[i]);
+  for (i = 0; i < num_clients; i++) if ((*clients)[i]) free((*clients)[i]);
   free(*clients);
 }
 
@@ -287,10 +309,10 @@ void page() {
 
   enumerate_clients(&clients, &num_clients, &active_desktop, &active_window);
 
-  pixmap = XCreatePixmap(DADisplay, DAWindow, dockapp_width, dockapp_height, dockapp_depth);
-  if (active_desktop == desktop) gc = active_desktopGC;
+  pixmap = XCreatePixmap(DADisplay, DAWindow, dockapp_attr.width, dockapp_attr.height, dockapp_attr.depth);
+  if (active_desktop == desktop || desktop < 0) gc = active_desktopGC;
   else gc = inactive_desktopGC;
-  draw_relief(pixmap, dockapp_width, dockapp_height, gc);
+  draw_relief(pixmap, dockapp_attr.width, dockapp_attr.height, gc);
 
   for (i = 0; i < num_clients; i++) {
     if (! clients[i]->window) continue;
@@ -330,7 +352,7 @@ void move(int x, int y) {
 
   if (! drag_window) return;
   if (x < 0 || y < 0) return;
-  if (x >= dockapp_width || y >= dockapp_height) return;
+  if (x >= dockapp_attr.width || y >= dockapp_attr.height) return;
 
   dest_x = (long) ((double) (x - drag_x) * scale);
   dest_y = (long) ((double) (y - drag_y) * scale);
@@ -349,7 +371,6 @@ int main(int argc, char **argv) {
   Display *display;
   unsigned int root_width, root_height;
   long num_desktops;
-  double aspect;
   DACallbacks callbacks = { 0, change, release, move, 0, 0, page };
   DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } };
 
@@ -362,17 +383,17 @@ int main(int argc, char **argv) {
   aspect = (double) root_width / (double) root_height;
 
   if (root_width > root_height) {
-    dockapp_width = DOCKAPP_SIZE;
-    dockapp_height = (double) DOCKAPP_SIZE / aspect;
+    dockapp_attr.width = DOCKAPP_SIZE;
+    dockapp_attr.height = (double) DOCKAPP_SIZE / aspect;
   }
   else {
-    dockapp_height = DOCKAPP_SIZE;
-    dockapp_width = (double) DOCKAPP_SIZE / aspect;
+    dockapp_attr.height = DOCKAPP_SIZE;
+    dockapp_attr.width = (double) DOCKAPP_SIZE / aspect;
   }
 
   DAParseArguments(argc, argv, &options, 1, "OPTIONS", "pager");
   DAOpenDisplay(0, argc, argv);
-  DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv);
+  DACreateIcon("pager", dockapp_attr.width, dockapp_attr.height, argc, argv);
 
   DASetCallbacks(&callbacks);
   DASetTimeout(SPEED);
@@ -387,8 +408,6 @@ int main(int argc, char **argv) {
   setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW");
   setup_atom(&moveresize_window_atom, "_NET_MOVERESIZE_WINDOW");
 
-  setup_atom(&above_state, "_NET_WM_STATE_ABOVE");
-  setup_atom(&below_state, "_NET_WM_STATE_BELOW");
   setup_atom(&shaded_state, "_NET_WM_STATE_SHADED");
   setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER");
   setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN");
@@ -396,9 +415,10 @@ int main(int argc, char **argv) {
   check_support();
 
   num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
-  if (desktop < 1) {
+  if (desktop == 0) {
     desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay));
   }
+  else if (desktop < 0) desktop = -1;
   else if (desktop-- > num_desktops) {
     fprintf(stderr, "There are only %ld desktops!\n", num_desktops);
     exit(111);