From f1bc9b87b11b29c00d4c89c3e0385fde1281ec47 Mon Sep 17 00:00:00 2001
From: Bohdan Shulyar <mars873@gmail.com>
Date: Fri, 16 Feb 2024 13:00:47 +0200
Subject: [PATCH] platform: android: port to SDL

---
 .github/workflows/c-cpp.yml   | 12 +++---------
 .gitignore                    |  5 +++++
 engine/client/console.c       |  4 ++++
 engine/client/in_touch.c      | 17 ++++++++++++++++-
 engine/platform/sdl/events.c  |  2 ++
 engine/platform/sdl/vid_sdl.c | 15 ++++++++-------
 engine/wscript                |  3 ++-
 scripts/gha/build_android.sh  |  8 ++++----
 8 files changed, 44 insertions(+), 22 deletions(-)

diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml
index d7cc76ee..5136764e 100644
--- a/.github/workflows/c-cpp.yml
+++ b/.github/workflows/c-cpp.yml
@@ -24,14 +24,9 @@ jobs:
 #          - os: ubuntu-aarch64-20.04
 #            targetos: linux
 #            targetarch: aarch64
-
-#          - os: ubuntu-20.04
-#            targetos: android
-#            targetarch: 32
-#          - os: ubuntu-20.04
-#            targetos: android
-#            targetarch: 64
-
+          - os: ubuntu-20.04
+            targetos: android
+            targetarch: multiarch
 #          - os: ubuntu-20.04
 #            targetos: motomagx
 #            targetarch: armv6
@@ -51,7 +46,6 @@ jobs:
     env:
       SDL_VERSION: 2.28.5
       GH_CPU_ARCH: ${{ matrix.targetarch }}
-      ANDROID_SDK_TOOLS_VER: 4333796
     steps:
     - name: Checkout
       uses: actions/checkout@v3
diff --git a/.gitignore b/.gitignore
index 42afad93..315b72ca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -54,6 +54,11 @@ CMakeFiles
 Makefile
 cmake_install.cmake
 install_manifest.txt
+CMakeLists.txt*
+CMakeScripts
+Testing
+compile_commands.json
+_deps
 # makedepend
 Makefile.dep
 *.bak
diff --git a/engine/client/console.c b/engine/client/console.c
index e4b58134..7b94e7da 100644
--- a/engine/client/console.c
+++ b/engine/client/console.c
@@ -2100,9 +2100,13 @@ void Con_RunConsole( void )
 	// decide on the destination height of the console
 	if( host.allow_console && cls.key_dest == key_console )
 	{
+#if XASH_MOBILE_PLATFORM
+		con.showlines = refState.height; // always full screen on mobile devices
+#else
 		if( cls.state < ca_active || cl.first_frame )
 			con.showlines = refState.height;	// full screen
 		else con.showlines = (refState.height >> 1);	// half screen
+#endif
 	}
 	else con.showlines = 0; // none visible
 
