Source

XEmacs / src / event-msw.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/* The  mswindows event_stream interface.
   Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
   Copyright (C) 1995 Sun Microsystems, Inc.
   Copyright (C) 1996 Ben Wing.
   Copyright (C) 1997 Jonathan Harris.

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Synched up with: Not in FSF. */

/* Authorship:

   Ultimately based on FSF.
   Rewritten by Ben Wing.
   Rewritten for mswindows by Jonathan Harris, November 1997 for 20.4.
 */

#include <config.h>
#include "lisp.h"

#include "device.h"
#include "console-msw.h"
#include "emacsfns.h"
#include "events.h"
#include "frame.h"
#include "process.h"
#include "redisplay.h"
#include "sysproc.h"
#include "syswait.h"
#include "systime.h"

#include "event-msw.h"

static struct event_stream *mswindows_event_stream;

/*
 * Two separate queues, for efficiency, one (_u_) for user events, and
 * another (_s_) for non-user ones. We always return events out of the
 * first one until it is empty and only then proceed with the second
 * one.
 */
static Lisp_Object mswindows_u_dispatch_event_queue, mswindows_u_dispatch_event_queue_tail;
static Lisp_Object mswindows_s_dispatch_event_queue, mswindows_s_dispatch_event_queue_tail;

/*
 * List of mswindows waitable handles.
 * Apart from the dispatch queue semaphore, all of these handles may be waited
 * on multiple times in emacs_mswindows_next_event before being processed and so
 * must be manual-reset events.
 */
static HANDLE mswindows_waitable[MAX_WAITABLE];

/* random emacs info associated with each of the wait handles */
static mswindows_waitable_info_type mswindows_waitable_info[MAX_WAITABLE];

/* Count of quit chars currently in the queue */
/* Incremented in WM_CHAR handler in msw-proc.c
   Decremented in mswindows_dequeue_dispatch_event() */
int mswindows_quit_chars_count = 0;

/* These are Lisp integers; see DEFVARS in this file for description. */
int mswindows_dynamic_frame_resize;
int mswindows_num_mouse_buttons;
int mswindows_button2_max_skew_x;
int mswindows_button2_max_skew_y;
int mswindows_button2_chord_time;

/* Number of wait handles */
static mswindows_waitable_count=0;

/* This is the event signaled by the event pump.
   See mswindows_pump_outstanding_events for comments */
static Lisp_Object mswindows_error_caught_in_modal_loop;
static int mswindows_in_modal_loop;

/* Count of wound timers */
static int mswindows_pending_timers_count;

static int
mswindows_user_event_p (struct Lisp_Event* sevt)
{
  return (sevt->event_type == key_press_event
	  || sevt->event_type == button_press_event
	  || sevt->event_type == button_release_event);
}

/*
 * Add an emacs event to the proper dispatch queue
 */
void
mswindows_enqueue_dispatch_event (Lisp_Object event)
{
  int user_p = mswindows_user_event_p (XEVENT(event));
  enqueue_event (event,
		 user_p ? &mswindows_u_dispatch_event_queue : 
		 	&mswindows_s_dispatch_event_queue,
		 user_p ? &mswindows_u_dispatch_event_queue_tail :
		 	&mswindows_s_dispatch_event_queue_tail);

  /* This one does not go to window procedure, hence does not
     generate XM_BUMPQUEUE magic event! */
  PostMessage (NULL, XM_BUMPQUEUE, 0, 0);
}

/*
 * Remove and return the first emacs event on the dispatch queue.
 * Give a preference to user events over non-user ones.
 */
static Lisp_Object
mswindows_dequeue_dispatch_event ()
{
  Lisp_Object event;
  struct Lisp_Event* sevt;

  assert (!NILP(mswindows_u_dispatch_event_queue) ||
	  !NILP(mswindows_s_dispatch_event_queue));

  event = dequeue_event (
		 NILP(mswindows_u_dispatch_event_queue) ? 
			 &mswindows_s_dispatch_event_queue : 
			 &mswindows_u_dispatch_event_queue,
		 NILP(mswindows_u_dispatch_event_queue) ? 
			 &mswindows_s_dispatch_event_queue_tail :
			 &mswindows_u_dispatch_event_queue_tail);

  sevt = XEVENT(event);
  if (sevt->event_type == key_press_event
      && (sevt->event.key.modifiers & FAKE_MOD_QUIT))
    {
      sevt->event.key.modifiers &= ~FAKE_MOD_QUIT;
      --mswindows_quit_chars_count;
    }

  return event;
}

