Mac OS X-style window cycling.
[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
108127f0 Vladimir Nadvornik2009-02-17 11:55:33 +010025#include <stdlib.h>
caa86529 kojima2000-04-07 22:47:04 +000026#include <X11/Xlib.h>
27#include <X11/Xutil.h>
f54f0856 kojima2004-10-14 05:02:24 +000028#include <X11/keysym.h>
caa86529 kojima2000-04-07 22:47:04 +000029
caa86529 kojima2000-04-07 22:47:04 +000030#include "WindowMaker.h"
31#include "GNUstep.h"
32#include "screen.h"
33#include "wcore.h"
34#include "window.h"
35#include "framewin.h"
36#include "keybind.h"
37#include "actions.h"
38#include "stacking.h"
39#include "funcs.h"
a10214a5 kojima2002-11-28 22:04:07 +000040#include "xinerama.h"
f54f0856 kojima2004-10-14 05:02:24 +000041#include "switchpanel.h"
caa86529 kojima2000-04-07 22:47:04 +000042
43/* Globals */
44extern WPreferences wPreferences;
45
46extern WShortKey wKeyBindings[WKBD_LAST];
47
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020048static void raiseWindow(WSwitchPanel * swpanel, WWindow * wwin)
7ab70f69 kojima2001-01-06 23:52:00 +000049{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020050 Window swwin = wSwitchPanelGetWindow(swpanel);
7ab70f69 kojima2001-01-06 23:52:00 +000051
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020052 if (wwin->flags.mapped) {
53 if (swwin != None) {
54 Window win[2];
7ab70f69 kojima2001-01-06 23:52:00 +000055
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020056 win[0] = swwin;
57 win[1] = wwin->frame->core->window;
58
59 XRestackWindows(dpy, win, 2);
60 } else
61 XRaiseWindow(dpy, wwin->frame->core->window);
62 }
63}
7ab70f69 kojima2001-01-06 23:52:00 +000064
333dbf54 Carlos R. Mafra2009-08-20 15:38:12 +020065static WWindow *change_focus_and_raise(WWindow *newFocused, WWindow *oldFocused,
f79379c0 Carlos R. Mafra2009-08-22 03:03:14 +020066 WSwitchPanel *swpanel, WScreen *scr, Bool esc_cancel)
333dbf54 Carlos R. Mafra2009-08-20 15:38:12 +020067{
d27e43e6
CM
Carlos R. Mafra2009-08-22 02:34:25 +020068 if (!newFocused)
69 return oldFocused;
70
333dbf54
CM
Carlos R. Mafra2009-08-20 15:38:12 +020071 wWindowFocus(newFocused, oldFocused);
72 oldFocused = newFocused;
73
74 if (wPreferences.circ_raise) {
75 CommitStacking(scr);
f79379c0
CM
Carlos R. Mafra2009-08-22 03:03:14 +020076
77 if (!esc_cancel)
78 raiseWindow(swpanel, newFocused);
333dbf54 Carlos R. Mafra2009-08-20 15:38:12 +020079 }
d27e43e6 Carlos R. Mafra2009-08-22 02:34:25 +020080
333dbf54
CM
Carlos R. Mafra2009-08-20 15:38:12 +020081 return oldFocused;
82}
83
18408fff Iain Patterson2009-09-14 14:37:15 +010084void StartWindozeCycle(WWindow * wwin, XEvent * event, Bool next, Bool class_only)
caa86529 kojima2000-04-07 22:47:04 +000085{
a9238c99
CM
Carlos R. Mafra2009-08-20 05:30:39 +020086
87 XModifierKeymap *keymap = NULL;
88 WSwitchPanel *swpanel = NULL;
89 WScreen *scr = wScreenForRootWindow(event->xkey.root);
90 KeyCode leftKey = XKeysymToKeycode(dpy, XK_Left);
91 KeyCode rightKey = XKeysymToKeycode(dpy, XK_Right);
92 KeyCode homeKey = XKeysymToKeycode(dpy, XK_Home);
93 KeyCode endKey = XKeysymToKeycode(dpy, XK_End);
94 KeyCode shiftLKey = XKeysymToKeycode(dpy, XK_Shift_L);
95 KeyCode shiftRKey = XKeysymToKeycode(dpy, XK_Shift_R);
f9bb2a42
CM
Carlos R. Mafra2009-08-20 16:00:26 +020096 KeyCode escapeKey = XKeysymToKeycode(dpy, XK_Escape);
97 Bool esc_cancel = False;
a9238c99
CM
Carlos R. Mafra2009-08-20 05:30:39 +020098 Bool somethingElse = False;
99 Bool done = False;
100 Bool hasModifier;
101 int modifiers;
102 WWindow *newFocused;
103 WWindow *oldFocused;
104 XEvent ev;
105
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200106
107 if (!wwin)
108 return;
109
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200110 if (next)
111 hasModifier = (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0);
112 else
113 hasModifier = (wKeyBindings[WKBD_FOCUSPREV].modifier != 0);
114
115 if (hasModifier) {
116 keymap = XGetModifierMapping(dpy);
caa86529 kojima2000-04-07 22:47:04 +0000117
9aca0d5f dan2004-10-12 01:34:32 +0000118#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200119 printf("Grabbing keyboard\n");
9aca0d5f dan2004-10-12 01:34:32 +0000120#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200121 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
122 }
123
124 scr->flags.doing_alt_tab = 1;
125
18408fff Iain Patterson2009-09-14 14:37:15 +0100126 swpanel = wInitSwitchPanel(scr, wwin, scr->current_workspace, class_only);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200127 oldFocused = wwin;
128
129 if (swpanel) {
3a0eb643
NB
Nicolas Bonifas2009-08-22 12:24:58 +0200130
131 if (wwin->flags.mapped)
132 newFocused = wSwitchPanelSelectNext(swpanel, !next);
133 else
134 newFocused = wSwitchPanelSelectFirst(swpanel, False);
135
f79379c0 Carlos R. Mafra2009-08-22 03:03:14 +0200136 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200137 } else {
138 if (wwin->frame->workspace == scr->current_workspace)
139 newFocused = wwin;
140 else
141 newFocused = NULL;
142 }
143
144 while (hasModifier && !done) {
145 int i;
146
147 WMMaskEvent(dpy, KeyPressMask | KeyReleaseMask | ExposureMask
3f7110b1 Daniel Déchelotte2009-08-19 00:50:49 +0200148 | PointerMotionMask | ButtonReleaseMask | EnterWindowMask, &ev);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200149
150 /* ignore CapsLock */
151 modifiers = ev.xkey.state & ValidModMask;
152
001bc280
CM
Carlos R. Mafra2009-08-22 02:55:45 +0200153 if (!swpanel)
154 done = True;
155
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200156 switch (ev.type) {
4a041b6f Carlos R. Mafra2009-08-20 04:07:15 +0200157
001bc280 Carlos R. Mafra2009-08-22 02:55:45 +0200158 case KeyPress:
4a041b6f Carlos R. Mafra2009-08-20 04:07:15 +0200159
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200160 if ((wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
161 && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers)
18408fff
IP
Iain Patterson2009-09-14 14:37:15 +0100162 || (wKeyBindings[WKBD_GROUPNEXT].keycode == ev.xkey.keycode
163 && wKeyBindings[WKBD_GROUPNEXT].modifier == modifiers)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200164 || ev.xkey.keycode == rightKey) {
165
4a041b6f Carlos R. Mafra2009-08-20 04:07:15 +0200166 newFocused = wSwitchPanelSelectNext(swpanel, False);
f79379c0 Carlos R. Mafra2009-08-22 03:03:14 +0200167 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
d27e43e6 Carlos R. Mafra2009-08-22 02:34:25 +0200168
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200169 } else if ((wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
170 && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers)
18408fff
IP
Iain Patterson2009-09-14 14:37:15 +0100171 || (wKeyBindings[WKBD_GROUPPREV].keycode == ev.xkey.keycode
172 && wKeyBindings[WKBD_GROUPPREV].modifier == modifiers)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200173 || ev.xkey.keycode == leftKey) {
174
4a041b6f Carlos R. Mafra2009-08-20 04:07:15 +0200175 newFocused = wSwitchPanelSelectNext(swpanel, True);
f79379c0 Carlos R. Mafra2009-08-22 03:03:14 +0200176 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
d27e43e6 Carlos R. Mafra2009-08-22 02:34:25 +0200177
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200178 } else if (ev.xkey.keycode == homeKey || ev.xkey.keycode == endKey) {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200179
4a041b6f Carlos R. Mafra2009-08-20 04:07:15 +0200180 newFocused = wSwitchPanelSelectFirst(swpanel, ev.xkey.keycode != homeKey);
f79379c0 Carlos R. Mafra2009-08-22 03:03:14 +0200181 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
d27e43e6 Carlos R. Mafra2009-08-22 02:34:25 +0200182
f9bb2a42 Carlos R. Mafra2009-08-20 16:00:26 +0200183 } else if (ev.xkey.keycode == escapeKey) {
d27e43e6 Carlos R. Mafra2009-08-22 02:34:25 +0200184
f9bb2a42
CM
Carlos R. Mafra2009-08-20 16:00:26 +0200185 /* Focus the first window of the swpanel, despite the 'False' */
186 newFocused = wSwitchPanelSelectFirst(swpanel, False);
f79379c0 Carlos R. Mafra2009-08-22 03:03:14 +0200187 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, True);
f9bb2a42
CM
Carlos R. Mafra2009-08-20 16:00:26 +0200188 esc_cancel = True;
189 done = True;
d27e43e6 Carlos R. Mafra2009-08-22 02:34:25 +0200190
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200191 } else if (ev.xkey.keycode != shiftLKey && ev.xkey.keycode != shiftRKey) {
4a041b6f Carlos R. Mafra2009-08-20 04:07:15 +0200192
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200193 somethingElse = True;
194 done = True;
195 }
196 break;
d27e43e6 Carlos R. Mafra2009-08-22 02:34:25 +0200197
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200198 case KeyRelease:
4a041b6f Carlos R. Mafra2009-08-20 04:07:15 +0200199
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200200 for (i = 0; i < 8 * keymap->max_keypermod; i++) {
d27e43e6 Carlos R. Mafra2009-08-22 02:34:25 +0200201
01dddf00
AV
Alexey Voinov2008-05-14 14:04:40 +0400202 int mask = 1 << (i / keymap->max_keypermod);
203
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200204 if (keymap->modifiermap[i] == ev.xkey.keycode &&
01dddf00 Alexey Voinov2008-05-14 14:04:40 +0400205 ((wKeyBindings[WKBD_FOCUSNEXT].modifier & mask)
18408fff
IP
Iain Patterson2009-09-14 14:37:15 +0100206 || (wKeyBindings[WKBD_FOCUSPREV].modifier & mask)
207 || (wKeyBindings[WKBD_GROUPNEXT].modifier & mask)
208 || (wKeyBindings[WKBD_GROUPPREV].modifier & mask))) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200209 done = True;
210 break;
211 }
212 }
213 break;
214
3f7110b1 Daniel Déchelotte2009-08-19 00:50:49 +0200215 case EnterNotify:
001bc280 Carlos R. Mafra2009-08-22 02:55:45 +0200216
3f7110b1
DD
Daniel Déchelotte2009-08-19 00:50:49 +0200217 /* ignore unwanted EnterNotify's */
218 break;
219
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200220 case LeaveNotify:
221 case MotionNotify:
001bc280 Carlos R. Mafra2009-08-22 02:55:45 +0200222
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200223 case ButtonRelease:
203eba89 Carlos R. Mafra2009-08-22 02:46:17 +0200224
001bc280 Carlos R. Mafra2009-08-22 02:55:45 +0200225 newFocused = wSwitchPanelHandleEvent(swpanel, &ev);
f79379c0 Carlos R. Mafra2009-08-22 03:03:14 +0200226 oldFocused = change_focus_and_raise(newFocused, oldFocused, swpanel, scr, False);
001bc280
CM
Carlos R. Mafra2009-08-22 02:55:45 +0200227
228 if (ev.type == ButtonRelease)
229 done = True;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200230 break;
231
232 default:
233 WMHandleEvent(&ev);
234 break;
235 }
236 }
237 if (keymap)
238 XFreeModifiermap(keymap);
239
240 if (hasModifier) {
4a041b6f Carlos R. Mafra2009-08-20 04:07:15 +0200241
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200242 XUngrabKeyboard(dpy, CurrentTime);
243 }
f6fb9fbb kojima2004-10-19 01:29:34 +0000244
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200245 if (swpanel)
246 wSwitchPanelDestroy(swpanel);
79864ba9 kojima2004-10-27 03:09:19 +0000247
f9bb2a42 Carlos R. Mafra2009-08-20 16:00:26 +0200248 if (newFocused && !esc_cancel) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200249 wRaiseFrame(newFocused->frame->core);
250 CommitStacking(scr);
251 if (!newFocused->flags.mapped)
252 wMakeWindowVisible(newFocused);
253 wSetFocusTo(scr, newFocused);
254 }
caa86529 kojima2000-04-07 22:47:04 +0000255
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200256 scr->flags.doing_alt_tab = 0;
a10214a5 kojima2002-11-28 22:04:07 +0000257
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200258 if (somethingElse)
259 WMHandleEvent(&ev);
caa86529 kojima2000-04-07 22:47:04 +0000260}