From 353d133ba38b52554fecc699b9e9e7e7a2dfc0da Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 13 Oct 2015 01:20:58 +0800 Subject: create list of viewable windows first, then operate on that --- x-alt-tab.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'x-alt-tab.c') diff --git a/x-alt-tab.c b/x-alt-tab.c index 180e227..66183be 100644 --- a/x-alt-tab.c +++ b/x-alt-tab.c @@ -12,32 +12,50 @@ If not, see http://creativecommons.org/publicdomain/zero/1.0/ */ #include #include +void alt_tab(int argc, Display *dpy, Window *wins, unsigned int nwins) { + XWindowAttributes attr; + Window *viewables[nwins], *w = 0; + unsigned int count = 0; + int vc = 0; + + // make list of viewable windows + for (count = 0; count < nwins; count++) { + w = wins + count; + XGetWindowAttributes(dpy, *w, &attr); + if (attr.map_state == IsViewable) { + viewables[vc] = w; + vc++; + } + } + viewables[vc] = NULL; + + // promote the last to top, or demote top to last and raise 2nd + if (argc > 1) { + w = viewables[vc - 2]; + XLowerWindow(dpy, *(viewables[vc - 1])); + } else { + w = viewables[0]; + } + + XRaiseWindow(dpy, *w); + XSetInputFocus(dpy, *w, RevertToPointerRoot, CurrentTime); + XSync(dpy, True); +} + int main(int argc, char **argv) { Display *dpy; - XWindowAttributes attr; - unsigned int nwins, count = 0; - Window root, parent, *wins, *w = 0; + unsigned int nwins = 0; + Window root, parent, *wins = 0; if ( (dpy = XOpenDisplay(NULL)) == NULL) return 1; - do { - if (argc > 1) - XCirculateSubwindowsDown(dpy, DefaultRootWindow(dpy)); - else - XCirculateSubwindowsUp(dpy, DefaultRootWindow(dpy)); - - XSync(dpy, True); - XQueryTree(dpy, DefaultRootWindow(dpy), &root, &parent, &wins, &nwins); - w = wins + nwins - 1; - XGetWindowAttributes(dpy, *w, &attr); - count++; - - } while (attr.map_state != IsViewable && count <= nwins+1); - - XSetInputFocus(dpy, *w, RevertToParent, CurrentTime); XSync(dpy, True); + XQueryTree(dpy, DefaultRootWindow(dpy), &root, &parent, &wins, &nwins); + if (nwins == 1) + return 0; + alt_tab(argc, dpy, wins, nwins); return 0; } -- cgit v1.2.3