| 1 | /* Input event support for Emacs on the Microsoft Windows API. |
| 2 | Copyright (C) 1992-1993, 1995, 2001-2014 Free Software Foundation, |
| 3 | Inc. |
| 4 | |
| 5 | This file is part of GNU Emacs. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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/>. */ |
| 19 | |
| 20 | /* |
| 21 | Drew Bliss 01-Oct-93 |
| 22 | Adapted from ntkbd.c by Tim Fleehart |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include <config.h> |
| 27 | #include <stdio.h> |
| 28 | #include <windows.h> |
| 29 | |
| 30 | #ifndef MOUSE_MOVED |
| 31 | #define MOUSE_MOVED 1 |
| 32 | #endif |
| 33 | #ifndef MOUSE_HWHEELED |
| 34 | #define MOUSE_HWHEELED 8 |
| 35 | #endif |
| 36 | |
| 37 | #include "lisp.h" |
| 38 | #include "keyboard.h" |
| 39 | #include "frame.h" |
| 40 | #include "dispextern.h" |
| 41 | #include "window.h" |
| 42 | #include "blockinput.h" |
| 43 | #include "termhooks.h" |
| 44 | #include "termchar.h" |
| 45 | #include "w32heap.h" |
| 46 | #include "w32term.h" |
| 47 | #include "w32inevt.h" |
| 48 | |
| 49 | /* stdin, from w32console.c */ |
| 50 | extern HANDLE keyboard_handle; |
| 51 | |
| 52 | /* Info for last mouse motion */ |
| 53 | static COORD movement_pos; |
| 54 | static Time movement_time; |
| 55 | |
| 56 | /* from w32fns.c */ |
| 57 | extern unsigned int map_keypad_keys (unsigned int, unsigned int); |
| 58 | extern unsigned int w32_key_to_modifier (int key); |
| 59 | |
| 60 | /* Event queue */ |
| 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; |
| 64 | |
| 65 | /* Temporarily store lead byte of DBCS input sequences. */ |
| 66 | static char dbcs_lead = 0; |
| 67 | |
| 68 | static inline BOOL |
| 69 | w32_read_console_input (HANDLE h, INPUT_RECORD *rec, DWORD recsize, |
| 70 | DWORD *waiting) |
| 71 | { |
| 72 | return (w32_console_unicode_input |
| 73 | ? ReadConsoleInputW (h, rec, recsize, waiting) |
| 74 | : ReadConsoleInputA (h, rec, recsize, waiting)); |
| 75 | } |
| 76 | |
| 77 | /* Set by w32_console_toggle_lock_key. */ |
| 78 | int faked_key; |
| 79 | |
| 80 | static int |
| 81 | fill_queue (BOOL block) |
| 82 | { |
| 83 | BOOL rc; |
| 84 | DWORD events_waiting; |
| 85 | |
| 86 | if (queue_ptr < queue_end) |
| 87 | return queue_end-queue_ptr; |
| 88 | |
| 89 | if (!block) |
| 90 | { |
| 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)) |
| 94 | return -1; |
| 95 | if (events_waiting == 0) |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | rc = w32_read_console_input (keyboard_handle, event_queue, EVENT_QUEUE_SIZE, |
| 100 | &events_waiting); |
| 101 | if (!rc) |
| 102 | return -1; |
| 103 | queue_ptr = event_queue; |
| 104 | queue_end = event_queue + events_waiting; |
| 105 | return (int) events_waiting; |
| 106 | } |
| 107 | |
| 108 | /* In a generic, multi-frame world this should take a console handle |
| 109 | and return the frame for it. |
| 110 | |
| 111 | Right now, there's only one frame so return it. */ |
| 112 | static struct frame * |
| 113 | get_frame (void) |
| 114 | { |
| 115 | return SELECTED_FRAME (); |
| 116 | } |
| 117 | |
| 118 | /* Translate console modifiers to emacs modifiers. |
| 119 | German keyboard support (Kai Morgan Zeise 2/18/95). */ |
| 120 | |
| 121 | |
| 122 | #if 0 |
| 123 | /* Return nonzero if the virtual key is a dead key. */ |
| 124 | static int |
| 125 | is_dead_key (int wparam) |
| 126 | { |
| 127 | unsigned int code = MapVirtualKey (wparam, 2); |
| 128 | |
| 129 | /* Windows 95 returns 0x8000, NT returns 0x80000000. */ |
| 130 | return (code & 0x80008000) ? 1 : 0; |
| 131 | } |
| 132 | #endif |
| 133 | |
| 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 |
| 136 | input codepage. */ |
| 137 | |
| 138 | |
| 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) */ |
| 141 | static int |
| 142 | key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead) |
| 143 | { |
| 144 | static int mod_key_state = 0; |
| 145 | int wParam; |
| 146 | |
| 147 | *isdead = 0; |
| 148 | |
| 149 | /* Skip key-up events. */ |
| 150 | if (!event->bKeyDown) |
| 151 | { |
| 152 | switch (event->wVirtualKeyCode) |
| 153 | { |
| 154 | case VK_LWIN: |
| 155 | mod_key_state &= ~LEFT_WIN_PRESSED; |
| 156 | break; |
| 157 | case VK_RWIN: |
| 158 | mod_key_state &= ~RIGHT_WIN_PRESSED; |
| 159 | break; |
| 160 | case VK_APPS: |
| 161 | mod_key_state &= ~APPS_PRESSED; |
| 162 | break; |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | /* Ignore keystrokes we fake ourself; see below. */ |
| 168 | if (faked_key == event->wVirtualKeyCode) |
| 169 | { |
| 170 | faked_key = 0; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* To make it easier to debug this code, ignore modifier keys! */ |
| 175 | switch (event->wVirtualKeyCode) |
| 176 | { |
| 177 | case VK_LWIN: |
| 178 | if (NILP (Vw32_pass_lwindow_to_system)) |
| 179 | { |
| 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) |
| 184 | { |
| 185 | if (NUMBERP (Vw32_phantom_key_code)) |
| 186 | faked_key = XUINT (Vw32_phantom_key_code) & 255; |
| 187 | else |
| 188 | faked_key = VK_SPACE; |
| 189 | keybd_event (faked_key, (BYTE) MapVirtualKey (faked_key, 0), 0, 0); |
| 190 | } |
| 191 | } |
| 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)) |
| 198 | { |
| 199 | if ((mod_key_state & RIGHT_WIN_PRESSED) == 0) |
| 200 | { |
| 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); |
| 206 | } |
| 207 | } |
| 208 | mod_key_state |= RIGHT_WIN_PRESSED; |
| 209 | if (!NILP (Vw32_rwindow_modifier)) |
| 210 | return 0; |
| 211 | break; |
| 212 | case VK_APPS: |
| 213 | mod_key_state |= APPS_PRESSED; |
| 214 | if (!NILP (Vw32_apps_modifier)) |
| 215 | return 0; |
| 216 | break; |
| 217 | case VK_CAPITAL: |
| 218 | /* Decide whether to treat as modifier or function key. */ |
| 219 | if (NILP (Vw32_enable_caps_lock)) |
| 220 | goto disable_lock_key; |
| 221 | return 0; |
| 222 | case VK_NUMLOCK: |
| 223 | /* Decide whether to treat as modifier or function key. */ |
| 224 | if (NILP (Vw32_enable_num_lock)) |
| 225 | goto disable_lock_key; |
| 226 | return 0; |
| 227 | case VK_SCROLL: |
| 228 | /* Decide whether to treat as modifier or function key. */ |
| 229 | if (NILP (Vw32_scroll_lock_modifier)) |
| 230 | goto disable_lock_key; |
| 231 | return 0; |
| 232 | 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) |
| 237 | { |
| 238 | /* Fake another press of the relevant key. Apparently, this |
| 239 | really is the only way to turn off the indicator. */ |
| 240 | faked_key = wParam; |
| 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); |
| 247 | } |
| 248 | break; |
| 249 | case VK_MENU: |
| 250 | case VK_CONTROL: |
| 251 | case VK_SHIFT: |
| 252 | return 0; |
| 253 | case VK_CANCEL: |
| 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; |
| 258 | break; |
| 259 | case 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; |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | /* Recognize state of Windows and Apps keys. */ |
| 273 | event->dwControlKeyState |= mod_key_state; |
| 274 | |
| 275 | /* Distinguish numeric keypad keys from extended keys. */ |
| 276 | event->wVirtualKeyCode = |
| 277 | map_keypad_keys (event->wVirtualKeyCode, |
| 278 | (event->dwControlKeyState & ENHANCED_KEY)); |
| 279 | |
| 280 | if (lispy_function_keys[event->wVirtualKeyCode] == 0) |
| 281 | { |
| 282 | if (!NILP (Vw32_recognize_altgr) |
| 283 | && (event->dwControlKeyState & LEFT_CTRL_PRESSED) |
| 284 | && (event->dwControlKeyState & RIGHT_ALT_PRESSED)) |
| 285 | { |
| 286 | /* Don't try to interpret AltGr key chords; ToAscii seems not |
| 287 | to process them correctly. */ |
| 288 | } |
| 289 | /* Handle key chords including any modifiers other than shift |
| 290 | directly, in order to preserve as much modifier information as |
| 291 | possible. */ |
| 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))) |
| 299 | { |
| 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') |
| 305 | { |
| 306 | event->uChar.AsciiChar = event->wVirtualKeyCode; |
| 307 | if ((event->dwControlKeyState & SHIFT_PRESSED) == 0) |
| 308 | event->uChar.AsciiChar += ('a' - 'A'); |
| 309 | } |
| 310 | /* Try to handle unrecognized keystrokes by determining the |
| 311 | base character (ie. translating the base key plus shift |
| 312 | modifier). */ |
| 313 | else if (event->uChar.AsciiChar == 0) |
| 314 | w32_kbd_patch_key (event, -1); |
| 315 | } |
| 316 | |
| 317 | if (event->uChar.AsciiChar == 0) |
| 318 | { |
| 319 | emacs_ev->kind = NO_EVENT; |
| 320 | return 0; |
| 321 | } |
| 322 | else if (event->uChar.AsciiChar > 0) |
| 323 | { |
| 324 | /* Pure ASCII characters < 128. */ |
| 325 | emacs_ev->kind = ASCII_KEYSTROKE_EVENT; |
| 326 | emacs_ev->code = event->uChar.AsciiChar; |
| 327 | } |
| 328 | else if (event->uChar.UnicodeChar > 0 |
| 329 | && w32_console_unicode_input) |
| 330 | { |
| 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; |
| 335 | } |
| 336 | else |
| 337 | { |
| 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. */ |
| 342 | wchar_t code; |
| 343 | char dbcs[2]; |
| 344 | int cpId; |
| 345 | |
| 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 (); |
| 352 | |
| 353 | dbcs[0] = dbcs_lead; |
| 354 | dbcs[1] = event->uChar.AsciiChar; |
| 355 | if (dbcs_lead) |
| 356 | { |
| 357 | dbcs_lead = 0; |
| 358 | if (!MultiByteToWideChar (cpId, 0, dbcs, 2, &code, 1)) |
| 359 | { |
| 360 | /* Garbage */ |
| 361 | DebPrint (("Invalid DBCS sequence: %d %d\n", |
| 362 | dbcs[0], dbcs[1])); |
| 363 | emacs_ev->kind = NO_EVENT; |
| 364 | } |
| 365 | } |
| 366 | else if (IsDBCSLeadByteEx (cpId, dbcs[1])) |
| 367 | { |
| 368 | dbcs_lead = dbcs[1]; |
| 369 | emacs_ev->kind = NO_EVENT; |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | if (!MultiByteToWideChar (cpId, 0, &dbcs[1], 1, &code, 1)) |
| 374 | { |
| 375 | /* Garbage */ |
| 376 | DebPrint (("Invalid character: %d\n", dbcs[1])); |
| 377 | emacs_ev->kind = NO_EVENT; |
| 378 | } |
| 379 | } |
| 380 | emacs_ev->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT; |
| 381 | emacs_ev->code = code; |
| 382 | } |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | /* Function keys and other non-character keys. */ |
| 387 | emacs_ev->kind = NON_ASCII_KEYSTROKE_EVENT; |
| 388 | emacs_ev->code = event->wVirtualKeyCode; |
| 389 | } |
| 390 | |
| 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 (); |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | /* Mouse position hook. */ |
| 399 | void |
| 400 | w32_console_mouse_position (struct frame **f, |
| 401 | int insist, |
| 402 | Lisp_Object *bar_window, |
| 403 | enum scroll_bar_part *part, |
| 404 | Lisp_Object *x, |
| 405 | Lisp_Object *y, |
| 406 | Time *time) |
| 407 | { |
| 408 | block_input (); |
| 409 | |
| 410 | insist = insist; |
| 411 | |
| 412 | *f = get_frame (); |
| 413 | *bar_window = Qnil; |
| 414 | *part = 0; |
| 415 | SELECTED_FRAME ()->mouse_moved = 0; |
| 416 | |
| 417 | XSETINT (*x, movement_pos.X); |
| 418 | XSETINT (*y, movement_pos.Y); |
| 419 | *time = movement_time; |
| 420 | |
| 421 | unblock_input (); |
| 422 | } |
| 423 | |
| 424 | /* Remember mouse motion and notify emacs. */ |
| 425 | static void |
| 426 | mouse_moved_to (int x, int y) |
| 427 | { |
| 428 | /* If we're in the same place, ignore it. */ |
| 429 | if (x != movement_pos.X || y != movement_pos.Y) |
| 430 | { |
| 431 | SELECTED_FRAME ()->mouse_moved = 1; |
| 432 | movement_pos.X = x; |
| 433 | movement_pos.Y = y; |
| 434 | movement_time = GetTickCount (); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* Consoles return button bits in a strange order: |
| 439 | least significant - Leftmost button |
| 440 | next - Rightmost button |
| 441 | next - Leftmost+1 |
| 442 | next - Leftmost+2... |
| 443 | |
| 444 | For the 3 standard buttons, we have: |
| 445 | Left == 0 |
| 446 | Middle == 1 |
| 447 | Right == 2 |
| 448 | Others increase from there. */ |
| 449 | |
| 450 | #define NUM_TRANSLATED_MOUSE_BUTTONS 5 |
| 451 | static int emacs_button_translation[NUM_TRANSLATED_MOUSE_BUTTONS] = |
| 452 | { |
| 453 | 0, 2, 1, 3, 4 |
| 454 | }; |
| 455 | |
| 456 | static int |
| 457 | do_mouse_event (MOUSE_EVENT_RECORD *event, |
| 458 | struct input_event *emacs_ev) |
| 459 | { |
| 460 | static DWORD button_state = 0; |
| 461 | static Lisp_Object last_mouse_window; |
| 462 | DWORD but_change, mask, flags = event->dwEventFlags; |
| 463 | int i; |
| 464 | |
| 465 | switch (flags) |
| 466 | { |
| 467 | case MOUSE_MOVED: |
| 468 | { |
| 469 | struct frame *f = get_frame (); |
| 470 | Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f); |
| 471 | int mx = event->dwMousePosition.X, my = event->dwMousePosition.Y; |
| 472 | |
| 473 | mouse_moved_to (mx, my); |
| 474 | |
| 475 | if (f->mouse_moved) |
| 476 | { |
| 477 | if (hlinfo->mouse_face_hidden) |
| 478 | { |
| 479 | hlinfo->mouse_face_hidden = 0; |
| 480 | clear_mouse_face (hlinfo); |
| 481 | } |
| 482 | |
| 483 | /* Generate SELECT_WINDOW_EVENTs when needed. */ |
| 484 | if (!NILP (Vmouse_autoselect_window)) |
| 485 | { |
| 486 | Lisp_Object mouse_window = window_from_coordinates (f, mx, my, |
| 487 | 0, 0); |
| 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 |
| 491 | it is active. */ |
| 492 | if (WINDOWP (mouse_window) |
| 493 | && !EQ (mouse_window, last_mouse_window) |
| 494 | && !EQ (mouse_window, selected_window)) |
| 495 | { |
| 496 | struct input_event event; |
| 497 | |
| 498 | EVENT_INIT (event); |
| 499 | event.kind = SELECT_WINDOW_EVENT; |
| 500 | event.frame_or_window = mouse_window; |
| 501 | event.arg = Qnil; |
| 502 | event.timestamp = movement_time; |
| 503 | kbd_buffer_store_event (&event); |
| 504 | } |
| 505 | last_mouse_window = mouse_window; |
| 506 | } |
| 507 | else |
| 508 | last_mouse_window = Qnil; |
| 509 | |
| 510 | previous_help_echo_string = help_echo_string; |
| 511 | help_echo_string = help_echo_object = help_echo_window = Qnil; |
| 512 | help_echo_pos = -1; |
| 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, |
| 519 | help_echo_pos); |
| 520 | } |
| 521 | /* We already called kbd_buffer_store_event, so indicate the |
| 522 | the caller it shouldn't. */ |
| 523 | return 0; |
| 524 | } |
| 525 | case MOUSE_WHEELED: |
| 526 | case MOUSE_HWHEELED: |
| 527 | { |
| 528 | struct frame *f = get_frame (); |
| 529 | int mx = event->dwMousePosition.X, my = event->dwMousePosition.Y; |
| 530 | bool down_p = (event->dwButtonState & 0x10000000) != 0; |
| 531 | |
| 532 | emacs_ev->kind = |
| 533 | flags == MOUSE_HWHEELED ? HORIZ_WHEEL_EVENT : WHEEL_EVENT; |
| 534 | emacs_ev->code = 0; |
| 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 (); |
| 543 | return 1; |
| 544 | } |
| 545 | case DOUBLE_CLICK: |
| 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 |
| 550 | case. */ |
| 551 | if (event->dwButtonState == button_state) |
| 552 | return 0; |
| 553 | |
| 554 | emacs_ev->kind = MOUSE_CLICK_EVENT; |
| 555 | |
| 556 | /* Find out what button has changed state since the last button |
| 557 | event. */ |
| 558 | but_change = button_state ^ event->dwButtonState; |
| 559 | mask = 1; |
| 560 | for (i = 0; mask; i++, mask <<= 1) |
| 561 | if (but_change & mask) |
| 562 | { |
| 563 | if (i < NUM_TRANSLATED_MOUSE_BUTTONS) |
| 564 | emacs_ev->code = emacs_button_translation[i]; |
| 565 | else |
| 566 | emacs_ev->code = i; |
| 567 | break; |
| 568 | } |
| 569 | |
| 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); |
| 574 | |
| 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 (); |
| 580 | |
| 581 | return 1; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static void |
| 586 | resize_event (WINDOW_BUFFER_SIZE_RECORD *event) |
| 587 | { |
| 588 | struct frame *f = get_frame (); |
| 589 | |
| 590 | change_frame_size (f, event->dwSize.X, event->dwSize.Y, 0, 1, 0, 0); |
| 591 | SET_FRAME_GARBAGED (f); |
| 592 | } |
| 593 | |
| 594 | static void |
| 595 | maybe_generate_resize_event (void) |
| 596 | { |
| 597 | CONSOLE_SCREEN_BUFFER_INFO info; |
| 598 | struct frame *f = get_frame (); |
| 599 | |
| 600 | GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &info); |
| 601 | |
| 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, |
| 607 | 0, 0, 0, 0); |
| 608 | } |
| 609 | |
| 610 | #if HAVE_W32NOTIFY |
| 611 | int |
| 612 | handle_file_notifications (struct input_event *hold_quit) |
| 613 | { |
| 614 | BYTE *p = file_notifications; |
| 615 | FILE_NOTIFY_INFORMATION *fni = (PFILE_NOTIFY_INFORMATION)p; |
| 616 | const DWORD min_size |
| 617 | = offsetof (FILE_NOTIFY_INFORMATION, FileName) + sizeof(wchar_t); |
| 618 | struct input_event inev; |
| 619 | int nevents = 0; |
| 620 | |
| 621 | /* We cannot process notification before Emacs is fully initialized, |
| 622 | since we need the UTF-16LE coding-system to be set up. */ |
| 623 | if (!initialized) |
| 624 | { |
| 625 | notification_buffer_in_use = 0; |
| 626 | return nevents; |
| 627 | } |
| 628 | |
| 629 | enter_crit (); |
| 630 | if (notification_buffer_in_use) |
| 631 | { |
| 632 | DWORD info_size = notifications_size; |
| 633 | Lisp_Object cs = intern ("utf-16le"); |
| 634 | Lisp_Object obj = w32_get_watch_object (notifications_desc); |
| 635 | |
| 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 |
| 639 | that case. */ |
| 640 | if (info_size |
| 641 | && !NILP (obj) && CONSP (obj)) |
| 642 | { |
| 643 | Lisp_Object callback = XCDR (obj); |
| 644 | |
| 645 | EVENT_INIT (inev); |
| 646 | |
| 647 | while (info_size >= min_size) |
| 648 | { |
| 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. */ |
| 654 | Lisp_Object fname |
| 655 | = code_convert_string_norecord (utf_16_fn, cs, 0); |
| 656 | Lisp_Object action = lispy_file_action (fni->Action); |
| 657 | |
| 658 | inev.kind = FILE_NOTIFY_EVENT; |
| 659 | inev.code = (ptrdiff_t)XINT (XIL ((EMACS_INT)notifications_desc)); |
| 660 | inev.timestamp = GetTickCount (); |
| 661 | inev.modifiers = 0; |
| 662 | inev.frame_or_window = callback; |
| 663 | inev.arg = Fcons (action, fname); |
| 664 | kbd_buffer_store_event_hold (&inev, hold_quit); |
| 665 | |
| 666 | if (!fni->NextEntryOffset) |
| 667 | break; |
| 668 | p += fni->NextEntryOffset; |
| 669 | fni = (PFILE_NOTIFY_INFORMATION)p; |
| 670 | info_size -= fni->NextEntryOffset; |
| 671 | } |
| 672 | } |
| 673 | notification_buffer_in_use = 0; |
| 674 | } |
| 675 | leave_crit (); |
| 676 | return nevents; |
| 677 | } |
| 678 | #else /* !HAVE_W32NOTIFY */ |
| 679 | int |
| 680 | handle_file_notifications (struct input_event *hold_quit) |
| 681 | { |
| 682 | return 0; |
| 683 | } |
| 684 | #endif /* !HAVE_W32NOTIFY */ |
| 685 | |
| 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.) |
| 689 | |
| 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). |
| 698 | |
| 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. |
| 706 | |
| 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. |
| 712 | |
| 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. */ |
| 717 | |
| 718 | int |
| 719 | w32_console_read_socket (struct terminal *terminal, |
| 720 | struct input_event *hold_quit) |
| 721 | { |
| 722 | int nev, add; |
| 723 | int isdead; |
| 724 | |
| 725 | block_input (); |
| 726 | |
| 727 | for (;;) |
| 728 | { |
| 729 | int nfnotify = handle_file_notifications (hold_quit); |
| 730 | |
| 731 | nev = fill_queue (0); |
| 732 | if (nev <= 0) |
| 733 | { |
| 734 | /* If nev == -1, there was some kind of error |
| 735 | If nev == 0 then no events were available |
| 736 | so return. */ |
| 737 | if (nfnotify) |
| 738 | nev = 0; |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | while (nev > 0) |
| 743 | { |
| 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; |
| 750 | |
| 751 | EVENT_INIT (inev); |
| 752 | inev.kind = NO_EVENT; |
| 753 | inev.arg = Qnil; |
| 754 | |
| 755 | switch (evtype) |
| 756 | { |
| 757 | case KEY_EVENT: |
| 758 | add = key_event (&queue_ptr->Event.KeyEvent, &inev, &isdead); |
| 759 | if (add == -1) /* 95.7.25 by himi */ |
| 760 | { |
| 761 | queue_ptr--; |
| 762 | add = 1; |
| 763 | } |
| 764 | if (add) |
| 765 | kbd_buffer_store_event_hold (&inev, hold_quit); |
| 766 | break; |
| 767 | |
| 768 | case MOUSE_EVENT: |
| 769 | add = do_mouse_event (&queue_ptr->Event.MouseEvent, &inev); |
| 770 | if (add) |
| 771 | kbd_buffer_store_event_hold (&inev, hold_quit); |
| 772 | break; |
| 773 | |
| 774 | case WINDOW_BUFFER_SIZE_EVENT: |
| 775 | if (w32_use_full_screen_buffer) |
| 776 | resize_event (&queue_ptr->Event.WindowBufferSizeEvent); |
| 777 | break; |
| 778 | |
| 779 | case MENU_EVENT: |
| 780 | case FOCUS_EVENT: |
| 781 | /* Internal event types, ignored. */ |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | queue_ptr++; |
| 786 | nev--; |
| 787 | } |
| 788 | } |
| 789 | |
| 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 |
| 792 | periodically. */ |
| 793 | if (!w32_use_full_screen_buffer) |
| 794 | maybe_generate_resize_event (); |
| 795 | |
| 796 | unblock_input (); |
| 797 | return nev; |
| 798 | } |