fixed typo bug in WPrefs/WindozeCycling
[wmaker-crm.git] / src / cycling.c
blob054ebc62fe92fd36c1b8028dd1b5ba131da042d4
1 /* cycling.c- window cycling
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 2000 Alfredo K. Kojima
6 *
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>
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"
40 /* Globals */
41 extern WPreferences wPreferences;
43 extern WShortKey wKeyBindings[WKBD_LAST];
50 static WWindow*
51 nextToFocusAfter(WWindow *wwin)
53 WWindow *tmp = wwin->prev;
55 while (tmp) {
56 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
57 return tmp;
59 tmp = tmp->prev;
62 tmp = wwin;
63 /* start over from the beginning of the list */
64 while (tmp->next)
65 tmp = tmp->next;
67 while (tmp && tmp != wwin) {
68 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
69 return tmp;
71 tmp = tmp->prev;
74 return wwin;
78 static WWindow*
79 nextToFocusBefore(WWindow *wwin)
81 WWindow *tmp = wwin->next;
83 while (tmp) {
84 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
85 return tmp;
87 tmp = tmp->next;
90 /* start over from the beginning of the list */
91 tmp = wwin;
92 while (tmp->prev)
93 tmp = tmp->prev;
95 while (tmp && tmp != wwin) {
96 if (wWindowCanReceiveFocus(tmp) && !WFLAGP(tmp, skip_window_list)) {
98 return tmp;
100 tmp = tmp->next;
103 return wwin;
109 static WWindow*
110 nextFocusWindow(WWindow *wwin)
112 WWindow *tmp, *closest, *min;
113 Window d;
115 if (!wwin)
116 return NULL;
117 tmp = wwin->prev;
118 closest = NULL;
119 min = wwin;
120 d = 0xffffffff;
121 while (tmp) {
122 if (wWindowCanReceiveFocus(tmp)
123 && (!WFLAGP(tmp, skip_window_list)|| tmp->flags.internal_window)) {
124 if (min->client_win > tmp->client_win)
125 min = tmp;
126 if (tmp->client_win > wwin->client_win
127 && (!closest
128 || (tmp->client_win - wwin->client_win) < d)) {
129 closest = tmp;
130 d = tmp->client_win - wwin->client_win;
133 tmp = tmp->prev;
135 if (!closest||closest==wwin)
136 return min;
137 return closest;
141 static WWindow*
142 prevFocusWindow(WWindow *wwin)
144 WWindow *tmp, *closest, *max;
145 Window d;
147 if (!wwin)
148 return NULL;
149 tmp = wwin->prev;
150 closest = NULL;
151 max = wwin;
152 d = 0xffffffff;
153 while (tmp) {
154 if (wWindowCanReceiveFocus(tmp) &&
155 (!WFLAGP(tmp, skip_window_list) || tmp->flags.internal_window)) {
156 if (max->client_win < tmp->client_win)
157 max = tmp;
158 if (tmp->client_win < wwin->client_win
159 && (!closest
160 || (wwin->client_win - tmp->client_win) < d)) {
161 closest = tmp;
162 d = wwin->client_win - tmp->client_win;
165 tmp = tmp->prev;
167 if (!closest||closest==wwin)
168 return max;
169 return closest;
175 void
176 StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
178 WScreen *scr = wScreenForRootWindow(event->xkey.root);
179 Bool done = False;
180 Bool openedSwitchMenu = False;
181 WWindow *newFocused;
182 WWindow *oldFocused;
183 int modifiers;
184 XModifierKeymap *keymap = NULL;
185 Bool hasModifier;
186 Bool somethingElse = False;
187 XEvent ev;
189 if (!wwin)
190 return;
192 if (next)
193 hasModifier = (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0);
194 else
195 hasModifier = (wKeyBindings[WKBD_FOCUSPREV].modifier != 0);
197 if (hasModifier) {
198 keymap = XGetModifierMapping(dpy);
201 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync,
202 CurrentTime);
205 if (next) {
206 if (wPreferences.windows_cycling)
207 newFocused = nextToFocusAfter(wwin);
208 else
209 newFocused = nextFocusWindow(wwin);
210 } else {
211 if (wPreferences.windows_cycling)
212 newFocused = nextToFocusBefore(wwin);
213 else
214 newFocused = prevFocusWindow(wwin);
217 scr->flags.doing_alt_tab = 1;
220 if (wPreferences.circ_raise)
221 XRaiseWindow(dpy, newFocused->frame->core->window);
222 wWindowFocus(newFocused, scr->focused_window);
223 oldFocused = newFocused;
225 if (hasModifier)
226 done = False;
227 else
228 done = True;
230 #if 0
231 if (wPreferences.popup_switchmenu &&
232 (!scr->switch_menu || !scr->switch_menu->flags.mapped)) {
234 OpenSwitchMenu(scr, scr->scr_width/2, scr->scr_height/2, False);
235 openedSwitchMenu = True;
237 #endif
238 while (!done) {
239 WMMaskEvent(dpy,KeyPressMask|KeyReleaseMask|ExposureMask, &ev);
241 if (ev.type != KeyRelease && ev.type != KeyPress) {
242 WMHandleEvent(&ev);
243 continue;
245 /* ignore CapsLock */
246 modifiers = ev.xkey.state & ValidModMask;
248 if (ev.type == KeyPress) {
249 if (wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
250 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers) {
252 UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
253 newFocused = nextToFocusAfter(newFocused);
254 wWindowFocus(newFocused, oldFocused);
255 oldFocused = newFocused;
257 if (wPreferences.circ_raise) {
258 /* restore order */
259 CommitStacking(scr);
260 XRaiseWindow(dpy, newFocused->frame->core->window);
263 UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
265 } else if (wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
266 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers) {
268 UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
269 newFocused = nextToFocusBefore(newFocused);
270 wWindowFocus(newFocused, oldFocused);
271 oldFocused = newFocused;
273 if (wPreferences.circ_raise) {
274 /* restore order */
275 CommitStacking(scr);
276 XRaiseWindow(dpy, newFocused->frame->core->window);
278 UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
280 } else {
281 somethingElse = True;
282 done = True;
284 } else if (ev.type == KeyRelease) {
285 int i;
287 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
288 if (keymap->modifiermap[i] == ev.xkey.keycode &&
289 wKeyBindings[WKBD_FOCUSNEXT].modifier
290 & 1<<(i/keymap->max_keypermod)) {
291 done = True;
292 break;
297 if (keymap)
298 XFreeModifiermap(keymap);
300 if (hasModifier) {
301 XUngrabKeyboard(dpy, CurrentTime);
303 wSetFocusTo(scr, newFocused);
305 if (wPreferences.circ_raise) {
306 wRaiseFrame(newFocused->frame->core);
307 CommitStacking(scr);
310 scr->flags.doing_alt_tab = 0;
311 if (openedSwitchMenu)
312 OpenSwitchMenu(scr, scr->scr_width/2, scr->scr_height/2, False);
314 if (somethingElse) {
315 WMHandleEvent(&ev);