1 /* Input event support for Emacs on the Microsoft Windows API.
2 Copyright (C) 1992-1993, 1995, 2001-2013 Free Software Foundation,
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
10 (at 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 <http://www.gnu.org/licenses/>. */
22 Adapted from ntkbd.c by Tim Fleehart
33 #ifndef MOUSE_HWHEELED
34 #define MOUSE_HWHEELED 8
40 #include "dispextern.h"
42 #include "blockinput.h"
43 #include "termhooks.h"
49 /* stdin, from w32console.c */
50 extern HANDLE keyboard_handle
;
52 /* Info for last mouse motion */
53 static COORD movement_pos
;
54 static Time movement_time
;
57 extern unsigned int map_keypad_keys (unsigned int, unsigned int);
58 extern unsigned int w32_key_to_modifier (int key
);
61 #define EVENT_QUEUE_SIZE 50
62 static INPUT_RECORD event_queue
[EVENT_QUEUE_SIZE
];
63 static INPUT_RECORD
*queue_ptr
= event_queue
, *queue_end
= event_queue
;
65 /* Temporarily store lead byte of DBCS input sequences. */
66 static char dbcs_lead
= 0;
69 w32_read_console_input (HANDLE h
, INPUT_RECORD
*rec
, DWORD recsize
,
72 return (w32_console_unicode_input
73 ? ReadConsoleInputW (h
, rec
, recsize
, waiting
)
74 : ReadConsoleInputA (h
, rec
, recsize
, waiting
));
77 /* Set by w32_console_toggle_lock_key. */
81 fill_queue (BOOL block
)
86 if (queue_ptr
< queue_end
)
87 return queue_end
-queue_ptr
;
91 /* Check to see if there are some events to read before we try
92 because we can't block. */
93 if (!GetNumberOfConsoleInputEvents (keyboard_handle
, &events_waiting
))
95 if (events_waiting
== 0)
99 rc
= w32_read_console_input (keyboard_handle
, event_queue
, EVENT_QUEUE_SIZE
,
103 queue_ptr
= event_queue
;
104 queue_end
= event_queue
+ events_waiting
;
105 return (int) events_waiting
;
108 /* In a generic, multi-frame world this should take a console handle
109 and return the frame for it.
111 Right now, there's only one frame so return it. */
112 static struct frame
*
115 return SELECTED_FRAME ();
118 /* Translate console modifiers to emacs modifiers.
119 German keyboard support (Kai Morgan Zeise 2/18/95). */
123 /* Return nonzero if the virtual key is a dead key. */
125 is_dead_key (int wparam
)
127 unsigned int code
= MapVirtualKey (wparam
, 2);
129 /* Windows 95 returns 0x8000, NT returns 0x80000000. */
130 return (code
& 0x80008000) ? 1 : 0;
134 /* The return code indicates key code size. cpID is the codepage to
135 use for translation to Unicode; -1 means use the current console
139 /* return code -1 means that event_queue_ptr won't be incremented.
140 In other word, this event makes two key codes. (by himi) */
142 key_event (KEY_EVENT_RECORD
*event
, struct input_event
*emacs_ev
, int *isdead
)
144 static int mod_key_state
= 0;
149 /* Skip key-up events. */
150 if (!event
->bKeyDown
)
152 switch (event
->wVirtualKeyCode
)
155 mod_key_state
&= ~LEFT_WIN_PRESSED
;
158 mod_key_state
&= ~RIGHT_WIN_PRESSED
;
161 mod_key_state
&= ~APPS_PRESSED
;
167 /* Ignore keystrokes we fake ourself; see below. */
168 if (faked_key
== event
->wVirtualKeyCode
)
174 /* To make it easier to debug this code, ignore modifier keys! */
175 switch (event
->wVirtualKeyCode
)
178 if (NILP (Vw32_pass_lwindow_to_system
))
180 /* Prevent system from acting on keyup (which opens the Start
181 menu if no other key was pressed) by simulating a press of
182 Space which we will ignore. */
183 if ((mod_key_state
& LEFT_WIN_PRESSED
) == 0)
185 if (NUMBERP (Vw32_phantom_key_code
))
186 faked_key
= XUINT (Vw32_phantom_key_code
) & 255;
188 faked_key
= VK_SPACE
;
189 keybd_event (faked_key
, (BYTE
) MapVirtualKey (faked_key
, 0), 0, 0);
192 mod_key_state
|= LEFT_WIN_PRESSED
;
193 if (!NILP (Vw32_lwindow_modifier
))
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;
204 faked_key
= VK_SPACE
;
205 keybd_event (faked_key
, (BYTE
) MapVirtualKey (faked_key
, 0), 0, 0);
208 mod_key_state
|= RIGHT_WIN_PRESSED
;
209 if (!NILP (Vw32_rwindow_modifier
))
213 mod_key_state
|= APPS_PRESSED
;
214 if (!NILP (Vw32_apps_modifier
))
218 /* Decide whether to treat as modifier or function key. */
219 if (NILP (Vw32_enable_caps_lock
))
220 goto disable_lock_key
;
223 /* Decide whether to treat as modifier or function key. */
224 if (NILP (Vw32_enable_num_lock
))
225 goto disable_lock_key
;
228 /* Decide whether to treat as modifier or function key. */
229 if (NILP (Vw32_scroll_lock_modifier
))
230 goto disable_lock_key
;
233 /* Ensure the appropriate lock key state is off (and the
234 indicator light as well). */
235 wParam
= event
->wVirtualKeyCode
;
236 if (GetAsyncKeyState (wParam
) & 0x8000)
238 /* Fake another press of the relevant key. Apparently, this
239 really is the only way to turn off the indicator. */
241 keybd_event ((BYTE
) wParam
, (BYTE
) MapVirtualKey (wParam
, 0),
242 KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
, 0);
243 keybd_event ((BYTE
) wParam
, (BYTE
) MapVirtualKey (wParam
, 0),
244 KEYEVENTF_EXTENDEDKEY
| 0, 0);
245 keybd_event ((BYTE
) wParam
, (BYTE
) MapVirtualKey (wParam
, 0),
246 KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
, 0);
254 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
255 which is confusing for purposes of key binding; convert
256 VK_CANCEL events into VK_PAUSE events. */
257 event
->wVirtualKeyCode
= VK_PAUSE
;
260 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
261 for purposes of key binding; convert these back into
262 VK_NUMLOCK events, at least when we want to see NumLock key
263 presses. (Note that there is never any possibility that
264 VK_PAUSE with Ctrl really is C-Pause as per above.) */
265 if (NILP (Vw32_enable_num_lock
)
266 && (event
->dwControlKeyState
267 & (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
)) != 0)
268 event
->wVirtualKeyCode
= VK_NUMLOCK
;
272 /* Recognize state of Windows and Apps keys. */
273 event
->dwControlKeyState
|= mod_key_state
;
275 /* Distinguish numeric keypad keys from extended keys. */
276 event
->wVirtualKeyCode
=
277 map_keypad_keys (event
->wVirtualKeyCode
,
278 (event
->dwControlKeyState
& ENHANCED_KEY
));
280 if (lispy_function_keys
[event
->wVirtualKeyCode
] == 0)
282 if (!NILP (Vw32_recognize_altgr
)
283 && (event
->dwControlKeyState
& LEFT_CTRL_PRESSED
)
284 && (event
->dwControlKeyState
& RIGHT_ALT_PRESSED
))
286 /* Don't try to interpret AltGr key chords; ToAscii seems not
287 to process them correctly. */
289 /* Handle key chords including any modifiers other than shift
290 directly, in order to preserve as much modifier information as
292 else if (event
->dwControlKeyState
293 & ( RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
294 | RIGHT_ALT_PRESSED
| LEFT_ALT_PRESSED
295 | (!NILP (Vw32_lwindow_modifier
) ? LEFT_WIN_PRESSED
: 0)
296 | (!NILP (Vw32_rwindow_modifier
) ? RIGHT_WIN_PRESSED
: 0)
297 | (!NILP (Vw32_apps_modifier
) ? APPS_PRESSED
: 0)
298 | (!NILP (Vw32_scroll_lock_modifier
) ? SCROLLLOCK_ON
: 0)))
300 /* Don't translate modified alphabetic keystrokes, so the user
301 doesn't need to constantly switch layout to type control or
302 meta keystrokes when the normal layout translates
303 alphabetic characters to non-ascii characters. */
304 if ('A' <= event
->wVirtualKeyCode
&& event
->wVirtualKeyCode
<= 'Z')
306 event
->uChar
.AsciiChar
= event
->wVirtualKeyCode
;
307 if ((event
->dwControlKeyState
& SHIFT_PRESSED
) == 0)
308 event
->uChar
.AsciiChar
+= ('a' - 'A');
310 /* Try to handle unrecognized keystrokes by determining the
311 base character (ie. translating the base key plus shift
313 else if (event
->uChar
.AsciiChar
== 0)
314 w32_kbd_patch_key (event
, -1);
317 if (event
->uChar
.AsciiChar
== 0)
319 emacs_ev
->kind
= NO_EVENT
;
322 else if (event
->uChar
.AsciiChar
> 0)
324 /* Pure ASCII characters < 128. */
325 emacs_ev
->kind
= ASCII_KEYSTROKE_EVENT
;
326 emacs_ev
->code
= event
->uChar
.AsciiChar
;
328 else if (event
->uChar
.UnicodeChar
> 0
329 && w32_console_unicode_input
)
331 /* Unicode codepoint; only valid if we are using Unicode
332 console input mode. */
333 emacs_ev
->kind
= MULTIBYTE_CHAR_KEYSTROKE_EVENT
;
334 emacs_ev
->code
= event
->uChar
.UnicodeChar
;
338 /* Fallback handling of non-ASCII characters for non-Unicode
339 versions of Windows, and for non-Unicode input on NT
340 family of Windows. Only characters in the current
341 console codepage are supported by this fallback. */
346 /* Get the current console input codepage to interpret this
347 key with. Note that the system defaults for the OEM
348 codepage could have been changed by calling SetConsoleCP
349 or w32-set-console-codepage, so using GetLocaleInfo to
350 get LOCALE_IDEFAULTCODEPAGE is not TRT here. */
351 cpId
= GetConsoleCP ();
354 dbcs
[1] = event
->uChar
.AsciiChar
;
358 if (!MultiByteToWideChar (cpId
, 0, dbcs
, 2, &code
, 1))
361 DebPrint (("Invalid DBCS sequence: %d %d\n",
363 emacs_ev
->kind
= NO_EVENT
;
366 else if (IsDBCSLeadByteEx (cpId
, dbcs
[1]))
369 emacs_ev
->kind
= NO_EVENT
;
373 if (!MultiByteToWideChar (cpId
, 0, &dbcs
[1], 1, &code
, 1))
376 DebPrint (("Invalid character: %d\n", dbcs
[1]));
377 emacs_ev
->kind
= NO_EVENT
;
380 emacs_ev
->kind
= MULTIBYTE_CHAR_KEYSTROKE_EVENT
;
381 emacs_ev
->code
= code
;
386 /* Function keys and other non-character keys. */
387 emacs_ev
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
388 emacs_ev
->code
= event
->wVirtualKeyCode
;
391 XSETFRAME (emacs_ev
->frame_or_window
, get_frame ());
392 emacs_ev
->modifiers
= w32_kbd_mods_to_emacs (event
->dwControlKeyState
,
393 event
->wVirtualKeyCode
);
394 emacs_ev
->timestamp
= GetTickCount ();
398 /* Mouse position hook. */
400 w32_console_mouse_position (struct frame
**f
,
402 Lisp_Object
*bar_window
,
403 enum scroll_bar_part
*part
,
415 SELECTED_FRAME ()->mouse_moved
= 0;
417 XSETINT (*x
, movement_pos
.X
);
418 XSETINT (*y
, movement_pos
.Y
);
419 *time
= movement_time
;
424 /* Remember mouse motion and notify emacs. */
426 mouse_moved_to (int x
, int y
)
428 /* If we're in the same place, ignore it. */
429 if (x
!= movement_pos
.X
|| y
!= movement_pos
.Y
)
431 SELECTED_FRAME ()->mouse_moved
= 1;
434 movement_time
= GetTickCount ();
438 /* Consoles return button bits in a strange order:
439 least significant - Leftmost button
440 next - Rightmost button
444 For the 3 standard buttons, we have:
448 Others increase from there. */
450 #define NUM_TRANSLATED_MOUSE_BUTTONS 5
451 static int emacs_button_translation
[NUM_TRANSLATED_MOUSE_BUTTONS
] =
457 do_mouse_event (MOUSE_EVENT_RECORD
*event
,
458 struct input_event
*emacs_ev
)
460 static DWORD button_state
= 0;
461 static Lisp_Object last_mouse_window
;
462 DWORD but_change
, mask
, flags
= event
->dwEventFlags
;
469 struct frame
*f
= get_frame ();
470 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO (f
);
471 int mx
= event
->dwMousePosition
.X
, my
= event
->dwMousePosition
.Y
;
473 mouse_moved_to (mx
, my
);
477 if (hlinfo
->mouse_face_hidden
)
479 hlinfo
->mouse_face_hidden
= 0;
480 clear_mouse_face (hlinfo
);
483 /* Generate SELECT_WINDOW_EVENTs when needed. */
484 if (!NILP (Vmouse_autoselect_window
))
486 Lisp_Object mouse_window
= window_from_coordinates (f
, mx
, my
,
488 /* A window will be selected only when it is not
489 selected now, and the last mouse movement event was
490 not in it. A minibuffer window will be selected iff
492 if (WINDOWP (mouse_window
)
493 && !EQ (mouse_window
, last_mouse_window
)
494 && !EQ (mouse_window
, selected_window
))
496 struct input_event event
;
499 event
.kind
= SELECT_WINDOW_EVENT
;
500 event
.frame_or_window
= mouse_window
;
502 event
.timestamp
= movement_time
;
503 kbd_buffer_store_event (&event
);
505 last_mouse_window
= mouse_window
;
508 last_mouse_window
= Qnil
;
510 previous_help_echo_string
= help_echo_string
;
511 help_echo_string
= help_echo_object
= help_echo_window
= Qnil
;
513 note_mouse_highlight (f
, mx
, my
);
514 /* If the contents of the global variable help_echo has
515 changed (inside note_mouse_highlight), generate a HELP_EVENT. */
516 if (!NILP (help_echo_string
) || !NILP (previous_help_echo_string
))
517 gen_help_event (help_echo_string
, selected_frame
,
518 help_echo_window
, help_echo_object
,
521 /* We already called kbd_buffer_store_event, so indicate the
522 the caller it shouldn't. */
528 struct frame
*f
= get_frame ();
529 int mx
= event
->dwMousePosition
.X
, my
= event
->dwMousePosition
.Y
;
530 bool down_p
= (event
->dwButtonState
& 0x10000000) != 0;
533 flags
== MOUSE_HWHEELED
? HORIZ_WHEEL_EVENT
: WHEEL_EVENT
;
535 emacs_ev
->modifiers
= down_p
? down_modifier
: up_modifier
;
536 emacs_ev
->modifiers
|=
537 w32_kbd_mods_to_emacs (event
->dwControlKeyState
, 0);
538 XSETINT (emacs_ev
->x
, mx
);
539 XSETINT (emacs_ev
->y
, my
);
540 XSETFRAME (emacs_ev
->frame_or_window
, f
);
541 emacs_ev
->arg
= Qnil
;
542 emacs_ev
->timestamp
= GetTickCount ();
546 default: /* mouse pressed or released */
547 /* It looks like the console code sends us a button-release
548 mouse event with dwButtonState == 0 when a window is
549 activated and when the mouse is first clicked. Ignore this
551 if (event
->dwButtonState
== button_state
)
554 emacs_ev
->kind
= MOUSE_CLICK_EVENT
;
556 /* Find out what button has changed state since the last button
558 but_change
= button_state
^ event
->dwButtonState
;
560 for (i
= 0; mask
; i
++, mask
<<= 1)
561 if (but_change
& mask
)
563 if (i
< NUM_TRANSLATED_MOUSE_BUTTONS
)
564 emacs_ev
->code
= emacs_button_translation
[i
];
570 button_state
= event
->dwButtonState
;
571 emacs_ev
->modifiers
=
572 w32_kbd_mods_to_emacs (event
->dwControlKeyState
, 0)
573 | ((event
->dwButtonState
& mask
) ? down_modifier
: up_modifier
);
575 XSETFASTINT (emacs_ev
->x
, event
->dwMousePosition
.X
);
576 XSETFASTINT (emacs_ev
->y
, event
->dwMousePosition
.Y
);
577 XSETFRAME (emacs_ev
->frame_or_window
, get_frame ());
578 emacs_ev
->arg
= Qnil
;
579 emacs_ev
->timestamp
= GetTickCount ();
586 resize_event (WINDOW_BUFFER_SIZE_RECORD
*event
)
588 struct frame
*f
= get_frame ();
590 change_frame_size (f
, event
->dwSize
.X
, event
->dwSize
.Y
, 0, 1, 0, 0);
591 SET_FRAME_GARBAGED (f
);
595 maybe_generate_resize_event (void)
597 CONSOLE_SCREEN_BUFFER_INFO info
;
598 struct frame
*f
= get_frame ();
600 GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE
), &info
);
602 /* It is okay to call this unconditionally, since it will do nothing
603 if the size hasn't actually changed. */
604 change_frame_size (f
,
605 1 + info
.srWindow
.Right
- info
.srWindow
.Left
,
606 1 + info
.srWindow
.Bottom
- info
.srWindow
.Top
,
612 handle_file_notifications (struct input_event
*hold_quit
)
614 BYTE
*p
= file_notifications
;
615 FILE_NOTIFY_INFORMATION
*fni
= (PFILE_NOTIFY_INFORMATION
)p
;
617 = offsetof (FILE_NOTIFY_INFORMATION
, FileName
) + sizeof(wchar_t);
618 struct input_event inev
;
621 /* We cannot process notification before Emacs is fully initialized,
622 since we need the UTF-16LE coding-system to be set up. */
625 notification_buffer_in_use
= 0;
630 if (notification_buffer_in_use
)
632 DWORD info_size
= notifications_size
;
633 Lisp_Object cs
= intern ("utf-16le");
634 Lisp_Object obj
= w32_get_watch_object (notifications_desc
);
636 /* notifications_size could be zero when the buffer of
637 notifications overflowed on the OS level, or when the
638 directory being watched was itself deleted. Do nothing in
641 && !NILP (obj
) && CONSP (obj
))
643 Lisp_Object callback
= XCDR (obj
);
647 while (info_size
>= min_size
)
649 Lisp_Object utf_16_fn
650 = make_unibyte_string ((char *)fni
->FileName
,
651 fni
->FileNameLength
);
652 /* Note: mule-conf is preloaded, so utf-16le must
653 already be defined at this point. */
655 = code_convert_string_norecord (utf_16_fn
, cs
, 0);
656 Lisp_Object action
= lispy_file_action (fni
->Action
);
658 inev
.kind
= FILE_NOTIFY_EVENT
;
659 inev
.code
= (ptrdiff_t)XINT (XIL ((EMACS_INT
)notifications_desc
));
660 inev
.timestamp
= GetTickCount ();
662 inev
.frame_or_window
= callback
;
663 inev
.arg
= Fcons (action
, fname
);
664 kbd_buffer_store_event_hold (&inev
, hold_quit
);
666 if (!fni
->NextEntryOffset
)
668 p
+= fni
->NextEntryOffset
;
669 fni
= (PFILE_NOTIFY_INFORMATION
)p
;
670 info_size
-= fni
->NextEntryOffset
;
673 notification_buffer_in_use
= 0;
678 #else /* !HAVE_W32NOTIFY */
680 handle_file_notifications (struct input_event
*hold_quit
)
684 #endif /* !HAVE_W32NOTIFY */
686 /* Here's an overview of how Emacs input works in non-GUI sessions on
687 MS-Windows. (For description of the GUI input, see the commentary
688 before w32_msg_pump in w32fns.c.)
690 When Emacs is idle, it loops inside wait_reading_process_output,
691 calling pselect periodically to check whether any input is
692 available. On Windows, pselect is redirected to sys_select, which
693 uses MsgWaitForMultipleObjects to wait for input, either from the
694 keyboard or from any of the Emacs subprocesses. In addition,
695 MsgWaitForMultipleObjects wakes up when some Windows message is
696 posted to the input queue of the Emacs's main thread (which is the
697 thread in which sys_select runs).
699 When the Emacs's console window has focus, Windows sends input
700 events that originate from the keyboard or the mouse; these events
701 wake up MsgWaitForMultipleObjects, which reports that input is
702 available. Emacs then calls w32_console_read_socket, below, to
703 read the input. w32_console_read_socket uses
704 GetNumberOfConsoleInputEvents and ReadConsoleInput to peek at and
705 read the console input events.
707 One type of non-keyboard input event that gets reported as input
708 available is due to the Emacs's console window receiving focus.
709 When that happens, Emacs gets the FOCUS_EVENT event and sys_select
710 reports some input; however, w32_console_read_socket ignores such
711 events when called to read them.
713 Note that any other Windows message sent to the main thread will
714 also wake up MsgWaitForMultipleObjects. These messages get
715 immediately dispatched to their destinations by calling
716 drain_message_queue. */
719 w32_console_read_socket (struct terminal
*terminal
,
720 struct input_event
*hold_quit
)
729 int nfnotify
= handle_file_notifications (hold_quit
);
731 nev
= fill_queue (0);
734 /* If nev == -1, there was some kind of error
735 If nev == 0 then no events were available
744 struct input_event inev
;
745 /* Having a separate variable with this value makes
746 debugging easier, as otherwise the compiler might
747 rearrange the switch below in a way that makes it hard to
748 track the event type. */
749 unsigned evtype
= queue_ptr
->EventType
;
752 inev
.kind
= NO_EVENT
;
758 add
= key_event (&queue_ptr
->Event
.KeyEvent
, &inev
, &isdead
);
759 if (add
== -1) /* 95.7.25 by himi */
765 kbd_buffer_store_event_hold (&inev
, hold_quit
);
769 add
= do_mouse_event (&queue_ptr
->Event
.MouseEvent
, &inev
);
771 kbd_buffer_store_event_hold (&inev
, hold_quit
);
774 case WINDOW_BUFFER_SIZE_EVENT
:
775 if (w32_use_full_screen_buffer
)
776 resize_event (&queue_ptr
->Event
.WindowBufferSizeEvent
);
781 /* Internal event types, ignored. */
790 /* We don't get told about changes in the window size (only the buffer
791 size, which we no longer care about), so we have to check it
793 if (!w32_use_full_screen_buffer
)
794 maybe_generate_resize_event ();