; * doc/lispref/hash.texi (Defining Hash): Fix typos.
[emacs.git] / src / w32inevt.c
blob907cc476a917a40cf04437b081a85f30d7f61490
1 /* Input event support for Emacs on the Microsoft Windows API.
2 Copyright (C) 1992-1993, 1995, 2001-2018 Free Software Foundation,
3 Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21 Drew Bliss 01-Oct-93
22 Adapted from ntkbd.c by Tim Fleehart
26 #include <config.h>
27 #include <stdio.h>
28 #include <windows.h>
30 #ifndef MOUSE_MOVED
31 #define MOUSE_MOVED 1
32 #endif
33 #ifndef MOUSE_HWHEELED
34 #define MOUSE_HWHEELED 8
35 #endif
37 #include "lisp.h"
38 #include "keyboard.h"
39 #include "frame.h"
40 #include "blockinput.h"
41 #include "termchar.h" /* for Mouse_HLInfo, tty_display_info */
42 #include "w32term.h"
43 #include "w32inevt.h"
44 #include "w32common.h"
46 /* stdin, from w32console.c */
47 extern HANDLE keyboard_handle;
49 /* Info for last mouse motion */
50 static COORD movement_pos;
51 static Time movement_time;
53 /* from w32fns.c */
54 extern unsigned int map_keypad_keys (unsigned int, unsigned int);
55 extern unsigned int w32_key_to_modifier (int key);
57 /* Event queue */
58 #define EVENT_QUEUE_SIZE 50
59 static INPUT_RECORD event_queue[EVENT_QUEUE_SIZE];
60 static INPUT_RECORD *queue_ptr = event_queue, *queue_end = event_queue;
62 /* Temporarily store lead byte of DBCS input sequences. */
63 static char dbcs_lead = 0;
65 static inline BOOL
66 w32_read_console_input (HANDLE h, INPUT_RECORD *rec, DWORD recsize,
67 DWORD *waiting)
69 return (w32_console_unicode_input
70 ? ReadConsoleInputW (h, rec, recsize, waiting)
71 : ReadConsoleInputA (h, rec, recsize, waiting));
74 /* Set by w32_console_toggle_lock_key. */
75 int faked_key;
77 static int
78 fill_queue (BOOL block)
80 BOOL rc;
81 DWORD events_waiting;
83 if (queue_ptr < queue_end)
84 return queue_end-queue_ptr;
86 if (!block)
88 /* Check to see if there are some events to read before we try
89 because we can't block. */
90 if (!GetNumberOfConsoleInputEvents (keyboard_handle, &events_waiting))
91 return -1;
92 if (events_waiting == 0)
93 return 0;
96 rc = w32_read_console_input (keyboard_handle, event_queue, EVENT_QUEUE_SIZE,
97 &events_waiting);
98 if (!rc)
99 return -1;
100 queue_ptr = event_queue;
101 queue_end = event_queue + events_waiting;
102 return (int) events_waiting;
105 /* In a generic, multi-frame world this should take a console handle
106 and return the frame for it.
108 Right now, there's only one frame so return it. */
109 static struct frame *
110 get_frame (void)
112 return SELECTED_FRAME ();
115 /* Translate console modifiers to emacs modifiers.
116 German keyboard support (Kai Morgan Zeise 2/18/95). */
119 #if 0
120 /* Return nonzero if the virtual key is a dead key. */
121 static int
122 is_dead_key (int wparam)
124 unsigned int code = MapVirtualKey (wparam, 2);
126 /* Windows 95 returns 0x8000, NT returns 0x80000000. */
127 return (code & 0x80008000) ? 1 : 0;
129 #endif
131 /* The return code indicates key code size. cpID is the codepage to
132 use for translation to Unicode; -1 means use the current console
133 input codepage. */
136 /* return code -1 means that event_queue_ptr won't be incremented.
137 In other word, this event makes two key codes. (by himi) */
138 static int
139 key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
141 static int mod_key_state = 0;
142 int wParam;
144 *isdead = 0;
146 /* Skip key-up events. */
147 if (!event->bKeyDown)
149 switch (event->wVirtualKeyCode)
151 case VK_LWIN:
152 if (!w32_kbdhook_active)
153 mod_key_state &= ~LEFT_WIN_PRESSED;
154 break;
155 case VK_RWIN:
156 if (!w32_kbdhook_active)
157 mod_key_state &= ~RIGHT_WIN_PRESSED;
158 break;
159 case VK_APPS:
160 mod_key_state &= ~APPS_PRESSED;
161 break;
163 return 0;
166 /* Ignore keystrokes we fake ourself; see below. */
167 if (faked_key == event->wVirtualKeyCode)
169 faked_key = 0;
170 return 0;
173 /* To make it easier to debug this code, ignore modifier keys! */
174 switch (event->wVirtualKeyCode)
176 case VK_LWIN:
177 if (NILP (Vw32_pass_lwindow_to_system))
179 /* Prevent system from acting on keyup (which opens the Start
180 menu if no other key was pressed) by simulating a press of
181 Space which we will ignore. */
182 if ((mod_key_state & LEFT_WIN_PRESSED) == 0)
184 if (NUMBERP (Vw32_phantom_key_code))
185 faked_key = XUINT (Vw32_phantom_key_code) & 255;
186 else
187 faked_key = VK_SPACE;
188 keybd_event (faked_key, (BYTE) MapVirtualKey (faked_key, 0), 0, 0);
191 if (!w32_kbdhook_active)
192 mod_key_state |= LEFT_WIN_PRESSED;
193 if (!NILP (Vw32_lwindow_modifier))
194 return 0;
195 break;
196 case VK_RWIN:
197 if (NILP (Vw32_pass_rwindow_to_system))
199 if ((mod_key_state & RIGHT_WIN_PRESSED) == 0)
201 if (NUMBERP (Vw32_phantom_key_code))
202 faked_key = XUINT (Vw32_phantom_key_code) & 255;
203 else
204 faked_key = VK_SPACE;
205 keybd_event (faked_key, (BYTE) MapVirtualKey (faked_key, 0), 0, 0);
208 if (!w32_kbdhook_active)
209 mod_key_state |= RIGHT_WIN_PRESSED;
210 if (!NILP (Vw32_rwindow_modifier))
211 return 0;
212 break;
213 case VK_APPS:
214 mod_key_state |= APPS_PRESSED;
215 if (!NILP (Vw32_apps_modifier))
216 return 0;
217 break;
218 case VK_CAPITAL:
219 /* Decide whether to treat as modifier or function key. */
220 if (NILP (Vw32_enable_caps_lock))
221 goto disable_lock_key;
222 return 0;
223 case VK_NUMLOCK:
224 /* Decide whether to treat as modifier or function key. */
225 if (NILP (Vw32_enable_num_lock))
226 goto disable_lock_key;
227 return 0;
228 case VK_SCROLL:
229 /* Decide whether to treat as modifier or function key. */
230 if (NILP (Vw32_scroll_lock_modifier))
231 goto disable_lock_key;
232 return 0;
233 disable_lock_key:
234 /* Ensure the appropriate lock key state is off (and the
235 indicator light as well). */
236 wParam = event->wVirtualKeyCode;
237 if (GetAsyncKeyState (wParam) & 0x8000)
239 /* Fake another press of the relevant key. Apparently, this
240 really is the only way to turn off the indicator. */
241 faked_key = wParam;
242 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
243 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
244 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
245 KEYEVENTF_EXTENDEDKEY | 0, 0);
246 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
247 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
249 break;
250 case VK_MENU:
251 case VK_CONTROL:
252 case VK_SHIFT:
253 return 0;
254 case VK_CANCEL:
255 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
256 which is confusing for purposes of key binding; convert
257 VK_CANCEL events into VK_PAUSE events. */
258 event->wVirtualKeyCode = VK_PAUSE;
259 break;
260 case VK_PAUSE:
261 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
262 for purposes of key binding; convert these back into
263 VK_NUMLOCK events, at least when we want to see NumLock key
264 presses. (Note that there is never any possibility that
265 VK_PAUSE with Ctrl really is C-Pause as per above.) */
266 if (NILP (Vw32_enable_num_lock)
267 && (event->dwControlKeyState
268 & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) != 0)
269 event->wVirtualKeyCode = VK_NUMLOCK;
270 break;
273 /* Recognize state of Windows and Apps keys. */
274 event->dwControlKeyState |= mod_key_state;
275 if (w32_kbdhook_active)
277 if (check_w32_winkey_state (VK_LWIN))
278 event->dwControlKeyState |= LEFT_WIN_PRESSED;
279 if (check_w32_winkey_state (VK_RWIN))
280 event->dwControlKeyState |= RIGHT_WIN_PRESSED;
283 /* Distinguish numeric keypad keys from extended keys. */
284 event->wVirtualKeyCode =
285 map_keypad_keys (event->wVirtualKeyCode,
286 (event->dwControlKeyState & ENHANCED_KEY));
288 if (lispy_function_keys[event->wVirtualKeyCode] == 0)
290 if (!NILP (Vw32_recognize_altgr)
291 && (event->dwControlKeyState & LEFT_CTRL_PRESSED)
292 && (event->dwControlKeyState & RIGHT_ALT_PRESSED))
294 /* Don't try to interpret AltGr key chords; ToAscii seems not
295 to process them correctly. */
297 /* Handle key chords including any modifiers other than shift
298 directly, in order to preserve as much modifier information as
299 possible. */
300 else if (event->dwControlKeyState
301 & ( RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED
302 | RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED
303 | (!NILP (Vw32_lwindow_modifier) ? LEFT_WIN_PRESSED : 0)
304 | (!NILP (Vw32_rwindow_modifier) ? RIGHT_WIN_PRESSED : 0)
305 | (!NILP (Vw32_apps_modifier) ? APPS_PRESSED : 0)
306 | (!NILP (Vw32_scroll_lock_modifier) ? SCROLLLOCK_ON : 0)))
308 /* Don't translate modified alphabetic keystrokes, so the user
309 doesn't need to constantly switch layout to type control or
310 meta keystrokes when the normal layout translates
311 alphabetic characters to non-ascii characters. */
312 if ('A' <= event->wVirtualKeyCode && event->wVirtualKeyCode <= 'Z')
314 event->uChar.AsciiChar = event->wVirtualKeyCode;
315 if ((event->dwControlKeyState & SHIFT_PRESSED) == 0)
316 event->uChar.AsciiChar += ('a' - 'A');
318 /* Try to handle unrecognized keystrokes by determining the
319 base character (ie. translating the base key plus shift
320 modifier). */
321 else if (event->uChar.AsciiChar == 0)
322 w32_kbd_patch_key (event, -1);
325 if (event->uChar.AsciiChar == 0)
327 emacs_ev->kind = NO_EVENT;
328 return 0;
330 else if (event->uChar.AsciiChar > 0)
332 /* Pure ASCII characters < 128. */
333 emacs_ev->kind = ASCII_KEYSTROKE_EVENT;
334 emacs_ev->code = event->uChar.AsciiChar;
336 else if (event->uChar.UnicodeChar > 0
337 && w32_console_unicode_input)
339 /* Unicode codepoint; only valid if we are using Unicode
340 console input mode. */
341 emacs_ev->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
342 emacs_ev->code = event->uChar.UnicodeChar;
344 else
346 /* Fallback handling of non-ASCII characters for non-Unicode
347 versions of Windows, and for non-Unicode input on NT
348 family of Windows. Only characters in the current
349 console codepage are supported by this fallback. */
350 wchar_t code;
351 char dbcs[2];
352 int cpId;
354 /* Get the current console input codepage to interpret this
355 key with. Note that the system defaults for the OEM
356 codepage could have been changed by calling SetConsoleCP
357 or w32-set-console-codepage, so using GetLocaleInfo to
358 get LOCALE_IDEFAULTCODEPAGE is not TRT here. */
359 cpId = GetConsoleCP ();
361 dbcs[0] = dbcs_lead;
362 dbcs[1] = event->uChar.AsciiChar;
363 if (dbcs_lead)
365 dbcs_lead = 0;
366 if (!MultiByteToWideChar (cpId, 0, dbcs, 2, &code, 1))
368 /* Garbage */
369 DebPrint (("Invalid DBCS sequence: %d %d\n",
370 dbcs[0], dbcs[1]));
371 emacs_ev->kind = NO_EVENT;
374 else if (IsDBCSLeadByteEx (cpId, dbcs[1]))
376 dbcs_lead = dbcs[1];
377 emacs_ev->kind = NO_EVENT;
379 else
381 if (!MultiByteToWideChar (cpId, 0, &dbcs[1], 1, &code, 1))
383 /* Garbage */
384 DebPrint (("Invalid character: %d\n", dbcs[1]));
385 emacs_ev->kind = NO_EVENT;
388 emacs_ev->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
389 emacs_ev->code = code;
392 else
394 /* Function keys and other non-character keys. */
395 emacs_ev->kind = NON_ASCII_KEYSTROKE_EVENT;
396 emacs_ev->code = event->wVirtualKeyCode;
399 XSETFRAME (emacs_ev->frame_or_window, get_frame ());
400 emacs_ev->modifiers = w32_kbd_mods_to_emacs (event->dwControlKeyState,
401 event->wVirtualKeyCode);
402 emacs_ev->timestamp = GetTickCount ();
403 return 1;
406 /* Mouse position hook. */
407 void
408 w32_console_mouse_position (struct frame **f,
409 int insist,
410 Lisp_Object *bar_window,
411 enum scroll_bar_part *part,
412 Lisp_Object *x,
413 Lisp_Object *y,
414 Time *time)
416 block_input ();
418 insist = insist;
420 *f = get_frame ();
421 *bar_window = Qnil;
422 *part = scroll_bar_above_handle;
423 SELECTED_FRAME ()->mouse_moved = 0;
425 XSETINT (*x, movement_pos.X);
426 XSETINT (*y, movement_pos.Y);
427 *time = movement_time;
429 unblock_input ();
432 /* Remember mouse motion and notify emacs. */
433 static void
434 mouse_moved_to (int x, int y)
436 /* If we're in the same place, ignore it. */
437 if (x != movement_pos.X || y != movement_pos.Y)
439 SELECTED_FRAME ()->mouse_moved = 1;
440 movement_pos.X = x;
441 movement_pos.Y = y;
442 movement_time = GetTickCount ();
446 /* Consoles return button bits in a strange order:
447 least significant - Leftmost button
448 next - Rightmost button
449 next - Leftmost+1
450 next - Leftmost+2...
452 For the 3 standard buttons, we have:
453 Left == 0
454 Middle == 1
455 Right == 2
456 Others increase from there. */
458 #define NUM_TRANSLATED_MOUSE_BUTTONS 5
459 static int emacs_button_translation[NUM_TRANSLATED_MOUSE_BUTTONS] =
461 0, 2, 1, 3, 4
464 static int
465 do_mouse_event (MOUSE_EVENT_RECORD *event,
466 struct input_event *emacs_ev)
468 static DWORD button_state = 0;
469 static Lisp_Object last_mouse_window;
470 DWORD but_change, mask, flags = event->dwEventFlags;
471 int i;
473 switch (flags)
475 case MOUSE_MOVED:
477 struct frame *f = get_frame ();
478 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
479 int mx = event->dwMousePosition.X, my = event->dwMousePosition.Y;
481 mouse_moved_to (mx, my);
483 if (f->mouse_moved)
485 if (hlinfo->mouse_face_hidden)
487 hlinfo->mouse_face_hidden = 0;
488 clear_mouse_face (hlinfo);
491 /* Generate SELECT_WINDOW_EVENTs when needed. */
492 if (!NILP (Vmouse_autoselect_window))
494 Lisp_Object mouse_window = window_from_coordinates (f, mx, my,
495 0, 0);
496 /* A window will be selected only when it is not
497 selected now, and the last mouse movement event was
498 not in it. A minibuffer window will be selected iff
499 it is active. */
500 if (WINDOWP (mouse_window)
501 && !EQ (mouse_window, last_mouse_window)
502 && !EQ (mouse_window, selected_window))
504 struct input_event event;
506 EVENT_INIT (event);
507 event.kind = SELECT_WINDOW_EVENT;
508 event.frame_or_window = mouse_window;
509 event.arg = Qnil;
510 event.timestamp = movement_time;
511 kbd_buffer_store_event (&event);
513 last_mouse_window = mouse_window;
515 else
516 last_mouse_window = Qnil;
518 previous_help_echo_string = help_echo_string;
519 help_echo_string = help_echo_object = help_echo_window = Qnil;
520 help_echo_pos = -1;
521 note_mouse_highlight (f, mx, my);
522 /* If the contents of the global variable help_echo has
523 changed (inside note_mouse_highlight), generate a HELP_EVENT. */
524 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
525 gen_help_event (help_echo_string, selected_frame,
526 help_echo_window, help_echo_object,
527 help_echo_pos);
529 /* We already called kbd_buffer_store_event, so indicate to
530 the caller it shouldn't. */
531 return 0;
533 case MOUSE_WHEELED:
534 case MOUSE_HWHEELED:
536 struct frame *f = get_frame ();
537 int mx = event->dwMousePosition.X, my = event->dwMousePosition.Y;
538 bool down_p = (event->dwButtonState & 0x10000000) != 0;
540 emacs_ev->kind =
541 flags == MOUSE_HWHEELED ? HORIZ_WHEEL_EVENT : WHEEL_EVENT;
542 emacs_ev->code = 0;
543 emacs_ev->modifiers = down_p ? down_modifier : up_modifier;
544 emacs_ev->modifiers |=
545 w32_kbd_mods_to_emacs (event->dwControlKeyState, 0);
546 XSETINT (emacs_ev->x, mx);
547 XSETINT (emacs_ev->y, my);
548 XSETFRAME (emacs_ev->frame_or_window, f);
549 emacs_ev->arg = Qnil;
550 emacs_ev->timestamp = GetTickCount ();
551 return 1;
553 case DOUBLE_CLICK:
554 default: /* mouse pressed or released */
555 /* It looks like the console code sends us a button-release
556 mouse event with dwButtonState == 0 when a window is
557 activated and when the mouse is first clicked. Ignore this
558 case. */
559 if (event->dwButtonState == button_state)
560 return 0;
562 emacs_ev->kind = MOUSE_CLICK_EVENT;
564 /* Find out what button has changed state since the last button
565 event. */
566 but_change = button_state ^ event->dwButtonState;
567 mask = 1;
568 for (i = 0; mask; i++, mask <<= 1)
569 if (but_change & mask)
571 if (i < NUM_TRANSLATED_MOUSE_BUTTONS)
572 emacs_ev->code = emacs_button_translation[i];
573 else
574 emacs_ev->code = i;
575 break;
578 button_state = event->dwButtonState;
579 emacs_ev->modifiers =
580 w32_kbd_mods_to_emacs (event->dwControlKeyState, 0)
581 | ((event->dwButtonState & mask) ? down_modifier : up_modifier);
583 XSETFASTINT (emacs_ev->x, event->dwMousePosition.X);
584 XSETFASTINT (emacs_ev->y, event->dwMousePosition.Y);
585 XSETFRAME (emacs_ev->frame_or_window, get_frame ());
586 emacs_ev->arg = Qnil;
587 emacs_ev->timestamp = GetTickCount ();
589 return 1;
593 static void
594 resize_event (WINDOW_BUFFER_SIZE_RECORD *event)
596 struct frame *f = get_frame ();
598 change_frame_size (f, event->dwSize.X, event->dwSize.Y
599 - FRAME_MENU_BAR_LINES (f), 0, 1, 0, 0);
600 SET_FRAME_GARBAGED (f);
603 static void
604 maybe_generate_resize_event (void)
606 CONSOLE_SCREEN_BUFFER_INFO info;
607 struct frame *f = get_frame ();
609 GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &info);
611 /* It is okay to call this unconditionally, since it will do nothing
612 if the size hasn't actually changed. */
613 change_frame_size (f,
614 1 + info.srWindow.Right - info.srWindow.Left,
615 1 + info.srWindow.Bottom - info.srWindow.Top
616 - FRAME_MENU_BAR_LINES (f), 0, 1, 0, 0);
619 #if HAVE_W32NOTIFY
621 handle_file_notifications (struct input_event *hold_quit)
623 struct notifications_set *ns = NULL;
624 int nevents = 0;
625 int done = 0;
627 /* We cannot process notification before Emacs is fully initialized,
628 since we need the UTF-16LE coding-system to be set up. */
629 if (!initialized)
631 return nevents;
634 while (!done)
636 ns = NULL;
638 /* Find out if there is a record available in the linked list of
639 notifications sets. If so, unlink te set from the linked list.
640 Use the critical section. */
641 enter_crit ();
642 if (notifications_set_head->next != notifications_set_head)
644 ns = notifications_set_head->next;
645 ns->prev->next = ns->next;
646 ns->next->prev = ns->prev;
648 else
649 done = 1;
650 leave_crit();
652 if (ns)
654 BYTE *p = ns->notifications;
655 FILE_NOTIFY_INFORMATION *fni = (PFILE_NOTIFY_INFORMATION)p;
656 const DWORD min_size
657 = offsetof (FILE_NOTIFY_INFORMATION, FileName) + sizeof(wchar_t);
658 struct input_event inev;
659 DWORD info_size = ns->size;
660 Lisp_Object cs = Qutf_16le;
661 Lisp_Object obj = w32_get_watch_object (ns->desc);
663 /* notifications size could be zero when the buffer of
664 notifications overflowed on the OS level, or when the
665 directory being watched was itself deleted. Do nothing in
666 that case. */
667 if (info_size
668 && !NILP (obj) && CONSP (obj))
670 Lisp_Object callback = XCDR (obj);
672 EVENT_INIT (inev);
674 while (info_size >= min_size)
676 Lisp_Object utf_16_fn
677 = make_unibyte_string ((char *)fni->FileName,
678 fni->FileNameLength);
679 /* Note: mule-conf is preloaded, so utf-16le must
680 already be defined at this point. */
681 Lisp_Object fname
682 = code_convert_string_norecord (utf_16_fn, cs, 0);
683 Lisp_Object action = lispy_file_action (fni->Action);
685 inev.kind = FILE_NOTIFY_EVENT;
686 inev.timestamp = GetTickCount ();
687 inev.modifiers = 0;
688 inev.frame_or_window = callback;
689 inev.arg = Fcons (action, fname);
690 inev.arg = list3 (make_pointer_integer (ns->desc),
691 action, fname);
692 kbd_buffer_store_event_hold (&inev, hold_quit);
693 nevents++;
694 if (!fni->NextEntryOffset)
695 break;
696 p += fni->NextEntryOffset;
697 fni = (PFILE_NOTIFY_INFORMATION)p;
698 info_size -= fni->NextEntryOffset;
701 /* Free this notification set. */
702 free (ns->notifications);
703 free (ns);
706 return nevents;
708 #else /* !HAVE_W32NOTIFY */
710 handle_file_notifications (struct input_event *hold_quit)
712 return 0;
714 #endif /* !HAVE_W32NOTIFY */
716 /* Here's an overview of how Emacs input works in non-GUI sessions on
717 MS-Windows. (For description of the GUI input, see the commentary
718 before w32_msg_pump in w32fns.c.)
720 When Emacs is idle, it loops inside wait_reading_process_output,
721 calling pselect periodically to check whether any input is
722 available. On Windows, pselect is redirected to sys_select, which
723 uses MsgWaitForMultipleObjects to wait for input, either from the
724 keyboard or from any of the Emacs subprocesses. In addition,
725 MsgWaitForMultipleObjects wakes up when some Windows message is
726 posted to the input queue of the Emacs's main thread (which is the
727 thread in which sys_select runs).
729 When the Emacs's console window has focus, Windows sends input
730 events that originate from the keyboard or the mouse; these events
731 wake up MsgWaitForMultipleObjects, which reports that input is
732 available. Emacs then calls w32_console_read_socket, below, to
733 read the input. w32_console_read_socket uses
734 GetNumberOfConsoleInputEvents and ReadConsoleInput to peek at and
735 read the console input events.
737 One type of non-keyboard input event that gets reported as input
738 available is due to the Emacs's console window receiving focus.
739 When that happens, Emacs gets the FOCUS_EVENT event and sys_select
740 reports some input; however, w32_console_read_socket ignores such
741 events when called to read them.
743 Note that any other Windows message sent to the main thread will
744 also wake up MsgWaitForMultipleObjects. These messages get
745 immediately dispatched to their destinations by calling
746 drain_message_queue. */
749 w32_console_read_socket (struct terminal *terminal,
750 struct input_event *hold_quit)
752 int nev, add;
753 int isdead;
755 block_input ();
757 for (;;)
759 int nfnotify = handle_file_notifications (hold_quit);
761 nev = fill_queue (0);
762 if (nev <= 0)
764 /* If nev == -1, there was some kind of error
765 If nev == 0 then no events were available
766 so return. */
767 if (nfnotify)
768 nev = 0;
769 break;
772 while (nev > 0)
774 struct input_event inev;
775 /* Having a separate variable with this value makes
776 debugging easier, as otherwise the compiler might
777 rearrange the switch below in a way that makes it hard to
778 track the event type. */
779 unsigned evtype = queue_ptr->EventType;
781 EVENT_INIT (inev);
782 inev.kind = NO_EVENT;
783 inev.arg = Qnil;
785 switch (evtype)
787 case KEY_EVENT:
788 add = key_event (&queue_ptr->Event.KeyEvent, &inev, &isdead);
789 if (add == -1) /* 95.7.25 by himi */
791 queue_ptr--;
792 add = 1;
794 if (add)
795 kbd_buffer_store_event_hold (&inev, hold_quit);
796 break;
798 case MOUSE_EVENT:
799 add = do_mouse_event (&queue_ptr->Event.MouseEvent, &inev);
800 if (add)
801 kbd_buffer_store_event_hold (&inev, hold_quit);
802 break;
804 case WINDOW_BUFFER_SIZE_EVENT:
805 if (w32_use_full_screen_buffer)
806 resize_event (&queue_ptr->Event.WindowBufferSizeEvent);
807 break;
809 case MENU_EVENT:
810 case FOCUS_EVENT:
811 /* Internal event types, ignored. */
812 break;
815 queue_ptr++;
816 nev--;
820 /* We don't get told about changes in the window size (only the buffer
821 size, which we no longer care about), so we have to check it
822 periodically. */
823 if (!w32_use_full_screen_buffer)
824 maybe_generate_resize_event ();
826 unblock_input ();
827 return nev;