File: Target.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (352 lines) | stat: -rw-r--r-- 7,299 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

/*
** fvwmlib_get_target_window and fvwmlib_keyboard_shortcuts - handle window
** selection from modules and fvwm.
*/

#include "config.h"

#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <unistd.h>

#include <X11/keysym.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>

#include "fvwmlib.h"
#include "Grab.h"
#include "Target.h"

void fvwmlib_keyboard_shortcuts(
	Display *dpy, int screen, XEvent *Event, int x_move_size,
	int y_move_size, int *x_defect, int *y_defect, int ReturnEvent)
{
	int x;
	int y;
	int x_root;
	int y_root;
	int x_move;
	int y_move;
	KeySym keysym;
	Window JunkRoot;
	unsigned int JunkMask;

	if (y_move_size < DEFAULT_KDB_SHORTCUT_MOVE_DISTANCE)
	{
		y_move_size = DEFAULT_KDB_SHORTCUT_MOVE_DISTANCE;
	}
	if (x_move_size < DEFAULT_KDB_SHORTCUT_MOVE_DISTANCE)
	{
		x_move_size = DEFAULT_KDB_SHORTCUT_MOVE_DISTANCE;
	}
	if (Event->xkey.state & ControlMask)
	{
		x_move_size = y_move_size = KDB_SHORTCUT_MOVE_DISTANCE_SMALL;
	}
	if (Event->xkey.state & ShiftMask)
	{
		x_move_size = y_move_size = KDB_SHORTCUT_MOVE_DISTANCE_BIG;
	}

	keysym = XLookupKeysym(&Event->xkey,0);

	x_move = 0;
	y_move = 0;
	switch(keysym)
	{
	case XK_Up:
	case XK_KP_8:
	case XK_k:
	case XK_p:
		y_move = -y_move_size;
		break;
	case XK_Down:
	case XK_KP_2:
	case XK_n:
	case XK_j:
		y_move = y_move_size;
		break;
	case XK_Left:
	case XK_KP_4:
	case XK_b:
	case XK_h:
		x_move = -x_move_size;
		break;
	case XK_Right:
	case XK_KP_6:
	case XK_f:
	case XK_l:
		x_move = x_move_size;
		break;
	case XK_KP_1:
		x_move = -x_move_size;
		y_move = y_move_size;
		break;
	case XK_KP_3:
		x_move = x_move_size;
		y_move = y_move_size;
		break;
	case XK_KP_7:
		x_move = -x_move_size;
		y_move = -y_move_size;
		break;
	case XK_KP_9:
		x_move = x_move_size;
		y_move = -y_move_size;
		break;
	case XK_Return:
	case XK_KP_Enter:
	case XK_space:
		/* beat up the event */
		Event->type = ReturnEvent;
		break;
	case XK_Escape:
		/* simple code to bag out of move - CKH */
		/* return keypress event instead */
		Event->type = KeyPress;
		Event->xkey.keycode = XKeysymToKeycode(
			Event->xkey.display,keysym);
		break;
	default:
		break;
	}
	if (x_move || y_move)
	{
		int x_def_new = 0;
		int y_def_new = 0;

		if (FQueryPointer(
			    dpy, RootWindow(dpy, screen), &JunkRoot,
			    &Event->xany.window, &x_root, &y_root, &x, &y,
			    &JunkMask) == False)
		{
			/* pointer is on a different screen - do nothing */
			return;
		}
		if (x + x_move < 0)
		{
			x_def_new = x + x_move;
			x_move = -x;
		}
		else if (x + x_move >= DisplayWidth(dpy, DefaultScreen(dpy)))
		{
			x_def_new = x + x_move - DisplayWidth(
				dpy, DefaultScreen(dpy));
			x_move = DisplayWidth(dpy, DefaultScreen(dpy)) - x - 1;
		}
		if (y + y_move < 0)
		{
			y_def_new = y + y_move;
			y_move = -y;
		}
		else if (y + y_move >= DisplayHeight(dpy, DefaultScreen(dpy)))
		{
			y_def_new = y + y_move - DisplayHeight(
				dpy, DefaultScreen(dpy));
			y_move = DisplayHeight(
				dpy, DefaultScreen(dpy)) - y - 1;
		}
		if (x_defect)
		{
			int diff = 0;

			*x_defect += x_def_new;
			if (*x_defect > 0 && x_move < 0)
			{
				diff = min(*x_defect, -x_move);
			}
			else if (*x_defect < 0 && x_move > 0)
			{
				diff = max(*x_defect, -x_move);
			}
			*x_defect -= diff;
			x_move += diff;
		}
		if (y_defect)
		{
			int diff = 0;

			*y_defect += y_def_new;
			if (*y_defect > 0 && y_move < 0)
			{
				diff = min(*y_defect, -y_move);
			}
			else if (*y_defect < 0 && y_move > 0)
			{
				diff = max(*y_defect, -y_move);
			}
			*y_defect -= diff;
			y_move += diff;
		}
		if (x_move || y_move)
		{
			FWarpPointer(
				dpy, None, RootWindow(dpy, screen), 0, 0, 0, 0,
				x_root + x_move, y_root + y_move);
		}
		/* beat up the event */
		Event->type = MotionNotify;
		Event->xkey.x += x_move;
		Event->xkey.y += y_move;
		Event->xkey.x_root += x_move;
		Event->xkey.y_root += y_move;
	}
}

