Change to the linux kernel coding style
[wmaker-crm.git] / src / cycling.c
Commit [+]AuthorDateLineData
caa86529 kojima2000-04-07 22:47:04 +00001/* cycling.c- window cycling
6830b057 dan2004-10-12 21:28:27 +00002 *
caa86529 kojima2000-04-07 22:47:04 +00003 * Window Maker window manager
6830b057 dan2004-10-12 21:28:27 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 2000-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00006 *
caa86529 kojima2000-04-07 22:47:04 +00007 * 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.
11 *
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.
16 *
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
6830b057 dan2004-10-12 21:28:27 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
caa86529 kojima2000-04-07 22:47:04 +000020 * USA.
21 */
22
23#include "wconfig.h"
24
25#include <X11/Xlib.h>
26#include <X11/Xutil.h>
f54f0856 kojima2004-10-14 05:02:24 +000027#include <X11/keysym.h>
caa86529 kojima2000-04-07 22:47:04 +000028
caa86529 kojima2000-04-07 22:47:04 +000029#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"
a10214a5 kojima2002-11-28 22:04:07 +000039#include "xinerama.h"
f54f0856 kojima2004-10-14 05:02:24 +000040#include "switchpanel.h"
caa86529 kojima2000-04-07 22:47:04 +000041
42/* Globals */
43extern WPreferences wPreferences;
44
45extern WShortKey wKeyBindings[WKBD_LAST];
46
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020047static void raiseWindow(WSwitchPanel * swpanel, WWindow * wwin)
7ab70f69 kojima2001-01-06 23:52:00 +000048{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020049 Window swwin = wSwitchPanelGetWindow(swpanel);
7ab70f69 kojima2001-01-06 23:52:00 +000050
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020051 if (wwin->flags.mapped) {
52 if (swwin != None) {
53 Window win[2];
7ab70f69 kojima2001-01-06 23:52:00 +000054
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020055 win[0] = swwin;
56 win[1] = wwin->frame->core->window;
57
58 XRestackWindows(dpy, win, 2);
59 } else
60 XRaiseWindow(dpy, wwin->frame->core->window);
61 }
62}
7ab70f69 kojima2001-01-06 23:52:00 +000063
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020064void StartWindozeCycle(WWindow * wwin, XEvent * event, Bool next)
caa86529 kojima2000-04-07 22:47:04 +000065{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020066 WScreen *scr = wScreenForRootWindow(event->xkey.root);
67 Bool done = False;
68 WWindow *newFocused;
69 WWindow *oldFocused;
70 int modifiers;
71 XModifierKeymap *keymap = NULL;
72 Bool hasModifier;
73 Bool somethingElse = False;
74 XEvent ev;
75 WSwitchPanel *swpanel = NULL;
76 KeyCode leftKey, rightKey, homeKey, endKey, shiftLKey, shiftRKey;
77
78 if (!wwin)
79 return;
80
81 leftKey = XKeysymToKeycode(dpy, XK_Left);
82 rightKey = XKeysymToKeycode(dpy, XK_Right);
83 homeKey = XKeysymToKeycode(dpy, XK_Home);
84 endKey = XKeysymToKeycode(dpy, XK_End);
85 shiftLKey = XKeysymToKeycode(dpy, XK_Shift_L);
86 shiftRKey = XKeysymToKeycode(dpy, XK_Shift_R);
87
88 if (next)
89 hasModifier = (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0);
90 else
91 hasModifier = (wKeyBindings[WKBD_FOCUSPREV].modifier != 0);
92
93 if (hasModifier) {
94 keymap = XGetModifierMapping(dpy);
caa86529 kojima2000-04-07 22:47:04 +000095
9aca0d5f dan2004-10-12 01:34:32 +000096#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020097 printf("Grabbing keyboard\n");
9aca0d5f dan2004-10-12 01:34:32 +000098#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020099 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
100 }
101
102 scr->flags.doing_alt_tab = 1;
103
104 swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace);
105 oldFocused = wwin;
106
107 if (swpanel) {
108 newFocused = wSwitchPanelSelectNext(swpanel, !next);
109 if (newFocused) {
110 wWindowFocus(newFocused, oldFocused);
111 oldFocused = newFocused;
112
113 if (wPreferences.circ_raise)
114 raiseWindow(swpanel, newFocused);
115 }
116 } else {
117 if (wwin->frame->workspace == scr->current_workspace)
118 newFocused = wwin;
119 else
120 newFocused = NULL;
121 }
122
123 while (hasModifier && !done) {
124 int i;
125
126 WMMaskEvent(dpy, KeyPressMask | KeyReleaseMask | ExposureMask
127 | PointerMotionMask | ButtonReleaseMask, &ev);
128
129 /* ignore CapsLock */
130 modifiers = ev.xkey.state & ValidModMask;
131
132 switch (ev.type) {
133 case KeyPress:
9aca0d5f dan2004-10-12 01:34:32 +0000134#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200135 printf("Got key press\n");
9aca0d5f dan2004-10-12 01:34:32 +0000136#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200137 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
138 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
139 || ev.xkey.keycode == rightKey) {
140
141 if (swpanel) {
142 newFocused = wSwitchPanelSelectNext(swpanel, False);
143 if (newFocused) {
144 wWindowFocus(newFocused, oldFocused);
145 oldFocused = newFocused;
146
147 if (wPreferences.circ_raise) {
148 CommitStacking(scr);
149 raiseWindow(swpanel, newFocused);
150 }
151 }
152 }
153 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
154 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
155 || ev.xkey.keycode == leftKey) {
156
157 if (swpanel) {
158 newFocused = wSwitchPanelSelectNext(swpanel, True);
159 if (newFocused) {
160 wWindowFocus(newFocused, oldFocused);
161 oldFocused = newFocused;
162
163 if (wPreferences.circ_raise) {
164 CommitStacking(scr);
165 raiseWindow(swpanel, newFocused);
166 }
167 }
168 }
169 } else if (ev.xkey.keycode == homeKey || ev.xkey.keycode == endKey) {
170 if (swpanel) {
171 newFocused = wSwitchPanelSelectFirst(swpanel, ev.xkey.keycode != homeKey);
172 if (newFocused) {
173 wWindowFocus(newFocused, oldFocused);
174 oldFocused = newFocused;
175
176 if (wPreferences.circ_raise) {
177 CommitStacking(scr);
178 raiseWindow(swpanel, newFocused);
179 }
180 }
181 }
182 } else if (ev.xkey.keycode != shiftLKey && ev.xkey.keycode != shiftRKey) {
9aca0d5f dan2004-10-12 01:34:32 +0000183#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200184 printf("Got something else\n");
9aca0d5f dan2004-10-12 01:34:32 +0000185#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200186 somethingElse = True;
187 done = True;
188 }
189 break;
190 case KeyRelease:
9aca0d5f dan2004-10-12 01:34:32 +0000191#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200192 printf("Got key release\n");
9aca0d5f dan2004-10-12 01:34:32 +0000193#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200194 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
195 if (keymap->modifiermap[i] == ev.xkey.keycode &&
196 wKeyBindings[WKBD_FOCUSNEXT].modifier & 1 << (i / keymap->max_keypermod)) {
197 done = True;
198 break;
199 }
200 }
201 break;
202
203 case LeaveNotify:
204 case MotionNotify:
205 case ButtonRelease:
206 {
207 WWindow *tmp;
208 if (swpanel) {
209 tmp = wSwitchPanelHandleEvent(swpanel, &ev);
210 if (tmp) {
211 newFocused = tmp;
212 wWindowFocus(newFocused, oldFocused);
213 oldFocused = newFocused;
214
215 if (wPreferences.circ_raise) {
216 CommitStacking(scr);
217 raiseWindow(swpanel, newFocused);
218 }
219
220 if (ev.type == ButtonRelease)
221 done = True;
222 }
223 }
224 }
225 break;
226
227 default:
228 WMHandleEvent(&ev);
229 break;
230 }
231 }
232 if (keymap)
233 XFreeModifiermap(keymap);
234
235 if (hasModifier) {
9aca0d5f dan2004-10-12 01:34:32 +0000236#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200237 printf("Ungrabbing keyboard\n");
9aca0d5f dan2004-10-12 01:34:32 +0000238#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200239 XUngrabKeyboard(dpy, CurrentTime);
240 }
f6fb9fbb kojima2004-10-19 01:29:34 +0000241
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200242 if (swpanel)
243 wSwitchPanelDestroy(swpanel);
79864ba9 kojima2004-10-27 03:09:19 +0000244
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200245 if (newFocused) {
246 wRaiseFrame(newFocused->frame->core);
247 CommitStacking(scr);
248 if (!newFocused->flags.mapped)
249 wMakeWindowVisible(newFocused);
250 wSetFocusTo(scr, newFocused);
251 }
caa86529 kojima2000-04-07 22:47:04 +0000252
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200253 scr->flags.doing_alt_tab = 0;
a10214a5 kojima2002-11-28 22:04:07 +0000254
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200255 if (somethingElse)
256 WMHandleEvent(&ev);
caa86529 kojima2000-04-07 22:47:04 +0000257}