sdl12-compat: video: Add basic support for SDL_WINDOWID.

From b42bcef599891c58018e543c4e01e0ccac1a67f8 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 29 Aug 2022 10:57:32 -0400
Subject: [PATCH] video: Add basic support for SDL_WINDOWID.

This is not perfect (and probably can't be), but this is better than nothing.

Fixes #192.
---
 src/SDL12_compat.c | 24 ++++++++++++++++++++----
 src/SDL20_syms.h   |  1 +
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 12740e5d..6cbb48b6 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -5623,6 +5623,7 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
     SDL_bool fix_bordless_fs_win = SDL_TRUE;
     int scaled_width = width;
     int scaled_height = height;
+    const char *fromwin_env = NULL;
 
     if (flags12 & SDL12_OPENGL) {
         /* For now we default GL scaling to ENABLED. If an app breaks or is linked directly
@@ -5654,8 +5655,6 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
 
     ForceGLSwapBufferContext = SDL12Compat_GetHintBoolean("SDL12COMPAT_FORCE_GL_SWAPBUFFER_CONTEXT", SDL_FALSE);
 
-    FIXME("currently ignores SDL_WINDOWID, which we could use with SDL_CreateWindowFrom ...?");
-
     flags12 &= ~SDL12_HWACCEL; /* just in case - https://github.com/libsdl-org/SDL-1.2/issues/817 */
 
     /* SDL_SetVideoMode() implicitly inits if necessary. */
@@ -5792,7 +5791,11 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
         fullscreen_flags20 |= SDL_WINDOW_FULLSCREEN_DESKTOP;
     }
 
-    if (window_size_scaling <= 0.0f) {
+    fromwin_env = SDL20_getenv("SDL_WINDOWID");
+
+    if (fromwin_env) {
+        window_size_scaling = 1.0f;  /* don't scale for external windows */
+    } else if (window_+size_scaling <= 0.0f) {
         window_size_scaling = 1.0f;  /* bogus value, reset to default */
     } else if (flags12 & SDL12_RESIZABLE) {
         window_size_scaling = 1.0f;  /* assume that resizable windows are already prepared to handle whatever without scaling. */
@@ -5805,7 +5808,20 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
         scaled_height = (int) (window_size_scaling * height);
     }
 
-    if (!VideoWindow20) {  /* create it */
+    if (fromwin_env) {
+        char *endp = NULL;
+        const Uint64 windowid = SDL_strtoull(fromwin_env, &endp, 0);
+        if ((*fromwin_env == '\0') || (*endp != '\0')) {
+            SDL20_SetError("Invalid SDL_WINDOWID");
+            return EndVidModeCreate();
+        } else {
+            EndVidModeCreate();
+            VideoWindow20 = SDL20_CreateWindowFrom((void *) (size_t) (windowid));
+            if (!VideoWindow20) {
+                return EndVidModeCreate();
+            }
+        }
+    } else if (!VideoWindow20) {  /* create it */
         int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
         Uint32 flags20 = fullscreen_flags20;
         if (flags12 & SDL12_OPENGL) { flags20 |= SDL_WINDOW_OPENGL; }
diff --git a/src/SDL20_syms.h b/src/SDL20_syms.h
index 2484cfb3..b4b7d04f 100644
--- a/src/SDL20_syms.h
+++ b/src/SDL20_syms.h
@@ -85,6 +85,7 @@ SDL20_SYM(void,EnableScreenSaver,(void),(),)
 SDL20_SYM(void,DisableScreenSaver,(void),(),)
 
 SDL20_SYM(SDL_Window *,CreateWindow,(const char *a, int b, int c, int d, int e, Uint32 f),(a,b,c,d,e,f),return)
+SDL20_SYM(SDL_Window *,CreateWindowFrom,(const void *a),(a),return)
 SDL20_SYM(void,DestroyWindow,(SDL_Window *a),(a),)
 SDL20_SYM(void,SetWindowIcon,(SDL_Window *a,SDL_Surface *b),(a,b),)
 SDL20_SYM(int,UpdateWindowSurface,(SDL_Window *a),(a),return)