increased libwraster version
[wmaker-crm.git] / src / cycling.c
blob8f01d6cee43b65e6248e02c7037171016c6c8c06
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 Window win[2];
55 win[0]= swwin;
56 win[1]= wwin->frame->core->window;
58 XRestackWindows(dpy, win, 2);
64 void
65 StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
67 WScreen *scr = wScreenForRootWindow(event->xkey.root);
68 Bool done = False;
69 WWindow *newFocused;
70 WWindow *oldFocused;
71 int modifiers;
72 XModifierKeymap *keymap = NULL;
73 Bool hasModifier;
74 Bool somethingElse = False;
75 XEvent ev;
76 WSwitchPanel *swpanel = NULL;
77 KeyCode leftKey, rightKey, homeKey, endKey, shiftLKey, shiftRKey;
79 if (!wwin)
80 return;
82 leftKey = XKeysymToKeycode(dpy, XK_Left);
83 rightKey = XKeysymToKeycode(dpy, XK_Right);
84 homeKey = XKeysymToKeycode(dpy, XK_Home);
85 endKey = XKeysymToKeycode(dpy, XK_End);
86 shiftLKey = XKeysymToKeycode(dpy, XK_Shift_L);
87 shiftRKey = XKeysymToKeycode(dpy, XK_Shift_R);
89 if (next)
90 hasModifier = (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0);
91 else
92 hasModifier = (wKeyBindings[WKBD_FOCUSPREV].modifier != 0);
94 if (hasModifier) {
95 keymap = XGetModifierMapping(dpy);
97 #ifdef DEBUG
98 printf("Grabbing keyboard\n");
99 #endif
100 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync,
101 CurrentTime);
104 scr->flags.doing_alt_tab = 1;
106 swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace);
107 oldFocused = wwin;
109 if (swpanel) {
110 newFocused = wSwitchPanelSelectNext(swpanel, !next);
111 if (newFocused) {
112 wWindowFocus(newFocused, oldFocused);
113 oldFocused = newFocused;
115 if (wPreferences.circ_raise)
116 raiseWindow(swpanel, newFocused);
119 else
121 if (wwin->frame->workspace == scr->current_workspace)
122 newFocused= wwin;
123 else
124 newFocused= NULL;
127 while (hasModifier && !done) {
128 int i;
130 WMMaskEvent(dpy, KeyPressMask|KeyReleaseMask|ExposureMask
131 |PointerMotionMask|ButtonReleaseMask, &ev);
133 /* ignore CapsLock */
134 modifiers = ev.xkey.state & ValidModMask;
136 switch (ev.type) {
137 case KeyPress:
138 #ifdef DEBUG
139 printf("Got key press\n");
140 #endif
141 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
142 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
143 || ev.xkey.keycode == rightKey) {
145 if (swpanel) {
146 newFocused = wSwitchPanelSelectNext(swpanel, False);
147 if (newFocused) {
148 wWindowFocus(newFocused, oldFocused);
149 oldFocused = newFocused;
151 if (wPreferences.circ_raise) {
152 CommitStacking(scr);
153 raiseWindow(swpanel, newFocused);
157 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
158 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
159 || ev.xkey.keycode == leftKey) {
161 if (swpanel) {
162 newFocused = wSwitchPanelSelectNext(swpanel, True);
163 if (newFocused) {
164 wWindowFocus(newFocused, oldFocused);
165 oldFocused = newFocused;
167 if (wPreferences.circ_raise) {
168 CommitStacking(scr);
169 raiseWindow(swpanel, newFocused);
173 } else if (ev.xkey.keycode == homeKey || ev.xkey.keycode == endKey) {
174 if (swpanel) {
175 newFocused = wSwitchPanelSelectFirst(swpanel, ev.xkey.keycode != homeKey);
176 if (newFocused) {
177 wWindowFocus(newFocused, oldFocused);
178 oldFocused = newFocused;
180 if (wPreferences.circ_raise) {
181 CommitStacking(scr);
182 raiseWindow(swpanel, newFocused);
186 } else if (ev.xkey.keycode != shiftLKey && ev.xkey.keycode != shiftRKey) {
187 #ifdef DEBUG
188 printf("Got something else\n");
189 #endif
190 somethingElse = True;
191 done = True;
193 break;
194 case KeyRelease:
195 #ifdef DEBUG
196 printf("Got key release\n");
197 #endif
198 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
199 if (keymap->modifiermap[i] == ev.xkey.keycode &&
200 wKeyBindings[WKBD_FOCUSNEXT].modifier
201 & 1<<(i/keymap->max_keypermod)) {
202 done = True;
203 break;
206 break;
208 case MotionNotify:
209 case ButtonRelease:
211 WWindow *tmp;
212 if (swpanel) {
213 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
214 if (tmp) {
215 newFocused = tmp;
216 wWindowFocus(newFocused, oldFocused);
217 oldFocused = newFocused;
219 if (wPreferences.circ_raise) {
220 CommitStacking(scr);
221 raiseWindow(swpanel, newFocused);
224 if (ev.type == ButtonRelease)
225 done= True;
229 break;
231 default:
232 WMHandleEvent(&ev);
233 break;
236 if (keymap)
237 XFreeModifiermap(keymap);
239 if (hasModifier) {
240 #ifdef DEBUG
241 printf("Ungrabbing keyboard\n");
242 #endif
243 XUngrabKeyboard(dpy, CurrentTime);
246 if (newFocused) {
247 wRaiseFrame(newFocused->frame->core);
248 CommitStacking(scr);
249 if (!newFocused->flags.mapped)
250 wMakeWindowVisible(newFocused);
251 wSetFocusTo(scr, newFocused);
254 if (!getenv("SWPDEBUG"))
256 if (swpanel)
257 wSwitchPanelDestroy(swpanel);
259 scr->flags.doing_alt_tab = 0;
261 if (somethingElse)
262 WMHandleEvent(&ev);