]> code.delx.au - gnu-emacs/blobdiff - src/w32inevt.c
Minor tweaks of copying text properties when padding strings
[gnu-emacs] / src / w32inevt.c
index 88a3f9739cdaee825f69363454efe377cdd612e7..2269d318051c9819725eac2b8cfa768dea0f33f9 100644 (file)
@@ -1,13 +1,13 @@
 /* Input event support for Emacs on the Microsoft Windows API.
-   Copyright (C) 1992-1993, 1995, 2001-2013 Free Software Foundation,
+   Copyright (C) 1992-1993, 1995, 2001-2016 Free Software Foundation,
    Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, either version 3 of the License, or (at
+your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -30,18 +30,18 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifndef MOUSE_MOVED
 #define MOUSE_MOVED   1
 #endif
+#ifndef MOUSE_HWHEELED
+#define MOUSE_HWHEELED 8
+#endif
 
 #include "lisp.h"
 #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 "termchar.h"  /* for Mouse_HLInfo, tty_display_info */
 #include "w32term.h"
 #include "w32inevt.h"
+#include "w32common.h"
 
 /* stdin, from w32console.c */
 extern HANDLE keyboard_handle;
@@ -103,10 +103,10 @@ fill_queue (BOOL block)
 }
 
 /* In a generic, multi-frame world this should take a console handle
-   and return the frame for it
+   and return the frame for it.
 
    Right now, there's only one frame so return it.  */
-static FRAME_PTR
+static struct frame *
 get_frame (void)
 {
   return SELECTED_FRAME ();
@@ -149,10 +149,12 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
       switch (event->wVirtualKeyCode)
        {
        case VK_LWIN:
-         mod_key_state &= ~LEFT_WIN_PRESSED;
+          if (!w32_kbdhook_active)
+            mod_key_state &= ~LEFT_WIN_PRESSED;
          break;
        case VK_RWIN:
-         mod_key_state &= ~RIGHT_WIN_PRESSED;
+          if (!w32_kbdhook_active)
+            mod_key_state &= ~RIGHT_WIN_PRESSED;
          break;
        case VK_APPS:
          mod_key_state &= ~APPS_PRESSED;
@@ -186,7 +188,8 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
              keybd_event (faked_key, (BYTE) MapVirtualKey (faked_key, 0), 0, 0);
            }
        }
-      mod_key_state |= LEFT_WIN_PRESSED;
+      if (!w32_kbdhook_active)
+        mod_key_state |= LEFT_WIN_PRESSED;
       if (!NILP (Vw32_lwindow_modifier))
        return 0;
       break;
@@ -202,7 +205,8 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
              keybd_event (faked_key, (BYTE) MapVirtualKey (faked_key, 0), 0, 0);
            }
        }
-      mod_key_state |= RIGHT_WIN_PRESSED;
+      if (!w32_kbdhook_active)
+        mod_key_state |= RIGHT_WIN_PRESSED;
       if (!NILP (Vw32_rwindow_modifier))
        return 0;
       break;
@@ -268,6 +272,13 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
 
   /* Recognize state of Windows and Apps keys.  */
   event->dwControlKeyState |= mod_key_state;
+  if (w32_kbdhook_active)
+    {
+      if (check_w32_winkey_state (VK_LWIN))
+        event->dwControlKeyState |= LEFT_WIN_PRESSED;
+      if (check_w32_winkey_state (VK_RWIN))
+        event->dwControlKeyState |= RIGHT_WIN_PRESSED;
+    }
 
   /* Distinguish numeric keypad keys from extended keys.  */
   event->wVirtualKeyCode =
@@ -394,7 +405,7 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
 
 /* Mouse position hook.  */
 void
