0
|
1 /* The event_stream interface for X11 with Xt, and/or tty frames.
|
|
2 Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
4 Copyright (C) 1996 Ben Wing.
|
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
|
25 #include <config.h>
|
|
26 #include "lisp.h"
|
|
27
|
|
28 #include "console-x.h"
|
|
29 #include "lwlib.h"
|
|
30 #include "EmacsFrame.h"
|
|
31
|
|
32 #include "blocktype.h"
|
|
33 #include "buffer.h"
|
|
34 #include "commands.h"
|
|
35 #include "console.h"
|
|
36 #include "console-tty.h"
|
|
37 #include "events.h"
|
|
38 #include "frame.h"
|
|
39 #include "objects-x.h"
|
|
40 #include "process.h"
|
|
41 #include "redisplay.h"
|
|
42
|
|
43 #include "systime.h"
|
|
44 #include "sysproc.h" /* for MAXDESC */
|
|
45
|
|
46 #include "xintrinsicp.h" /* CoreP.h needs this */
|
|
47 #include <X11/CoreP.h> /* Numerous places access the fields of
|
|
48 a core widget directly. We could
|
|
49 use XtVaGetValues(), but ... */
|
|
50
|
|
51 static struct event_stream *Xt_event_stream;
|
|
52
|
|
53 /* With the new event model, all events go through XtDispatchEvent()
|
|
54 and are picked up by an event handler that is added to each frame
|
|
55 widget. (This is how it's supposed to be.) In the old method,
|
|
56 Emacs sucks out events directly from XtNextEvent() and only
|
|
57 dispatches the events that it doesn't need to deal with. This
|
|
58 old way has lots of corresponding junk that is no longer
|
|
59 necessary: lwlib extensions, synthetic XAnyEvents, unnecessary
|
|
60 magic events, etc. */
|
|
61
|
|
62 /* The one and only one application context that Emacs uses. */
|
|
63 XtAppContext Xt_app_con;
|
|
64
|
|
65 /* Do we accept events sent by other clients? */
|
|
66 int x_allow_sendevents;
|
|
67
|
|
68 int modifier_keys_are_sticky;
|
|
69
|
|
70 #ifdef DEBUG_XEMACS
|
|
71 int x_debug_events;
|
|
72 #endif
|
|
73
|
|
74 static int process_events_occurred;
|
|
75 static int tty_events_occurred;
|
|
76
|
|
77 /* Mask of bits indicating the descriptors that we wait for input on */
|
|
78 extern SELECT_TYPE input_wait_mask, process_only_mask, tty_only_mask;
|
|
79
|
|
80 static CONST String x_fallback_resources[] =
|
|
81 {
|
|
82 /* This file is automatically generated from the app-defaults file
|
|
83 in ../etc/Emacs.ad. These resources are consulted only if no
|
|
84 app-defaults file is found at all.
|
|
85 */
|
|
86 #include "Emacs.ad.h"
|
|
87 0
|
|
88 };
|
|
89
|
|
90 void emacs_Xt_mapping_action (Widget w, XEvent *event);
|
|
91 void debug_process_finalization (struct Lisp_Process *p);
|
|
92 Lisp_Object dequeue_Xt_dispatch_event (void);
|
|
93 void emacs_Xt_event_handler (Widget wid, XtPointer closure, XEvent *event,
|
|
94 Boolean *continue_to_dispatch);
|
|
95
|
|
96 #ifdef EPOCH
|
|
97 void dispatch_epoch_event (struct frame *f, XEvent *event, Lisp_Object type);
|
|
98 #endif
|
|
99
|
|
100 static int last_quit_check_signal_tick_count;
|
|
101
|
|
102 Lisp_Object Qkey_mapping;
|
|
103
|
|
104
|
|
105 /************************************************************************/
|
|
106 /* keymap handling */
|
|
107 /************************************************************************/
|
|
108
|
|
109 /* X bogusly doesn't define the interpretations of any bits besides
|
|
110 ModControl, ModShift, and ModLock; so the Interclient Communication
|
|
111 Conventions Manual says that we have to bend over backwards to figure
|
|
112 out what the other modifier bits mean. According to ICCCM:
|
|
113
|
|
114 - Any keycode which is assigned ModControl is a "control" key.
|
|
115
|
|
116 - Any modifier bit which is assigned to a keycode which generates Meta_L
|
|
117 or Meta_R is the modifier bit meaning "meta". Likewise for Super, Hyper,
|
|
118 etc.
|
|
119
|
|
120 - Any keypress event which contains ModControl in its state should be
|
|
121 interpreted as a "control" character.
|
|
122
|
|
123 - Any keypress event which contains a modifier bit in its state which is
|
|
124 generated by a keycode whose corresponding keysym is Meta_L or Meta_R
|
|
125 should be interpreted as a "meta" character. Likewise for Super, Hyper,
|
|
126 etc.
|
|
127
|
|
128 - It is illegal for a keysym to be associated with more than one modifier
|
|
129 bit.
|
|
130
|
|
131 This means that the only thing that emacs can reasonably interpret as a
|
|
132 "meta" key is a key whose keysym is Meta_L or Meta_R, and which generates
|
|
133 one of the modifier bits Mod1-Mod5.
|
|
134
|
|
135 Unfortunately, many keyboards don't have Meta keys in their default
|
|
136 configuration. So, if there are no Meta keys, but there are "Alt" keys,
|
|
137 emacs will interpret Alt as Meta. If there are both Meta and Alt keys,
|
|
138 then the Meta keys mean "Meta", and the Alt keys mean "Alt" (it used to
|
|
139 mean "Symbol," but that just confused the hell out of way too many people).
|
|
140
|
|
141 This works with the default configurations of the 19 keyboard-types I've
|
|
142 checked.
|
|
143
|
|
144 Emacs detects keyboard configurations which violate the above rules, and
|
|
145 prints an error message on the standard-error-output. (Perhaps it should
|
|
146 use a pop-up-window instead.)
|
|
147 */
|
|
148
|
|
149 static void
|
|
150 x_reset_key_mapping (struct device *d)
|
|
151 {
|
|
152 Display *display = DEVICE_X_DISPLAY (d);
|
|
153 struct x_device *xd = DEVICE_X_DATA (d);
|
|
154 int max_code;
|
|
155 if (xd->x_keysym_map)
|
|
156 XFree ((char *) xd->x_keysym_map);
|
|
157 XDisplayKeycodes (display, &xd->x_keysym_map_min_code,
|
|
158 &max_code);
|
|
159 xd->x_keysym_map =
|
|
160 XGetKeyboardMapping (display, xd->x_keysym_map_min_code,
|
|
161 max_code - xd->x_keysym_map_min_code + 1,
|
|
162 &xd->x_keysym_map_keysyms_per_code);
|
|
163 }
|
|
164
|
|
165 static CONST char *
|
|
166 index_to_name (int indice)
|
|
167 {
|
2
|
168 switch (indice)
|
|
169 {
|
|
170 case ShiftMapIndex: return "ModShift";
|
|
171 case LockMapIndex: return "ModLock";
|
|
172 case ControlMapIndex: return "ModControl";
|
|
173 case Mod1MapIndex: return "Mod1";
|
|
174 case Mod2MapIndex: return "Mod2";
|
|
175 case Mod3MapIndex: return "Mod3";
|
|
176 case Mod4MapIndex: return "Mod4";
|
|
177 case Mod5MapIndex: return "Mod5";
|
|
178 default: return "???";
|
|
179 }
|
0
|
180 }
|
|
181
|
|
182 /* Boy, I really wish C had local functions... */
|
|
183 struct c_doesnt_have_closures /* #### not yet used */
|
|
184 {
|
|
185 int warned_about_overlapping_modifiers;
|
|
186 int warned_about_predefined_modifiers;
|
|
187 int warned_about_duplicate_modifiers;
|
|
188 int meta_bit;
|
|
189 int hyper_bit;
|
|
190 int super_bit;
|
|
191 int alt_bit;
|
|
192 int mode_bit;
|
|
193 };
|
|
194
|
|
195 static void
|
|
196 x_reset_modifier_mapping (struct device *d)
|
|
197 {
|
|
198 Display *display = DEVICE_X_DISPLAY (d);
|
|
199 struct x_device *xd = DEVICE_X_DATA (d);
|
|
200 int modifier_index, modifier_key, column, mkpm;
|
|
201 int warned_about_overlapping_modifiers = 0;
|
2
|
202 int warned_about_predefined_modifiers = 0;
|
|
203 int warned_about_duplicate_modifiers = 0;
|
|
204 int meta_bit = 0;
|
0
|
205 int hyper_bit = 0;
|
|
206 int super_bit = 0;
|
2
|
207 int alt_bit = 0;
|
|
208 int mode_bit = 0;
|
0
|
209
|
|
210 xd->lock_interpretation = 0;
|
|
211
|
|
212 if (xd->x_modifier_keymap)
|
|
213 XFreeModifiermap (xd->x_modifier_keymap);
|
|
214
|
|
215 x_reset_key_mapping (d);
|
|
216
|
|
217 xd->x_modifier_keymap = XGetModifierMapping (display);
|
|
218
|
|
219 /* Boy, I really wish C had local functions...
|
|
220 */
|
|
221
|
|
222 /* The call to warn_when_safe must be on the same line as the string or
|
|
223 make-msgfile won't pick it up properly (the newline doesn't confuse
|
|
224 it, but the backslash does). */
|
|
225
|
|
226 #define modwarn(name,old,other) \
|
|
227 warn_when_safe (Qkey_mapping, Qwarning, "XEmacs: %s (0x%x) generates %s, which is generated by %s.", \
|
|
228 name, code, index_to_name (old), other), \
|
|
229 warned_about_overlapping_modifiers = 1
|
|
230
|
|
231 #define modbarf(name,other) \
|
|
232 warn_when_safe (Qkey_mapping, Qwarning, "XEmacs: %s (0x%x) generates %s, which is nonsensical.", \
|
|
233 name, code, other), \
|
|
234 warned_about_predefined_modifiers = 1
|
|
235
|
|
236 #define check_modifier(name,mask) \
|
|
237 if ((1<<modifier_index) != mask) \
|
|
238 warn_when_safe (Qkey_mapping, Qwarning, "XEmacs: %s (0x%x) generates %s, which is nonsensical.", \
|
|
239 name, code, index_to_name (modifier_index)), \
|
|
240 warned_about_predefined_modifiers = 1
|
|
241
|
|
242 #define store_modifier(name,old) \
|
|
243 if (old && old != modifier_index) \
|
|
244 warn_when_safe (Qkey_mapping, Qwarning, "XEmacs: %s (0x%x) generates both %s and %s, which is nonsensical.",\
|
|
245 name, code, index_to_name (old), \
|
|
246 index_to_name (modifier_index)), \
|
|
247 warned_about_duplicate_modifiers = 1; \
|
|
248 if (modifier_index == ShiftMapIndex) modbarf (name,"ModShift"); \
|
|
249 else if (modifier_index == LockMapIndex) modbarf (name,"ModLock"); \
|
|
250 else if (modifier_index == ControlMapIndex) modbarf (name,"ModControl"); \
|
|
251 else if (sym == XK_Mode_switch) \
|
|
252 mode_bit = modifier_index; /* Mode_switch is special, see below... */ \
|
|
253 else if (modifier_index == meta_bit && old != meta_bit) \
|
|
254 modwarn (name, meta_bit, "Meta"); \
|
|
255 else if (modifier_index == super_bit && old != super_bit) \
|
|
256 modwarn (name, super_bit, "Super"); \
|
|
257 else if (modifier_index == hyper_bit && old != hyper_bit) \
|
|
258 modwarn (name, hyper_bit, "Hyper"); \
|
|
259 else if (modifier_index == alt_bit && old != alt_bit) \
|
|
260 modwarn (name, alt_bit, "Alt"); \
|
|
261 else \
|
|
262 old = modifier_index;
|
|
263
|
|
264 mkpm = xd->x_modifier_keymap->max_keypermod;
|
|
265 for (modifier_index = 0; modifier_index < 8; modifier_index++)
|
|
266 for (modifier_key = 0; modifier_key < mkpm; modifier_key++) {
|
|
267 KeySym last_sym = 0;
|
|
268 for (column = 0; column < 4; column += 2) {
|
|
269 KeyCode code = xd->x_modifier_keymap->modifiermap[modifier_index * mkpm
|
|
270 + modifier_key];
|
|
271 KeySym sym = (code ? XKeycodeToKeysym (display, code, column) : 0);
|
|
272 if (sym == last_sym) continue;
|
|
273 last_sym = sym;
|
|
274 switch (sym) {
|
|
275 case XK_Mode_switch:store_modifier ("Mode_switch", mode_bit); break;
|
|
276 case XK_Meta_L: store_modifier ("Meta_L", meta_bit); break;
|
|
277 case XK_Meta_R: store_modifier ("Meta_R", meta_bit); break;
|
|
278 case XK_Super_L: store_modifier ("Super_L", super_bit); break;
|
|
279 case XK_Super_R: store_modifier ("Super_R", super_bit); break;
|
|
280 case XK_Hyper_L: store_modifier ("Hyper_L", hyper_bit); break;
|
|
281 case XK_Hyper_R: store_modifier ("Hyper_R", hyper_bit); break;
|
|
282 case XK_Alt_L: store_modifier ("Alt_L", alt_bit); break;
|
|
283 case XK_Alt_R: store_modifier ("Alt_R", alt_bit); break;
|
|
284 case XK_Control_L: check_modifier ("Control_L", ControlMask); break;
|
|
285 case XK_Control_R: check_modifier ("Control_R", ControlMask); break;
|
|
286 case XK_Shift_L: check_modifier ("Shift_L", ShiftMask); break;
|
|
287 case XK_Shift_R: check_modifier ("Shift_R", ShiftMask); break;
|
|
288 case XK_Shift_Lock: check_modifier ("Shift_Lock", LockMask);
|
|
289 xd->lock_interpretation = XK_Shift_Lock; break;
|
|
290 case XK_Caps_Lock: check_modifier ("Caps_Lock", LockMask);
|
|
291 xd->lock_interpretation = XK_Caps_Lock; break;
|
|
292
|
|
293 /* It probably doesn't make any sense for a modifier bit to be
|
|
294 assigned to a key that is not one of the above, but OpenWindows
|
|
295 assigns modifier bits to a couple of random function keys for
|
|
296 no reason that I can discern, so printing a warning here would
|
|
297 be annoying.
|
|
298 */
|
|
299 }
|
|
300 }
|
|
301 }
|
|
302 #undef store_modifier
|
|
303 #undef check_modifier
|
|
304 #undef modwarn
|
|
305 #undef modbarf
|
|
306
|
|
307 /* If there was no Meta key, then try using the Alt key instead.
|
|
308 If there is both a Meta key and an Alt key, then the Alt key
|
|
309 is not disturbed and remains an Alt key.
|
|
310 */
|
|
311 if (! meta_bit && alt_bit)
|
|
312 meta_bit = alt_bit, alt_bit = 0;
|
|
313
|
|
314 /* mode_bit overrides everything, since it's processed down inside of
|
|
315 XLookupString() instead of by us. If Meta and Mode_switch both
|
|
316 generate the same modifier bit (which is an error), then we don't
|
|
317 interpret that bit as Meta, because we can't make XLookupString()
|
|
318 not interpret it as Mode_switch; and interpreting it as both would
|
|
319 be totally wrong.
|
|
320 */
|
|
321 if (mode_bit)
|
|
322 {
|
|
323 CONST char *warn = 0;
|
|
324 if (mode_bit == meta_bit) warn = "Meta", meta_bit = 0;
|
|
325 else if (mode_bit == hyper_bit) warn = "Hyper", hyper_bit = 0;
|
|
326 else if (mode_bit == super_bit) warn = "Super", super_bit = 0;
|
|
327 else if (mode_bit == alt_bit) warn = "Alt", alt_bit = 0;
|
|
328 if (warn)
|
|
329 {
|
|
330 warn_when_safe
|
|
331 (Qkey_mapping, Qwarning,
|
|
332 "XEmacs: %s is being used for both Mode_switch and %s.",
|
|
333 index_to_name (mode_bit), warn),
|
|
334 warned_about_overlapping_modifiers = 1;
|
|
335 }
|
|
336 }
|
|
337 #undef index_to_name
|
|
338
|
|
339 xd->MetaMask = (meta_bit ? (1 << meta_bit) : 0);
|
|
340 xd->HyperMask = (hyper_bit ? (1 << hyper_bit) : 0);
|
|
341 xd->SuperMask = (super_bit ? (1 << super_bit) : 0);
|
|
342 xd->AltMask = (alt_bit ? (1 << alt_bit) : 0);
|
|
343 xd->ModeMask = (mode_bit ? (1 << mode_bit) : 0); /* unused */
|
|
344
|
|
345
|
|
346 if (warned_about_overlapping_modifiers)
|
|
347 warn_when_safe (Qkey_mapping, Qwarning, "\n"
|
|
348 " Two distinct modifier keys (such as Meta and Hyper) cannot generate\n"
|
|
349 " the same modifier bit, because Emacs won't be able to tell which\n"
|
|
350 " modifier was actually held down when some other key is pressed. It\n"
|
|
351 " won't be able to tell Meta-x and Hyper-x apart, for example. Change\n"
|
|
352 " one of these keys to use some other modifier bit. If you intend for\n"
|
|
353 " these keys to have the same behavior, then change them to have the\n"
|
|
354 " same keysym as well as the same modifier bit.");
|
|
355
|
|
356 if (warned_about_predefined_modifiers)
|
|
357 warn_when_safe (Qkey_mapping, Qwarning, "\n"
|
|
358 " The semantics of the modifier bits ModShift, ModLock, and ModControl\n"
|
|
359 " are predefined. It does not make sense to assign ModControl to any\n"
|
|
360 " keysym other than Control_L or Control_R, or to assign any modifier\n"
|
|
361 " bits to the \"control\" keysyms other than ModControl. You can't\n"
|
|
362 " turn a \"control\" key into a \"meta\" key (or vice versa) by simply\n"
|
|
363 " assigning the key a different modifier bit. You must also make that\n"
|
|
364 " key generate an appropriate keysym (Control_L, Meta_L, etc).");
|
|
365
|
|
366 /* Don\'t need to say anything more for warned_about_duplicate_modifiers. */
|
|
367
|
|
368 if (warned_about_overlapping_modifiers || warned_about_predefined_modifiers)
|
|
369 warn_when_safe (Qkey_mapping, Qwarning, "\n"
|
|
370 " The meanings of the modifier bits Mod1 through Mod5 are determined\n"
|
|
371 " by the keysyms used to control those bits. Mod1 does NOT always\n"
|
|
372 " mean Meta, although some non-ICCCM-compliant programs assume that.");
|
|
373 }
|
|
374
|
|
375 void
|
|
376 x_init_modifier_mapping (struct device *d)
|
|
377 {
|
|
378 DEVICE_X_DATA (d)->x_keysym_map = 0;
|
|
379 DEVICE_X_DATA (d)->x_modifier_keymap = 0;
|
|
380 x_reset_modifier_mapping (d);
|
|
381 }
|
|
382
|
|
383 static int
|
|
384 x_key_is_modifier_p (KeyCode keycode, struct device *d)
|
|
385 {
|
|
386 struct x_device *xd = DEVICE_X_DATA (d);
|
|
387 KeySym *syms = &xd->x_keysym_map [(keycode - xd->x_keysym_map_min_code) *
|
|
388 xd->x_keysym_map_keysyms_per_code];
|
|
389 int i;
|
|
390 for (i = 0; i < xd->x_keysym_map_keysyms_per_code; i++)
|
|
391 if (IsModifierKey (syms [i]) ||
|
|
392 syms [i] == XK_Mode_switch) /* why doesn't IsModifierKey count this? */
|
|
393 return 1;
|
|
394 return 0;
|
|
395 }
|
|
396
|
|
397 /* key-handling code is always ugly. It just ends up working out
|
|
398 that way.
|
|
399
|
|
400 Here are some pointers:
|
|
401
|
|
402 -- DOWN_MASK indicates which modifiers should be treated as "down"
|
|
403 when the corresponding upstroke happens. It gets reset for
|
|
404 a particular modifier when that modifier goes up, and reset
|
|
405 for all modifiers when a non-modifier key is pressed. Example:
|
|
406
|
|
407 I press Control-A-Shift and then release Control-A-Shift.
|
|
408 I want the Shift key to be sticky but not the Control key.
|
|
409
|
|
410 -- LAST_DOWNKEY and RELEASE_TIME are used to keep track of
|
|
411 auto-repeat -- see below.
|
|
412
|
|
413 -- If a modifier key is sticky, I can unstick it by pressing
|
|
414 the modifier key again. */
|
|
415
|
|
416 static void
|
|
417 x_handle_sticky_modifiers (XEvent *ev, struct device *d)
|
|
418 {
|
2
|
419 struct x_device *xd;
|
|
420 KeyCode keycode;
|
|
421 int type;
|
|
422
|
|
423 if (!modifier_keys_are_sticky) /* Optimize for non-sticky modifiers */
|
0
|
424 return;
|
|
425
|
2
|
426 xd = DEVICE_X_DATA (d);
|
|
427 keycode = ev->xkey.keycode;
|
|
428 type = ev->type;
|
|
429
|
|
430 if (! ((type == KeyPress || type == KeyRelease) &&
|
|
431 x_key_is_modifier_p (keycode, d)))
|
|
432 { /* Not a modifier key */
|
|
433 Bool key_event_p = (type == KeyPress || type == KeyRelease);
|
|
434
|
0
|
435 if (type == KeyPress && !xd->last_downkey)
|
|
436 xd->last_downkey = keycode;
|
|
437 else if (type == ButtonPress ||
|
|
438 (type == KeyPress && xd->last_downkey &&
|
|
439 (keycode != xd->last_downkey ||
|
|
440 ev->xkey.time != xd->release_time)))
|
|
441 {
|
|
442 xd->need_to_add_mask = 0;
|
|
443 xd->last_downkey = 0;
|
|
444 }
|
|
445 if (type == KeyPress)
|
|
446 xd->release_time = 0;
|
|
447 if (type == KeyPress || type == ButtonPress)
|
|
448 xd->down_mask = 0;
|
|
449
|
2
|
450 if (key_event_p)
|
|
451 ev->xkey.state |= xd->need_to_add_mask;
|
|
452 else
|
|
453 ev->xbutton.state |= xd->need_to_add_mask;
|
0
|
454
|
|
455 if (type == KeyRelease && keycode == xd->last_downkey)
|
|
456 /* If I hold press-and-release the Control key and then press
|
|
457 and hold down the right arrow, I want it to auto-repeat
|
|
458 Control-Right. On the other hand, if I do the same but
|
|
459 manually press the Right arrow a bunch of times, I want
|
|
460 to see one Control-Right and then a bunch of Rights.
|
|
461 This means that we need to distinguish between an
|
|
462 auto-repeated key and a key pressed and released a bunch
|
|
463 of times.
|
|
464
|
|
465 Naturally, the designers of the X spec didn't see fit
|
|
466 to provide an obvious way to distinguish these cases.
|
|
467 So we assume that if the release and the next press
|
|
468 occur at the same time, the key was actually auto-
|
|
469 repeated. Under Open-Windows, at least, this works.
|
|
470 */
|
2
|
471 xd->release_time = key_event_p ? ev->xkey.time : ev->xbutton.time;
|
0
|
472 }
|
2
|
473 else /* Modifier key pressed */
|
0
|
474 {
|
2
|
475 int i;
|
0
|
476 KeySym *syms = &xd->x_keysym_map [(keycode - xd->x_keysym_map_min_code) *
|
|
477 xd->x_keysym_map_keysyms_per_code];
|
2
|
478
|
|
479 /* If a non-modifier key was pressed in the middle of a bunch
|
|
480 of modifiers, then it unsticks all the modifiers that were
|
|
481 previously pressed. We cannot unstick the modifiers until
|
|
482 now because we want to check for auto-repeat of the
|
|
483 non-modifier key. */
|
|
484
|
|
485 if (xd->last_downkey)
|
|
486 {
|
|
487 xd->last_downkey = 0;
|
|
488 xd->need_to_add_mask = 0;
|
|
489 }
|
0
|
490
|
|
491 #define FROB(mask) \
|
|
492 do { \
|
|
493 if (type == KeyPress) \
|
|
494 { \
|
|
495 /* If modifier key is already sticky, \
|
|
496 then unstick it. Note that we do \
|
|
497 not test down_mask to deal with the \
|
|
498 unlikely but possible case that the \
|
|
499 modifier key auto-repeats. */ \
|
|
500 if (xd->need_to_add_mask & mask) \
|
|
501 { \
|
|
502 xd->need_to_add_mask &= ~mask; \
|
|
503 xd->down_mask &= ~mask; \
|
|
504 } \
|
|
505 else \
|
|
506 xd->down_mask |= mask; \
|
|
507 } \
|
|
508 else \
|
|
509 { \
|
|
510 if (xd->down_mask & mask) \
|
|
511 { \
|
|
512 xd->down_mask &= ~mask; \
|
|
513 xd->need_to_add_mask |= mask; \
|
|
514 } \
|
|
515 } \
|
|
516 } while (0)
|
|
517
|
|
518 for (i = 0; i < xd->x_keysym_map_keysyms_per_code; i++)
|
2
|
519 switch (syms[i])
|
|
520 {
|
|
521 case XK_Control_L: case XK_Control_R: FROB (ControlMask); break;
|
|
522 case XK_Shift_L: case XK_Shift_R: FROB (ShiftMask); break;
|
|
523 case XK_Meta_L: case XK_Meta_R: FROB (xd->MetaMask); break;
|
|
524 case XK_Super_L: case XK_Super_R: FROB (xd->SuperMask); break;
|
|
525 case XK_Hyper_L: case XK_Hyper_R: FROB (xd->HyperMask); break;
|
|
526 case XK_Alt_L: case XK_Alt_R: FROB (xd->AltMask); break;
|
|
527 }
|
0
|
528 }
|
|
529 #undef FROB
|
|
530 }
|
|
531
|
|
532 static void
|
|
533 clear_sticky_modifiers (struct device *d)
|
|
534 {
|
|
535 struct x_device *xd = DEVICE_X_DATA (d);
|
|
536
|
|
537 xd->need_to_add_mask = 0;
|
|
538 xd->last_downkey = 0;
|
|
539 xd->release_time = 0;
|
|
540 xd->down_mask = 0;
|
|
541 }
|
|
542
|
|
543 static int
|
|
544 keysym_obeys_caps_lock_p (KeySym sym, struct device *d)
|
|
545 {
|
|
546 struct x_device *xd = DEVICE_X_DATA (d);
|
2
|
547 /* Eeeeevil hack. Don't apply Caps_Lock to things that aren't alphabetic
|
0
|
548 characters, where "alphabetic" means something more than simply A-Z.
|
2
|
549 That is, if Caps_Lock is down, typing ESC doesn't produce Shift-ESC.
|
0
|
550 But if shift-lock is down, then it does.
|
|
551 */
|
|
552 if (xd->lock_interpretation == XK_Shift_Lock)
|
|
553 return 1;
|
2
|
554
|
|
555 return
|
|
556 ((sym >= XK_A) && (sym <= XK_Z)) ||
|
|
557 ((sym >= XK_a) && (sym <= XK_z)) ||
|
|
558 ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis)) ||
|
|
559 ((sym >= XK_agrave) && (sym <= XK_odiaeresis)) ||
|
|
560 ((sym >= XK_Ooblique) && (sym <= XK_Thorn)) ||
|
|
561 ((sym >= XK_oslash) && (sym <= XK_thorn));
|
0
|
562 }
|
|
563
|
|
564 /* called from EmacsFrame.c (actually from Xt itself) when a
|
2
|
565 MappingNotify event is received. In its infinite wisdom, Xt
|
|
566 decided that Xt event handlers never get MappingNotify events.
|
|
567 O'Reilly Xt Programming Manual 9.1.2 says:
|
|
568
|
|
569 MappingNotify is automatically handled by Xt, so it isn't passed
|
|
570 to event handlers and you don't need to worry about it.
|
|
571
|
|
572 Of course, we DO worry about it, so we need a special translation. */
|
0
|
573 void
|
|
574 emacs_Xt_mapping_action (Widget w, XEvent* event)
|
|
575 {
|
|
576 struct device *d = get_device_from_display (event->xany.display);
|
|
577 #if 0
|
|
578 /* nyet. Now this is handled by Xt. */
|
|
579 XRefreshKeyboardMapping (&event->xmapping);
|
|
580 #endif
|
|
581 /* xmodmap generates about a billion MappingKeyboard events, followed
|
|
582 by a single MappingModifier event, so it might be worthwhile to
|
|
583 take extra MappingKeyboard events out of the queue before requesting
|
|
584 the current keymap from the server.
|
|
585 */
|
2
|
586 switch (event->xmapping.request)
|
|
587 {
|
|
588 case MappingKeyboard: x_reset_key_mapping (d); break;
|
|
589 case MappingModifier: x_reset_modifier_mapping (d); break;
|
|
590 case MappingPointer: /* Do something here? */ break;
|
|
591 default: abort();
|
|
592 }
|
0
|
593 }
|
|
594
|
|
595
|
|
596 /************************************************************************/
|
|
597 /* X to Emacs event conversion */
|
|
598 /************************************************************************/
|
|
599
|
|
600 #if (defined(sun) || defined(__sun)) && defined(__GNUC__)
|
|
601 # define SUNOS_GCC_L0_BUG
|
|
602 #endif
|
|
603
|
|
604 #ifdef SUNOS_GCC_L0_BUG
|
|
605 static void
|
|
606 x_to_emacs_keysym_sunos_bug (Lisp_Object *return_value_sunos_bug, /* #### */
|
|
607 XEvent *event, int simple_p)
|
|
608 #else /* !SUNOS_GCC_L0_BUG */
|
|
609 static Lisp_Object
|
|
610 x_to_emacs_keysym (XEvent *event, int simple_p)
|
|
611 #endif /* !SUNOS_GCC_L0_BUG */
|
|
612 /* simple_p means don't try too hard (ASCII only) */
|
|
613 {
|
|
614 char *name;
|
|
615 KeySym keysym = 0;
|
2
|
616 /* struct device *d = get_device_from_display (event->xany.display); */
|
0
|
617 /* Apparently it's necessary to specify a dummy here (rather than
|
|
618 passing in 0) to avoid crashes on German IRIX */
|
|
619 char dummy[256];
|
|
620
|
|
621 #ifdef SUNOS_GCC_L0_BUG
|
|
622 # define return(lose) \
|
|
623 do {*return_value_sunos_bug = (lose); goto return_it; } while (0)
|
|
624 #endif
|
|
625
|
|
626 /* ### FIX this by replacing with calls to XmbLookupString.
|
|
627 XLookupString should never be called. --mrb */
|
|
628 XLookupString (&event->xkey, dummy, 200, &keysym, 0);
|
|
629
|
|
630 /* &DEVICE_X_X_COMPOSE_STATUS (d)); */
|
|
631
|
|
632 if (keysym >= XK_exclam && keysym <= XK_asciitilde)
|
|
633 /* We must assume that the X keysym numbers for the ASCII graphic
|
|
634 characters are the same as their ASCII codes. */
|
|
635 return (make_char (keysym));
|
|
636
|
|
637 switch (keysym)
|
|
638 {
|
|
639 /* These would be handled correctly by the default case, but by
|
|
640 special-casing them here we don't garbage a string or call intern().
|
|
641 */
|
|
642 case XK_BackSpace: return (QKbackspace);
|
|
643 case XK_Tab: return (QKtab);
|
|
644 case XK_Linefeed: return (QKlinefeed);
|
|
645 case XK_Return: return (QKreturn);
|
|
646 case XK_Escape: return (QKescape);
|
|
647 case XK_space: return (QKspace);
|
|
648 case XK_Delete: return (QKdelete);
|
|
649 case 0: return (Qnil);
|
|
650 /* This kludge prevents bogus Xlib compose conversions.
|
|
651 Don't ask why. The following case must be removed when we
|
|
652 switch to using XmbLookupString */
|
|
653 case XK_Multi_key: XLookupString (&event->xkey, dummy, 200, &keysym, 0);
|
|
654 /* Fallthrough!! */
|
|
655 default:
|
|
656 if (simple_p) return (Qnil);
|
|
657 /* #### without return_value_sunos_bug, %l0 (GCC struct return pointer)
|
|
658 * #### gets roached (top 8 bits cleared) around this call.
|
|
659 */
|
|
660 /* !!#### not Mule-ized */
|
|
661 name = XKeysymToString (keysym);
|
|
662 if (!name || !name[0]) /* this shouldn't happen... */
|
|
663 {
|
|
664 char buf [255];
|
|
665 sprintf (buf, "unknown_keysym_0x%X", (int) keysym);
|
|
666 return (KEYSYM (buf));
|
|
667 }
|
|
668 /* If it's got a one-character name, that's good enough. */
|
|
669 if (!name[1]) return (make_char (name[0]));
|
|
670
|
|
671 /* If it's in the "Keyboard" character set, downcase it.
|
|
672 The case of those keysyms is too totally random for us to
|
|
673 force anyone to remember them.
|
|
674 The case of the other character sets is significant, however.
|
|
675 */
|
|
676 if ((((unsigned int) keysym) & (~0xFF)) == ((unsigned int) 0xFF00))
|
|
677 {
|
|
678 char buf [255];
|
|
679 char *s1, *s2;
|
|
680 for (s1 = name, s2 = buf; *s1; s1++, s2++)
|
|
681 *s2 = tolower (* (unsigned char *) s1);
|
|
682 *s2 = 0;
|
|
683 return (KEYSYM (buf));
|
|
684 }
|
|
685 return (KEYSYM (name));
|
|
686 }
|
|
687 #ifdef SUNOS_GCC_L0_BUG
|
|
688 # undef return
|
|
689 return_it:
|
|
690 return;
|
|
691 #endif
|
|
692 }
|
|
693
|
|
694 #ifdef SUNOS_GCC_L0_BUG
|
|
695 /* #### */
|
|
696 static Lisp_Object
|
|
697 x_to_emacs_keysym (XEvent *event, int simple_p)
|
|
698 {
|
|
699 Lisp_Object return_value_sunos_bug;
|
|
700 x_to_emacs_keysym_sunos_bug (&return_value_sunos_bug, event, simple_p);
|
|
701 return (return_value_sunos_bug);
|
|
702 }
|
|
703 #endif
|
|
704
|
|
705 static void
|
|
706 set_last_server_timestamp (struct device *d, XEvent *x_event)
|
|
707 {
|
2
|
708 Time t;
|
|
709 switch (x_event->type)
|
0
|
710 {
|
|
711 case KeyPress:
|
2
|
712 case KeyRelease: t = x_event->xkey.time; break;
|
0
|
713 case ButtonPress:
|
2
|
714 case ButtonRelease: t = x_event->xbutton.time; break;
|
0
|
715 case EnterNotify:
|
2
|
716 case LeaveNotify: t = x_event->xcrossing.time; break;
|
|
717 case MotionNotify: t = x_event->xmotion.time; break;
|
|
718 case PropertyNotify: t = x_event->xproperty.time; break;
|
|
719 case SelectionClear: t = x_event->xselectionclear.time; break;
|
|
720 case SelectionRequest: t = x_event->xselectionrequest.time; break;
|
|
721 case SelectionNotify: t = x_event->xselection.time; break;
|
|
722 default: return;
|
0
|
723 }
|
2
|
724 DEVICE_X_LAST_SERVER_TIMESTAMP (d) = t;
|
0
|
725 }
|
|
726
|
|
727 static int
|
|
728 x_event_to_emacs_event (XEvent *x_event, struct Lisp_Event *emacs_event)
|
|
729 {
|
2
|
730 Display *display = x_event->xany.display;
|
|
731 struct device *d = get_device_from_display (display);
|
0
|
732 struct x_device *xd = DEVICE_X_DATA (d);
|
|
733
|
|
734 set_last_server_timestamp (d, x_event);
|
|
735
|
2
|
736 switch (x_event->type)
|
0
|
737 {
|
|
738 case KeyRelease:
|
|
739 x_handle_sticky_modifiers (x_event, d);
|
|
740 return 0;
|
|
741
|
|
742 case KeyPress:
|
|
743 case ButtonPress:
|
|
744 case ButtonRelease:
|
|
745 {
|
2
|
746 unsigned int modifiers = 0;
|
|
747 int shift_p;
|
|
748 int lock_p;
|
|
749 Bool key_event_p = (x_event->type == KeyPress);
|
|
750 unsigned int *state =
|
|
751 key_event_p ? &x_event->xkey.state : &x_event->xbutton.state;
|
0
|
752
|
2
|
753 /* If this is a synthetic KeyPress or Button event, and the user
|
|
754 has expressed a disinterest in this security hole, then drop
|
|
755 it on the floor. */
|
|
756 if ((key_event_p
|
|
757 ? x_event->xkey.send_event
|
|
758 : x_event->xbutton.send_event)
|
|
759 #ifdef EXTERNAL_WIDGET
|
|
760 /* ben: events get sent to an ExternalShell using XSendEvent.
|
|
761 This is not a perfect solution. */
|
|
762 && !FRAME_X_EXTERNAL_WINDOW_P (
|
|
763 x_any_window_to_frame (d, x_event->xany.window))
|
|
764 #endif
|
|
765 && !x_allow_sendevents)
|
|
766 return 0;
|
|
767
|
|
768 DEVICE_X_MOUSE_TIMESTAMP (d) =
|
|
769 DEVICE_X_GLOBAL_MOUSE_TIMESTAMP (d) =
|
|
770 key_event_p ? x_event->xkey.time : x_event->xbutton.time;
|
|
771
|
|
772 x_handle_sticky_modifiers (x_event, d);
|
|
773
|
|
774 if (*state & ControlMask) modifiers |= MOD_CONTROL;
|
|
775 if (*state & xd->MetaMask) modifiers |= MOD_META;
|
|
776 if (*state & xd->SuperMask) modifiers |= MOD_SUPER;
|
|
777 if (*state & xd->HyperMask) modifiers |= MOD_HYPER;
|
|
778 if (*state & xd->AltMask) modifiers |= MOD_ALT;
|
0
|
779
|
2
|
780 /* Ignore the Caps_Lock key if:
|
|
781 - any other modifiers are down, so that Caps_Lock doesn't
|
|
782 turn C-x into C-X, which would suck.
|
|
783 - the event was a mouse event. */
|
|
784 if (modifiers || ! key_event_p)
|
|
785 *state &= (~LockMask);
|
|
786
|
|
787 shift_p = *state & ShiftMask;
|
|
788 lock_p = *state & LockMask;
|
|
789
|
|
790 if (shift_p || lock_p)
|
|
791 modifiers |= MOD_SHIFT;
|
0
|
792
|
2
|
793 if (key_event_p)
|
|
794 {
|
|
795 Lisp_Object keysym;
|
|
796 XKeyEvent *ev = &x_event->xkey;
|
|
797 KeyCode keycode = ev->keycode;
|
|
798
|
0
|
799
|
2
|
800 if (x_key_is_modifier_p (keycode, d)) /* it's a modifier key */
|
|
801 return 0;
|
|
802
|
|
803 /* This used to compute the frame from the given X window and
|
|
804 store it here, but we really don't care about the frame. */
|
|
805 emacs_event->channel = DEVICE_CONSOLE (d);
|
|
806 keysym = x_to_emacs_keysym (x_event, 0);
|
0
|
807
|
2
|
808 /* If the emacs keysym is nil, then that means that the
|
|
809 X keysym was NoSymbol, which probably means that
|
|
810 we're in the midst of reading a Multi_key sequence,
|
|
811 or a "dead" key prefix. Ignore it. */
|
|
812 if (NILP (keysym))
|
|
813 return 0;
|
0
|
814
|
2
|
815 /* More Caps_Lock garbage: Caps_Lock should *only* add the
|
|
816 shift modifier to two-case keys (that is, A-Z and
|
|
817 related characters). So at this point (after looking up
|
|
818 the keysym) if the keysym isn't a dual-case alphabetic,
|
|
819 and if the caps lock key was down but the shift key
|
|
820 wasn't, then turn off the shift modifier. Gag barf */
|
|
821 /* #### type lossage: assuming equivalence of emacs and
|
|
822 X keysyms */
|
|
823 /* !!#### maybe fix for Mule */
|
|
824 if (lock_p && !shift_p &&
|
|
825 ! (CHAR_OR_CHAR_INTP (keysym)
|
|
826 && keysym_obeys_caps_lock_p
|
|
827 ((KeySym) XCHAR_OR_CHAR_INT (keysym), d)))
|
|
828 modifiers &= (~MOD_SHIFT);
|
0
|
829
|
2
|
830 /* If this key contains two distinct keysyms, that is,
|
|
831 "shift" generates a different keysym than the
|
|
832 non-shifted key, then don't apply the shift modifier
|
|
833 bit: it's implicit. Otherwise, if there would be no
|
|
834 other way to tell the difference between the shifted
|
|
835 and unshifted version of this key, apply the shift bit.
|
|
836 Non-graphics, like Backspace and F1 get the shift bit
|
|
837 in the modifiers slot. Neither the characters "a",
|
|
838 "A", "2", nor "@" normally have the shift bit set.
|
|
839 However, "F1" normally does. */
|
|
840 if (modifiers & MOD_SHIFT)
|
|
841 {
|
|
842 int Mode_switch_p = *state & xd->ModeMask;
|
|
843 KeySym bot = XLookupKeysym (ev, Mode_switch_p ? 2 : 0);
|
|
844 KeySym top = XLookupKeysym (ev, Mode_switch_p ? 3 : 1);
|
|
845 if (top && bot && top != bot)
|
|
846 modifiers &= ~MOD_SHIFT;
|
|
847 }
|
|
848 emacs_event->event_type = key_press_event;
|
|
849 emacs_event->timestamp = ev->time;
|
|
850 emacs_event->event.key.modifiers = modifiers;
|
|
851 emacs_event->event.key.keysym = keysym;
|
|
852 }
|
|
853 else /* Mouse press/release event */
|
|
854 {
|
|
855 XButtonEvent *ev = &x_event->xbutton;
|
|
856 struct frame *frame = x_window_to_frame (d, ev->window);
|
|
857 if (! frame)
|
|
858 return 0; /* not for us */
|
|
859 XSETFRAME (emacs_event->channel, frame);
|
|
860
|
|
861 emacs_event->event_type = (x_event->type == ButtonPress) ?
|
|
862 button_press_event : button_release_event;
|
|
863
|
|
864 emacs_event->event.button.modifiers = modifiers;
|
|
865 emacs_event->timestamp = ev->time;
|
|
866 emacs_event->event.button.button = ev->button;
|
|
867 emacs_event->event.button.x = ev->x;
|
|
868 emacs_event->event.button.y = ev->y;
|
|
869 }
|
0
|
870 }
|
2
|
871 break;
|
0
|
872
|
|
873 case MotionNotify:
|
|
874 {
|
2
|
875 XMotionEvent *ev = &x_event->xmotion;
|
|
876 struct frame *frame = x_window_to_frame (d, ev->window);
|
|
877 unsigned int modifiers = 0;
|
|
878 XMotionEvent event2;
|
0
|
879
|
2
|
880 if (! frame)
|
|
881 return 0; /* not for us */
|
0
|
882
|
2
|
883 /* We use MotionHintMask, so we will get only one motion event
|
|
884 until the next time we call XQueryPointer or the user
|
|
885 clicks the mouse. So call XQueryPointer now (meaning that
|
|
886 the event will be in sync with the server just before
|
|
887 Fnext_event() returns). If the mouse is still in motion,
|
|
888 then the server will immediately generate exactly one more
|
|
889 motion event, which will be on the queue waiting for us
|
|
890 next time around. */
|
|
891 event2 = *ev;
|
|
892 if (XQueryPointer (event2.display, event2.window,
|
|
893 &event2.root, &event2.subwindow,
|
|
894 &event2.x_root, &event2.y_root,
|
|
895 &event2.x, &event2.y,
|
|
896 &event2.state))
|
|
897 ev = &event2; /* only one structure copy */
|
0
|
898
|
2
|
899 DEVICE_X_MOUSE_TIMESTAMP (d) = ev->time;
|
|
900
|
|
901 XSETFRAME (emacs_event->channel, frame);
|
|
902 emacs_event->event_type = pointer_motion_event;
|
|
903 emacs_event->timestamp = ev->time;
|
|
904 emacs_event->event.motion.x = ev->x;
|
|
905 emacs_event->event.motion.y = ev->y;
|
|
906 if (ev->state & ShiftMask) modifiers |= MOD_SHIFT;
|
|
907 if (ev->state & ControlMask) modifiers |= MOD_CONTROL;
|
|
908 if (ev->state & xd->MetaMask) modifiers |= MOD_META;
|
|
909 if (ev->state & xd->SuperMask) modifiers |= MOD_SUPER;
|
|
910 if (ev->state & xd->HyperMask) modifiers |= MOD_HYPER;
|
|
911 if (ev->state & xd->AltMask) modifiers |= MOD_ALT;
|
|
912 /* Currently ignores Shift_Lock but probably shouldn't
|
|
913 (but it definitely should ignore Caps_Lock). */
|
|
914 emacs_event->event.motion.modifiers = modifiers;
|
0
|
915 }
|
2
|
916 break;
|
0
|
917
|
|
918 case ClientMessage:
|
2
|
919 {
|
|
920 /* Patch bogus TAKE_FOCUS messages from MWM; CurrentTime is
|
|
921 passed as the timestamp of the TAKE_FOCUS, which the ICCCM
|
|
922 explicitly prohibits. */
|
|
923 XClientMessageEvent *ev = &x_event->xclient;
|
|
924 if (ev->message_type == DEVICE_XATOM_WM_PROTOCOLS (d)
|
|
925 && ev->data.l[0] == DEVICE_XATOM_WM_TAKE_FOCUS (d)
|
|
926 && ev->data.l[1] == 0)
|
|
927 {
|
|
928 ev->data.l[1] = DEVICE_X_LAST_SERVER_TIMESTAMP (d);
|
|
929 }
|
|
930 }
|
|
931 /* fall through */
|
0
|
932
|
|
933 default: /* it's a magic event */
|
|
934 {
|
2
|
935 struct frame *frame;
|
|
936 Window w;
|
|
937
|
|
938 switch (x_event->type)
|
|
939 {
|
|
940 /* Note: the number of cases could be reduced to two or
|
|
941 three by using xany.window, but it's perhaps clearer
|
|
942 and potentially more robust this way */
|
|
943 case SelectionRequest: w = x_event->xselectionrequest.owner; break;
|
|
944 case SelectionClear: w = x_event->xselectionclear.window; break;
|
|
945 case SelectionNotify: w = x_event->xselection.requestor; break;
|
|
946 case PropertyNotify: w = x_event->xproperty.window; break;
|
|
947 case ClientMessage: w = x_event->xclient.window; break;
|
|
948 case ConfigureNotify: w = x_event->xconfigure.window; break;
|
|
949 case Expose:
|
|
950 case GraphicsExpose: w = x_event->xexpose.window; break;
|
|
951 case MapNotify:
|
|
952 case UnmapNotify: w = x_event->xmap.window; break;
|
|
953 case EnterNotify:
|
|
954 case LeaveNotify: w = x_event->xcrossing.window; break;
|
|
955 case FocusIn:
|
|
956 case FocusOut: w = x_event->xfocus.window; break;
|
|
957 case VisibilityNotify: w = x_event->xvisibility.window; break;
|
|
958 default: w = x_event->xany.window; break;
|
|
959 }
|
|
960 frame = x_any_window_to_frame (d, w);
|
|
961
|
|
962 if (!frame)
|
|
963 return 0;
|
0
|
964
|
2
|
965 emacs_event->event_type = magic_event;
|
|
966 XSETFRAME (emacs_event->channel, frame);
|
|
967 emacs_event->event.magic.underlying_x_event = *x_event;
|
|
968 break;
|
0
|
969 }
|
|
970 }
|
|
971 return 1;
|
|
972 }
|
|
973
|
|
974
|
|
975 /************************************************************************/
|
|
976 /* magic-event handling */
|
|
977 /************************************************************************/
|
|
978
|
|
979 static void
|
|
980 handle_focus_event_1 (struct frame *f, int in_p)
|
|
981 {
|
|
982 /* On focus change, clear all memory of sticky modifiers
|
|
983 to avoid non-intuitive behavior. */
|
|
984 clear_sticky_modifiers (XDEVICE (FRAME_DEVICE (f)));
|
|
985
|
|
986 /* We don't want to handle the focus change now, because we might
|
|
987 be in an accept-process-output, sleep-for, or sit-for. So
|
|
988 we enqueue it.
|
|
989
|
|
990 Actually, we half handle it: we handle it as far as changing the
|
|
991 box cursor for redisplay, but we don't call any hooks or do any
|
|
992 select-frame stuff until after the sit-for.
|
|
993 */
|
|
994 {
|
|
995 Lisp_Object frm;
|
|
996 Lisp_Object conser;
|
|
997 struct gcpro gcpro1;
|
|
998
|
|
999 XSETFRAME (frm, f);
|
|
1000 conser = Fcons (frm, Fcons (FRAME_DEVICE (f), in_p ? Qt : Qnil));
|
|
1001 GCPRO1 (conser);
|
|
1002 emacs_handle_focus_change_preliminary (conser);
|
|
1003 enqueue_magic_eval_event (emacs_handle_focus_change_final,
|
|
1004 conser);
|
|
1005 UNGCPRO;
|
|
1006 }
|
|
1007 }
|
|
1008
|
|
1009 /* This is called from the external-widget code */
|
|
1010
|
|
1011 void emacs_Xt_handle_focus_event (XEvent *event);
|
|
1012 void
|
|
1013 emacs_Xt_handle_focus_event (XEvent *event)
|
|
1014 {
|
|
1015 /*
|
|
1016 * It's curious that we're using x_any_window_to_frame() instead
|
|
1017 * of x_window_to_frame(). I don't know what the impact of this is.
|
|
1018 */
|
|
1019
|
|
1020 struct frame *f =
|
|
1021 x_any_window_to_frame (get_device_from_display (event->xany.display),
|
|
1022 event->xfocus.window);
|
|
1023 if (!f)
|
|
1024 /* focus events are sometimes generated just before
|
|
1025 a frame is destroyed. */
|
|
1026 return;
|
2
|
1027 handle_focus_event_1 (f, event->type == FocusIn);
|
0
|
1028 }
|
|
1029
|
|
1030 static void
|
|
1031 handle_map_event (struct frame *f, XEvent *event)
|
|
1032 {
|
|
1033 Lisp_Object frame = Qnil;
|
|
1034
|
|
1035 XSETFRAME (frame, f);
|
2
|
1036 if (event->type == MapNotify)
|
0
|
1037 {
|
|
1038 XWindowAttributes xwa;
|
|
1039
|
|
1040 /* Bleagh!!!!!! Apparently some window managers (e.g. MWM)
|
|
1041 send synthetic MapNotify events when a window is first
|
|
1042 created, EVENT IF IT'S CREATED ICONIFIED OR INVISIBLE.
|
|
1043 Or something like that. We initially tried a different
|
|
1044 solution below, but that ran into a different window-
|
|
1045 manager bug.
|
|
1046
|
|
1047 It seems that the only reliable way is to treat a
|
|
1048 MapNotify event as a "hint" that the window might or
|
|
1049 might not be visible, and check explicitly. */
|
|
1050
|
|
1051 XGetWindowAttributes (event->xany.display, event->xmap.window,
|
|
1052 &xwa);
|
|
1053 if (xwa.map_state != IsViewable)
|
|
1054 {
|
|
1055 /* Calling Fframe_iconified_p is the only way we have to
|
|
1056 correctly update FRAME_ICONIFIED_P */
|
|
1057 Fframe_iconified_p (frame);
|
|
1058 return;
|
|
1059 }
|
|
1060
|
|
1061 FRAME_X_TOTALLY_VISIBLE_P (f) = 1;
|
|
1062 #if 0
|
|
1063 /* Bleagh again!!!! We initially tried the following hack
|
|
1064 around the MWM problem, but it turns out that TWM
|
|
1065 has a race condition when you un-iconify, where it maps
|
|
1066 the window and then tells the server that the window
|
|
1067 is un-iconified. Usually, XEmacs wakes up between
|
|
1068 those two occurrences, and thus thinks that un-iconified
|
|
1069 windows are still iconified.
|
|
1070
|
|
1071 Ah, the joys of X. */
|
|
1072
|
|
1073 /* By Emacs definition, a frame that is iconified is not
|
|
1074 visible. Marking a frame as visible will automatically cause
|
|
1075 frame-iconified-p to return nil, regardless of whether the
|
|
1076 frame is actually iconified. Therefore, we have to ignore
|
|
1077 MapNotify events on iconified frames. (It's not obvious
|
|
1078 to me why these are being sent, but it happens at startup
|
|
1079 with frames that are initially iconified; perhaps they are
|
|
1080 synthetic MapNotify events coming from the window manager.)
|
|
1081 Note that `frame-iconified-p' queries the server
|
|
1082 to determine whether the frame is currently iconified,
|
|
1083 rather than consulting some internal (and likely
|
|
1084 inaccurate) state flag. Therefore, ignoring the MapNotify
|
|
1085 is correct. */
|
|
1086 if (!f->visible && NILP (Fframe_iconified_p (frame)))
|
|
1087 #endif
|
|
1088 if (!f->visible)
|
|
1089 {
|
|
1090 f->visible = 1;
|
|
1091 /* This improves the double flicker when uniconifying a frame
|
|
1092 some. A lot of it is not showing a buffer which has changed
|
|
1093 while the frame was iconified. To fix it further requires
|
|
1094 the good 'ol double redisplay structure. */
|
|
1095 MARK_FRAME_WINDOWS_STRUCTURE_CHANGED (f);
|
|
1096 va_run_hook_with_args (Qmap_frame_hook, 1, frame);
|
|
1097 #ifdef EPOCH
|
|
1098 dispatch_epoch_event (f, event, Qx_map);
|
|
1099 #endif
|
|
1100 }
|
|
1101 }
|
|
1102 else
|
|
1103 {
|
|
1104 FRAME_X_TOTALLY_VISIBLE_P (f) = 0;
|
|
1105 if (f->visible)
|
|
1106 {
|
|
1107 f->visible = 0;
|
|
1108 va_run_hook_with_args (Qunmap_frame_hook, 1, frame);
|
|
1109 #ifdef EPOCH
|
|
1110 dispatch_epoch_event (f, event, Qx_unmap);
|
|
1111 #endif
|
|
1112 }
|
|
1113
|
|
1114 /* Calling Fframe_iconified_p is the only way we have to
|
|
1115 correctly update FRAME_ICONIFIED_P */
|
|
1116 Fframe_iconified_p (frame);
|
|
1117 }
|
|
1118 }
|
|
1119
|
|
1120 static void
|
|
1121 handle_client_message (struct frame *f, XEvent *event)
|
|
1122 {
|
|
1123 struct device *d = XDEVICE (FRAME_DEVICE (f));
|
|
1124 Lisp_Object frame = Qnil;
|
|
1125
|
|
1126 XSETFRAME (frame, f);
|
|
1127
|
|
1128 if (event->xclient.message_type == DEVICE_XATOM_WM_PROTOCOLS (d) &&
|
|
1129 event->xclient.data.l[0] == DEVICE_XATOM_WM_DELETE_WINDOW (d))
|
|
1130 {
|
|
1131 /* WM_DELETE_WINDOW is a misc-user event, but other ClientMessages,
|
|
1132 such as WM_TAKE_FOCUS, are eval events. That's because delete-window
|
|
1133 was probably executed with a mouse click, while the others could
|
|
1134 have been sent as a result of mouse motion or some other implicit
|
|
1135 action. (Call this a "heuristic"...) The reason for caring about
|
|
1136 this is so that clicking on the close-box will make emacs prompt
|
|
1137 using a dialog box instead of the minibuffer if there are unsaved
|
|
1138 buffers.
|
|
1139 */
|
|
1140 enqueue_misc_user_event (frame, Qeval,
|
|
1141 list3 (Qdelete_frame, frame, Qt));
|
|
1142 }
|
|
1143 else if (event->xclient.message_type == DEVICE_XATOM_WM_PROTOCOLS (d) &&
|
|
1144 event->xclient.data.l[0] == DEVICE_XATOM_WM_TAKE_FOCUS (d))
|
|
1145 {
|
|
1146 handle_focus_event_1 (f, 1);
|
|
1147 #if 0
|
|
1148 /* If there is a dialog box up, focus on it.
|
|
1149
|
|
1150 #### Actually, we're raising it too, which is wrong. We should
|
|
1151 #### just focus on it, but lwlib doesn't currently give us an
|
|
1152 #### easy way to do that. This should be fixed.
|
|
1153 */
|
|
1154 unsigned long take_focus_timestamp = event->xclient.data.l[1];
|
|
1155 Widget widget = lw_raise_all_pop_up_widgets ();
|
|
1156 if (widget)
|
|
1157 {
|
|
1158 /* kludge: raise_all returns bottommost widget, but we really
|
|
1159 want the topmost. So just raise it for now. */
|
|
1160 XMapRaised (XtDisplay (widget), XtWindow (widget));
|
|
1161 /* Grab the focus with the timestamp of the TAKE_FOCUS. */
|
|
1162 XSetInputFocus (XtDisplay (widget), XtWindow (widget),
|
|
1163 RevertToParent, take_focus_timestamp);
|
|
1164 }
|
|
1165 #endif
|
|
1166 }
|
|
1167 #ifdef EPOCH
|
|
1168 dispatch_epoch_event (f, event, Qx_client_message);
|
|
1169 #endif
|
|
1170 }
|
|
1171
|
|
1172 static void
|
|
1173 emacs_Xt_handle_magic_event (struct Lisp_Event *emacs_event)
|
|
1174 {
|
|
1175 /* This function can GC */
|
|
1176 XEvent *event = (XEvent *) &emacs_event->event.magic.underlying_x_event;
|
|
1177 struct frame *f = XFRAME (EVENT_CHANNEL (emacs_event));
|
|
1178
|
|
1179 if (!FRAME_LIVE_P (f))
|
|
1180 return;
|
|
1181
|
|
1182 switch (event->type)
|
|
1183 {
|
|
1184 case SelectionRequest:
|
|
1185 x_handle_selection_request (&event->xselectionrequest);
|
|
1186 break;
|
|
1187
|
|
1188 case SelectionClear:
|
|
1189 x_handle_selection_clear (&event->xselectionclear);
|
|
1190 break;
|
|
1191
|
|
1192 case SelectionNotify:
|
|
1193 x_handle_selection_notify (&event->xselection);
|
|
1194 break;
|
|
1195
|
|
1196 case PropertyNotify:
|
|
1197 x_handle_property_notify (&event->xproperty);
|
|
1198 #ifdef EPOCH
|
|
1199 dispatch_epoch_event (f, event, Qx_property_change);
|
|
1200 #endif
|
|
1201 break;
|
|
1202
|
|
1203 case Expose:
|
|
1204 x_redraw_exposed_area (f, event->xexpose.x, event->xexpose.y,
|
|
1205 event->xexpose.width, event->xexpose.height);
|
|
1206 break;
|
|
1207
|
|
1208 case GraphicsExpose: /* This occurs when an XCopyArea's source area was
|
|
1209 obscured or not available. */
|
|
1210 x_redraw_exposed_area (f, event->xexpose.x, event->xexpose.y,
|
|
1211 event->xexpose.width, event->xexpose.height);
|
|
1212 break;
|
|
1213
|
|
1214 case MapNotify:
|
|
1215 case UnmapNotify:
|
|
1216 handle_map_event (f, event);
|
|
1217 break;
|
|
1218
|
|
1219 case EnterNotify:
|
|
1220 if (event->xcrossing.detail != NotifyInferior)
|
|
1221 {
|
|
1222 Lisp_Object frame;
|
|
1223
|
|
1224 XSETFRAME (frame, f);
|
|
1225 /* FRAME_X_MOUSE_P (f) = 1; */
|
|
1226 va_run_hook_with_args (Qmouse_enter_frame_hook, 1, frame);
|
|
1227 }
|
|
1228 break;
|
|
1229
|
|
1230 case LeaveNotify:
|
|
1231 if (event->xcrossing.detail != NotifyInferior)
|
|
1232 {
|
|
1233 Lisp_Object frame;
|
|
1234
|
|
1235 XSETFRAME (frame, f);
|
|
1236 /* FRAME_X_MOUSE_P (f) = 0; */
|
|
1237 va_run_hook_with_args (Qmouse_leave_frame_hook, 1, frame);
|
|
1238 }
|
|
1239 break;
|
|
1240
|
|
1241 case FocusIn:
|
|
1242 case FocusOut:
|
|
1243 #ifdef EXTERNAL_WIDGET
|
|
1244 /* External widget lossage: Ben said:
|
|
1245 YUCK. The only way to make focus changes work properly is to
|
|
1246 completely ignore all FocusIn/FocusOut events and depend only
|
|
1247 on notifications from the ExternalClient widget. */
|
|
1248 if (FRAME_X_EXTERNAL_WINDOW_P (f))
|
|
1249 break;
|
|
1250 #endif
|
2
|
1251 handle_focus_event_1 (f, event->type == FocusIn);
|
0
|
1252 break;
|
|
1253
|
|
1254 case ClientMessage:
|
|
1255 handle_client_message (f, event);
|
|
1256 break;
|
|
1257
|
|
1258 case VisibilityNotify: /* window visiblity has changed */
|
|
1259 if (event->xvisibility.state == VisibilityUnobscured)
|
|
1260 FRAME_X_TOTALLY_VISIBLE_P (f) = 1;
|
|
1261 else
|
|
1262 FRAME_X_TOTALLY_VISIBLE_P (f) = 0;
|
|
1263 break;
|
|
1264
|
|
1265 case ConfigureNotify:
|
2
|
1266 #ifdef HAVE_XIM
|
|
1267 XIC_SetGeometry (f);
|
|
1268 #endif
|
|
1269 /* ### If the following code fails to work, simply always call
|
|
1270 x_smash_bastardly_shell_position always. In this case we no
|
|
1271 longer rely on the data in the events, merely on their
|
|
1272 occurrence. */
|
|
1273 /* ### Well, actually we shouldn't have to ever call
|
|
1274 x_smash_bastardly_shell_position. We should just call
|
|
1275 XtTranslateCoordinates and only access the core.{x,y} fields
|
|
1276 using XtGetValue -- mrb */
|
|
1277 {
|
|
1278 XConfigureEvent *ev = &event->xconfigure;
|
|
1279 if (ev->window == XtWindow (FRAME_X_SHELL_WIDGET (f)) &&
|
|
1280 ! (ev->x == 0 && ev->y == 0 && !ev->send_event))
|
|
1281 {
|
|
1282 FRAME_X_SHELL_WIDGET (f)->core.x = ev->x;
|
|
1283 FRAME_X_SHELL_WIDGET (f)->core.y = ev->y;
|
|
1284 }
|
|
1285 }
|
0
|
1286 break;
|
|
1287
|
|
1288 default:
|
|
1289 break;
|
|
1290 }
|
|
1291 }
|
|
1292
|
|
1293
|
|
1294 /************************************************************************/
|
|
1295 /* timeout events */
|
|
1296 /************************************************************************/
|
|
1297
|
|
1298 static int timeout_id_tick;
|
|
1299
|
|
1300 /* Xt interval id's might not fit into an int (they're pointers, as it
|
|
1301 happens), so we need to provide a conversion list. */
|
|
1302
|
|
1303 struct Xt_timeout
|
|
1304 {
|
|
1305 int id;
|
|
1306 XtIntervalId interval_id;
|
|
1307 struct Xt_timeout *next;
|
|
1308 } *pending_timeouts, *completed_timeouts;
|
|
1309
|
|
1310 struct Xt_timeout_blocktype
|
|
1311 {
|
|
1312 Blocktype_declare (struct Xt_timeout);
|
|
1313 } *the_Xt_timeout_blocktype;
|
|
1314
|
|
1315 /* called by XtAppNextEvent() */
|
|
1316 static void
|
|
1317 Xt_timeout_callback (XtPointer closure, XtIntervalId *id)
|
|
1318 {
|
|
1319 struct Xt_timeout *timeout = (struct Xt_timeout *) closure;
|
|
1320 struct Xt_timeout *t2 = pending_timeouts;
|
|
1321 /* Remove this one from the list of pending timeouts */
|
|
1322 if (t2 == timeout)
|
|
1323 pending_timeouts = pending_timeouts->next;
|
|
1324 else
|
|
1325 {
|
|
1326 while (t2->next && t2->next != timeout) t2 = t2->next;
|
|
1327 assert (t2->next);
|
|
1328 t2->next = t2->next->next;
|
|
1329 }
|
|
1330 /* Add this one to the list of completed timeouts */
|
|
1331 timeout->next = completed_timeouts;
|
|
1332 completed_timeouts = timeout;
|
|
1333 }
|
|
1334
|
|
1335 static int
|
|
1336 emacs_Xt_add_timeout (EMACS_TIME thyme)
|
|
1337 {
|
|
1338 struct Xt_timeout *timeout = Blocktype_alloc (the_Xt_timeout_blocktype);
|
|
1339 EMACS_TIME current_time;
|
|
1340 int milliseconds;
|
|
1341
|
|
1342 timeout->id = timeout_id_tick++;
|
|
1343 timeout->next = pending_timeouts;
|
|
1344 pending_timeouts = timeout;
|
|
1345 EMACS_GET_TIME (current_time);
|
|
1346 EMACS_SUB_TIME (thyme, thyme, current_time);
|
|
1347 milliseconds = EMACS_SECS (thyme) * 1000 +
|
|
1348 EMACS_USECS (thyme) / 1000;
|
|
1349 if (milliseconds < 1)
|
|
1350 milliseconds = 1;
|
|
1351 timeout->interval_id = XtAppAddTimeOut (Xt_app_con, milliseconds,
|
|
1352 Xt_timeout_callback,
|
|
1353 (XtPointer) timeout);
|
|
1354 return timeout->id;
|
|
1355 }
|
|
1356
|
|
1357 static void
|
|
1358 emacs_Xt_remove_timeout (int id)
|
|
1359 {
|
|
1360 struct Xt_timeout *timeout, *t2;
|
|
1361
|
|
1362 /* Find the timeout on the list of pending ones, if it's still there. */
|
|
1363 if (!pending_timeouts) return;
|
|
1364 if (id == pending_timeouts->id)
|
|
1365 {
|
|
1366 timeout = pending_timeouts;
|
|
1367 pending_timeouts = pending_timeouts->next;
|
|
1368 }
|
|
1369 else
|
|
1370 {
|
|
1371 t2 = pending_timeouts;
|
|
1372 while (t2->next && t2->next->id != id) t2 = t2->next;
|
|
1373 if (! t2->next) return;
|
|
1374 timeout = t2->next;
|
|
1375 t2->next = t2->next->next;
|
|
1376 }
|
|
1377
|
|
1378 /* At this point, we've found the thing on the list of pending timeouts,
|
|
1379 and removed it.
|
|
1380 */
|
|
1381
|
|
1382 XtRemoveTimeOut (timeout->interval_id);
|
|
1383 Blocktype_free (the_Xt_timeout_blocktype, timeout);
|
|
1384 }
|
|
1385
|
|
1386 static void
|
|
1387 Xt_timeout_to_emacs_event (struct Lisp_Event *emacs_event)
|
|
1388 {
|
|
1389 struct Xt_timeout *timeout = completed_timeouts;
|
|
1390 assert (timeout);
|
|
1391 completed_timeouts = completed_timeouts->next;
|
|
1392 emacs_event->event_type = timeout_event;
|
|
1393 /* timeout events have nil as channel */
|
|
1394 emacs_event->timestamp = 0; /* #### wrong!! */
|
|
1395 emacs_event->event.timeout.interval_id = timeout->id;
|
|
1396 Blocktype_free (the_Xt_timeout_blocktype, timeout);
|
|
1397 }
|
|
1398
|
|
1399
|
|
1400 /************************************************************************/
|
|
1401 /* process and tty events */
|
|
1402 /************************************************************************/
|
|
1403
|
|
1404 struct what_is_ready_closure
|
|
1405 {
|
|
1406 int fd;
|
|
1407 Lisp_Object what;
|
|
1408 XtInputId id;
|
|
1409 };
|
|
1410
|
|
1411 static Lisp_Object *filedesc_with_input;
|
|
1412 static struct what_is_ready_closure **filedesc_to_what_closure;
|
|
1413
|
|
1414 static void
|
|
1415 init_what_input_once (void)
|
|
1416 {
|
|
1417 int i;
|
|
1418
|
|
1419 filedesc_with_input = (Lisp_Object *)
|
|
1420 xmalloc (MAXDESC * sizeof (Lisp_Object));
|
|
1421 filedesc_to_what_closure = (struct what_is_ready_closure **)
|
|
1422 xmalloc (MAXDESC * sizeof (struct what_is_ready_closure *));
|
|
1423
|
|
1424 for (i = 0; i < MAXDESC; i++)
|
|
1425 {
|
|
1426 filedesc_to_what_closure[i] = 0;
|
|
1427 filedesc_with_input[i] = Qnil;
|
|
1428 }
|
|
1429
|
|
1430 process_events_occurred = 0;
|
|
1431 tty_events_occurred = 0;
|
|
1432 }
|
|
1433
|
|
1434 static void
|
|
1435 mark_what_as_being_ready (struct what_is_ready_closure *closure)
|
|
1436 {
|
|
1437 if (NILP (filedesc_with_input[closure->fd]))
|
|
1438 {
|
|
1439 SELECT_TYPE temp_mask;
|
|
1440 FD_ZERO (&temp_mask);
|
|
1441 FD_SET (closure->fd, &temp_mask);
|
|
1442 /* Check to make sure there's *really* input available.
|
|
1443 Sometimes things seem to get confused and this gets called
|
|
1444 for the tty fd when there's really only input available
|
|
1445 on some process's fd. (It will subsequently get called
|
|
1446 for that process's fd, so returning without setting any
|
|
1447 flags will take care of it.) To see the problem, uncomment
|
|
1448 the stderr_out below, turn NORMAL_QUIT_CHECK_TIMEOUT_MSECS
|
|
1449 down to 25, do sh -c 'xemacs -nw -q -f shell 2>/tmp/log'
|
|
1450 and press return repeatedly. (Seen under AIX & Linux.)
|
|
1451 -dkindred@cs.cmu.edu */
|
|
1452 if (!poll_fds_for_input (temp_mask))
|
|
1453 {
|
|
1454 #if 0
|
|
1455 stderr_out ("mark_what_as_being_ready: no input available (fd=%d)\n",
|
|
1456 closure->fd);
|
|
1457 #endif
|
|
1458 return;
|
|
1459 }
|
|
1460 filedesc_with_input[closure->fd] = closure->what;
|
|
1461 if (PROCESSP (closure->what))
|
|
1462 /* Don't increment this if the current process is already marked
|
|
1463 * as having input. */
|
|
1464 process_events_occurred++;
|
|
1465 else
|
|
1466 tty_events_occurred++;
|
|
1467 }
|
|
1468 }
|
|
1469
|
|
1470 static void
|
|
1471 Xt_what_callback (void *closure, int *source, XtInputId *id)
|
|
1472 {
|
|
1473 /* If closure is 0, then we got a fake event from a signal handler.
|
|
1474 The only purpose of this is to make XtAppProcessEvent() stop
|
|
1475 blocking. */
|
|
1476 if (closure)
|
|
1477 mark_what_as_being_ready ((struct what_is_ready_closure *) closure);
|
|
1478 else
|
|
1479 {
|
|
1480 fake_event_occurred++;
|
|
1481 drain_signal_event_pipe ();
|
|
1482 }
|
|
1483 }
|
|
1484
|
|
1485 static void
|
|
1486 select_filedesc (int fd, Lisp_Object what)
|
|
1487 {
|
|
1488 struct what_is_ready_closure *closure;
|
|
1489
|
|
1490 /* If somebody is trying to select something that's already selected
|
|
1491 for, then something went wrong. The generic routines ought to
|
|
1492 detect this and error before here. */
|
|
1493 assert (!filedesc_to_what_closure[fd]);
|
|
1494
|
|
1495 closure = (struct what_is_ready_closure *) xmalloc (sizeof (*closure));
|
|
1496 closure->fd = fd;
|
|
1497 closure->what = what;
|
|
1498 closure->id =
|
|
1499 XtAppAddInput (Xt_app_con, fd,
|
|
1500 (XtPointer) (XtInputReadMask /* | XtInputExceptMask */),
|
|
1501 Xt_what_callback, closure);
|
|
1502 filedesc_to_what_closure[fd] = closure;
|
|
1503 }
|
|
1504
|
|
1505 static void
|
|
1506 unselect_filedesc (int fd)
|
|
1507 {
|
|
1508 struct what_is_ready_closure *closure = filedesc_to_what_closure[fd];
|
|
1509
|
|
1510 assert (closure);
|
|
1511 if (!NILP (filedesc_with_input[fd]))
|
|
1512 {
|
|
1513 /* We are unselecting this process before we have drained the rest of
|
|
1514 the input from it, probably from status_notify() in the command loop.
|
|
1515 This can happen like so:
|
|
1516
|
|
1517 - We are waiting in XtAppNextEvent()
|
|
1518 - Process generates output
|
|
1519 - Process is marked as being ready
|
|
1520 - Process dies, SIGCHLD gets generated before we return (!?)
|
|
1521 It could happen I guess.
|
|
1522 - sigchld_handler() marks process as dead
|
|
1523 - Somehow we end up getting a new KeyPress event on the queue
|
|
1524 at the same time (I'm really so sure how that happens but I'm
|
|
1525 not sure it can't either so let's assume it can...).
|
|
1526 - Key events have priority so we return that instead of the proc.
|
|
1527 - Before dispatching the lisp key event we call status_notify()
|
|
1528 - Which deselects the process that SIGCHLD marked as dead.
|
|
1529
|
|
1530 Thus we never remove it from _with_input and turn it into a lisp
|
|
1531 event, so we need to do it here. But this does not mean that we're
|
|
1532 throwing away the last block of output - status_notify() has already
|
|
1533 taken care of running the proc filter or whatever.
|
|
1534 */
|
|
1535 filedesc_with_input[fd] = Qnil;
|
|
1536 if (PROCESSP (closure->what))
|
|
1537 {
|
|
1538 assert (process_events_occurred > 0);
|
|
1539 process_events_occurred--;
|
|
1540 }
|
|
1541 else
|
|
1542 {
|
|
1543 assert (tty_events_occurred > 0);
|
|
1544 tty_events_occurred--;
|
|
1545 }
|
|
1546 }
|
|
1547 XtRemoveInput (closure->id);
|
|
1548 xfree (closure);
|
|
1549 filedesc_to_what_closure[fd] = 0;
|
|
1550 }
|
|
1551
|
|
1552 static void
|
|
1553 emacs_Xt_select_process (struct Lisp_Process *p)
|
|
1554 {
|
|
1555 int infd;
|
|
1556 Lisp_Object process;
|
|
1557
|
|
1558 infd = event_stream_unixoid_select_process (p);
|
|
1559
|
|
1560 XSETPROCESS (process, p);
|
|
1561 select_filedesc (infd, process);
|
|
1562 }
|
|
1563
|
|
1564 static void
|
|
1565 emacs_Xt_unselect_process (struct Lisp_Process *p)
|
|
1566 {
|
|
1567 int infd;
|
|
1568
|
|
1569 infd = event_stream_unixoid_unselect_process (p);
|
|
1570
|
|
1571 unselect_filedesc (infd);
|
|
1572 }
|
|
1573
|
|
1574 /* This is called from GC when a process object is about to be freed.
|
|
1575 If we've still got pointers to it in this file, we're gonna lose hard.
|
|
1576 */
|
|
1577 void
|
|
1578 debug_process_finalization (struct Lisp_Process *p)
|
|
1579 {
|
|
1580 #if 0 /* #### */
|
|
1581 int i;
|
|
1582 int infd, outfd;
|
|
1583 get_process_file_descriptors (p, &infd, &outfd);
|
|
1584 /* if it still has fds, then it hasn't been killed yet. */
|
|
1585 assert (infd < 0);
|
|
1586 assert (outfd < 0);
|
|
1587 /* Better not still be in the "with input" table; we know it's got no fds. */
|
|
1588 for (i = 0; i < MAXDESC; i++)
|
|
1589 {
|
|
1590 Lisp_Object process = filedesc_fds_with_input [i];
|
|
1591 assert (!PROCESSP (process) || XPROCESS (process) != p);
|
|
1592 }
|
|
1593 #endif
|
|
1594 }
|
|
1595
|
|
1596 static void
|
|
1597 Xt_process_to_emacs_event (struct Lisp_Event *emacs_event)
|
|
1598 {
|
|
1599 int i;
|
|
1600 Lisp_Object process;
|
|
1601
|
|
1602 assert (process_events_occurred > 0);
|
|
1603 for (i = 0; i < MAXDESC; i++)
|
|
1604 {
|
|
1605 process = filedesc_with_input[i];
|
|
1606 if (PROCESSP (process))
|
|
1607 break;
|
|
1608 }
|
|
1609 assert (i < MAXDESC);
|
|
1610 filedesc_with_input[i] = Qnil;
|
|
1611 process_events_occurred--;
|
|
1612 /* process events have nil as channel */
|
|
1613 emacs_event->event_type = process_event;
|
|
1614 emacs_event->timestamp = 0; /* #### */
|
|
1615 emacs_event->event.process.process = process;
|
|
1616 }
|
|
1617
|
|
1618 static void
|
|
1619 emacs_Xt_select_console (struct console *con)
|
|
1620 {
|
|
1621 Lisp_Object console = Qnil;
|
|
1622 int infd;
|
|
1623
|
|
1624 if (CONSOLE_X_P (con))
|
|
1625 return; /* X consoles are automatically selected for when we
|
|
1626 initialize them in Xt */
|
|
1627 infd = event_stream_unixoid_select_console (con);
|
|
1628 XSETCONSOLE (console, con);
|
|
1629 select_filedesc (infd, console);
|
|
1630 }
|
|
1631
|
|
1632 static void
|
|
1633 emacs_Xt_unselect_console (struct console *con)
|
|
1634 {
|
|
1635 Lisp_Object console = Qnil;
|
|
1636 int infd;
|
|
1637
|
|
1638 if (CONSOLE_X_P (con))
|
|
1639 return; /* X consoles are automatically selected for when we
|
|
1640 initialize them in Xt */
|
|
1641 infd = event_stream_unixoid_unselect_console (con);
|
|
1642 XSETCONSOLE (console, con);
|
|
1643 unselect_filedesc (infd);
|
|
1644 }
|
|
1645
|
|
1646 /* read an event from a tty, if one is available. Returns non-zero
|
|
1647 if an event was available. Note that when this function is
|
|
1648 called, there should always be a tty marked as ready for input.
|
|
1649 However, the input condition might actually be EOF, so there
|
|
1650 may not really be any input available. (In this case,
|
|
1651 read_event_from_tty_or_stream_desc() will arrange for the TTY device
|
|
1652 to be deleted.) */
|
|
1653
|
|
1654 static int
|
|
1655 Xt_tty_to_emacs_event (struct Lisp_Event *emacs_event)
|
|
1656 {
|
|
1657 int i;
|
|
1658
|
|
1659 assert (tty_events_occurred > 0);
|
|
1660 for (i = 0; i < MAXDESC; i++)
|
|
1661 {
|
|
1662 Lisp_Object console = filedesc_with_input[i];
|
|
1663 if (CONSOLEP (console))
|
|
1664 {
|
|
1665 assert (tty_events_occurred > 0);
|
|
1666 tty_events_occurred--;
|
|
1667 filedesc_with_input[i] = Qnil;
|
|
1668 if (read_event_from_tty_or_stream_desc
|
|
1669 (emacs_event, XCONSOLE (console), i))
|
|
1670 return 1;
|
|
1671 }
|
|
1672 }
|
|
1673
|
|
1674 return 0;
|
|
1675 }
|
|
1676
|
|
1677
|
|
1678 /************************************************************************/
|
|
1679 /* debugging functions to decipher an event */
|
|
1680 /************************************************************************/
|
|
1681
|
|
1682 #ifdef DEBUG_XEMACS
|
|
1683 #include "xintrinsicp.h" /* only describe_event() needs this */
|
|
1684 #include <X11/Xproto.h> /* only describe_event() needs this */
|
|
1685
|
|
1686 static void
|
|
1687 describe_event_window (Window window, Display *display)
|
|
1688 {
|
|
1689 struct frame *f;
|
|
1690 Widget w;
|
|
1691 stderr_out (" window: 0x%x", (int) window);
|
|
1692 w = XtWindowToWidget (display, window);
|
|
1693 if (w)
|
|
1694 stderr_out (" %s", w->core.widget_class->core_class.class_name);
|
|
1695 f = x_any_window_to_frame (get_device_from_display (display), window);
|
|
1696 if (f) {
|
|
1697 char buf[500];
|
|
1698 sprintf (buf, " \"%s\"", string_data (XSTRING (f->name)));
|
|
1699 write_string_to_stdio_stream (stderr, 0, (Bufbyte *) buf, 0, strlen (buf),
|
|
1700 FORMAT_DISPLAY);
|
|
1701 }
|
|
1702 stderr_out ("\n");
|
|
1703 }
|
|
1704
|
|
1705 static CONST char *
|
|
1706 XEvent_mode_to_string (int mode)
|
|
1707 {
|
|
1708 switch (mode)
|
|
1709 {
|
2
|
1710 case NotifyNormal: return "Normal";
|
|
1711 case NotifyGrab: return "Grab";
|
|
1712 case NotifyUngrab: return "Ungrab";
|
0
|
1713 case NotifyWhileGrabbed: return "WhileGrabbed";
|
2
|
1714 default: return "???";
|
0
|
1715 }
|
|
1716 }
|
|
1717
|
|
1718 static CONST char *
|
|
1719 XEvent_detail_to_string (int detail)
|
|
1720 {
|
|
1721 switch (detail)
|
|
1722 {
|
2
|
1723 case NotifyAncestor: return "Ancestor";
|
|
1724 case NotifyInferior: return "Inferior";
|
|
1725 case NotifyNonlinear: return "Nonlinear";
|
|
1726 case NotifyNonlinearVirtual: return "NonlinearVirtual";
|
|
1727 case NotifyPointer: return "Pointer";
|
|
1728 case NotifyPointerRoot: return "PointerRoot";
|
|
1729 case NotifyDetailNone: return "DetailNone";
|
|
1730 default: return "???";
|
0
|
1731 }
|
|
1732 }
|
|
1733
|
|
1734 static CONST char *
|
|
1735 XEvent_visibility_to_string (int state)
|
|
1736 {
|
|
1737 switch (state)
|
|
1738 {
|
2
|
1739 case VisibilityFullyObscured: return "FullyObscured";
|
0
|
1740 case VisibilityPartiallyObscured: return "PartiallyObscured";
|
2
|
1741 case VisibilityUnobscured: return "Unobscured";
|
|
1742 default: return "???";
|
0
|
1743 }
|
|
1744 }
|
|
1745
|
|
1746 static void
|
|
1747 describe_event (XEvent *event)
|
|
1748 {
|
|
1749 char buf[100];
|
|
1750 struct device *d = get_device_from_display (event->xany.display);
|
|
1751
|
|
1752 sprintf (buf, "%s%s", x_event_name (event->type),
|
|
1753 event->xany.send_event ? " (send)" : "");
|
|
1754 stderr_out ("%-30s", buf);
|
|
1755 switch (event->type)
|
|
1756 {
|
|
1757 case FocusIn:
|
|
1758 case FocusOut:
|
|
1759 {
|
|
1760 XFocusChangeEvent *ev = &event->xfocus;
|
|
1761 describe_event_window (ev->window, ev->display);
|
|
1762 stderr_out (" mode: %s\n", XEvent_mode_to_string (ev->mode));
|
|
1763 stderr_out (" detail: %s\n", XEvent_detail_to_string(ev->detail));
|
|
1764 break;
|
|
1765 }
|
|
1766
|
|
1767 case KeyPress:
|
|
1768 {
|
|
1769 Lisp_Object keysym;
|
|
1770 XKeyEvent *ev = &event->xkey;
|
|
1771 unsigned int state = ev->state;
|
|
1772
|
|
1773 describe_event_window (ev->window, ev->display);
|
|
1774 stderr_out (" subwindow: %ld\n", ev->subwindow);
|
|
1775 stderr_out (" state: ");
|
|
1776 if (state & ShiftMask) stderr_out ("Shift ");
|
|
1777 if (state & LockMask) stderr_out ("Lock ");
|
|
1778 if (state & ControlMask) stderr_out ("Control ");
|
|
1779 if (state & Mod1Mask) stderr_out ("Mod1 ");
|
|
1780 if (state & Mod2Mask) stderr_out ("Mod2 ");
|
|
1781 if (state & Mod3Mask) stderr_out ("Mod3 ");
|
|
1782 if (state & Mod4Mask) stderr_out ("Mod4 ");
|
|
1783 if (state & Mod5Mask) stderr_out ("Mod5 ");
|
|
1784 #if 0 /* Apparently these don't exist? */
|
|
1785 if (state & MetaMask) stderr_out ("Meta ");
|
|
1786 if (state & SuperMask) stderr_out ("Super ");
|
|
1787 if (state & HyperMask) stderr_out ("Hyper ");
|
|
1788 if (state & AltMask) stderr_out ("Alt ");
|
|
1789 if (state & ModeMask) stderr_out ("Mode_switch ");
|
|
1790 #endif
|
|
1791
|
|
1792 if (! state)
|
|
1793 stderr_out ("vanilla\n");
|
|
1794 else
|
|
1795 stderr_out ("\n");
|
|
1796 if (x_key_is_modifier_p (ev->keycode, d))
|
|
1797 stderr_out (" Modifier key");
|
|
1798 stderr_out (" keycode: 0x%x\n", ev->keycode);
|
|
1799 keysym = x_to_emacs_keysym (event, 0);
|
|
1800 if (CHAR_OR_CHAR_INTP (keysym))
|
|
1801 {
|
2
|
1802 Emchar c = XCHAR_OR_CHAR_INT (keysym);
|
|
1803 if (c > 32 && c < 127)
|
|
1804 stderr_out (" keysym: %c\n", c);
|
0
|
1805 else
|
2
|
1806 stderr_out (" keysym: %d\n", c);
|
0
|
1807 }
|
|
1808 else
|
|
1809 stderr_out (" keysym: %s\n", string_data (XSYMBOL (keysym)->name));
|
|
1810 }
|
|
1811 break;
|
|
1812
|
|
1813 case Expose:
|
|
1814 if (x_debug_events > 1)
|
|
1815 {
|
|
1816 XExposeEvent *ev = &event->xexpose;
|
|
1817 describe_event_window (ev->window, ev->display);
|
2
|
1818 stderr_out (" region: x=%d y=%d width=%d height=%d\n",
|
|
1819 ev->x, ev->y, ev->width, ev->height);
|
0
|
1820 stderr_out (" count: %d\n", ev->count);
|
|
1821 }
|
|
1822 else
|
|
1823 stderr_out ("\n");
|
|
1824 break;
|
|
1825
|
|
1826 case GraphicsExpose:
|
|
1827 if (x_debug_events > 1)
|
|
1828 {
|
|
1829 XGraphicsExposeEvent *ev = &event->xgraphicsexpose;
|
|
1830 describe_event_window (ev->drawable, ev->display);
|
|
1831 stderr_out (" major: %s\n",
|
|
1832 (ev ->major_code == X_CopyArea ? "CopyArea" :
|
|
1833 (ev->major_code == X_CopyPlane ? "CopyPlane" : "?")));
|
2
|
1834 stderr_out (" region: x=%d y=%d width=%d height=%d\n",
|
|
1835 ev->x, ev->y, ev->width, ev->height);
|
0
|
1836 stderr_out (" count: %d\n", ev->count);
|
|
1837 }
|
|
1838 else
|
|
1839 stderr_out ("\n");
|
|
1840 break;
|
|
1841
|
|
1842 case EnterNotify:
|
|
1843 case LeaveNotify:
|
|
1844 if (x_debug_events > 1)
|
|
1845 {
|
|
1846 XCrossingEvent *ev = &event->xcrossing;
|
|
1847 describe_event_window (ev->window, ev->display);
|
|
1848 #if 0
|
|
1849 stderr_out(" subwindow: 0x%x\n", ev->subwindow);
|
|
1850 stderr_out(" pos: %d %d\n", ev->x, ev->y);
|
|
1851 stderr_out(" root pos: %d %d\n", ev->x_root, ev->y_root);
|
|
1852 #endif
|
|
1853 stderr_out(" mode: %s\n", XEvent_mode_to_string(ev->mode));
|
|
1854 stderr_out(" detail: %s\n", XEvent_detail_to_string(ev->detail));
|
|
1855 stderr_out(" focus: %d\n", ev->focus);
|
|
1856 #if 0
|
|
1857 stderr_out(" state: 0x%x\n", ev->state);
|
|
1858 #endif
|
|
1859 }
|
|
1860 else
|
|
1861 stderr_out("\n");
|
|
1862 break;
|
|
1863
|
|
1864 case ConfigureNotify:
|
|
1865 if (x_debug_events > 1)
|
|
1866 {
|
|
1867 XConfigureEvent *ev = &event->xconfigure;
|
|
1868 describe_event_window (ev->window, ev->display);
|
|
1869 stderr_out(" above: 0x%lx\n", ev->above);
|
|
1870 stderr_out(" size: %d %d %d %d\n", ev->x, ev->y,
|
|
1871 ev->width, ev->height);
|
|
1872 stderr_out(" redirect: %d\n", ev->override_redirect);
|
|
1873 }
|
|
1874 else
|
|
1875 stderr_out("\n");
|
|
1876 break;
|
|
1877
|
|
1878 case VisibilityNotify:
|
|
1879 if (x_debug_events > 1)
|
|
1880 {
|
|
1881 XVisibilityEvent *ev = &event->xvisibility;
|
|
1882 describe_event_window (ev->window, ev->display);
|
|
1883 stderr_out(" state: %s\n", XEvent_visibility_to_string(ev->state));
|
|
1884 }
|
|
1885 else
|
|
1886 stderr_out ("\n");
|
|
1887 break;
|
|
1888
|
|
1889 case ClientMessage:
|
|
1890 {
|
|
1891 XClientMessageEvent *ev = &event->xclient;
|
|
1892 char *name = XGetAtomName (ev->display, ev->message_type);
|
|
1893 stderr_out ("%s", name);
|
|
1894 if (!strcmp (name, "WM_PROTOCOLS")) {
|
|
1895 char *protname = XGetAtomName (ev->display, ev->data.l[0]);
|
|
1896 stderr_out ("(%s)", protname);
|
|
1897 XFree (protname);
|
|
1898 }
|
|
1899 XFree (name);
|
|
1900 stderr_out ("\n");
|
|
1901 break;
|
|
1902 }
|
|
1903
|
|
1904 default:
|
|
1905 stderr_out ("\n");
|
|
1906 break;
|
|
1907 }
|
|
1908
|
|
1909 fflush (stdout);
|
|
1910 }
|
|
1911
|
|
1912 #endif /* include describe_event definition */
|
|
1913
|
|
1914
|
|
1915 /************************************************************************/
|
|
1916 /* get the next event from Xt */
|
|
1917 /************************************************************************/
|
|
1918
|
|
1919 static Lisp_Object dispatch_event_queue, dispatch_event_queue_tail;
|
|
1920
|
|
1921 static void
|
|
1922 enqueue_Xt_dispatch_event (Lisp_Object event)
|
|
1923 {
|
|
1924 enqueue_event (event, &dispatch_event_queue, &dispatch_event_queue_tail);
|
|
1925 }
|
|
1926
|
|
1927 Lisp_Object
|
|
1928 dequeue_Xt_dispatch_event (void)
|
|
1929 {
|
|
1930 return dequeue_event (&dispatch_event_queue, &dispatch_event_queue_tail);
|
|
1931 }
|
|
1932
|
|
1933 /* This business exists because menu events "happen" when
|
|
1934 menubar_selection_callback() is called from somewhere deep
|
|
1935 within XtAppProcessEvent in emacs_Xt_next_event(). The
|
|
1936 callback needs to terminate the modal loop in that function
|
|
1937 or else it will continue waiting until another event is
|
|
1938 received.
|
|
1939
|
|
1940 Same business applies to scrollbar events. */
|
|
1941
|
|
1942 void
|
|
1943 signal_special_Xt_user_event (Lisp_Object channel, Lisp_Object function,
|
|
1944 Lisp_Object object)
|
|
1945 {
|
|
1946 Lisp_Object event;
|
|
1947
|
|
1948 event = Fmake_event ();
|
|
1949
|
|
1950 XEVENT (event)->event_type = misc_user_event;
|
|
1951 XEVENT (event)->channel = channel;
|
|
1952 XEVENT (event)->event.eval.function = function;
|
|
1953 XEVENT (event)->event.eval.object = object;
|
|
1954
|
|
1955 enqueue_Xt_dispatch_event (event);
|
|
1956 }
|
|
1957
|
|
1958 static void
|
|
1959 emacs_Xt_next_event (struct Lisp_Event *emacs_event)
|
|
1960 {
|
|
1961 we_didnt_get_an_event:
|
|
1962
|
|
1963 while (NILP (dispatch_event_queue) &&
|
2
|
1964 !completed_timeouts &&
|
|
1965 !fake_event_occurred &&
|
|
1966 !process_events_occurred &&
|
0
|
1967 !tty_events_occurred)
|
|
1968 {
|
|
1969
|
|
1970 /* Stupid logic in XtAppProcessEvent() dictates that, if process
|
|
1971 events and X events are both available, the process event gets
|
|
1972 taken first. This will cause an infinite loop if we're being
|
|
1973 called from Fdiscard_input().
|
|
1974 */
|
|
1975 if (XtAppPending (Xt_app_con) & XtIMXEvent)
|
|
1976 XtAppProcessEvent (Xt_app_con, XtIMXEvent);
|
|
1977 else
|
|
1978 {
|
|
1979 Lisp_Object devcons, concons;
|
|
1980
|
|
1981 /* We're about to block. Xt has a bug in it (big surprise,
|
|
1982 there) in that it blocks using select() and doesn't
|
|
1983 flush the Xlib output buffers (XNextEvent() does this
|
|
1984 automatically before blocking). So it's necessary
|
|
1985 for us to do this ourselves. If we don't do it, then
|
|
1986 display output may not be seen until the next time
|
|
1987 an X event is received. (This happens esp. with
|
|
1988 subprocess output that gets sent to a visible buffer.)
|
|
1989
|
|
1990 #### The above comment may not have any validity. */
|
|
1991
|
|
1992 DEVICE_LOOP_NO_BREAK (devcons, concons)
|
|
1993 {
|
|
1994 struct device *d;
|
|
1995 d = XDEVICE (XCAR (devcons));
|
|
1996
|
|
1997 if (DEVICE_X_P (d) && DEVICE_X_DISPLAY (d))
|
|
1998 /* emacs may be exiting */
|
|
1999 XFlush (DEVICE_X_DISPLAY (d));
|
|
2000 }
|
|
2001 XtAppProcessEvent (Xt_app_con, XtIMAll);
|
|
2002 }
|
|
2003 }
|
|
2004
|
|
2005 if (!NILP (dispatch_event_queue))
|
|
2006 {
|
|
2007 Lisp_Object event, event2;
|
|
2008 XSETEVENT (event2, emacs_event);
|
|
2009 event = dequeue_Xt_dispatch_event ();
|
|
2010 Fcopy_event (event, event2);
|
|
2011 Fdeallocate_event (event);
|
|
2012 }
|
|
2013 else if (tty_events_occurred)
|
|
2014 {
|
|
2015 if (!Xt_tty_to_emacs_event (emacs_event))
|
|
2016 goto we_didnt_get_an_event;
|
|
2017 }
|
|
2018 else if (completed_timeouts)
|
|
2019 Xt_timeout_to_emacs_event (emacs_event);
|
|
2020 else if (fake_event_occurred)
|
|
2021 {
|
|
2022 /* A dummy event, so that a cycle of the command loop will
|
|
2023 occur. */
|
|
2024 fake_event_occurred = 0;
|
|
2025 /* eval events have nil as channel */
|
|
2026 emacs_event->event_type = eval_event;
|
|
2027 emacs_event->event.eval.function = Qidentity;
|
|
2028 emacs_event->event.eval.object = Qnil;
|
|
2029 }
|
|
2030 else /* if (process_events_occurred) */
|
|
2031 Xt_process_to_emacs_event (emacs_event);
|
|
2032
|
|
2033 /* No need to call XFilterEvent; Xt does it for us */
|
|
2034 }
|
|
2035
|
|
2036 void
|
|
2037 emacs_Xt_event_handler (Widget wid /* unused */,
|
|
2038 XtPointer closure /* unused */,
|
|
2039 XEvent *event,
|
|
2040 Boolean *continue_to_dispatch /* unused */)
|
|
2041 {
|
|
2042 Lisp_Object emacs_event = Fmake_event ();
|
|
2043
|
|
2044 #ifdef DEBUG_XEMACS
|
|
2045 if (x_debug_events > 0)
|
|
2046 {
|
|
2047 describe_event (event);
|
|
2048 }
|
|
2049 #endif /* DEBUG_XEMACS */
|
|
2050 if (x_event_to_emacs_event (event, XEVENT (emacs_event)))
|
|
2051 enqueue_Xt_dispatch_event (emacs_event);
|
|
2052 else
|
|
2053 Fdeallocate_event (emacs_event);
|
|
2054 }
|
|
2055
|
|
2056
|
|
2057 /************************************************************************/
|
|
2058 /* input pending / C-g checking */
|
|
2059 /************************************************************************/
|
|
2060
|
|
2061 static Bool
|
|
2062 quit_char_predicate (Display *display, XEvent *event, XPointer data)
|
|
2063 {
|
|
2064 struct device *d = get_device_from_display (display);
|
|
2065 struct x_device *xd = DEVICE_X_DATA (d);
|
|
2066 char c, quit_char;
|
|
2067 Bool *critical = (Bool *) data;
|
|
2068 Lisp_Object keysym;
|
|
2069
|
|
2070 if (critical) *critical = False;
|
|
2071 if (event->type != KeyPress) return 0;
|
|
2072 if (! x_any_window_to_frame (d, event->xany.window)) return 0;
|
|
2073 if (event->xkey.state
|
|
2074 & (xd->MetaMask | xd->HyperMask | xd->SuperMask | xd->AltMask))
|
|
2075 return 0;
|
|
2076
|
|
2077 /* This duplicates some code that exists elsewhere, but it's relatively
|
|
2078 fast and doesn't cons.
|
|
2079 */
|
|
2080 keysym = x_to_emacs_keysym (event, 1);
|
|
2081 if (NILP (keysym)) return 0;
|
|
2082 if (CHAR_OR_CHAR_INTP (keysym))
|
|
2083 c = XCHAR_OR_CHAR_INT (keysym);
|
|
2084 /* Highly doubtful that these are the quit character, but... */
|
|
2085 else if (EQ (keysym, QKbackspace)) c = '\b';
|
|
2086 else if (EQ (keysym, QKtab)) c = '\t';
|
|
2087 else if (EQ (keysym, QKlinefeed)) c = '\n';
|
|
2088 else if (EQ (keysym, QKreturn)) c = '\r';
|
|
2089 else if (EQ (keysym, QKescape)) c = 27;
|
|
2090 else if (EQ (keysym, QKspace)) c = ' ';
|
|
2091 else if (EQ (keysym, QKdelete)) c = 127;
|
|
2092 else return 0;
|
|
2093
|
|
2094 if (event->xkey.state & xd->MetaMask) c |= 0x80;
|
|
2095 if ((event->xkey.state & ControlMask) && !(c >= 'A' && c <= 'Z'))
|
|
2096 c &= 0x1F; /* unshifted control characters */
|
|
2097 quit_char = CONSOLE_QUIT_CHAR (XCONSOLE (DEVICE_CONSOLE (d)));
|
|
2098 if (c == quit_char)
|
|
2099 return True;
|
|
2100 /* If we've got Control-Shift-G instead of Control-G, that means
|
|
2101 we have a critical_quit. Caps_Lock is its own modifier, so it
|
|
2102 won't cause ^G to act differently than before. */
|
|
2103 if (event->xkey.state & ControlMask) c &= 0x1F;
|
|
2104 if (c == quit_char)
|
|
2105 {
|
|
2106 if (critical) *critical = True;
|
|
2107 return True;
|
|
2108 }
|
|
2109 return False;
|
|
2110 }
|
|
2111
|
|
2112 /* This scans the X input queue for a KeyPress event that matches the
|
|
2113 quit character, and sets Vquit_flag. This is called from the
|
|
2114 QUIT macro to determine whether we should quit.
|
|
2115
|
|
2116 In a SIGIO world, this won't be called unless a SIGIO has happened
|
|
2117 since the last time we checked.
|
|
2118
|
|
2119 In a non-SIGIO world, this is called from emacs_Xt_event_pending_p
|
|
2120 (which is called from input_pending_p).
|
|
2121 */
|
|
2122 static void
|
|
2123 x_check_for_quit_char (Display *display)
|
|
2124 {
|
|
2125 XEvent event;
|
|
2126 int queued;
|
|
2127 Bool critical_quit = False;
|
|
2128 XEventsQueued (display, QueuedAfterReading);
|
|
2129 queued = XCheckIfEvent (display, &event,
|
|
2130 quit_char_predicate,
|
|
2131 (XtPointer)&critical_quit);
|
|
2132 if (queued)
|
|
2133 {
|
|
2134 Vquit_flag = (critical_quit ? Qcritical : Qt);
|
|
2135 /* don't put the event back onto the queue. Those functions that
|
|
2136 wanted to read a ^G directly have arranged to do this. */
|
|
2137 }
|
|
2138 }
|
|
2139
|
|
2140 static void
|
|
2141 check_for_tty_quit_char (struct device *d)
|
|
2142 {
|
|
2143 SELECT_TYPE temp_mask;
|
|
2144 int infd = DEVICE_INFD (d);
|
|
2145 struct console *con = XCONSOLE (DEVICE_CONSOLE (d));
|
|
2146 Emchar quit_char = CONSOLE_QUIT_CHAR (con);
|
|
2147
|
|
2148 FD_ZERO (&temp_mask);
|
|
2149 FD_SET (infd, &temp_mask);
|
|
2150
|
|
2151 while (1)
|
|
2152 {
|
|
2153 Lisp_Object event;
|
|
2154 Emchar the_char;
|
|
2155
|
|
2156 if (!poll_fds_for_input (temp_mask))
|
|
2157 return;
|
|
2158
|
|
2159 event = Fmake_event ();
|
|
2160 if (!read_event_from_tty_or_stream_desc (XEVENT (event), con, infd))
|
|
2161 /* EOF, or something ... */
|
|
2162 return;
|
|
2163 /* #### bogus. quit-char should be allowed to be any sort
|
|
2164 of event. */
|
|
2165 the_char = event_to_character (XEVENT (event), 1, 0, 0);
|
|
2166 if (the_char >= 0 && the_char == quit_char)
|
|
2167 {
|
|
2168 Vquit_flag = Qt;
|
|
2169 /* do not queue the C-g. See above. */
|
|
2170 return;
|
|
2171 }
|
|
2172
|
|
2173 /* queue the read event to be read for real later. */
|
|
2174 enqueue_Xt_dispatch_event (event);
|
|
2175 }
|
|
2176 }
|
|
2177
|
|
2178 static void
|
|
2179 emacs_Xt_quit_p (void)
|
|
2180 {
|
|
2181 Lisp_Object devcons, concons;
|
|
2182 CONSOLE_LOOP (concons)
|
|
2183 {
|
|
2184 struct console *con = XCONSOLE (XCAR (concons));
|
|
2185 if (!con->input_enabled)
|
|
2186 continue;
|
|
2187
|
|
2188 CONSOLE_DEVICE_LOOP (devcons, con)
|
|
2189 {
|
|
2190 struct device *d;
|
|
2191 d = XDEVICE (XCAR (devcons));
|
|
2192
|
|
2193 if (DEVICE_X_P (d) && DEVICE_X_DISPLAY (d))
|
|
2194 /* emacs may be exiting */
|
|
2195 x_check_for_quit_char (DEVICE_X_DISPLAY (d));
|
|
2196 else if (DEVICE_TTY_P (d))
|
|
2197 check_for_tty_quit_char (d);
|
|
2198 }
|
|
2199 }
|
|
2200 }
|
|
2201
|
|
2202 static void
|
|
2203 drain_X_queue (void)
|
|
2204 {
|
|
2205 while (XtAppPending (Xt_app_con) & XtIMXEvent)
|
|
2206 XtAppProcessEvent (Xt_app_con, XtIMXEvent);
|
|
2207 }
|
|
2208
|
|
2209 static int
|
|
2210 emacs_Xt_event_pending_p (int user_p)
|
|
2211 {
|
|
2212 Lisp_Object event;
|
|
2213 int tick_count_val;
|
|
2214
|
|
2215 /* If `user_p' is false, then this function returns whether there are any
|
|
2216 X, timeout, or fd events pending (that is, whether emacs_Xt_next_event()
|
|
2217 would return immediately without blocking).
|
|
2218
|
|
2219 if `user_p' is true, then this function returns whether there are any
|
|
2220 *user generated* events available (that is, whether there are keyboard
|
|
2221 or mouse-click events ready to be read). This also implies that
|
|
2222 emacs_Xt_next_event() would not block.
|
|
2223
|
|
2224 In a non-SIGIO world, this also checks whether the user has typed ^G,
|
|
2225 since this is a convenient place to do so. We don't need to do this
|
|
2226 in a SIGIO world, since input causes an interrupt.
|
|
2227 */
|
|
2228
|
|
2229 #if 0
|
|
2230 /* I don't think there's any point to this and it will nullify
|
|
2231 the speed gains achieved by the sigio_happened checking below.
|
|
2232 Its only advantage is that it may possibly make C-g response
|
|
2233 a bit faster. The C-g will be noticed within 0.25 second, anyway,
|
|
2234 even without this. */
|
|
2235 #ifndef SIGIO
|
|
2236 /* First check for C-g if necessary */
|
|
2237 emacs_Xt_quit_p ();
|
|
2238 #endif
|
|
2239 #endif
|
|
2240
|
|
2241 /* This function used to simply check whether there were any X
|
|
2242 events (or is user_p was 1, it iterated over all the pending
|
|
2243 X events using XCheckIfEvent(), looking for keystrokes and
|
|
2244 button events). That worked in the old cheesoid event loop,
|
|
2245 which didn't go through XtAppDispatchEvent(), but it doesn't
|
|
2246 work any more -- X events may not result in anything. For
|
|
2247 example, a button press in a blank part of the menubar appears
|
|
2248 as an X event but will not result in any Emacs events (a
|
|
2249 button press that activates the menubar results in an Emacs
|
|
2250 event through the stop_next_event mechanism).
|
|
2251
|
|
2252 The only accurate way of determining whether these X events
|
|
2253 translate into Emacs events is to go ahead and dispatch them
|
|
2254 until there's something on the dispatch queue. */
|
|
2255
|
|
2256 /* See if there are any user events already on the queue. */
|
|
2257 EVENT_CHAIN_LOOP (event, dispatch_event_queue)
|
|
2258 if (!user_p || command_event_p (event))
|
|
2259 return 1;
|
|
2260
|
|
2261 /* See if there's any TTY input available.
|
|
2262 */
|
|
2263 if (poll_fds_for_input (tty_only_mask))
|
|
2264 return 1;
|
|
2265
|
|
2266 if (!user_p)
|
|
2267 {
|
|
2268 /* If not user_p and there are any timer or file-desc events
|
|
2269 pending, we know there will be an event so we're through. */
|
|
2270 XtInputMask pending_value;
|
|
2271
|
|
2272 /* Note that formerly we just checked the value of XtAppPending()
|
|
2273 to determine if there was file-desc input. This doesn't
|
|
2274 work any more with the signal_event_pipe; XtAppPending()
|
|
2275 will says "yes" in this case but there isn't really any
|
|
2276 input. Another way of fixing this problem is for the
|
|
2277 signal_event_pipe to generate actual input in the form
|
|
2278 of an identity eval event or something. (#### maybe this
|
|
2279 actually happens?) */
|
|
2280
|
|
2281 if (poll_fds_for_input (process_only_mask))
|
|
2282 return 1;
|
|
2283
|
|
2284 pending_value = XtAppPending (Xt_app_con);
|
|
2285
|
|
2286 if (pending_value & XtIMTimer)
|
|
2287 return 1;
|
|
2288 }
|
|
2289
|
|
2290 /* XtAppPending() can be super-slow, esp. over a network connection.
|
|
2291 Quantify results have indicated that in some cases the
|
|
2292 call to detect_input_pending() completely dominates the
|
|
2293 running time of redisplay(). Fortunately, in a SIGIO world
|
|
2294 we can more quickly determine whether there are any X events:
|
|
2295 if an event has happened since the last time we checked, then
|
|
2296 a SIGIO will have happened. On a machine with broken SIGIO,
|
|
2297 we'll still be in an OK state -- the sigio_happened flag
|
|
2298 will get set at least once a second, so we'll be no more than
|
|
2299 one second behind reality. (In general it's OK if we
|
|
2300 erroneously report no input pending when input is actually
|
|
2301 pending() -- preemption is just a bit less efficient, that's
|
|
2302 all. It's bad bad bad if you err the other way -- you've
|
|
2303 promised that `next-event' won't block but it actually will,
|
|
2304 and some action might get delayed until the next time you
|
|
2305 hit a key.)
|
|
2306 */
|
|
2307
|
|
2308 /* quit_check_signal_tick_count is volatile so try to avoid race conditions
|
|
2309 by using a temporary variable */
|
|
2310 tick_count_val = quit_check_signal_tick_count;
|
|
2311 if (last_quit_check_signal_tick_count != tick_count_val)
|
|
2312 {
|
|
2313 last_quit_check_signal_tick_count = tick_count_val;
|
|
2314
|
|
2315 /* We need to drain the entire queue now -- if we only
|
|
2316 drain part of it, we may later on end up with events
|
|
2317 actually pending but detect_input_pending() returning
|
|
2318 false because there wasn't another SIGIO. */
|
|
2319 drain_X_queue ();
|
|
2320
|
|
2321 EVENT_CHAIN_LOOP (event, dispatch_event_queue)
|
|
2322 if (!user_p || command_event_p (event))
|
|
2323 return 1;
|
|
2324 }
|
|
2325
|
|
2326 return 0;
|
|
2327 }
|
|
2328
|
|
2329
|
|
2330 /************************************************************************/
|
|
2331 /* replacement for standard string-to-pixel converter */
|
|
2332 /************************************************************************/
|
|
2333
|
|
2334 /* This was constructed by ripping off the standard string-to-pixel
|
|
2335 converter from Converters.c in the Xt source code and modifying
|
|
2336 appropriately. */
|
|
2337
|
|
2338 #if 0
|
|
2339
|
|
2340 /* This is exported by the Xt library (at least by mine). If this
|
|
2341 isn't the case somewhere, rename this appropriately and remove
|
|
2342 the '#if 0'. Note, however, that I got "unknown structure"
|
|
2343 errors when I tried this. */
|
|
2344 XtConvertArgRec Const colorConvertArgs[] = {
|
|
2345 {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.screen),
|
|
2346 sizeof(Screen *)},
|
|
2347 {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.colormap),
|
|
2348 sizeof(Colormap)}
|
|
2349 };
|
|
2350
|
|
2351 #endif
|
|
2352
|
|
2353 #define done(type, value) \
|
|
2354 if (toVal->addr != NULL) { \
|
|
2355 if (toVal->size < sizeof(type)) { \
|
|
2356 toVal->size = sizeof(type); \
|
|
2357 return False; \
|
|
2358 } \
|
|
2359 *(type*)(toVal->addr) = (value); \
|
|
2360 } else { \
|
|
2361 static type static_val; \
|
|
2362 static_val = (value); \
|
|
2363 toVal->addr = (XPointer)&static_val; \
|
|
2364 } \
|
|
2365 toVal->size = sizeof(type); \
|
|
2366 return True /* Caller supplies `;' */
|
|
2367
|
|
2368 static
|
|
2369 Boolean EmacsXtCvtStringToPixel (
|
|
2370 Display *dpy,
|
|
2371 XrmValuePtr args,
|
|
2372 Cardinal *num_args,
|
|
2373 XrmValuePtr fromVal,
|
|
2374 XrmValuePtr toVal,
|
|
2375 XtPointer *closure_ret)
|
|
2376 {
|
|
2377 String str = (String)fromVal->addr;
|
|
2378 XColor screenColor;
|
|
2379 XColor exactColor;
|
|
2380 Screen *screen;
|
|
2381 Colormap colormap;
|
|
2382 Status status;
|
|
2383 String params[1];
|
|
2384 Cardinal num_params = 1;
|
|
2385 XtAppContext the_app_con = XtDisplayToApplicationContext (dpy);
|
|
2386
|
|
2387 if (*num_args != 2) {
|
|
2388 XtAppWarningMsg(the_app_con, "wrongParameters", "cvtStringToPixel",
|
|
2389 "XtToolkitError",
|
|
2390 "String to pixel conversion needs screen and colormap arguments",
|
|
2391 (String *)NULL, (Cardinal *)NULL);
|
|
2392 return False;
|
|
2393 }
|
|
2394
|
|
2395 screen = *((Screen **) args[0].addr);
|
|
2396 colormap = *((Colormap *) args[1].addr);
|
|
2397
|
|
2398 /* The original uses the private function CompareISOLatin1().
|
|
2399 Use XmuCompareISOLatin1() if you want, but I don't think it
|
|
2400 makes any difference here. */
|
|
2401 if (strcmp(str, XtDefaultBackground) == 0) {
|
|
2402 *closure_ret = False;
|
|
2403 /* This refers to the display's "*reverseVideo" resource.
|
|
2404 These display resources aren't documented anywhere that
|
|
2405 I can find, so I'm going to ignore this. */
|
|
2406 /* if (pd->rv) done(Pixel, BlackPixelOfScreen(screen)) else */
|
|
2407 done(Pixel, WhitePixelOfScreen(screen));
|
|
2408 }
|
|
2409 if (strcmp(str, XtDefaultForeground) == 0) {
|
|
2410 *closure_ret = False;
|
|
2411 /* if (pd->rv) done(Pixel, WhitePixelOfScreen(screen)) else */
|
|
2412 done(Pixel, BlackPixelOfScreen(screen));
|
|
2413 }
|
|
2414
|
|
2415 /* Originally called XAllocNamedColor() here. */
|
|
2416 status = XParseColor (DisplayOfScreen(screen), colormap, (char*)str,
|
|
2417 &screenColor);
|
|
2418 if (status) {
|
|
2419 status = allocate_nearest_color (DisplayOfScreen(screen), colormap,
|
|
2420 &screenColor);
|
|
2421 }
|
|
2422
|
|
2423 if (status == 0) {
|
|
2424 params[0] = str;
|
|
2425 /* Server returns a specific error code but Xlib discards it. Ugh */
|
|
2426 if (XLookupColor(DisplayOfScreen(screen), colormap, (char*)str,
|
|
2427 &exactColor, &screenColor)) {
|
|
2428 XtAppWarningMsg(the_app_con, "noColormap", "cvtStringToPixel",
|
|
2429 "XtToolkitError",
|
|
2430 "Cannot allocate colormap entry for \"%s\"",
|
|
2431 params, &num_params);
|
|
2432
|
|
2433 } else {
|
|
2434 XtAppWarningMsg(the_app_con, "badValue", "cvtStringToPixel",
|
|
2435 "XtToolkitError",
|
|
2436 "Color name \"%s\" is not defined", params, &num_params);
|
|
2437 }
|
|
2438
|
|
2439 *closure_ret = False;
|
|
2440 return False;
|
|
2441 } else {
|
|
2442 *closure_ret = (char*)True;
|
|
2443 done(Pixel, screenColor.pixel);
|
|
2444 }
|
|
2445 }
|
|
2446
|
|
2447 /* ARGSUSED */
|
|
2448 static void EmacsFreePixel (
|
|
2449 XtAppContext app,
|
|
2450 XrmValuePtr toVal,
|
|
2451 XtPointer closure,
|
|
2452 XrmValuePtr args,
|
|
2453 Cardinal *num_args)
|
|
2454 {
|
|
2455 if (*num_args != 2) {
|
|
2456 XtAppWarningMsg(app, "wrongParameters","freePixel","XtToolkitError",
|
|
2457 "Freeing a pixel requires screen and colormap arguments",
|
|
2458 (String *)NULL, (Cardinal *)NULL);
|
|
2459 return;
|
|
2460 }
|
|
2461
|
|
2462 if (closure) {
|
|
2463 Screen *screen = *((Screen **) args[0].addr);
|
|
2464 Colormap colormap = *((Colormap *) args[1].addr);
|
|
2465 XFreeColors(DisplayOfScreen(screen), colormap,
|
|
2466 (unsigned long*)toVal->addr, 1, (unsigned long)0);
|
|
2467 }
|
|
2468 }
|
|
2469
|
|
2470
|
|
2471 /************************************************************************/
|
|
2472 /* initialization */
|
|
2473 /************************************************************************/
|
|
2474
|
|
2475 void
|
|
2476 syms_of_event_Xt (void)
|
|
2477 {
|
|
2478 defsymbol (&Qkey_mapping, "key-mapping");
|
|
2479 }
|
|
2480
|
|
2481 void
|
|
2482 vars_of_event_Xt (void)
|
|
2483 {
|
|
2484 dispatch_event_queue = Qnil;
|
|
2485 staticpro (&dispatch_event_queue);
|
|
2486 dispatch_event_queue_tail = Qnil;
|
|
2487
|
|
2488 /* this function only makes safe calls */
|
|
2489 init_what_input_once ();
|
|
2490
|
|
2491 Xt_event_stream =
|
|
2492 (struct event_stream *) xmalloc (sizeof (struct event_stream));
|
|
2493 Xt_event_stream->event_pending_p = emacs_Xt_event_pending_p;
|
|
2494 Xt_event_stream->next_event_cb = emacs_Xt_next_event;
|
|
2495 Xt_event_stream->handle_magic_event_cb= emacs_Xt_handle_magic_event;
|
|
2496 Xt_event_stream->add_timeout_cb = emacs_Xt_add_timeout;
|
|
2497 Xt_event_stream->remove_timeout_cb = emacs_Xt_remove_timeout;
|
|
2498 Xt_event_stream->select_console_cb = emacs_Xt_select_console;
|
|
2499 Xt_event_stream->unselect_console_cb = emacs_Xt_unselect_console;
|
|
2500 Xt_event_stream->select_process_cb = emacs_Xt_select_process;
|
|
2501 Xt_event_stream->unselect_process_cb = emacs_Xt_unselect_process;
|
|
2502 Xt_event_stream->quit_p_cb = emacs_Xt_quit_p;
|
|
2503
|
|
2504 DEFVAR_BOOL ("modifier-keys-are-sticky", &modifier_keys_are_sticky /*
|
|
2505 *Non-nil makes modifier keys sticky.
|
|
2506 This means that you can release the modifier key before pressing down
|
|
2507 the key that you wish to be modified. Although this is non-standard
|
|
2508 behavior, it is recommended because it reduces the strain on your hand,
|
|
2509 thus reducing the incidence of the dreaded Emacs-pinky syndrome.
|
|
2510 */ );
|
|
2511 modifier_keys_are_sticky = 0;
|
|
2512
|
|
2513 DEFVAR_BOOL ("x-allow-sendevents", &x_allow_sendevents /*
|
|
2514 *Non-nil means to allow synthetic events. Nil means they are ignored.
|
|
2515 Beware: allowing emacs to process SendEvents opens a big security hole.
|
|
2516 */ );
|
|
2517 x_allow_sendevents = 0;
|
|
2518
|
|
2519 #ifdef DEBUG_XEMACS
|
|
2520 DEFVAR_INT ("x-debug-events", &x_debug_events /*
|
|
2521 If non-zero, display debug information about X events that XEmacs sees.
|
|
2522 Information is displayed on stderr. Currently defined values are:
|
|
2523
|
|
2524 1 == non-verbose output
|
|
2525 2 == verbose output
|
|
2526 */ );
|
|
2527 x_debug_events = 0;
|
|
2528 #endif
|
|
2529
|
|
2530 the_Xt_timeout_blocktype = Blocktype_new (struct Xt_timeout_blocktype);
|
|
2531
|
|
2532 last_quit_check_signal_tick_count = 0;
|
|
2533 }
|
|
2534
|
|
2535 void
|
|
2536 init_event_Xt_late (void) /* called when already initialized */
|
|
2537 {
|
|
2538 timeout_id_tick = 1;
|
|
2539 pending_timeouts = 0;
|
|
2540 completed_timeouts = 0;
|
|
2541
|
|
2542 event_stream = Xt_event_stream;
|
|
2543 XtToolkitInitialize ();
|
|
2544 Xt_app_con = XtCreateApplicationContext ();
|
|
2545 XtAppSetFallbackResources (Xt_app_con, (String *) x_fallback_resources);
|
|
2546
|
|
2547 /* In xselect.c */
|
|
2548 x_selection_timeout = (XtAppGetSelectionTimeout (Xt_app_con) / 1000);
|
|
2549 XSetErrorHandler (x_error_handler);
|
|
2550 XSetIOErrorHandler (x_IO_error_handler);
|
|
2551
|
|
2552 XtAppAddInput (Xt_app_con, signal_event_pipe[0],
|
|
2553 (XtPointer) (XtInputReadMask /* | XtInputExceptMask */),
|
|
2554 Xt_what_callback, 0);
|
|
2555
|
|
2556 XtAppSetTypeConverter (Xt_app_con, XtRString, XtRPixel,
|
|
2557 EmacsXtCvtStringToPixel,
|
|
2558 (XtConvertArgList) colorConvertArgs,
|
|
2559 2, XtCacheByDisplay, EmacsFreePixel);
|
|
2560 }
|