use dynwind_begin and dynwind_end
[bpt/emacs.git] / src / xselect.c
index 3a16d7d..8ff783c 100644 (file)
@@ -1,5 +1,5 @@
 /* X Selection processing for Emacs.
-   Copyright (C) 1993-1997, 2000-2013 Free Software Foundation, Inc.
+   Copyright (C) 1993-1997, 2000-2014 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -318,9 +318,9 @@ x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
 {
   struct frame *f = XFRAME (frame);
   Window selecting_window = FRAME_X_WINDOW (f);
-  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
+  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
   Display *display = dpyinfo->display;
-  Time timestamp = last_event_timestamp;
+  Time timestamp = dpyinfo->last_user_time;
   Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name);
 
   block_input ();
@@ -390,7 +390,7 @@ x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
       /* Don't allow a quit within the converter.
         When the user types C-g, he would be surprised
         if by luck it came during a converter.  */
-      ptrdiff_t count = SPECPDL_INDEX ();
+      dynwind_begin ();
       specbind (Qinhibit_quit, Qt);
 
       CHECK_SYMBOL (target_type);
@@ -404,7 +404,7 @@ x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
                       XCAR (XCDR (local_value)));
       else
        value = Qnil;
-      unbind_to (count, Qnil);
+      dynwind_end ();
     }
 
   /* Make sure this value is of a type that we could transmit
@@ -570,7 +570,7 @@ x_reply_selection_request (struct input_event *event,
   Window window = SELECTION_EVENT_REQUESTOR (event);
   ptrdiff_t bytes_remaining;
   int max_bytes = selection_quantum (display);
-  ptrdiff_t count = SPECPDL_INDEX ();
+  dynwind_begin ();
   struct selection_data *cs;
 
   reply->type = SelectionNotify;
@@ -737,7 +737,7 @@ x_reply_selection_request (struct input_event *event,
      and then BLOCK again because x_uncatch_errors requires it.  */
   block_input ();
   /* This calls x_uncatch_errors.  */
-  unbind_to (count, Qnil);
+  dynwind_end ();
   unblock_input ();
 }
 \f
@@ -759,7 +759,7 @@ x_handle_selection_request (struct input_event *event)
   Atom property = SELECTION_EVENT_PROPERTY (event);
   Lisp_Object local_selection_data;
   int success = 0;
-  ptrdiff_t count = SPECPDL_INDEX ();
+  dynwind_begin ();
   GCPRO2 (local_selection_data, target_symbol);
 
   if (!dpyinfo) goto DONE;
@@ -849,7 +849,7 @@ x_handle_selection_request (struct input_event *event)
       Frun_hook_with_args (4, args);
     }
 
-  unbind_to (count, Qnil);
+  dynwind_end ();
   UNGCPRO;
 }
 
@@ -972,7 +972,6 @@ x_handle_selection_clear (struct input_event *event)
     Frun_hook_with_args (2, args);
   }
 
-  prepare_menu_bars ();
   redisplay_preserve_echo_area (20);
 }
 
@@ -997,7 +996,7 @@ x_clear_frame_selections (struct frame *f)
 {
   Lisp_Object frame;
   Lisp_Object rest;
-  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
+  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
   struct terminal *t = dpyinfo->terminal;
 
   XSETFRAME (frame, f);
@@ -1106,7 +1105,7 @@ wait_for_property_change_unwind (void *loc)
 static void
 wait_for_property_change (struct prop_location *location)
 {
-  ptrdiff_t count = SPECPDL_INDEX ();
+  dynwind_begin ();
 
   if (property_change_reply_object)
     emacs_abort ();
@@ -1135,13 +1134,13 @@ wait_for_property_change (struct prop_location *location)
        }
     }
 
-  unbind_to (count, Qnil);
+  dynwind_end ();
 }
 
 /* Called from XTread_socket in response to a PropertyNotify event.  */
 
 void