diff --git a/engine/client/in_touch.c b/engine/client/in_touch.c
index 62bf9089..d804297a 100644
--- a/engine/client/in_touch.c
+++ b/engine/client/in_touch.c
@@ -2029,10 +2029,18 @@ int IN_TouchEvent( touchEventType type, int fingerID, float x, float y, float dx
 	{
 		touch.move_finger = touch.resize_finger = touch.look_finger = -1;
 		// Hack for keyboard, hope it help
+		// a1ba: this is absolutely horrible
 		if( cls.key_dest == key_console || cls.key_dest == key_message )
 		{
-			if ( type == event_down ) // don't pop it again on event_up
+			static float x1 = 0.0f;
+			x1 += dx;
+
+			if( type == event_up ) // don't show keyboard on every tap
+			{
 				Key_EnableTextInput( true, true );
+				x1 = 0.0f;
+			}
+
 			if( cls.key_dest == key_console )
 			{
 				static float y1 = 0;
@@ -2054,6 +2062,13 @@ int IN_TouchEvent( touchEventType type, int fingerID, float x, float y, float dx
 			// exit of console area
 			if( type == event_down && x < 0.1f && y > 0.9f )
 				Cbuf_AddText( "escape\n" );
+
+			// swipe from edge to exit console/chat
+			if(( x > 0.8f && x1 < -0.1f ) || ( x < 0.2f && x1 > 0.1f ))
+			{
+				Cbuf_AddText( "escape\n" );
+				x1 = 0.0f;
+			}
 		}
 		UI_MouseMove( TO_SCRN_X(x), TO_SCRN_Y(y) );
 		//MsgDev( D_NOTE, "touch %d %d\n", TO_SCRN_X(x), TO_SCRN_Y(y) );
diff --git a/engine/platform/sdl/events.c b/engine/platform/sdl/events.c
index 2e9531a1..7e7f9b28 100644
--- a/engine/platform/sdl/events.c
+++ b/engine/platform/sdl/events.c
@@ -654,7 +654,9 @@ static void SDLash_EventFilter( SDL_Event *event )
 			SDLash_ActiveEvent( false );
 			break;
 		case SDL_WINDOWEVENT_RESIZED:
+#if !XASH_MOBILE_PLATFORM
 			if( vid_fullscreen.value == WINDOW_MODE_WINDOWED )
+#endif
 			{
 				SDL_Window *wnd = SDL_GetWindowFromID( event->window.windowID );
 				VID_SaveWindowSize( event->window.data1, event->window.data2,
diff --git a/engine/platform/sdl/vid_sdl.c b/engine/platform/sdl/vid_sdl.c
index 54ef7a89..1d097d42 100644
--- a/engine/platform/sdl/vid_sdl.c
+++ b/engine/platform/sdl/vid_sdl.c
@@ -744,7 +744,6 @@ qboolean VID_CreateWindow( int width, int height, window_mode_t window_mode )
 	if( !glw_state.software )
 		SetBits( wndFlags, SDL_WINDOW_OPENGL );
 
-#if !XASH_MOBILE_PLATFORM
 	if( window_mode == WINDOW_MODE_WINDOWED )
 	{
 		SDL_Rect r;
@@ -787,10 +786,6 @@ qboolean VID_CreateWindow( int width, int height, window_mode_t window_mode )
 		SetBits( wndFlags, SDL_WINDOW_BORDERLESS );
 		xpos = ypos = 0;
 	}
-#else
-	SetBits( wndFlags, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_INPUT_GRABBED );
-	xpos = ypos = SDL_WINDOWPOS_UNDEFINED;
-#endif
 
 	if( !VID_CreateWindowWithSafeGL( wndname, xpos, ypos, width, height, wndFlags ))
 		return false;
@@ -799,14 +794,12 @@ qboolean VID_CreateWindow( int width, int height, window_mode_t window_mode )
 	if( FBitSet( SDL_GetWindowFlags( host.hWnd ), SDL_WINDOW_MAXIMIZED|SDL_WINDOW_FULLSCREEN_DESKTOP ) != 0 )
 		SDL_GetWindowSize( host.hWnd, &width, &height );
 
-#if !XASH_MOBILE_PLATFORM
 	if( window_mode != WINDOW_MODE_WINDOWED )
 	{
 		if( !VID_SetScreenResolution( width, height, window_mode ))
 			return false;
 	}
 	else VID_RestoreScreenResolution();
-#endif
 
 	VID_SetWindowIcon( host.hWnd );
 	SDL_ShowWindow( host.hWnd );
@@ -1175,6 +1168,14 @@ qboolean VID_SetMode( void )
 #endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
 	}
 
+#if XASH_MOBILE_PLATFORM
+	if( Q_strcmp( vid_fullscreen.string, DEFAULT_FULLSCREEN ))
+	{
+		Cvar_DirectSet( &vid_fullscreen, DEFAULT_FULLSCREEN );
+		Con_Reportf( S_ERROR  "VID_SetMode: windowed unavailable on this platform\n" );
+	}
+#endif
+
 	if( !FBitSet( vid_fullscreen.flags, FCVAR_CHANGED ))
 		Cvar_DirectSet( &vid_fullscreen, DEFAULT_FULLSCREEN );
 	else
diff --git a/engine/wscript b/engine/wscript
index e2a5157e..c9fcb9dd 100644
--- a/engine/wscript
+++ b/engine/wscript
@@ -237,7 +237,8 @@ def build(bld):
 			app_name = 'xash3d-fwgs'
 		)
 	else:
-		if bld.env.DISABLE_LAUNCHER:
+		# always build as shared library on Android
+		if bld.env.DISABLE_LAUNCHER and bld.env.DEST_OS != "android":
 			install_path = bld.env.BINDIR
 			program = 'cxxprogram' if is_cxx_link else 'cprogram'
 			if bld.env.STATIC:
diff --git a/scripts/gha/build_android.sh b/scripts/gha/build_android.sh
index db3bed28..9636d157 100755
--- a/scripts/gha/build_android.sh
+++ b/scripts/gha/build_android.sh
@@ -7,16 +7,16 @@ export PATH=$PATH:$JAVA_HOME/bin:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$AN
 
 pushd android
 
-./gradlew assembleContinuous
+./gradlew assembleDebug
 
-pushd app/build/outputs/apk/continuous
+pushd app/build/outputs/apk/debug
 
 $ANDROID_HOME/build-tools/34.0.0/apksigner sign --ks $GITHUB_WORKSPACE/android/debug.keystore --ks-key-alias androiddebugkey \
-    --ks-pass pass:android --key-pass pass:android --out app-continuous-signed.apk app-continuous-unsigned.apk
+    --ks-pass pass:android --key-pass pass:android --out app-debug-signed.apk app-debug.apk
 
 popd
 popd
 
 mkdir -p artifacts/
 
-mv android/app/build/outputs/apk/continuous/app-continuous-signed.apk artifacts/xash3d-fwgs-android.apk
+mv android/app/build/outputs/apk/debug/app-debug-signed.apk artifacts/xash3d-fwgs-android.apk