Mac OS X-style window cycling.
authorIain Patterson <wm@iain.cx>
Mon, 14 Sep 2009 13:37:15 +0000 (14 14:37 +0100)
committerCarlos R. Mafra <crmafra@gmail.com>
Mon, 14 Sep 2009 14:51:26 +0000 (14 16:51 +0200)
For those not familiar with the way Macs cycle windows, the Command-Tab
sequence (Alt-Tab elsewhere) switches between DIFFERENT application windows
and Command-Grave (key above tab) switches between windows owned by the
SAME application as is currently focused.  So if you had three Safari and
two Finder windows open, and Safari had focus, Command-Tab would switch to
Finder; Command-Tab would switch back to Safari; Command-Grave would switch
to a different Safari window etc.

This patch implements "something like" the above by only populating the
switchpanel with windows matching the currently-focused WWindow's wm_class
when the new cycling mode is activated.  In practice this means you can
switch to The Next XTerm or The Next Firefox Window using this method.

The configuration names for these new shortcuts are GroupNext and
GroupPrev.  The patch tells WPrefs.app about them.  Of course switching to
The Next Window is still possible with the (unchanged) FocusNext and
FocusPrev keys.

WPrefs.app/KeyboardShortcuts.c
src/cycling.c
src/defaults.c
src/event.c
src/funcs.h
src/keybind.h
src/switchpanel.c
src/switchpanel.h

index 6cc6f9e..2c44b8c 100644 (file)
@@ -83,6 +83,8 @@ static char *keyOptions[] = {
        "SelectKey",
        "FocusNextKey",
        "FocusPrevKey",
+       "GroupNextKey",
+       "GroupPrevKey",
        "NextWorkspaceKey",
        "PrevWorkspaceKey",
        "NextWorkspaceLayerKey",
@@ -487,6 +489,8 @@ static void createPanel(Panel * p)
        WMAddListItem(panel->actLs, _("Select active window"));
        WMAddListItem(panel->actLs, _("Focus next window"));
        WMAddListItem(panel->actLs, _("Focus previous window"));
+       WMAddListItem(panel->actLs, _("Focus next group window"));
+       WMAddListItem(panel->actLs, _("Focus previous group window"));
        WMAddListItem(panel->actLs, _("Switch to next workspace"));
        WMAddListItem(panel->actLs, _("Switch to previous workspace"));
        WMAddListItem(panel->actLs, _("Switch to next ten workspaces"));
index 2510d2f..c064f1d 100644 (file)
@@ -81,7 +81,7 @@ static WWindow *change_focus_and_raise(WWindow *newFocused, WWindow *oldFocused,
        return oldFocused;
 }
 
-void StartWindozeCycle(WWindow * wwin, XEvent * event, Bool next)
+void StartWindozeCycle(WWindow * wwin, XEvent * event, Bool next, Bool class_only)
 {
 
        XModifierKeymap *keymap        = NULL;
@@ -123,7 +123,7 @@ void StartWindozeCycle(WWindow * wwin, XEvent * event, Bool next)
 
        scr->flags.doing_alt_tab = 1;
 
-       swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace);
+       swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace, class_only);
        oldFocused = wwin;
 
        if (swpanel) {
@@ -159,6 +159,8 @@ void StartWindozeCycle(WWindow * wwin, XEvent * event, Bool next)
 
                        if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
                             && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
+                           || (wKeyBindings[WKBD_GROUPNEXT].keycode == ev.xkey.keycode
+                           && wKeyBindings[WKBD_GROUPNEXT].modifier == modifiers)
                            || ev.xkey.keycode == rightKey) {
 
                                newFocused = wSwitchPanelSelectNext(swpanel, False);
@@ -166,6 +168,8 @@ void StartWindozeCycle(WWindow * wwin, XEvent * event, Bool next)
 
                        } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
                                    && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
+                           || (wKeyBindings[WKBD_GROUPPREV].keycode == ev.xkey.keycode
+                           && wKeyBindings[WKBD_GROUPPREV].modifier == modifiers)
                                   || ev.xkey.keycode == leftKey) {
 
                                newFocused = wSwitchPanelSelectNext(swpanel, True);
@@ -199,7 +203,9 @@ void StartWindozeCycle(WWindow * wwin, XEvent * event, Bool next)
 
                                if (keymap->modifiermap[i] == ev.xkey.keycode &&
                                    ((wKeyBindings[WKBD_FOCUSNEXT].modifier & mask)
-                                    || (wKeyBindings[WKBD_FOCUSPREV].modifier & mask))) {
+                                    || (wKeyBindings[WKBD_FOCUSPREV].modifier & mask)
+                                    || (wKeyBindings[WKBD_GROUPNEXT].modifier & mask)
+                                    || (wKeyBindings[WKBD_GROUPPREV].modifier & mask))) {
                                        done = True;
                                        break;
                                }
index 61756b0..555bdac 100644 (file)
@@ -601,6 +601,10 @@ WDefaultEntry optionList[] = {
         NULL, getKeybind, setKeyGrab},
        {"FocusPrevKey", "None", (void *)WKBD_FOCUSPREV,
         NULL, getKeybind, setKeyGrab},