/*
 * Remove and return the first emacs event on the dispatch queue that matches
 * the supplied event
 * Timeout event matches if interval_id equals to that of the given event.
 * Keypress event matches if logical AND between modifiers bitmask of the
 * event in the queue and that of the given event is non-zero
 * For all other event types, this function asserts.
 */

Lisp_Object
mswindows_cancel_dispatch_event (struct Lisp_Event* match)
{
  Lisp_Object event;
  Lisp_Object previous_event=Qnil;
  int user_p = mswindows_user_event_p (match);
  Lisp_Object* head = user_p ? &mswindows_u_dispatch_event_queue : 
    			       &mswindows_s_dispatch_event_queue;
  Lisp_Object* tail = user_p ? &mswindows_u_dispatch_event_queue_tail : 
    			       &mswindows_s_dispatch_event_queue_tail;

  assert (match->event_type == timeout_event
	  || match->event_type == key_press_event);

  EVENT_CHAIN_LOOP (event, *head)
    {
      int found = 1;
      if (XEVENT_TYPE (event) != match->event_type)
	found = 0;
      if (found && match->event_type == timeout_event
	  && (XEVENT(event)->event.timeout.interval_id !=
	      match->event.timeout.interval_id))
	found = 0;
      if (found && match->event_type == key_press_event
	  && ((XEVENT(event)->event.key.modifiers &
	      match->event.key.modifiers) == 0))
	found = 0;

      if (found)
	{
	  if (NILP (previous_event))
	    dequeue_event (head, tail);
	  else
	    {
	      XSET_EVENT_NEXT (previous_event, XEVENT_NEXT (event));
	      if (EQ (*tail, event))
		*tail = previous_event;
	    }
	  
	  return event;
	}
      previous_event = event;
    }
}

static Lisp_Object
mswindows_modal_loop_error_handler (Lisp_Object cons_sig_data,
				    Lisp_Object u_n_u_s_e_d)
{
  mswindows_error_caught_in_modal_loop = cons_sig_data;
  return Qunbound;
}

Lisp_Object
mswindows_protect_modal_loop (Lisp_Object (*bfun) (Lisp_Object barg),
			      Lisp_Object barg)
{
  Lisp_Object tmp;

  ++mswindows_in_modal_loop; 
  tmp = condition_case_1 (Qt,
			  bfun, barg,
			  mswindows_modal_loop_error_handler, Qnil);
  --mswindows_in_modal_loop;

  return tmp;
}

void
mswindows_unmodalize_signal_maybe (void)
{
  if (!NILP (mswindows_error_caught_in_modal_loop))
    {
      /* Got an error while messages were pumped while
	 in window procedure - have to resignal */
      Lisp_Object sym = XCAR (mswindows_error_caught_in_modal_loop);
      Lisp_Object data = XCDR (mswindows_error_caught_in_modal_loop);
      mswindows_error_caught_in_modal_loop = Qnil;
      Fsignal (sym, data);
    }
}

/*
 * This is an unsafe part of event pump, guarded by 
 * condition_case. See mswindows_pump_outstanding_events
 */
static Lisp_Object
mswindows_unsafe_pump_events (Lisp_Object u_n_u_s_e_d)
{
  /* This function can call lisp */
  Lisp_Object event = Fmake_event (Qnil, Qnil);
  struct gcpro gcpro1;
  int do_redisplay = 0;
  GCPRO1 (event);

  while (detect_input_pending ())
    {
      Fnext_event (event, Qnil);
      Fdispatch_event (event);
      do_redisplay = 1;
    }

  if (do_redisplay)
    redisplay ();

  Fdeallocate_event (event);
  UNGCPRO;
  
  /* Qt becomes return value of mswindows_pump_outstanding_events
     once we get here */
  return Qt;
}

