(Android) Move DPI metric code to dispserv_android.c - and remove

duplicate code
This commit is contained in:
libretroadmin 2023-06-20 08:31:19 +02:00
parent 16fcd1d62c
commit 1cff8e26f4
5 changed files with 78 additions and 107 deletions

View File

@ -204,33 +204,6 @@ int system_property_get(const char *command,
/* forward declaration */
bool android_run_events(void *data);
void android_dpi_get_density(char *s, size_t len)
{
static bool inited_once = false;
static bool inited2_once = false;
static char string[PROP_VALUE_MAX] = {0};
static char string2[PROP_VALUE_MAX] = {0};
if (!inited_once)
{
system_property_get("getprop", "ro.sf.lcd_density", string);
inited_once = true;
}
if (!string_is_empty(string))
{
strlcpy(s, string, len);
return;
}
if (!inited2_once)
{
system_property_get("wm", "density", string2);
inited2_once = true;
}
strlcpy(s, string2, len);
}
void android_app_write_cmd(struct android_app *android_app, int8_t cmd)
{
if (android_app)

View File

@ -361,8 +361,6 @@ extern JNIEnv *jni_thread_getenv(void);
void android_app_write_cmd(struct android_app *android_app, int8_t cmd);
void android_dpi_get_density(char *s, size_t len);
extern struct android_app *g_android;
#endif

View File

@ -19,6 +19,9 @@
#include "../video_display_server.h"
#include "../../frontend/drivers/platform_unix.h"
/* FORWARD DECLARATIONS */
int system_property_get(const char *cmd, const char *args, char *value);
static void* android_display_server_init(void) { return NULL; }
static void android_display_server_destroy(void *data) { }
static bool android_display_server_set_window_opacity(void *data, unsigned opacity) { return true; }
@ -38,6 +41,71 @@ static void android_display_server_set_screen_orientation(void *data,
g_android->setScreenOrientation, rotation);
}
static void android_display_dpi_get_density(char *s, size_t len)
{
static bool inited_once = false;
static bool inited2_once = false;
static char string[PROP_VALUE_MAX] = {0};
static char string2[PROP_VALUE_MAX] = {0};
if (!inited_once)
{
system_property_get("getprop", "ro.sf.lcd_density", string);
inited_once = true;
}
if (!string_is_empty(string))
{
strlcpy(s, string, len);
return;
}
if (!inited2_once)
{
system_property_get("wm", "density", string2);
inited2_once = true;
}
strlcpy(s, string2, len);
}
bool android_display_get_metrics(void *data,
enum display_metric_types type, float *value)
{
static int dpi = -1;
switch (type)
{
case DISPLAY_METRIC_MM_WIDTH:
case DISPLAY_METRIC_MM_HEIGHT:
return false;
case DISPLAY_METRIC_DPI:
if (dpi == -1)
{
char density[PROP_VALUE_MAX];
android_display_dpi_get_density(density, sizeof(density));
if (string_is_empty(density))
goto dpi_fallback;
if ((dpi = atoi(density)) <= 0)
goto dpi_fallback;
}
*value = (float)dpi;
break;
case DISPLAY_METRIC_NONE:
default:
*value = 0;
return false;
}
return true;
dpi_fallback:
/* add a fallback in case the device doesn't report DPI.
* Hopefully fixes issues with the moto G2. */
dpi = 90;
*value = (float)dpi;
return true;
}
const video_display_server_t dispserv_android = {
android_display_server_init,
android_display_server_destroy,

View File

@ -53,6 +53,10 @@ static enum gfx_ctx_api android_api = GFX_CTX_NONE;
static bool g_es3 = false;
#endif
/* FORWARD DECLARATION */
bool android_display_get_metrics(void *data,
enum display_metric_types type, float *value);
static void android_gfx_ctx_destroy(void *data)
{
android_ctx_data_t *and = (android_ctx_data_t*)data;
@ -246,44 +250,6 @@ static bool android_gfx_ctx_has_focus(void *data)
static bool android_gfx_ctx_suppress_screensaver(void *data, bool enable) { return false; }
static bool android_gfx_ctx_get_metrics(void *data,
enum display_metric_types type, float *value)
{
static int dpi = -1;
switch (type)
{
case DISPLAY_METRIC_MM_WIDTH:
case DISPLAY_METRIC_MM_HEIGHT:
return false;
case DISPLAY_METRIC_DPI:
if (dpi == -1)
{
char density[PROP_VALUE_MAX];
android_dpi_get_density(density, sizeof(density));
if (string_is_empty(density))
goto dpi_fallback;
if ((dpi = atoi(density)) <= 0)
goto dpi_fallback;
}
*value = (float)dpi;
break;
case DISPLAY_METRIC_NONE:
default:
*value = 0;
return false;
}
return true;
dpi_fallback:
/* add a fallback in case the device doesn't report DPI.
* Hopefully fixes issues with the moto G2. */
dpi = 90;
*value = (float)dpi;
return true;
}
static void android_gfx_ctx_swap_buffers(void *data)
{
#ifdef HAVE_EGL
@ -333,7 +299,7 @@ const gfx_ctx_driver_t gfx_ctx_android = {
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
android_gfx_ctx_get_metrics,
android_display_get_metrics,
NULL,
NULL, /* update_title */
android_gfx_ctx_check_window,

View File

@ -41,6 +41,10 @@ typedef struct
int swap_interval;
} android_ctx_data_vk_t;
/* FORWARD DECLARATION */
bool android_display_get_metrics(void *data,
enum display_metric_types type, float *value);
static void android_gfx_ctx_vk_destroy(void *data)
{
android_ctx_data_vk_t *and = (android_ctx_data_vk_t*)data;
@ -205,44 +209,6 @@ static bool android_gfx_ctx_vk_has_focus(void *data)
static bool android_gfx_ctx_vk_suppress_screensaver(void *data, bool enable) { return false; }
static bool android_gfx_ctx_vk_get_metrics(void *data,
enum display_metric_types type, float *value)
{
static int dpi = -1;
switch (type)
{
case DISPLAY_METRIC_MM_WIDTH:
case DISPLAY_METRIC_MM_HEIGHT:
return false;
case DISPLAY_METRIC_DPI:
if (dpi == -1)
{
char density[PROP_VALUE_MAX];
android_dpi_get_density(density, sizeof(density));
if (string_is_empty(density))
goto dpi_fallback;
if ((dpi = atoi(density)) <= 0)
goto dpi_fallback;
}
*value = (float)dpi;
break;
case DISPLAY_METRIC_NONE:
default:
*value = 0;
return false;
}
return true;
dpi_fallback:
/* Add a fallback in case the device doesn't report DPI.
* Hopefully fixes issues with the moto G2. */
dpi = 90;
*value = (float)dpi;
return true;
}
static void android_gfx_ctx_vk_swap_buffers(void *data)
{
android_ctx_data_vk_t *and = (android_ctx_data_vk_t*)data;
@ -307,7 +273,7 @@ const gfx_ctx_driver_t gfx_ctx_vk_android = {
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
android_gfx_ctx_vk_get_metrics,
android_display_get_metrics,
NULL,
NULL, /* update_title */
android_gfx_ctx_vk_check_window,