]> code.delx.au - gnu-emacs/blobdiff - src/w32inevt.c
Revert recent change for Bug#8855.
[gnu-emacs] / src / w32inevt.c
index d0b097100fd2e4d28bcf32b12862835a65ef2761..899a6fb89bfff5b469c75b0f20160ed465456099 100644 (file)
@@ -1,5 +1,5 @@
-/* Input event support for Emacs on the Microsoft W32 API.
-   Copyright (C) 1992-1993, 1995, 2001-2011  Free Software Foundation, Inc.
+/* Input event support for Emacs on the Microsoft Windows API.
+   Copyright (C) 1992-1993, 1995, 2001-2012  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -25,7 +25,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <config.h>
 #include <stdio.h>
 #include <windows.h>
-#include <setjmp.h>
 
 #ifndef MOUSE_MOVED
 #define MOUSE_MOVED   1
@@ -35,23 +34,23 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "keyboard.h"
 #include "frame.h"
 #include "dispextern.h"
+#include "window.h"
 #include "blockinput.h"
 #include "termhooks.h"
+#include "termchar.h"
 #include "w32heap.h"
 #include "w32term.h"
+#include "w32inevt.h"
 
-/* stdin, from ntterm */
+/* stdin, from w32console.c */
 extern HANDLE keyboard_handle;
 
 /* Info for last mouse motion */
 static COORD movement_pos;
-static DWORD movement_time;
-
-/* from keyboard.c */
-extern void reinvoke_input_signal (void);
+static Time movement_time;
 
+/* from w32fns.c */
 extern unsigned int map_keypad_keys (unsigned int, unsigned int);
-
 extern unsigned int w32_key_to_modifier (int key);
 
 /* Event queue */
@@ -62,6 +61,18 @@ static INPUT_RECORD *queue_ptr = event_queue, *queue_end = event_queue;
 /* Temporarily store lead byte of DBCS input sequences.  */
 static char dbcs_lead = 0;
 
+static inline BOOL
+w32_read_console_input (HANDLE h, INPUT_RECORD *rec, DWORD recsize,
+                       DWORD *waiting)
+{
+  return (w32_console_unicode_input
+         ? ReadConsoleInputW (h, rec, recsize, waiting)
+         : ReadConsoleInputA (h, rec, recsize, waiting));
+}
+
+/* Set by w32_console_toggle_lock_key.  */
+int faked_key;
+
 static int
 fill_queue (BOOL block)
 {
@@ -81,8 +92,8 @@ fill_queue (BOOL block)
        return 0;
     }
 
-  rc = ReadConsoleInput (keyboard_handle, event_queue, EVENT_QUEUE_SIZE,
-                        &events_waiting);
+  rc = w32_read_console_input (keyboard_handle, event_queue, EVENT_QUEUE_SIZE,
+                              &events_waiting);
   if (!rc)
     return -1;
   queue_ptr = event_queue;
@@ -102,67 +113,7 @@ get_frame (void)
 
 /* Translate console modifiers to emacs modifiers.
    German keyboard support (Kai Morgan Zeise 2/18/95).  */
