improve --help output and code style in main.c, use ARRAY_SIZE consistently

This commit is contained in:
Mis012 2024-09-15 00:42:55 +02:00
parent 4cb9cea3ba
commit 6560512b47
3 changed files with 104 additions and 150 deletions

View file

@ -31,7 +31,7 @@
#include "jni.h"
#include "../generated_headers/android_media_MediaCodec.h"
#include "../util.h"
#include "src/api-impl-jni/defines.h"
#include "../defines.h"
#include "../../libandroid/native_window.h"
struct ATL_codec_context {
@ -111,7 +111,7 @@ static uint32_t get_drm_frame_format(const AVDRMFrameDescriptor *drm_frame_desc)
if (drm_frame_desc->nb_layers == 1) {
return drm_frame_desc->layers[0].format;
}
for (size_t i = 0; i < sizeof(drm_format_map) / sizeof(drm_format_map[0]); i++) {
for (size_t i = 0; i < ARRAY_SIZE(drm_format_map); i++) {
if (drm_format_map[i].nb_layers != drm_frame_desc->nb_layers) {
continue;
}

View file

@ -604,8 +604,6 @@ PFN_vkVoidFunction bionic_vkGetInstanceProcAddr(VkInstance instance, const char
typedef XrResult(*xr_func)();
#define ARRRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
// avoid hard dependency on libopenxr_loader for the three functions that we only ever call when running a VR app
static void *openxr_loader_handle = NULL;
static inline __attribute__((__always_inline__)) XrResult xr_lazy_call(char *func_name, ...) {
@ -677,7 +675,7 @@ XrResult bionic_xrCreateInstance(XrInstanceCreateInfo *createInfo, XrInstance *i
const char * const*old_names = createInfo->enabledExtensionNames;
const char **new_names;
int new_count = createInfo->enabledExtensionCount + ARRRAY_SIZE(extra_exts);
int new_count = createInfo->enabledExtensionCount + ARRAY_SIZE(extra_exts);
//FIXME: Leak?
new_names = malloc(sizeof(*new_names) * new_count);
@ -688,7 +686,7 @@ XrResult bionic_xrCreateInstance(XrInstanceCreateInfo *createInfo, XrInstance *i
new_names[i] = harmless_extension;
}
for (int i = 0; i < ARRRAY_SIZE(extra_exts); i++)
for (int i = 0; i < ARRAY_SIZE(extra_exts); i++)
new_names[createInfo->enabledExtensionCount + i] = extra_exts[i];
createInfo->enabledExtensionCount = new_count;
@ -751,7 +749,7 @@ XrResult bionic_xrGetInstanceProcAddr(XrInstance instance, const char *name, PFN
printf("xrGetInstanceProcAddr(%s)\n", name);
struct xr_proc_override *match = bsearch(name, xr_proc_override_tbl,
ARRRAY_SIZE(xr_proc_override_tbl),
ARRAY_SIZE(xr_proc_override_tbl),
sizeof(xr_proc_override_tbl[0]),
(int (*)(const void *, const void *))strcmp);

View file

@ -1,10 +1,11 @@
// for dladdr
#define _GNU_SOURCE
#include <gtk/gtk.h>
#include <gdk/wayland/gdkwayland.h>
#include <gtk/gtk.h>
#include <libportal/portal.h>
#include "../api-impl-jni/defines.h"
#include "../api-impl-jni/util.h"
#include "../api-impl-jni/app/android_app_Activity.h"
@ -75,7 +76,8 @@ char *construct_classpath(char *prefix, char **cp_array, size_t len)
#define JDWP_ARG "-XjdwpOptions:transport=dt_socket,server=y,suspend=y,address="
JNIEnv* create_vm(char *api_impl_jar, char *apk_classpath, char *microg_apk, char *framework_res_apk, char *api_impl_natives_dir, char *app_lib_dir, char **extra_jvm_options) {
JNIEnv *create_vm(char *api_impl_jar, char *apk_classpath, char *microg_apk, char *framework_res_apk, char *api_impl_natives_dir, char *app_lib_dir, char **extra_jvm_options)
{
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs args = {
@ -96,7 +98,6 @@ JNIEnv* create_vm(char *api_impl_jar, char *apk_classpath, char *microg_apk, cha
args.nOptions += g_strv_length(extra_jvm_options);
options = malloc(sizeof(JavaVMOption) * args.nOptions);
if (getenv("RUN_FROM_BUILDDIR")) {
options[0].optionString = construct_classpath("-Djava.library.path=", (char *[]){"./", app_lib_dir}, 2);
} else {
@ -129,7 +130,8 @@ JNIEnv* create_vm(char *api_impl_jar, char *apk_classpath, char *microg_apk, cha
return env;
}
void icon_override(GtkWidget *window, GList *icon_list) {
void icon_override(GtkWidget *window, GList *icon_list)
{
GdkSurface *window_surface = gtk_native_get_surface(GTK_NATIVE(window));
// set app icon as window icon; this is a noop on Wayland because there is currently no way to set a window icon on Wayland
gdk_toplevel_set_icon_list(GDK_TOPLEVEL(window_surface), icon_list);
@ -150,7 +152,8 @@ gboolean hacky_on_window_focus_changed_callback(JNIEnv *env)
}
struct dynamic_launcher_callback_data {char *desktop_file_id; char *desktop_entry;};
static void dynamic_launcher_ready_callback(GObject *portal, GAsyncResult *res, gpointer user_data) {
static void dynamic_launcher_ready_callback(GObject *portal, GAsyncResult *res, gpointer user_data)
{
struct dynamic_launcher_callback_data *data = user_data;
GVariant *result = xdp_portal_dynamic_launcher_prepare_install_finish(XDP_PORTAL(portal), res, NULL);
if (!result) {
@ -490,8 +493,7 @@ static void open(GtkApplication *app, GFile** files, gint nfiles, const gchar* h
g_file_make_directory(g_file_get_parent(dest), NULL, NULL);
g_file_copy(files[0], dest, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, NULL);
GString *desktop_entry = g_string_new(
"[Desktop Entry]\n"
GString *desktop_entry = g_string_new("[Desktop Entry]\n"
"Type=Application\n"
"Exec=env ");
if (getenv("RUN_FROM_BUILDDIR")) {
@ -499,7 +501,7 @@ static void open(GtkApplication *app, GFile** files, gint nfiles, const gchar* h
g_string_append_printf(desktop_entry, "-C %s ", g_get_current_dir());
}
char *envs[] = {"RUN_FROM_BUILDDIR", "LD_LIBRARY_PATH", "ANDROID_APP_DATA_DIR", "ATL_UGLY_ENABLE_LOCATION", "ATL_UGLY_ENABLE_WEBVIEW", "ATL_DISABLE_WINDOW_DECORATIONS", "ATL_FORCE_FULLSCREEN"};
for (int i = 0; i < sizeof(envs)/sizeof(envs[0]); i++) {
for (int i = 0; i < ARRAY_SIZE(envs); i++) {
if (getenv(envs[i])) {
g_string_append_printf(desktop_entry, "%s=%s ", envs[i], getenv(envs[i]));
}
@ -554,7 +556,6 @@ static void open(GtkApplication *app, GFile** files, gint nfiles, const gchar* h
g_timeout_add(10, G_SOURCE_FUNC(hacky_on_window_focus_changed_callback), env);
jobject input_queue_callback = g_object_get_data(G_OBJECT(window), "input_queue_callback");
if (input_queue_callback) {
jobject input_queue = g_object_get_data(G_OBJECT(window), "input_queue");
@ -567,66 +568,21 @@ static void open(GtkApplication *app, GFile** files, gint nfiles, const gchar* h
static void activate(GtkApplication *app, struct jni_callback_data *d)
{
printf("error: usage: ./android-translation-layer [app.apk] -l [path/to/activity]\n"
printf("error: usage: ./android-translation-layer [app.apk] [-l path/to/activity]\n"
"you can specify --help to see the list of options\n");
exit(1);
}
void init_cmd_parameters(GApplication *app, struct jni_callback_data *d)
{
const GOptionEntry cmd_params[] =
{
{
.long_name = "launch-activity",
.short_name = 'l',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_STRING,
.arg_data = &d->apk_main_activity_class,
.description = "the fully quilifed name of the activity you wish to launch (usually the apk's main activity)",
.arg_description = NULL,
},
{
.long_name = "window-width",
.short_name = 'w',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_INT,
.arg_data = &d->window_width,
.description = "window width to launch with (some apps react poorly to runtime window size adjustments)",
.arg_description = NULL,
},
{
.long_name = "window-height",
.short_name = 'h',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_INT,
.arg_data = &d->window_height,
.description = "window height to launch with (some apps react poorly to runtime window size adjustments)",
.arg_description = NULL,
},
{
.long_name = "install",
.short_name = 'i',
.flags = 0,
.arg = G_OPTION_ARG_NONE,
.arg_data = &d->install,
.description = "install .desktop file for the given apk",
},
{
.long_name = "extra-jvm-option",
.short_name = 'X',
.flags = 0,
.arg = G_OPTION_ARG_STRING_ARRAY,
.arg_data = &d->extra_jvm_options,
.description = "pass an additional option directly to art",
},
{
.long_name = "extra-string-key",
.short_name = 'e',
.flags = 0,
.arg = G_OPTION_ARG_STRING_ARRAY,
.arg_data = &d->extra_string_keys,
.description = "pass a string extra (-e key=value)",
},
const GOptionEntry cmd_params[] = {
/* long_name | short_name | flags | arg | arg_data | description | arg_desc */
{ "launch-activity", 'l', 0, G_OPTION_ARG_STRING, &d->apk_main_activity_class, "the fully qualifed name of the activity you wish to launch (usually the apk's main activity)", "ACTIVITY_NAME" },
{ "window-width", 'w', 0, G_OPTION_ARG_INT, &d->window_width, "window width to launch with (some apps react poorly to runtime window size adjustments)", "WIDTH" },
{ "window-height", 'h', 0, G_OPTION_ARG_INT, &d->window_height, "window height to launch with (some apps react poorly to runtime window size adjustments)", "HEIGHT" },
{ "install", 'i', 0, G_OPTION_ARG_NONE, &d->install, "install .desktop file for the given apk", NULL },
{ "extra-jvm-option", 'X', 0, G_OPTION_ARG_STRING_ARRAY, &d->extra_jvm_options, "pass an additional option directly to art (e.g -X \"-verbose:jni\")", "\"OPTION\"" },
{ "extra-string-key", 'e', 0, G_OPTION_ARG_STRING_ARRAY, &d->extra_string_keys, "pass a string extra (-e key=value)", "\"KEY=VALUE\"" },
{NULL}
};