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>
29 #include "WindowMaker.h"
42 extern WPreferences wPreferences
;
44 extern WShortKey wKeyBindings
[WKBD_LAST
];
52 nextToFocusAfter(WWindow
*wwin
)
54 WWindow
*tmp
= wwin
->prev
;
57 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
64 /* start over from the beginning of the list */
68 while (tmp
&& tmp
!= wwin
) {
69 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
80 nextToFocusBefore(WWindow
*wwin
)
82 WWindow
*tmp
= wwin
->next
;
85 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
91 /* start over from the beginning of the list */
96 while (tmp
&& tmp
!= wwin
) {
97 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
111 nextFocusWindow(WWindow
*wwin
)
113 WWindow
*tmp
, *closest
, *min
;
123 if (wWindowCanReceiveFocus(tmp
)
124 && (!WFLAGP(tmp
, skip_window_list
)|| tmp
->flags
.internal_window
)) {
125 if (min
->client_win
> tmp
->client_win
)
127 if (tmp
->client_win
> wwin
->client_win
129 || (tmp
->client_win
- wwin
->client_win
) < d
)) {
131 d
= tmp
->client_win
- wwin
->client_win
;
136 if (!closest
||closest
==wwin
)
143 prevFocusWindow(WWindow
*wwin
)
145 WWindow
*tmp
, *closest
, *max
;
155 if (wWindowCanReceiveFocus(tmp
) &&
156 (!WFLAGP(tmp
, skip_window_list
) || tmp
->flags
.internal_window
)) {
157 if (max
->client_win
< tmp
->client_win
)
159 if (tmp
->client_win
< wwin
->client_win
161 || (wwin
->client_win
- tmp
->client_win
) < d
)) {
163 d
= wwin
->client_win
- tmp
->client_win
;
168 if (!closest
||closest
==wwin
)
177 StartWindozeCycle(WWindow
*wwin
, XEvent
*event
, Bool next
)
179 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
181 Bool openedSwitchMenu
= False
;
185 XModifierKeymap
*keymap
= NULL
;
187 Bool somethingElse
= False
;
194 hasModifier
= (wKeyBindings
[WKBD_FOCUSNEXT
].modifier
!= 0);
196 hasModifier
= (wKeyBindings
[WKBD_FOCUSPREV
].modifier
!= 0);
199 keymap
= XGetModifierMapping(dpy
);
202 XGrabKeyboard(dpy
, scr
->root_win
, False
, GrabModeAsync
, GrabModeAsync
,
207 if (wPreferences
.windows_cycling
)
208 newFocused
= nextToFocusAfter(wwin
);
210 newFocused
= nextFocusWindow(wwin
);
212 if (wPreferences
.windows_cycling
)
213 newFocused
= nextToFocusBefore(wwin
);
215 newFocused
= prevFocusWindow(wwin
);
218 scr
->flags
.doing_alt_tab
= 1;
221 if (wPreferences
.circ_raise
)
222 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
223 wWindowFocus(newFocused
, scr
->focused_window
);
224 oldFocused
= newFocused
;
232 if (wPreferences
.popup_switchmenu
&&
233 (!scr
->switch_menu
|| !scr
->switch_menu
->flags
.mapped
)) {
235 OpenSwitchMenu(scr
, scr
->scr_width
/2, scr
->scr_height
/2, False
);
236 openedSwitchMenu
= True
;
240 WMMaskEvent(dpy
,KeyPressMask
|KeyReleaseMask
|ExposureMask
, &ev
);
242 if (ev
.type
!= KeyRelease
&& ev
.type
!= KeyPress
) {
246 /* ignore CapsLock */
247 modifiers
= ev
.xkey
.state
& ValidModMask
;
249 if (ev
.type
== KeyPress
) {
250 if (wKeyBindings
[WKBD_FOCUSNEXT
].keycode
== ev
.xkey
.keycode
251 && wKeyBindings
[WKBD_FOCUSNEXT
].modifier
== modifiers
) {
253 newFocused
= nextToFocusAfter(newFocused
);
254 wWindowFocus(newFocused
, oldFocused
);
255 oldFocused
= newFocused
;
257 if (wPreferences
.circ_raise
) {
260 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
263 } else if (wKeyBindings
[WKBD_FOCUSPREV
].keycode
== ev
.xkey
.keycode
264 && wKeyBindings
[WKBD_FOCUSPREV
].modifier
== modifiers
) {
266 newFocused
= nextToFocusBefore(newFocused
);
267 wWindowFocus(newFocused
, oldFocused
);
268 oldFocused
= newFocused
;
270 if (wPreferences
.circ_raise
) {
273 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
277 somethingElse
= True
;
280 } else if (ev
.type
== KeyRelease
) {
283 for (i
= 0; i
< 8 * keymap
->max_keypermod
; i
++) {
284 if (keymap
->modifiermap
[i
] == ev
.xkey
.keycode
&&
285 wKeyBindings
[WKBD_FOCUSNEXT
].modifier
286 & 1<<(i
/keymap
->max_keypermod
)) {
294 XFreeModifiermap(keymap
);
297 XUngrabKeyboard(dpy
, CurrentTime
);
299 wSetFocusTo(scr
, newFocused
);
301 if (wPreferences
.circ_raise
) {
302 wRaiseFrame(newFocused
->frame
->core
);
306 scr
->flags
.doing_alt_tab
= 0;
307 if (openedSwitchMenu
) {
309 * place window in center of current head
311 WMPoint center
= wGetPointToCenterRectInHead(scr
,
312 wGetHeadForPointerLocation(scr
),
314 OpenSwitchMenu(scr
, center
.x
, center
.y
, False
);