-int
-w32_kbd_mods_to_emacs (DWORD mods, WORD key)
-{
-  int retval = 0;
-
-  /* If we recognize right-alt and left-ctrl as AltGr, and it has been
-     pressed, first remove those modifiers.  */
-  if (!NILP (Vw32_recognize_altgr)
-      && (mods & (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED))
-      == (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED))
-    mods &= ~ (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED);
-
-  if (mods & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
-    retval = ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier);
 
-  if (mods & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
-    {
-      retval |= ctrl_modifier;
-      if ((mods & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
-         == (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
-       retval |= meta_modifier;
-    }
-
-  if (mods & LEFT_WIN_PRESSED)
-    retval |= w32_key_to_modifier (VK_LWIN);
-  if (mods & RIGHT_WIN_PRESSED)
-    retval |= w32_key_to_modifier (VK_RWIN);
-  if (mods & APPS_PRESSED)
-    retval |= w32_key_to_modifier (VK_APPS);
-  if (mods & SCROLLLOCK_ON)
-    retval |= w32_key_to_modifier (VK_SCROLL);
-
-  /* Just in case someone wanted the original behavior, make it
-     optional by setting w32-capslock-is-shiftlock to t.  */
-  if (NILP (Vw32_capslock_is_shiftlock)
-      /* Keys that should _not_ be affected by CapsLock.  */
-      && (    (key == VK_BACK)
-          || (key == VK_TAB)
-          || (key == VK_CLEAR)
-          || (key == VK_RETURN)
-          || (key == VK_ESCAPE)
-          || ((key >= VK_SPACE) && (key <= VK_HELP))
-          || ((key >= VK_NUMPAD0) && (key <= VK_F24))
-          || ((key >= VK_NUMPAD_CLEAR) && (key <= VK_NUMPAD_DELETE))
-        ))
-    {
-      /* Only consider shift state.  */
-      if ((mods & SHIFT_PRESSED) != 0)
-       retval |= shift_modifier;
-    }
-  else
-    {
-      /* Ignore CapsLock state if not enabled.  */
-      if (NILP (Vw32_enable_caps_lock))
-       mods &= ~CAPSLOCK_ON;
-      if ((mods & (SHIFT_PRESSED | CAPSLOCK_ON)) != 0)
-       retval |= shift_modifier;
-    }
-
-  return retval;
-}
 
 #if 0
 /* Return nonzero if the virtual key is a dead key.  */
@@ -176,94 +127,10 @@ is_dead_key (int wparam)
 }
 #endif
 
-/* The return code indicates key code size. */
-int
-w32_kbd_patch_key (KEY_EVENT_RECORD *event)
-{
-  unsigned int key_code = event->wVirtualKeyCode;
-  unsigned int mods = event->dwControlKeyState;
-  BYTE keystate[256];
-  static BYTE ansi_code[4];
-  static int isdead = 0;
+/* The return code indicates key code size.  cpID is the codepage to
+   use for translation to Unicode; -1 means use the current console
+   input codepage.  */
 
-  if (isdead == 2)
-    {
-      event->uChar.AsciiChar = ansi_code[2];
-      isdead = 0;
-      return 1;
-    }
-  if (event->uChar.AsciiChar != 0)
-    return 1;
-
-  memset (keystate, 0, sizeof (keystate));
-  keystate[key_code] = 0x80;
-  if (mods & SHIFT_PRESSED)
-    keystate[VK_SHIFT] = 0x80;
-  if (mods & CAPSLOCK_ON)
-    keystate[VK_CAPITAL] = 1;
-  /* If we recognize right-alt and left-ctrl as AltGr, set the key
-     states accordingly before invoking ToAscii.  */
-  if (!NILP (Vw32_recognize_altgr)
-      && (mods & LEFT_CTRL_PRESSED) && (mods & RIGHT_ALT_PRESSED))
-    {
-      keystate[VK_CONTROL] = 0x80;
-      keystate[VK_LCONTROL] = 0x80;
-      keystate[VK_MENU] = 0x80;
-      keystate[VK_RMENU] = 0x80;
-    }
-
-#if 0
-  /* Because of an OS bug, ToAscii corrupts the stack when called to
-     convert a dead key in console mode on NT4.  Unfortunately, trying
-     to check for dead keys using MapVirtualKey doesn't work either -
-     these functions apparently use internal information about keyboard
-     layout which doesn't get properly updated in console programs when
-     changing layout (though apparently it gets partly updated,
-     otherwise ToAscii wouldn't crash).  */
-  if (is_dead_key (event->wVirtualKeyCode))
-    return 0;
-#endif
-
-  /* On NT, call ToUnicode instead and then convert to the current
-     locale's default codepage.  */
-  if (os_subtype == OS_NT)
-    {
-      WCHAR buf[128];
-
-      isdead = ToUnicode (event->wVirtualKeyCode, event->wVirtualScanCode,
-                         keystate, buf, 128, 0);
-      if (isdead > 0)
-       {
-         char cp[20];
-         int cpId;
-
-         event->uChar.UnicodeChar = buf[isdead - 1];
-
-         GetLocaleInfo (GetThreadLocale (),
-                        LOCALE_IDEFAULTANSICODEPAGE, cp, 20);
-         cpId = atoi (cp);
-         isdead = WideCharToMultiByte (cpId, 0, buf, isdead,
-                                       ansi_code, 4, NULL, NULL);
-       }
-      else
-       isdead = 0;
-    }
-  else
-    {
-      isdead = ToAscii (event->wVirtualKeyCode, event->wVirtualScanCode,
-                        keystate, (LPWORD) ansi_code, 0);
-    }
-
-  if (isdead == 0)
-    return 0;
-  event->uChar.AsciiChar = ansi_code[0];
-  return isdead;
-}
-
-
-extern const char *const lispy_function_keys[];
-
-static int faked_key = 0;
 
 /* return code -1 means that event_queue_ptr won't be incremented.
    In other word, this event makes two key codes.   (by himi)       */
@@ -440,7 +307,7 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
              base character (ie. translating the base key plus shift
              modifier).  */
          else if (event->uChar.AsciiChar == 0)
-           w32_kbd_patch_key (event);
+           w32_kbd_patch_key (event, -1);
        }
 
       if (event->uChar.AsciiChar == 0)
@@ -450,26 +317,34 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
        }
       else if (event->uChar.AsciiChar > 0)
        {
+         /* Pure ASCII characters < 128.  */
          emacs_ev->kind = ASCII_KEYSTROKE_EVENT;
          emacs_ev->code = event->uChar.AsciiChar;
        }
-      else if (event->uChar.UnicodeChar > 0)
+      else if (event->uChar.UnicodeChar > 0
+              && w32_console_unicode_input)
        {
+         /* Unicode codepoint; only valid if we are using Unicode
+            console input mode.  */
          emacs_ev->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
          emacs_ev->code = event->uChar.UnicodeChar;
        }
       else
        {
-         /* Fallback for non-Unicode versions of Windows.  */
+         /* Fallback handling of non-ASCII characters for non-Unicode
+            versions of Windows, and for non-Unicode input on NT
+            family of Windows.  Only characters in the current
+            console codepage are supported by this fallback.  */
          wchar_t code;
          char dbcs[2];
-          char cp[20];
           int cpId;
 
-         /* Get the codepage to interpret this key with.  */
-          GetLocaleInfo (GetThreadLocale (),
-                        LOCALE_IDEFAULTANSICODEPAGE, cp, 20);
-          cpId = atoi (cp);
+         /* Get the current console input codepage to interpret this
+            key with.  Note that the system defaults for the OEM
+            codepage could have been changed by calling SetConsoleCP
+            or w32-set-console-codepage, so using GetLocaleInfo to
+            get LOCALE_IDEFAULTCODEPAGE is not TRT here.  */
+          cpId = GetConsoleCP ();
 
          dbcs[0] = dbcs_lead;
          dbcs[1] = event->uChar.AsciiChar;
@@ -504,6 +379,7 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
     }
   else
     {
+      /* Function keys and other non-character keys.  */
       emacs_ev->kind = NON_ASCII_KEYSTROKE_EVENT;
       emacs_ev->code = event->wVirtualKeyCode;
     }
@@ -515,32 +391,6 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
   return 1;
 }
 