/*
 * This function pumps emacs events, while available, by using
 * next_message/dispatch_message loop. Errors are trapped around
 * the loop so the function always returns.
 *
 * Windows message queue is not looked into during the call,
 * neither are waitable handles checked. The function pumps
 * thus only dispatch events already queued, as well as those
 * resulted in dispatching thereof. This is done by setting
 * module local variable mswidows_in_modal_loop to nonzero.
 *
 * Return value is Qt if no errors was trapped, or Qunbound if
 * there was an error.
 *
 * In case of error, a cons representing the error, in the
 * form (SIGNAL . DATA), is stored in the module local variable
 * mswindows_error_caught_in_modal_loop. This error is signaled
 * again when DispatchMessage returns. Thus, Windows internal
 * modal loops are protected against throws, which are proven
 * to corrupt internal Windows structures.
 *
 * In case of success, mswindows_error_caught_in_modal_loop is
 * assigned Qnil.
 *
 * If the value of mswindows_error_caught_in_modal_loop is not
 * nil already upon entry, the function just returns non-nil.
 * This situation means that a new event has been queued while
 * cancleng mode. The event will be dequeued on the next regular
 * call of next-event; the pump is off since error is caught.
 * The caller must *unconditionally* cancel modal loop if the
 * value returned by this function is nil. Otherwise, everything
 * will become frozen until the modal loop exits under normal
 * condition (scrollbar drag is released, menu closed etc.)
 */
Lisp_Object
mswindows_pump_outstanding_events (void)
{
  /* This function can call lisp */

  Lisp_Object result = Qt;

  if (NILP(mswindows_error_caught_in_modal_loop))
      result = mswindows_protect_modal_loop (mswindows_unsafe_pump_events, Qnil);
  return result;
}

/*
 * Find a free waitable slot
 */
static int
mswindows_find_free_waitable(void)
{
  int i;
  for (i=0; i<mswindows_waitable_count; i++)
    if (mswindows_waitable_info[i].type == mswindows_waitable_type_none)
      return i;
  assert (mswindows_waitable_count < MAX_WAITABLE);
  return mswindows_waitable_count++;
}

/*
 * Create a new waitable using the type and data passed in by the info structure
 * Returns a pointer to the info associated with the assigned waitable object
 */
mswindows_waitable_info_type *
mswindows_add_waitable(mswindows_waitable_info_type *info)
{
  int waitable;

  switch (info->type)
  {
  case mswindows_waitable_type_dispatch:
    assert (0); /* kkm - should not get here */
    /* Can only have one waitable for the dispatch queue, and it's the first one */
    assert (mswindows_waitable_count++ == 0);
    waitable=0;
#if 0
    InitializeCriticalSection(&mswindows_dispatch_crit);
#endif
    assert (mswindows_waitable[0] = CreateSemaphore (NULL, 0, 0x7fffffff, NULL));
    return mswindows_waitable_info+0;

  default:
    assert(0);
  }
  mswindows_waitable_info[waitable].type = info->type;
  return mswindows_waitable_info+waitable;
}

/*
 * Remove a waitable using the type and data passed in by the info structure.
 */
void
mswindows_remove_waitable(mswindows_waitable_info_type *info)
{
  int waitable;

  switch (info->type)
  {

  default:
    assert(0);
  }

  CloseHandle(mswindows_waitable[waitable]);
  mswindows_waitable[waitable] = 0;
  mswindows_waitable_info[waitable].type = mswindows_waitable_type_none;
  if (waitable == mswindows_waitable_count-1)
    --mswindows_waitable_count;
}

/* 
 * Callback procedure for synchronous timer messages
 */
static void CALLBACK
mswindows_wm_timer_callback (HWND hwnd, UINT umsg, UINT id_timer, DWORD dwtime)
{
  Lisp_Object emacs_event = Fmake_event (Qnil, Qnil);
  struct Lisp_Event *event = XEVENT (emacs_event);

  if (KillTimer (NULL, id_timer))
    --mswindows_pending_timers_count;

  event->channel = Qnil;
  event->timestamp = dwtime;
  event->event_type = timeout_event;
  event->event.timeout.interval_id = id_timer;

  mswindows_enqueue_dispatch_event (emacs_event);
}

static void 
mswindows_drain_windows_queue ()
{
  MSG msg;
  while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
    {
      DispatchMessage (&msg);
      mswindows_unmodalize_signal_maybe ();
    }
}

/* 
 * This is a special flavour of the mswindows_need_event function,
 * used while in event pump. Actually, there is only kind of events
 * allowed while in event pump: a timer.  An attempt to fetch any
 * other event leads to a dealock, as there's no source of user input
 * ('cause event pump mirrors windows modal loop, which is a sole
 * owner of thread message queue).
 *
 * To detect this, we use a counter of active timers, and allow
 * fetching WM_TIMER messages. Instead of trying to fetch a WM_TIMER
 * which will never come when there are no pending timers, which leads
 * to deadlock, we simply signal an error.
 *
 * The implementation does not honor user_p by design.
 */