void fvwmlib_get_target_window(
	Display *dpy, int screen, char *MyName, Window *app_win,
	Bool return_subwindow)
{
	XEvent eventp;
	int val = -10,trials;
	Bool finished = False;
	Bool canceled = False;
	Window Root = RootWindow(dpy, screen);
	int is_key_pressed = 0;
	int is_button_pressed = 0;
	KeySym keysym;

	trials = 0;
	while((trials <10)&&(val != GrabSuccess))
	{
		val=XGrabPointer(dpy, Root, True,
				 ButtonPressMask | ButtonReleaseMask,
				 GrabModeAsync, GrabModeAsync, Root,
				 XCreateFontCursor(dpy,XC_crosshair),
				 CurrentTime);
		switch (val)
		{
		case GrabInvalidTime:
		case GrabNotViewable:
			/* give up */
			trials += 100000;
			break;
		case GrabSuccess:
			break;
		case AlreadyGrabbed:
		case GrabFrozen:
		default:
			usleep(10000);
			trials++;
			break;
		}
	}
	if(val != GrabSuccess)
	{
		fprintf(stderr,"%s: Couldn't grab the cursor!\n",MyName);
		exit(1);
	}
	MyXGrabKeyboard(dpy);

	while (!finished && !canceled)
	{
		FMaskEvent(dpy, ButtonPressMask | ButtonReleaseMask |
			   KeyPressMask | KeyReleaseMask, &eventp);
		switch (eventp.type)
		{
		case KeyPress:
			is_key_pressed++;
			break;
		case KeyRelease:
			keysym = XLookupKeysym(&eventp.xkey,0);
			if( !is_key_pressed ) break;
			switch (keysym)
			{
			case XK_Escape:
				canceled = True;
				break;
			case XK_space:
			case XK_Return:
			case XK_KP_Enter:
				finished = True;
				break;
			default:
				fvwmlib_keyboard_shortcuts(
					dpy, screen, &eventp, 0, 0, NULL,
					NULL, 0);
				break;
			}
			break;
		case ButtonPress:
			is_button_pressed++;
			break;
		case ButtonRelease:
			if( is_button_pressed ) finished = True;
			break;
		}
	}

	MyXUngrabKeyboard(dpy);
	XUngrabPointer(dpy, CurrentTime);
	XSync(dpy,0);
	if (canceled)
	{
		*app_win = None;
		return;
	}
	*app_win = eventp.xany.window;
	if(return_subwindow && eventp.xbutton.subwindow != None)
		*app_win = eventp.xbutton.subwindow;

	return;
}

Window fvwmlib_client_window(Display *dpy, Window input)
{
	Atom _XA_WM_STATE;
	unsigned int nchildren;
	Window root, parent, *children,target;
	unsigned long nitems, bytesafter;
	unsigned char *prop;
	Atom atype;
	int aformat;
	int i;

	_XA_WM_STATE = XInternAtom (dpy, "WM_STATE", False);

	if (
		XGetWindowProperty(
			dpy, input, _XA_WM_STATE , 0L, 3L , False,
			_XA_WM_STATE, &atype, &aformat, &nitems, &bytesafter,
			&prop) == Success)
	{
		if(prop != NULL)
		{
			XFree(prop);
			return input;
		}
	}

	if (!XQueryTree(dpy, input, &root, &parent, &children, &nchildren))
		return None;

	for (i = 0; i < nchildren; i++)
	{
		target = fvwmlib_client_window(dpy, children[i]);
		if(target != None)
		{
			XFree((char *)children);
			return target;
		}
	}
	XFree((char *)children);

	return None;
}