added some netwm support in WINGs
[wmaker-crm.git] / src / cycling.c
blob401a1082c41c2027f83313a44f4ef7cfa5809c01
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 if (wwin->flags.mapped) {
51 Window win[2];
53 win[0]= wSwitchPanelGetWindow(swpanel);
54 win[1]= wwin->frame->core->window;
56 XRestackWindows(dpy, win, 2);
62 void
63 StartWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
65 WScreen *scr = wScreenForRootWindow(event->xkey.root);
66 Bool done = False;
67 WWindow *newFocused;
68 WWindow *oldFocused;
69 int modifiers;
70 XModifierKeymap *keymap = NULL;
71 Bool hasModifier;
72 Bool somethingElse = False;
73 XEvent ev;
74 WSwitchPanel *swpanel = NULL;
75 KeyCode leftKey, rightKey, homeKey, endKey, shiftLKey, shiftRKey;
77 if (!wwin)
78 return;
80 leftKey = XKeysymToKeycode(dpy, XK_Left);
81 rightKey = XKeysymToKeycode(dpy, XK_Right);
82 homeKey = XKeysymToKeycode(dpy, XK_Home);
83 endKey = XKeysymToKeycode(dpy, XK_End);
84 shiftLKey = XKeysymToKeycode(dpy, XK_Shift_L);
85 shiftRKey = XKeysymToKeycode(dpy, XK_Shift_R);
87 if (next)
88 hasModifier = (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0);
89 else
90 hasModifier = (wKeyBindings[WKBD_FOCUSPREV].modifier != 0);
92 if (hasModifier) {
93 keymap = XGetModifierMapping(dpy);
95 #ifdef DEBUG
96 printf("Grabbing keyboard\n");
97 #endif
98 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync,
99 CurrentTime);
102 scr->flags.doing_alt_tab = 1;
104 swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace);
105 oldFocused = wwin;
107 if (swpanel) {
108 newFocused = wSwitchPanelSelectNext(swpanel, !next);
109 if (newFocused) {
110 wWindowFocus(newFocused, oldFocused);
111 oldFocused = newFocused;
113 if (wPreferences.circ_raise)
114 raiseWindow(swpanel, newFocused);
117 else
119 if (wwin->frame->workspace == scr->current_workspace)
120 newFocused= wwin;
121 else
122 newFocused= NULL;
125 while (hasModifier && !done) {
126 int i;
128 WMMaskEvent(dpy, KeyPressMask|KeyReleaseMask|ExposureMask
129 |PointerMotionMask|ButtonReleaseMask, &ev);
131 /* ignore CapsLock */
132 modifiers = ev.xkey.state & ValidModMask;
134 switch (ev.type) {
135 case KeyPress:
136 #ifdef DEBUG
137 printf("Got key press\n");
138 #endif
139 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
140 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
141 || ev.xkey.keycode == rightKey) {
143 if (swpanel) {
144 newFocused = wSwitchPanelSelectNext(swpanel, False);
145 if (newFocused) {
146 wWindowFocus(newFocused, oldFocused);
147 oldFocused = newFocused;
149 if (wPreferences.circ_raise) {
150 CommitStacking(scr);
151 raiseWindow(swpanel, newFocused);
155 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
156 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
157 || ev.xkey.keycode == leftKey) {
159 if (swpanel) {
160 newFocused = wSwitchPanelSelectNext(swpanel, True);
161 if (newFocused) {
162 wWindowFocus(newFocused, oldFocused);
163 oldFocused = newFocused;
165 if (wPreferences.circ_raise) {
166 CommitStacking(scr);
167 raiseWindow(swpanel, newFocused);
171 } else if (ev.xkey.keycode == homeKey || ev.xkey.keycode == endKey) {
172 if (swpanel) {
173 newFocused = wSwitchPanelSelectFirst(swpanel, ev.xkey.keycode != homeKey);
174 if (newFocused) {
175 wWindowFocus(newFocused, oldFocused);
176 oldFocused = newFocused;
178 if (wPreferences.circ_raise) {
179 CommitStacking(scr);
180 raiseWindow(swpanel, newFocused);
184 } else if (ev.xkey.keycode != shiftLKey && ev.xkey.keycode != shiftRKey) {
185 #ifdef DEBUG
186 printf("Got something else\n");
187 #endif
188 somethingElse = True;
189 done = True;
191 break;
192 case KeyRelease:
193 #ifdef DEBUG
194 printf("Got key release\n");
195 #endif
196 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
197 if (keymap->modifiermap[i] == ev.xkey.keycode &&
198 wKeyBindings[WKBD_FOCUSNEXT].modifier
199 & 1<<(i/keymap->max_keypermod)) {
200 done = True;
201 break;
204 break;
206 case MotionNotify:
207 case ButtonRelease:
209 WWindow *tmp;
210 if (swpanel) {
211 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
212 if (tmp) {
213 newFocused = tmp;
214 wWindowFocus(newFocused, oldFocused);
215 oldFocused = newFocused;
217 if (wPreferences.circ_raise) {
218 CommitStacking(scr);
219 raiseWindow(swpanel, newFocused);
222 if (ev.type == ButtonRelease)
223 done= True;
227 break;
229 default:
230 WMHandleEvent(&ev);
231 break;
234 if (keymap)
235 XFreeModifiermap(keymap);
237 if (hasModifier) {
238 #ifdef DEBUG
239 printf("Ungrabbing keyboard\n");
240 #endif
241 XUngrabKeyboard(dpy, CurrentTime);
244 if (newFocused) {
245 wRaiseFrame(newFocused->frame->core);
246 CommitStacking(scr);
247 if (!newFocused->flags.mapped)
248 wMakeWindowVisible(newFocused);
249 wSetFocusTo(scr, newFocused);
252 if (swpanel)
253 wSwitchPanelDestroy(swpanel);
255 scr->flags.doing_alt_tab = 0;
257 if (somethingElse)
258 WMHandleEvent(&ev);