From 2aceca35462c26243dfdf9c76963a03172fc7355 Mon Sep 17 00:00:00 2001
From: Augustin Cavalier <waddlesplash@gmail.com>
Date: Wed, 17 Nov 2021 17:42:17 -0500
Subject: Fixes for Haiku.


diff --git a/sdl/SdlTkX.c b/sdl/SdlTkX.c
index 4ac99ba..78f102d 100644
--- a/sdl/SdlTkX.c
+++ b/sdl/SdlTkX.c
@@ -106,7 +106,7 @@ struct EventThreadStartup {
     int init_done;
     int *root_width;
     int *root_height;
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     int want_startup_script;
     char *startup_script;
 #endif
@@ -128,7 +128,7 @@ static Tcl_Condition xlib_cond;
 static Tcl_Condition time_cond;
 static SDL_atomic_t timer_enabled;
 static int num_displays = 0;
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
 struct EventThreadStartup evt_startup = { 0, NULL, NULL, 0, NULL };
 #endif
 
@@ -155,6 +155,12 @@ static void SdlTkLostFocusWindow(void);
 void
 SdlTkLock(Display *display)
 {
+#ifdef __HAIKU__
+	// We may get called before things are fully initialized.
+	while (!tclStubsPtr)
+		sched_yield();
+#endif
+
     Tcl_MutexLock(&xlib_lock);
     if (display != NULL) {
 	while ((xlib_grab != NULL) && (xlib_grab != display)) {
@@ -5752,6 +5758,15 @@ fatal:
     }
 #endif
 
+#ifdef __HAIKU__
+	// Set defaults so that things work better.
+	SdlTkX.arg_nogl = 1;
+	SdlTkX.arg_resizable = 1;
+	SdlTkX.arg_swcursor = 1;
+	width = 800;
+	height = 600;
+#endif
+
     /* Preset some defaults. */
     SdlTkX.dec_frame_width = 6;
     SdlTkX.dec_title_height = 20;
@@ -5785,8 +5800,10 @@ fatal:
     if (SdlTkX.arg_noborder) {
 	videoFlags |= SDL_WINDOW_BORDERLESS;
     }
+#ifndef __HAIKU__
     width = 1024;
     height = 768;
+#endif
     /*
      * Start the root window hidden since font init
      * may take some time. At end of font init in
@@ -6523,7 +6540,7 @@ ctxRetry:
  *----------------------------------------------------------------------
  */
 
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
 void
 SdlTkEventThread(void)
 #else
@@ -6543,7 +6560,7 @@ EventThread(ClientData clientData)
 				   Uint16 rate, Uint16 delay);
     extern int SDL_SendKeyboardText(const char *text);
 #endif
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     struct EventThreadStartup *evs = &evt_startup;
 #else
     struct EventThreadStartup *evs = (struct EventThreadStartup *) clientData;
@@ -6554,13 +6571,13 @@ EventThread(ClientData clientData)
     Android_JNI_SetupThread();
 #endif
     SdlTkLock(NULL);
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     while (evs->root_width == NULL && evs->root_height == NULL) {
 	SdlTkWaitLock();
     }
 #endif
     initSuccess = PerformSDLInit(evs->root_width, evs->root_height);
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     evs->root_width = evs->root_height = NULL;
     SDL_PumpEvents();
     if (evs->want_startup_script) {
@@ -6774,7 +6791,7 @@ EventThread(ClientData clientData)
     }
 
 eventThreadEnd:
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     exit(3);
 #else
     Tcl_ExitThread(0);
@@ -6782,7 +6799,7 @@ eventThreadEnd:
 #endif
 }
 
-#ifndef __APPLE__
+#if !defined(__APPLE__) && !defined(__HAIKU__)
 static void
 EventThreadExitHandler(ClientData clientData)
 {
@@ -6802,7 +6819,7 @@ EventThreadExitHandler(ClientData clientData)
 static void
 OpenVeryFirstDisplay(int *root_width, int *root_height)
 {
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     struct EventThreadStartup *evs = &evt_startup;
 #else
     extern void TclCreateLateExitHandler(Tcl_ExitProc *, ClientData);
@@ -6812,7 +6829,7 @@ OpenVeryFirstDisplay(int *root_width, int *root_height)
     evs->init_done = 0;
     evs->root_width = root_width;
     evs->root_height = root_height;
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     /*
      * Rendezvous with already running event thread, which
      * in MacOSX must be the main thread.
diff --git a/sdl/tkAppInit.c b/sdl/tkAppInit.c
index e40eab2..e2ae821 100644
--- a/sdl/tkAppInit.c
+++ b/sdl/tkAppInit.c
@@ -68,7 +68,7 @@ MODULE_SCOPE int TK_LOCAL_MAIN_HOOK(int *argc, char ***argv);
 #undef Tcl_ObjSetVar2
 #undef Tcl_NewStringObj
 
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
 struct ThreadStartup {
     int argc;
     char **argv;
@@ -308,7 +308,7 @@ GetOBBDir(void)
 }
 #endif
 
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
 /*
  *----------------------------------------------------------------------
  *
@@ -356,7 +356,7 @@ main(
     const char *path, *temp, *pcpath;
     char *ldpath = NULL, *end;
 #endif
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     Tcl_ThreadId thrId;
     struct ThreadStartup startup;
     extern void SdlTkEventThread(void);
@@ -438,7 +438,7 @@ main(
 		    free(ldpath);
 		    ldpath = NULL;
 		}
-	    }	
+	    }
 	}
 	/* now enhance LD_LIBRARY_PATH and PATH */
 	if (ldpath != NULL) {
@@ -504,7 +504,7 @@ main(
 #else
     Tcl_FindExecutable(argv[0]);
 #endif
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__HAIKU__)
     /*
      * We need the SDL event handling run in the main thread,
      * which seems to be a Cocoa requirement. Therefore Tk_MainEx()
-- 
2.30.2

