Add SDL_GetDisplayDPI implementation on Android. (thanks Rachel!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index 176f07a..ae0bc81 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -18,6 +18,7 @@ import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.*;
+import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.graphics.*;
@@ -613,6 +614,10 @@ public class SDLActivity extends Activity {
return SDL.getContext();
}
+ public static DisplayMetrics getDisplayDPI() {
+ return getContext().getResources().getDisplayMetrics();
+ }
+
/**
* This method is called by SDL using JNI.
*/
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 4fe6e8c..bfc1c71 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -214,6 +214,7 @@ static jmethodID midClipboardGetText;
static jmethodID midClipboardHasText;
static jmethodID midOpenAPKExpansionInputStream;
static jmethodID midGetManifestEnvironmentVariable;
+static jmethodID midGetDisplayDPI;
/* audio manager */
static jclass mAudioManagerClass;
@@ -316,11 +317,13 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
midGetManifestEnvironmentVariable = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"getManifestEnvironmentVariable", "(Ljava/lang/String;)Ljava/lang/String;");
+ midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
+
if (!midGetNativeSurface ||
!midSetActivityTitle || !midSetOrientation || !midGetContext || !midInputGetInputDeviceIds ||
!midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
!midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
- !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariable) {
+ !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariable || !midGetDisplayDPI) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
}
@@ -1047,6 +1050,28 @@ int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int
return audioBufferFrames;
}
+int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi)
+{
+ JNIEnv *env = Android_JNI_GetEnv();
+
+ jobject jDisplayObj = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetDisplayDPI);
+ jclass jDisplayClass = (*env)->GetObjectClass(env, jDisplayObj);
+
+ jfieldID fidXdpi = (*env)->GetFieldID(env, jDisplayClass, "xdpi", "F");
+ jfieldID fidYdpi = (*env)->GetFieldID(env, jDisplayClass, "ydpi", "F");
+ jfieldID fidDdpi = (*env)->GetFieldID(env, jDisplayClass, "densityDpi", "I");
+
+ float nativeXdpi = (*env)->GetFloatField(env, jDisplayObj, fidXdpi);
+ float nativeYdpi = (*env)->GetFloatField(env, jDisplayObj, fidYdpi);
+ int nativeDdpi = (*env)->GetIntField(env, jDisplayObj, fidDdpi);
+
+ *ddpi = (float)nativeDdpi;
+ *xdpi = nativeXdpi;
+ *ydpi = nativeYdpi;
+
+ return 0;
+}
+
void * Android_JNI_GetAudioBuffer(void)
{
return audioBufferPinned;
diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h
index 1d7e81a..06d40e2 100644
--- a/src/core/android/SDL_android.h
+++ b/src/core/android/SDL_android.h
@@ -42,6 +42,8 @@ extern void Android_JNI_HideTextInput(void);
extern SDL_bool Android_JNI_IsScreenKeyboardShown(void);
extern ANativeWindow* Android_JNI_GetNativeWindow(void);
+extern int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi);
+
/* Audio support */
extern int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
extern void* Android_JNI_GetAudioBuffer(void);
diff --git a/src/video/android/SDL_androidvideo.c b/src/video/android/SDL_androidvideo.c
index bd7b1ae..6ce8257 100644
--- a/src/video/android/SDL_androidvideo.c
+++ b/src/video/android/SDL_androidvideo.c
@@ -47,6 +47,7 @@
/* Initialization/Query functions */
static int Android_VideoInit(_THIS);
static void Android_VideoQuit(_THIS);
+int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi);
#include "../SDL_egl_c.h"
#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
@@ -115,6 +116,8 @@ Android_CreateDevice(int devindex)
device->VideoQuit = Android_VideoQuit;
device->PumpEvents = Android_PumpEvents;
+ device->GetDisplayDPI = Android_GetDisplayDPI;
+
device->CreateSDLWindow = Android_CreateWindow;
device->SetWindowTitle = Android_SetWindowTitle;
device->DestroyWindow = Android_DestroyWindow;
@@ -198,6 +201,12 @@ Android_VideoQuit(_THIS)
Android_QuitTouch();
}
+int
+Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
+{
+ return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
+}
+
void
Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
{