From 5e200188e783123842b45070a15096262e96f604 Mon Sep 17 00:00:00 2001
From: fvogel <fvogelnew1@free.fr>
Date: Wed, 17 Jan 2018 07:18:37 +0000
Subject: [PATCH 115/693] More complete patch from Christian Werner to fix
 [1821174fff] and [1938774fff]

---
 unix/tkUnixColor.c  |  1 +
 unix/tkUnixEmbed.c  |  3 +++
 unix/tkUnixFocus.c  |  1 +
 unix/tkUnixRFont.c  | 12 +++---------
 unix/tkUnixSelect.c |  3 +++
 unix/tkUnixSend.c   |  1 +
 unix/tkUnixWm.c     |  9 +++++++--
 7 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/unix/tkUnixColor.c b/unix/tkUnixColor.c
index 43500ad5b..b11a1ebbc 100644
--- a/unix/tkUnixColor.c
+++ b/unix/tkUnixColor.c
@@ -94,6 +94,7 @@ TkpFreeColor(
 		-1, -1, -1, NULL, NULL);
 	XFreeColors(DisplayOfScreen(screen), tkColPtr->colormap,
 		&tkColPtr->color.pixel, 1, 0L);
+        XSync(DisplayOfScreen(screen), False);
 	Tk_DeleteErrorHandler(handler);
     }
     DeleteStressedCmap(DisplayOfScreen(screen), tkColPtr->colormap);
diff --git a/unix/tkUnixEmbed.c b/unix/tkUnixEmbed.c
index b170ad037..b285fbe73 100644
--- a/unix/tkUnixEmbed.c
+++ b/unix/tkUnixEmbed.c
@@ -472,6 +472,7 @@ ContainerEventProc(
 
 	Tk_DestroyWindow((Tk_Window) winPtr);
     }
+    XSync(eventPtr->xfocus.display, False);
     Tk_DeleteErrorHandler(errHandler);
 }
 
@@ -515,6 +516,7 @@ EmbedStructureProc(
 		    containerPtr->wrapper, 0, 0,
 		    (unsigned) Tk_Width((Tk_Window) containerPtr->parentPtr),
 		    (unsigned) Tk_Height((Tk_Window) containerPtr->parentPtr));
+            XSync(eventPtr->xfocus.display, False);
 	    Tk_DeleteErrorHandler(errHandler);
 	}
     } else if (eventPtr->type == DestroyNotify) {
@@ -564,6 +566,7 @@ EmbedFocusProc(
 		    -1, -1, NULL, NULL);
 	    XSetInputFocus(display, containerPtr->wrapper, RevertToParent,
 		    CurrentTime);
+            XSync(eventPtr->xfocus.display, False);
 	    Tk_DeleteErrorHandler(errHandler);
 	}
     }
diff --git a/unix/tkUnixFocus.c b/unix/tkUnixFocus.c
index 07676181d..7f4771e9d 100644
--- a/unix/tkUnixFocus.c
+++ b/unix/tkUnixFocus.c
@@ -112,6 +112,7 @@ TkpChangeFocus(
     }
     XSetInputFocus(dispPtr->display, winPtr->window, RevertToParent,
 	    CurrentTime);
+    XSync(dispPtr->display, False);
     Tk_DeleteErrorHandler(errHandler);
 
     /*
diff --git a/unix/tkUnixRFont.c b/unix/tkUnixRFont.c
index abdde7819..44e733944 100644
--- a/unix/tkUnixRFont.c
+++ b/unix/tkUnixRFont.c
@@ -388,16 +388,7 @@ FinishedWithFont(
     if (fontPtr->fontset) {
 	FcFontSetDestroy(fontPtr->fontset);
     }
-
-    /*
-     * Synchronize with X server before dropping the error handler.
-     * This seems to catch sporadic RenderBadPicture errors on tear
-     * down of an application.
-     *
-     * See bugs [1821174fff] and [1938774fff].
-     */
     XSync(fontPtr->display, False);
-
     Tk_DeleteErrorHandler(handler);
 }
 
@@ -885,6 +876,7 @@ Tk_DrawChars(
 		Tk_CreateErrorHandler(display, -1, -1, -1, NULL, NULL);
 
 	XftDrawChange(fontPtr->ftDraw, drawable);
+        XSync(display, False);
 	Tk_DeleteErrorHandler(handler);
     }
     XGetGCValues(display, gc, GCForeground, &values);
@@ -1018,6 +1010,7 @@ TkDrawAngledChars(
 		Tk_CreateErrorHandler(display, -1, -1, -1, NULL, NULL);
 
 	XftDrawChange(fontPtr->ftDraw, drawable);
+        XSync(display, False);
 	Tk_DeleteErrorHandler(handler);
     }
 
