Skip to content

Commit

Permalink
Add SDL_IsTablet() to Android and iOS SDL.
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Aug 21, 2018
1 parent b09b25f commit 109544c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
18 changes: 18 additions & 0 deletions android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.Hashtable;
import java.lang.reflect.Method;
import java.lang.Math;

import android.app.*;
import android.content.*;
Expand Down Expand Up @@ -776,6 +777,23 @@ public static boolean isAndroidTV() {
return false;
}

/**
* This method is called by SDL using JNI.
*/
public static boolean isTablet() {
DisplayMetrics metrics = new DisplayMetrics();
Activity sdlActivity = (Activity)getContext();
sdlActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

double dWidthInches = metrics.widthPixels / (double)metrics.densityDpi;
double dHeightInches = metrics.heightPixels / (double)metrics.densityDpi;

double dDiagonal = Math.sqrt((dWidthInches * dWidthInches) + (dHeightInches * dHeightInches));

// If our diagonal size is seven inches or greater, we consider ourselves a tablet.
return (dDiagonal > 7.0);
}

/**
* This method is called by SDL using JNI.
*/
Expand Down
9 changes: 9 additions & 0 deletions include/SDL_system.h
Expand Up @@ -174,6 +174,15 @@ extern DECLSPEC const char * SDLCALL SDL_AndroidGetExternalStoragePath(void);

#endif /* __ANDROID__ */

#if defined(__ANDROID__) || defined(__IPHONEOS__)

/**
\brief Return true if the current device is a tablet.
*/
extern DECLSPEC SDL_bool SDLCALL SDL_IsTablet(void);

#endif

/* Platform specific functions for WinRT */
#if defined(__WINRT__) && __WINRT__

Expand Down
11 changes: 10 additions & 1 deletion src/core/android/SDL_android.c
Expand Up @@ -232,6 +232,7 @@ static jmethodID midSetCustomCursor;
static jmethodID midSetSystemCursor;
static jmethodID midSupportsRelativeMouse;
static jmethodID midSetRelativeMouseEnabled;
static jmethodID midIsTablet;

/* audio manager */
static jclass mAudioManagerClass;
Expand Down Expand Up @@ -354,13 +355,15 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
midSupportsRelativeMouse = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "supportsRelativeMouse", "()Z");
midSetRelativeMouseEnabled = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setRelativeMouseEnabled", "(Z)Z");

midIsTablet = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "isTablet", "()Z");

if (!midGetNativeSurface ||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
!midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
!midCreateCustomCursor || !midSetCustomCursor || !midSetSystemCursor || !midSupportsRelativeMouse || !midSetRelativeMouseEnabled ||
!midIsChromebook || !midIsDeXMode || !midManualBackButton) {
!midIsChromebook || !midIsDeXMode || !midManualBackButton || !midIsTablet) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
}

Expand Down Expand Up @@ -2048,6 +2051,12 @@ SDL_bool SDL_IsDeXMode(void)
return (*env)->CallStaticBooleanMethod(env, mActivityClass, midIsDeXMode);
}

SDL_bool SDL_IsTablet(void)
{
JNIEnv *env = Android_JNI_GetEnv();
return (*env)->CallStaticBooleanMethod(env, mActivityClass, midIsTablet);
}

void SDL_AndroidBackButton(void)
{
JNIEnv *env = Android_JNI_GetEnv();
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Expand Up @@ -680,3 +680,4 @@
#define SDL_wcsdup SDL_wcsdup_REAL
#define SDL_GameControllerRumble SDL_GameControllerRumble_REAL
#define SDL_JoystickRumble SDL_JoystickRumble_REAL
#define SDL_IsTablet SDL_IsTablet_REAL
3 changes: 3 additions & 0 deletions src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -722,3 +722,6 @@ SDL_DYNAPI_PROC(float,SDL_expf,(float a),(a),return)
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsdup,(const wchar_t *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GameControllerRumble,(SDL_GameController *a, Uint16 b, Uint16 c, Uint32 d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_JoystickRumble,(SDL_Joystick *a, Uint16 b, Uint16 c, Uint32 d),(a,b,c,d),return)
#if defined(__ANDROID__) || defined(__IPHONEOS__)
SDL_DYNAPI_PROC(SDL_bool,SDL_IsTablet,(void),(),return)
#endif
11 changes: 11 additions & 0 deletions src/video/uikit/SDL_uikitvideo.m
Expand Up @@ -233,6 +233,17 @@ void SDL_NSLog(const char *text)
NSLog(@"%s", text);
}

/*
* iOS Tablet detection
*
* This doesn't really have aything to do with the interfaces of the SDL video
* subsystem, but we need to stuff this into an Objective-C source code file.
*/
SDL_bool SDL_IsTablet(void)
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
}

#endif /* SDL_VIDEO_DRIVER_UIKIT */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 109544c

Please sign in to comment.