diff options
| author | Patrick <phaller@lap4.haller.ws> | 2015-10-13 01:20:58 +0800 |
|---|---|---|
| committer | Patrick <phaller@lap4.haller.ws> | 2015-10-13 01:22:43 +0800 |
| commit | 353d133ba38b52554fecc699b9e9e7e7a2dfc0da (patch) | |
| tree | 73cf10b6a49025c28a1b508b00f5f6cbf79fbb3c | |
| parent | 3fbb7c6e9823052e6b3492ad96baae51f19a07b7 (diff) | |
create list of viewable windows first, then operate on that
| -rw-r--r-- | x-alt-tab.c | 54 |
1 files changed, 36 insertions, 18 deletions
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 <X11/Xutil.h> #include <stdio.h> +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; } |