static void
mswindows_need_event_in_modal_loop (int user_p, int badly_p)
{
  MSG msg;

  /* Check if already have one */
  if (!NILP (mswindows_u_dispatch_event_queue)
      || !NILP (mswindows_s_dispatch_event_queue))
    return;

  /* No event is ok */
  if (!badly_p)
    return;

  /* We do not check the _u_ queue, because timers go to _s_ */
  while (NILP (mswindows_s_dispatch_event_queue))
    {
      /* We'll deadlock if go waiting */
      if (mswindows_pending_timers_count == 0)
	error ("Deadlock due to an attempt to call next-event in a wrong context");
      
      /* Fetch and dispatch any pending timers */
      GetMessage (&msg, NULL, WM_TIMER, WM_TIMER);
      DispatchMessage (&msg);
    }
}

/*
 * This drains the event queue and fills up two internal queues until
 * an event of a type specified by USER_P is retrieved.
 *
 * If user_p, then the function drains until the first user event, or
 * the first non-user event if there no user events. Otherwise, If
 * not user_p, it does not give preference to user events.
 *
 * If badly_p, then the function does not return until an event is
 * available.
 *
 * The code does not rely on MsgWaitForMultipleObjects preference for
 * messages over waitable handles.
 *
 * Used by emacs_mswindows_event_pending_p and emacs_mswindows_next_event
 */
static void
mswindows_need_event (int user_p, int badly_p)
{
  int active;

  if (mswindows_in_modal_loop)
    {
      mswindows_need_event_in_modal_loop (user_p, badly_p);
      return;
    }

  /* Have to drain Windows message queue first, otherwise, we may miss
     quit char when called from quit_p */
  mswindows_drain_windows_queue ();

  while (NILP (mswindows_u_dispatch_event_queue) &&
	 (user_p || NILP (mswindows_s_dispatch_event_queue)))
  {
    /* If we already have an event, we've got someting to return - no wait! */
    if (!NILP (mswindows_u_dispatch_event_queue)
	|| !NILP (mswindows_s_dispatch_event_queue))
      badly_p = 0;
    
    /* Now try getting a message */
    active = MsgWaitForMultipleObjects (mswindows_waitable_count,
					mswindows_waitable,
					FALSE, badly_p ? INFINITE : 0,
					QS_ALLINPUT);

    /* This will assert if handle being waited for becomes abandoned.
       Not the case currently tho */
    assert ((!badly_p && active == WAIT_TIMEOUT) ||
	    (active >= WAIT_OBJECT_0 &&
	     active <= WAIT_OBJECT_0 + mswindows_waitable_count));
    
    if (active == WAIT_TIMEOUT)
      {
	/* No luck trying - just return what we've already got */
	return;
      }
    else if (active == WAIT_OBJECT_0 + mswindows_waitable_count)
      {
	/* Got your message, thanks */
	mswindows_drain_windows_queue ();
      }
    else
      {
	/* XXX FIXME: We should do some kind of round-robin scheme to ensure fairness */
	int waitable = active - WAIT_OBJECT_0;
	mswindows_waitable_info_type *info  = mswindows_waitable_info + waitable;

	switch (info->type)
	  {
	    /* XXX FIXME: Should enque subprocess event here so that it is not lost */
	  default:
	    assert(0);
	  }
      }
  } /* while */

  return;
}


/************************************************************************/
/*                            methods                                   */
/************************************************************************/

static int
emacs_mswindows_add_timeout (EMACS_TIME thyme)
{
  int milliseconds;
  EMACS_TIME current_time;
  EMACS_GET_TIME (current_time);
  EMACS_SUB_TIME (thyme, thyme, current_time);
  milliseconds = EMACS_SECS (thyme) * 1000 +
    (EMACS_USECS (thyme) + 500) / 1000;
  if (milliseconds < 1)
    milliseconds = 1;
  ++mswindows_pending_timers_count;
  return SetTimer (NULL, 0, milliseconds, mswindows_wm_timer_callback);
}

static void
emacs_mswindows_remove_timeout (int id)
{
  struct Lisp_Event match_against;
  Lisp_Object emacs_event;

  if (KillTimer (NULL, id))
    --mswindows_pending_timers_count;

  /* If there is a dispatch event generated by this
     timeout in the queue, we have to remove it too. */
  match_against.event_type = timeout_event;
  match_against.event.timeout.interval_id = id;
  emacs_event = mswindows_cancel_dispatch_event (&match_against);
  if (!NILP (emacs_event))
    Fdeallocate_event(emacs_event);
}

