fixed bugs, re-enabled circulate raise
[wmaker-crm.git] / src / cycling.c
blob05fd2a4aae564f2b36ac3aad647292a39e52d8bf
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 // TODO: remove non-MOX_CYCLING junk
30 // fix the stacking/window raising during alt-tabbing
31 // allow selection of icons with mouse
33 #include "WindowMaker.h"
34 #include "GNUstep.h"
35 #include "screen.h"
36 #include "wcore.h"
37 #include "window.h"
38 #include "framewin.h"
39 #include "keybind.h"
40 #include "actions.h"
41 #include "stacking.h"
42 #include "funcs.h"
43 #include "xinerama.h"
44 #include "switchpanel.h"
46 /* Globals */
47 extern WPreferences wPreferences;
49 extern WShortKey wKeyBindings[WKBD_LAST];
52 static void raiseWindow(WSwitchPanel *swpanel, WWindow *wwin)
54 if (wwin->flags.mapped) {
55 Window win[2];
57 win[0]= wSwitchPanelGetWindow(swpanel);
58 win[1]= wwin->frame->core->window;
60 XRestackWindows(dpy, win, 2);
66 void
67 StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
69 WScreen *scr = wScreenForRootWindow(event->xkey.root);
70 Bool done = False;
71 WWindow *newFocused;
72 WWindow *oldFocused;
73 int modifiers;
74 XModifierKeymap *keymap = NULL;
75 Bool hasModifier;
76 Bool somethingElse = False;
77 XEvent ev;
78 WSwitchPanel *swpanel = NULL;
79 KeyCode leftKey, rightKey, homeKey, endKey;
81 if (!wwin || wwin->frame->workspace != scr->current_workspace)
82 return;
84 leftKey = XKeysymToKeycode(dpy, XK_Left);
85 rightKey = XKeysymToKeycode(dpy, XK_Right);
86 homeKey = XKeysymToKeycode(dpy, XK_Home);
87 endKey = XKeysymToKeycode(dpy, XK_End);
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
120 newFocused= wwin;
122 while (hasModifier && !done) {
123 WMMaskEvent(dpy, KeyPressMask|KeyReleaseMask|ExposureMask|PointerMotionMask, &ev);
125 if (ev.type != KeyRelease && ev.type != KeyPress) {
126 WMHandleEvent(&ev);
127 continue;
129 /* ignore CapsLock */
130 modifiers = ev.xkey.state & ValidModMask;
132 if (ev.type == KeyPress) {
133 #ifdef DEBUG
134 printf("Got key press\n");
135 #endif
136 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
137 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
138 || ev.xkey.keycode == rightKey) {
140 if (swpanel) {
141 newFocused = wSwitchPanelSelectNext(swpanel, False);
142 if (newFocused) {
143 wWindowFocus(newFocused, oldFocused);
144 oldFocused = newFocused;
146 if (wPreferences.circ_raise) {
147 CommitStacking(scr);
148 raiseWindow(swpanel, newFocused);
152 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
153 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
154 || ev.xkey.keycode == leftKey) {
156 if (swpanel) {
157 newFocused = wSwitchPanelSelectNext(swpanel, True);
158 if (newFocused) {
159 wWindowFocus(newFocused, oldFocused);
160 oldFocused = newFocused;
162 if (wPreferences.circ_raise) {
163 CommitStacking(scr);
164 raiseWindow(swpanel, newFocused);
168 } else if (ev.xkey.keycode == homeKey || ev.xkey.keycode == endKey) {
169 if (swpanel) {
170 newFocused = wSwitchPanelSelectFirst(swpanel, ev.xkey.keycode != homeKey);
171 if (newFocused) {
172 wWindowFocus(newFocused, oldFocused);
173 oldFocused = newFocused;
175 if (wPreferences.circ_raise) {
176 CommitStacking(scr);
177 raiseWindow(swpanel, newFocused);
181 } else if (ev.type == MotionNotify) {
182 WWindow *tmp;
183 if (swpanel) {
184 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
185 if (tmp) {
186 newFocused = tmp;
187 wWindowFocus(newFocused, oldFocused);
188 oldFocused = newFocused;
191 } else {
192 #ifdef DEBUG
193 printf("Got something else\n");
194 #endif
195 somethingElse = True;
196 done = True;
198 } else if (ev.type == KeyRelease) {
199 int i;
201 #ifdef DEBUG
202 printf("Got key release\n");
203 #endif
204 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
205 if (keymap->modifiermap[i] == ev.xkey.keycode &&
206 wKeyBindings[WKBD_FOCUSNEXT].modifier
207 & 1<<(i/keymap->max_keypermod)) {
208 done = True;
209 break;
214 if (keymap)
215 XFreeModifiermap(keymap);
217 if (hasModifier) {
218 #ifdef DEBUG
219 printf("Ungrabbing keyboard\n");
220 #endif
221 XUngrabKeyboard(dpy, CurrentTime);
224 if (newFocused) {
225 CommitStacking(scr);
226 if (!newFocused->flags.mapped)
227 wMakeWindowVisible(newFocused);
228 wSetFocusTo(scr, newFocused);
229 wRaiseFrame(newFocused->frame->core);
232 if (swpanel)
233 wSwitchPanelDestroy(swpanel);
235 scr->flags.doing_alt_tab = 0;
237 if (somethingElse)
238 WMHandleEvent(&ev);