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,
26 #include <X11/Xutil.h>
27 #include <X11/keysym.h>
31 #include "WindowMaker.h"
42 #include "switchpanel.h"
45 extern WPreferences wPreferences
;
47 extern WShortKey wKeyBindings
[WKBD_LAST
];
55 nextToFocusAfter(WWindow
*wwin
)
57 WWindow
*tmp
= wwin
->prev
;
60 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
67 /* start over from the beginning of the list */
71 while (tmp
&& tmp
!= wwin
) {
72 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
83 nextToFocusBefore(WWindow
*wwin
)
85 WWindow
*tmp
= wwin
->next
;
88 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
94 /* start over from the beginning of the list */
99 while (tmp
&& tmp
!= wwin
) {
100 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
114 nextFocusWindow(WWindow
*wwin
)
116 WWindow
*tmp
, *closest
, *min
;
126 if (wWindowCanReceiveFocus(tmp
)
127 && (!WFLAGP(tmp
, skip_window_list
)|| tmp
->flags
.internal_window
)) {
128 if (min
->client_win
> tmp
->client_win
)
130 if (tmp
->client_win
> wwin
->client_win
132 || (tmp
->client_win
- wwin
->client_win
) < d
)) {
134 d
= tmp
->client_win
- wwin
->client_win
;
139 if (!closest
||closest
==wwin
)
146 prevFocusWindow(WWindow
*wwin
)
148 WWindow
*tmp
, *closest
, *max
;
158 if (wWindowCanReceiveFocus(tmp
) &&
159 (!WFLAGP(tmp
, skip_window_list
) || tmp
->flags
.internal_window
)) {
160 if (max
->client_win
< tmp
->client_win
)
162 if (tmp
->client_win
< wwin
->client_win
164 || (wwin
->client_win
- tmp
->client_win
) < d
)) {
166 d
= wwin
->client_win
- tmp
->client_win
;
171 if (!closest
||closest
==wwin
)
175 #endif /* !MOX_CYCLING */
179 StartWindozeCycle(WWindow
*wwin
, XEvent
*event
, Bool next
)
181 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
186 XModifierKeymap
*keymap
= NULL
;
188 Bool somethingElse
= False
;
191 WSwitchPanel
*swpanel
= NULL
;
193 KeyCode leftKey
, rightKey
;
198 leftKey
= XKeysymToKeycode(dpy
, XK_Left
);
199 rightKey
= XKeysymToKeycode(dpy
, XK_Right
);
202 hasModifier
= (wKeyBindings
[WKBD_FOCUSNEXT
].modifier
!= 0);
204 hasModifier
= (wKeyBindings
[WKBD_FOCUSPREV
].modifier
!= 0);
207 keymap
= XGetModifierMapping(dpy
);
210 printf("Grabbing keyboard\n");
212 XGrabKeyboard(dpy
, scr
->root_win
, False
, GrabModeAsync
, GrabModeAsync
,
216 scr
->flags
.doing_alt_tab
= 1;
219 swpanel
= wInitSwitchPanel(scr
, wwin
, scr
->current_workspace
);
223 newFocused
= wSwitchPanelSelectNext(swpanel
, !next
);
225 wWindowFocus(newFocused
, oldFocused
);
226 oldFocused
= newFocused
;
231 #else /* !MOX_CYCLING */
233 if (wPreferences
.windows_cycling
)
234 newFocused
= nextToFocusAfter(wwin
);
236 newFocused
= nextFocusWindow(wwin
);
238 if (wPreferences
.windows_cycling
)
239 newFocused
= nextToFocusBefore(wwin
);
241 newFocused
= prevFocusWindow(wwin
);
244 if (wPreferences
.circ_raise
)
245 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
246 wWindowFocus(newFocused
, scr
->focused_window
);
247 oldFocused
= newFocused
;
248 #endif /* !MOX_CYCLING */
250 while (hasModifier
&& !done
) {
251 WMMaskEvent(dpy
, KeyPressMask
|KeyReleaseMask
|ExposureMask
|PointerMotionMask
, &ev
);
253 if (ev
.type
!= KeyRelease
&& ev
.type
!= KeyPress
) {
257 /* ignore CapsLock */
258 modifiers
= ev
.xkey
.state
& ValidModMask
;
260 if (ev
.type
== KeyPress
) {
262 printf("Got key press\n");
264 if ((wKeyBindings
[WKBD_FOCUSNEXT
].keycode
== ev
.xkey
.keycode
265 && wKeyBindings
[WKBD_FOCUSNEXT
].modifier
== modifiers
)
266 || ev
.xkey
.keycode
== rightKey
) {
270 newFocused
= wSwitchPanelSelectNext(swpanel
, False
);
272 wWindowFocus(newFocused
, oldFocused
);
273 oldFocused
= newFocused
;
276 #else /* !MOX_CYCLING */
277 newFocused
= nextToFocusAfter(newFocused
);
278 wWindowFocus(newFocused
, oldFocused
);
279 oldFocused
= newFocused
;
281 if (wPreferences
.circ_raise
) {
284 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
286 #endif /* !MOX_CYCLING */
287 } else if ((wKeyBindings
[WKBD_FOCUSPREV
].keycode
== ev
.xkey
.keycode
288 && wKeyBindings
[WKBD_FOCUSPREV
].modifier
== modifiers
)
289 || ev
.xkey
.keycode
== leftKey
) {
293 newFocused
= wSwitchPanelSelectNext(swpanel
, True
);
295 wWindowFocus(newFocused
, oldFocused
);
296 oldFocused
= newFocused
;
299 #else /* !MOX_CYCLING */
300 newFocused
= nextToFocusBefore(newFocused
);
301 wWindowFocus(newFocused
, oldFocused
);
302 oldFocused
= newFocused
;
304 if (wPreferences
.circ_raise
) {
307 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
309 #endif /* !MOX_CYCLING */
310 } else if (ev
.type
== MotionNotify
) {
313 tmp
= wSwitchPanelHandleEvent(swpanel
, &ev
);
316 wWindowFocus(newFocused
, oldFocused
);
317 oldFocused
= newFocused
;
322 printf("Got something else\n");
324 somethingElse
= True
;
327 } else if (ev
.type
== KeyRelease
) {
331 printf("Got key release\n");
333 for (i
= 0; i
< 8 * keymap
->max_keypermod
; i
++) {
334 if (keymap
->modifiermap
[i
] == ev
.xkey
.keycode
&&
335 wKeyBindings
[WKBD_FOCUSNEXT
].modifier
336 & 1<<(i
/keymap
->max_keypermod
)) {
344 XFreeModifiermap(keymap
);
348 printf("Ungrabbing keyboard\n");
350 XUngrabKeyboard(dpy
, CurrentTime
);
354 wSetFocusTo(scr
, newFocused
);
355 wMakeWindowVisible(newFocused
);
360 wSwitchPanelDestroy(swpanel
);
363 if (wPreferences
.circ_raise
&& newFocused
) {
364 wRaiseFrame(newFocused
->frame
->core
);
368 scr
->flags
.doing_alt_tab
= 0;