Fix non-ASCII input in non-GUI frames on MS-Windows. (Bug#12055)
[bpt/emacs.git] / src / w32inevt.c
index c4858de..8d04119 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.
+   Copyright (C) 1992-1993, 1995, 2001-2012  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -35,17 +35,20 @@ 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 w32console.c */
 extern HANDLE keyboard_handle;
 
 /* Info for last mouse motion */
 static COORD movement_pos;
-static DWORD movement_time;
+static Time movement_time;
 
 /* from w32fns.c */
 extern unsigned int map_keypad_keys (unsigned int, unsigned int);
@@ -59,6 +62,15 @@ 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));
+}
+
 static int
 fill_queue (BOOL block)
 {
@@ -78,8 +90,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;
@@ -222,7 +234,7 @@ w32_kbd_patch_key (KEY_EVENT_RECORD *event)
 #endif
 
   /* On NT, call ToUnicode instead and then convert to the current
-     locale's default codepage.  */
+     console input codepage.  */
   if (os_subtype == OS_NT)
     {
       WCHAR buf[128];
@@ -231,14 +243,9 @@ w32_kbd_patch_key (KEY_EVENT_RECORD *event)
                          keystate, buf, 128, 0);
       if (isdead > 0)
        {
-         char cp[20];
-         int cpId;
+         int cpId = GetConsoleCP ();
 
          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);
        }
@@ -445,26 +452,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;
@@ -499,6 +514,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;
     }
@@ -544,7 +560,7 @@ 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;
 
@@ -566,7 +582,7 @@ w32_console_mouse_position (FRAME_PTR *f,
 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;
@@ -599,14 +615,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,7 +742,6 @@ w32_console_read_socket (struct terminal *terminal,
                          int expected,
                          struct input_event *hold_quit)
 {
-  BOOL no_events = TRUE;
   int nev, ret = 0, add;
   int isdead;
 
@@ -757,4 +821,3 @@ w32_console_read_socket (struct terminal *terminal,
   UNBLOCK_INPUT;
   return ret;
 }
-