-x_handle_property_notify (XPropertyEvent *event)
+x_handle_property_notify (const XPropertyEvent *event)
 {
   struct prop_location *rest;
 
@@ -1185,10 +1184,10 @@ x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
                         Lisp_Object time_stamp, Lisp_Object frame)
 {
   struct frame *f = XFRAME (frame);
-  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
+  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
   Display *display = dpyinfo->display;
   Window requestor_window = FRAME_X_WINDOW (f);
-  Time requestor_time = last_event_timestamp;
+  Time requestor_time = dpyinfo->last_user_time;
   Atom target_property = dpyinfo->Xatom_EMACS_TMP;
   Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol);
   Atom type_atom = (CONSP (target_type)
@@ -1300,9 +1299,7 @@ x_get_window_property (Display *display, Window window, Atom property,
   if (total_size_max < bytes_remaining)
     goto size_overflow;
   total_size = bytes_remaining;
-  data = malloc (total_size + 1);
-  if (! data)
-    goto memory_exhausted;
+  data = xmalloc_atomic (total_size + 1);
 
   /* Now read, until we've gotten it all.  */
   while (bytes_remaining)
@@ -1348,15 +1345,11 @@ x_get_window_property (Display *display, Window window, Atom property,
 
       if (total_size - offset < bytes_gotten)
        {
-         unsigned char *data1;
          ptrdiff_t remaining_lim = total_size_max - offset - bytes_gotten;
          if (remaining_lim < 0 || remaining_lim < bytes_remaining)
            goto size_overflow;
          total_size = offset + bytes_gotten + bytes_remaining;
-         data1 = realloc (data, total_size + 1);
-         if (! data1)
-           goto memory_exhausted;
-         data = data1;
+         data = xrealloc (data, total_size + 1);
        }
 
       if (BITS_PER_LONG > 32 && *actual_format_ret == 32)
@@ -1387,14 +1380,9 @@ x_get_window_property (Display *display, Window window, Atom property,
   return;
 
  size_overflow:
-  free (data);
+  xfree (data);
   unblock_input ();
   memory_full (SIZE_MAX);
-
- memory_exhausted:
-  free (data);
-  unblock_input ();
-  memory_full (total_size + 1);
 }
 \f
 /* Use xfree, not XFree, to free the data obtained with this function.  */
@@ -1412,7 +1400,7 @@ receive_incremental_selection (Display *display, Window window, Atom property,
   struct prop_location *wait_object;
   if (min (PTRDIFF_MAX, SIZE_MAX) < min_size_bytes)
     memory_full (SIZE_MAX);
-  *data_ret = xmalloc (min_size_bytes);
+  *data_ret = xmalloc_atomic (min_size_bytes);
   *size_bytes_ret = min_size_bytes;
 
   TRACE1 ("Read %u bytes incrementally", min_size_bytes);
@@ -1888,7 +1876,7 @@ clean_local_selection_data (Lisp_Object obj)
    We store t there if the reply is successful, lambda if not.  */
 
 void
-x_handle_selection_notify (XSelectionEvent *event)
+x_handle_selection_notify (const XSelectionEvent *event)
 {
   if (event->requestor != reading_selection_window)
     return;
@@ -2009,14 +1997,13 @@ On Nextstep, TIME-STAMP and TERMINAL are unused.  */)
     error ("X selection unavailable for this frame");
 
   val = x_get_local_selection (selection_symbol, target_type, 1,
-                              FRAME_X_DISPLAY_INFO (f));
+                              FRAME_DISPLAY_INFO (f));
 
   if (NILP (val) && FRAME_LIVE_P (f))
     {
       Lisp_Object frame;
       XSETFRAME (frame, f);
-      RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol, target_type,
-                                              time_stamp, frame));
+      return x_get_foreign_selection (selection_symbol, target_type, time_stamp, frame);
     }
 
   if (CONSP (val) && SYMBOLP (XCAR (val)))
@@ -2025,7 +2012,7 @@ On Nextstep, TIME-STAMP and TERMINAL are unused.  */)
       if (CONSP (val) && NILP (XCDR (val)))
        val = XCAR (val);
     }
-  RETURN_UNGCPRO (clean_local_selection_data (val));
+  return clean_local_selection_data (val);
 }
 
 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
@@ -2056,7 +2043,7 @@ On MS-DOS, all this does is return non-nil if we own the selection.  */)
   if (!f)
     return Qnil;
 
-  dpyinfo = FRAME_X_DISPLAY_INFO (f);
+  dpyinfo = FRAME_DISPLAY_INFO (f);
   CHECK_SYMBOL (selection);
 
   /* Don't disown the selection when we're not the owner.  */
@@ -2067,7 +2054,7 @@ On MS-DOS, all this does is return non-nil if we own the selection.  */)
 
   block_input ();
   if (NILP (time_object))
-    timestamp = last_event_timestamp;
+    timestamp = dpyinfo->last_user_time;
   else
     CONS_TO_INTEGER (time_object, Time, timestamp);
   XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
@@ -2108,7 +2095,7 @@ On Nextstep, TERMINAL is unused.  */)
   if (EQ (selection, Qnil)) selection = QPRIMARY;
   if (EQ (selection, Qt)) selection = QSECONDARY;
 
-  if (f && !NILP (LOCAL_SELECTION (selection, FRAME_X_DISPLAY_INFO (f))))
+  if (f && !NILP (LOCAL_SELECTION (selection, FRAME_DISPLAY_INFO (f))))
     return Qt;
   else
     return Qnil;
@@ -2141,7 +2128,7 @@ On Nextstep, TERMINAL is unused.  */)
   if (!f)
     return Qnil;
 
-  dpyinfo = FRAME_X_DISPLAY_INFO (f);
+  dpyinfo = FRAME_DISPLAY_INFO (f);
 
   if (!NILP (LOCAL_SELECTION (selection, dpyinfo)))
     return Qt;
