fixed cycling
[wmaker-crm.git] / src / cycling.c
blob9fb899efa09b9a4e17d0d155407b62c41aaee047
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 #define MOX_CYCLING
31 #include "WindowMaker.h"
32 #include "GNUstep.h"
33 #include "screen.h"
34 #include "wcore.h"
35 #include "window.h"
36 #include "framewin.h"
37 #include "keybind.h"
38 #include "actions.h"
39 #include "stacking.h"
40 #include "funcs.h"
41 #include "xinerama.h"
42 #include "switchpanel.h"
44 /* Globals */
45 extern WPreferences wPreferences;
47 extern WShortKey wKeyBindings[WKBD_LAST];
53 #ifndef MOX_CYCLING
54 static WWindow*
55 nextToFocusAfter(WWindow *wwin)
57 WWindow *tmp = wwin->prev;
59 while (tmp) {
60 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
61 return tmp;
63 tmp = tmp->prev;
66 tmp = wwin;
67 /* start over from the beginning of the list */
68 while (tmp->next)
69 tmp = tmp->next;
71 while (tmp && tmp != wwin) {
72 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
73 return tmp;
75 tmp = tmp->prev;
78 return wwin;
82 static WWindow*
83 nextToFocusBefore(WWindow *wwin)
85 WWindow *tmp = wwin->next;
87 while (tmp) {
88 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
89 return tmp;
91 tmp = tmp->next;
94 /* start over from the beginning of the list */
95 tmp = wwin;
96 while (tmp->prev)
97 tmp = tmp->prev;
99 while (tmp && tmp != wwin) {
100 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
102 return tmp;
104 tmp = tmp->next;
107 return wwin;
113 static WWindow*
114 nextFocusWindow(WWindow *wwin)
116 WWindow *tmp, *closest, *min;
117 Window d;
119 if (!wwin)
120 return NULL;
121 tmp = wwin->prev;
122 closest = NULL;
123 min = wwin;
124 d = 0xffffffff;
125 while (tmp) {
126 if (wWindowCanReceiveFocus(tmp)
127 && (!WFLAGP(tmp, skip_window_list)|| tmp->flags.internal_window)) {
128 if (min->client_win > tmp->client_win)
129 min = tmp;
130 if (tmp->client_win > wwin->client_win
131 && (!closest
132 || (tmp->client_win - wwin->client_win) < d)) {
133 closest = tmp;
134 d = tmp->client_win - wwin->client_win;
137 tmp = tmp->prev;
139 if (!closest||closest==wwin)
140 return min;
141 return closest;
145 static WWindow*
146 prevFocusWindow(WWindow *wwin)
148 WWindow *tmp, *closest, *max;
149 Window d;
151 if (!wwin)
152 return NULL;
153 tmp = wwin->prev;
154 closest = NULL;
155 max = wwin;
156 d = 0xffffffff;
157 while (tmp) {
158 if (wWindowCanReceiveFocus(tmp) &&
159 (!WFLAGP(tmp, skip_window_list) || tmp->flags.internal_window)) {
160 if (max->client_win < tmp->client_win)
161 max = tmp;
162 if (tmp->client_win < wwin->client_win
163 && (!closest
164 || (wwin->client_win - tmp->client_win) < d)) {
165 closest = tmp;
166 d = wwin->client_win - tmp->client_win;
169 tmp = tmp->prev;
171 if (!closest||closest==wwin)
172 return max;
173 return closest;
175 #endif /* !MOX_CYCLING */
178 void
179 StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
181 WScreen *scr = wScreenForRootWindow(event->xkey.root);
182 Bool done = False;
183 WWindow *newFocused;
184 WWindow *oldFocused;
185 int modifiers;
186 XModifierKeymap *keymap = NULL;
187 Bool hasModifier;
188 Bool somethingElse = False;
189 XEvent ev;
190 #ifdef MOX_CYCLING
191 WSwitchPanel *swpanel = NULL;
192 #endif
193 KeyCode leftKey, rightKey;
195 if (!wwin)
196 return;
198 leftKey = XKeysymToKeycode(dpy, XK_Left);
199 rightKey = XKeysymToKeycode(dpy, XK_Right);
201 if (next)
202 hasModifier = (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0);
203 else
204 hasModifier = (wKeyBindings[WKBD_FOCUSPREV].modifier != 0);
206 if (hasModifier) {
207 keymap = XGetModifierMapping(dpy);
209 #ifdef DEBUG
210 printf("Grabbing keyboard\n");
211 #endif
212 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync,
213 CurrentTime);
216 scr->flags.doing_alt_tab = 1;
218 #ifdef MOX_CYCLING
219 swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace);
220 oldFocused = wwin;
222 if (swpanel) {
223 newFocused = wSwitchPanelSelectNext(swpanel, !next);
224 wWindowFocus(newFocused, oldFocused);
225 oldFocused = newFocused;
227 else
228 newFocused= wwin;
229 #else /* !MOX_CYCLING */
230 if (next) {
231 if (wPreferences.windows_cycling)
232 newFocused = nextToFocusAfter(wwin);
233 else
234 newFocused = nextFocusWindow(wwin);
235 } else {
236 if (wPreferences.windows_cycling)
237 newFocused = nextToFocusBefore(wwin);
238 else
239 newFocused = prevFocusWindow(wwin);
242 if (wPreferences.circ_raise)
243 XRaiseWindow(dpy, newFocused->frame->core->window);
244 wWindowFocus(newFocused, scr->focused_window);
245 oldFocused = newFocused;
246 #endif /* !MOX_CYCLING */
248 while (hasModifier && !done) {
249 WMMaskEvent(dpy, KeyPressMask|KeyReleaseMask|ExposureMask|PointerMotionMask, &ev);
251 if (ev.type != KeyRelease && ev.type != KeyPress) {
252 WMHandleEvent(&ev);
253 continue;
255 /* ignore CapsLock */
256 modifiers = ev.xkey.state & ValidModMask;
258 if (ev.type == KeyPress) {
259 #ifdef DEBUG
260 printf("Got key press\n");
261 #endif
262 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
263 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
264 || ev.xkey.keycode == rightKey) {
266 #ifdef MOX_CYCLING
267 if (swpanel) {
268 newFocused = wSwitchPanelSelectNext(swpanel, False);
269 wWindowFocus(newFocused, oldFocused);
270 oldFocused = newFocused;
272 #else /* !MOX_CYCLING */
273 newFocused = nextToFocusAfter(newFocused);
274 wWindowFocus(newFocused, oldFocused);
275 oldFocused = newFocused;
277 if (wPreferences.circ_raise) {
278 /* restore order */
279 CommitStacking(scr);
280 XRaiseWindow(dpy, newFocused->frame->core->window);
282 #endif /* !MOX_CYCLING */
283 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
284 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
285 || ev.xkey.keycode == leftKey) {
287 #ifdef MOX_CYCLING
288 if (swpanel) {
289 newFocused = wSwitchPanelSelectNext(swpanel, True);
290 wWindowFocus(newFocused, oldFocused);
291 oldFocused = newFocused;
293 #else /* !MOX_CYCLING */
294 newFocused = nextToFocusBefore(newFocused);
295 wWindowFocus(newFocused, oldFocused);
296 oldFocused = newFocused;
298 if (wPreferences.circ_raise) {
299 /* restore order */
300 CommitStacking(scr);
301 XRaiseWindow(dpy, newFocused->frame->core->window);
303 #endif /* !MOX_CYCLING */
304 } else if (ev.type == MotionNotify) {
305 WWindow *tmp;
306 if (swpanel) {
307 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
308 if (tmp) {
309 newFocused = tmp;
310 wWindowFocus(newFocused, oldFocused);
311 oldFocused = newFocused;
314 } else {
315 #ifdef DEBUG
316 printf("Got something else\n");
317 #endif
318 somethingElse = True;
319 done = True;
321 } else if (ev.type == KeyRelease) {
322 int i;
324 #ifdef DEBUG
325 printf("Got key release\n");
326 #endif
327 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
328 if (keymap->modifiermap[i] == ev.xkey.keycode &&
329 wKeyBindings[WKBD_FOCUSNEXT].modifier
330 & 1<<(i/keymap->max_keypermod)) {
331 done = True;
332 break;
337 if (keymap)
338 XFreeModifiermap(keymap);
340 if (hasModifier) {
341 #ifdef DEBUG
342 printf("Ungrabbing keyboard\n");
343 #endif
344 XUngrabKeyboard(dpy, CurrentTime);
346 wSetFocusTo(scr, newFocused);
348 #ifdef MOX_CYCLING
349 if (swpanel)
350 wSwitchPanelDestroy(swpanel);
351 #endif
353 if (wPreferences.circ_raise && newFocused) {
354 wRaiseFrame(newFocused->frame->core);
355 CommitStacking(scr);
358 scr->flags.doing_alt_tab = 0;
360 if (somethingElse) {
361 WMHandleEvent(&ev);