~ctwm/ctwm/trunk

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
/*
 * Core event handling
 *
 *       Copyright 1988 by Evans & Sutherland Computer Corporation,
 *                          Salt Lake City, Utah
 *  Portions Copyright 1989 by the Massachusetts Institute of Technology
 *                        Cambridge, Massachusetts
 *
 * Copyright 1992 Claude Lecommandeur.
 */

/***********************************************************************
 *
 * $XConsortium: events.c,v 1.182 91/07/17 13:59:14 dave Exp $
 *
 * twm event handling
 *
 * 17-Nov-87 Thomas E. LaStrange                File created
 *
 * Do the necessary modification to be integrated in ctwm.
 * Can no longer be used for the standard twm.
 *
 * 22-April-92 Claude Lecommandeur.
 *
 *
 ***********************************************************************/

#include "ctwm.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>

#include <X11/extensions/shape.h>

#include "animate.h"
#ifdef CAPTIVE
#include "captive.h"
#endif
#include "colormaps.h"
#include "events.h"
#include "event_handlers.h"
#include "event_internal.h"
#include "event_names.h"
#include "functions.h"
#include "iconmgr.h"
#include "image.h"
#include "screen.h"
#include "signals.h"
#include "util.h"
#include "version.h"
#include "win_utils.h"
#ifdef SOUNDS
#include "sound.h"
#endif


static void CtwmNextEvent(Display *display, XEvent  *event);
static bool StashEventTime(XEvent *ev);
static void dumpevent(const XEvent *e);

#define MAX_X_EVENT 256
event_proc EventHandler[MAX_X_EVENT]; /* event handler jump table */
int Context = C_NO_CONTEXT;     /* current button press context */
XEvent Event;                   /* the current event */

Window DragWindow;              /* variables used in moving windows */
int origDragX;
int origDragY;
int DragX;
int DragY;
unsigned int DragWidth;
unsigned int DragHeight;
unsigned int DragBW;
int CurrentDragX;
int CurrentDragY;

Time EventTime = CurrentTime;       /* until Xlib does this for us */

/* Maybe more staticizable later? */
bool enter_flag;
bool leave_flag;
TwmWindow *enter_win, *raise_win, *leave_win, *lower_win;

/*
 * Not static because shared with colormaps.c and other events files, but
 * not listed in events.h since nowhere else needs it.
 */
bool ColortableThrashing;
TwmWindow *Tmp_win; // the current twm window; shared with other event code

int ButtonPressed = -1;
bool Cancel = false;



/*
 * Initialize the event handling bits.  Mainly the jump table for event
 * handling, plus various event vars.  Called during startup.
 */
void
InitEvents(void)
{
	/* Clear out vars */
	ResizeWindow = (Window) 0;
	DragWindow = (Window) 0;
	enter_flag = false;
	enter_win = raise_win = NULL;
	leave_flag = false;
	leave_win = lower_win = NULL;

	/* Set everything to unknown to start */
	for(int i = 0; i < MAX_X_EVENT; i++) {
		EventHandler[i] = HandleUnknown;
	}

#define STDH(evt) EventHandler[evt] = Handle##evt

	/* Init the standard events */
	STDH(Expose);
	STDH(CreateNotify);
	STDH(DestroyNotify);
	STDH(MapRequest);
	STDH(MapNotify);
	STDH(UnmapNotify);
	STDH(MotionNotify);
	STDH(ButtonRelease);
	STDH(ButtonPress);
	STDH(EnterNotify);
	STDH(LeaveNotify);
	STDH(ConfigureRequest);
	STDH(ClientMessage);
	STDH(PropertyNotify);
	STDH(KeyPress);
	STDH(KeyRelease);
	STDH(ColormapNotify);
	STDH(VisibilityNotify);
	STDH(CirculateNotify);

	/* Focus handlers are special; see comment on HandleFocusChange() */
	EventHandler[FocusIn] = HandleFocusChange;
	EventHandler[FocusOut] = HandleFocusChange;

	/* Some more conditional bits */
#ifdef EWMH
	STDH(SelectionClear);
#endif

	/*
	 * Optional extensions are registered dynamically, so their events
	 * are put by the X server at some offset into the event number tree
	 * when it starts.
	 */
	if(HasShape) {
		EventHandler[ShapeEventBase + ShapeNotify] = HandleShapeNotify;
	}

#undef STDH

	/* And done */
	return;
}



