From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jelle Raaijmakers <jelle@gmta.nl>
Date: Wed, 31 Aug 2022 14:15:48 +0200
Subject: [PATCH] Add Minerva platform support

Co-Authored-By: Jesse Buhagiar <jooster669@gmail.com>
---
 CMakeLists.txt        |  9 -----
 README.md             | 32 ++++++++-------
 src/CMakeLists.txt    | 33 ++--------------
 src/linux/glw_linux.h |  2 +-
 src/linux/net_udp.c   |  2 +-
 src/linux/q_shlinux.c | 36 +++++++++--------
 src/linux/rw_sdl.c    |  9 ++++-
 src/linux/snd_sdl.c   |  2 +-
 src/linux/sys_linux.c | 90 +++++++++++++++++++++----------------------
 src/linux/vid_so.c    |  9 +++--
 10 files changed, 99 insertions(+), 125 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 45d107b..49d329b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,16 +3,7 @@ cmake_policy(SET CMP0048 NEW)
 
 project (quake2sdl VERSION 0.17)
 
-set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/Modules")
 set (CMAKE_C_FLAGS_RELEASE "-O3")
 find_package (SDL2 REQUIRED)
-find_package (OpenGL REQUIRED)
-
-option (WITH_RETEX "Support for retexture" OFF)
-option (WITH_QMAX "Improved graphics" OFF)
-
-if (WITH_QMAX)
-  find_package (JPEG REQUIRED)
-endif (WITH_QMAX)
 
 add_subdirectory (src)
diff --git a/README.md b/README.md
index d86b324..1b119bf 100644
--- a/README.md
+++ b/README.md
@@ -26,20 +26,18 @@ TODO:
 Building and installation
 -------------------------
 
-To build **Quake2 SDL** install SDL2, OpenGL  and jpeg libraries, then
-run the following commands:
-````
-mkdir build
-cd build
-cmake ..
-make
-make install
-````
-
-You can pass `-DWITH_QMAX=ON` option to cmake to build an improved
-OpenGL renderer or `-DWITH_RETEX=ON` to allow retexture packs. You
-will need to download these packs from
-[icculus](http://offload1.icculus.org/quake2/files/) site (download
-`.pak` files). You will also need `pak0.pak` from the original
-game. Place these files where `game.so` is installed or to
-`/install/prefix/share/quake2/baseq2`.
+Build like a regular Minerva Port.
+In the `quake2` port directory, run `./package.sh`.
+
+You'll then need to source the QuakeII demo pak file yourself. iD's FTP is offline, however you can find the demo in a few places. Once you've downloaded the demo, move the `pak0.pak` file to `/home/anon/.quake2/baseq2/` and run `quake2` from the command line. 
+
+Running with OpenGL
+-------------------
+
+Dynamically switching between software rendering and LibGL does not yet work; to run Quake2 in OpenGL mode, add the
+following statements to `~/.quake2/baseq2/config.cfg`:
+
+```
+set vid_ref "sdlgl"
+set gl_driver "libgl.so.minerva"
+```
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d8ea312..fcf320f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -2,23 +2,7 @@ set (quake_resource_libdir ${CMAKE_INSTALL_PREFIX}/lib/quake2sdl)
 set (quake_resource_datadir ${CMAKE_INSTALL_PREFIX}/share/quake2)
 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/q_resources.h.in ${CMAKE_CURRENT_BINARY_DIR}/q_resources.h)
 
