File: misc.c

package info (click to toggle)
fvwm 1%3A2.6.5.ds-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,820 kB
  • ctags: 16,761
  • sloc: ansic: 166,014; xml: 17,707; perl: 12,361; sh: 5,115; makefile: 1,354; yacc: 688; lex: 188; sed: 11
file content (602 lines) | stat: -rw-r--r-- 13,607 bytes parent folder | download | duplicates (2)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/* -*-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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * This module is all original code
 * by Rob Nation
 * Copyright 1993, Robert Nation
 *     You may use this code for any purpose, as long as the original
 *     copyright remains in the source code and all documentation
 */

/* ---------------------------- included header files ---------------------- */

#include "config.h"

#include <stdio.h>
#include <stdarg.h>
#include <X11/Xatom.h>

#include "libs/ftime.h"
#ifdef FVWM_DEBUG_TIME
#include <sys/times.h>
#endif
#include "libs/Parse.h"
#include "libs/Target.h"
#include "fvwm.h"
#include "externs.h"
#include "execcontext.h"
#include "cursor.h"
#include "misc.h"
#include "screen.h"
#include "module_interface.h"
#include "events.h"
#include "eventmask.h"

/* ---------------------------- local definitions -------------------------- */

/* ---------------------------- local macros ------------------------------- */

#define GRAB_EVMASK (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | \
	PointerMotionMask | EnterWindowMask | LeaveWindowMask)

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- included code files ------------------------ */

/* ---------------------------- local types -------------------------------- */

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

static int grab_count[GRAB_MAXVAL] = { 1, 1, 0, 0, 0, 0, 0 };

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

/*
 * Change the appearance of the grabbed cursor.
 */
static void change_grab_cursor(int cursor)
{
	if (cursor != None)
	{
		XChangeActivePointerGrab(
			dpy, GRAB_EVMASK, Scr.FvwmCursors[cursor], CurrentTime);
	}

	return;
}

/* ---------------------------- interface functions ------------------------ */

int GetTwoArguments(
	char *action, int *val1, int *val2, int *val1_unit, int *val2_unit)
{
	*val1_unit = Scr.MyDisplayWidth;
	*val2_unit = Scr.MyDisplayHeight;

	return GetTwoPercentArguments(action, val1, val2, val1_unit, val2_unit);
}

/*
 * Grab the pointer.
 * grab_context: GRAB_NORMAL, GRAB_BUSY, GRAB_MENU, GRAB_BUSYMENU,
 * GRAB_PASSIVE.
 * GRAB_STARTUP and GRAB_NONE are used at startup but are not made
 * to be grab_context.
 * GRAB_PASSIVE does not actually grab, but only delays the following ungrab
 * until the GRAB_PASSIVE is released too.
 */
#define DEBUG_GRAB 0
#if DEBUG_GRAB
void print_grab_stats(char *text)
{
	int i;

	fprintf(stderr,"grab_stats (%s):", text);
	for (i = 0; i < GRAB_MAXVAL; i++)
	{
		fprintf(stderr," %d", grab_count[i]);
	}
	fprintf(stderr," \n");

	return;
}
#endif