-w32_console_mouse_position (FRAME_PTR *f,
+w32_console_mouse_position (struct frame **f,
                            int insist,
                            Lisp_Object *bar_window,
                            enum scroll_bar_part *part,
@@ -408,7 +419,7 @@ w32_console_mouse_position (FRAME_PTR *f,
 
   *f = get_frame ();
   *bar_window = Qnil;
-  *part = 0;
+  *part = scroll_bar_above_handle;
   SELECTED_FRAME ()->mouse_moved = 0;
 
   XSETINT (*x, movement_pos.X);
@@ -438,16 +449,16 @@ mouse_moved_to (int x, int y)
      next - Leftmost+1
      next - Leftmost+2...
 
-   Assume emacs likes three button mice, so
+   For the 3 standard buttons, we have:
      Left == 0
      Middle == 1
      Right == 2
    Others increase from there.  */
 
-#define NUM_TRANSLATED_MOUSE_BUTTONS 3
+#define NUM_TRANSLATED_MOUSE_BUTTONS 5
 static int emacs_button_translation[NUM_TRANSLATED_MOUSE_BUTTONS] =
 {
-  0, 2, 1
+  0, 2, 1, 3, 4
 };
 
 static int
@@ -456,108 +467,136 @@ do_mouse_event (MOUSE_EVENT_RECORD *event,
 {
   static DWORD button_state = 0;
   static Lisp_Object last_mouse_window;
-  DWORD but_change, mask;
+  DWORD but_change, mask, flags = event->dwEventFlags;
   int i;
 
-  if (event->dwEventFlags == MOUSE_MOVED)
+  switch (flags)
     {
-      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;
-    }
-
-  /* It looks like the console code sends us a mouse event with
-     dwButtonState == 0 when a window is activated.  Ignore this case.  */
-  if (event->dwButtonState == button_state)
-    return 0;
-
-  emacs_ev->kind = MOUSE_CLICK_EVENT;
-
-  /* Find out what button has changed state since the last button event.  */
-  but_change = button_state ^ event->dwButtonState;
-  mask = 1;
-  for (i = 0; mask; i++, mask <<= 1)
-    if (but_change & mask)
+    case MOUSE_MOVED:
       {
-        if (i < NUM_TRANSLATED_MOUSE_BUTTONS)
-          emacs_ev->code = emacs_button_translation[i];
-        else
-          emacs_ev->code = i;
-       break;
+       struct frame *f = get_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);
+         }
+       /* We already called kbd_buffer_store_event, so indicate the
+          the caller it shouldn't.  */
+       return 0;
       }
+    case MOUSE_WHEELED:
+    case MOUSE_HWHEELED:
+      {
+       struct frame *f = get_frame ();
+       int mx = event->dwMousePosition.X, my = event->dwMousePosition.Y;
+       bool down_p = (event->dwButtonState & 0x10000000) != 0;
+
+       emacs_ev->kind =
+         flags == MOUSE_HWHEELED ? HORIZ_WHEEL_EVENT : WHEEL_EVENT;
+       emacs_ev->code = 0;
+       emacs_ev->modifiers = down_p ? down_modifier : up_modifier;
+       emacs_ev->modifiers |=
+         w32_kbd_mods_to_emacs (event->dwControlKeyState, 0);
+       XSETINT (emacs_ev->x, mx);
+       XSETINT (emacs_ev->y, my);
+       XSETFRAME (emacs_ev->frame_or_window, f);
+       emacs_ev->arg = Qnil;
+       emacs_ev->timestamp = GetTickCount ();
+       return 1;
+      }
+    case DOUBLE_CLICK:
+    default:   /* mouse pressed or released */
+      /* It looks like the console code sends us a button-release
+        mouse event with dwButtonState == 0 when a window is
+        activated and when the mouse is first clicked.  Ignore this
+        case.  */
+      if (event->dwButtonState == button_state)
+       return 0;
 
-  button_state = event->dwButtonState;
-  emacs_ev->timestamp = GetTickCount ();
-  emacs_ev->modifiers = w32_kbd_mods_to_emacs (event->dwControlKeyState, 0) |
-    ((event->dwButtonState & mask) ? down_modifier : up_modifier);
-
-  XSETFASTINT (emacs_ev->x, event->dwMousePosition.X);
-  XSETFASTINT (emacs_ev->y, event->dwMousePosition.Y);
-/* for Mule 2.2 (Based on Emacs 19.28 */
-#ifdef MULE
-  XSET (emacs_ev->frame_or_window, Lisp_Frame, get_frame ());
-#else
-  XSETFRAME (emacs_ev->frame_or_window, get_frame ());
-#endif
-
-  return 1;
+      emacs_ev->kind = MOUSE_CLICK_EVENT;
+
+      /* Find out what button has changed state since the last button
+        event.  */
+      but_change = button_state ^ event->dwButtonState;
+      mask = 1;
+      for (i = 0; mask; i++, mask <<= 1)
+       if (but_change & mask)
+         {
+           if (i < NUM_TRANSLATED_MOUSE_BUTTONS)
+             emacs_ev->code = emacs_button_translation[i];
+           else
+             emacs_ev->code = i;
+           break;
+         }
+
+      button_state = event->dwButtonState;
+      emacs_ev->modifiers =
+       w32_kbd_mods_to_emacs (event->dwControlKeyState, 0)
+       | ((event->dwButtonState & mask) ? down_modifier : up_modifier);
+
+      XSETFASTINT (emacs_ev->x, event->dwMousePosition.X);
+      XSETFASTINT (emacs_ev->y, event->dwMousePosition.Y);
+      XSETFRAME (emacs_ev->frame_or_window, get_frame ());
+      emacs_ev->arg = Qnil;
+      emacs_ev->timestamp = GetTickCount ();
+
+      return 1;
+    }
 }
 
 static void
 resize_event (WINDOW_BUFFER_SIZE_RECORD *event)
 {
-  FRAME_PTR f = get_frame ();
+  struct frame *f = get_frame ();
 
-  change_frame_size (f, event->dwSize.Y, event->dwSize.X, 0, 1, 0);
+  change_frame_size (f, event->dwSize.X, event->dwSize.Y
+                    - FRAME_MENU_BAR_LINES (f), 0, 1, 0, 0);
   SET_FRAME_GARBAGED (f);
 }
 
@@ -565,88 +604,109 @@ static void
 maybe_generate_resize_event (void)
 {
   CONSOLE_SCREEN_BUFFER_INFO info;
-  FRAME_PTR f = get_frame ();
+  struct frame *f = get_frame ();
 
   GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &info);
 
   /* It is okay to call this unconditionally, since it will do nothing
      if the size hasn't actually changed.  */
   change_frame_size (f,
-                    1 + info.srWindow.Bottom - info.srWindow.Top,
                     1 + info.srWindow.Right - info.srWindow.Left,
-                    0, 0, 0);
+                    1 + info.srWindow.Bottom - info.srWindow.Top
+                    - FRAME_MENU_BAR_LINES (f), 0, 1, 0, 0);
 }
 
 #if HAVE_W32NOTIFY