@@ -1107,6 +1100,7 @@ TkDrawAngledChars(
 		Tk_CreateErrorHandler(display, -1, -1, -1, NULL, NULL);
 
 	XftDrawChange(fontPtr->ftDraw, drawable);
+        XSync(display, False);
 	Tk_DeleteErrorHandler(handler);
     }
     XGetGCValues(display, gc, GCForeground, &values);
diff --git a/unix/tkUnixSelect.c b/unix/tkUnixSelect.c
index 6d379ecc8..9313df5c0 100644
--- a/unix/tkUnixSelect.c
+++ b/unix/tkUnixSelect.c
@@ -470,6 +470,7 @@ TkSelPropProc(
 		    ckfree(propPtr);
 		}
 	    }
+            XSync(eventPtr->xproperty.display, False);
 	    Tk_DeleteErrorHandler(errorHandler);
 
 	    /*
@@ -1048,6 +1049,7 @@ ConvertSelection(
 	reply.xsel.property = incr.multAtoms[1];
     }
     XSendEvent(reply.xsel.display, reply.xsel.requestor, False, 0, &reply.ev);
+    XSync(reply.xsel.display, False);
     Tk_DeleteErrorHandler(errorHandler);
 
     /*
@@ -1066,6 +1068,7 @@ ConvertSelection(
 	errorHandler = Tk_CreateErrorHandler(winPtr->display,
 		-1, -1, -1, (int (*)()) NULL, NULL);
 	XSelectInput(reply.xsel.display, reply.xsel.requestor, 0L);
+        XSync(winPtr->display, False);
 	Tk_DeleteErrorHandler(errorHandler);
 	if (tsdPtr->pendingIncrs == &incr) {
 	    tsdPtr->pendingIncrs = incr.nextPtr;
diff --git a/unix/tkUnixSend.c b/unix/tkUnixSend.c
index bbbdd774c..0322a78d5 100644
--- a/unix/tkUnixSend.c
+++ b/unix/tkUnixSend.c
@@ -1748,6 +1748,7 @@ AppendPropCarefully(
 	    pendingPtr);
     XChangeProperty(display, window, property, XA_STRING, 8,
 	    PropModeAppend, (unsigned char *) value, length);
+    XSync(display, False);
     Tk_DeleteErrorHandler(handler);
 }
 
diff --git a/unix/tkUnixWm.c b/unix/tkUnixWm.c
index 19ac86cb5..31e14cb72 100644
--- a/unix/tkUnixWm.c
+++ b/unix/tkUnixWm.c
@@ -4084,6 +4084,7 @@ ConfigureEvent(
     XMoveResizeWindow(winPtr->display, winPtr->window, 0,
 	    wmPtr->menuHeight, (unsigned) wrapperPtr->changes.width,
 	    (unsigned) (wrapperPtr->changes.height - wmPtr->menuHeight));
+    XSync(winPtr->display, False);
     Tk_DeleteErrorHandler(handler);
     if ((wmPtr->menubar != NULL)
 	    && ((Tk_Width(wmPtr->menubar) != wrapperPtr->changes.width)
@@ -4420,10 +4421,11 @@ WrapperEventProc(
 	     * Tk_DestroyWindow will try to destroy the window, but of course
 	     * it's already gone.
 	     */
+            Display *display = wmPtr->winPtr->display;
 
-	    handler = Tk_CreateErrorHandler(wmPtr->winPtr->display, -1, -1, -1,
-		    NULL, NULL);
+	    handler = Tk_CreateErrorHandler(display, -1, -1, -1, NULL, NULL);
 	    Tk_DestroyWindow((Tk_Window) wmPtr->winPtr);
+            XSync(display, False);
 	    Tk_DeleteErrorHandler(handler);
 	}
 	if (dispPtr->flags & TK_DISPLAY_WM_TRACING) {
@@ -5846,10 +5848,12 @@ Tk_CoordsToWindow(
 	     * deleted
 	     */
 
+            XSync(Tk_Display(tkwin), False);
 	    Tk_DeleteErrorHandler(handler);
 	    return NULL;
 	}
 	if (child == None) {
+            XSync(Tk_Display(tkwin), False);
 	    Tk_DeleteErrorHandler(handler);
 	    return NULL;
 	}
@@ -5879,6 +5883,7 @@ Tk_CoordsToWindow(
 	 * or below
 	 */
 
+        XSync(Tk_Display(tkwin), False);
 	Tk_DeleteErrorHandler(handler);
 	handler = NULL;
     }
-- 
2.19.1