-add_definitions (-DHAVE_IPV6 -DLINUX_VERSION="1")
 include_directories (${SDL2_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${OPENGL_INCLUDE_DIR})
-if (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
-  add_definitions (-DHAVE_SIN6_LEN)
-elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
-  # Linux-specific stuff like mremap
-  add_definitions (-D_GNU_SOURCE)
-endif ()
-
-if (WITH_RETEX)
-  add_definitions (-DRETEX)
-endif (WITH_RETEX)
-
-if (WITH_QMAX)
-  add_definitions (-DQMAX)
-  include_directories (${JPEG_INCLUDE_DIR})
-endif (WITH_QMAX)
 
 set (Q2_SOURCES
   client/cl_cin.c
@@ -65,22 +49,14 @@ set (Q2_SOURCES
   linux/vid_so.c
   linux/sys_linux.c
   linux/glob.c
-  linux/net_udp6.c
+  linux/net_udp.c
   game/q_shared.c
   linux/snd_sdl.c
-  )
-
-if (WITH_QMAX)
-  set (Q2_SOURCES ${Q2_SOURCES} client/cl_fxmax.c)
-else (WITH_QMAX)
-  set (Q2_SOURCES ${Q2_SOURCES} client/cl_fx.c)
-endif (WITH_QMAX)
+)
 
+set (Q2_SOURCES ${Q2_SOURCES} client/cl_fx.c)
 add_executable (quake2 ${Q2_SOURCES})
-target_link_libraries (quake2 ${SDL2_LIBRARY} m)
-if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
-  target_link_libraries (quake2 dl)
-endif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+target_link_libraries (quake2 dl SDL2 pthread gfx gui c m)
 
 add_library (ref-softsdl SHARED
   ref_soft/r_aclip.c
@@ -130,7 +106,6 @@ add_library (ref-sdlgl SHARED
   linux/glob.c
   linux/rw_sdl.c
   linux/rw_linux.c
-  linux/joystick.c
 )
 if (WITH_QMAX)
   target_link_libraries (ref-sdlgl ${JPEG_LIBRARY})
diff --git a/src/linux/glw_linux.h b/src/linux/glw_linux.h
index 4994129..9b0cd6b 100644
--- a/src/linux/glw_linux.h
+++ b/src/linux/glw_linux.h
@@ -19,7 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 #ifndef __linux__
 #ifndef __FreeBSD__
-#error You shouldnt be including this file on non-unix platforms
+// #error You shouldnt be including this file on non-unix platforms
 #endif
 #endif
 
diff --git a/src/linux/net_udp.c b/src/linux/net_udp.c
index 447317b..b630e0f 100644
--- a/src/linux/net_udp.c
+++ b/src/linux/net_udp.c
@@ -496,7 +496,7 @@ int NET_Socket (char *net_interface, int port)
         return 0;
     }
 
-    if (!net_interface || !net_interface[0] || !stricmp(net_interface, "localhost"))
+    if (!net_interface || !net_interface[0] || !strcmp(net_interface, "localhost"))
         address.sin_addr.s_addr = INADDR_ANY;
     else
         NET_StringToSockaddr (net_interface, (struct sockaddr *)&address);
diff --git a/src/linux/q_shlinux.c b/src/linux/q_shlinux.c
index 801f750..1851276 100644
--- a/src/linux/q_shlinux.c
+++ b/src/linux/q_shlinux.c
@@ -76,26 +76,30 @@ void *Hunk_Alloc (int size)
     return buf;
 }
 
+
+// FIXME: 64-bit????
+#define PAGE_SIZE 4096
+
 int Hunk_End (void)
 {
     byte *n;
 
-#if defined(__FreeBSD__)
-  size_t old_size = maxhunksize;
-  size_t new_size = curhunksize + sizeof(int);
-  void * unmap_base;
-  size_t unmap_len;
-
-  new_size = round_page(new_size);
-  old_size = round_page(old_size);
-  if (new_size > old_size)
-      n = 0; /* error */
-  else if (new_size < old_size)
-  {
-    unmap_base = (caddr_t)(membase + new_size);
-    unmap_len = old_size - new_size;
-    n = munmap(unmap_base, unmap_len) + membase;
-  }
+#if defined(__FreeBSD__) || defined(__minerva__)
+    size_t old_size = maxhunksize;
+    size_t new_size = curhunksize + sizeof(int);
+    void * unmap_base;
+    size_t unmap_len;
+
+    new_size = ((new_size + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)));
+    old_size = ((old_size + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)));
+    if (new_size > old_size)
+        n = 0; /* error */
+    else if (new_size < old_size)
+    {
+        unmap_base = (caddr_t)(membase + new_size);
+        unmap_len = old_size - new_size;
+        n = munmap(unmap_base, unmap_len) + membase;
+    }
 #endif
 #if defined(__linux__)
     n = mremap(membase, maxhunksize, curhunksize + sizeof(int), 0);
diff --git a/src/linux/rw_sdl.c b/src/linux/rw_sdl.c
index 460e28a..2e5a82f 100644
--- a/src/linux/rw_sdl.c
+++ b/src/linux/rw_sdl.c
@@ -20,7 +20,7 @@
 #include <string.h>
 #include <sys/mman.h>
 
-#include <SDL.h>
+#include <SDL2/SDL.h>
 
 #ifdef OPENGL
 #include <GL/gl.h>
@@ -562,7 +562,7 @@ static qboolean SWimp_InitGraphics( qboolean fullscreen )
         return false;
     }
 
