added option to disable switchpanel
[wmaker-crm.git] / src / cycling.c
blobee4218e9a551f3ef42b62b85517bc1677b7c6e34
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 Window swwin= wSwitchPanelGetWindow(swpanel);
52 if (wwin->flags.mapped) {
53 if (swwin!=None) {
54 Window win[2];
56 win[0]= swwin;
57 win[1]= wwin->frame->core->window;
59 XRestackWindows(dpy, win, 2);
60 } else
61 XRaiseWindow(dpy, wwin->frame->core->window);
67 void
68 StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
70 WScreen *scr = wScreenForRootWindow(event->xkey.root);
71 Bool done = False;
72 WWindow *newFocused;
73 WWindow *oldFocused;
74 int modifiers;
75 XModifierKeymap *keymap = NULL;
76 Bool hasModifier;
77 Bool somethingElse = False;
78 XEvent ev;
79 WSwitchPanel *swpanel = NULL;
80 KeyCode leftKey, rightKey, homeKey, endKey, shiftLKey, shiftRKey;
82 if (!wwin)
83 return;
85 leftKey = XKeysymToKeycode(dpy, XK_Left);
86 rightKey = XKeysymToKeycode(dpy, XK_Right);
87 homeKey = XKeysymToKeycode(dpy, XK_Home);
88 endKey = XKeysymToKeycode(dpy, XK_End);
89 shiftLKey = XKeysymToKeycode(dpy, XK_Shift_L);
90 shiftRKey = XKeysymToKeycode(dpy, XK_Shift_R);
92 if (next)
93 hasModifier = (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0);
94 else
95 hasModifier = (wKeyBindings[WKBD_FOCUSPREV].modifier != 0);
97 if (hasModifier) {
98 keymap = XGetModifierMapping(dpy);
100 #ifdef DEBUG
101 printf("Grabbing keyboard\n");
102 #endif
103 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync,
104 CurrentTime);
107 scr->flags.doing_alt_tab = 1;
109 swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace);
110 oldFocused = wwin;
112 if (swpanel) {
113 newFocused = wSwitchPanelSelectNext(swpanel, !next);
114 if (newFocused) {
115 wWindowFocus(newFocused, oldFocused);
116 oldFocused = newFocused;
118 if (wPreferences.circ_raise)
119 raiseWindow(swpanel, newFocused);
122 else
124 if (wwin->frame->workspace == scr->current_workspace)
125 newFocused= wwin;
126 else
127 newFocused= NULL;
130 while (hasModifier && !done) {
131 int i;
133 WMMaskEvent(dpy, KeyPressMask|KeyReleaseMask|ExposureMask
134 |PointerMotionMask|ButtonReleaseMask, &ev);
136 /* ignore CapsLock */
137 modifiers = ev.xkey.state & ValidModMask;
139 switch (ev.type) {
140 case KeyPress:
141 #ifdef DEBUG
142 printf("Got key press\n");
143 #endif
144 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
145 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
146 || ev.xkey.keycode == rightKey) {
148 if (swpanel) {
149 newFocused = wSwitchPanelSelectNext(swpanel, False);
150 if (newFocused) {
151 wWindowFocus(newFocused, oldFocused);
152 oldFocused = newFocused;
154 if (wPreferences.circ_raise) {
155 CommitStacking(scr);
156 raiseWindow(swpanel, newFocused);
160 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
161 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
162 || ev.xkey.keycode == leftKey) {
164 if (swpanel) {
165 newFocused = wSwitchPanelSelectNext(swpanel, True);
166 if (newFocused) {
167 wWindowFocus(newFocused, oldFocused);
168 oldFocused = newFocused;
170 if (wPreferences.circ_raise) {
171 CommitStacking(scr);
172 raiseWindow(swpanel, newFocused);
176 } else if (ev.xkey.keycode == homeKey || ev.xkey.keycode == endKey) {
177 if (swpanel) {
178 newFocused = wSwitchPanelSelectFirst(swpanel, ev.xkey.keycode != homeKey);
179 if (newFocused) {
180 wWindowFocus(newFocused, oldFocused);
181 oldFocused = newFocused;
183 if (wPreferences.circ_raise) {
184 CommitStacking(scr);
185 raiseWindow(swpanel, newFocused);
189 } else if (ev.xkey.keycode != shiftLKey && ev.xkey.keycode != shiftRKey) {
190 #ifdef DEBUG
191 printf("Got something else\n");
192 #endif
193 somethingElse = True;
194 done = True;
196 break;
197 case KeyRelease:
198 #ifdef DEBUG
199 printf("Got key release\n");
200 #endif
201 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
202 if (keymap->modifiermap[i] == ev.xkey.keycode &&
203 wKeyBindings[WKBD_FOCUSNEXT].modifier
204 & 1<<(i/keymap->max_keypermod)) {
205 done = True;
206 break;
209 break;
211 case LeaveNotify:
212 case MotionNotify:
213 case ButtonRelease:
215 WWindow *tmp;
216 if (swpanel) {
217 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
218 if (tmp) {
219 newFocused = tmp;
220 wWindowFocus(newFocused, oldFocused);
221 oldFocused = newFocused;
223 if (wPreferences.circ_raise) {
224 CommitStacking(scr);
225 raiseWindow(swpanel, newFocused);
228 if (ev.type == ButtonRelease)
229 done= True;
233 break;
235 default:
236 WMHandleEvent(&ev);
237 break;
240 if (keymap)
241 XFreeModifiermap(keymap);
243 if (hasModifier) {
244 #ifdef DEBUG
245 printf("Ungrabbing keyboard\n");
246 #endif
247 XUngrabKeyboard(dpy, CurrentTime);
250 if (newFocused) {
251 wRaiseFrame(newFocused->frame->core);
252 CommitStacking(scr);
253 if (!newFocused->flags.mapped)
254 wMakeWindowVisible(newFocused);
255 wSetFocusTo(scr, newFocused);
258 scr->flags.doing_alt_tab = 0;
260 if (somethingElse)
261 WMHandleEvent(&ev);