2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ** fvwmlib_get_target_window and fvwmlib_keyboard_shortcuts - handle window
19 ** selection from modules and fvwm.
29 #include <X11/keysym.h>
32 #include <X11/cursorfont.h>
38 void fvwmlib_keyboard_shortcuts(
39 Display
*dpy
, int screen
, XEvent
*Event
, int x_move_size
,
40 int y_move_size
, int *x_defect
, int *y_defect
, int ReturnEvent
)
50 unsigned int JunkMask
;
52 if (y_move_size
< DEFAULT_KDB_SHORTCUT_MOVE_DISTANCE
)
54 y_move_size
= DEFAULT_KDB_SHORTCUT_MOVE_DISTANCE
;
56 if (x_move_size
< DEFAULT_KDB_SHORTCUT_MOVE_DISTANCE
)
58 x_move_size
= DEFAULT_KDB_SHORTCUT_MOVE_DISTANCE
;
60 if (Event
->xkey
.state
& ControlMask
)
62 x_move_size
= y_move_size
= KDB_SHORTCUT_MOVE_DISTANCE_SMALL
;
64 if (Event
->xkey
.state
& ShiftMask
)
66 x_move_size
= y_move_size
= KDB_SHORTCUT_MOVE_DISTANCE_BIG
;
69 keysym
= XLookupKeysym(&Event
->xkey
,0);
79 y_move
= -y_move_size
;
91 x_move
= -x_move_size
;
100 x_move
= -x_move_size
;
101 y_move
= y_move_size
;
104 x_move
= x_move_size
;
105 y_move
= y_move_size
;
108 x_move
= -x_move_size
;
109 y_move
= -y_move_size
;
112 x_move
= x_move_size
;
113 y_move
= -y_move_size
;
118 /* beat up the event */
119 Event
->type
= ReturnEvent
;
122 /* simple code to bag out of move - CKH */
123 /* return keypress event instead */
124 Event
->type
= KeyPress
;
125 Event
->xkey
.keycode
= XKeysymToKeycode(
126 Event
->xkey
.display
,keysym
);
131 if (x_move
|| y_move
)
137 dpy
, RootWindow(dpy
, screen
), &JunkRoot
,
138 &Event
->xany
.window
, &x_root
, &y_root
, &x
, &y
,
141 /* pointer is on a different screen - do nothing */
146 x_def_new
= x
+ x_move
;
149 else if (x
+ x_move
>= DisplayWidth(dpy
, DefaultScreen(dpy
)))
151 x_def_new
= x
+ x_move
- DisplayWidth(
152 dpy
, DefaultScreen(dpy
));
153 x_move
= DisplayWidth(dpy
, DefaultScreen(dpy
)) - x
- 1;
157 y_def_new
= y
+ y_move
;
160 else if (y
+ y_move
>= DisplayHeight(dpy
, DefaultScreen(dpy
)))
162 y_def_new
= y
+ y_move
- DisplayHeight(
163 dpy
, DefaultScreen(dpy
));
164 y_move
= DisplayHeight(
165 dpy
, DefaultScreen(dpy
)) - y
- 1;
171 *x_defect
+= x_def_new
;
172 if (*x_defect
> 0 && x_move
< 0)
174 diff
= min(*x_defect
, -x_move
);
176 else if (*x_defect
< 0 && x_move
> 0)
178 diff
= max(*x_defect
, -x_move
);
187 *y_defect
+= y_def_new
;
188 if (*y_defect
> 0 && y_move
< 0)
190 diff
= min(*y_defect
, -y_move
);
192 else if (*y_defect
< 0 && y_move
> 0)
194 diff
= max(*y_defect
, -y_move
);
199 if (x_move
|| y_move
)
202 dpy
, None
, RootWindow(dpy
, screen
), 0, 0, 0, 0,
203 x_root
+ x_move
, y_root
+ y_move
);
205 /* beat up the event */
206 Event
->type
= MotionNotify
;
207 Event
->xkey
.x
+= x_move
;
208 Event
->xkey
.y
+= y_move
;
209 Event
->xkey
.x_root
+= x_move
;
210 Event
->xkey
.y_root
+= y_move
;
214 void fvwmlib_get_target_window(
215 Display
*dpy
, int screen
, char *MyName
, Window
*app_win
,
216 Bool return_subwindow
)
219 int val
= -10,trials
;
220 Bool finished
= False
;
221 Bool canceled
= False
;
222 Window Root
= RootWindow(dpy
, screen
);
223 int is_key_pressed
= 0;
224 int is_button_pressed
= 0;
228 while((trials
<10)&&(val
!= GrabSuccess
))
230 val
=XGrabPointer(dpy
, Root
, True
,
231 ButtonPressMask
| ButtonReleaseMask
,
232 GrabModeAsync
, GrabModeAsync
, Root
,
233 XCreateFontCursor(dpy
,XC_crosshair
),
237 case GrabInvalidTime
:
238 case GrabNotViewable
:
252 if(val
!= GrabSuccess
)
254 fprintf(stderr
,"%s: Couldn't grab the cursor!\n",MyName
);
257 MyXGrabKeyboard(dpy
);
259 while (!finished
&& !canceled
)
261 FMaskEvent(dpy
, ButtonPressMask
| ButtonReleaseMask
|
262 KeyPressMask
| KeyReleaseMask
, &eventp
);
269 keysym
= XLookupKeysym(&eventp
.xkey
,0);
270 if( !is_key_pressed
) break;
282 fvwmlib_keyboard_shortcuts(
283 dpy
, screen
, &eventp
, 0, 0, NULL
,
292 if( is_button_pressed
) finished
= True
;
297 MyXUngrabKeyboard(dpy
);
298 XUngrabPointer(dpy
, CurrentTime
);
305 *app_win
= eventp
.xany
.window
;
306 if(return_subwindow
&& eventp
.xbutton
.subwindow
!= None
)
307 *app_win
= eventp
.xbutton
.subwindow
;
312 Window
fvwmlib_client_window(Display
*dpy
, Window input
)
315 unsigned int nchildren
;
316 Window root
, parent
, *children
,target
;
317 unsigned long nitems
, bytesafter
;
323 _XA_WM_STATE
= XInternAtom (dpy
, "WM_STATE", False
);
327 dpy
, input
, _XA_WM_STATE
, 0L, 3L , False
,
328 _XA_WM_STATE
, &atype
, &aformat
, &nitems
, &bytesafter
,
338 if (!XQueryTree(dpy
, input
, &root
, &parent
, &children
, &nchildren
))
341 for (i
= 0; i
< nchildren
; i
++)
343 target
= fvwmlib_client_window(dpy
, children
[i
]);
346 XFree((char *)children
);
350 XFree((char *)children
);