Bool GrabEm(int cursor, int grab_context)
{
	int i = 0;
	int val = 0;
	int rep;
	Window grab_win;
	extern Window PressedW;

	if (grab_context <= GRAB_STARTUP || grab_context >= GRAB_MAXVAL)
	{
		fvwm_msg(
			ERR, "GrabEm", "Bug: Called with illegal context %d",
			grab_context);
		return False;
	}

	if (grab_context == GRAB_PASSIVE)
	{
		grab_count[grab_context]++;
		grab_count[GRAB_ALL]++;
		return True;
	}

	if (grab_count[GRAB_ALL] > grab_count[GRAB_PASSIVE])
	{
		/* already grabbed, just change the grab cursor */
		if (grab_context == GRAB_FREEZE_CURSOR)
		{
			if (XGrabPointer(
				    dpy, (PressedW != None) ?
				    PressedW : Scr.NoFocusWin, True,
				    GRAB_EVMASK, GrabModeAsync, GrabModeAsync,
				    None, Scr.FvwmCursors[CRS_DEFAULT],
				    CurrentTime) != GrabSuccess)
			{
				return False;
			}
			return True;
		}
		grab_count[grab_context]++;
		grab_count[GRAB_ALL]++;
		if (grab_context != GRAB_BUSY || grab_count[GRAB_STARTUP] == 0)
		{
			change_grab_cursor(cursor);
		}
		return True;
	}

	/* move the keyboard focus prior to grabbing the pointer to
	 * eliminate the enterNotify and exitNotify events that go
	 * to the windows. But GRAB_BUSY. */
	switch (grab_context)
	{
	case GRAB_BUSY:
		if ( Scr.Hilite != NULL )
		{
			grab_win = FW_W(Scr.Hilite);
		}
		else
		{
			grab_win = Scr.Root;
		}
		/* retry to grab the busy cursor only once */
		rep = 2;
		break;
	case GRAB_PASSIVE:
		/* cannot happen */
		return False;
	case GRAB_FREEZE_CURSOR:
		grab_win = (PressedW != None) ? PressedW : Scr.NoFocusWin;
		rep = 2;
		break;
	default:
		grab_win = Scr.Root;
		rep = NUMBER_OF_GRAB_ATTEMPTS;
		break;
	}

	XFlush(dpy);
	while (i < rep &&
	       (val = XGrabPointer(
		      dpy, grab_win, True, GRAB_EVMASK, GrabModeAsync,
		      GrabModeAsync, None,
		      (grab_context == GRAB_FREEZE_CURSOR) ?
		      None : Scr.FvwmCursors[cursor], CurrentTime) !=
		GrabSuccess))
	{
		switch (val)
		{
		case GrabInvalidTime:
		case GrabNotViewable:
			/* give up */
			i += rep;
			break;
		case GrabSuccess:
			break;
		case AlreadyGrabbed:
		case GrabFrozen:
		default:
			/* If you go too fast, other windows may not get a
			 * chance to release any grab that they have. */
			i++;
			if (grab_context == GRAB_FREEZE_CURSOR)
			{
				break;
			}
			if (i < rep)
			{
				usleep(1000 * TIME_BETWEEN_GRAB_ATTEMPTS);
			}
			break;
		}
	}
	XFlush(dpy);

	/* If we fall out of the loop without grabbing the pointer, its
	 * time to give up */
	if (val != GrabSuccess)
	{
		return False;
	}
	grab_count[grab_context]++;
	grab_count[GRAB_ALL]++;
#if DEBUG_GRAB
	print_grab_stats("grabbed");
#endif
	return True;
}


/*
 *
 * UnGrab the pointer
 *
 */
Bool UngrabEm(int ungrab_context)
{
	if (ungrab_context <= GRAB_ALL || ungrab_context >= GRAB_MAXVAL)
	{
		fvwm_msg(
			ERR, "UngrabEm", "Bug: Called with illegal context %d",
			ungrab_context);
		return False;
	}

	if (grab_count[ungrab_context] == 0 || grab_count[GRAB_ALL] == 0)
	{
		/* context is not grabbed */
		return False;
	}

	XFlush(dpy);
	grab_count[ungrab_context]--;
	grab_count[GRAB_ALL]--;
	if (grab_count[GRAB_ALL] > 0)
	{
		int new_cursor = None;

		/* there are still grabs left - switch grab cursor */
		switch (ungrab_context)
		{
		case GRAB_NORMAL:
		case GRAB_BUSY:
		case GRAB_MENU:
			if (grab_count[GRAB_BUSYMENU] > 0)
			{
				new_cursor = CRS_WAIT;
			}
			else if (grab_count[GRAB_BUSY] > 0)
			{
				new_cursor = CRS_WAIT;
			}
			else if (grab_count[GRAB_MENU] > 0)
			{
				new_cursor = CRS_MENU;
			}
			else
			{
				new_cursor = None;
			}
			break;
		case GRAB_BUSYMENU:
			/* switch back from busymenu cursor to normal menu
			 * cursor */
			new_cursor = CRS_MENU;
			break;
		default:
			new_cursor = None;
			break;
		}
		if (grab_count[GRAB_ALL] > grab_count[GRAB_PASSIVE])
		{
#if DEBUG_GRAB
			print_grab_stats("-restore");
#endif
			change_grab_cursor(new_cursor);
		}
	}
	else
	{
#if DEBUG_GRAB
		print_grab_stats("-ungrab");
#endif
		XUngrabPointer(dpy, CurrentTime);
	}
	XFlush(dpy);

	return True;
}

