2017-10-12 12:02:25 +00:00
# include <jni.h>
# include <errno.h>
# include <android/log.h>
# include "quakedef.h"
# include <unistd.h>
# include <fcntl.h>
# include <dlfcn.h>
# include <sys/stat.h>
# include <dirent.h>
2018-01-24 20:19:15 +00:00
# include <pthread.h>
2019-04-15 18:43:42 +00:00
# include "glquake.h"
2017-10-12 12:02:25 +00:00
# ifndef ANDROID
# error ANDROID wasnt defined
# endif
2019-04-17 00:50:14 +00:00
#if 0
//FIXME: remove the nativeactivity shit. android's standard NativeActivity class is buggy and basically fucked.
2019-04-15 18:43:42 +00:00
# include <android/keycodes.h>
# include <android/native_window_jni.h>
// ANativeWindow_fromSurface((jobject)getSurfaceHolder().getSurface())
2019-04-17 00:50:14 +00:00
# else
//NOTE: This is apache 2.0, which means GPL3.0+ ONLY, no gpl2.
# include <../../../../../sources/android/native_app_glue/android_native_app_glue.h> //Fucking frameworks suck big hairy donkey balls.
JNIEXPORT void ANativeActivity_onCreate ( ANativeActivity * activity , void * savedState , size_t savedStateSize ) ;
# include <../../../../../sources/android/native_app_glue/android_native_app_glue.c> //Fucking frameworks suck big hairy donkey balls.
# endif
2019-04-15 18:43:42 +00:00
2017-10-12 12:02:25 +00:00
# ifndef isDedicated
# ifdef SERVERONLY
qboolean isDedicated = true ;
# else
qboolean isDedicated = false ;
# endif
# endif
extern int r_blockvidrestart ;
float sys_dpi_x , sys_dpi_y ;
static void * sys_memheap ;
2019-04-15 18:43:42 +00:00
//static unsigned int vibrateduration;
2017-10-12 12:02:25 +00:00
static char sys_basedir [ MAX_OSPATH ] ;
static char sys_basepak [ MAX_OSPATH ] ;
extern jmp_buf host_abort ;
2019-04-15 18:43:42 +00:00
extern qboolean r_forceheadless ;
static qboolean r_forcevidrestart ;
ANativeWindow * sys_nativewindow ;
static struct android_app * android_app_state ; //basically used only for errors.
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
//cvar_t sys_vibrate = CVARFD("sys_vibrate", "1", CVAR_ARCHIVE, "Enables the system vibrator for damage events and such things. The value provided is a duration scaler.");
2017-10-12 12:02:25 +00:00
cvar_t sys_osk = CVAR ( " sys_osk " , " 0 " ) ; //to be toggled
cvar_t sys_keepscreenon = CVARFD ( " sys_keepscreenon " , " 1 " , CVAR_ARCHIVE , " If set, the screen will never darken. This might cost some extra battery power, but then so will running a 3d engine. " ) ; //to be toggled
cvar_t sys_orientation = CVARFD ( " sys_orientation " , " landscape " , CVAR_ARCHIVE , " Specifies what angle to render quake at. \n Valid values are: sensor (autodetect), landscape, portrait, reverselandscape, reverseportrait " ) ;
extern cvar_t vid_conautoscale ;
void VID_Register ( void ) ;
2019-04-15 18:43:42 +00:00
# undef LOGI
# undef LOGW
# undef LOGE
# ifndef LOGI
2017-10-12 12:02:25 +00:00
# define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, DISTRIBUTION"Droid", __VA_ARGS__))
2019-04-15 18:43:42 +00:00
# endif
# ifndef LOGW
2017-10-12 12:02:25 +00:00
# define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, DISTRIBUTION"Droid", __VA_ARGS__))
2019-04-15 18:43:42 +00:00
# endif
# ifndef LOGE
2017-10-12 12:02:25 +00:00
# define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, DISTRIBUTION"Droid", __VA_ARGS__))
2019-04-15 18:43:42 +00:00
# endif
2017-10-12 12:02:25 +00:00
2018-03-04 14:41:16 +00:00
void INS_Move ( void )
2017-10-12 12:02:25 +00:00
{
}
void INS_Commands ( void )
{
}
2016-07-21 19:27:59 +00:00
void INS_EnumerateDevices ( void * ctx , void ( * callback ) ( void * ctx , const char * type , const char * devicename , unsigned int * qdevid ) )
2015-04-14 23:12:17 +00:00
{
2017-10-12 12:02:25 +00:00
}
void INS_Init ( void )
{
}
void INS_ReInit ( void )
{
}
void INS_Shutdown ( void )
{
}
2019-04-15 18:43:42 +00:00
void Sys_Vibrate ( float count )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
// if (count < 0)
// count = 0;
// vibrateduration += count*10*sys_vibrate.value;
2017-10-12 12:02:25 +00:00
}
2019-04-15 18:43:42 +00:00
static int mapkey ( int androidkey )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
switch ( androidkey )
{
// case AKEYCODE_SOFT_LEFT: return K_;
// case AKEYCODE_SOFT_RIGHT: return K_;
// case AKEYCODE_HOME: return K_;
// case AKEYCODE_BACK: return K_;
// case AKEYCODE_CALL: return K_;
// case AKEYCODE_ENDCALL: return K_;
case AKEYCODE_0 : return ' 0 ' ;
case AKEYCODE_1 : return ' 1 ' ;
case AKEYCODE_2 : return ' 2 ' ;
case AKEYCODE_3 : return ' 3 ' ;
case AKEYCODE_4 : return ' 4 ' ;
case AKEYCODE_5 : return ' 5 ' ;
case AKEYCODE_6 : return ' 6 ' ;
case AKEYCODE_7 : return ' 7 ' ;
case AKEYCODE_8 : return ' 8 ' ;
case AKEYCODE_9 : return ' 9 ' ;
// case AKEYCODE_STAR: return K_;
// case AKEYCODE_POUND: return K_; //americans don't know what a pound symbol looks like.
case AKEYCODE_DPAD_UP : return K_GP_DPAD_UP ;
case AKEYCODE_DPAD_DOWN : return K_GP_DPAD_DOWN ;
case AKEYCODE_DPAD_LEFT : return K_GP_DPAD_LEFT ;
case AKEYCODE_DPAD_RIGHT : return K_GP_DPAD_RIGHT ;
case AKEYCODE_DPAD_CENTER : return K_ENTER ;
case AKEYCODE_VOLUME_UP : return K_VOLUP ;
case AKEYCODE_VOLUME_DOWN : return K_VOLDOWN ;
case AKEYCODE_POWER : return K_POWER ;
// case AKEYCODE_CAMERA: return K_CAMERA;
// case AKEYCODE_CLEAR: return K_;
case AKEYCODE_A : return ' a ' ;
case AKEYCODE_B : return ' b ' ;
case AKEYCODE_C : return ' c ' ;
case AKEYCODE_D : return ' d ' ;
case AKEYCODE_E : return ' e ' ;
case AKEYCODE_F : return ' f ' ;
case AKEYCODE_G : return ' g ' ;
case AKEYCODE_H : return ' h ' ;
case AKEYCODE_I : return ' i ' ;
case AKEYCODE_J : return ' j ' ;
case AKEYCODE_K : return ' k ' ;
case AKEYCODE_L : return ' l ' ;
case AKEYCODE_M : return ' m ' ;
case AKEYCODE_N : return ' n ' ;
case AKEYCODE_O : return ' o ' ;
case AKEYCODE_P : return ' p ' ;
case AKEYCODE_Q : return ' q ' ;
case AKEYCODE_R : return ' r ' ;
case AKEYCODE_S : return ' s ' ;
case AKEYCODE_T : return ' t ' ;
case AKEYCODE_U : return ' u ' ;
case AKEYCODE_V : return ' v ' ;
case AKEYCODE_W : return ' w ' ;
case AKEYCODE_X : return ' x ' ;
case AKEYCODE_Y : return ' y ' ;
case AKEYCODE_Z : return ' z ' ;
case AKEYCODE_COMMA : return ' , ' ;
case AKEYCODE_PERIOD : return ' . ' ;
case AKEYCODE_ALT_LEFT : return K_LALT ;
case AKEYCODE_ALT_RIGHT : return K_RALT ;
case AKEYCODE_SHIFT_LEFT : return K_LSHIFT ;
case AKEYCODE_SHIFT_RIGHT : return K_RSHIFT ;
case AKEYCODE_TAB : return K_TAB ;
case AKEYCODE_SPACE : return K_SPACE ;
// case AKEYCODE_SYM: return K_;
// case AKEYCODE_EXPLORER : return K_;
// case AKEYCODE_ENVELOPE: return K_;
case AKEYCODE_ENTER : return K_ENTER ;
case AKEYCODE_DEL : return K_BACKSPACE ;
case AKEYCODE_GRAVE : return ' ` ' ;
case AKEYCODE_MINUS : return ' - ' ;
case AKEYCODE_EQUALS : return ' = ' ;
case AKEYCODE_LEFT_BRACKET : return ' [ ' ;
case AKEYCODE_RIGHT_BRACKET : return ' ] ' ;
case AKEYCODE_BACKSLASH : return ' # ' ; //this kinda sums up keymaps like this.
case AKEYCODE_SEMICOLON : return ' ; ' ;
case AKEYCODE_APOSTROPHE : return ' \' ' ;
case AKEYCODE_SLASH : return ' / ' ;
// case AKEYCODE_AT: return K_;
// case AKEYCODE_NUM: return K_;
// case AKEYCODE_HEADSETHOOK: return K_;
// case AKEYCODE_FOCUS: return K_;
// case AKEYCODE_PLUS: return K_;
case AKEYCODE_MENU : return K_APP ;
// case AKEYCODE_NOTIFICATION: return K_;
case AKEYCODE_SEARCH : return K_SEARCH ;
// case AKEYCODE_MEDIA_PLAY_PAUSE: return K_;
// case AKEYCODE_MEDIA_STOP: return K_;
// case AKEYCODE_MEDIA_NEXT: return K_;
// case AKEYCODE_MEDIA_PREVIOUS: return K_;
// case AKEYCODE_MEDIA_REWIND: return K_;
// case AKEYCODE_MEDIA_FAST_FORWARD: return K_;
// case AKEYCODE_MUTE: return K_;
case AKEYCODE_PAGE_UP : return K_PGUP ;
case AKEYCODE_PAGE_DOWN : return K_PGDN ;
// case AKEYCODE_PICTSYMBOLS: return K_;
// case AKEYCODE_SWITCH_CHARSET: return K_;
case AKEYCODE_BUTTON_A : return K_GP_A ;
case AKEYCODE_BUTTON_B : return K_GP_B ;
// case AKEYCODE_BUTTON_C: return K_GP_C;
case AKEYCODE_BUTTON_X : return K_GP_X ;
case AKEYCODE_BUTTON_Y : return K_GP_Y ;
// case AKEYCODE_BUTTON_Z: return K_GP_Z;
case AKEYCODE_BUTTON_L1 : return K_GP_LEFT_SHOULDER ;
case AKEYCODE_BUTTON_R1 : return K_GP_RIGHT_SHOULDER ;
case AKEYCODE_BUTTON_L2 : return K_GP_LEFT_TRIGGER ;
case AKEYCODE_BUTTON_R2 : return K_GP_RIGHT_TRIGGER ;
case AKEYCODE_BUTTON_THUMBL : return K_GP_LEFT_THUMB ;
case AKEYCODE_BUTTON_THUMBR : return K_GP_RIGHT_THUMB ;
case AKEYCODE_BUTTON_START : return K_GP_START ;
case AKEYCODE_BUTTON_SELECT : return K_GP_BACK ;
case AKEYCODE_BUTTON_MODE : return K_GP_GUIDE ;
//And this is the part where you start to see quite why I hate android so much
case 111 : return K_ESCAPE ;
case 112 : return K_DEL ;
case 113 : return K_LCTRL ;
case 114 : return K_RCTRL ;
case 115 : return K_CAPSLOCK ;
case 116 : return K_SCRLCK ;
case 117 : return K_LWIN ;
case 118 : return K_RWIN ;
// case 119: return K_FUNCTION;
// case 120: return K_SYSRQ;
case 121 : return K_PAUSE ;
case 122 : return K_HOME ;
case 123 : return K_END ;
case 124 : return K_INS ;
// case 125: return K_FORWARD;
// case 126: return K_MEDIA_PLAY;
// case 127: return K_MEDIA_PAUSE;
// case 128: return K_MEDIA_CLOSE;
// case 129: return K_MEDIA_EJECT;
// case 130: return K_MEDIA_RECORD;
case 131 : return K_F1 ;
case 132 : return K_F2 ;
case 133 : return K_F3 ;
case 134 : return K_F4 ;
case 135 : return K_F5 ;
case 136 : return K_F6 ;
case 137 : return K_F7 ;
case 138 : return K_F8 ;
case 139 : return K_F9 ;
case 140 : return K_F10 ;
case 141 : return K_F11 ;
case 142 : return K_F12 ;
case 143 : return K_KP_NUMLOCK ;
case 144 : return K_KP_INS ;
case 145 : return K_KP_END ;
case 146 : return K_KP_DOWNARROW ;
case 147 : return K_KP_PGDN ;
case 148 : return K_KP_LEFTARROW ;
case 149 : return K_KP_5 ;
case 150 : return K_KP_RIGHTARROW ;
case 151 : return K_KP_HOME ;
case 152 : return K_KP_UPARROW ;
case 153 : return K_KP_PGUP ;
case 154 : return K_KP_SLASH ;
case 155 : return K_KP_STAR ;
case 156 : return K_KP_MINUS ;
case 157 : return K_KP_PLUS ;
case 158 : return K_KP_DEL ;
case 160 : return K_KP_ENTER ;
default :
Con_DPrintf ( " Android keycode %i is not supported \n " , androidkey ) ;
}
return 0 ;
2017-10-12 12:02:25 +00:00
}
2019-04-15 18:43:42 +00:00
static int32_t engine_handle_input ( struct android_app * app , AInputEvent * event )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
switch ( AInputEvent_getType ( event ) )
{
case AINPUT_EVENT_TYPE_MOTION :
case AINPUT_EVENT_TYPE_KEY :
return 0 ; //we handle these in the java code, so shouldn't ever see them.
}
return 0 ; //no idea what sort of event it is.
2017-10-12 12:02:25 +00:00
}
2019-04-15 18:43:42 +00:00
static void engine_handle_cmd ( struct android_app * app , int32_t cmd )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
switch ( cmd )
{
case APP_CMD_SAVE_STATE :
//FIXME: implement save-game-to-memory...
break ;
case APP_CMD_INIT_WINDOW :
if ( sys_nativewindow ! = app - > window )
{
sys_nativewindow = app - > window ;
r_forceheadless = ( sys_nativewindow = = NULL ) ;
r_forcevidrestart = true ;
}
break ;
case APP_CMD_TERM_WINDOW :
r_forceheadless = true ;
if ( qrenderer & & ! r_forcevidrestart & & sys_nativewindow )
R_RestartRenderer_f ( ) ;
sys_nativewindow = NULL ;
break ;
case APP_CMD_GAINED_FOCUS :
vid . activeapp = true ;
break ;
case APP_CMD_LOST_FOCUS :
vid . activeapp = false ;
break ;
}
}
static void run_intent_url ( struct android_app * app )
{
jobject act = app - > activity - > clazz ;
JNIEnv * jni ;
if ( JNI_OK = = ( * app - > activity - > vm ) - > AttachCurrentThread ( app - > activity - > vm , & jni , NULL ) )
{
jobject intent = ( * jni ) - > CallObjectMethod ( jni , act , ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , act ) , " getIntent " , " ()Landroid/content/Intent; " ) ) ;
if ( intent )
{
jstring data = ( * jni ) - > CallObjectMethod ( jni , intent , ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , intent ) , " getDataString " , " ()Ljava/lang/String; " ) ) ;
if ( data )
{
const char * url = ( * jni ) - > GetStringUTFChars ( jni , data , NULL ) ;
if ( url )
{
if ( ! strncmp ( url , " content: " , 8 ) )
{
/*Java:
Cursor cursor = this . getContentResolver ( ) . query ( data , null , null , null , null ) ;
cursor . moveToFirst ( ) ;
String myloc = cursor . getString ( 0 ) ;
cursor . close ( ) ;
*/
}
else
Host_RunFile ( url , strlen ( url ) , NULL ) ;
( * jni ) - > ReleaseStringUTFChars ( jni , data , url ) ;
}
}
}
//FIXME: do we need to release methodids/objects?
( * app - > activity - > vm ) - > DetachCurrentThread ( app - > activity - > vm ) ;
}
2017-10-12 12:02:25 +00:00
}
2019-04-15 18:43:42 +00:00
static qboolean read_apk_path ( struct android_app * app , char * out , size_t outsize )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
qboolean res = false ;
jobject act = app - > activity - > clazz ;
JNIEnv * jni ;
if ( JNI_OK = = ( * app - > activity - > vm ) - > AttachCurrentThread ( app - > activity - > vm , & jni , NULL ) )
{
jstring result = ( * jni ) - > CallObjectMethod ( jni , act , ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , act ) , " getPackageCodePath " , " ()Ljava/lang/String; " ) ) ;
const char * tmp = ( * jni ) - > GetStringUTFChars ( jni , result , NULL ) ;
if ( tmp )
{
res = true ;
Q_strncpyz ( out , tmp , outsize ) ;
( * jni ) - > ReleaseStringUTFChars ( jni , result , tmp ) ;
}
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
//FIXME: do we need to release methodids/objects?
( * app - > activity - > vm ) - > DetachCurrentThread ( app - > activity - > vm ) ;
}
2018-01-24 20:19:15 +00:00
2019-04-15 18:43:42 +00:00
return res ;
}
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
static void setsoftkeyboard ( struct android_app * app , int flags )
{ //the NDK is unusably buggy when it comes to keyboards, so call into java.
jobject act = app - > activity - > clazz ;
JNIEnv * jni ;
if ( JNI_OK = = ( * app - > activity - > vm ) - > AttachCurrentThread ( app - > activity - > vm , & jni , NULL ) )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
jmethodID func = ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , act ) , " showKeyboard " , " (I)V " ) ;
if ( func )
( * jni ) - > CallVoidMethod ( jni , act , func , flags ) ;
( * app - > activity - > vm ) - > DetachCurrentThread ( app - > activity - > vm ) ;
2017-10-12 12:02:25 +00:00
}
2019-04-15 18:43:42 +00:00
}
static void showMessageAndQuit ( struct android_app * app , const char * errormsg )
{ //no nice way to do this from native.
jobject act = app - > activity - > clazz ;
JNIEnv * jni ;
if ( JNI_OK = = ( * app - > activity - > vm ) - > AttachCurrentThread ( app - > activity - > vm , & jni , NULL ) )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
jmethodID func = ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , act ) , " showMessageAndQuit " , " (Ljava/lang/String;)V " ) ;
if ( func )
( * jni ) - > CallVoidMethod ( jni , act , func , ( * jni ) - > NewStringUTF ( jni , errormsg ) ) ;
( * app - > activity - > vm ) - > DetachCurrentThread ( app - > activity - > vm ) ;
2017-10-12 12:02:25 +00:00
}
2019-04-15 18:43:42 +00:00
}
static void updateOrientation ( struct android_app * app , const char * neworientation )
{ //no nice way to do this from native.
jobject act = app - > activity - > clazz ;
JNIEnv * jni ;
if ( JNI_OK = = ( * app - > activity - > vm ) - > AttachCurrentThread ( app - > activity - > vm , & jni , NULL ) )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
jmethodID func = ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , act ) , " updateOrientation " , " (Ljava/lang/String;)V " ) ;
if ( func )
( * jni ) - > CallVoidMethod ( jni , act , func , ( * jni ) - > NewStringUTF ( jni , neworientation ) ) ;
( * app - > activity - > vm ) - > DetachCurrentThread ( app - > activity - > vm ) ;
2017-10-12 12:02:25 +00:00
}
}
2019-04-15 18:43:42 +00:00
static void updateScreenKeepOn ( struct android_app * app , jboolean keepon )
{ //the NDK is unusably buggy when it comes to keyboards, so call into java.
jobject act = app - > activity - > clazz ;
JNIEnv * jni ;
if ( JNI_OK = = ( * app - > activity - > vm ) - > AttachCurrentThread ( app - > activity - > vm , & jni , NULL ) )
{
jmethodID func = ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , act ) , " updateScreenKeepOn " , " (Z)V " ) ;
if ( func )
( * jni ) - > CallVoidMethod ( jni , act , func , keepon ) ;
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
( * app - > activity - > vm ) - > DetachCurrentThread ( app - > activity - > vm ) ;
}
2017-10-12 12:02:25 +00:00
}
2019-04-15 18:43:42 +00:00
static void setCursorVisibility ( struct android_app * app , jboolean visible )
{ //this is meant to use the nvidia-added setCursorVisibility function
//but its fatal if it doesn't exist, and it doesn't seem to exist.
#if 0
jobject act = app - > activity - > clazz ;
JNIEnv * jni ;
if ( JNI_OK = = ( * app - > activity - > vm ) - > AttachCurrentThread ( app - > activity - > vm , & jni , NULL ) )
{
jobject inputManager = NULL ;
jmethodID setvis = NULL ;
jmethodID func = ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , act ) , " getSystemService " , " (Ljava/lang/String;)Ljava/lang/Object; " ) ;
if ( func )
inputManager = ( * jni ) - > CallObjectMethod ( jni , act , func , ( * jni ) - > NewStringUTF ( jni , " input " ) ) ;
if ( inputManager )
setvis = ( * jni ) - > GetMethodID ( jni , ( * jni ) - > GetObjectClass ( jni , inputManager ) , " setCursorVisibility " , " (Z)V " ) ;
if ( setvis )
( * jni ) - > CallVoidMethod ( jni , inputManager , setvis , visible ) ;
( * app - > activity - > vm ) - > DetachCurrentThread ( app - > activity - > vm ) ;
}
# endif
}
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
static void FTENativeActivity_keypress ( JNIEnv * env , jobject this , jint devid , jboolean down , jint keycode , jint unicode )
{
int qkeycode = mapkey ( keycode ) ;
// Sys_Printf("FTENativeActivity_keypress: d=%i s=%i a=%i,q=%i u=%i\n", devid, down, keycode, qkeycode, unicode);
IN_KeyEvent ( devid , down , qkeycode , unicode ) ;
}
static jboolean FTENativeActivity_wantrelative ( JNIEnv * env , jobject this )
{
if ( ! in_windowed_mouse . ival ) //emulators etc have no grabs so we're effectively always windowed in such situations.
return false ;
return ! Key_MouseShouldBeFree ( ) ;
}
static void FTENativeActivity_mousepress ( JNIEnv * env , jobject this , jint devid , jint buttonbits )
{
static int heldbuttons ;
jint changed = buttonbits ^ heldbuttons ;
// Sys_Printf("FTENativeActivity_mousepress: d=%i bits=%x (changed=%x)\n", devid, buttonbits, changed);
static int qbutton [ ] = {
K_MOUSE1 , //primary
K_MOUSE2 , //secondary
K_MOUSE3 , //tertiary
K_MOUSE4 , //back
K_MOUSE5 , //forward
K_MOUSE1 , //stylus_primary
K_MOUSE2 , //stylus_secondary
} ;
size_t i ;
heldbuttons = buttonbits ;
if ( changed )
for ( i = 0 ; i < countof ( qbutton ) ; i + + )
{
if ( changed & ( 1 < < i ) )
IN_KeyEvent ( devid , buttonbits & ( 1 < < i ) , qbutton [ i ] , 0 ) ;
}
}
static void FTENativeActivity_motion ( JNIEnv * env , jobject this , jint ptrid , jint act , jfloat x , jfloat y , jfloat z , jfloat size )
{
// Sys_Printf("FTENativeActivity_motion: d=%i a=%i x=%f y=%f z=%f s=%f\n", ptrid, act, x, y, z, size);
switch ( act )
{
case 2 : //mouse down
case 3 : //mouse up
IN_KeyEvent ( ptrid , act = = 2 , K_MOUSE1 , 0 ) ;
break ;
case 1 : //relative motion
case 0 : //absolute motion (android sucks)
IN_MouseMove ( ptrid , act = = 0 , x , y , z , size ) ;
break ;
} ;
}
static void FTENativeActivity_axis ( JNIEnv * env , jobject this , jint devid , jint axis , jfloat value )
{
IN_JoystickAxisEvent ( devid , axis , value ) ;
}
//static void FTENativeActivity_accelerometer(JNIEnv *env, jobject obj, jint devid, jfloat x, jfloat y, jfloat z)
//{
// IN_Accelerometer(devid, x, y, z);
//}
//static void FTENativeActivity_gryoscope(JNIEnv *env, jobject obj, jint devid, jfloat pitch, jfloat yaw, jfloat roll)
//{
// IN_Gyroscope(devid, pitch, yaw, roll);
//}
static JNINativeMethod methods [ ] = {
{ " keypress " , " (IZII)V " , FTENativeActivity_keypress } ,
{ " mousepress " , " (II)V " , FTENativeActivity_mousepress } ,
{ " motion " , " (IIFFFF)V " , FTENativeActivity_motion } ,
{ " wantrelative " , " ()Z " , FTENativeActivity_wantrelative } , //so the java code knows if it should use (often buggy) relative mouse movement or (limited) abs cursor coords.
{ " axis " , " (IIF)V " , FTENativeActivity_axis } ,
// {"accelerometer", "(IFFF)V", FTENativeActivity_accelerometer},
// {"gyroscope", "(IFFF)V", FTENativeActivity_gyroscope},
} ;
JNIEXPORT jint JNICALL JNI_OnLoad ( JavaVM * vm , void * reserved )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
JNIEnv * jni ;
if ( JNI_OK = = ( * vm ) - > GetEnv ( vm , ( void * * ) & jni , JNI_VERSION_1_2 ) )
{
jclass naclass = ( * jni ) - > FindClass ( jni , " com.fteqw.FTENativeActivity " ) ;
if ( naclass )
{
( * jni ) - > RegisterNatives ( jni , naclass , methods , countof ( methods ) ) ;
return JNI_VERSION_1_2 ;
}
}
return - 1 ;
}
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
void android_main ( struct android_app * state )
{
static pthread_mutex_t onemainthread = PTHREAD_MUTEX_INITIALIZER ; //android likes spawning multiple 'main' threads
int osk = 0 , wantgrabs = 0 , t ;
double ltime , ctime , tdelta ;
pthread_mutex_lock ( & onemainthread ) ;
android_app_state = state ;
state - > userData = NULL ;
state - > onAppCmd = engine_handle_cmd ;
state - > onInputEvent = engine_handle_input ;
r_forceheadless = true ;
sys_nativewindow = NULL ;
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
if ( ! host_initialized )
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
static const char * args [ ] =
2017-10-12 12:02:25 +00:00
{
2019-04-15 18:43:42 +00:00
" ftedroid " , /*binary name, not really meaningful*/
2017-10-12 12:02:25 +00:00
" -basepack " ,
sys_basepak , /*filled in later*/
" " ,
" "
} ;
2019-04-15 18:43:42 +00:00
static quakeparms_t parms ;
2017-10-12 12:02:25 +00:00
if ( sys_memheap )
free ( sys_memheap ) ;
memset ( & parms , 0 , sizeof ( parms ) ) ;
parms . basedir = sys_basedir ; /*filled in later*/
2019-04-15 18:43:42 +00:00
parms . argc = read_apk_path ( state , sys_basepak , sizeof ( sys_basepak ) ) ? 3 : 1 ;
2017-10-12 12:02:25 +00:00
parms . argv = args ;
2017-04-18 11:12:17 +00:00
# ifdef CONFIG_MANIFEST_TEXT
parms . manifest = CONFIG_MANIFEST_TEXT ;
2017-10-12 12:02:25 +00:00
# endif
2019-04-15 18:43:42 +00:00
sys_dpi_x = 72 ; //no idea
sys_dpi_y = 72 ; //no idea
#if 0 //google made this a fucking pain.
Q_strncpyz ( sys_basedir , getenv ( " EXTERNAL_STORAGE " ) , sizeof ( sys_basedir ) ) ;
Q_strncatz ( sys_basedir , " /fte " , sizeof ( sys_basedir ) ) ;
# else //so now users have to use some big long path to install stuff instead
Q_strncatz ( sys_basedir , state - > activity - > externalDataPath , sizeof ( sys_basedir ) ) ;
# endif
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
Sys_Printf ( " Starting up (apk=%s, usr=%s) \n " , sys_basepak , parms . basedir ) ;
2017-10-12 12:02:25 +00:00
VID_Register ( ) ;
COM_InitArgv ( parms . argc , parms . argv ) ;
TL_InitLanguages ( sys_basedir ) ;
2019-04-15 18:43:42 +00:00
Host_Init ( & parms ) ;
Sys_Printf ( " Host Inited \n " ) ;
}
else
Sys_Printf ( " Restarting up! \n " ) ;
ltime = Sys_DoubleTime ( ) ;
sys_orientation . modified = false ;
updateOrientation ( state , sys_orientation . string ) ;
sys_keepscreenon . modified = false ;
updateScreenKeepOn ( state , sys_keepscreenon . ival ) ;
run_intent_url ( state ) ;
if ( state - > savedState ! = NULL )
{ //oh look, we're pretending to already be running...
//oh.
}
for ( ; ; )
{
int ident , events ;
struct android_poll_source * source ;
while ( ( ident = ALooper_pollAll ( vid . activeapp ? 0 : 250 , NULL , & events , ( void * * ) & source ) ) > = 0 )
{
if ( source ! = NULL )
source - > process ( state , source ) ;
//FIXME: sensor crap
if ( state - > destroyRequested ! = 0 )
{
Sys_Printf ( " Shutdown requested \n " ) ;
Host_Shutdown ( ) ;
pthread_mutex_unlock ( & onemainthread ) ;
return ;
}
}
if ( host_initialized )
{
if ( r_forcevidrestart )
{
if ( qrenderer )
R_RestartRenderer_f ( ) ;
r_forcevidrestart = false ;
}
if ( sys_nativewindow )
{
ctime = Sys_DoubleTime ( ) ;
tdelta = ctime - ltime ;
ltime = ctime ;
Host_Frame ( tdelta ) ;
}
}
t = 0 ;
if ( Key_Dest_Has ( kdm_console | kdm_message ) )
t | = ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT ;
if ( ! Key_Dest_Has ( ~ kdm_game ) & & cls . state = = ca_disconnected )
t | = ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT ;
if ( sys_osk . ival )
t | = ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED ;
if ( osk ! = t )
2018-01-24 20:19:15 +00:00
{
2019-04-15 18:43:42 +00:00
setsoftkeyboard ( state , t ) ;
osk = t ;
}
if ( sys_orientation . modified )
{
sys_orientation . modified = false ;
updateOrientation ( state , sys_orientation . string ) ;
}
if ( sys_keepscreenon . modified )
{
sys_keepscreenon . modified = false ;
updateScreenKeepOn ( state , sys_keepscreenon . ival ) ;
2018-01-24 20:19:15 +00:00
}
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
t = FTENativeActivity_wantrelative ( NULL , NULL ) ;
if ( wantgrabs ! = t )
{
wantgrabs = t ;
setCursorVisibility ( state , wantgrabs ) ;
}
2017-10-12 12:02:25 +00:00
}
}
static int secbase ;
# ifdef _POSIX_TIMERS
double Sys_DoubleTime ( void )
{
struct timespec ts ;
clock_gettime ( CLOCK_MONOTONIC , & ts ) ;
if ( ! secbase )
{
secbase = ts . tv_sec ;
return ts . tv_nsec / 1000000000.0 ;
}
return ( ts . tv_sec - secbase ) + ts . tv_nsec / 1000000000.0 ;
}
unsigned int Sys_Milliseconds ( void )
{
struct timespec ts ;
clock_gettime ( CLOCK_MONOTONIC , & ts ) ;
if ( ! secbase )
{
secbase = ts . tv_sec ;
return ts . tv_nsec / 1000000 ;
}
return ( ts . tv_sec - secbase ) * 1000 + ts . tv_nsec / 1000000 ;
}
# else
double Sys_DoubleTime ( void )
{
struct timeval tp ;
struct timezone tzp ;
gettimeofday ( & tp , & tzp ) ;
if ( ! secbase )
{
secbase = tp . tv_sec ;
return tp . tv_usec / 1000000.0 ;
}
return ( tp . tv_sec - secbase ) + tp . tv_usec / 1000000.0 ;
}
unsigned int Sys_Milliseconds ( void )
{
struct timeval tp ;
struct timezone tzp ;
gettimeofday ( & tp , & tzp ) ;
if ( ! secbase )
{
secbase = tp . tv_sec ;
return tp . tv_usec / 1000 ;
}
return ( tp . tv_sec - secbase ) * 1000 + tp . tv_usec / 1000 ;
}
# endif
void Sys_Shutdown ( void )
{
free ( sys_memheap ) ;
}
void Sys_Quit ( void )
{
# ifndef SERVERONLY
Host_Shutdown ( ) ;
# else
SV_Shutdown ( ) ;
# endif
LOGI ( " %s " , " quitting " ) ;
2019-04-15 18:43:42 +00:00
showMessageAndQuit ( android_app_state , " " ) ;
2017-10-12 12:02:25 +00:00
longjmp ( host_abort , 1 ) ;
exit ( 0 ) ;
}
void Sys_Error ( const char * error , . . . )
{
va_list argptr ;
char string [ 1024 ] ;
va_start ( argptr , error ) ;
vsnprintf ( string , sizeof ( string ) - 1 , error , argptr ) ;
va_end ( argptr ) ;
2019-03-12 06:41:52 +00:00
COM_WorkerAbort ( string ) ;
2017-10-12 12:02:25 +00:00
if ( ! * string )
strcpy ( string , " no error " ) ;
LOGE ( " e: %s " , string ) ;
2019-04-15 18:43:42 +00:00
showMessageAndQuit ( android_app_state , string ) ;
2017-10-12 12:02:25 +00:00
2019-04-15 18:43:42 +00:00
host_initialized = false ; //don't keep calling Host_Frame, because it'll screw stuff up more. Can't trust Host_Shutdown either. :(
vid . activeapp = false ; //make sure we don't busyloop.
2017-10-12 12:02:25 +00:00
longjmp ( host_abort , 1 ) ;
exit ( 1 ) ;
}
void Sys_Printf ( char * fmt , . . . )
{
va_list argptr ;
2018-01-24 20:19:15 +00:00
char * e ;
2019-04-15 18:43:42 +00:00
//android doesn't do \ns properly *sigh*
//this means we have to buffer+split it ourselves.
//and because of lots of threads, we have to mutex it too.
static char linebuf [ 2048 ] ;
static char * endbuf = linebuf ;
2018-01-24 20:19:15 +00:00
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER ;
pthread_mutex_lock ( & lock ) ;
2017-10-12 12:02:25 +00:00
2018-01-24 20:19:15 +00:00
//append the new data
2017-10-12 12:02:25 +00:00
va_start ( argptr , fmt ) ;
vsnprintf ( endbuf , sizeof ( linebuf ) - ( endbuf - linebuf ) - 1 , fmt , argptr ) ;
va_end ( argptr ) ;
endbuf + = strlen ( endbuf ) ;
2018-01-24 20:19:15 +00:00
//split it on linebreaks
2017-10-12 12:02:25 +00:00
while ( ( e = strchr ( linebuf , ' \n ' ) ) )
{
* e = 0 ;
LOGI ( " %s " , linebuf ) ;
memmove ( linebuf , e + 1 , endbuf - ( e + 1 ) ) ;
linebuf [ endbuf - ( e + 1 ) ] = 0 ;
endbuf - = ( e + 1 ) - linebuf ;
}
2018-01-24 20:19:15 +00:00
pthread_mutex_unlock ( & lock ) ;
2017-10-12 12:02:25 +00:00
}
void Sys_Warn ( char * fmt , . . . )
{
va_list argptr ;
char string [ 1024 ] ;
va_start ( argptr , fmt ) ;
vsnprintf ( string , sizeof ( string ) - 1 , fmt , argptr ) ;
va_end ( argptr ) ;
LOGW ( " w: %s " , string ) ;
}
void Sys_CloseLibrary ( dllhandle_t * lib )
{
if ( lib )
dlclose ( lib ) ;
}
void * Sys_GetAddressForName ( dllhandle_t * module , const char * exportname )
{
return dlsym ( module , exportname ) ;
}
dllhandle_t * Sys_LoadLibrary ( const char * name , dllfunction_t * funcs )
{
size_t i ;
dllhandle_t * h ;
h = dlopen ( va ( " %s.so " , name ) , RTLD_LAZY | RTLD_LOCAL ) ;
if ( ! h )
h = dlopen ( name , RTLD_LAZY | RTLD_LOCAL ) ;
if ( h & & funcs )
{
for ( i = 0 ; funcs [ i ] . name ; i + + )
{
* funcs [ i ] . funcptr = dlsym ( h , funcs [ i ] . name ) ;
if ( ! * funcs [ i ] . funcptr )
break ;
}
if ( funcs [ i ] . name )
{
Sys_CloseLibrary ( h ) ;
h = NULL ;
}
}
return h ;
}
char * Sys_ConsoleInput ( void )
{
return NULL ;
}
void Sys_mkdir ( const char * path ) //not all pre-unix systems have directories (including dos 1)
{
mkdir ( path , 0755 ) ;
}
playdemo accepts https urls now. will start playing before the file has finished downloading, to avoid unnecessary delays.
reworked network addresses to separate address family and connection type. this should make banning people more reliable, as well as simplifying a whole load of logic (no need to check for ipv4 AND ipv6).
tcpconnect will keep trying to connect even if the connection wasn't instant, instead of giving up instantly.
rewrote tcp connections quite a bit. sv_port_tcp now handles qtv+qizmo+http+ws+rtcbroker+tls equivalents.
qtv_streamport is now a legacy cvar and now acts equivalently to sv_port_tcp (but still separate).
rewrote screenshot and video capture code to use strides. this solves image-is-upside down issues with vulkan.
ignore alt key in browser port. oh no! no more red text! oh no! no more alt-being-wrongly-down-and-being-unable-to-type-anything-without-forcing-alt-released!
reworked audio decoder interface. now has clearly defined success/unavailable/end-of-file results. this should solve a whole load of issues with audio streaming.
fixed various openal audio streaming issues too. openal also got some workarounds for emscripten's poor emulation.
fixed ogg decoder to retain sync properly if seeked.
updated menu_media a bit. now reads vorbis comments/id3v1 tags to get proper track names. also saves the playlist so you don't have to manually repopulate the list so it might actually be usable now (after how many years?)
r_stains now defaults to 0, and is no longer enabled by presets. use decals if you want that sort of thing.
added fs_noreexec cvar, so configs will not be reexeced on gamedir change. this also means defaults won't be reapplied, etc.
added 'nvvk' renderer on windows, using nvidia's vulkan-inside-opengl gl extension. mostly just to see how much slower it is.
fixed up the ftp server quite a lot. more complete, more compliant, and should do ipv6 properly to-boot. file transfers also threaded.
fixed potential crash inside runclientphys.
experimental sv_antilag=3 setting. totally untested. the aim is to avoid missing due to lagged knockbacks. may be expensive for the server.
browser port's websockets support fixed. experimental support for webrtc ('works for me', requires a broker server).
updated avplug(renamed to ffmpeg so people know what it is) to use ffmpeg 3.2.4 properly, with its new encoder api. should be much more robust... also added experimental audio decoder for game music etc (currently doesn't resample, so playback rates are screwed, disabled by cvar).
git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5097 fc73d0e0-1445-4013-8a0c-d673dee63da5
2017-05-10 02:08:58 +00:00
qboolean Sys_rmdir ( const char * path )
{
if ( rmdir ( path ) = = 0 )
return true ;
if ( errno = = ENOENT )
return true ;
return false ;
2017-10-12 12:02:25 +00:00
}
qboolean Sys_remove ( const char * path )
{
return ! unlink ( path ) ;
}
qboolean Sys_Rename ( const char * oldfname , const char * newfname )
{
return ! rename ( oldfname , newfname ) ;
}
void Sys_SendKeyEvents ( void )
{
}
void Sys_Init ( void )
{
Cvar_Register ( & sys_keepscreenon , " android stuff " ) ;
Cvar_Register ( & sys_orientation , " android stuff " ) ;
2019-04-15 18:43:42 +00:00
Cvar_Register ( & sys_osk , " android stuff " ) ;
2017-10-12 12:02:25 +00:00
}
qboolean Sys_GetDesktopParameters ( int * width , int * height , int * bpp , int * refreshrate )
{
* width = 320 ;
* height = 240 ;
* bpp = 16 ;
* refreshrate = 60 ;
return false ;
}
qboolean Sys_RandomBytes ( qbyte * string , int len )
{
qboolean res = false ;
int fd = open ( " /dev/urandom " , 0 ) ;
if ( fd > = 0 )
{
res = ( read ( fd , string , len ) = = len ) ;
close ( fd ) ;
}
return res ;
}
void Sys_ServerActivity ( void )
{
/*FIXME: flash window*/
}
2019-03-23 07:06:37 +00:00
# ifdef WEBCLIENT
qboolean Sys_RunInstaller ( void )
{ //not implemented
return false ;
}
# endif
2017-10-12 12:02:25 +00:00
# ifndef MULTITHREAD
void Sys_Sleep ( double seconds )
{
struct timespec ts ;
ts . tv_sec = ( time_t ) seconds ;
seconds - = ts . tv_sec ;
ts . tv_nsec = seconds * 1000000000.0 ;
nanosleep ( & ts , NULL ) ;
}
# endif
qboolean Sys_InitTerminal ( void )
{
/*switching to dedicated mode, show text window*/
return false ;
}
void Sys_CloseTerminal ( void )
{
}
2018-10-11 10:31:23 +00:00
# define SYS_CLIPBOARD_SIZE 256
2017-10-12 12:02:25 +00:00
static char clipboard_buffer [ SYS_CLIPBOARD_SIZE ] = { 0 } ;
2018-10-11 10:31:23 +00:00
void Sys_Clipboard_PasteText ( clipboardtype_t cbt , void ( * callback ) ( void * cb , char * utf8 ) , void * ctx )
2017-10-12 12:02:25 +00:00
{
2018-10-11 10:31:23 +00:00
callback ( ctx , clipboard_buffer ) ;
2017-10-12 12:02:25 +00:00
}
2018-12-06 04:55:35 +00:00
void Sys_SaveClipboard ( clipboardtype_t cbt , const char * text )
2017-10-12 12:02:25 +00:00
{
2018-10-11 10:31:23 +00:00
Q_strncpyz ( clipboard_buffer , text , SYS_CLIPBOARD_SIZE ) ;
2017-10-12 12:02:25 +00:00
}
int Sys_EnumerateFiles ( const char * gpath , const char * match , int ( * func ) ( const char * , qofs_t , time_t mtime , void * , searchpathfuncs_t * ) , void * parm , searchpathfuncs_t * spath )
{
DIR * dir ;
char apath [ MAX_OSPATH ] ;
char file [ MAX_OSPATH ] ;
char truepath [ MAX_OSPATH ] ;
char * s ;
struct dirent * ent ;
struct stat st ;
//printf("path = %s\n", gpath);
//printf("match = %s\n", match);
if ( ! gpath )
gpath = " " ;
* apath = ' \0 ' ;
Q_strncpyz ( apath , match , sizeof ( apath ) ) ;
for ( s = apath + strlen ( apath ) - 1 ; s > = apath ; s - - )
{
if ( * s = = ' / ' )
{
s [ 1 ] = ' \0 ' ;
match + = s - apath + 1 ;
break ;
}
}
if ( s < apath ) //didn't find a '/'
* apath = ' \0 ' ;
Q_snprintfz ( truepath , sizeof ( truepath ) , " %s/%s " , gpath , apath ) ;
//printf("truepath = %s\n", truepath);
//printf("gamepath = %s\n", gpath);
//printf("apppath = %s\n", apath);
//printf("match = %s\n", match);
dir = opendir ( truepath ) ;
if ( ! dir )
{
Con_DPrintf ( " Failed to open dir %s \n " , truepath ) ;
return true ;
}
do
{
ent = readdir ( dir ) ;
if ( ! ent )
break ;
if ( * ent - > d_name ! = ' . ' )
{
if ( wildcmp ( match , ent - > d_name ) )
{
Q_snprintfz ( file , sizeof ( file ) , " %s/%s " , truepath , ent - > d_name ) ;
if ( stat ( file , & st ) = = 0 )
{
Q_snprintfz ( file , sizeof ( file ) , " %s%s%s " , apath , ent - > d_name , S_ISDIR ( st . st_mode ) ? " / " : " " ) ;
if ( ! func ( file , st . st_size , st . st_mtime , parm , spath ) )
{
closedir ( dir ) ;
return false ;
}
}
else
printf ( " Stat failed for \" %s \" \n " , file ) ;
}
}
} while ( 1 ) ;
closedir ( dir ) ;
return true ;
}