Add SDL_IsTablet() to Android and iOS SDL.
authorSam Lantinga <slouken@libsdl.org>
Tue, 21 Aug 2018 11:23:47 -0700
changeset 12127f92fd2c57780
parent 12126 70315808397f
child 12128 dfd9a3bda4e4
Add SDL_IsTablet() to Android and iOS SDL.
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
include/SDL_system.h
src/core/android/SDL_android.c
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/video/uikit/SDL_uikitvideo.m
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Tue Aug 21 11:07:56 2018 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Tue Aug 21 11:23:47 2018 -0700
     1.3 @@ -5,6 +5,7 @@
     1.4  import java.util.Arrays;
     1.5  import java.util.Hashtable;
     1.6  import java.lang.reflect.Method;
     1.7 +import java.lang.Math;
     1.8  
     1.9  import android.app.*;
    1.10  import android.content.*;
    1.11 @@ -779,6 +780,23 @@
    1.12      /**
    1.13       * This method is called by SDL using JNI.
    1.14       */
    1.15 +    public static boolean isTablet() {
    1.16 +        DisplayMetrics metrics = new DisplayMetrics();
    1.17 +        Activity sdlActivity = (Activity)getContext();
    1.18 +        sdlActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    1.19 +
    1.20 +        double dWidthInches = metrics.widthPixels / (double)metrics.densityDpi;
    1.21 +        double dHeightInches = metrics.heightPixels / (double)metrics.densityDpi;
    1.22 +
    1.23 +        double dDiagonal = Math.sqrt((dWidthInches * dWidthInches) + (dHeightInches * dHeightInches));
    1.24 +
    1.25 +        // If our diagonal size is seven inches or greater, we consider ourselves a tablet.
    1.26 +        return (dDiagonal > 7.0);
    1.27 +    }
    1.28 +
    1.29 +    /**
    1.30 +     * This method is called by SDL using JNI.
    1.31 +     */
    1.32      public static boolean isChromebook() {
    1.33          return getContext().getPackageManager().hasSystemFeature("org.chromium.arc.device_management");
    1.34      }    
     2.1 --- a/include/SDL_system.h	Tue Aug 21 11:07:56 2018 -0700
     2.2 +++ b/include/SDL_system.h	Tue Aug 21 11:23:47 2018 -0700
     2.3 @@ -174,6 +174,15 @@
     2.4  
     2.5  #endif /* __ANDROID__ */
     2.6  
     2.7 +#if defined(__ANDROID__) || defined(__IPHONEOS__)
     2.8 +
     2.9 +/**
    2.10 + \brief Return true if the current device is a tablet.
    2.11 + */
    2.12 +extern DECLSPEC SDL_bool SDLCALL SDL_IsTablet(void);
    2.13 +
    2.14 +#endif
    2.15 +
    2.16  /* Platform specific functions for WinRT */
    2.17  #if defined(__WINRT__) && __WINRT__
    2.18  
     3.1 --- a/src/core/android/SDL_android.c	Tue Aug 21 11:07:56 2018 -0700
     3.2 +++ b/src/core/android/SDL_android.c	Tue Aug 21 11:23:47 2018 -0700
     3.3 @@ -232,6 +232,7 @@
     3.4  static jmethodID midSetSystemCursor;
     3.5  static jmethodID midSupportsRelativeMouse;
     3.6  static jmethodID midSetRelativeMouseEnabled;
     3.7 +static jmethodID midIsTablet;
     3.8  
     3.9  /* audio manager */
    3.10  static jclass mAudioManagerClass;
    3.11 @@ -354,13 +355,15 @@
    3.12      midSupportsRelativeMouse = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "supportsRelativeMouse", "()Z");
    3.13      midSetRelativeMouseEnabled = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setRelativeMouseEnabled", "(Z)Z");
    3.14  
    3.15 +    midIsTablet = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "isTablet", "()Z");
    3.16 +
    3.17      if (!midGetNativeSurface ||
    3.18         !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
    3.19         !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
    3.20         !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
    3.21         !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
    3.22         !midCreateCustomCursor || !midSetCustomCursor || !midSetSystemCursor || !midSupportsRelativeMouse || !midSetRelativeMouseEnabled ||
    3.23 -       !midIsChromebook || !midIsDeXMode || !midManualBackButton) {
    3.24 +       !midIsChromebook || !midIsDeXMode || !midManualBackButton || !midIsTablet) {
    3.25          __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
    3.26      }
    3.27  
    3.28 @@ -2048,6 +2051,12 @@
    3.29      return (*env)->CallStaticBooleanMethod(env, mActivityClass, midIsDeXMode);
    3.30  }
    3.31  
    3.32 +SDL_bool SDL_IsTablet(void)
    3.33 +{
    3.34 +    JNIEnv *env = Android_JNI_GetEnv();
    3.35 +    return (*env)->CallStaticBooleanMethod(env, mActivityClass, midIsTablet);
    3.36 +}
    3.37 +
    3.38  void SDL_AndroidBackButton(void)
    3.39  {
    3.40      JNIEnv *env = Android_JNI_GetEnv();
     4.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Tue Aug 21 11:07:56 2018 -0700
     4.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Tue Aug 21 11:23:47 2018 -0700
     4.3 @@ -680,3 +680,4 @@
     4.4  #define SDL_wcsdup SDL_wcsdup_REAL
     4.5  #define SDL_GameControllerRumble SDL_GameControllerRumble_REAL
     4.6  #define SDL_JoystickRumble SDL_JoystickRumble_REAL
     4.7 +#define SDL_IsTablet SDL_IsTablet_REAL
     5.1 --- a/src/dynapi/SDL_dynapi_procs.h	Tue Aug 21 11:07:56 2018 -0700
     5.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Tue Aug 21 11:23:47 2018 -0700
     5.3 @@ -722,3 +722,6 @@
     5.4  SDL_DYNAPI_PROC(wchar_t*,SDL_wcsdup,(const wchar_t *a),(a),return)
     5.5  SDL_DYNAPI_PROC(int,SDL_GameControllerRumble,(SDL_GameController *a, Uint16 b, Uint16 c, Uint32 d),(a,b,c,d),return)
     5.6  SDL_DYNAPI_PROC(int,SDL_JoystickRumble,(SDL_Joystick *a, Uint16 b, Uint16 c, Uint32 d),(a,b,c,d),return)
     5.7 +#if defined(__ANDROID__) || defined(__IPHONEOS__)
     5.8 +SDL_DYNAPI_PROC(SDL_bool,SDL_IsTablet,(void),(),return)
     5.9 +#endif
     6.1 --- a/src/video/uikit/SDL_uikitvideo.m	Tue Aug 21 11:07:56 2018 -0700
     6.2 +++ b/src/video/uikit/SDL_uikitvideo.m	Tue Aug 21 11:23:47 2018 -0700
     6.3 @@ -233,6 +233,17 @@
     6.4      NSLog(@"%s", text);
     6.5  }
     6.6  
     6.7 +/*
     6.8 + * iOS Tablet detection
     6.9 + *
    6.10 + * This doesn't really have aything to do with the interfaces of the SDL video
    6.11 + *  subsystem, but we need to stuff this into an Objective-C source code file.
    6.12 + */
    6.13 +SDL_bool SDL_IsTablet(void)
    6.14 +{
    6.15 +    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
    6.16 +}
    6.17 +
    6.18  #endif /* SDL_VIDEO_DRIVER_UIKIT */
    6.19  
    6.20  /* vi: set ts=4 sw=4 expandtab: */