/* If `user_p' is false, then return whether there are any win32, timeout,
 * or subprocess events pending (that is, whether
 * emacs_mswindows_next_event() would return immediately without blocking).
 *
 * if `user_p' is true, then return whether there are any *user generated*
 * events available (that is, whether there are keyboard or mouse-click
 * events ready to be read).  This also implies that
 * emacs_mswindows_next_event() would not block.
 */
static int
emacs_mswindows_event_pending_p (int user_p)
{
  mswindows_need_event (user_p, 0);

  return (!NILP (mswindows_u_dispatch_event_queue)
	  || (!user_p && !NILP (mswindows_s_dispatch_event_queue)));
}

/*
 * Return the next event
 */
static void
emacs_mswindows_next_event (struct Lisp_Event *emacs_event)
{
  Lisp_Object event, event2;

  /* Give strong preference to user events */
  mswindows_need_event (1, 1);

  /* XXX Copied from event-Xt.c */
  event = mswindows_dequeue_dispatch_event (!NILP(mswindows_u_dispatch_event_queue));
  XSETEVENT (event2, emacs_event);
  Fcopy_event (event, event2);
  Fdeallocate_event (event);
}

/*
 * Handle a magic event off the dispatch queue.
 */
static void
emacs_mswindows_handle_magic_event (struct Lisp_Event *emacs_event)
{
#if 0  
  stderr_out("magic %x, (%d,%d), (%d,%d)\n",
	     EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event),
	     rect->left, rect->top, rect->right, rect->bottom);
#endif
  switch (EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event))
  {
  case WM_SETFOCUS:
  case WM_KILLFOCUS:
    {
      Lisp_Object frame = EVENT_CHANNEL (emacs_event);
      struct frame *f = XFRAME (frame);
      int in_p = (EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event) == WM_SETFOCUS);
      Lisp_Object conser;

      /* struct gcpro gcpro1; */

      /* Clear sticky modifiers here (if we had any) */

      conser = Fcons (frame, Fcons (FRAME_DEVICE (f), in_p ? Qt : Qnil));
      /* GCPRO1 (conser); XXX Not necessary? */
      emacs_handle_focus_change_preliminary (conser);
      /* Under X the stuff up to here is done in the X event handler.
	 I Don't know why */
      emacs_handle_focus_change_final (conser);
      /* UNGCPRO; */

    }
    break;

  case XM_BUMPQUEUE:
    /* This is a nice event, when we're in need to queue *something* */
    break;

  case XM_MAPFRAME:
  case XM_UNMAPFRAME:
    {
      Lisp_Object frame = EVENT_CHANNEL (emacs_event);
      va_run_hook_with_args (EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event) 
			      == XM_MAPFRAME ?
			      Qmap_frame_hook : Qunmap_frame_hook, 
			      1, frame);
    }
    break;
			    
      /* XXX What about Enter & Leave */
#if 0
      va_run_hook_with_args (in_p ? Qmouse_enter_frame_hook :
			     Qmouse_leave_frame_hook, 1, frame);
#endif

  default:
    assert(0);
  }
}

static void
emacs_mswindows_select_process (struct Lisp_Process *process)
{
}

static void
emacs_mswindows_unselect_process (struct Lisp_Process *process)
{
}

static void
emacs_mswindows_select_console (struct console *con)
{
}

static void
emacs_mswindows_unselect_console (struct console *con)
{
}

static void
emacs_mswindows_quit_p (void)
{
  mswindows_need_event (1, 0);

  if (mswindows_quit_chars_count > 0)
    {
      /* Yes there's a hidden one... Throw it away */
      struct Lisp_Event match_against;
      Lisp_Object emacs_event;

      match_against.event_type = key_press_event;
      match_against.event.key.modifiers = FAKE_MOD_QUIT;

      emacs_event = mswindows_cancel_dispatch_event (&match_against);
      assert (!NILP (emacs_event));

      Vquit_flag = (XEVENT(emacs_event)->event.key.modifiers & MOD_SHIFT
		    ? Qcritical : Qt);

      Fdeallocate_event(emacs_event);
      --mswindows_quit_chars_count;
    }
}

/* This is called from GC when a process object is about to be freed.
   If we've still got pointers to it in this file, we're gonna lose hard.
 */
