From 8f182cd74b2e1217624ed80de8f62220c09dad2e Mon Sep 17 00:00:00 2001
From: Ulrich Sibiller <uli42@gmx.de>
Date: Wed, 25 Nov 2020 00:29:37 +0100
Subject: Clipboard.c: introduce replyPendingRequestSelectionToXServer helper

---
 nx-X11/programs/Xserver/hw/nxagent/Clipboard.c | 70 ++++++++++++++------------
 1 file changed, 39 insertions(+), 31 deletions(-)

(limited to 'nx-X11/programs/Xserver/hw/nxagent')

diff --git a/nx-X11/programs/Xserver/hw/nxagent/Clipboard.c b/nx-X11/programs/Xserver/hw/nxagent/Clipboard.c
index 7598f9916..6e2337a4d 100644
--- a/nx-X11/programs/Xserver/hw/nxagent/Clipboard.c
+++ b/nx-X11/programs/Xserver/hw/nxagent/Clipboard.c
@@ -320,6 +320,7 @@ static void sendSelectionNotifyEventToClient(ClientPtr client,
                                              Atom target,
                                              Atom property);
 static Status sendSelectionNotifyEventToXServer(XSelectionEvent *event_to_send);
+static void replyPendingRequestSelectionToXServer(int index, Bool success);
 #ifdef DEBUG
 static void printSelectionStat(int sel);
 #endif
@@ -1898,24 +1899,45 @@ void nxagentHandleSelectionNotifyFromXServer(XEvent *X)
        * effectively will send a "Request denied" to the initial
        * requestor.
        */
-      XSelectionEvent eventSelection = {
-        .requestor = lastServers[index].requestor,
-        .selection = X->xselection.selection,
-        /* .target = X->xselection.target, */
-        .target    = lastServers[index].target,
-        .property  = lastServers[index].property,
-        .time      = lastServers[index].time,
-        /* .time   = CurrentTime */
-      };
-      #ifdef DEBUG
-      fprintf(stderr, "%s: Sending SelectionNotify event to requestor [%p].\n", __func__,
-              (void *)eventSelection.requestor);
-      #endif
+      replyPendingRequestSelectionToXServer(index, True);
+    }
+  }
+}
 
-      sendSelectionNotifyEventToXServer(&eventSelection);
+/*
+ * This is similar to replyRequestSelectionToXServer(), but gets the
+ * required values from a stored request instead of an XEvent
+ * structure.
+ */
+void replyPendingRequestSelectionToXServer(int index, Bool success)
+{
+  if (lastServers[index].requestor == None)
+  {
+    #ifdef DEBUG
+    fprintf(stderr, "%s: no pending request for index [%d] - doing nothing\n", __func__, index);
+    #endif
+  }
+  else
+  {
+    XSelectionEvent eventSelection = {
+      .requestor = lastServers[index].requestor,
+      .selection = remSelAtoms[index],
+      .target    = lastServers[index].target,
+      .time      = lastServers[index].time,
+      .property  = success ? lastServers[index].property : None,
+    };
 
-      lastServers[index].requestor = None; /* allow further request */
-    }
+    #ifdef DEBUG
+    fprintf(stderr, "%s: Sending %s SelectionNotify event to requestor [%p].\n", __func__,
+                success ? "positive" : "negative", (void *)eventSelection.requestor);
+    #endif
+
+    sendSelectionNotifyEventToXServer(&eventSelection);
+
+    lastServers[index].requestor = None; /* allow further request */
+    lastServers[index].property = 0;
+    lastServers[index].target = 0;
+    lastServers[index].time = 0;
   }
 }
 
@@ -2703,21 +2725,7 @@ int nxagentSendNotificationToSelfViaXServer(xEvent *event)
      */
     if (lastServers[index].requestor != None && event->u.selectionNotify.property == 0)
     {
-      #ifdef DEBUG
-      fprintf(stderr, "%s: passing on failure to lastServers[%d].requestor [%ld].\n", __func__,
-                  index, lastServers[index].requestor);
-      #endif
-
-      XSelectionEvent eventSelection = {
-        .requestor = lastServers[index].requestor,
-        .selection = remSelAtoms[index],
-        .target    = lastServers[index].target,
-        .property  = 0,
-        .time      = lastServers[index].time,
-      };
-      sendSelectionNotifyEventToXServer(&eventSelection);
-
-      lastServers[index].requestor = None;
+      replyPendingRequestSelectionToXServer(index, False);
       return 1;
     }
     else
-- 
cgit v1.2.3