-int
-w32_console_toggle_lock_key (int vk_code, Lisp_Object new_state)
-{
-  int cur_state = (GetKeyState (vk_code) & 1);
-
-  if (NILP (new_state)
-      || (NUMBERP (new_state)
-         && ((XUINT (new_state)) & 1) != cur_state))
-    {
-      faked_key = vk_code;
-
-      keybd_event ((BYTE) vk_code,
-                  (BYTE) MapVirtualKey (vk_code, 0),
-                  KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
-      keybd_event ((BYTE) vk_code,
-                  (BYTE) MapVirtualKey (vk_code, 0),
-                  KEYEVENTF_EXTENDEDKEY | 0, 0);
-      keybd_event ((BYTE) vk_code,
-                  (BYTE) MapVirtualKey (vk_code, 0),
-                  KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
-      cur_state = !cur_state;
-    }
-
-  return cur_state;
-}
-
 /* Mouse position hook.  */
 void
 w32_console_mouse_position (FRAME_PTR *f,
@@ -549,9 +399,9 @@ w32_console_mouse_position (FRAME_PTR *f,
                            enum scroll_bar_part *part,
                            Lisp_Object *x,
                            Lisp_Object *y,
-                           unsigned long *time)
+                           Time *time)
 {
-  BLOCK_INPUT;
+  block_input ();
 
   insist = insist;
 
@@ -564,14 +414,14 @@ w32_console_mouse_position (FRAME_PTR *f,
   XSETINT (*y, movement_pos.Y);
   *time = movement_time;
 
-  UNBLOCK_INPUT;
+  unblock_input ();
 }
 
 /* Remember mouse motion and notify emacs.  */
 static void
 mouse_moved_to (int x, int y)
 {
-  /* If we're in the same place, ignore it */
+  /* If we're in the same place, ignore it */
   if (x != movement_pos.X || y != movement_pos.Y)
     {
       SELECTED_FRAME ()->mouse_moved = 1;
@@ -604,14 +454,63 @@ do_mouse_event (MOUSE_EVENT_RECORD *event,
                struct input_event *emacs_ev)
 {
   static DWORD button_state = 0;
+  static Lisp_Object last_mouse_window;
   DWORD but_change, mask;
   int i;
 
   if (event->dwEventFlags == MOUSE_MOVED)
     {
-      /* For movement events we just note that the mouse has moved
-        so that emacs will generate drag events.  */
-      mouse_moved_to (event->dwMousePosition.X, event->dwMousePosition.Y);
+      FRAME_PTR f = SELECTED_FRAME ();
+      Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
+      int mx = event->dwMousePosition.X, my = event->dwMousePosition.Y;
+
+      mouse_moved_to (mx, my);
+
+      if (f->mouse_moved)
+       {
+         if (hlinfo->mouse_face_hidden)
+           {
+             hlinfo->mouse_face_hidden = 0;
+             clear_mouse_face (hlinfo);
+           }
+
+         /* Generate SELECT_WINDOW_EVENTs when needed.  */
+         if (!NILP (Vmouse_autoselect_window))
+           {
+             Lisp_Object mouse_window = window_from_coordinates (f, mx, my,
+                                                                 0, 0);
+             /* A window will be selected only when it is not
+                selected now, and the last mouse movement event was
+                not in it.  A minibuffer window will be selected iff
+                it is active.  */
+             if (WINDOWP (mouse_window)
+                 && !EQ (mouse_window, last_mouse_window)
+                 && !EQ (mouse_window, selected_window))
+               {
+                 struct input_event event;
+
+                 EVENT_INIT (event);
+                 event.kind = SELECT_WINDOW_EVENT;
+                 event.frame_or_window = mouse_window;
+                 event.arg = Qnil;
+                 event.timestamp = movement_time;
+                 kbd_buffer_store_event (&event);
+               }
+             last_mouse_window = mouse_window;
+           }
+         else
+           last_mouse_window = Qnil;
+
+         previous_help_echo_string = help_echo_string;
+         help_echo_string = help_echo_object = help_echo_window = Qnil;
+         help_echo_pos = -1;
+         note_mouse_highlight (f, mx, my);
+         /* If the contents of the global variable help_echo has
+            changed (inside note_mouse_highlight), generate a HELP_EVENT.  */
+         if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
+           gen_help_event (help_echo_string, selected_frame, help_echo_window,
+                           help_echo_object, help_echo_pos);
+       }
       return 0;
     }
 
@@ -677,23 +576,46 @@ maybe_generate_resize_event (void)
                     0, 0, 0);
 }
 
+/* Here's an overview of how Emacs input works in non-GUI sessions on
+   MS-Windows.  (For description of the GUI input, see the commentary
+   before w32_msg_pump in w32fns.c.)
+
+   When Emacs is idle, it loops inside wait_reading_process_output,
+   calling pselect periodically to check whether any input is
+   available.  On Windows, pselect is redirected to sys_select, which
+   uses MsgWaitForMultipleObjects to wait for input, either from the
+   keyboard or from any of the Emacs subprocesses.  In addition,
+   MsgWaitForMultipleObjects wakes up when some Windows message is
+   posted to the input queue of the Emacs's main thread (which is the
+   thread in which sys_select runs).
+
+   When the Emacs's console window has focus, Windows sends input
+   events that originate from the keyboard or the mouse; these events
+   wake up MsgWaitForMultipleObjects, which reports that input is
+   available.  Emacs then calls w32_console_read_socket, below, to
+   read the input.  w32_console_read_socket uses
+   GetNumberOfConsoleInputEvents and ReadConsoleInput to peek at and
+   read the console input events.
+
+   One type of non-keyboard input event that gets reported as input
+   available is due to the Emacs's console window receiving focus.
+   When that happens, Emacs gets the FOCUS_EVENT event and sys_select
+   reports some input; however, w32_console_read_socket ignores such
+   events when called to read them.
+
+   Note that any other Windows message sent to the main thread will
+   also wake up MsgWaitForMultipleObjects.  These messages get
+   immediately dispatched to their destinations by calling
+   drain_message_queue.  */
+
 int
 w32_console_read_socket (struct terminal *terminal,
-                         int expected,
                          struct input_event *hold_quit)
 {
-  BOOL no_events = TRUE;
-  int nev, ret = 0, add;
+  int nev, add;
   int isdead;
 
-  if (interrupt_input_blocked)
-    {
-      interrupt_input_pending = 1;
-      return -1;
-    }
-
-  interrupt_input_pending = 0;
-  BLOCK_INPUT;
+  block_input ();
 
   for (;;)
     {
@@ -703,8 +625,7 @@ w32_console_read_socket (struct terminal *terminal,
          /* If nev == -1, there was some kind of error
             If nev == 0 then waitp must be zero and no events were available
             so return.  */
-         UNBLOCK_INPUT;
-         return nev;
+         break;
         }
 
       while (nev > 0)
@@ -748,9 +669,6 @@ w32_console_read_socket (struct terminal *terminal,
          queue_ptr++;
          nev--;
         }
-
-      if (ret > 0 || expected == 0)
-       break;
     }
 
   /* We don't get told about changes in the window size (only the buffer
@@ -759,7 +677,6 @@ w32_console_read_socket (struct terminal *terminal,
   if (!w32_use_full_screen_buffer)
     maybe_generate_resize_event ();
 
-  UNBLOCK_INPUT;
-  return ret;
+  unblock_input ();
+  return nev;
 }
-