-static int
+int
 handle_file_notifications (struct input_event *hold_quit)
 {
-  BYTE *p = file_notifications;
-  FILE_NOTIFY_INFORMATION *fni = (PFILE_NOTIFY_INFORMATION)p;
-  const DWORD min_size
-    = offsetof (FILE_NOTIFY_INFORMATION, FileName) + sizeof(wchar_t);
-  struct input_event inev;
+  struct notifications_set *ns = NULL;
   int nevents = 0;
+  int done = 0;
 
   /* We cannot process notification before Emacs is fully initialized,
      since we need the UTF-16LE coding-system to be set up.  */
   if (!initialized)
     {
-      notification_buffer_in_use = 0;
       return nevents;
     }
 
-  enter_crit ();
-  if (notification_buffer_in_use)
+  while (!done)
     {
-      DWORD info_size = notifications_size;
-      Lisp_Object cs = intern ("utf-16le");
-      Lisp_Object obj = w32_get_watch_object (notifications_desc);
-
-      /* notifications_size could be zero when the buffer of
-        notifications overflowed on the OS level, or when the
-        directory being watched was itself deleted.  Do nothing in
-        that case.  */
-      if (info_size
-         && !NILP (obj) && CONSP (obj))
-       {
-         Lisp_Object callback = XCDR (obj);
+      ns = NULL;
 
-         EVENT_INIT (inev);
+      /* Find out if there is a record available in the linked list of
+        notifications sets.  If so, unlink te set from the linked list.
+        Use the critical section.  */
+      enter_crit ();
+      if (notifications_set_head->next != notifications_set_head)
+       {
+         ns = notifications_set_head->next;
+         ns->prev->next = ns->next;
+         ns->next->prev = ns->prev;
+       }
+      else
+       done = 1;
+      leave_crit();
 
-         while (info_size >= min_size)
+      if (ns)
+       {
+         BYTE *p = ns->notifications;
+         FILE_NOTIFY_INFORMATION *fni = (PFILE_NOTIFY_INFORMATION)p;
+         const DWORD min_size
+           = offsetof (FILE_NOTIFY_INFORMATION, FileName) + sizeof(wchar_t);
+         struct input_event inev;
+         DWORD info_size = ns->size;
+         Lisp_Object cs = Qutf_16le;
+         Lisp_Object obj = w32_get_watch_object (ns->desc);
+
+         /* notifications size could be zero when the buffer of
+            notifications overflowed on the OS level, or when the
+            directory being watched was itself deleted.  Do nothing in
+            that case.  */
+         if (info_size
+             && !NILP (obj) && CONSP (obj))
            {
-             Lisp_Object utf_16_fn
-               = make_unibyte_string ((char *)fni->FileName,
-                                      fni->FileNameLength);
-             /* Note: mule-conf is preloaded, so utf-16le must
-                already be defined at this point.  */
-             Lisp_Object fname
-               = code_convert_string_norecord (utf_16_fn, cs, 0);
-             Lisp_Object action = lispy_file_action (fni->Action);
-
-             inev.kind = FILE_NOTIFY_EVENT;
-             inev.code = (ptrdiff_t)XINT (XIL ((EMACS_INT)notifications_desc));
-             inev.timestamp = GetTickCount ();
-             inev.modifiers = 0;
-             inev.frame_or_window = callback;
-             inev.arg = Fcons (action, fname);
-             kbd_buffer_store_event_hold (&inev, hold_quit);
-
-             if (!fni->NextEntryOffset)
-               break;
-             p += fni->NextEntryOffset;
-             fni = (PFILE_NOTIFY_INFORMATION)p;
-             info_size -= fni->NextEntryOffset;
+             Lisp_Object callback = XCDR (obj);
+
+             EVENT_INIT (inev);
+
+             while (info_size >= min_size)
+               {
+                 Lisp_Object utf_16_fn
+                   = make_unibyte_string ((char *)fni->FileName,
+                                          fni->FileNameLength);
+                 /* Note: mule-conf is preloaded, so utf-16le must
+                    already be defined at this point.  */
+                 Lisp_Object fname
+                   = code_convert_string_norecord (utf_16_fn, cs, 0);
+                 Lisp_Object action = lispy_file_action (fni->Action);
+
+                 inev.kind = FILE_NOTIFY_EVENT;
+                 inev.timestamp = GetTickCount ();
+                 inev.modifiers = 0;
+                 inev.frame_or_window = callback;
+                 inev.arg = Fcons (action, fname);
+                 inev.arg = list3 (make_pointer_integer (ns->desc),
+                                   action, fname);
+                 kbd_buffer_store_event_hold (&inev, hold_quit);
+                 nevents++;
+                 if (!fni->NextEntryOffset)
+                   break;
+                 p += fni->NextEntryOffset;
+                 fni = (PFILE_NOTIFY_INFORMATION)p;
+                 info_size -= fni->NextEntryOffset;
+               }
            }
+         /* Free this notification set.  */
+         free (ns->notifications);
+         free (ns);
        }
-      notification_buffer_in_use = 0;
     }
-  leave_crit ();
   return nevents;
 }
 #else  /* !HAVE_W32NOTIFY */
-static int
+int
 handle_file_notifications (struct input_event *hold_quit)
 {
   return 0;
@@ -712,12 +772,17 @@ w32_console_read_socket (struct terminal *terminal,
       while (nev > 0)
         {
          struct input_event inev;
+         /* Having a separate variable with this value makes
+            debugging easier, as otherwise the compiler might
+            rearrange the switch below in a way that makes it hard to
+            track the event type.  */
+         unsigned evtype = queue_ptr->EventType;
 
          EVENT_INIT (inev);
          inev.kind = NO_EVENT;
          inev.arg = Qnil;
 
-         switch (queue_ptr->EventType)
+         switch (evtype)
             {
             case KEY_EVENT:
              add = key_event (&queue_ptr->Event.KeyEvent, &inev, &isdead);