@@ -2162,7 +2149,7 @@ static Lisp_Object
 x_clipboard_manager_save (Lisp_Object frame)
 {
   struct frame *f = XFRAME (frame);
-  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
+  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
   Atom data = dpyinfo->Xatom_UTF8_STRING;
 
   XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
@@ -2212,7 +2199,7 @@ x_clipboard_manager_save_frame (Lisp_Object frame)
       && (f = XFRAME (frame), FRAME_X_P (f))
       && FRAME_LIVE_P (f))
     {
-      struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
+      struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
       Lisp_Object local_selection
        = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
 
@@ -2365,7 +2352,7 @@ x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
 
 Lisp_Object
 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
-                        Atom type, int format, long unsigned int size)
+                        Atom type, int format, unsigned long size)
 {
   ptrdiff_t format_bytes = format >> 3;
   if (PTRDIFF_MAX / format_bytes < size)
@@ -2374,43 +2361,6 @@ x_property_data_to_lisp (struct frame *f, const unsigned char *data,
                                      data, size * format_bytes, type, format);
 }
 
-/* Get the mouse position in frame relative coordinates.  */
-
-static void
-mouse_position_for_drop (struct frame *f, int *x, int *y)
-{
-  Window root, dummy_window;
-  int dummy;
-
-  block_input ();
-
-  XQueryPointer (FRAME_X_DISPLAY (f),
-                 DefaultRootWindow (FRAME_X_DISPLAY (f)),
-
-                 /* The root window which contains the pointer.  */
-                 &root,
-
-                 /* Window pointer is on, not used  */
-                 &dummy_window,
-
-                 /* The position on that root window.  */
-                 x, y,
-
-                 /* x/y in dummy_window coordinates, not used.  */
-                 &dummy, &dummy,
-
-                 /* Modifier keys and pointer buttons, about which
-                    we don't care.  */
-                 (unsigned int *) &dummy);
-
-
-  /* Absolute to relative.  */
-  *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
-  *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
-
-  unblock_input ();
-}
-
 DEFUN ("x-get-atom-name", Fx_get_atom_name,
        Sx_get_atom_name, 1, 2, 0,
        doc: /* Return the X atom name for VALUE as a string.
@@ -2458,7 +2408,7 @@ FRAME is on.  If FRAME is nil, the selected frame is used.  */)
   Atom x_atom;
   struct frame *f = decode_window_system_frame (frame);
   ptrdiff_t i;
-  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
+  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
 
 
   if (SYMBOLP (atom))
@@ -2488,7 +2438,7 @@ FRAME is on.  If FRAME is nil, the selected frame is used.  */)
 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT.  */
 
 int
-x_handle_dnd_message (struct frame *f, XClientMessageEvent *event,
+x_handle_dnd_message (struct frame *f, const XClientMessageEvent *event,
                       struct x_display_info *dpyinfo, struct input_event *bufp)
 {
   Lisp_Object vec;
@@ -2530,7 +2480,7 @@ x_handle_dnd_message (struct frame *f, XClientMessageEvent *event,
                                         event->format,
                                         size));
 
-  mouse_position_for_drop (f, &x, &y);
+  x_relative_mouse_position (f, &x, &y);
   bufp->kind = DRAG_N_DROP_EVENT;
   bufp->frame_or_window = frame;
   bufp->timestamp = CurrentTime;
@@ -2628,6 +2578,8 @@ x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
 
   block_input ();
 
+  event.xclient.send_event = True;
+  event.xclient.serial = 0;
   event.xclient.message_type = message_type;
   event.xclient.display = dpyinfo->display;
 
@@ -2635,19 +2587,19 @@ x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
      when sending to the root window.  */
   event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
 
-
-  memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b));
+  memset (event.xclient.data.l, 0, sizeof (event.xclient.data.l));
   x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
                         event.xclient.format);
 
   /* If event mask is 0 the event is sent to the client that created
      the destination window.  But if we are sending to the root window,
-     there is no such client.  Then we set the event mask to 0xffff.  The
+     there is no such client.  Then we set the event mask to 0xffffff.  The
      event then goes to clients selecting for events on the root window.  */
   x_catch_errors (dpyinfo->display);
   {
     int propagate = to_root ? False : True;
-    unsigned mask = to_root ? 0xffff : 0;
+    long mask = to_root ? 0xffffff : 0;
+
     XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
     XFlush (dpyinfo->display);
   }
@@ -2659,15 +2611,7 @@ x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
 void
 syms_of_xselect (void)
 {
-  defsubr (&Sx_get_selection_internal);
-  defsubr (&Sx_own_selection_internal);
-  defsubr (&Sx_disown_selection_internal);
-  defsubr (&Sx_selection_owner_p);
-  defsubr (&Sx_selection_exists_p);
-
-  defsubr (&Sx_get_atom_name);
-  defsubr (&Sx_send_client_message);
-  defsubr (&Sx_register_dnd_atom);
+#include "xselect.x"
 
   reading_selection_reply = Fcons (Qnil, Qnil);
   staticpro (&reading_selection_reply);