/*
 * Main event loop.
 *
 * This is the last thing called during ctwm startup, and never returns.
 * So in essence, this is everything ctwm does except starting up and
 * shutting down (and that latter winds up called through us as well).
 */
void
HandleEvents(void)
{
	while(1) {
		if(enter_flag && !QLength(dpy)) {
			if(enter_win && enter_win != raise_win) {
				AutoRaiseWindow(enter_win);   /* sets enter_flag T */
			}
			else {
				enter_flag = false;
			}
		}
		if(leave_flag && !QLength(dpy)) {
			if(leave_win && leave_win != lower_win) {
				AutoLowerWindow(leave_win);  /* sets leave_flag T */
			}
			else {
				leave_flag = false;
			}
		}
		if(ColortableThrashing && !QLength(dpy) && Scr) {
			InstallColormaps(ColormapNotify, NULL);
		}
		WindowMoved = false;

		CtwmNextEvent(dpy, &Event);

		if(Event.type < 0 || Event.type >= MAX_X_EVENT) {
			XtDispatchEvent(&Event);
		}
		else {
			DispatchEvent();
		}
	}

	/* NOTREACHED */
	fprintf(stderr, "Error: Should never reach the end of HandleEvents()\n");
	exit(1);
}


/*
 * Grab the next event in the queue to process.
 */
static void
CtwmNextEvent(Display *display, XEvent *event)
{
	int         fd;
	struct timeval timeout, *tout = NULL;
	const bool animate = (AnimationActive && MaybeAnimate);

#define NEXTEVENT XtAppNextEvent(appContext, event)

	if(SignalFlag) {
		handle_signal_flag(CurrentTime);
	}
	if(XEventsQueued(display, QueuedAfterFlush) != 0) {
		NEXTEVENT;
		return;
	}
	fd = ConnectionNumber(display);

	if(animate) {
		TryToAnimate();
	}
	if(SignalFlag) {
		handle_signal_flag(CurrentTime);
	}
	if(! MaybeAnimate) {
		NEXTEVENT;
		return;
	}
	if(animate) {
		tout = (AnimationSpeed > 0) ? &timeout : NULL;
	}
	while(1) {
		fd_set mask;
		int found;

		FD_ZERO(&mask);
		FD_SET(fd, &mask);
		if(animate) {
			timeout = AnimateTimeout;
		}
		found = select(fd + 1, &mask, NULL, NULL, tout);
		if(SignalFlag) {
			handle_signal_flag(CurrentTime);
		}
		if(found < 0) {
			if(errno != EINTR) {
				perror("select");
			}
			continue;
		}
		if(FD_ISSET(fd, &mask)) {
			NEXTEVENT;
			return;
		}
		if(found == 0) {
			if(animate) {
				TryToAnimate();
			}
			if(SignalFlag) {
				handle_signal_flag(CurrentTime);
			}
			if(! MaybeAnimate) {
				NEXTEVENT;
				return;
			}
			continue;
		}
	}

#undef NEXTEVENT

	/* NOTREACHED */
}



/*
 * And dispatchers.  These look at the global Event and run with it from
 * there.
 *
 * There are slight differences between the two, and it's not at all
 * clear to what extent there should be or why they're different.  At
 * least some of the difference seem gratuitous; this requires further
 * research.  They probably can and should be collapsed together.
 */