void
debug_process_finalization (struct Lisp_Process *p)
{
#if 0 /* #### */
  int i;
  int infd, outfd;
  get_process_file_descriptors (p, &infd, &outfd);
  /* if it still has fds, then it hasn't been killed yet. */
  assert (infd < 0);
  assert (outfd < 0);
  /* Better not still be in the "with input" table; we know it's got no fds. */
  for (i = 0; i < MAXDESC; i++)
    {
      Lisp_Object process = filedesc_fds_with_input [i];
      assert (!PROCESSP (process) || XPROCESS (process) != p);
    }
#endif
}

/************************************************************************/
/*                            initialization                            */
/************************************************************************/
 
void
vars_of_event_mswindows (void)
{
  mswindows_u_dispatch_event_queue = Qnil;
  staticpro (&mswindows_u_dispatch_event_queue);
  mswindows_u_dispatch_event_queue_tail = Qnil;

  mswindows_s_dispatch_event_queue = Qnil;
  staticpro (&mswindows_s_dispatch_event_queue);
  mswindows_s_dispatch_event_queue_tail = Qnil;

  mswindows_error_caught_in_modal_loop = Qnil;
  staticpro (&mswindows_error_caught_in_modal_loop);
  mswindows_in_modal_loop = 0;
  mswindows_pending_timers_count = 0;

  mswindows_event_stream = xnew (struct event_stream);

  mswindows_event_stream->event_pending_p 	= emacs_mswindows_event_pending_p;
  mswindows_event_stream->next_event_cb		= emacs_mswindows_next_event;
  mswindows_event_stream->handle_magic_event_cb = emacs_mswindows_handle_magic_event;
  mswindows_event_stream->add_timeout_cb 	= emacs_mswindows_add_timeout;
  mswindows_event_stream->remove_timeout_cb 	= emacs_mswindows_remove_timeout;
  mswindows_event_stream->select_console_cb 	= emacs_mswindows_select_console;
  mswindows_event_stream->unselect_console_cb	= emacs_mswindows_unselect_console;
  mswindows_event_stream->select_process_cb 	= emacs_mswindows_select_process;
  mswindows_event_stream->unselect_process_cb	= emacs_mswindows_unselect_process;
  mswindows_event_stream->quit_p_cb		= emacs_mswindows_quit_p;

  DEFVAR_BOOL ("mswindows-dynamic-frame-resize", &mswindows_dynamic_frame_resize /*
*Controls redrawing frame contents during mouse-drag or keyboard resize
operation. When non-nil, the frame is redrawn while being resized. When
nil, frame is not redrawn, and exposed areas are filled with default
MDI application background color. Note that this option only has effect
if "Show window contents while dragging" is on in system Display/Plus!
settings.
Default is t on fast machines, nil on slow.
*/ );

/* The description copied verbatim from nt-emacs. (C) Geoff Voelker */
  DEFVAR_INT ("mswindows-mouse-button-tolerance", &mswindows_button2_chord_time /*
*Analogue of double click interval for faking middle mouse events.
The value is the minimum time in milliseconds that must elapse between
left/right button down events before they are considered distinct events.
If both mouse buttons are depressed within this interval, a middle mouse
button down event is generated instead.
If negative or zero, currently set system default is used instead.
*/ );

/* The description copied verbatim from nt-emacs. (C) Geoff Voelker */
  DEFVAR_INT ("mswindows-num-mouse-buttons", &mswindows_num_mouse_buttons /*
Number of physical mouse buttons.
*/ );

  DEFVAR_INT ("mswindows-mouse-button-max-skew-x", &mswindows_button2_max_skew_x /*
*Maximum horizontal distance in pixels between points in which left and
right button clicks occured for them to be translated into single
middle button event. Clicks must occur in time not longer than defined
by the variable mswindows-mouse-button-tolerance.
If negative or zero, currently set system default is used instead.
*/ );

  DEFVAR_INT ("mswindows-mouse-button-max-skew-y", &mswindows_button2_max_skew_y /*
*Maximum vertical distance in pixels between points in which left and
right button clicks occured for them to be translated into single
middle button event. Clicks must occur in time not longer than defined
by the variable mswindows-mouse-button-tolerance.
If negative or zero, currently set system default is used instead.
*/ );

  mswindows_button2_max_skew_x = 0;
  mswindows_button2_max_skew_y = 0;
  mswindows_button2_chord_time = 0;
}

void
syms_of_event_mswindows (void)
{
}

void
init_event_mswindows_late (void)
{
  event_stream = mswindows_event_stream;

  mswindows_dynamic_frame_resize = !GetSystemMetrics (SM_SLOWMACHINE);
  mswindows_num_mouse_buttons = GetSystemMetrics (SM_CMOUSEBUTTONS);
}