wmaker: replace and be replaced (ICCCM protocol)
[wmaker-crm.git] / src / cycling.c
blob916d9477804b10a7b59a6b870d705b41e7368f22
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 || wwin->flags.shaded) {
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 == scr->current_workspace)
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 switch panel 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 == leftKey || ev.xkey.keycode == rightKey)
201 break;
203 if (ev.xkey.keycode == XK_Return)
204 break;
206 if (ev.xkey.keycode != binding.keycode)
207 done = True;
209 break;
211 case EnterNotify:
213 /* ignore unwanted EnterNotify's */
214 break;
216 case LeaveNotify:
217 case MotionNotify:
219 case ButtonRelease:
221 WWindow *tmp;
222 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
223 if (tmp) {
224 newFocused = tmp;
225 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
227 if (ev.type == ButtonRelease)
228 done = True;
231 break;
233 default:
234 WMHandleEvent(&ev);
235 break;
239 if (hasModifier)
240 XUngrabKeyboard(dpy, CurrentTime);
242 if (swpanel)
243 wSwitchPanelDestroy(swpanel);
245 if (newFocused && !esc_cancel) {
246 wRaiseFrame(newFocused->frame->core);
247 CommitStacking(scr);
248 if (!newFocused->flags.mapped)
249 wMakeWindowVisible(newFocused);
250 wSetFocusTo(scr, newFocused);
253 scr->flags.doing_alt_tab = 0;
255 if (somethingElse)
256 WMHandleEvent(&ev);