diff --git a/src/icons.cc b/src/icons.cc index b7e8f78..6c8aa9d 100644 --- a/src/icons.cc +++ b/src/icons.cc @@ -67,45 +67,52 @@ inline bool operator<(Span a, Span b) static void PlaceIcon(TwmWindow *tmp_win, int def_x, int def_y, int *final_x, int *final_y) { - // Try to place in a gap on the right side. + // Try to place in a gap along the right side of (current?) screen. - Window root, parent; - Window* children; - unsigned int nchildren; - XWindowAttributes attr; int y = 0; - if (XQueryTree(dpy, Scr->Root, &root, &parent, &children, &nchildren)) { - XArray childarray(children, nchildren); - vector occupied; - for (Window w : childarray) { - if (XGetWindowAttributes(dpy, w, &attr)) { - // w looks like icon? - if (attr.map_state != IsUnmapped - && attr.height <= 200 && attr.width <= 200 - && attr.x >= icon_area_x) { - - occupied.push_back({attr.y, attr.y + attr.height}); - } + vector occupied; + for (TwmWindow* pw = Scr->TwmRoot.next; pw; pw = pw->next) { + // Iconified means was it ever iconified. + if (pw->iconified) { + Window root; + int icon_x, icon_y; + unsigned icon_w, icon_h, border_width, depth; + if (pw->icon_w + && XGetGeometry(dpy, pw->icon_w, &root, + &icon_x, &icon_y, &icon_w, &icon_h, + &border_width, &depth)) { + if (icon_x >= icon_area_x) + occupied.push_back({icon_y, icon_y + static_cast(icon_h)}); } } - int h = 64; - if (tmp_win->icon_height) { - h = tmp_win->icon_height; - } + else { + // A small non-iconified window sitting where an icon would? E.g. xclock. + // I assume here that non-iconified windows that look like icons won't + // ever be iconified, making this an either or decision. + if (pw->frame_x >= icon_area_x + && pw->frame_width <= 150 && pw->frame_height <= 150) { - sort(occupied.begin(), occupied.end()); - int prev = 0; - for (auto span : occupied) { - if (span.p1 - prev >= h) { - if (span.p1 - prev >= h + 2*icon_pad) - y = prev + icon_pad; - else - y = prev; - break; + occupied.push_back({pw->frame_y, pw->frame_y + pw->frame_height}); } - prev = span.p2; } } + int h = 64; + if (tmp_win->icon_height) { + h = tmp_win->icon_height; + } + + sort(occupied.begin(), occupied.end()); + int prev = 0; + for (auto span : occupied) { + if (span.p1 - prev >= h) { + if (span.p1 - prev >= h + 2*icon_pad) + y = prev + icon_pad; + else + y = prev; + break; + } + prev = span.p2; + } if (y) { *final_x = icon_area_x + 20; // TODO: stick the icon roughly in the center of the space. @@ -114,7 +121,6 @@ PlaceIcon(TwmWindow *tmp_win, int def_x, int def_y, int *final_x, int *final_y) *final_x = def_x; *final_y = def_y; } - return; } void