enhacnced mouse alt-tabbing
[wmaker-crm.git] / src / cycling.c
blob3c7882881c821da06388ed5bdb21590ab3a5985a
1 /* cycling.c- window cycling
3 * Window Maker window manager
5 * Copyright (c) 2000-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <X11/keysym.h>
29 #include "WindowMaker.h"
30 #include "GNUstep.h"
31 #include "screen.h"
32 #include "wcore.h"
33 #include "window.h"
34 #include "framewin.h"
35 #include "keybind.h"
36 #include "actions.h"
37 #include "stacking.h"
38 #include "funcs.h"
39 #include "xinerama.h"
40 #include "switchpanel.h"
42 /* Globals */
43 extern WPreferences wPreferences;
45 extern WShortKey wKeyBindings[WKBD_LAST];
48 static void raiseWindow(WSwitchPanel *swpanel, WWindow *wwin)
50 if (wwin->flags.mapped) {
51 Window win[2];
53 win[0]= wSwitchPanelGetWindow(swpanel);
54 win[1]= wwin->frame->core->window;
56 XRestackWindows(dpy, win, 2);
62 void
63 StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
65 WScreen *scr = wScreenForRootWindow(event->xkey.root);
66 Bool done = False;
67 WWindow *newFocused;
68 WWindow *oldFocused;
69 int modifiers;
70 XModifierKeymap *keymap = NULL;
71 Bool hasModifier;
72 Bool somethingElse = False;
73 XEvent ev;
74 WSwitchPanel *swpanel = NULL;
75 KeyCode leftKey, rightKey, homeKey, endKey;
77 if (!wwin)
78 return;
80 leftKey = XKeysymToKeycode(dpy, XK_Left);
81 rightKey = XKeysymToKeycode(dpy, XK_Right);
82 homeKey = XKeysymToKeycode(dpy, XK_Home);
83 endKey = XKeysymToKeycode(dpy, XK_End);
85 if (next)
86 hasModifier = (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0);
87 else
88 hasModifier = (wKeyBindings[WKBD_FOCUSPREV].modifier != 0);
90 if (hasModifier) {
91 keymap = XGetModifierMapping(dpy);
93 #ifdef DEBUG
94 printf("Grabbing keyboard\n");
95 #endif
96 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync,
97 CurrentTime);
100 scr->flags.doing_alt_tab = 1;
102 swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace);
103 oldFocused = wwin;
105 if (swpanel) {
106 newFocused = wSwitchPanelSelectNext(swpanel, !next);
107 if (newFocused) {
108 wWindowFocus(newFocused, oldFocused);
109 oldFocused = newFocused;
111 if (wPreferences.circ_raise)
112 raiseWindow(swpanel, newFocused);
115 else
117 if (wwin->frame->workspace == scr->current_workspace)
118 newFocused= wwin;
119 else
120 newFocused= NULL;
123 while (hasModifier && !done) {
124 int i;
126 WMMaskEvent(dpy, KeyPressMask|KeyReleaseMask|ExposureMask
127 |PointerMotionMask|ButtonReleaseMask, &ev);
129 /* ignore CapsLock */
130 modifiers = ev.xkey.state & ValidModMask;
132 switch (ev.type) {
133 case KeyPress:
134 #ifdef DEBUG
135 printf("Got key press\n");
136 #endif
137 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
138 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
139 || ev.xkey.keycode == rightKey) {
141 if (swpanel) {
142 newFocused = wSwitchPanelSelectNext(swpanel, False);
143 if (newFocused) {
144 wWindowFocus(newFocused, oldFocused);
145 oldFocused = newFocused;
147 if (wPreferences.circ_raise) {
148 CommitStacking(scr);
149 raiseWindow(swpanel, newFocused);
153 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
154 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
155 || ev.xkey.keycode == leftKey) {
157 if (swpanel) {
158 newFocused = wSwitchPanelSelectNext(swpanel, True);
159 if (newFocused) {
160 wWindowFocus(newFocused, oldFocused);
161 oldFocused = newFocused;
163 if (wPreferences.circ_raise) {
164 CommitStacking(scr);
165 raiseWindow(swpanel, newFocused);
169 } else if (ev.xkey.keycode == homeKey || ev.xkey.keycode == endKey) {
170 if (swpanel) {
171 newFocused = wSwitchPanelSelectFirst(swpanel, ev.xkey.keycode != homeKey);
172 if (newFocused) {
173 wWindowFocus(newFocused, oldFocused);
174 oldFocused = newFocused;
176 if (wPreferences.circ_raise) {
177 CommitStacking(scr);
178 raiseWindow(swpanel, newFocused);
182 } else {
183 #ifdef DEBUG
184 printf("Got something else\n");
185 #endif
186 somethingElse = True;
187 done = True;
189 break;
190 case KeyRelease:
191 #ifdef DEBUG
192 printf("Got key release\n");
193 #endif
194 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
195 if (keymap->modifiermap[i] == ev.xkey.keycode &&
196 wKeyBindings[WKBD_FOCUSNEXT].modifier
197 & 1<<(i/keymap->max_keypermod)) {
198 done = True;
199 break;
202 break;
204 case MotionNotify:
205 case ButtonRelease:
207 WWindow *tmp;
208 if (swpanel) {
209 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
210 if (tmp && tmp != newFocused) {
211 newFocused = tmp;
212 wWindowFocus(newFocused, oldFocused);
213 oldFocused = newFocused;
215 if (wPreferences.circ_raise) {
216 CommitStacking(scr);
217 raiseWindow(swpanel, newFocused);
220 if (ev.type == ButtonRelease)
221 done= True;
225 break;
227 default:
228 WMHandleEvent(&ev);
229 break;
232 if (keymap)
233 XFreeModifiermap(keymap);
235 if (hasModifier) {
236 #ifdef DEBUG
237 printf("Ungrabbing keyboard\n");
238 #endif
239 XUngrabKeyboard(dpy, CurrentTime);
242 if (newFocused) {
243 wRaiseFrame(newFocused->frame->core);
244 CommitStacking(scr);
245 if (!newFocused->flags.mapped)
246 wMakeWindowVisible(newFocused);
247 wSetFocusTo(scr, newFocused);
250 if (swpanel)
251 wSwitchPanelDestroy(swpanel);
253 scr->flags.doing_alt_tab = 0;
255 if (somethingElse)
256 WMHandleEvent(&ev);