From 0dd5050b292cdfa21e50653c568cf1f23e2eeeb0 Mon Sep 17 00:00:00 2001 From: Larpon Date: Tue, 5 Jul 2022 15:30:10 +0200 Subject: [PATCH] os: clean up usage of ANativeActivity, allow access to fields (#14948) --- vlib/gg/gg_android_outside_termux.c.v | 21 ++------------------- vlib/os/os_android_outside_termux.c.v | 15 +++++++++------ 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/vlib/gg/gg_android_outside_termux.c.v b/vlib/gg/gg_android_outside_termux.c.v index bef8d2b7f..41e5ba26a 100644 --- a/vlib/gg/gg_android_outside_termux.c.v +++ b/vlib/gg/gg_android_outside_termux.c.v @@ -1,36 +1,19 @@ module gg +import os import sokol.sapp #include -#include fn C.AConfiguration_new() voidptr fn C.AConfiguration_fromAssetManager(voidptr, voidptr) fn C.AConfiguration_getDensity(voidptr) u32 fn C.AConfiguration_delete(voidptr) -struct C.AAssetManager {} - -// See https://developer.android.com/ndk/reference/struct/a-native-activity for more info. -struct C.ANativeActivity { -pub: - assetManager voidptr // Pointer to the Asset Manager instance for the application. (AAssetManager *) - callbacks voidptr // Pointer to the callback function table of the native application. (struct ANativeActivityCallbacks *) - clazz voidptr // The NativeActivity object handle. - env voidptr // JNI context for the main thread of the app. - externalDataPath &char // Path to this application's external (removable/mountable) data directory. - instance voidptr // This is the native instance of the application. - internalDataPath &char // Path to this application's internal data directory. - obbPath &char // Available starting with Honeycomb: path to the directory containing the application's OBB files (if any). - sdkVersion int // The platform's SDK version code. - vm voidptr // The global handle on the process's Java VM -} - // android_dpi_scale returns the scale factor of the device. pub fn android_dpi_scale() f32 { config := C.AConfiguration_new() - activity := &C.ANativeActivity(sapp.android_get_native_activity()) + activity := &os.NativeActivity(sapp.android_get_native_activity()) C.AConfiguration_fromAssetManager(config, activity.assetManager) density := C.AConfiguration_getDensity(config) C.AConfiguration_delete(config) diff --git a/vlib/os/os_android_outside_termux.c.v b/vlib/os/os_android_outside_termux.c.v index c7e6f6551..e0b457bf0 100644 --- a/vlib/os/os_android_outside_termux.c.v +++ b/vlib/os/os_android_outside_termux.c.v @@ -1,7 +1,8 @@ module os -#include -#include +#include +#include +#include pub enum AssetMode { buffer = C.AASSET_MODE_BUFFER // Caller plans to ask for a read-only buffer with all data. @@ -10,7 +11,9 @@ pub enum AssetMode { unknown = C.AASSET_MODE_UNKNOWN // No specific information about how data will be accessed. } +// See https://developer.android.com/ndk/reference/struct/a-native-activity for more info. struct C.ANativeActivity { +pub: assetManager &AssetManager // Pointer to the Asset Manager instance for the application. clazz voidptr // (jobject) The NativeActivity object handle. env voidptr // (JNIEnv *) JNI context for the main thread of the app. @@ -63,8 +66,8 @@ pub fn (a &Asset) get_length() int { fn C.AAsset_getLength64(&C.AAsset) i64 -// get_length_64 returns the total size of the asset data. -// get_length_64 returns the size using a 64-bit number insted of 32-bit as `get_length`. +// get_length_64 returns the total size of the asset data using +// a 64-bit number insted of 32-bit as `get_length`. pub fn (a &Asset) get_length_64() i64 { return C.AAsset_getLength64(a) } @@ -85,14 +88,14 @@ pub fn (a &Asset) close() { } // read_apk_asset returns all the data located at `path`. -// `path` is expected to be relative to the `assets` +// `path` is expected to be relative to the APK/AAB `assets` directory. pub fn read_apk_asset(path string) ![]u8 { $if apk { act := &NativeActivity(C.sapp_android_get_native_activity()) if isnil(act) { return error('could not get reference to Android activity') } - asset_manager := &AssetManager(act.assetManager) + asset_manager := act.assetManager asset := asset_manager.open(path, .streaming)! len := asset.get_length() buf := []u8{len: len} -- 2.30.2