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, 2000 Ben Wing.
5 Copyright (C) 1997 Jonathan Harris.
7 This file is part of XEmacs.
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
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
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. */
24 /* Synched up with: Not in FSF. */
28 Ultimately based on FSF.
29 Rewritten by Ben Wing.
30 Rewritten for mswindows by Jonathan Harris, November 1997 for 21.0.
31 Subprocess and modal loop support by Kirill M. Katsnelson.
37 #include "console-msw.h"
39 #ifdef HAVE_SCROLLBARS
40 # include "scrollbar-msw.h"
45 # include "menubar-msw.h"
49 # include "dragdrop.h"
59 #include "redisplay.h"
65 #include "objects-msw.h"
67 #include "events-mod.h"
68 #ifdef HAVE_MSG_SELECT
70 #include "console-tty.h"
71 #elif defined(__CYGWIN32__)
72 typedef unsigned int SOCKET;
77 #if !(defined(__CYGWIN32__) || defined(__MINGW32__))
78 # include <shlobj.h> /* For IShellLink */
81 #if defined (__CYGWIN32__) && (CYGWIN_VERSION_DLL_MAJOR < 20)
82 typedef NMHDR *LPNMHDR;
86 #define ADJR_MENUFLAG TRUE
88 #define ADJR_MENUFLAG FALSE
91 /* Fake key modifier which is attached to a quit char event.
92 Removed upon dequeueing an event */
93 #define FAKE_MOD_QUIT 0x80
95 /* Timer ID used for button2 emulation */
96 #define BUTTON_2_TIMER_ID 1
98 static Lisp_Object mswindows_find_frame (HWND hwnd);
99 static Lisp_Object mswindows_find_console (HWND hwnd);
100 static Lisp_Object mswindows_key_to_emacs_keysym (int mswindows_key, int mods,
102 static int mswindows_modifier_state (BYTE* keymap, int has_AltGr);
103 static void mswindows_set_chord_timer (HWND hwnd);
104 static int mswindows_button2_near_enough (POINTS p1, POINTS p2);
105 static int mswindows_current_layout_has_AltGr (void);
107 static struct event_stream *mswindows_event_stream;
109 #ifdef HAVE_MSG_SELECT
110 extern SELECT_TYPE input_wait_mask, non_fake_input_wait_mask;
111 extern SELECT_TYPE process_only_mask, tty_only_mask;
112 SELECT_TYPE zero_mask;
113 extern int signal_event_pipe_initialized;
118 * Two separate queues, for efficiency, one (_u_) for user events, and
119 * another (_s_) for non-user ones. We always return events out of the
120 * first one until it is empty and only then proceed with the second
123 static Lisp_Object mswindows_u_dispatch_event_queue, mswindows_u_dispatch_event_queue_tail;
124 static Lisp_Object mswindows_s_dispatch_event_queue, mswindows_s_dispatch_event_queue_tail;
126 /* The number of things we can wait on */
127 #define MAX_WAITABLE (MAXIMUM_WAIT_OBJECTS - 1)
129 #ifndef HAVE_MSG_SELECT
130 /* List of mswindows waitable handles. */
131 static HANDLE mswindows_waitable_handles[MAX_WAITABLE];
133 /* Number of wait handles */
134 static int mswindows_waitable_count=0;
135 #endif /* HAVE_MSG_SELECT */
137 /* Brush for painting widgets */
138 static HBRUSH widget_brush = 0;
139 static LONG last_widget_brushed = 0;
141 /* Count of quit chars currently in the queue */
142 /* Incremented in WM_[SYS]KEYDOWN handler in the mswindows_wnd_proc()
143 Decremented in mswindows_dequeue_dispatch_event() */
144 int mswindows_quit_chars_count = 0;
146 /* These are Lisp integers; see DEFVARS in this file for description. */
147 int mswindows_dynamic_frame_resize;
148 int mswindows_alt_by_itself_activates_menu;
149 int mswindows_num_mouse_buttons;
150 int mswindows_mouse_button_max_skew_x;
151 int mswindows_mouse_button_max_skew_y;
152 int mswindows_mouse_button_tolerance;
154 /* This is the event signaled by the event pump.
155 See mswindows_pump_outstanding_events for comments */
156 static Lisp_Object mswindows_error_caught_in_modal_loop;
157 static int mswindows_in_modal_loop;
159 /* Count of wound timers */
160 static int mswindows_pending_timers_count;
162 /************************************************************************/
163 /* Pipe instream - reads process output */
164 /************************************************************************/
166 #define PIPE_READ_DELAY 20
168 #define HANDLE_TO_USID(h) ((USID)(h))
170 #define NTPIPE_SLURP_STREAM_DATA(stream) \
171 LSTREAM_TYPE_DATA (stream, ntpipe_slurp)
173 /* This structure is allocated by the main thread, and is deallocated
174 in the thread upon exit. There are situations when a thread
175 remains blocked for a long time, much longer than the lstream
176 exists. For example, "start notepad" command is issued from the
177 shell, then the shell is closed by C-c C-d. Although the shell
178 process exits, its output pipe will not get closed until the
179 notepad process exits also, because it inherits the pipe form the
180 shell. In this case, we abandon the thread, and let it live until
181 all such processes exit. While struct ntpipe_slurp_stream is
182 deallocated in this case, ntpipe_slurp_stream_shared_data are not. */
184 struct ntpipe_slurp_stream_shared_data
186 HANDLE hev_thread; /* Our thread blocks on this, signaled by caller */
187 /* This is a manual-reset object. */
188 HANDLE hev_caller; /* Caller blocks on this, and we signal it */
189 /* This is a manual-reset object. */
190 HANDLE hev_unsleep; /* Pipe read delay is canceled if this is set */
191 /* This is a manual-reset object. */
192 HANDLE hpipe; /* Pipe read end handle. */
193 LONG die_p; /* Thread must exit ASAP if non-zero */
194 BOOL eof_p : 1; /* Set when thread saw EOF */
195 BOOL error_p : 1; /* Read error other than EOF/broken pipe */
196 BOOL inuse_p : 1; /* this structure is in use */
197 LONG lock_count; /* Client count of this struct, 0=safe to free */
198 BYTE onebyte; /* One byte buffer read by thread */
201 #define MAX_SLURP_STREAMS 32
202 struct ntpipe_slurp_stream_shared_data
203 shared_data_block[MAX_SLURP_STREAMS]={{0}};
205 struct ntpipe_slurp_stream
207 LPARAM user_data; /* Any user data stored in the stream object */
208 struct ntpipe_slurp_stream_shared_data* thread_data;
211 DEFINE_LSTREAM_IMPLEMENTATION ("ntpipe-input", lstream_ntpipe_slurp,
212 sizeof (struct ntpipe_slurp_stream));
214 /* This function is thread-safe, and is called from either thread
215 context. It serializes freeing shared data structure */
217 slurper_free_shared_data_maybe (struct ntpipe_slurp_stream_shared_data* s)
219 if (InterlockedDecrement (&s->lock_count) == 0)
222 CloseHandle (s->hev_thread);
223 CloseHandle (s->hev_caller);
224 CloseHandle (s->hev_unsleep);
229 static struct ntpipe_slurp_stream_shared_data*
230 slurper_allocate_shared_data (void)
233 for (i=0; i<MAX_SLURP_STREAMS; i++)
235 if (!shared_data_block[i].inuse_p)
237 shared_data_block[i].inuse_p=1;
238 return &shared_data_block[i];
241 return (struct ntpipe_slurp_stream_shared_data*)0;
245 slurp_thread (LPVOID vparam)
247 struct ntpipe_slurp_stream_shared_data *s =
248 (struct ntpipe_slurp_stream_shared_data*)vparam;
252 /* Read one byte from the pipe */
254 if (!ReadFile (s->hpipe, &s->onebyte, 1, &actually_read, NULL))
256 DWORD err = GetLastError ();
257 if (err == ERROR_BROKEN_PIPE || err == ERROR_NO_DATA)
262 else if (actually_read == 0)
265 /* We must terminate on an error or eof */
266 if (s->eof_p || s->error_p)
267 InterlockedIncrement (&s->die_p);
269 /* Before we notify caller, we unsignal our event. */
270 ResetEvent (s->hev_thread);
272 /* Now we got something to notify caller, either a byte or an
273 error/eof indication. Before we do, allow internal pipe
274 buffer to accumulate little bit more data.
275 Reader function pulses this event before waiting for
276 a character, to avoid pipe delay, and to get the byte
279 WaitForSingleObject (s->hev_unsleep, PIPE_READ_DELAY);
281 /* Either make event loop generate a process event, or
283 SetEvent (s->hev_caller);
285 /* Cleanup and exit if we're shot off */
289 /* Block until the client finishes with retrieving the rest of
291 WaitForSingleObject (s->hev_thread, INFINITE);
294 slurper_free_shared_data_maybe (s);
300 make_ntpipe_input_stream (HANDLE hpipe, LPARAM param)
303 Lstream *lstr = Lstream_new (lstream_ntpipe_slurp, "r");
304 struct ntpipe_slurp_stream* s = NTPIPE_SLURP_STREAM_DATA (lstr);
305 DWORD thread_id_unused;
308 /* We deal only with pipes, for we're using PeekNamedPipe api */
309 assert (GetFileType (hpipe) == FILE_TYPE_PIPE);
311 s->thread_data = slurper_allocate_shared_data();
313 /* Create reader thread. This could fail, so do not create events
314 until thread is created */
315 hthread = CreateThread (NULL, 0, slurp_thread, (LPVOID)s->thread_data,
316 CREATE_SUSPENDED, &thread_id_unused);
319 Lstream_delete (lstr);
320 s->thread_data->inuse_p=0;
324 /* Shared data are initially owned by both main and slurper
326 s->thread_data->lock_count = 2;
327 s->thread_data->die_p = 0;
328 s->thread_data->eof_p = FALSE;
329 s->thread_data->error_p = FALSE;
330 s->thread_data->hpipe = hpipe;
331 s->user_data = param;
333 /* hev_thread is a manual-reset event, initially signaled */
334 s->thread_data->hev_thread = CreateEvent (NULL, TRUE, TRUE, NULL);
335 /* hev_caller is a manual-reset event, initially nonsignaled */
336 s->thread_data->hev_caller = CreateEvent (NULL, TRUE, FALSE, NULL);
337 /* hev_unsleep is a manual-reset event, initially nonsignaled */
338 s->thread_data->hev_unsleep = CreateEvent (NULL, TRUE, FALSE, NULL);
341 ResumeThread (hthread);
342 CloseHandle (hthread);
344 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE;
345 XSETLSTREAM (obj, lstr);
350 get_ntpipe_input_stream_param (Lstream *stream)
352 struct ntpipe_slurp_stream* s = NTPIPE_SLURP_STREAM_DATA(stream);
357 get_ntpipe_input_stream_waitable (Lstream *stream)
359 struct ntpipe_slurp_stream* s = NTPIPE_SLURP_STREAM_DATA(stream);
360 return s->thread_data->hev_caller;
364 ntpipe_slurp_reader (Lstream *stream, unsigned char *data, size_t size)
366 /* This function must be called from the main thread only */
367 struct ntpipe_slurp_stream_shared_data* s =
368 NTPIPE_SLURP_STREAM_DATA(stream)->thread_data;
373 /* Disallow pipe read delay for the thread: we need a character
375 SetEvent (s->hev_unsleep);
377 /* Check if we have a character ready. Give it a short delay,
378 for the thread to awake from pipe delay, just ion case*/
379 wait_result = WaitForSingleObject (s->hev_caller, 2);
381 /* Revert to the normal sleep behavior. */
382 ResetEvent (s->hev_unsleep);
384 /* If there's no byte buffered yet, give up */
385 if (wait_result == WAIT_TIMEOUT)
392 /* Reset caller unlock event now, as we've handled the pending
393 process output event */
394 ResetEvent (s->hev_caller);
396 /* It is now safe to do anything with contents of S, except for
397 changing s->die_p, which still should be interlocked */
401 if (s->error_p || s->die_p)
404 /* Ok, there were no error neither eof - we've got a byte from the
406 *(data++) = s->onebyte;
410 DWORD bytes_read = 0;
413 DWORD bytes_available;
415 /* If the api call fails, return at least one byte already
416 read. ReadFile in thread will return error */
417 if (PeekNamedPipe (s->hpipe, NULL, 0, NULL, &bytes_available, NULL))
420 /* Fetch available bytes. The same consideration applies,
421 so do not check for errors. ReadFile in the thread will
422 fail if the next call fails. */
424 ReadFile (s->hpipe, data, min (bytes_available, size),
428 /* Now we can unblock thread, so it attempts to read more */
429 SetEvent (s->hev_thread);
430 return bytes_read + 1;
437 ntpipe_slurp_closer (Lstream *stream)
439 /* This function must be called from the main thread only */
440 struct ntpipe_slurp_stream_shared_data* s =
441 NTPIPE_SLURP_STREAM_DATA(stream)->thread_data;
443 /* Force thread to stop */
444 InterlockedIncrement (&s->die_p);
446 /* Set events which could possibly block slurper. Let it finish soon
448 SetEvent (s->hev_unsleep);
449 SetEvent (s->hev_thread);
451 /* Unlock and maybe free shared data */
452 slurper_free_shared_data_maybe (s);
458 init_slurp_stream (void)
460 LSTREAM_HAS_METHOD (ntpipe_slurp, reader);
461 LSTREAM_HAS_METHOD (ntpipe_slurp, closer);
464 /************************************************************************/
465 /* Pipe outstream - writes process input */
466 /************************************************************************/
468 #define NTPIPE_SHOVE_STREAM_DATA(stream) \
469 LSTREAM_TYPE_DATA (stream, ntpipe_shove)
471 #define MAX_SHOVE_BUFFER_SIZE 128
473 struct ntpipe_shove_stream
475 LPARAM user_data; /* Any user data stored in the stream object */
476 HANDLE hev_thread; /* Our thread blocks on this, signaled by caller */
477 /* This is an auto-reset object. */
478 HANDLE hpipe; /* Pipe write end handle. */
479 HANDLE hthread; /* Reader thread handle. */
480 char buffer[MAX_SHOVE_BUFFER_SIZE]; /* Buffer being written */
481 DWORD size; /* Number of bytes to write */
482 LONG die_p; /* Thread must exit ASAP if non-zero */
483 LONG idle_p; /* Non-zero if thread is waiting for job */
484 BOOL error_p : 1; /* Read error other than EOF/broken pipe */
485 BOOL blocking_p : 1;/* Last write attempt would cause blocking */
488 DEFINE_LSTREAM_IMPLEMENTATION ("ntpipe-output", lstream_ntpipe_shove,
489 sizeof (struct ntpipe_shove_stream));
491 #ifndef HAVE_MSG_SELECT
493 shove_thread (LPVOID vparam)
495 struct ntpipe_shove_stream *s = (struct ntpipe_shove_stream*) vparam;
501 /* Block on event and wait for a job */
502 InterlockedIncrement (&s->idle_p);
503 WaitForSingleObject (s->hev_thread, INFINITE);
508 /* Write passed buffer */
509 if (!WriteFile (s->hpipe, s->buffer, s->size, &bytes_written, NULL)
510 || bytes_written != s->size)
513 InterlockedIncrement (&s->die_p);
524 make_ntpipe_output_stream (HANDLE hpipe, LPARAM param)
527 Lstream *lstr = Lstream_new (lstream_ntpipe_shove, "w");
528 struct ntpipe_shove_stream* s = NTPIPE_SHOVE_STREAM_DATA (lstr);
529 DWORD thread_id_unused;
534 s->user_data = param;
536 /* Create reader thread. This could fail, so do not
537 create the event until thread is created */
538 s->hthread = CreateThread (NULL, 0, shove_thread, (LPVOID)s,
539 CREATE_SUSPENDED, &thread_id_unused);
540 if (s->hthread == NULL)
542 Lstream_delete (lstr);
546 /* hev_thread is an auto-reset event, initially nonsignaled */
547 s->hev_thread = CreateEvent (NULL, FALSE, FALSE, NULL);
550 ResumeThread (s->hthread);
552 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE;
553 XSETLSTREAM (obj, lstr);
558 get_ntpipe_output_stream_param (Lstream *stream)
560 struct ntpipe_shove_stream* s = NTPIPE_SHOVE_STREAM_DATA(stream);
566 ntpipe_shove_writer (Lstream *stream, const unsigned char *data, size_t size)
568 struct ntpipe_shove_stream* s = NTPIPE_SHOVE_STREAM_DATA(stream);
573 s->blocking_p = !s->idle_p;
577 if (size>MAX_SHOVE_BUFFER_SIZE)
580 memcpy (s->buffer, data, size);
584 InterlockedDecrement (&s->idle_p);
585 SetEvent (s->hev_thread);
590 ntpipe_shove_was_blocked_p (Lstream *stream)
592 struct ntpipe_shove_stream* s = NTPIPE_SHOVE_STREAM_DATA(stream);
593 return s->blocking_p;
597 ntpipe_shove_closer (Lstream *stream)
599 struct ntpipe_shove_stream* s = NTPIPE_SHOVE_STREAM_DATA(stream);
601 /* Force thread stop */
602 InterlockedIncrement (&s->die_p);
604 /* Close pipe handle, possibly breaking it */
605 CloseHandle (s->hpipe);
607 /* Thread will end upon unblocking */
608 SetEvent (s->hev_thread);
610 /* Wait while thread terminates */
611 WaitForSingleObject (s->hthread, INFINITE);
612 CloseHandle (s->hthread);
614 /* Destroy the event */
615 CloseHandle (s->hev_thread);
621 init_shove_stream (void)
623 LSTREAM_HAS_METHOD (ntpipe_shove, writer);
624 LSTREAM_HAS_METHOD (ntpipe_shove, was_blocked_p);
625 LSTREAM_HAS_METHOD (ntpipe_shove, closer);
628 /************************************************************************/
629 /* Winsock I/O stream */
630 /************************************************************************/
631 #if defined (HAVE_SOCKETS) && !defined(HAVE_MSG_SELECT)
633 #define WINSOCK_READ_BUFFER_SIZE 1024
635 struct winsock_stream
637 LPARAM user_data; /* Any user data stored in the stream object */
638 SOCKET s; /* Socket handle (which is a Win32 handle) */
639 OVERLAPPED ov; /* Overlapped I/O structure */
640 void* buffer; /* Buffer. Allocated for input stream only */
641 unsigned long bufsize; /* Number of bytes last read */
642 unsigned long bufpos; /* Position in buffer for next fetch */
643 unsigned int error_p :1; /* I/O Error seen */
644 unsigned int eof_p :1; /* EOF Error seen */
645 unsigned int pending_p :1; /* There is a pending I/O operation */
646 unsigned int blocking_p :1; /* Last write attempt would block */
649 #define WINSOCK_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, winsock)
651 DEFINE_LSTREAM_IMPLEMENTATION ("winsock", lstream_winsock,
652 sizeof (struct winsock_stream));
655 winsock_initiate_read (struct winsock_stream *str)
657 ResetEvent (str->ov.hEvent);
660 if (!ReadFile ((HANDLE)str->s, str->buffer, WINSOCK_READ_BUFFER_SIZE,
661 &str->bufsize, &str->ov))
663 if (GetLastError () == ERROR_IO_PENDING)
665 else if (GetLastError () == ERROR_HANDLE_EOF)
670 else if (str->bufsize == 0)
675 winsock_reader (Lstream *stream, unsigned char *data, size_t size)
677 struct winsock_stream *str = WINSOCK_STREAM_DATA (stream);
679 /* If the current operation is not yet complete, there's nothing to
683 if (WaitForSingleObject (str->ov.hEvent, 0) == WAIT_TIMEOUT)
690 if (!GetOverlappedResult ((HANDLE)str->s, &str->ov, &str->bufsize, TRUE))
692 if (GetLastError() == ERROR_HANDLE_EOF)
697 if (str->bufsize == 0)
708 /* Return as much of buffer as we have */
709 size = min (size, (size_t) (str->bufsize - str->bufpos));
710 memcpy (data, (void*)((BYTE*)str->buffer + str->bufpos), size);
713 /* Read more if buffer is exhausted */
714 if (str->bufsize == str->bufpos)
715 winsock_initiate_read (str);
721 winsock_writer (Lstream *stream, const unsigned char *data, size_t size)
723 struct winsock_stream *str = WINSOCK_STREAM_DATA (stream);
727 if (WaitForSingleObject (str->ov.hEvent, 0) == WAIT_TIMEOUT)
735 if (!GetOverlappedResult ((HANDLE)str->s, &str->ov, &dw_unused, TRUE))
750 ResetEvent (str->ov.hEvent);
752 /* Docs indicate that 4th parameter to WriteFile can be NULL since this is
753 * an overlapped operation. This fails on Win95 with winsock 1.x so we
754 * supply a spare address which is ignored by Win95 anyway. Sheesh. */
755 if (WriteFile ((HANDLE)str->s, data, size, (LPDWORD)&str->buffer, &str->ov)
756 || GetLastError() == ERROR_IO_PENDING)
762 return str->error_p ? -1 : size;
766 winsock_closer (Lstream *lstr)
768 struct winsock_stream *str = WINSOCK_STREAM_DATA (lstr);
770 if (lstr->flags & LSTREAM_FL_READ)
771 shutdown (str->s, 0);
773 shutdown (str->s, 1);
775 CloseHandle ((HANDLE)str->s);
777 WaitForSingleObject (str->ov.hEvent, INFINITE);
779 if (lstr->flags & LSTREAM_FL_READ)
782 CloseHandle (str->ov.hEvent);
787 winsock_was_blocked_p (Lstream *stream)
789 struct winsock_stream *str = WINSOCK_STREAM_DATA (stream);
790 return str->blocking_p;
794 make_winsock_stream_1 (SOCKET s, LPARAM param, const char *mode)
797 Lstream *lstr = Lstream_new (lstream_winsock, mode);
798 struct winsock_stream *str = WINSOCK_STREAM_DATA (lstr);
805 str->user_data = param;
808 str->ov.hEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
810 if (lstr->flags & LSTREAM_FL_READ)
812 str->buffer = xmalloc (WINSOCK_READ_BUFFER_SIZE);
813 winsock_initiate_read (str);
816 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE;
817 XSETLSTREAM (obj, lstr);
822 make_winsock_input_stream (SOCKET s, LPARAM param)
824 return make_winsock_stream_1 (s, param, "r");
828 make_winsock_output_stream (SOCKET s, LPARAM param)
830 return make_winsock_stream_1 (s, param, "w");
834 get_winsock_stream_waitable (Lstream *lstr)
836 struct winsock_stream *str = WINSOCK_STREAM_DATA (lstr);
837 return str->ov.hEvent;
841 get_winsock_stream_param (Lstream *lstr)
843 struct winsock_stream *str = WINSOCK_STREAM_DATA (lstr);
844 return str->user_data;
848 init_winsock_stream (void)
850 LSTREAM_HAS_METHOD (winsock, reader);
851 LSTREAM_HAS_METHOD (winsock, writer);
852 LSTREAM_HAS_METHOD (winsock, closer);
853 LSTREAM_HAS_METHOD (winsock, was_blocked_p);
855 #endif /* defined (HAVE_SOCKETS) */
857 /************************************************************************/
858 /* Dispatch queue management */
859 /************************************************************************/
862 mswindows_user_event_p (Lisp_Event* sevt)
864 return (sevt->event_type == key_press_event
865 || sevt->event_type == button_press_event
866 || sevt->event_type == button_release_event
867 || sevt->event_type == misc_user_event);
871 * Add an emacs event to the proper dispatch queue
874 mswindows_enqueue_dispatch_event (Lisp_Object event)
876 int user_p = mswindows_user_event_p (XEVENT(event));
877 enqueue_event (event,
878 user_p ? &mswindows_u_dispatch_event_queue :
879 &mswindows_s_dispatch_event_queue,
880 user_p ? &mswindows_u_dispatch_event_queue_tail :
881 &mswindows_s_dispatch_event_queue_tail);
883 /* Avoid blocking on WaitMessage */
884 PostMessage (NULL, XM_BUMPQUEUE, 0, 0);
888 * Add a misc-user event to the dispatch queue.
890 * Stuff it into our own dispatch queue, so we have something
891 * to return from next_event callback.
894 mswindows_enqueue_misc_user_event (Lisp_Object channel, Lisp_Object function,
897 Lisp_Object event = Fmake_event (Qnil, Qnil);
898 Lisp_Event* e = XEVENT (event);
900 e->event_type = misc_user_event;
901 e->channel = channel;
902 e->timestamp = GetTickCount ();
903 e->event.misc.function = function;
904 e->event.misc.object = object;
906 mswindows_enqueue_dispatch_event (event);
910 mswindows_enqueue_magic_event (HWND hwnd, UINT msg)
912 Lisp_Object emacs_event = Fmake_event (Qnil, Qnil);
913 Lisp_Event* event = XEVENT (emacs_event);
915 event->channel = hwnd ? mswindows_find_frame (hwnd) : Qnil;
916 event->timestamp = GetMessageTime();
917 event->event_type = magic_event;
918 EVENT_MSWINDOWS_MAGIC_TYPE (event) = msg;
920 mswindows_enqueue_dispatch_event (emacs_event);
924 mswindows_enqueue_process_event (Lisp_Process* p)
926 Lisp_Object emacs_event = Fmake_event (Qnil, Qnil);
927 Lisp_Event* event = XEVENT (emacs_event);
929 XSETPROCESS (process, p);
931 event->event_type = process_event;
932 event->timestamp = GetTickCount ();
933 event->event.process.process = process;
935 mswindows_enqueue_dispatch_event (emacs_event);
939 mswindows_enqueue_mouse_button_event (HWND hwnd, UINT msg, POINTS where, DWORD when)
942 /* We always use last message time, because mouse button
943 events may get delayed, and XEmacs double click
944 recognition will fail */
946 Lisp_Object emacs_event = Fmake_event (Qnil, Qnil);
947 Lisp_Event* event = XEVENT(emacs_event);
949 event->channel = mswindows_find_frame(hwnd);
950 event->timestamp = when;
951 event->event.button.button =
952 (msg==WM_LBUTTONDOWN || msg==WM_LBUTTONUP) ? 1 :
953 ((msg==WM_RBUTTONDOWN || msg==WM_RBUTTONUP) ? 3 : 2);
954 event->event.button.x = where.x;
955 event->event.button.y = where.y;
956 event->event.button.modifiers = mswindows_modifier_state (NULL, 0);
958 if (msg==WM_LBUTTONDOWN || msg==WM_MBUTTONDOWN ||
961 event->event_type = button_press_event;
963 /* we need this to make sure the main window regains the focus
964 from control subwindows */
965 if (GetFocus() != hwnd)
968 mswindows_enqueue_magic_event (hwnd, WM_SETFOCUS);
973 event->event_type = button_release_event;
977 mswindows_enqueue_dispatch_event (emacs_event);
981 mswindows_enqueue_keypress_event (HWND hwnd, Lisp_Object keysym, int mods)
983 Lisp_Object emacs_event = Fmake_event (Qnil, Qnil);
984 Lisp_Event* event = XEVENT(emacs_event);
986 event->channel = mswindows_find_console(hwnd);
987 event->timestamp = GetMessageTime();
988 event->event_type = key_press_event;
989 event->event.key.keysym = keysym;
990 event->event.key.modifiers = mods;
991 mswindows_enqueue_dispatch_event (emacs_event);
995 * Remove and return the first emacs event on the dispatch queue.
996 * Give a preference to user events over non-user ones.
999 mswindows_dequeue_dispatch_event (void)
1004 assert (!NILP(mswindows_u_dispatch_event_queue) ||
1005 !NILP(mswindows_s_dispatch_event_queue));
1007 event = dequeue_event (
1008 NILP(mswindows_u_dispatch_event_queue) ?
1009 &mswindows_s_dispatch_event_queue :
1010 &mswindows_u_dispatch_event_queue,
1011 NILP(mswindows_u_dispatch_event_queue) ?
1012 &mswindows_s_dispatch_event_queue_tail :
1013 &mswindows_u_dispatch_event_queue_tail);
1015 sevt = XEVENT(event);
1016 if (sevt->event_type == key_press_event
1017 && (sevt->event.key.modifiers & FAKE_MOD_QUIT))
1019 sevt->event.key.modifiers &= ~FAKE_MOD_QUIT;
1020 --mswindows_quit_chars_count;
1027 * Remove and return the first emacs event on the dispatch queue that matches
1028 * the supplied event.
1029 * Timeout event matches if interval_id is equal to that of the given event.
1030 * Keypress event matches if logical AND between modifiers bitmask of the
1031 * event in the queue and that of the given event is non-zero.
1032 * For all other event types, this function aborts.
1036 mswindows_cancel_dispatch_event (Lisp_Event *match)
1039 Lisp_Object previous_event = Qnil;
1040 int user_p = mswindows_user_event_p (match);
1041 Lisp_Object* head = user_p ? &mswindows_u_dispatch_event_queue :
1042 &mswindows_s_dispatch_event_queue;
1043 Lisp_Object* tail = user_p ? &mswindows_u_dispatch_event_queue_tail :
1044 &mswindows_s_dispatch_event_queue_tail;
1046 assert (match->event_type == timeout_event
1047 || match->event_type == key_press_event);
1049 EVENT_CHAIN_LOOP (event, *head)
1051 Lisp_Event *e = XEVENT (event);
1052 if ((e->event_type == match->event_type) &&
1053 ((e->event_type == timeout_event) ?
1054 (e->event.timeout.interval_id == match->event.timeout.interval_id) :
1055 /* Must be key_press_event */
1056 ((e->event.key.modifiers & match->event.key.modifiers) != 0)))
1058 if (NILP (previous_event))
1059 dequeue_event (head, tail);
1062 XSET_EVENT_NEXT (previous_event, XEVENT_NEXT (event));
1063 if (EQ (*tail, event))
1064 *tail = previous_event;
1069 previous_event = event;
1074 #ifndef HAVE_MSG_SELECT
1075 /************************************************************************/
1076 /* Waitable handles manipulation */
1077 /************************************************************************/
1079 find_waitable_handle (HANDLE h)
1082 for (i = 0; i < mswindows_waitable_count; ++i)
1083 if (mswindows_waitable_handles[i] == h)
1090 add_waitable_handle (HANDLE h)
1092 assert (find_waitable_handle (h) < 0);
1093 if (mswindows_waitable_count == MAX_WAITABLE)
1096 mswindows_waitable_handles [mswindows_waitable_count++] = h;
1101 remove_waitable_handle (HANDLE h)
1103 int ix = find_waitable_handle (h);
1107 mswindows_waitable_handles [ix] =
1108 mswindows_waitable_handles [--mswindows_waitable_count];
1110 #endif /* HAVE_MSG_SELECT */
1113 /************************************************************************/
1115 /************************************************************************/
1118 mswindows_modal_loop_error_handler (Lisp_Object cons_sig_data,
1119 Lisp_Object u_n_u_s_e_d)
1121 mswindows_error_caught_in_modal_loop = cons_sig_data;
1126 mswindows_protect_modal_loop (Lisp_Object (*bfun) (Lisp_Object barg),
1131 ++mswindows_in_modal_loop;
1132 tmp = condition_case_1 (Qt,
1134 mswindows_modal_loop_error_handler, Qnil);
1135 --mswindows_in_modal_loop;
1141 mswindows_unmodalize_signal_maybe (void)
1143 if (!NILP (mswindows_error_caught_in_modal_loop))
1145 /* Got an error while messages were pumped while
1146 in window procedure - have to resignal */
1147 Lisp_Object sym = XCAR (mswindows_error_caught_in_modal_loop);
1148 Lisp_Object data = XCDR (mswindows_error_caught_in_modal_loop);
1149 mswindows_error_caught_in_modal_loop = Qnil;
1150 Fsignal (sym, data);
1155 * This is an unsafe part of event pump, guarded by
1156 * condition_case. See mswindows_pump_outstanding_events
1159 mswindows_unsafe_pump_events (Lisp_Object u_n_u_s_e_d)
1161 /* This function can call lisp */
1162 Lisp_Object event = Fmake_event (Qnil, Qnil);
1163 struct gcpro gcpro1;
1164 int do_redisplay = 0;
1167 while (detect_input_pending ())
1169 Fnext_event (event, Qnil);
1170 Fdispatch_event (event);
1177 Fdeallocate_event (event);
1180 /* Qt becomes return value of mswindows_pump_outstanding_events
1186 * This function pumps emacs events, while available, by using
1187 * next_message/dispatch_message loop. Errors are trapped around
1188 * the loop so the function always returns.
1190 * Windows message queue is not looked into during the call,
1191 * neither are waitable handles checked. The function pumps
1192 * thus only dispatch events already queued, as well as those
1193 * resulted in dispatching thereof. This is done by setting
1194 * module local variable mswindows_in_modal_loop to nonzero.
1196 * Return value is Qt if no errors was trapped, or Qunbound if
1197 * there was an error.
1199 * In case of error, a cons representing the error, in the
1200 * form (SIGNAL . DATA), is stored in the module local variable
1201 * mswindows_error_caught_in_modal_loop. This error is signaled
1202 * again when DispatchMessage returns. Thus, Windows internal
1203 * modal loops are protected against throws, which are proven
1204 * to corrupt internal Windows structures.
1206 * In case of success, mswindows_error_caught_in_modal_loop is
1209 * If the value of mswindows_error_caught_in_modal_loop is not
1210 * nil already upon entry, the function just returns non-nil.
1211 * This situation means that a new event has been queued while
1212 * in cancel mode. The event will be dequeued on the next regular
1213 * call of next-event; the pump is off since error is caught.
1214 * The caller must *unconditionally* cancel modal loop if the
1215 * value returned by this function is nil. Otherwise, everything
1216 * will become frozen until the modal loop exits under normal
1217 * condition (scrollbar drag is released, menu closed etc.)
1220 mswindows_pump_outstanding_events (void)
1222 /* This function can call lisp */
1224 Lisp_Object result = Qt;
1225 struct gcpro gcpro1;
1228 if (NILP(mswindows_error_caught_in_modal_loop))
1229 result = mswindows_protect_modal_loop (mswindows_unsafe_pump_events, Qnil);
1235 * KEYBOARD_ONLY_P is set to non-zero when we are called from
1236 * QUITP, and are interesting in keyboard messages only.
1239 mswindows_drain_windows_queue (void)
1243 /* should call mswindows_need_event_in_modal_loop() if in modal loop */
1244 assert (!mswindows_in_modal_loop);
1246 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
1248 /* We have to translate messages that are not sent to the main
1249 window. This is so that key presses work ok in things like
1250 edit fields. However, we *musn't* translate message for the
1251 main window as this is handled in the wnd proc.
1252 We also have to avoid generating paint magic events for windows
1253 that aren't XEmacs frames */
1254 if (GetWindowLong (msg.hwnd, GWL_STYLE) & (WS_CHILD|WS_POPUP))
1256 TranslateMessage (&msg);
1258 else if (msg.message == WM_PAINT)
1260 struct mswindows_frame* msframe;
1262 /* hdc will be NULL unless this is a subwindow - in which case we
1263 shouldn't have received a paint message for it here. */
1264 assert (msg.wParam == 0);
1266 /* Queue a magic event for handling when safe */
1267 msframe = FRAME_MSWINDOWS_DATA (
1268 XFRAME (mswindows_find_frame (msg.hwnd)));
1269 if (!msframe->paint_pending)
1271 msframe->paint_pending = 1;
1272 mswindows_enqueue_magic_event (msg.hwnd, WM_PAINT);
1274 /* Don't dispatch. WM_PAINT is always the last message in the
1275 queue so it's OK to just return. */
1278 DispatchMessage (&msg);
1279 mswindows_unmodalize_signal_maybe ();
1284 * This is a special flavor of the mswindows_need_event function,
1285 * used while in event pump. Actually, there is only kind of events
1286 * allowed while in event pump: a timer. An attempt to fetch any
1287 * other event leads to a deadlock, as there's no source of user input
1288 * ('cause event pump mirrors windows modal loop, which is a sole
1289 * owner of thread message queue).
1291 * To detect this, we use a counter of active timers, and allow
1292 * fetching WM_TIMER messages. Instead of trying to fetch a WM_TIMER
1293 * which will never come when there are no pending timers, which leads
1294 * to deadlock, we simply signal an error.
1297 mswindows_need_event_in_modal_loop (int badly_p)
1301 /* Check if already have one */
1302 if (!NILP (mswindows_u_dispatch_event_queue)
1303 || !NILP (mswindows_s_dispatch_event_queue))
1306 /* No event is ok */
1310 /* We do not check the _u_ queue, because timers go to _s_ */
1311 while (NILP (mswindows_s_dispatch_event_queue))
1313 /* We'll deadlock if go waiting */
1314 if (mswindows_pending_timers_count == 0)
1315 error ("Deadlock due to an attempt to call next-event in a wrong context");
1317 /* Fetch and dispatch any pending timers */
1318 GetMessage (&msg, NULL, WM_TIMER, WM_TIMER);
1319 DispatchMessage (&msg);
1324 * This drains the event queue and fills up two internal queues until
1325 * an event of a type specified by USER_P is retrieved.
1328 * Used by emacs_mswindows_event_pending_p and emacs_mswindows_next_event
1331 mswindows_need_event (int badly_p)
1335 if (mswindows_in_modal_loop)
1337 mswindows_need_event_in_modal_loop (badly_p);
1341 while (NILP (mswindows_u_dispatch_event_queue)
1342 && NILP (mswindows_s_dispatch_event_queue))
1344 #ifdef HAVE_MSG_SELECT
1346 SELECT_TYPE temp_mask = input_wait_mask;
1347 EMACS_TIME sometime;
1348 EMACS_SELECT_TIME select_time_to_block, *pointer_to_this;
1351 pointer_to_this = 0;
1354 EMACS_SET_SECS_USECS (sometime, 0, 0);
1355 EMACS_TIME_TO_SELECT_TIME (sometime, select_time_to_block);
1356 pointer_to_this = &select_time_to_block;
1359 active = select (MAXDESC, &temp_mask, 0, 0, pointer_to_this);
1364 return; /* timeout */
1366 else if (active > 0)
1368 if (FD_ISSET (windows_fd, &temp_mask))
1370 mswindows_drain_windows_queue ();
1375 /* Look for a TTY event */
1376 for (i = 0; i < MAXDESC-1; i++)
1378 /* To avoid race conditions (among other things, an infinite
1379 loop when called from Fdiscard_input()), we must return
1380 user events ahead of process events. */
1381 if (FD_ISSET (i, &temp_mask) && FD_ISSET (i, &tty_only_mask))
1383 struct console *c = tty_find_console_from_fd (i);
1384 Lisp_Object emacs_event = Fmake_event (Qnil, Qnil);
1385 Lisp_Event* event = XEVENT (emacs_event);
1388 if (read_event_from_tty_or_stream_desc (event, c, i))
1390 mswindows_enqueue_dispatch_event (emacs_event);
1396 /* Look for a process event */
1397 for (i = 0; i < MAXDESC-1; i++)
1399 if (FD_ISSET (i, &temp_mask))
1401 if (FD_ISSET (i, &process_only_mask))
1404 get_process_from_usid (FD_TO_USID(i));
1406 mswindows_enqueue_process_event (p);
1410 /* We might get here when a fake event came
1411 through a signal. Return a dummy event, so
1412 that a cycle of the command loop will
1414 drain_signal_event_pipe ();
1415 mswindows_enqueue_magic_event (NULL, XM_BUMPQUEUE);
1421 else if (active==-1)
1425 /* something bad happened */
1434 /* Now try getting a message or process event */
1435 active = MsgWaitForMultipleObjects (mswindows_waitable_count,
1436 mswindows_waitable_handles,
1437 FALSE, badly_p ? INFINITE : 0,
1440 /* This will assert if handle being waited for becomes abandoned.
1441 Not the case currently tho */
1442 assert ((!badly_p && active == WAIT_TIMEOUT) ||
1443 (active >= WAIT_OBJECT_0 &&
1444 active <= WAIT_OBJECT_0 + mswindows_waitable_count));
1446 if (active == WAIT_TIMEOUT)
1448 /* No luck trying - just return what we've already got */
1451 else if (active == WAIT_OBJECT_0 + mswindows_waitable_count)
1453 /* Got your message, thanks */
1454 mswindows_drain_windows_queue ();
1458 int ix = active - WAIT_OBJECT_0;
1459 /* First, try to find which process' output has signaled */
1461 get_process_from_usid (HANDLE_TO_USID (mswindows_waitable_handles[ix]));
1464 /* Found a signaled process input handle */
1465 mswindows_enqueue_process_event (p);
1469 /* None. This means that the process handle itself has signaled.
1470 Remove the handle from the wait vector, and make status_notify
1471 note the exited process */
1472 mswindows_waitable_handles [ix] =
1473 mswindows_waitable_handles [--mswindows_waitable_count];
1474 kick_status_notify ();
1475 /* Have to return something: there may be no accompanying
1477 mswindows_enqueue_magic_event (NULL, XM_BUMPQUEUE);
1484 /************************************************************************/
1485 /* Event generators */
1486 /************************************************************************/
1489 * Callback procedure for synchronous timer messages
1491 static void CALLBACK
1492 mswindows_wm_timer_callback (HWND hwnd, UINT umsg, UINT id_timer, DWORD dwtime)
1494 Lisp_Object emacs_event = Fmake_event (Qnil, Qnil);
1495 Lisp_Event *event = XEVENT (emacs_event);
1497 if (KillTimer (NULL, id_timer))
1498 --mswindows_pending_timers_count;
1500 event->channel = Qnil;
1501 event->timestamp = dwtime;
1502 event->event_type = timeout_event;
1503 event->event.timeout.interval_id = id_timer;
1504 event->event.timeout.function = Qnil;
1505 event->event.timeout.object = Qnil;
1507 mswindows_enqueue_dispatch_event (emacs_event);
1511 * Callback procedure for dde messages
1513 * We execute a dde Open("file") by simulating a file drop, so dde support
1514 * depends on dnd support.
1516 #ifdef HAVE_DRAGNDROP
1518 mswindows_dde_callback (UINT uType, UINT uFmt, HCONV hconv,
1519 HSZ hszTopic, HSZ hszItem, HDDEDATA hdata,
1520 DWORD dwData1, DWORD dwData2)
1525 if (!DdeCmpStringHandles (hszTopic, mswindows_dde_topic_system))
1526 return (HDDEDATA)TRUE;
1527 return (HDDEDATA)FALSE;
1529 case XTYP_WILDCONNECT:
1531 /* We only support one {service,topic} pair */
1532 HSZPAIR pairs[2] = {
1533 { mswindows_dde_service, mswindows_dde_topic_system }, { 0, 0 } };
1535 if (!(hszItem || DdeCmpStringHandles (hszItem, mswindows_dde_service)) &&
1536 !(hszTopic || DdeCmpStringHandles (hszTopic, mswindows_dde_topic_system)))
1537 return (DdeCreateDataHandle (mswindows_dde_mlid, (LPBYTE)pairs,
1538 sizeof (pairs), 0L, 0, uFmt, 0));
1540 return (HDDEDATA)NULL;
1543 if (!DdeCmpStringHandles (hszTopic, mswindows_dde_topic_system))
1545 DWORD len = DdeGetData (hdata, NULL, 0, 0);
1546 LPBYTE cmd = (LPBYTE) alloca (len+1);
1549 struct gcpro gcpro1, gcpro2;
1550 Lisp_Object l_dndlist = Qnil;
1551 Lisp_Object emacs_event = Fmake_event (Qnil, Qnil);
1552 Lisp_Object frmcons, devcons, concons;
1553 Lisp_Event *event = XEVENT (emacs_event);
1555 DdeGetData (hdata, cmd, len, 0);
1557 DdeFreeDataHandle (hdata);
1559 /* Check syntax & that it's an [Open("foo")] command, which we
1560 * treat like a file drop */
1561 /* #### Ought to be generalised and accept some other commands */
1564 if (strnicmp (cmd, MSWINDOWS_DDE_ITEM_OPEN,
1565 strlen (MSWINDOWS_DDE_ITEM_OPEN)))
1566 return DDE_FNOTPROCESSED;
1567 cmd += strlen (MSWINDOWS_DDE_ITEM_OPEN);
1570 if (*cmd!='(' || *(cmd+1)!='\"')
1571 return DDE_FNOTPROCESSED;
1573 while (*end && *end!='\"')
1576 return DDE_FNOTPROCESSED;
1579 return DDE_FNOTPROCESSED;
1583 return DDE_FNOTPROCESSED;
1586 filename = alloca (cygwin32_win32_to_posix_path_list_buf_size (cmd) + 5);
1587 strcpy (filename, "file:");
1588 cygwin32_win32_to_posix_path_list (cmd, filename+5);
1590 dostounix_filename (cmd);
1591 filename = alloca (strlen (cmd)+6);
1592 strcpy (filename, "file:");
1593 strcat (filename, cmd);
1595 GCPRO2 (emacs_event, l_dndlist);
1596 l_dndlist = make_string (filename, strlen (filename));
1598 /* Find a mswindows frame */
1599 event->channel = Qnil;
1600 FRAME_LOOP_NO_BREAK (frmcons, devcons, concons)
1602 Lisp_Object frame = XCAR (frmcons);
1603 if (FRAME_TYPE_P (XFRAME (frame), mswindows))
1604 event->channel = frame;
1606 assert (!NILP (event->channel));
1608 event->timestamp = GetTickCount();
1609 event->event_type = misc_user_event;
1610 event->event.misc.button = 1;
1611 event->event.misc.modifiers = 0;
1612 event->event.misc.x = -1;
1613 event->event.misc.y = -1;
1614 event->event.misc.function = Qdragdrop_drop_dispatch;
1615 event->event.misc.object = Fcons (Qdragdrop_URL,
1616 Fcons (l_dndlist, Qnil));
1617 mswindows_enqueue_dispatch_event (emacs_event);
1619 return (HDDEDATA) DDE_FACK;
1621 DdeFreeDataHandle (hdata);
1622 return (HDDEDATA) DDE_FNOTPROCESSED;
1625 return (HDDEDATA) NULL;
1631 * Helper to do repainting - repaints can happen both from the windows
1632 * procedure and from magic events
1635 mswindows_handle_paint (struct frame *frame)
1637 HWND hwnd = FRAME_MSWINDOWS_HANDLE (frame);
1639 /* According to the docs we need to check GetUpdateRect() before
1640 actually doing a WM_PAINT */
1641 if (GetUpdateRect (hwnd, NULL, FALSE))
1643 PAINTSTRUCT paintStruct;
1644 int x, y, width, height;
1646 BeginPaint (hwnd, &paintStruct);
1647 x = paintStruct.rcPaint.left;
1648 y = paintStruct.rcPaint.top;
1649 width = paintStruct.rcPaint.right - paintStruct.rcPaint.left;
1650 height = paintStruct.rcPaint.bottom - paintStruct.rcPaint.top;
1651 /* Normally we want to ignore expose events when child
1652 windows are unmapped, however once we are in the guts of
1653 WM_PAINT we need to make sure that we don't register
1654 unmaps then because they will not actually occur. */
1655 if (!check_for_ignored_expose (frame, x, y, width, height))
1657 hold_ignored_expose_registration = 1;
1658 mswindows_redraw_exposed_area (frame, x, y, width, height);
1659 hold_ignored_expose_registration = 0;
1661 EndPaint (hwnd, &paintStruct);
1666 * Returns 1 if a key is a real modifier or special key, which
1667 * is better handled by DefWindowProc
1670 key_needs_default_processing_p (UINT vkey)
1672 if (mswindows_alt_by_itself_activates_menu && vkey == VK_MENU)
1679 * The windows procedure for the window class XEMACS_CLASS
1682 mswindows_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1684 /* Note: Remember to initialize emacs_event and event before use.
1685 This code calls code that can GC. You must GCPRO before calling such code. */
1686 Lisp_Object emacs_event = Qnil;
1687 Lisp_Object fobj = Qnil;
1690 struct frame *frame;
1691 struct mswindows_frame* msframe;
1693 assert (!GetWindowLong (hwnd, GWL_USERDATA));
1696 case WM_DESTROYCLIPBOARD:
1697 /* We own the clipboard and someone else wants it. Delete our
1698 cached copy of the clipboard contents so we'll ask for it from
1699 Windows again when someone does a paste. */
1700 handle_selection_clear(QCLIPBOARD);
1704 /* Erase background only during non-dynamic sizing */
1705 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
1706 if (msframe->sizing && !mswindows_dynamic_frame_resize)
1711 fobj = mswindows_find_frame (hwnd);
1712 mswindows_enqueue_misc_user_event (fobj, Qeval, list3 (Qdelete_frame, fobj, Qt));
1717 /* See Win95 comment under WM_KEYDOWN */
1720 int should_set_keymap = 0;
1722 if (wParam == VK_CONTROL)
1724 GetKeyboardState (keymap);
1725 keymap [(lParam & 0x1000000) ? VK_RCONTROL : VK_LCONTROL] &= ~0x80;
1726 should_set_keymap = 1;
1728 else if (wParam == VK_MENU)
1730 GetKeyboardState (keymap);
1731 keymap [(lParam & 0x1000000) ? VK_RMENU : VK_LMENU] &= ~0x80;
1732 should_set_keymap = 1;
1735 if (should_set_keymap
1736 && (message != WM_SYSKEYUP
1737 || NILP (Vmenu_accelerator_enabled)))
1738 SetKeyboardState (keymap);
1741 if (key_needs_default_processing_p (wParam))
1748 /* In some locales the right-hand Alt key is labelled AltGr. This key
1749 * should produce alternative charcaters when combined with another key.
1750 * eg on a German keyboard pressing AltGr+q should produce '@'.
1751 * AltGr generates exactly the same keystrokes as LCtrl+RAlt. But if
1752 * TranslateMessage() is called with *any* combination of Ctrl+Alt down,
1753 * it translates as if AltGr were down.
1754 * We get round this by removing all modifiers from the keymap before
1755 * calling TranslateMessage() unless AltGr is *really* down. */
1758 int has_AltGr = mswindows_current_layout_has_AltGr ();
1760 int extendedp = lParam & 0x1000000;
1763 frame = XFRAME (mswindows_find_frame (hwnd));
1764 GetKeyboardState (keymap);
1765 mods = mswindows_modifier_state (keymap, has_AltGr);
1767 /* Handle non-printables */
1768 if (!NILP (keysym = mswindows_key_to_emacs_keysym (wParam, mods,
1770 mswindows_enqueue_keypress_event (hwnd, keysym, mods);
1771 else /* Normal keys & modifiers */
1773 Emchar quit_ch = CONSOLE_QUIT_CHAR (XCONSOLE (mswindows_find_console (hwnd)));
1774 BYTE keymap_orig[256];
1775 POINT pnt = { LOWORD (GetMessagePos()), HIWORD (GetMessagePos()) };
1777 int potential_accelerator = 0;
1778 int got_accelerator = 0;
1781 msg.message = message;
1782 msg.wParam = wParam;
1783 msg.lParam = lParam;
1784 msg.time = GetMessageTime();
1787 /* GetKeyboardState() does not work as documented on Win95. We have
1788 * to loosely track Left and Right modifiers on behalf of the OS,
1789 * without screwing up Windows NT which tracks them properly. */
1790 if (wParam == VK_CONTROL)
1791 keymap [extendedp ? VK_RCONTROL : VK_LCONTROL] |= 0x80;
1792 else if (wParam == VK_MENU)
1793 keymap [extendedp ? VK_RMENU : VK_LMENU] |= 0x80;
1795 memcpy (keymap_orig, keymap, 256);
1797 if (!NILP (Vmenu_accelerator_enabled) &&
1798 !(mods & XEMACS_MOD_SHIFT) && message == WM_SYSKEYDOWN)
1799 potential_accelerator = 1;
1801 /* Remove shift modifier from an ascii character */
1802 mods &= ~XEMACS_MOD_SHIFT;
1804 /* Clear control and alt modifiers unless AltGr is pressed */
1805 keymap [VK_RCONTROL] = 0;
1806 keymap [VK_LMENU] = 0;
1807 if (!has_AltGr || !(keymap [VK_LCONTROL] & 0x80)
1808 || !(keymap [VK_RMENU] & 0x80))
1810 keymap [VK_LCONTROL] = 0;
1811 keymap [VK_CONTROL] = 0;
1812 keymap [VK_RMENU] = 0;
1813 keymap [VK_MENU] = 0;
1815 SetKeyboardState (keymap);
1817 /* Maybe generate some WM_[SYS]CHARs in the queue */
1818 TranslateMessage (&msg);
1820 while (PeekMessage (&tranmsg, hwnd, WM_CHAR, WM_CHAR, PM_REMOVE)
1821 || PeekMessage (&tranmsg, hwnd, WM_SYSCHAR, WM_SYSCHAR, PM_REMOVE))
1824 WPARAM ch = tranmsg.wParam;
1826 /* If a quit char with no modifiers other than control and
1827 shift, then mark it with a fake modifier, which is removed
1828 upon dequeueing the event */
1829 /* #### This might also not withstand localization, if
1830 quit character is not a latin-1 symbol */
1831 if (((quit_ch < ' ' && (mods & XEMACS_MOD_CONTROL) && quit_ch + 'a' - 1 == ch)
1832 || (quit_ch >= ' ' && !(mods & XEMACS_MOD_CONTROL) && quit_ch == ch))
1833 && ((mods & ~(XEMACS_MOD_CONTROL | XEMACS_MOD_SHIFT)) == 0))
1835 mods1 |= FAKE_MOD_QUIT;
1836 ++mswindows_quit_chars_count;
1838 else if (potential_accelerator && !got_accelerator &&
1839 msw_char_is_accelerator (frame, ch))
1841 got_accelerator = 1;
1844 mswindows_enqueue_keypress_event (hwnd, make_char (ch), mods1);
1846 SetKeyboardState (keymap_orig);
1847 /* This generates WM_SYSCHAR messages, which are interpreted
1848 by DefWindowProc as the menu selections. */
1849 if (got_accelerator)
1851 TranslateMessage (&msg);
1856 if (key_needs_default_processing_p (wParam))
1861 case WM_MBUTTONDOWN:
1863 /* Real middle mouse button has nothing to do with emulated one:
1864 if one wants to exercise fingers playing chords on the mouse,
1865 he is allowed to do that! */
1866 mswindows_enqueue_mouse_button_event (hwnd, message,
1867 MAKEPOINTS (lParam), GetMessageTime());
1871 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
1872 msframe->last_click_time = GetMessageTime();
1874 KillTimer (hwnd, BUTTON_2_TIMER_ID);
1875 msframe->button2_need_lbutton = 0;
1876 if (msframe->ignore_next_lbutton_up)
1878 msframe->ignore_next_lbutton_up = 0;
1880 else if (msframe->button2_is_down)
1882 msframe->button2_is_down = 0;
1883 msframe->ignore_next_rbutton_up = 1;
1884 mswindows_enqueue_mouse_button_event (hwnd, WM_MBUTTONUP,
1885 MAKEPOINTS (lParam), GetMessageTime());
1889 if (msframe->button2_need_rbutton)
1891 msframe->button2_need_rbutton = 0;
1892 mswindows_enqueue_mouse_button_event (hwnd, WM_LBUTTONDOWN,
1893 MAKEPOINTS (lParam), GetMessageTime());
1895 mswindows_enqueue_mouse_button_event (hwnd, WM_LBUTTONUP,
1896 MAKEPOINTS (lParam), GetMessageTime());
1901 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
1902 msframe->last_click_time = GetMessageTime();
1904 KillTimer (hwnd, BUTTON_2_TIMER_ID);
1905 msframe->button2_need_rbutton = 0;
1906 if (msframe->ignore_next_rbutton_up)
1908 msframe->ignore_next_rbutton_up = 0;
1910 else if (msframe->button2_is_down)
1912 msframe->button2_is_down = 0;
1913 msframe->ignore_next_lbutton_up = 1;
1914 mswindows_enqueue_mouse_button_event (hwnd, WM_MBUTTONUP,
1915 MAKEPOINTS (lParam), GetMessageTime());
1919 if (msframe->button2_need_lbutton)
1921 msframe->button2_need_lbutton = 0;
1922 mswindows_enqueue_mouse_button_event (hwnd, WM_RBUTTONDOWN,
1923 MAKEPOINTS (lParam), GetMessageTime());
1925 mswindows_enqueue_mouse_button_event (hwnd, WM_RBUTTONUP,
1926 MAKEPOINTS (lParam), GetMessageTime());
1930 case WM_LBUTTONDOWN:
1931 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
1933 if (msframe->button2_need_lbutton)
1935 KillTimer (hwnd, BUTTON_2_TIMER_ID);
1936 msframe->button2_need_lbutton = 0;
1937 msframe->button2_need_rbutton = 0;
1938 if (mswindows_button2_near_enough (msframe->last_click_point, MAKEPOINTS (lParam)))
1940 mswindows_enqueue_mouse_button_event (hwnd, WM_MBUTTONDOWN,
1941 MAKEPOINTS (lParam), GetMessageTime());
1942 msframe->button2_is_down = 1;
1946 mswindows_enqueue_mouse_button_event (hwnd, WM_RBUTTONDOWN,
1947 msframe->last_click_point, msframe->last_click_time);
1948 mswindows_enqueue_mouse_button_event (hwnd, WM_LBUTTONDOWN,
1949 MAKEPOINTS (lParam), GetMessageTime());
1954 mswindows_set_chord_timer (hwnd);
1955 msframe->button2_need_rbutton = 1;
1956 msframe->last_click_point = MAKEPOINTS (lParam);
1958 msframe->last_click_time = GetMessageTime();
1961 case WM_RBUTTONDOWN:
1962 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
1964 if (msframe->button2_need_rbutton)
1966 KillTimer (hwnd, BUTTON_2_TIMER_ID);
1967 msframe->button2_need_lbutton = 0;
1968 msframe->button2_need_rbutton = 0;
1969 if (mswindows_button2_near_enough (msframe->last_click_point, MAKEPOINTS (lParam)))
1971 mswindows_enqueue_mouse_button_event (hwnd, WM_MBUTTONDOWN,
1972 MAKEPOINTS (lParam), GetMessageTime());
1973 msframe->button2_is_down = 1;
1977 mswindows_enqueue_mouse_button_event (hwnd, WM_LBUTTONDOWN,
1978 msframe->last_click_point, msframe->last_click_time);
1979 mswindows_enqueue_mouse_button_event (hwnd, WM_RBUTTONDOWN,
1980 MAKEPOINTS (lParam), GetMessageTime());
1985 mswindows_set_chord_timer (hwnd);
1986 msframe->button2_need_lbutton = 1;
1987 msframe->last_click_point = MAKEPOINTS (lParam);
1989 msframe->last_click_time = GetMessageTime();
1993 if (wParam == BUTTON_2_TIMER_ID)
1995 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
1996 KillTimer (hwnd, BUTTON_2_TIMER_ID);
1998 if (msframe->button2_need_lbutton)
2000 msframe->button2_need_lbutton = 0;
2001 mswindows_enqueue_mouse_button_event (hwnd, WM_RBUTTONDOWN,
2002 msframe->last_click_point, msframe->last_click_time);
2004 else if (msframe->button2_need_rbutton)
2006 msframe->button2_need_rbutton = 0;
2007 mswindows_enqueue_mouse_button_event (hwnd, WM_LBUTTONDOWN,
2008 msframe->last_click_point, msframe->last_click_time);
2012 assert ("Spurious timer fired" == 0);
2016 /* Optimization: don't report mouse movement while size is changing */
2017 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
2018 if (!msframe->sizing)
2020 /* When waiting for the second mouse button to finish
2021 button2 emulation, and have moved too far, just pretend
2022 as if timer has expired. This improves drag-select feedback */
2023 if ((msframe->button2_need_lbutton || msframe->button2_need_rbutton)
2024 && !mswindows_button2_near_enough (msframe->last_click_point,
2025 MAKEPOINTS (lParam)))
2027 KillTimer (hwnd, BUTTON_2_TIMER_ID);
2028 SendMessage (hwnd, WM_TIMER, BUTTON_2_TIMER_ID, 0);
2031 emacs_event = Fmake_event (Qnil, Qnil);
2032 event = XEVENT(emacs_event);
2034 event->channel = mswindows_find_frame(hwnd);
2035 event->timestamp = GetMessageTime();
2036 event->event_type = pointer_motion_event;
2037 event->event.motion.x = MAKEPOINTS(lParam).x;
2038 event->event.motion.y = MAKEPOINTS(lParam).y;
2039 event->event.motion.modifiers = mswindows_modifier_state (NULL, 0);
2041 mswindows_enqueue_dispatch_event (emacs_event);
2047 /* Queue a `cancel-mode-internal' misc user event, so mouse
2048 selection would be canceled if any */
2049 mswindows_enqueue_misc_user_event (mswindows_find_frame (hwnd),
2050 Qcancel_mode_internal, Qnil);
2055 LPNMHDR nmhdr = (LPNMHDR)lParam;
2057 if (nmhdr->code == TTN_NEEDTEXT)
2059 #ifdef HAVE_TOOLBARS
2060 LPTOOLTIPTEXT tttext = (LPTOOLTIPTEXT)lParam;
2063 /* find out which toolbar */
2064 frame = XFRAME (mswindows_find_frame (hwnd));
2065 btext = mswindows_get_toolbar_button_text ( frame,
2068 tttext->lpszText = NULL;
2069 tttext->hinst = NULL;
2073 /* I think this is safe since the text will only go away
2074 when the toolbar does...*/
2075 TO_EXTERNAL_FORMAT (LISP_STRING, btext,
2076 C_STRING_ALLOCA, tttext->lpszText,
2081 /* handle tree view callbacks */
2082 else if (nmhdr->code == TVN_SELCHANGED)
2084 NM_TREEVIEW* ptree = (NM_TREEVIEW*)lParam;
2085 frame = XFRAME (mswindows_find_frame (hwnd));
2086 mswindows_handle_gui_wm_command (frame, 0, ptree->itemNew.lParam);
2088 /* handle tab control callbacks */
2089 else if (nmhdr->code == TCN_SELCHANGE)
2092 int idx = SendMessage (nmhdr->hwndFrom, TCM_GETCURSEL, 0, 0);
2093 frame = XFRAME (mswindows_find_frame (hwnd));
2095 item.mask = TCIF_PARAM;
2096 SendMessage (nmhdr->hwndFrom, TCM_GETITEM, (WPARAM)idx,
2099 mswindows_handle_gui_wm_command (frame, 0, item.lParam);
2105 /* hdc will be NULL unless this is a subwindow - in which case we
2106 shouldn't have received a paint message for it here. */
2107 assert (wParam == 0);
2109 /* Can't queue a magic event because windows goes modal and sends paint
2110 messages directly to the windows procedure when doing solid drags
2111 and the message queue doesn't get processed. */
2112 mswindows_handle_paint (XFRAME (mswindows_find_frame (hwnd)));
2116 /* We only care about this message if our size has really changed */
2117 if (wParam==SIZE_RESTORED || wParam==SIZE_MAXIMIZED || wParam==SIZE_MINIMIZED)
2122 fobj = mswindows_find_frame (hwnd);
2123 frame = XFRAME (fobj);
2124 msframe = FRAME_MSWINDOWS_DATA (frame);
2126 /* We cannot handle frame map and unmap hooks right in
2127 this routine, because these may throw. We queue
2128 magic events to run these hooks instead - kkm */
2130 if (wParam==SIZE_MINIMIZED)
2133 FRAME_VISIBLE_P (frame) = 0;
2134 mswindows_enqueue_magic_event (hwnd, XM_UNMAPFRAME);
2138 GetClientRect(hwnd, &rect);
2139 FRAME_PIXWIDTH(frame) = rect.right;
2140 FRAME_PIXHEIGHT(frame) = rect.bottom;
2142 pixel_to_real_char_size (frame, rect.right, rect.bottom,
2143 &FRAME_MSWINDOWS_CHARWIDTH (frame),
2144 &FRAME_MSWINDOWS_CHARHEIGHT (frame));
2146 pixel_to_char_size (frame, rect.right, rect.bottom, &columns, &rows);
2147 change_frame_size (frame, rows, columns, 1);
2149 /* If we are inside frame creation, we have to apply geometric
2151 if (FRAME_MSWINDOWS_TARGET_RECT (frame))
2153 /* Yes, we have to size again */
2154 mswindows_size_frame_internal ( frame,
2155 FRAME_MSWINDOWS_TARGET_RECT
2157 /* Reset so we do not get here again. The SetWindowPos call in
2158 * mswindows_size_frame_internal can cause recursion here. */
2159 if (FRAME_MSWINDOWS_TARGET_RECT (frame))
2161 xfree (FRAME_MSWINDOWS_TARGET_RECT (frame));
2162 FRAME_MSWINDOWS_TARGET_RECT (frame) = 0;
2167 if (!msframe->sizing && !FRAME_VISIBLE_P (frame))
2168 mswindows_enqueue_magic_event (hwnd, XM_MAPFRAME);
2169 FRAME_VISIBLE_P (frame) = 1;
2171 if (!msframe->sizing || mswindows_dynamic_frame_resize)
2178 case WM_DISPLAYCHANGE:
2182 fobj = mswindows_find_frame (hwnd);
2183 frame = XFRAME (fobj);
2184 d = XDEVICE (FRAME_DEVICE (frame));
2186 DEVICE_MSWINDOWS_HORZRES(d) = LOWORD (lParam);
2187 DEVICE_MSWINDOWS_VERTRES(d) = HIWORD (lParam);
2188 DEVICE_MSWINDOWS_BITSPIXEL(d) = wParam;
2192 /* Misc magic events which only require that the frame be identified */
2195 mswindows_enqueue_magic_event (hwnd, message);
2198 case WM_WINDOWPOSCHANGING:
2200 WINDOWPOS *wp = (LPWINDOWPOS) lParam;
2201 WINDOWPLACEMENT wpl = { sizeof(WINDOWPLACEMENT) };
2202 GetWindowPlacement(hwnd, &wpl);
2204 /* Only interested if size is changing and we're not being iconified */
2205 if (wpl.showCmd != SW_SHOWMINIMIZED
2206 && wpl.showCmd != SW_SHOWMAXIMIZED
2207 && !(wp->flags & SWP_NOSIZE))
2209 RECT ncsize = { 0, 0, 0, 0 };
2210 int pixwidth, pixheight;
2211 AdjustWindowRectEx (&ncsize, GetWindowLong (hwnd, GWL_STYLE),
2212 GetMenu(hwnd) != NULL,
2213 GetWindowLong (hwnd, GWL_EXSTYLE));
2215 round_size_to_real_char (XFRAME (mswindows_find_frame (hwnd)),
2216 wp->cx - (ncsize.right - ncsize.left),
2217 wp->cy - (ncsize.bottom - ncsize.top),
2218 &pixwidth, &pixheight);
2220 /* Convert client sizes to window sizes */
2221 pixwidth += (ncsize.right - ncsize.left);
2222 pixheight += (ncsize.bottom - ncsize.top);
2224 if (wpl.showCmd != SW_SHOWMAXIMIZED)
2226 /* Adjust so that the bottom or right doesn't move if it's
2227 * the top or left that's being changed */
2229 GetWindowRect (hwnd, &rect);
2231 if (rect.left != wp->x)
2232 wp->x += wp->cx - pixwidth;
2233 if (rect.top != wp->y)
2234 wp->y += wp->cy - pixheight;
2240 /* DefWindowProc sends useful WM_GETMINMAXINFO message, and adjusts
2241 window position if the user tries to track window too small */
2245 case WM_ENTERSIZEMOVE:
2246 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
2247 msframe->sizing = 1;
2250 case WM_EXITSIZEMOVE:
2251 msframe = FRAME_MSWINDOWS_DATA (XFRAME (mswindows_find_frame (hwnd)));
2252 msframe->sizing = 0;
2253 /* Queue noop event */
2254 mswindows_enqueue_magic_event (NULL, XM_BUMPQUEUE);
2257 #ifdef HAVE_SCROLLBARS
2261 /* Direction of scroll is determined by scrollbar instance. */
2262 int code = (int) LOWORD(wParam);
2263 int pos = (short int) HIWORD(wParam);
2264 HWND hwndScrollBar = (HWND) lParam;
2265 struct gcpro gcpro1, gcpro2;
2267 mswindows_handle_scrollbar_event (hwndScrollBar, code, pos);
2268 GCPRO2 (emacs_event, fobj);
2269 if (UNBOUNDP(mswindows_pump_outstanding_events())) /* Can GC */
2271 /* Error during event pumping - cancel scroll */
2272 SendMessage (hwndScrollBar, WM_CANCELMODE, 0, 0);
2280 int keys = LOWORD (wParam); /* Modifier key flags */
2281 int delta = (short) HIWORD (wParam); /* Wheel rotation amount */
2282 struct gcpro gcpro1, gcpro2;
2284 if (mswindows_handle_mousewheel_event (mswindows_find_frame (hwnd), keys, delta))
2286 GCPRO2 (emacs_event, fobj);
2287 mswindows_pump_outstanding_events (); /* Can GC */
2296 #ifdef HAVE_MENUBARS
2298 if (UNBOUNDP (mswindows_handle_wm_initmenu (
2300 XFRAME (mswindows_find_frame (hwnd)))))
2301 SendMessage (hwnd, WM_CANCELMODE, 0, 0);
2304 case WM_INITMENUPOPUP:
2305 if (!HIWORD(lParam))
2307 if (UNBOUNDP (mswindows_handle_wm_initmenupopup (
2309 XFRAME (mswindows_find_frame (hwnd)))))
2310 SendMessage (hwnd, WM_CANCELMODE, 0, 0);
2314 #endif /* HAVE_MENUBARS */
2318 WORD id = LOWORD (wParam);
2319 WORD nid = HIWORD (wParam);
2320 HWND cid = (HWND)lParam;
2321 frame = XFRAME (mswindows_find_frame (hwnd));
2323 #ifdef HAVE_TOOLBARS
2324 if (!NILP (mswindows_handle_toolbar_wm_command (frame, cid, id)))
2327 /* widgets in a buffer only eval a callback for suitable events.*/
2332 case CBN_EDITCHANGE:
2334 if (!NILP (mswindows_handle_gui_wm_command (frame, cid, id)))
2337 /* menubars always must come last since the hashtables do not
2339 #ifdef HAVE_MENUBARS
2340 if (!NILP (mswindows_handle_wm_command (frame, id)))
2344 return DefWindowProc (hwnd, message, wParam, lParam);
2345 /* Bite me - a spurious command. This used to not be able to
2346 happen but with the introduction of widgets its now
2351 case WM_CTLCOLORBTN:
2352 case WM_CTLCOLORLISTBOX:
2353 case WM_CTLCOLOREDIT:
2354 case WM_CTLCOLORSTATIC:
2355 case WM_CTLCOLORSCROLLBAR:
2357 /* if we get an opportunity to paint a widget then do so if
2358 there is an appropriate face */
2359 HWND crtlwnd = (HWND)lParam;
2360 LONG ii = GetWindowLong (crtlwnd, GWL_USERDATA);
2363 Lisp_Object image_instance;
2364 VOID_TO_LISP (image_instance, ii);
2365 if (IMAGE_INSTANCEP (image_instance)
2367 IMAGE_INSTANCE_TYPE_P (image_instance, IMAGE_WIDGET))
2369 /* set colors for the buttons */
2370 HDC hdc = (HDC)wParam;
2371 if (last_widget_brushed != ii)
2374 DeleteObject (widget_brush);
2375 widget_brush = CreateSolidBrush
2376 (COLOR_INSTANCE_MSWINDOWS_COLOR
2379 (XIMAGE_INSTANCE_WIDGET_FACE (image_instance),
2380 XIMAGE_INSTANCE_SUBWINDOW_FRAME (image_instance)))));
2382 last_widget_brushed = ii;
2385 COLOR_INSTANCE_MSWINDOWS_COLOR
2388 (XIMAGE_INSTANCE_WIDGET_FACE (image_instance),
2389 XIMAGE_INSTANCE_SUBWINDOW_FRAME (image_instance)))));
2390 SetBkMode (hdc, OPAQUE);
2393 COLOR_INSTANCE_MSWINDOWS_COLOR
2396 (XIMAGE_INSTANCE_WIDGET_FACE (image_instance),
2397 XIMAGE_INSTANCE_SUBWINDOW_FRAME (image_instance)))));
2398 return (LRESULT)widget_brush;
2404 #ifdef HAVE_DRAGNDROP
2405 case WM_DROPFILES: /* implementation ripped-off from event-Xt.c */
2407 UINT filecount, i, len;
2412 Lisp_Object l_dndlist = Qnil, l_item = Qnil;
2413 struct gcpro gcpro1, gcpro2, gcpro3;
2415 emacs_event = Fmake_event (Qnil, Qnil);
2416 event = XEVENT(emacs_event);
2418 GCPRO3 (emacs_event, l_dndlist, l_item);
2420 if (!DragQueryPoint ((HDROP) wParam, &point))
2421 point.x = point.y = -1; /* outside client area */
2423 event->event_type = misc_user_event;
2424 event->channel = mswindows_find_frame(hwnd);
2425 event->timestamp = GetMessageTime();
2426 event->event.misc.button = 1; /* #### Should try harder */
2427 event->event.misc.modifiers = mswindows_modifier_state (NULL, 0);
2428 event->event.misc.x = point.x;
2429 event->event.misc.y = point.y;
2430 event->event.misc.function = Qdragdrop_drop_dispatch;
2432 filecount = DragQueryFile ((HDROP) wParam, 0xffffffff, NULL, 0);
2433 for (i=0; i<filecount; i++)
2435 len = DragQueryFile ((HDROP) wParam, i, NULL, 0);
2436 /* The URLs that we make here aren't correct according to section
2437 * 3.10 of rfc1738 because they're missing the //<host>/ part and
2438 * because they may contain reserved characters. But that's OK -
2439 * they just need to be good enough to keep dragdrop.el happy. */
2440 fname = (char *)xmalloc (len+1);
2441 DragQueryFile ((HANDLE) wParam, i, fname, len+1);
2443 /* May be a shell link aka "shortcut" - replace fname if so */
2444 #if !(defined(__CYGWIN32__) || defined(__MINGW32__))
2445 /* cygwin doesn't define this COM stuff */
2446 if (!stricmp (fname + strlen (fname) - 4, ".LNK"))
2450 if (CoCreateInstance (&CLSID_ShellLink, NULL,
2451 CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl) == S_OK)
2455 if (psl->lpVtbl->QueryInterface (psl, &IID_IPersistFile,
2459 WIN32_FIND_DATA wfd;
2460 LPSTR resolved = (char *) xmalloc (MAX_PATH+1);
2462 MultiByteToWideChar (CP_ACP,0, fname, -1, wsz, MAX_PATH);
2464 if ((ppf->lpVtbl->Load (ppf, wsz, STGM_READ) == S_OK) &&
2465 (psl->lpVtbl->GetPath (psl, resolved, MAX_PATH,
2470 len = strlen (fname);
2473 ppf->lpVtbl->Release (ppf);
2476 psl->lpVtbl->Release (psl);
2482 filename = xmalloc (cygwin32_win32_to_posix_path_list_buf_size (fname) + 5);
2483 strcpy (filename, "file:");
2484 cygwin32_win32_to_posix_path_list (fname, filename+5);
2486 filename = (char *)xmalloc (len+6);
2487 strcat (strcpy (filename, "file:"), fname);
2488 dostounix_filename (filename+5);
2491 l_item = make_string (filename, strlen (filename));
2492 l_dndlist = Fcons (l_item, l_dndlist);
2495 DragFinish ((HDROP) wParam);
2497 event->event.misc.object = Fcons (Qdragdrop_URL, l_dndlist);
2498 mswindows_enqueue_dispatch_event (emacs_event);
2506 return DefWindowProc (hwnd, message, wParam, lParam);
2512 /************************************************************************/
2513 /* keyboard, mouse & other helpers for the windows procedure */
2514 /************************************************************************/
2516 mswindows_set_chord_timer (HWND hwnd)
2520 /* We get one third half system double click threshold */
2521 if (mswindows_mouse_button_tolerance <= 0)
2522 interval = GetDoubleClickTime () / 3;
2524 interval = mswindows_mouse_button_tolerance;
2526 SetTimer (hwnd, BUTTON_2_TIMER_ID, interval, 0);
2530 mswindows_button2_near_enough (POINTS p1, POINTS p2)
2533 if (mswindows_mouse_button_max_skew_x <= 0)
2534 dx = GetSystemMetrics (SM_CXDOUBLECLK) / 2;
2536 dx = mswindows_mouse_button_max_skew_x;
2538 if (mswindows_mouse_button_max_skew_y <= 0)
2539 dy = GetSystemMetrics (SM_CYDOUBLECLK) / 2;
2541 dy = mswindows_mouse_button_max_skew_y;
2543 return abs (p1.x - p2.x) < dx && abs (p1.y- p2.y)< dy;
2547 mswindows_current_layout_has_AltGr (void)
2549 /* This simple caching mechanism saves 10% of CPU
2550 time when a key typed at autorepeat rate of 30 cps! */
2551 static HKL last_hkl = 0;
2552 static int last_hkl_has_AltGr;
2554 HKL current_hkl = GetKeyboardLayout (0);
2555 if (current_hkl != last_hkl)
2558 last_hkl_has_AltGr = 0;
2559 /* In this loop, we query whether a character requires
2560 AltGr to be down to generate it. If at least such one
2561 found, this means that the layout does regard AltGr */
2562 for (c = ' '; c <= 0xFFU && c != 0 && !last_hkl_has_AltGr; ++c)
2563 if (HIBYTE (VkKeyScan (c)) == 6)
2564 last_hkl_has_AltGr = 1;
2565 last_hkl = current_hkl;
2567 return last_hkl_has_AltGr;
2571 /* Returns the state of the modifier keys in the format expected by the
2572 * Lisp_Event key_data, button_data and motion_data modifiers member */
2573 int mswindows_modifier_state (BYTE* keymap, int has_AltGr)
2579 keymap = (BYTE*) alloca(256);
2580 GetKeyboardState (keymap);
2581 has_AltGr = mswindows_current_layout_has_AltGr ();
2584 if (has_AltGr && (keymap [VK_LCONTROL] & 0x80) && (keymap [VK_RMENU] & 0x80))
2586 mods |= (keymap [VK_LMENU] & 0x80) ? XEMACS_MOD_META : 0;
2587 mods |= (keymap [VK_RCONTROL] & 0x80) ? XEMACS_MOD_CONTROL : 0;
2591 mods |= (keymap [VK_MENU] & 0x80) ? XEMACS_MOD_META : 0;
2592 mods |= (keymap [VK_CONTROL] & 0x80) ? XEMACS_MOD_CONTROL : 0;
2595 mods |= (keymap [VK_SHIFT] & 0x80) ? XEMACS_MOD_SHIFT : 0;
2601 * Translate a mswindows virtual key to a keysym.
2602 * Only returns non-Qnil for keys that don't generate WM_CHAR messages
2603 * or whose ASCII codes (like space) xemacs doesn't like.
2604 * Virtual key values are defined in winresrc.h
2606 Lisp_Object mswindows_key_to_emacs_keysym (int mswindows_key, int mods,
2609 if (extendedp) /* Keys not present on a 82 key keyboard */
2611 switch (mswindows_key)
2613 case VK_RETURN: return KEYSYM ("kp-enter");
2614 case VK_PRIOR: return KEYSYM ("prior");
2615 case VK_NEXT: return KEYSYM ("next");
2616 case VK_END: return KEYSYM ("end");
2617 case VK_HOME: return KEYSYM ("home");
2618 case VK_LEFT: return KEYSYM ("left");
2619 case VK_UP: return KEYSYM ("up");
2620 case VK_RIGHT: return KEYSYM ("right");
2621 case VK_DOWN: return KEYSYM ("down");
2622 case VK_INSERT: return KEYSYM ("insert");
2623 case VK_DELETE: return QKdelete;
2628 switch (mswindows_key)
2630 case VK_BACK: return QKbackspace;
2631 case VK_TAB: return QKtab;
2632 case '\n': return QKlinefeed;
2633 case VK_CLEAR: return KEYSYM ("clear");
2634 case VK_RETURN: return QKreturn;
2635 case VK_ESCAPE: return QKescape;
2636 case VK_SPACE: return QKspace;
2637 case VK_PRIOR: return KEYSYM ("kp-prior");
2638 case VK_NEXT: return KEYSYM ("kp-next");
2639 case VK_END: return KEYSYM ("kp-end");
2640 case VK_HOME: return KEYSYM ("kp-home");
2641 case VK_LEFT: return KEYSYM ("kp-left");
2642 case VK_UP: return KEYSYM ("kp-up");
2643 case VK_RIGHT: return KEYSYM ("kp-right");
2644 case VK_DOWN: return KEYSYM ("kp-down");
2645 case VK_SELECT: return KEYSYM ("select");
2646 case VK_PRINT: return KEYSYM ("print");
2647 case VK_EXECUTE: return KEYSYM ("execute");
2648 case VK_SNAPSHOT: return KEYSYM ("print");
2649 case VK_INSERT: return KEYSYM ("kp-insert");
2650 case VK_DELETE: return KEYSYM ("kp-delete");
2651 case VK_HELP: return KEYSYM ("help");
2652 #if 0 /* FSF Emacs allows these to return configurable syms/mods */
2653 case VK_LWIN return KEYSYM ("");
2654 case VK_RWIN return KEYSYM ("");
2656 case VK_APPS: return KEYSYM ("menu");
2657 case VK_NUMPAD0: return KEYSYM ("kp-0");
2658 case VK_NUMPAD1: return KEYSYM ("kp-1");
2659 case VK_NUMPAD2: return KEYSYM ("kp-2");
2660 case VK_NUMPAD3: return KEYSYM ("kp-3");
2661 case VK_NUMPAD4: return KEYSYM ("kp-4");
2662 case VK_NUMPAD5: return KEYSYM ("kp-5");
2663 case VK_NUMPAD6: return KEYSYM ("kp-6");
2664 case VK_NUMPAD7: return KEYSYM ("kp-7");
2665 case VK_NUMPAD8: return KEYSYM ("kp-8");
2666 case VK_NUMPAD9: return KEYSYM ("kp-9");
2667 case VK_MULTIPLY: return KEYSYM ("kp-multiply");
2668 case VK_ADD: return KEYSYM ("kp-add");
2669 case VK_SEPARATOR: return KEYSYM ("kp-separator");
2670 case VK_SUBTRACT: return KEYSYM ("kp-subtract");
2671 case VK_DECIMAL: return KEYSYM ("kp-decimal");
2672 case VK_DIVIDE: return KEYSYM ("kp-divide");
2673 case VK_F1: return KEYSYM ("f1");
2674 case VK_F2: return KEYSYM ("f2");
2675 case VK_F3: return KEYSYM ("f3");
2676 case VK_F4: return KEYSYM ("f4");
2677 case VK_F5: return KEYSYM ("f5");
2678 case VK_F6: return KEYSYM ("f6");
2679 case VK_F7: return KEYSYM ("f7");
2680 case VK_F8: return KEYSYM ("f8");
2681 case VK_F9: return KEYSYM ("f9");
2682 case VK_F10: return KEYSYM ("f10");
2683 case VK_F11: return KEYSYM ("f11");
2684 case VK_F12: return KEYSYM ("f12");
2685 case VK_F13: return KEYSYM ("f13");
2686 case VK_F14: return KEYSYM ("f14");
2687 case VK_F15: return KEYSYM ("f15");
2688 case VK_F16: return KEYSYM ("f16");
2689 case VK_F17: return KEYSYM ("f17");
2690 case VK_F18: return KEYSYM ("f18");
2691 case VK_F19: return KEYSYM ("f19");
2692 case VK_F20: return KEYSYM ("f20");
2693 case VK_F21: return KEYSYM ("f21");
2694 case VK_F22: return KEYSYM ("f22");
2695 case VK_F23: return KEYSYM ("f23");
2696 case VK_F24: return KEYSYM ("f24");
2703 * Find the console that matches the supplied mswindows window handle
2706 mswindows_find_console (HWND hwnd)
2708 /* We only support one console */
2709 return XCAR (Vconsole_list);
2713 * Find the frame that matches the supplied mswindows window handle
2716 mswindows_find_frame (HWND hwnd)
2718 LONG l = GetWindowLong (hwnd, XWL_FRAMEOBJ);
2722 /* We are in progress of frame creation. Return the frame
2723 being created, as it still not remembered in the window
2725 assert (!NILP (Vmswindows_frame_being_created));
2726 return Vmswindows_frame_being_created;
2728 VOID_TO_LISP (f, l);
2733 /************************************************************************/
2735 /************************************************************************/
2738 emacs_mswindows_add_timeout (EMACS_TIME thyme)
2741 EMACS_TIME current_time;
2742 EMACS_GET_TIME (current_time);
2743 EMACS_SUB_TIME (thyme, thyme, current_time);
2744 milliseconds = EMACS_SECS (thyme) * 1000 +
2745 (EMACS_USECS (thyme) + 500) / 1000;
2746 if (milliseconds < 1)
2748 ++mswindows_pending_timers_count;
2749 return SetTimer (NULL, 0, milliseconds,
2750 (TIMERPROC) mswindows_wm_timer_callback);
2754 emacs_mswindows_remove_timeout (int id)
2756 Lisp_Event match_against;
2757 Lisp_Object emacs_event;
2759 if (KillTimer (NULL, id))
2760 --mswindows_pending_timers_count;
2762 /* If there is a dispatch event generated by this
2763 timeout in the queue, we have to remove it too. */
2764 match_against.event_type = timeout_event;
2765 match_against.event.timeout.interval_id = id;
2766 emacs_event = mswindows_cancel_dispatch_event (&match_against);
2767 if (!NILP (emacs_event))
2768 Fdeallocate_event(emacs_event);
2771 /* If `user_p' is false, then return whether there are any win32, timeout,
2772 * or subprocess events pending (that is, whether
2773 * emacs_mswindows_next_event() would return immediately without blocking).
2775 * if `user_p' is true, then return whether there are any *user generated*
2776 * events available (that is, whether there are keyboard or mouse-click
2777 * events ready to be read). This also implies that
2778 * emacs_mswindows_next_event() would not block.
2781 emacs_mswindows_event_pending_p (int user_p)
2783 mswindows_need_event (0);
2784 return (!NILP (mswindows_u_dispatch_event_queue)
2785 || (!user_p && !NILP (mswindows_s_dispatch_event_queue)));
2789 * Return the next event
2792 emacs_mswindows_next_event (Lisp_Event *emacs_event)
2794 Lisp_Object event, event2;
2796 mswindows_need_event (1);
2798 event = mswindows_dequeue_dispatch_event ();
2799 XSETEVENT (event2, emacs_event);
2800 Fcopy_event (event, event2);
2801 Fdeallocate_event (event);
2805 * Handle a magic event off the dispatch queue.
2808 emacs_mswindows_handle_magic_event (Lisp_Event *emacs_event)
2810 switch (EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event))
2817 struct frame *f = XFRAME (EVENT_CHANNEL (emacs_event));
2818 mswindows_handle_paint (f);
2819 (FRAME_MSWINDOWS_DATA (f))->paint_pending = 0;
2826 Lisp_Object frame = EVENT_CHANNEL (emacs_event);
2827 struct frame *f = XFRAME (frame);
2828 int in_p = (EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event) == WM_SETFOCUS);
2831 /* struct gcpro gcpro1; */
2833 /* Clear sticky modifiers here (if we had any) */
2835 conser = Fcons (frame, Fcons (FRAME_DEVICE (f), in_p ? Qt : Qnil));
2836 /* GCPRO1 (conser); XXX Not necessary? */
2837 emacs_handle_focus_change_preliminary (conser);
2838 /* Under X the stuff up to here is done in the X event handler.
2840 emacs_handle_focus_change_final (conser);
2849 Lisp_Object frame = EVENT_CHANNEL (emacs_event);
2850 va_run_hook_with_args (EVENT_MSWINDOWS_MAGIC_TYPE(emacs_event)
2852 Qmap_frame_hook : Qunmap_frame_hook,
2857 /* #### What about Enter & Leave */
2859 va_run_hook_with_args (in_p ? Qmouse_enter_frame_hook :
2860 Qmouse_leave_frame_hook, 1, frame);
2868 #ifndef HAVE_MSG_SELECT
2870 get_process_input_waitable (Lisp_Process *process)
2872 Lisp_Object instr, outstr, p;
2873 XSETPROCESS (p, process);
2874 get_process_streams (process, &instr, &outstr);
2875 assert (!NILP (instr));
2876 #if defined (HAVE_SOCKETS) && !defined(HAVE_MSG_SELECT)
2877 return (network_connection_p (p)
2878 ? get_winsock_stream_waitable (XLSTREAM (instr))
2879 : get_ntpipe_input_stream_waitable (XLSTREAM (instr)));
2881 return get_ntpipe_input_stream_waitable (XLSTREAM (instr));
2886 emacs_mswindows_select_process (Lisp_Process *process)
2888 HANDLE hev = get_process_input_waitable (process);
2890 if (!add_waitable_handle (hev))
2891 error ("Too many active processes");
2893 #ifdef HAVE_WIN32_PROCESSES
2896 XSETPROCESS (p, process);
2897 if (!network_connection_p (p))
2899 HANDLE hprocess = get_nt_process_handle (process);
2900 if (!add_waitable_handle (hprocess))
2902 remove_waitable_handle (hev);
2903 error ("Too many active processes");
2911 emacs_mswindows_unselect_process (Lisp_Process *process)
2913 /* Process handle is removed in the event loop as soon
2914 as it is signaled, so don't bother here about it */
2915 HANDLE hev = get_process_input_waitable (process);
2916 remove_waitable_handle (hev);
2918 #endif /* HAVE_MSG_SELECT */
2921 emacs_mswindows_select_console (struct console *con)
2923 #ifdef HAVE_MSG_SELECT
2924 if (CONSOLE_MSWINDOWS_P (con))
2925 return; /* mswindows consoles are automatically selected */
2927 event_stream_unixoid_select_console (con);
2932 emacs_mswindows_unselect_console (struct console *con)
2934 #ifdef HAVE_MSG_SELECT
2935 if (CONSOLE_MSWINDOWS_P (con))
2936 return; /* mswindows consoles are automatically selected */
2938 event_stream_unixoid_unselect_console (con);
2943 emacs_mswindows_quit_p (void)
2945 /* Quit cannot happen in modal loop: all program
2946 input is dedicated to Windows. */
2947 if (mswindows_in_modal_loop)
2950 /* Drain windows queue. This sets up number of quit characters in
2952 mswindows_drain_windows_queue ();
2954 if (mswindows_quit_chars_count > 0)
2956 /* Yes there's a hidden one... Throw it away */
2957 Lisp_Event match_against;
2958 Lisp_Object emacs_event;
2961 match_against.event_type = key_press_event;
2962 match_against.event.key.modifiers = FAKE_MOD_QUIT;
2964 while (mswindows_quit_chars_count-- > 0)
2966 emacs_event = mswindows_cancel_dispatch_event (&match_against);
2967 assert (!NILP (emacs_event));
2969 if (XEVENT(emacs_event)->event.key.modifiers & XEMACS_MOD_SHIFT)
2972 Fdeallocate_event(emacs_event);
2975 Vquit_flag = critical_p ? Qcritical : Qt;
2980 emacs_mswindows_create_stream_pair (void* inhandle, void* outhandle,
2981 Lisp_Object* instream,
2982 Lisp_Object* outstream,
2985 /* Handles for streams */
2987 /* fds. These just stored along with the streams, and are closed in
2988 delete stream pair method, because we need to handle fake unices
2992 /* Decode inhandle and outhandle. Their meaning depends on
2993 the process implementation being used. */
2994 #if defined (HAVE_WIN32_PROCESSES)
2995 /* We're passed in Windows handles. That's what we like most... */
2996 hin = (HANDLE) inhandle;
2997 hout = (HANDLE) outhandle;
2999 #elif defined (HAVE_UNIX_PROCESSES)
3000 /* We are passed UNIX fds. This must be Cygwin.
3002 hin = inhandle >= 0 ? (HANDLE)get_osfhandle ((int)inhandle) : INVALID_HANDLE_VALUE;
3003 hout = outhandle >= 0 ? (HANDLE)get_osfhandle ((int)outhandle) : INVALID_HANDLE_VALUE;
3007 #error "So, WHICH kind of processes do you want?"
3010 *instream = (hin == INVALID_HANDLE_VALUE
3012 #if defined (HAVE_SOCKETS) && !defined (HAVE_MSG_SELECT)
3013 : flags & STREAM_NETWORK_CONNECTION
3014 ? make_winsock_input_stream ((SOCKET)hin, fdi)
3016 : make_ntpipe_input_stream (hin, fdi));
3018 #ifdef HAVE_WIN32_PROCESSES
3019 *outstream = (hout == INVALID_HANDLE_VALUE
3021 #if defined (HAVE_SOCKETS) && !defined (HAVE_MSG_SELECT)
3022 : flags & STREAM_NETWORK_CONNECTION
3023 ? make_winsock_output_stream ((SOCKET)hout, fdo)
3025 : make_ntpipe_output_stream (hout, fdo));
3026 #elif defined (HAVE_UNIX_PROCESSES)
3027 *outstream = (fdo >= 0
3028 ? make_filedesc_output_stream (fdo, 0, -1, LSTR_BLOCKED_OK)
3031 #if defined(HAVE_UNIX_PROCESSES) && defined(HAVE_PTYS)
3032 /* FLAGS is process->pty_flag for UNIX_PROCESSES */
3033 if ((flags & STREAM_PTY_FLUSHING) && fdo >= 0)
3035 Bufbyte eof_char = get_eof_char (fdo);
3036 int pty_max_bytes = get_pty_max_bytes (fdo);
3037 filedesc_stream_set_pty_flushing (XLSTREAM(*outstream), pty_max_bytes, eof_char);
3042 return (NILP (*instream)
3044 #if defined(HAVE_SOCKETS) && !defined(HAVE_MSG_SELECT)
3045 : flags & STREAM_NETWORK_CONNECTION
3046 ? HANDLE_TO_USID (get_winsock_stream_waitable (XLSTREAM (*instream)))
3048 : HANDLE_TO_USID (get_ntpipe_input_stream_waitable (XLSTREAM (*instream))));
3052 emacs_mswindows_delete_stream_pair (Lisp_Object instream,
3053 Lisp_Object outstream)
3055 /* Oh nothing special here for Win32 at all */
3056 #if defined (HAVE_UNIX_PROCESSES)
3057 int in = (NILP(instream)
3059 #if defined(HAVE_SOCKETS) && !defined(HAVE_MSG_SELECT)
3060 : LSTREAM_TYPE_P (XLSTREAM (instream), winsock)
3061 ? get_winsock_stream_param (XLSTREAM (instream))
3063 : get_ntpipe_input_stream_param (XLSTREAM (instream)));
3064 int out = (NILP(outstream) ? -1
3065 : filedesc_stream_fd (XLSTREAM (outstream)));
3069 if (out != in && out >= 0)
3073 return (NILP (instream)
3075 #if defined(HAVE_SOCKETS) && !defined(HAVE_MSG_SELECT)
3076 : LSTREAM_TYPE_P (XLSTREAM (instream), winsock)
3077 ? HANDLE_TO_USID (get_winsock_stream_waitable (XLSTREAM (instream)))
3079 : HANDLE_TO_USID (get_ntpipe_input_stream_waitable (XLSTREAM (instream))));
3082 #ifndef HAVE_X_WINDOWS
3083 /* This is called from GC when a process object is about to be freed.
3084 If we've still got pointers to it in this file, we're gonna lose hard.
3087 debug_process_finalization (Lisp_Process *p)
3090 Lisp_Object instr, outstr;
3092 get_process_streams (p, &instr, &outstr);
3093 /* if it still has fds, then it hasn't been killed yet. */
3094 assert (NILP(instr));
3095 assert (NILP(outstr));
3097 /* #### More checks here */
3102 /************************************************************************/
3103 /* initialization */
3104 /************************************************************************/
3107 reinit_vars_of_event_mswindows (void)
3109 mswindows_in_modal_loop = 0;
3110 mswindows_pending_timers_count = 0;
3112 mswindows_event_stream = xnew (struct event_stream);
3114 mswindows_event_stream->event_pending_p = emacs_mswindows_event_pending_p;
3115 mswindows_event_stream->force_event_pending = 0;
3116 mswindows_event_stream->next_event_cb = emacs_mswindows_next_event;
3117 mswindows_event_stream->handle_magic_event_cb = emacs_mswindows_handle_magic_event;
3118 mswindows_event_stream->add_timeout_cb = emacs_mswindows_add_timeout;
3119 mswindows_event_stream->remove_timeout_cb = emacs_mswindows_remove_timeout;
3120 mswindows_event_stream->quit_p_cb = emacs_mswindows_quit_p;
3121 mswindows_event_stream->select_console_cb = emacs_mswindows_select_console;
3122 mswindows_event_stream->unselect_console_cb = emacs_mswindows_unselect_console;
3123 #ifdef HAVE_MSG_SELECT
3124 mswindows_event_stream->select_process_cb =
3125 (void (*)(Lisp_Process*))event_stream_unixoid_select_process;
3126 mswindows_event_stream->unselect_process_cb =
3127 (void (*)(Lisp_Process*))event_stream_unixoid_unselect_process;
3128 mswindows_event_stream->create_stream_pair_cb = event_stream_unixoid_create_stream_pair;
3129 mswindows_event_stream->delete_stream_pair_cb = event_stream_unixoid_delete_stream_pair;
3131 mswindows_event_stream->select_process_cb = emacs_mswindows_select_process;
3132 mswindows_event_stream->unselect_process_cb = emacs_mswindows_unselect_process;
3133 mswindows_event_stream->create_stream_pair_cb = emacs_mswindows_create_stream_pair;
3134 mswindows_event_stream->delete_stream_pair_cb = emacs_mswindows_delete_stream_pair;
3139 vars_of_event_mswindows (void)
3141 reinit_vars_of_event_mswindows ();
3143 mswindows_u_dispatch_event_queue = Qnil;
3144 staticpro (&mswindows_u_dispatch_event_queue);
3145 mswindows_u_dispatch_event_queue_tail = Qnil;
3146 pdump_wire (&mswindows_u_dispatch_event_queue_tail);
3148 mswindows_s_dispatch_event_queue = Qnil;
3149 staticpro (&mswindows_s_dispatch_event_queue);
3150 mswindows_s_dispatch_event_queue_tail = Qnil;
3151 pdump_wire (&mswindows_s_dispatch_event_queue_tail);
3153 mswindows_error_caught_in_modal_loop = Qnil;
3154 staticpro (&mswindows_error_caught_in_modal_loop);
3156 DEFVAR_BOOL ("mswindows-alt-by-itself-activates-menu",
3157 &mswindows_alt_by_itself_activates_menu /*
3158 *Controls whether pressing and releasing the Alt key activates the menubar.
3159 This applies only if no intervening key was pressed. See also
3160 `menu-accelerator-enabled', which is probably the behavior you actually want.
3164 DEFVAR_BOOL ("mswindows-dynamic-frame-resize",
3165 &mswindows_dynamic_frame_resize /*
3166 *Controls redrawing frame contents during mouse-drag or keyboard resize
3167 operation. When non-nil, the frame is redrawn while being resized. When
3168 nil, frame is not redrawn, and exposed areas are filled with default
3169 MDI application background color. Note that this option only has effect
3170 if "Show window contents while dragging" is on in system Display/Plus!
3172 Default is t on fast machines, nil on slow.
3175 DEFVAR_INT ("mswindows-mouse-button-tolerance",
3176 &mswindows_mouse_button_tolerance /*
3177 *Analogue of double click interval for faking middle mouse events.
3178 The value is the minimum time in milliseconds that must elapse between
3179 left/right button down events before they are considered distinct events.
3180 If both mouse buttons are depressed within this interval, a middle mouse
3181 button down event is generated instead.
3182 If negative or zero, currently set system default is used instead.
3185 DEFVAR_INT ("mswindows-num-mouse-buttons", &mswindows_num_mouse_buttons /*
3186 Number of physical mouse buttons.
3189 DEFVAR_INT ("mswindows-mouse-button-max-skew-x",
3190 &mswindows_mouse_button_max_skew_x /*
3191 *Maximum horizontal distance in pixels between points in which left and
3192 right button clicks occurred for them to be translated into single
3193 middle button event. Clicks must occur in time not longer than defined
3194 by the variable `mswindows-mouse-button-tolerance'.
3195 If negative or zero, currently set system default is used instead.
3198 DEFVAR_INT ("mswindows-mouse-button-max-skew-y",
3199 &mswindows_mouse_button_max_skew_y /*
3200 *Maximum vertical distance in pixels between points in which left and
3201 right button clicks occurred for them to be translated into single
3202 middle button event. Clicks must occur in time not longer than defined
3203 by the variable `mswindows-mouse-button-tolerance'.
3204 If negative or zero, currently set system default is used instead.
3207 mswindows_mouse_button_max_skew_x = 0;
3208 mswindows_mouse_button_max_skew_y = 0;
3209 mswindows_mouse_button_tolerance = 0;
3210 mswindows_alt_by_itself_activates_menu = 1;
3214 syms_of_event_mswindows (void)
3219 lstream_type_create_mswindows_selectable (void)
3221 init_slurp_stream ();
3222 init_shove_stream ();
3223 #if defined (HAVE_SOCKETS) && !defined(HAVE_MSG_SELECT)
3224 init_winsock_stream ();
3229 init_event_mswindows_late (void)
3231 #ifdef HAVE_MSG_SELECT
3232 windows_fd = open("/dev/windows", O_RDONLY | O_NONBLOCK, 0);
3233 assert (windows_fd>=0);
3234 FD_SET (windows_fd, &input_wait_mask);
3235 FD_ZERO(&zero_mask);
3238 event_stream = mswindows_event_stream;
3240 mswindows_dynamic_frame_resize = !GetSystemMetrics (SM_SLOWMACHINE);
3241 mswindows_num_mouse_buttons = GetSystemMetrics (SM_CMOUSEBUTTONS);