configure: Do not keep assertion if debug was not enabled
[wmaker-crm.git] / src / cycling.c
blob7a9afc95cf5a46857025b10617ad530fee8e8554
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <stdlib.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 "window.h"
33 #include "framewin.h"
34 #include "keybind.h"
35 #include "actions.h"
36 #include "stacking.h"
37 #include "cycling.h"
38 #include "xinerama.h"
39 #include "switchpanel.h"
42 static void raiseWindow(WSwitchPanel * swpanel, WWindow * wwin)
44 Window swwin = wSwitchPanelGetWindow(swpanel);
46 if (wwin->flags.mapped) {
47 if (swwin != None) {
48 Window win[2];
50 win[0] = swwin;
51 win[1] = wwin->frame->core->window;
53 XRestackWindows(dpy, win, 2);
54 } else
55 XRaiseWindow(dpy, wwin->frame->core->window);
59 static WWindow *change_focus_and_raise(WWindow *newFocused, WWindow *oldFocused,
60 WSwitchPanel *swpanel, WScreen *scr, Bool esc_cancel)
62 if (!newFocused)
63 return oldFocused;
65 wWindowFocus(newFocused, oldFocused);
66 oldFocused = newFocused;
68 if (wPreferences.circ_raise) {
69 CommitStacking(scr);
71 if (!esc_cancel)
72 raiseWindow(swpanel, newFocused);
75 return oldFocused;
78 void StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next, Bool class_only)
81 WShortKey binding;
82 WSwitchPanel *swpanel = NULL;
83 WScreen *scr = wScreenForRootWindow(event->xkey.root);
84 KeyCode leftKey = XKeysymToKeycode(dpy, XK_Left);
85 KeyCode rightKey = XKeysymToKeycode(dpy, XK_Right);
86 KeyCode homeKey = XKeysymToKeycode(dpy, XK_Home);
87 KeyCode endKey = XKeysymToKeycode(dpy, XK_End);
88 KeyCode shiftLKey = XKeysymToKeycode(dpy, XK_Shift_L);
89 KeyCode shiftRKey = XKeysymToKeycode(dpy, XK_Shift_R);
90 KeyCode escapeKey = XKeysymToKeycode(dpy, XK_Escape);
91 KeyCode returnKey = XKeysymToKeycode(dpy, XK_Return);
92 Bool esc_cancel = False;
93 Bool somethingElse = False;
94 Bool done = False;
95 Bool hasModifier;
96 int modifiers;
97 WWindow *newFocused;
98 WWindow *oldFocused;
99 XEvent ev;
102 if (!wwin)
103 return;
105 if (next) {
106 if (class_only)
107 binding = wKeyBindings[WKBD_GROUPNEXT];
108 else
109 binding = wKeyBindings[WKBD_FOCUSNEXT];
110 } else {
111 if (class_only)
112 binding = wKeyBindings[WKBD_GROUPPREV];
113 else
114 binding = wKeyBindings[WKBD_FOCUSPREV];
117 hasModifier = (binding.modifier != 0);
118 if (hasModifier)
119 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
121 scr->flags.doing_alt_tab = 1;
123 swpanel = wInitSwitchPanel(scr, wwin, class_only);
124 oldFocused = wwin;
126 if (swpanel) {
127 if (wwin->flags.mapped && !wPreferences.panel_only_open)
128 newFocused = wSwitchPanelSelectNext(swpanel, !next, True, False);
129 else
130 newFocused = wSwitchPanelSelectFirst(swpanel, False);
132 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
133 } else {
134 if (wwin->frame->workspace == w_global.workspace.current)
135 newFocused = wwin;
136 else
137 newFocused = NULL;
140 while (hasModifier && !done) {
141 WMMaskEvent(dpy, KeyPressMask | KeyReleaseMask | ExposureMask
142 | PointerMotionMask | ButtonReleaseMask | EnterWindowMask, &ev);
144 /* ignore CapsLock */
145 modifiers = ev.xkey.state & w_global.shortcut.modifiers_mask;
147 if (!swpanel)
148 break;
150 switch (ev.type) {
151 case KeyPress:
152 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
153 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
154 || (wKeyBindings[WKBD_GROUPNEXT].keycode == ev.xkey.keycode
155 && wKeyBindings[WKBD_GROUPNEXT].modifier == modifiers)
156 || ev.xkey.keycode == rightKey) {
158 newFocused = wSwitchPanelSelectNext(swpanel, False, ev.xkey.keycode != rightKey, (!class_only && wKeyBindings[WKBD_GROUPNEXT].keycode == ev.xkey.keycode && wKeyBindings[WKBD_GROUPNEXT].modifier == modifiers));
159 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
161 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
162 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
163 || (wKeyBindings[WKBD_GROUPPREV].keycode == ev.xkey.keycode
164 && wKeyBindings[WKBD_GROUPPREV].modifier == modifiers)
165 || ev.xkey.keycode == leftKey) {
167 newFocused = wSwitchPanelSelectNext(swpanel, True, ev.xkey.keycode != leftKey, (!class_only && wKeyBindings[WKBD_GROUPPREV].keycode == ev.xkey.keycode && wKeyBindings[WKBD_GROUPPREV].modifier == modifiers));
168 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
170 } else if (ev.xkey.keycode == homeKey || ev.xkey.keycode == endKey) {
172 newFocused = wSwitchPanelSelectFirst(swpanel, ev.xkey.keycode != homeKey);
173 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
175 } else if (ev.xkey.keycode == escapeKey) {
177 /* Focus the first window of the swpanel, despite the 'False' */
178 newFocused = wSwitchPanelSelectFirst(swpanel, False);
179 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, True);
180 esc_cancel = True;
181 done = True;
183 } else if (ev.xkey.keycode == returnKey) {
185 /* Close the switchpanel without eating the keypress */
186 done = True;
188 } else if (ev.xkey.keycode != shiftLKey && ev.xkey.keycode != shiftRKey) {
190 somethingElse = True;
191 done = True;
193 break;
195 case KeyRelease:
196 if (ev.xkey.keycode == shiftLKey || ev.xkey.keycode == shiftRKey)
197 if (wPreferences.strict_windoze_cycle)
198 break;
200 if (ev.xkey.keycode == XK_Return)
201 break;
203 if (ev.xkey.keycode != binding.keycode)
204 done = True;
206 break;
208 case EnterNotify:
210 /* ignore unwanted EnterNotify's */
211 break;
213 case LeaveNotify:
214 case MotionNotify:
216 case ButtonRelease:
218 WWindow *tmp;
219 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
220 if (tmp) {
221 newFocused = tmp;
222 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
224 if (ev.type == ButtonRelease)
225 done = True;
228 break;
230 default:
231 WMHandleEvent(&ev);
232 break;
236 if (hasModifier)
237 XUngrabKeyboard(dpy, CurrentTime);
239 if (swpanel)
240 wSwitchPanelDestroy(swpanel);
242 if (newFocused && !esc_cancel) {
243 wRaiseFrame(newFocused->frame->core);
244 CommitStacking(scr);
245 if (!newFocused->flags.mapped)
246 wMakeWindowVisible(newFocused);
247 wSetFocusTo(scr, newFocused);
250 scr->flags.doing_alt_tab = 0;
252 if (somethingElse)
253 WMHandleEvent(&ev);