1 /* cycling.c- window cycling
3 * Window Maker window manager
5 * Copyright (c) 2000-2002 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"
41 extern WPreferences wPreferences
;
43 extern WShortKey wKeyBindings
[WKBD_LAST
];
51 nextToFocusAfter(WWindow
*wwin
)
53 WWindow
*tmp
= wwin
->prev
;
56 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
63 /* start over from the beginning of the list */
67 while (tmp
&& tmp
!= wwin
) {
68 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
79 nextToFocusBefore(WWindow
*wwin
)
81 WWindow
*tmp
= wwin
->next
;
84 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
90 /* start over from the beginning of the list */
95 while (tmp
&& tmp
!= wwin
) {
96 if (wWindowCanReceiveFocus(tmp
) && !WFLAGP(tmp
, skip_window_list
)) {
110 nextFocusWindow(WWindow
*wwin
)
112 WWindow
*tmp
, *closest
, *min
;
122 if (wWindowCanReceiveFocus(tmp
)
123 && (!WFLAGP(tmp
, skip_window_list
)|| tmp
->flags
.internal_window
)) {
124 if (min
->client_win
> tmp
->client_win
)
126 if (tmp
->client_win
> wwin
->client_win
128 || (tmp
->client_win
- wwin
->client_win
) < d
)) {
130 d
= tmp
->client_win
- wwin
->client_win
;
135 if (!closest
||closest
==wwin
)
142 prevFocusWindow(WWindow
*wwin
)
144 WWindow
*tmp
, *closest
, *max
;
154 if (wWindowCanReceiveFocus(tmp
) &&
155 (!WFLAGP(tmp
, skip_window_list
) || tmp
->flags
.internal_window
)) {
156 if (max
->client_win
< tmp
->client_win
)
158 if (tmp
->client_win
< wwin
->client_win
160 || (wwin
->client_win
- tmp
->client_win
) < d
)) {
162 d
= wwin
->client_win
- tmp
->client_win
;
167 if (!closest
||closest
==wwin
)
176 StartWindozeCycle(WWindow
*wwin
, XEvent
*event
, Bool next
)
178 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
180 Bool openedSwitchMenu
= False
;
184 XModifierKeymap
*keymap
= NULL
;
186 Bool somethingElse
= False
;
193 hasModifier
= (wKeyBindings
[WKBD_FOCUSNEXT
].modifier
!= 0);
195 hasModifier
= (wKeyBindings
[WKBD_FOCUSPREV
].modifier
!= 0);
198 keymap
= XGetModifierMapping(dpy
);
201 XGrabKeyboard(dpy
, scr
->root_win
, False
, GrabModeAsync
, GrabModeAsync
,
206 if (wPreferences
.windows_cycling
)
207 newFocused
= nextToFocusAfter(wwin
);
209 newFocused
= nextFocusWindow(wwin
);
211 if (wPreferences
.windows_cycling
)
212 newFocused
= nextToFocusBefore(wwin
);
214 newFocused
= prevFocusWindow(wwin
);
217 scr
->flags
.doing_alt_tab
= 1;
220 if (wPreferences
.circ_raise
)
221 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
222 wWindowFocus(newFocused
, scr
->focused_window
);
223 oldFocused
= newFocused
;
231 if (wPreferences
.popup_switchmenu
&&
232 (!scr
->switch_menu
|| !scr
->switch_menu
->flags
.mapped
)) {
234 OpenSwitchMenu(scr
, scr
->scr_width
/2, scr
->scr_height
/2, False
);
235 openedSwitchMenu
= True
;
239 WMMaskEvent(dpy
,KeyPressMask
|KeyReleaseMask
|ExposureMask
, &ev
);
241 if (ev
.type
!= KeyRelease
&& ev
.type
!= KeyPress
) {
245 /* ignore CapsLock */
246 modifiers
= ev
.xkey
.state
& ValidModMask
;
248 if (ev
.type
== KeyPress
) {
249 if (wKeyBindings
[WKBD_FOCUSNEXT
].keycode
== ev
.xkey
.keycode
250 && wKeyBindings
[WKBD_FOCUSNEXT
].modifier
== modifiers
) {
252 newFocused
= nextToFocusAfter(newFocused
);
253 wWindowFocus(newFocused
, oldFocused
);
254 oldFocused
= newFocused
;
256 if (wPreferences
.circ_raise
) {
259 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
262 } else if (wKeyBindings
[WKBD_FOCUSPREV
].keycode
== ev
.xkey
.keycode
263 && wKeyBindings
[WKBD_FOCUSPREV
].modifier
== modifiers
) {
265 newFocused
= nextToFocusBefore(newFocused
);
266 wWindowFocus(newFocused
, oldFocused
);
267 oldFocused
= newFocused
;
269 if (wPreferences
.circ_raise
) {
272 XRaiseWindow(dpy
, newFocused
->frame
->core
->window
);
276 somethingElse
= True
;
279 } else if (ev
.type
== KeyRelease
) {
282 for (i
= 0; i
< 8 * keymap
->max_keypermod
; i
++) {
283 if (keymap
->modifiermap
[i
] == ev
.xkey
.keycode
&&
284 wKeyBindings
[WKBD_FOCUSNEXT
].modifier
285 & 1<<(i
/keymap
->max_keypermod
)) {
293 XFreeModifiermap(keymap
);
296 XUngrabKeyboard(dpy
, CurrentTime
);
298 wSetFocusTo(scr
, newFocused
);
300 if (wPreferences
.circ_raise
) {
301 wRaiseFrame(newFocused
->frame
->core
);
305 scr
->flags
.doing_alt_tab
= 0;
306 if (openedSwitchMenu
)
307 OpenSwitchMenu(scr
, scr
->scr_width
/2, scr
->scr_height
/2, False
);