-    renderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_ACCELERATED);
+    renderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_SOFTWARE);
     if (renderer == NULL) {
         Sys_Error("(SOFTSDL) SDL CreateRenderer failed: %s\n", SDL_GetError());
         return false;
@@ -951,3 +951,8 @@ void Fake_glColorTableEXT( GLenum target, GLenum internalformat,
     qgl3DfxSetPaletteEXT((GLuint *)temptable);
 }
 #endif
+
+void UpdateHardwareGamma(void)
+{
+    // Noop
+}
diff --git a/src/linux/snd_sdl.c b/src/linux/snd_sdl.c
index b9ae691..a2b4755 100644
--- a/src/linux/snd_sdl.c
+++ b/src/linux/snd_sdl.c
@@ -25,7 +25,7 @@
     $Id: snd_sdl.c,v 1.2 2002/02/09 20:29:38 relnev Exp $
 */
 
-#include <SDL.h>
+#include <SDL2/SDL.h>
 
 #include "../client/client.h"
 #include "../client/snd_loc.h"
diff --git a/src/linux/sys_linux.c b/src/linux/sys_linux.c
index 9d12722..4e3b425 100644
--- a/src/linux/sys_linux.c
+++ b/src/linux/sys_linux.c
@@ -27,8 +27,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include <fcntl.h>
 #include <stdarg.h>
 #include <stdio.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
 #include <sys/stat.h>
 #include <string.h>
 #include <ctype.h>
@@ -86,7 +84,7 @@ void Sys_Quit (void)
 {
     CL_Shutdown ();
     Qcommon_Shutdown ();
-    fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
+    //fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
     _exit(0);
 }
 
@@ -103,7 +101,7 @@ void Sys_Error (char *error, ...)
     char        string[1024];
 
 // change stdin to non blocking
-    fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
+	//fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
 
     CL_Shutdown ();
     Qcommon_Shutdown ();
@@ -153,35 +151,35 @@ void floating_point_exception_handler(int whatever)
 
 char *Sys_ConsoleInput(void)
 {
-    static char text[256];
-    int     len;
-    fd_set    fdset;
-    struct timeval timeout;
-
-    if (!dedicated || !dedicated->value)
-        return NULL;
-
-    if (!stdin_active)
-        return NULL;
-
-    FD_ZERO(&fdset);
-    FD_SET(0, &fdset); // stdin
-    timeout.tv_sec = 0;
-    timeout.tv_usec = 0;
-    if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
-        return NULL;
-
-    len = read (0, text, sizeof(text));
-    if (len == 0) { // eof!
-        stdin_active = false;
-        return NULL;
-    }
-
-    if (len < 1)
-        return NULL;
-    text[len-1] = 0;    // rip off the /n and terminate
-
-    return text;
+	static char text[256];
+	int     len;
+	fd_set	fdset;
+	struct timeval timeout;
+
+	if (!dedicated || !dedicated->value)
+		return NULL;
+
+	if (!stdin_active)
+		return NULL;
+
+	FD_ZERO(&fdset);
+	FD_SET(0, &fdset); // stdin
+	timeout.tv_sec = 0;
+	timeout.tv_usec = 0;
+	//if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
+		//return NULL;
+
+	len = read (0, text, sizeof(text));
+	if (len == 0) { // eof!
+		stdin_active = false;
+		return NULL;
+	}
+
+	if (len < 1)
+		return NULL;
+	text[len-1] = 0;    // rip off the /n and terminate
+
+	return text;
 }
 
 /*****************************************************************************/
@@ -297,23 +295,23 @@ int main (int argc, char **argv)
 {
     int     time, oldtime, newtime;
 
-    // go back to real user for config loads
-    saved_euid = geteuid();
-    seteuid(getuid());
-    
-    printf ("Quake 2 -- Version %s\n", LINUX_VERSION);
+	// go back to real user for config loads
+	saved_euid = geteuid();
+	seteuid(getuid());
+	
+	printf ("Quake 2 -- Version %s Minerva\n", "0.1");
 
     Qcommon_Init(argc, argv);
 
-    fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
+	//fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
 
-    nostdout = Cvar_Get("nostdout", "0", 0);
-    if (!nostdout->value) {
-        fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);    
-    }
-    oldtime = Sys_Milliseconds ();
-    while (1)
-    {
+	nostdout = Cvar_Get("nostdout", "0", 0);
+	if (!nostdout->value) {
+		//fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);	
+	}
+	oldtime = Sys_Milliseconds ();
+	while (1)
+	{
 // find time spent rendering last frame
         do {
             newtime = Sys_Milliseconds ();
diff --git a/src/linux/vid_so.c b/src/linux/vid_so.c
index a060303..fa326b5 100644
--- a/src/linux/vid_so.c
+++ b/src/linux/vid_so.c
@@ -140,7 +140,7 @@ typedef struct vidmode_s
 
 vidmode_t vid_modes[] =
 {
-    { "Mode 0: 320x240",   320, 240,   0 },
+    { "Mode 0: 320x240",   640, 480,   0 }, // HACK: Trying to access graphics options or setting video mode kills the program.
     { "Mode 1: 400x300",   400, 300,   1 },
     { "Mode 2: 512x384",   512, 384,   2 },
     { "Mode 3: 640x480",   640, 480,   3 },
@@ -239,8 +239,11 @@ qboolean VID_LoadRefresh( char *name )
     //regain root
     seteuid(saved_euid);
 
-    snprintf (fn, MAX_OSPATH, "%s/%s", RESOURCE_LIBDIR, name );
-    
+    // FIXME: RESOURCE_LIBDIR is a completely invalid path!
+    // Un-hardcode me eventually please!
+    //snprintf (fn, MAX_OSPATH, "%s/%s", RESOURCE_LIBDIR, name );
+    snprintf (fn, MAX_OSPATH, "%s/%s", "/usr/local/lib/quake2sdl", name );
+    Com_Printf("%s\n", fn);
     if (stat(fn, &st) == -1) {
         Com_Printf( "LoadLibrary(\"%s\") failed: %s\n", name, strerror(errno));
         return false;