/* Main dispatcher, from the loop above and a few other places */
bool
DispatchEvent(void)
{
	Window w = Event.xany.window;
	ScreenInfo *thisScr;

	StashEventTime(&Event);
	Tmp_win = GetTwmWindow(w);
	thisScr = GetTwmScreen(&Event);

	dumpevent(&Event);

	if(!thisScr) {
		return false;
	}
	Scr = thisScr;

#ifdef CAPTIVE
	if(CLarg.is_captive) {
		if((Event.type == ConfigureNotify)
		                && (Event.xconfigure.window == Scr->CaptiveRoot)) {
			ConfigureCaptiveRootWindow(&Event);
			return false;
		}
	}
#endif
	FixRootEvent(&Event);
	if(Event.type >= 0 && Event.type < MAX_X_EVENT) {
#ifdef SOUNDS
		play_sound(Event.type);
#endif
		(*EventHandler[Event.type])();
	}
	return true;
}


/* Alternate; called from various function and menu code */
bool
DispatchEvent2(void)
{
	Window w = Event.xany.window;
	ScreenInfo *thisScr;

	StashEventTime(&Event);
	Tmp_win = GetTwmWindow(w);
	thisScr = GetTwmScreen(&Event);

	dumpevent(&Event);

	if(!thisScr) {
		return false;
	}
	Scr = thisScr;

	FixRootEvent(&Event);

#ifdef SOUNDS
	play_sound(Event.type);
#endif

	if(menuFromFrameOrWindowOrTitlebar) {
		if(Event.type == Expose) {
			HandleExpose();
		}
	}
	else {
		if(Event.type >= 0 && Event.type < MAX_X_EVENT) {
			(*EventHandler[Event.type])();
		}
	}

	return true;
}




/*
 * Stash the time of the given event in our EventTime global.  Called
 * during dispatching the event.
 */
static bool
StashEventTime(XEvent *ev)
{
	switch(ev->type) {
		case KeyPress:
		case KeyRelease:
			EventTime = ev->xkey.time;
			return true;
		case ButtonPress:
		case ButtonRelease:
			EventTime = ev->xbutton.time;
			return true;
		case MotionNotify:
			EventTime = ev->xmotion.time;
			return true;
		case EnterNotify:
		case LeaveNotify:
			EventTime = ev->xcrossing.time;
			return true;
		case PropertyNotify:
			EventTime = ev->xproperty.time;
			return true;
		case SelectionClear:
			EventTime = ev->xselectionclear.time;
			return true;
		case SelectionRequest:
			EventTime = ev->xselectionrequest.time;
			return true;
		case SelectionNotify:
			EventTime = ev->xselection.time;
			return true;
	}
	return false;
}


/*
 * Debugging: output details of an event.  Or at least, details of a few
 * events, presumably those which somebody doing debugging needed at the
 * time.
 */
static void
dumpevent(const XEvent *e)
{
	const char *name;

	if(! tracefile) {
		return;
	}

	/* Whatsit? */
	name = event_name_by_num(e->type);
	if(!name) {
		name = "Unknown event";
	}

	/* Tell about it */
	fprintf(tracefile, "event:  %s in window 0x%x\n", name,
	        (unsigned int)e->xany.window);
	switch(e->type) {
		case KeyPress:
		case KeyRelease:
			fprintf(tracefile, "     :  +%d,+%d (+%d,+%d)  state=%d, keycode=%d\n",
			        e->xkey.x, e->xkey.y,
			        e->xkey.x_root, e->xkey.y_root,
			        e->xkey.state, e->xkey.keycode);
			break;
		case ButtonPress:
		case ButtonRelease:
			fprintf(tracefile, "     :  +%d,+%d (+%d,+%d)  state=%d, button=%d\n",
			        e->xbutton.x, e->xbutton.y,
			        e->xbutton.x_root, e->xbutton.y_root,
			        e->xbutton.state, e->xbutton.button);
			break;
	}
}