#ifndef fvwm_msg /* Some ports (i.e. VMS) define their own version */
/*
** fvwm_msg: used to send output from fvwm to files and or stderr/stdout
**
** type -> DBG == Debug, ERR == Error, INFO == Information, WARN == Warning,
** OLD == Command or option deprecated
** id -> name of function, or other identifier
*/
static char *fvwm_msg_strings[] =
{
	"<<DEBUG>> ", "", "", "<<WARNING>> ", "<<DEPRECATED>> ", "<<ERROR>> "
};

void fvwm_msg(fvwm_msg_t type, char *id, char *msg, ...)
{
	va_list args;
	char *mfmt;
	char fvwm_id[20];
	char time_str[40] = "\0";
#ifdef FVWM_DEBUG_TIME
	clock_t time_val, time_taken;
	static clock_t start_time = 0;
	static clock_t prev_time = 0;
	struct tms not_used_tms;
	time_t mytime;
	struct tm *t_ptr;
#endif

#ifdef FVWM_DEBUG_TIME
	time(&mytime);
	t_ptr = localtime(&mytime);
	if (start_time == 0)
	{
		/* get clock ticks */
		prev_time = start_time = (unsigned int)times(&not_used_tms);
	}
	time_val = (unsigned int)times(&not_used_tms); /* get clock ticks */
	time_taken = time_val - prev_time;
	prev_time = time_val;
	sprintf(time_str, "%.2d:%.2d:%.2d%7ld ",
		t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec, time_taken);
#endif

	strcpy(fvwm_id, "fvwm");
	if (Scr.NumberOfScreens > 1)
	{
		sprintf(&fvwm_id[strlen(fvwm_id)], ".%d", (int)Scr.screen);
	}

	fprintf(stderr, "%s[%s][%s]: %s\n",
		time_str, fvwm_id, id, fvwm_msg_strings[(int)type]);

	va_start(args, msg);
	asprintf(&mfmt, "%s\n", msg);
	vfprintf(stderr, mfmt, args);
	va_end(args);
	free(mfmt);

	if (type == ERR)
	{
		/* I hate to use a fixed length but this will do for now */
		char tmp[2 * MAX_TOKEN_LENGTH];
		sprintf(tmp, "[%s][%s]: %s",
			fvwm_id, id, fvwm_msg_strings[(int)type]);
		va_start(args, msg);
		vsprintf(tmp + strlen(tmp), msg, args);
		va_end(args);
		tmp[strlen(tmp) + 1] = '\0';
		tmp[strlen(tmp)] = '\n';
		if (strlen(tmp) >= MAX_MODULE_INPUT_TEXT_LEN)
		{
			sprintf(tmp + MAX_MODULE_INPUT_TEXT_LEN - 5, "...\n");
		}
		BroadcastName(M_ERROR, 0, 0, 0, tmp);
	}
	fprintf(stderr, "\n");

} /* fvwm_msg */
#endif

void fvwm_msg_report_app(void)
{
	fprintf(
		stderr,
		"    If you are having a problem with the application, send a"
		" bug report\n"
		"    with this message included to the application owner.\n"
		"    There is no need to notify fvwm-workers@fvwm.org.\n");

	return;
}

void fvwm_msg_report_app_and_workers(void)
{
	fprintf(
		stderr,
		"    If you are having a problem with the application, send"
		" a bug report with\n"
		"    this message included to the application owner and"
		" notify\n"
		"    fvwm-workers@fvwm.org.\n");

	return;
}

/* Store the last item that was added with '+' */
void set_last_added_item(last_added_item_t type, void *item)
{
	Scr.last_added_item.type = type;
	Scr.last_added_item.item = item;

	return;
}

/* some fancy font handling stuff */
void NewFontAndColor(FlocaleFont *flf, Pixel color, Pixel backcolor)
{
	Globalgcm = GCForeground | GCBackground;
	if (flf->font)
	{
		Globalgcm |= GCFont;
		Globalgcv.font = flf->font->fid;
	}
	Globalgcv.foreground = color;
	Globalgcv.background = backcolor;
	XChangeGC(dpy,Scr.TitleGC,Globalgcm,&Globalgcv);

	return;
}


