Add SDL_GetDisplayDPI implementation on Android. (thanks Rachel!)
authorSam Lantinga <slouken@libsdl.org>
Tue, 31 Oct 2017 13:49:59 -0700
changeset 11662a996f135cc81
parent 11661 bd1495ebc979
child 11663 0d96acbd34f0
Add SDL_GetDisplayDPI implementation on Android. (thanks Rachel!)
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/core/android/SDL_android.h
src/video/android/SDL_androidvideo.c
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Tue Oct 24 13:21:11 2017 -0400
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Tue Oct 31 13:49:59 2017 -0700
     1.3 @@ -18,6 +18,7 @@
     1.4  import android.widget.LinearLayout;
     1.5  import android.widget.TextView;
     1.6  import android.os.*;
     1.7 +import android.util.DisplayMetrics;
     1.8  import android.util.Log;
     1.9  import android.util.SparseArray;
    1.10  import android.graphics.*;
    1.11 @@ -613,6 +614,10 @@
    1.12          return SDL.getContext();
    1.13      }
    1.14  
    1.15 +    public static DisplayMetrics getDisplayDPI() {
    1.16 +        return getContext().getResources().getDisplayMetrics();
    1.17 +    }
    1.18 +
    1.19      /**
    1.20       * This method is called by SDL using JNI.
    1.21       */
     2.1 --- a/src/core/android/SDL_android.c	Tue Oct 24 13:21:11 2017 -0400
     2.2 +++ b/src/core/android/SDL_android.c	Tue Oct 31 13:49:59 2017 -0700
     2.3 @@ -214,6 +214,7 @@
     2.4  static jmethodID midClipboardHasText;
     2.5  static jmethodID midOpenAPKExpansionInputStream;
     2.6  static jmethodID midGetManifestEnvironmentVariable;
     2.7 +static jmethodID midGetDisplayDPI;
     2.8  
     2.9  /* audio manager */
    2.10  static jclass mAudioManagerClass;
    2.11 @@ -316,11 +317,13 @@
    2.12      midGetManifestEnvironmentVariable = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.13                                  "getManifestEnvironmentVariable", "(Ljava/lang/String;)Ljava/lang/String;");
    2.14  
    2.15 +    midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
    2.16 +
    2.17      if (!midGetNativeSurface ||
    2.18         !midSetActivityTitle || !midSetOrientation || !midGetContext || !midInputGetInputDeviceIds ||
    2.19         !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown || 
    2.20         !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
    2.21 -       !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariable) {
    2.22 +       !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariable || !midGetDisplayDPI) {
    2.23          __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
    2.24      }
    2.25  
    2.26 @@ -1047,6 +1050,28 @@
    2.27      return audioBufferFrames;
    2.28  }
    2.29  
    2.30 +int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi)
    2.31 +{
    2.32 +    JNIEnv *env = Android_JNI_GetEnv();
    2.33 +
    2.34 +    jobject jDisplayObj = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetDisplayDPI);
    2.35 +    jclass jDisplayClass = (*env)->GetObjectClass(env, jDisplayObj);
    2.36 +
    2.37 +    jfieldID fidXdpi = (*env)->GetFieldID(env, jDisplayClass, "xdpi", "F");
    2.38 +    jfieldID fidYdpi = (*env)->GetFieldID(env, jDisplayClass, "ydpi", "F");
    2.39 +    jfieldID fidDdpi = (*env)->GetFieldID(env, jDisplayClass, "densityDpi", "I");
    2.40 +
    2.41 +    float nativeXdpi = (*env)->GetFloatField(env, jDisplayObj, fidXdpi);
    2.42 +    float nativeYdpi = (*env)->GetFloatField(env, jDisplayObj, fidYdpi);
    2.43 +    int nativeDdpi = (*env)->GetIntField(env, jDisplayObj, fidDdpi);
    2.44 +
    2.45 +    *ddpi = (float)nativeDdpi;
    2.46 +    *xdpi = nativeXdpi;
    2.47 +    *ydpi = nativeYdpi;
    2.48 +
    2.49 +    return 0;
    2.50 +}
    2.51 +
    2.52  void * Android_JNI_GetAudioBuffer(void)
    2.53  {
    2.54      return audioBufferPinned;
     3.1 --- a/src/core/android/SDL_android.h	Tue Oct 24 13:21:11 2017 -0400
     3.2 +++ b/src/core/android/SDL_android.h	Tue Oct 31 13:49:59 2017 -0700
     3.3 @@ -42,6 +42,8 @@
     3.4  extern SDL_bool Android_JNI_IsScreenKeyboardShown(void);
     3.5  extern ANativeWindow* Android_JNI_GetNativeWindow(void);
     3.6  
     3.7 +extern int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi);
     3.8 +
     3.9  /* Audio support */
    3.10  extern int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
    3.11  extern void* Android_JNI_GetAudioBuffer(void);
     4.1 --- a/src/video/android/SDL_androidvideo.c	Tue Oct 24 13:21:11 2017 -0400
     4.2 +++ b/src/video/android/SDL_androidvideo.c	Tue Oct 31 13:49:59 2017 -0700
     4.3 @@ -47,6 +47,7 @@
     4.4  /* Initialization/Query functions */
     4.5  static int Android_VideoInit(_THIS);
     4.6  static void Android_VideoQuit(_THIS);
     4.7 +int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi);
     4.8  
     4.9  #include "../SDL_egl_c.h"
    4.10  #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
    4.11 @@ -115,6 +116,8 @@
    4.12      device->VideoQuit = Android_VideoQuit;
    4.13      device->PumpEvents = Android_PumpEvents;
    4.14  
    4.15 +    device->GetDisplayDPI = Android_GetDisplayDPI;
    4.16 +
    4.17      device->CreateSDLWindow = Android_CreateWindow;
    4.18      device->SetWindowTitle = Android_SetWindowTitle;
    4.19      device->DestroyWindow = Android_DestroyWindow;
    4.20 @@ -198,6 +201,12 @@
    4.21      Android_QuitTouch();
    4.22  }
    4.23  
    4.24 +int
    4.25 +Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
    4.26 +{
    4.27 +    return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
    4.28 +}
    4.29 +
    4.30  void
    4.31  Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
    4.32  {