From d3d548c89b2c20e14dae5d3598bdd55da436d988 Mon Sep 17 00:00:00 2001
From: Werner Almesberger <werner@almesberger.net>
Date: Tue, 10 Jan 2017 07:40:59 -0300
Subject: [PATCH] experimental support for showing text window on alternate X
 display

Example:

DISPLAY_TEXTWND=:0.1 solvespace

Known issue: text window may close immediately after appearing.
In the test setup, toggling its visibility (with Tab) a number of
times would eventually make it stay.
---
 src/platform/gtkmain.cpp | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/src/platform/gtkmain.cpp b/src/platform/gtkmain.cpp
index 079fd39..0171158 100644
--- a/src/platform/gtkmain.cpp
+++ b/src/platform/gtkmain.cpp
@@ -1128,7 +1128,7 @@ public:
         Glib::RefPtr<Gdk::Window> gdkwin = get_window();
         if(gdkwin) { // returns NULL if not realized
             Gdk::CursorType type = is_hand ? Gdk::HAND1 : Gdk::ARROW;
-            gdkwin->set_cursor(Gdk::Cursor::create(type));
+	    gdkwin->set_cursor(Gdk::Cursor::create(get_display(), type));
         }
     }
 
@@ -1180,8 +1180,37 @@ private:
 
 class TextWindowGtk : public Gtk::Window {
 public:
+    Glib::RefPtr<Gdk::Display> alt_display;
+
     TextWindowGtk() : _scrollbar(), _widget(_scrollbar.get_adjustment()),
                       _overlay(_widget), _box() {
+	const char *display_textwnd = getenv("DISPLAY_TEXTWND");
+
+	/*
+	 * If DISPLAY_TEXTWND is set, we interpret it as an X11 DISPLAY
+	 * variable to use for the text window, e.g.,
+	 * DISPLAY_TEXTWND=host:0 solvespace ...
+	 * If opening that display fails, we just proceed using the same
+	 * display for TW and GW.
+	 *
+	 * Known issue: in the setup where this was tested, the text window
+	 * would sometimes only appear for an instant, then vanish. Repeatedly
+	 * toggling it with Tab would eventually make it stay.
+	 *
+	 * The reason for this failure to appear properly is unknown and could
+	 * be a rare race condition, perhaps in Gdk/Gtk. In any case, it
+	 * doesn't seem to cause any other issues.
+	 */
+	if (display_textwnd) {
+		alt_display = Gdk::Display::open(display_textwnd);
+
+		if (alt_display) {
+			set_screen(alt_display->get_default_screen());
+		} else {
+			fprintf(stderr, "cannot open %s\n", display_textwnd);
+		}
+	}
+
         set_type_hint(Gdk::WINDOW_TYPE_HINT_UTILITY);
         set_skip_taskbar_hint(true);
         set_skip_pager_hint(true);
@@ -1431,7 +1460,9 @@ int main(int argc, char** argv) {
 
     TW.reset(new TextWindowGtk);
     GW.reset(new GraphicsWindowGtk);
-    TW->set_transient_for(*GW);
+    if (!TW->alt_display) {
+	TW->set_transient_for(*GW);
+    }
     GW->set_icon(icon_gdk);
     TW->set_icon(icon_gdk);
 
-- 
2.9.3