/*
 *
 * For menus, move, and resize operations, we can effect keyboard
 * shortcuts by warping the pointer.
 *
 */
void Keyboard_shortcuts(
	XEvent *ev, FvwmWindow *fw, int *x_defect, int *y_defect,
	int ReturnEvent)
{
	int x_move_size = 0;
	int y_move_size = 0;

	if (fw)
	{
		x_move_size = fw->hints.width_inc;
		y_move_size = fw->hints.height_inc;
	}
	fvwmlib_keyboard_shortcuts(
		dpy, Scr.screen, ev, x_move_size, y_move_size, x_defect,
		y_defect, ReturnEvent);

	return;
}


/*
 *
 * Check if the given FvwmWindow structure still points to a valid window.
 *
 */

Bool check_if_fvwm_window_exists(FvwmWindow *fw)
{
	FvwmWindow *t;

	for (t = Scr.FvwmRoot.next; t != NULL; t = t->next)
	{
		if (t == fw)
			return True;
	}
	return False;
}

/* rounds x down to the next multiple of m */
int truncate_to_multiple (int x, int m)
{
	return (x < 0) ? (m * (((x + 1) / m) - 1)) : (m * (x / m));
}

Bool IsRectangleOnThisPage(const rectangle *rec, int desk)
{
	return (desk == Scr.CurrentDesk &&
		rec->x + (signed int)rec->width > 0 &&
		(rec->x < 0 || rec->x < Scr.MyDisplayWidth) &&
		rec->y + (signed int)rec->height > 0 &&
		(rec->y < 0 || rec->y < Scr.MyDisplayHeight)) ?
		True : False;
}

/* returns the FvwmWindow that contains the pointer or NULL if none */
FvwmWindow *get_pointer_fvwm_window(void)
{
	int x,y;
	Window win;
	Window ancestor;
	FvwmWindow *t;

	if (FQueryPointer(
		    dpy, Scr.Root, &JunkRoot, &win, &JunkX, &JunkY,
		    &x, &y, &JunkMask) == False)
	{
		/* pointer is on a different screen */
		return NULL;
	}
	for (t = NULL ; win != Scr.Root && win != None; win = ancestor)
	{
		Window root = None;
		Window *children;
		unsigned int nchildren;

		if (XFindContext(dpy, win, FvwmContext, (caddr_t *) &t) !=
		    XCNOENT)
		{
			/* found a matching window context */
			return t;
		}
		/* get next higher ancestor window */
		children = NULL;
		if (!XQueryTree(
			    dpy, win, &root, &ancestor, &children, &nchildren))
		{
			return NULL;
		}
		if (children)
		{
			XFree(children);
		}
	}

	return t;
}


/* Returns the current X server time */
Time get_server_time(void)
{
	XEvent xev;
	XSetWindowAttributes attr;

	/* add PropChange to NoFocusWin events */
	attr.event_mask = PropertyChangeMask;
	XChangeWindowAttributes (dpy, Scr.NoFocusWin, CWEventMask, &attr);
	/* provoke an event */
	XChangeProperty(
		dpy, Scr.NoFocusWin, XA_WM_CLASS, XA_STRING, 8, PropModeAppend,
		NULL, 0);
	FWindowEvent(dpy, Scr.NoFocusWin, PropertyChangeMask, &xev);
	attr.event_mask = XEVMASK_NOFOCUSW;
	XChangeWindowAttributes(dpy, Scr.NoFocusWin, CWEventMask, &attr);

	return xev.xproperty.time;
}

void print_g(char *text, rectangle *g)
{
	fprintf(stderr,"%s: ", (text != NULL) ? text : "");
	if (g == NULL)
	{
		fprintf(stderr, "(null)\n");

		return;
	}
	fprintf(stderr,"%4d %4d %4dx%4d (%4d - %4d, %4d - %4d)\n",
		g->x, g->y, g->width, g->height,
		g->x, g->x + g->width, g->y, g->y + g->height);

	return;
}