213
|
1 /* The mswindows event_stream interface.
|
|
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 Copyright (C) 1997 Jonathan Harris.
|
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
|
24 /* Synched up with: Not in FSF. */
|
|
25
|
|
26 /* Authorship:
|
|
27
|
|
28 Ultimately based on FSF.
|
|
29 Rewritten by Ben Wing.
|
|
30 Rewritten for mswindows by Jonathan Harris, November 1997 for 20.4.
|
|
31 */
|
|
32
|
|
33 #include <config.h>
|
|
34 #include "lisp.h"
|
|
35
|
|
36 #include "device.h"
|
|
37 #include "console-msw.h"
|
|
38 #include "events.h"
|
|
39 #include "frame.h"
|
|
40 #include "process.h"
|
|
41
|
|
42 #include "sysproc.h"
|
|
43 #include "syswait.h"
|
|
44 #include "systime.h"
|
|
45
|
|
46 #include "event-msw.h"
|
|
47
|
|
48 static struct event_stream *mswindows_event_stream;
|
|
49 static Lisp_Object mswindows_dispatch_event_queue, mswindows_dispatch_event_queue_tail;
|
|
50 CRITICAL_SECTION mswindows_dispatch_crit;
|
|
51
|
|
52 /*
|
|
53 * List of mswindows waitable handles.
|
|
54 * Apart from the dispatch queue semaphore, all of these handles may be waited
|
219
|
55 * on multiple times in emacs_mswindows_next_event before being processed and so
|
|
56 * must be manual-reset events.
|
213
|
57 */
|
|
58 static HANDLE mswindows_waitable[MAX_WAITABLE];
|
|
59
|
|
60 /* random emacs info associated with each of the wait handles */
|
|
61 static mswindows_waitable_info_type mswindows_waitable_info[MAX_WAITABLE];
|
|
62
|
219
|
63 /* Number of wait handles */
|
|
64 static mswindows_waitable_count=0;
|
|
65
|
|
66 /*
|
|
67 * Add an emacs event to the dispatch queue and increment the semaphore
|
|
68 */
|
213
|
69 void
|
|
70 mswindows_enqueue_dispatch_event (Lisp_Object event)
|
|
71 {
|
219
|
72 enqueue_event (event, &mswindows_dispatch_event_queue,
|
|
73 &mswindows_dispatch_event_queue_tail);
|
213
|
74 ReleaseSemaphore(mswindows_waitable[0], 1, NULL);
|
|
75 }
|
|
76
|
219
|
77 /*
|
|
78 * Remove and return the first emacs event on the dispatch queue. Don't
|
|
79 * decrement the queue's semaphore because it will be decremented by being
|
|
80 * waited on.
|
|
81 */
|
213
|
82 static Lisp_Object
|
|
83 mswindows_dequeue_dispatch_event (void)
|
|
84 {
|
|
85 Lisp_Object event;
|
219
|
86 event = dequeue_event (&mswindows_dispatch_event_queue,
|
|
87 &mswindows_dispatch_event_queue_tail);
|
213
|
88 return event;
|
|
89 }
|
|
90
|
|
91 /*
|
219
|
92 * Remove and return the first emacs event on the dispatch queue that matches
|
|
93 * the supplied event and decrement the queue's semaphore.
|
|
94 * Only supports timeout events.
|
|
95 */
|
|
96 Lisp_Object
|
|
97 mswindows_cancel_dispatch_event (Lisp_Object match_event)
|
|
98 {
|
|
99 Lisp_Object event;
|
|
100 Lisp_Object previous_event=Qnil;
|
|
101 struct Lisp_Event *match = XEVENT(match_event);
|
|
102
|
|
103 assert (match->event_type == timeout_event);
|
|
104
|
|
105 EVENT_CHAIN_LOOP (event, mswindows_dispatch_event_queue)
|
|
106 if (XEVENT_TYPE (event) == match->event_type)
|
|
107 {
|
|
108 /* We only handle timeouts */
|
|
109 if (XEVENT(event)->event.timeout.interval_id ==
|
|
110 match->event.timeout.interval_id)
|
|
111 {
|
|
112 if (NILP (previous_event))
|
|
113 dequeue_event (&mswindows_dispatch_event_queue,
|
|
114 &mswindows_dispatch_event_queue_tail);
|
|
115 else
|
|
116 {
|
|
117 XSET_EVENT_NEXT (previous_event, XEVENT_NEXT (event));
|
|
118 if (EQ (mswindows_dispatch_event_queue_tail, event))
|
|
119 mswindows_dispatch_event_queue_tail = previous_event;
|
|
120 }
|
|
121
|
|
122 /* Decrement the dispatch queue counter */
|
|
123 WaitForSingleObject(mswindows_waitable[0], INFINITE);
|
|
124 return event;
|
|
125 }
|
|
126 }
|
|
127 else
|
|
128 previous_event = event;
|
|
129
|
|
130 return Qnil;
|
|
131 }
|
|
132
|
|
133 /*
|
213
|
134 * Find a free waitable slot
|
|
135 */
|
|
136 static int
|
|
137 mswindows_find_free_waitable(void)
|
|
138 {
|
|
139 int i;
|
|
140 for (i=0; i<mswindows_waitable_count; i++)
|
|
141 if (mswindows_waitable_info[i].type == mswindows_waitable_type_none)
|
|
142 return i;
|
|
143 assert (mswindows_waitable_count < MAX_WAITABLE);
|
|
144 return mswindows_waitable_count++;
|
|
145 }
|
|
146
|
|
147 /*
|
|
148 * Create a new waitable using the type and data passed in by the info structure
|
|
149 * Returns a pointer to the info associated with the assigned waitable object
|
|
150 */
|
|
151 mswindows_waitable_info_type *
|
|
152 mswindows_add_waitable(mswindows_waitable_info_type *info)
|
|
153 {
|
|
154 int waitable;
|
|
155
|
|
156 switch (info->type)
|
|
157 {
|
|
158 case mswindows_waitable_type_dispatch:
|
|
159 /* Can only have one waitable for the dispatch queue, and it's the first one */
|
|
160 assert (mswindows_waitable_count++ == 0);
|
|
161 waitable=0;
|
|
162 InitializeCriticalSection(&mswindows_dispatch_crit);
|
|
163 assert (mswindows_waitable[0] = CreateSemaphore (NULL, 0, 0x7fffffff, NULL));
|
|
164 return mswindows_waitable_info+0;
|
|
165
|
|
166 #if 0 /* Windows95 doesn't support WaitableTimers */
|
|
167 case mswindows_waitable_type_timeout:
|
|
168 {
|
|
169 LARGE_INTEGER due;
|
|
170 due.QuadPart = 10000 * (LONGLONG) info->data.timeout.milliseconds;
|
|
171 waitable = mswindows_find_free_waitable();
|
|
172 mswindows_waitable[waitable] = CreateWaitableTimer(NULL, TRUE, NULL);
|
|
173 SetWaitableTimer(mswindows_waitable[waitable], &due, 0, NULL, NULL, FALSE);
|
|
174 mswindows_waitable_info[waitable].data.timeout.id = waitable;
|
|
175 }
|
|
176 break;
|
|
177 #endif
|
|
178
|
|
179 default:
|
|
180 assert(0);
|
|
181 }
|
|
182 mswindows_waitable_info[waitable].type = info->type;
|
|
183 return mswindows_waitable_info+waitable;
|
|
184 }
|
|
185
|
|
186 /*
|
|
187 * Remove a waitable using the type and data passed in by the info structure.
|
|
188 */
|
|
189 void
|
|
190 mswindows_remove_waitable(mswindows_waitable_info_type *info)
|
|
191 {
|
|
192 int waitable;
|
|
193
|
|
194 switch (info->type)
|
|
195 {
|
|
196 #if 0
|
|
197 case mswindows_waitable_type_timeout:
|
|
198 waitable = info->data.timeout.id;
|
|
199 CancelWaitableTimeout(mswindows_waitable[waitable]);
|
|
200 break;
|
|
201 #endif
|
|
202
|
|
203 default:
|
|
204 assert(0);
|
|
205 }
|
|
206
|
|
207 CloseHandle(mswindows_waitable[waitable]);
|
|
208 mswindows_waitable[waitable] = 0;
|
|
209 mswindows_waitable_info[waitable].type = mswindows_waitable_type_none;
|
|
210 if (waitable == mswindows_waitable_count-1)
|
|
211 --mswindows_waitable_count;
|
|
212 }
|
|
213
|
|
214
|
|
215 /************************************************************************/
|
|
216 /* methods */
|
|
217 /************************************************************************/
|
|
218
|
|
219 static int
|
|
220 emacs_mswindows_add_timeout (EMACS_TIME thyme)
|
|
221 {
|
|
222 EMACS_TIME current_time;
|
|
223 int milliseconds;
|
|
224 int id;
|
|
225 mswindows_request_type request;
|
|
226
|
|
227 EMACS_GET_TIME (current_time);
|
|
228 EMACS_SUB_TIME (thyme, thyme, current_time);
|
|
229 milliseconds = EMACS_SECS (thyme) * 1000 + EMACS_USECS (thyme) / 1000;
|
|
230 if (milliseconds < 1)
|
|
231 milliseconds = 1;
|
|
232 request.thing1 = (void *) milliseconds;
|
|
233 id = mswindows_make_request(WM_XEMACS_SETTIMER, 0, &request);
|
|
234 assert(id); /* XXX */
|
|
235 return id;
|
|
236 }
|
|
237
|
|
238 static void
|
|
239 emacs_mswindows_remove_timeout (int id)
|
|
240 {
|
|
241 mswindows_request_type request = { (void *) id };
|
|
242 mswindows_make_request(WM_XEMACS_KILLTIMER, 0, &request);
|
|
243 }
|
|
244
|
219
|
245 /* If `user_p' is false, then return whether there are any win32, timeout,
|
|
246 * or subprocess events pending (that is, whether
|
|
247 * emacs_mswindows_next_event() would return immediately without blocking).
|
|
248 *
|
|
249 * if `user_p' is true, then return whether there are any *user generated*
|
|
250 * events available (that is, whether there are keyboard or mouse-click
|
|
251 * events ready to be read). This also implies that
|
|
252 * emacs_mswindows_next_event() would not block.
|
|
253 */
|
213
|
254 static int
|
|
255 emacs_mswindows_event_pending_p (int user_p)
|
|
256 {
|
219
|
257 if (user_p)
|
|
258 {
|
|
259 /* Iterate over the dispatch queue looking for user-events */
|
|
260 int found = 0;
|
|
261 Lisp_Object event;
|
|
262
|
|
263 EnterCriticalSection (&mswindows_dispatch_crit);
|
|
264 EVENT_CHAIN_LOOP (event, mswindows_dispatch_event_queue)
|
|
265 if (command_event_p (event))
|
|
266 found = 1;
|
|
267 LeaveCriticalSection (&mswindows_dispatch_crit);
|
|
268 return found;
|
|
269 }
|
|
270 else
|
|
271 {
|
|
272 /* Check for any kind of input, including the dispatch queue */
|
|
273 #if 0
|
|
274 /* Want do do the following, but it's not clear whether this would
|
|
275 * cause the waitables to become unsignalled */
|
|
276 return (WaitForMultipleObjects (mswindows_waitable_count,
|
|
277 mswindows_waitable, FALSE, 0)
|
|
278 != WAIT_TIMEOUT);
|
|
279 #else
|
|
280 return !NILP (mswindows_dispatch_event_queue);
|
|
281 #endif
|
|
282 }
|
213
|
283 }
|
|
284
|
|
285 static struct console *
|
|
286 find_console_from_fd (int fd)
|
|
287 {
|
|
288 return 0;
|
|
289 }
|
|
290
|
|
291 /*
|
|
292 * Return the next event
|
|
293 * We return windows events off the dispatch event queue in preference to other events
|
|
294 */
|
|
295 static void
|
|
296 emacs_mswindows_next_event (struct Lisp_Event *emacs_event)
|
|
297 {
|
|
298 DWORD active;
|
|
299 active = WaitForMultipleObjects (mswindows_waitable_count, mswindows_waitable,
|
|
300 FALSE, INFINITE);
|
|
301 assert(active >= WAIT_OBJECT_0 && active <= WAIT_OBJECT_0 + mswindows_waitable_count - 1);
|
|
302
|
|
303 /* Windows events on the dispatch event queue */
|
|
304 if (active == WAIT_OBJECT_0)
|
|
305 {
|
|
306 /* XXX Copied from event-Xt.c */
|
|
307 Lisp_Object event, event2;
|
|
308
|
|
309 EnterCriticalSection (&mswindows_dispatch_crit);
|
|
310 XSETEVENT (event2, emacs_event);
|
|
311 event = mswindows_dequeue_dispatch_event ();
|
|
312 Fcopy_event (event, event2);
|
|
313 Fdeallocate_event (event);
|
|
314 LeaveCriticalSection (&mswindows_dispatch_crit);
|
|
315 }
|
|
316 else
|
|
317 {
|
|
318 /* XXX FIXME: We should do some kind of round-robin scheme to ensure fairness */
|
|
319 int waitable = active - WAIT_OBJECT_0;
|
|
320 mswindows_waitable_info_type *info = mswindows_waitable_info + waitable;
|
|
321
|
|
322 switch (info->type)
|
|
323 {
|
|
324 case mswindows_waitable_type_timeout:
|
|
325 emacs_event->channel = Qnil;
|
|
326 emacs_event->event_type = timeout_event;
|
|
327 emacs_event->event.timeout.interval_id = info->data.timeout.id;
|
|
328 mswindows_remove_waitable(info);
|
|
329 break;
|
|
330
|
|
331 default:
|
|
332 assert(0);
|
|
333 }
|
|
334 }
|
|
335 }
|
|
336
|
|
337 /*
|
|
338 * Handle a magic event off the dispatch queue.
|
|
339 * XXX split into seperate functions for clarity.
|
|
340 */
|
|
341 static void
|
|
342 emacs_mswindows_handle_magic_event (struct Lisp_Event *emacs_event)
|
|
343 {
|
|
344 RECT *rect = &EVENT_MSWINDOWS_MAGIC_DATA(emacs_event);
|
|
345 struct frame *f = XFRAME (EVENT_CHANNEL (emacs_event));
|
|
346 Lisp_Object frame = Qnil;
|
|
347 XSETFRAME (frame, f);
|
|
348 #if 0
|
|
349 stderr_out("magic %x, (%d,%d), (%d,%d)\n",
|
|
350 EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event),
|
|
351 rect->left, rect->top, rect->right, rect->bottom);
|
|
352 #endif
|
|
353 switch (EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event))
|
|
354 {
|
|
355 case WM_SETFOCUS:
|
|
356 case WM_KILLFOCUS:
|
|
357 {
|
|
358 int in_p = (EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event) == WM_SETFOCUS);
|
|
359 Lisp_Object conser;
|
|
360 /* struct gcpro gcpro1; */
|
|
361
|
|
362 /* Clear sticky modifiers here (if we had any) */
|
|
363
|
|
364 conser = Fcons (frame, Fcons (FRAME_DEVICE (f), in_p ? Qt : Qnil));
|
|
365 /* GCPRO1 (conser); XXX Not necessary? */
|
|
366 emacs_handle_focus_change_preliminary (conser);
|
|
367 /* Under X the stuff up to here is done in the X event handler.
|
|
368 I Don't know why */
|
|
369 emacs_handle_focus_change_final (conser);
|
|
370 /* UNGCPRO; */
|
|
371 }
|
|
372 break;
|
|
373
|
|
374 /* XXX What about Enter & Leave */
|
|
375 #if 0
|
|
376 va_run_hook_with_args (in_p ? Qmouse_enter_frame_hook :
|
|
377 Qmouse_leave_frame_hook, 1, frame);
|
|
378 break;
|
|
379 #endif
|
|
380
|
|
381 case WM_SIZE:
|
|
382 if ((rect->left & rect->top & rect->right & rect->bottom) == -1)
|
|
383 {
|
|
384 /* Iconified */
|
|
385 FRAME_VISIBLE_P (f) = 0;
|
|
386 va_run_hook_with_args (Qunmap_frame_hook, 1, frame);
|
|
387 Fframe_iconified_p (frame);
|
|
388 }
|
|
389 else
|
|
390 {
|
|
391 /* If we're uniconified, our size may or may not have changed */
|
|
392 int columns, rows;
|
|
393 int was_visible = FRAME_VISIBLE_P (f);
|
|
394
|
|
395 FRAME_VISIBLE_P (f) = 1;
|
219
|
396 FRAME_PIXWIDTH(f) = rect->right;
|
|
397 FRAME_PIXHEIGHT(f) = rect->bottom;
|
|
398
|
|
399 pixel_to_char_size (f, rect->right, rect->bottom, &columns, &rows);
|
|
400 change_frame_size (f, rows, columns, 0);
|
213
|
401 /* MARK_FRAME_WINDOWS_STRUCTURE_CHANGED (f); /* XXX Too extreme? */
|
|
402
|
|
403 if (!was_visible)
|
|
404 va_run_hook_with_args (Qmap_frame_hook, 1, frame);
|
|
405
|
|
406 }
|
|
407 break;
|
|
408
|
|
409 case WM_PAINT:
|
|
410 mswindows_redraw_exposed_area(f, rect->left, rect->top,
|
219
|
411 rect->right, rect->bottom);
|
|
412 break;
|
|
413
|
|
414 case WM_CLOSE:
|
|
415 enqueue_misc_user_event (frame, Qeval, list3 (Qdelete_frame, frame, Qt));
|
213
|
416 break;
|
|
417
|
|
418 default:
|
|
419 assert(0);
|
|
420 }
|
|
421 }
|
|
422
|
|
423 static void
|
|
424 emacs_mswindows_select_process (struct Lisp_Process *process)
|
|
425 {
|
|
426 }
|
|
427
|
|
428 static void
|
|
429 emacs_mswindows_unselect_process (struct Lisp_Process *process)
|
|
430 {
|
|
431 }
|
|
432
|
|
433 static void
|
|
434 emacs_mswindows_select_console (struct console *con)
|
|
435 {
|
|
436 }
|
|
437
|
|
438 static void
|
|
439 emacs_mswindows_unselect_console (struct console *con)
|
|
440 {
|
|
441 }
|
|
442
|
|
443 static void
|
|
444 emacs_mswindows_quit_p (void)
|
|
445 {
|
|
446 }
|
|
447
|
|
448 /* This is called from GC when a process object is about to be freed.
|
|
449 If we've still got pointers to it in this file, we're gonna lose hard.
|
|
450 */
|
|
451 void
|
|
452 debug_process_finalization (struct Lisp_Process *p)
|
|
453 {
|
|
454 #if 0 /* #### */
|
|
455 int i;
|
|
456 int infd, outfd;
|
|
457 get_process_file_descriptors (p, &infd, &outfd);
|
|
458 /* if it still has fds, then it hasn't been killed yet. */
|
|
459 assert (infd < 0);
|
|
460 assert (outfd < 0);
|
|
461 /* Better not still be in the "with input" table; we know it's got no fds. */
|
|
462 for (i = 0; i < MAXDESC; i++)
|
|
463 {
|
|
464 Lisp_Object process = filedesc_fds_with_input [i];
|
|
465 assert (!PROCESSP (process) || XPROCESS (process) != p);
|
|
466 }
|
|
467 #endif
|
|
468 }
|
|
469
|
|
470 /************************************************************************/
|
|
471 /* initialization */
|
|
472 /************************************************************************/
|
|
473
|
|
474 void
|
|
475 vars_of_event_mswindows (void)
|
|
476 {
|
|
477 mswindows_dispatch_event_queue = Qnil;
|
|
478 staticpro (&mswindows_dispatch_event_queue);
|
|
479 mswindows_dispatch_event_queue_tail = Qnil;
|
|
480
|
|
481 mswindows_event_stream = xnew (struct event_stream);
|
|
482
|
|
483 mswindows_event_stream->event_pending_p = emacs_mswindows_event_pending_p;
|
|
484 mswindows_event_stream->next_event_cb = emacs_mswindows_next_event;
|
|
485 mswindows_event_stream->handle_magic_event_cb = emacs_mswindows_handle_magic_event;
|
|
486 mswindows_event_stream->add_timeout_cb = emacs_mswindows_add_timeout;
|
|
487 mswindows_event_stream->remove_timeout_cb = emacs_mswindows_remove_timeout;
|
|
488 mswindows_event_stream->select_console_cb = emacs_mswindows_select_console;
|
|
489 mswindows_event_stream->unselect_console_cb = emacs_mswindows_unselect_console;
|
|
490 mswindows_event_stream->select_process_cb = emacs_mswindows_select_process;
|
|
491 mswindows_event_stream->unselect_process_cb = emacs_mswindows_unselect_process;
|
|
492 mswindows_event_stream->quit_p_cb = emacs_mswindows_quit_p;
|
|
493 }
|
|
494
|
|
495 void
|
|
496 syms_of_event_mswindows (void)
|
|
497 {
|
|
498 }
|
|
499
|
|
500 void
|
|
501 init_event_mswindows_late (void)
|
|
502 {
|
|
503 event_stream = mswindows_event_stream;
|
|
504 }
|