+       {"GroupNextKey", "None", (void *)WKBD_GROUPNEXT,
+        NULL, getKeybind, setKeyGrab},
+       {"GroupPrevKey", "None", (void *)WKBD_GROUPPREV,
+        NULL, getKeybind, setKeyGrab},
        {"NextWorkspaceKey", "None", (void *)WKBD_NEXTWORKSPACE,
         NULL, getKeybind, setKeyGrab},
        {"PrevWorkspaceKey", "None", (void *)WKBD_PREVWORKSPACE,
index 023e896..71720b6 100644 (file)
@@ -1542,11 +1542,19 @@ static void handleKeyPress(XEvent * event)
                }
                break;
        case WKBD_FOCUSNEXT:
-               StartWindozeCycle(wwin, event, True);
+               StartWindozeCycle(wwin, event, True, False);
                break;
 
        case WKBD_FOCUSPREV:
-               StartWindozeCycle(wwin, event, False);
+               StartWindozeCycle(wwin, event, False, False);
+               break;
+
+       case WKBD_GROUPNEXT:
+               StartWindozeCycle(wwin, event, True, True);
+               break;
+
+       case WKBD_GROUPPREV:
+               StartWindozeCycle(wwin, event, False, True);
                break;
 
        case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
index c900976..f49536e 100644 (file)
@@ -85,7 +85,7 @@ void PlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
                  unsigned int width, unsigned int height);
 
 
-void StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next);
+void StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next, Bool class_only);
 
 #ifdef USECPP
 char *MakeCPPArgs(char *path);
index 995e7a7..2daca8d 100644 (file)
@@ -44,6 +44,8 @@ enum {
        WKBD_SHADE,
        WKBD_FOCUSNEXT,
        WKBD_FOCUSPREV,
+       WKBD_GROUPNEXT,
+       WKBD_GROUPPREV,
 
        /* window, menu */
        WKBD_CLOSE,
index 3e1ae15..41cb1ba 100644 (file)
@@ -374,7 +374,7 @@ static void drawTitle(WSwitchPanel * panel, int idecks, char *title)
                free(ntitle);
 }
 
-static WMArray *makeWindowListArray(WScreen * scr, WWindow * curwin, int workspace, int include_unmapped)
+static WMArray *makeWindowListArray(WScreen * scr, WWindow * curwin, int workspace, int include_unmapped, Bool class_only)
 {
        WMArray *windows = WMCreateArray(10);
        int fl;
@@ -385,6 +385,12 @@ static WMArray *makeWindowListArray(WScreen * scr, WWindow * curwin, int workspa
                        if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
                            (!WFLAGP(wwin, skip_window_list) || wwin->flags.internal_window) &&
                            (wwin->flags.mapped || include_unmapped)) {
+                               if (class_only) {
+                                   if (!wwin->wm_class || !curwin->wm_class)
+                                       continue;
+                                   if (strcmp(wwin->wm_class, curwin->wm_class))
+                                       continue;
+                               }
                                WMAddToArray(windows, wwin);
                        }
                }
@@ -397,6 +403,12 @@ static WMArray *makeWindowListArray(WScreen * scr, WWindow * curwin, int workspa
                        if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
                            (!WFLAGP(wwin, skip_window_list) || wwin->flags.internal_window) &&
                            (wwin->flags.mapped || include_unmapped)) {
+                               if (class_only) {
+                                   if (!wwin->wm_class || !curwin->wm_class)
+                                       continue;
+                                   if (strcmp(wwin->wm_class, curwin->wm_class))
+                                       continue;
+                               }
                                WMAddToArray(windows, wwin);
                        }
                }
@@ -405,7 +417,7 @@ static WMArray *makeWindowListArray(WScreen * scr, WWindow * curwin, int workspa
        return windows;
 }
 
-WSwitchPanel *wInitSwitchPanel(WScreen * scr, WWindow * curwin, int workspace)
+WSwitchPanel *wInitSwitchPanel(WScreen * scr, WWindow * curwin, int workspace, Bool class_only)
 {
        WWindow *wwin;
        WSwitchPanel *panel = wmalloc(sizeof(WSwitchPanel));
@@ -421,7 +433,7 @@ WSwitchPanel *wInitSwitchPanel(WScreen * scr, WWindow * curwin, int workspace)
 
        panel->scr = scr;
 
-       panel->windows = makeWindowListArray(scr, curwin, workspace, wPreferences.swtileImage != 0);
+       panel->windows = makeWindowListArray(scr, curwin, workspace, wPreferences.swtileImage != 0, class_only);
        count = WMGetArrayItemCount(panel->windows);
 
        if (count == 0) {
index 6a931b0..0ed1928 100644 (file)
@@ -24,7 +24,7 @@
 
 typedef struct SwitchPanel WSwitchPanel;
 
-WSwitchPanel *wInitSwitchPanel(WScreen *scr, WWindow *curwin, int workspace);
+WSwitchPanel *wInitSwitchPanel(WScreen *scr, WWindow *curwin, int workspace, Bool class_only);
 
 void wSwitchPanelDestroy(WSwitchPanel *panel);