From 62ae7adab2bebde04864c12543caefbffab24963 Mon Sep 17 00:00:00 2001 From: Maksim Date: Wed, 15 Apr 2020 16:27:40 +0200 Subject: Android: add Android Studio support, completely redone java part (#9066) --- src/porting_android.cpp | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'src/porting_android.cpp') diff --git a/src/porting_android.cpp b/src/porting_android.cpp index 7c74f7b5b..4130bdb6d 100644 --- a/src/porting_android.cpp +++ b/src/porting_android.cpp @@ -69,10 +69,10 @@ void android_main(android_app *app) /* TODO this doesn't work as expected, no idea why but there's a workaround */ /* for it right now */ extern "C" { - JNIEXPORT void JNICALL Java_net_minetest_MtNativeActivity_putMessageBoxResult( + JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult( JNIEnv * env, jclass thiz, jstring text) { - errorstream << "Java_net_minetest_MtNativeActivity_putMessageBoxResult got: " + errorstream << "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " << std::string((const char*)env->GetStringChars(text,0)) << std::endl; } @@ -107,17 +107,6 @@ jclass findClass(std::string classname) return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName); } -void copyAssets() -{ - jmethodID assetcopy = jnienv->GetMethodID(nativeActivity,"copyAssets","()V"); - - if (assetcopy == 0) { - assert("porting::copyAssets unable to find copy assets method" == 0); - } - - jnienv->CallVoidMethod(app_global->activity->clazz, assetcopy); -} - void initAndroid() { porting::jnienv = NULL; @@ -136,7 +125,7 @@ void initAndroid() exit(-1); } - nativeActivity = findClass("net/minetest/minetest/MtNativeActivity"); + nativeActivity = findClass("net/minetest/minetest/GameActivity"); if (nativeActivity == 0) { errorstream << "porting::initAndroid unable to find java native activity class" << -- cgit v1.2.3 From 4f9a5f67ee745d78fdc3e173972a3df110f989cf Mon Sep 17 00:00:00 2001 From: Maksim Date: Wed, 6 May 2020 14:25:57 +0200 Subject: Android: porting_android.cpp refactoring (#9687) * Android: porting_android.cpp refactoring * Replace assert to FATAL_ERROR_IF --- src/porting_android.cpp | 135 ++++++++++++++++++++---------------------------- src/porting_android.h | 14 +++-- 2 files changed, 62 insertions(+), 87 deletions(-) (limited to 'src/porting_android.cpp') diff --git a/src/porting_android.cpp b/src/porting_android.cpp index 4130bdb6d..2c91df235 100644 --- a/src/porting_android.cpp +++ b/src/porting_android.cpp @@ -47,8 +47,7 @@ void android_main(android_app *app) Thread::setName("Main"); try { - app_dummy(); - char *argv[] = {strdup(PROJECT_NAME), NULL}; + char *argv[] = {strdup(PROJECT_NAME), nullptr}; main(ARRLEN(argv) - 1, argv); free(argv[0]); } catch (std::exception &e) { @@ -64,85 +63,73 @@ void android_main(android_app *app) exit(retval); } -/* handler for finished message box input */ -/* Intentionally NOT in namespace porting */ -/* TODO this doesn't work as expected, no idea why but there's a workaround */ -/* for it right now */ +/** + * Handler for finished message box input + * Intentionally NOT in namespace porting + * ToDo: this doesn't work as expected, there's a workaround for it right now + */ extern "C" { JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult( - JNIEnv * env, jclass thiz, jstring text) + JNIEnv *env, jclass thiz, jstring text) { - errorstream << "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " - << std::string((const char*)env->GetStringChars(text,0)) - << std::endl; + errorstream << + "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " << + std::string((const char*) env->GetStringChars(text, nullptr)) << std::endl; } } namespace porting { - -std::string path_storage = DIR_DELIM "sdcard" DIR_DELIM; - -android_app* app_global; -JNIEnv* jnienv; +android_app *app_global; +JNIEnv *jnienv; jclass nativeActivity; -jclass findClass(std::string classname) +jclass findClass(const std::string &classname) { - if (jnienv == 0) { - return 0; - } + if (jnienv == nullptr) + return nullptr; jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity"); - jmethodID getClassLoader = - jnienv->GetMethodID(nativeactivity,"getClassLoader", - "()Ljava/lang/ClassLoader;"); - jobject cls = - jnienv->CallObjectMethod(app_global->activity->clazz, getClassLoader); + jmethodID getClassLoader = jnienv->GetMethodID( + nativeactivity, "getClassLoader", "()Ljava/lang/ClassLoader;"); + jobject cls = jnienv->CallObjectMethod( + app_global->activity->clazz, getClassLoader); jclass classLoader = jnienv->FindClass("java/lang/ClassLoader"); - jmethodID findClass = - jnienv->GetMethodID(classLoader, "loadClass", + jmethodID findClass = jnienv->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); - jstring strClassName = - jnienv->NewStringUTF(classname.c_str()); + jstring strClassName = jnienv->NewStringUTF(classname.c_str()); return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName); } void initAndroid() { - porting::jnienv = NULL; + porting::jnienv = nullptr; JavaVM *jvm = app_global->activity->vm; JavaVMAttachArgs lJavaVMAttachArgs; lJavaVMAttachArgs.version = JNI_VERSION_1_6; lJavaVMAttachArgs.name = PROJECT_NAME_C "NativeThread"; - lJavaVMAttachArgs.group = NULL; -#ifdef NDEBUG - // This is a ugly hack as arm v7a non debuggable builds crash without this - // printf ... if someone finds out why please fix it! - infostream << "Attaching native thread. " << std::endl; -#endif - if ( jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) { + lJavaVMAttachArgs.group = nullptr; + + if (jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) { errorstream << "Failed to attach native thread to jvm" << std::endl; exit(-1); } nativeActivity = findClass("net/minetest/minetest/GameActivity"); - if (nativeActivity == 0) { + if (nativeActivity == nullptr) errorstream << "porting::initAndroid unable to find java native activity class" << std::endl; - } #ifdef GPROF - /* in the start-up code */ + // in the start-up code __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C, "Initializing GPROF profiler"); - monstartup("libminetest.so"); + monstartup("libMinetest.so"); #endif } void cleanupAndroid() { - #ifdef GPROF errorstream << "Shutting down GPROF profiler" << std::endl; setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1); @@ -157,7 +144,7 @@ static std::string javaStringToUTF8(jstring js) { std::string str; // Get string as a UTF-8 c-string - const char *c_str = jnienv->GetStringUTFChars(js, NULL); + const char *c_str = jnienv->GetStringUTFChars(js, nullptr); // Save it str = c_str; // And free the c-string @@ -166,17 +153,15 @@ static std::string javaStringToUTF8(jstring js) } // Calls static method if obj is NULL -static std::string getAndroidPath(jclass cls, jobject obj, jclass cls_File, - jmethodID mt_getAbsPath, const char *getter) +static std::string getAndroidPath( + jclass cls, jobject obj, jmethodID mt_getAbsPath, const char *getter) { // Get getter method jmethodID mt_getter; if (obj) - mt_getter = jnienv->GetMethodID(cls, getter, - "()Ljava/io/File;"); + mt_getter = jnienv->GetMethodID(cls, getter, "()Ljava/io/File;"); else - mt_getter = jnienv->GetStaticMethodID(cls, getter, - "()Ljava/io/File;"); + mt_getter = jnienv->GetStaticMethodID(cls, getter, "()Ljava/io/File;"); // Call getter jobject ob_file; @@ -186,8 +171,7 @@ static std::string getAndroidPath(jclass cls, jobject obj, jclass cls_File, ob_file = jnienv->CallStaticObjectMethod(cls, mt_getter); // Call getAbsolutePath - jstring js_path = (jstring) jnienv->CallObjectMethod(ob_file, - mt_getAbsPath); + auto js_path = (jstring) jnienv->CallObjectMethod(ob_file, mt_getAbsPath); return javaStringToUTF8(js_path); } @@ -201,26 +185,24 @@ void initializePathsAndroid() // Get getAbsolutePath method jmethodID mt_getAbsPath = jnienv->GetMethodID(cls_File, "getAbsolutePath", "()Ljava/lang/String;"); + std::string path_storage = getAndroidPath(cls_Env, nullptr, + mt_getAbsPath, "getExternalStorageDirectory"); - path_cache = getAndroidPath(nativeActivity, app_global->activity->clazz, - cls_File, mt_getAbsPath, "getCacheDir"); - path_storage = getAndroidPath(cls_Env, NULL, cls_File, mt_getAbsPath, - "getExternalStorageDirectory"); path_user = path_storage + DIR_DELIM + PROJECT_NAME_C; path_share = path_storage + DIR_DELIM + PROJECT_NAME_C; - + path_cache = getAndroidPath(nativeActivity, + app_global->activity->clazz, mt_getAbsPath, "getCacheDir"); migrateCachePath(); } -void showInputDialog(const std::string& acceptButton, const std::string& hint, - const std::string& current, int editType) +void showInputDialog(const std::string &acceptButton, const std::string &hint, + const std::string ¤t, int editType) { - jmethodID showdialog = jnienv->GetMethodID(nativeActivity,"showDialog", + jmethodID showdialog = jnienv->GetMethodID(nativeActivity, "showDialog", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V"); - if (showdialog == 0) { - assert("porting::showInputDialog unable to find java show dialog method" == 0); - } + FATAL_ERROR_IF(showdialog == nullptr, + "porting::showInputDialog unable to find java show dialog method"); jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str()); jstring jhint = jnienv->NewStringUTF(hint.c_str()); @@ -236,9 +218,8 @@ int getInputDialogState() jmethodID dialogstate = jnienv->GetMethodID(nativeActivity, "getDialogState", "()I"); - if (dialogstate == 0) { - assert("porting::getInputDialogState unable to find java dialog state method" == 0); - } + FATAL_ERROR_IF(dialogstate == nullptr, + "porting::getInputDialogState unable to find java dialog state method"); return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate); } @@ -248,14 +229,13 @@ std::string getInputDialogValue() jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity, "getDialogValue", "()Ljava/lang/String;"); - if (dialogvalue == 0) { - assert("porting::getInputDialogValue unable to find java dialog value method" == 0); - } + FATAL_ERROR_IF(dialogvalue == nullptr, + "porting::getInputDialogValue unable to find java dialog value method"); jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, dialogvalue); - const char* javachars = jnienv->GetStringUTFChars((jstring) result,0); + const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr); std::string text(javachars); jnienv->ReleaseStringUTFChars((jstring) result, javachars); @@ -269,12 +249,11 @@ float getDisplayDensity() static float value = 0; if (firstrun) { - jmethodID getDensity = jnienv->GetMethodID(nativeActivity, "getDensity", - "()F"); + jmethodID getDensity = jnienv->GetMethodID(nativeActivity, + "getDensity", "()F"); - if (getDensity == 0) { - assert("porting::getDisplayDensity unable to find java getDensity method" == 0); - } + FATAL_ERROR_IF(getDensity == nullptr, + "porting::getDisplayDensity unable to find java getDensity method"); value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity); firstrun = false; @@ -291,9 +270,8 @@ v2u32 getDisplaySize() jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity, "getDisplayWidth", "()I"); - if (getDisplayWidth == 0) { - assert("porting::getDisplayWidth unable to find java getDisplayWidth method" == 0); - } + FATAL_ERROR_IF(getDisplayWidth == nullptr, + "porting::getDisplayWidth unable to find java getDisplayWidth method"); retval.X = jnienv->CallIntMethod(app_global->activity->clazz, getDisplayWidth); @@ -301,9 +279,8 @@ v2u32 getDisplaySize() jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity, "getDisplayHeight", "()I"); - if (getDisplayHeight == 0) { - assert("porting::getDisplayHeight unable to find java getDisplayHeight method" == 0); - } + FATAL_ERROR_IF(getDisplayHeight == nullptr, + "porting::getDisplayHeight unable to find java getDisplayHeight method"); retval.Y = jnienv->CallIntMethod(app_global->activity->clazz, getDisplayHeight); diff --git a/src/porting_android.h b/src/porting_android.h index 60eb2a9c0..42f90b60b 100644 --- a/src/porting_android.h +++ b/src/porting_android.h @@ -30,16 +30,15 @@ with this program; if not, write to the Free Software Foundation, Inc., #include namespace porting { -/** java app **/ +// java app extern android_app *app_global; -/** java <-> c++ interaction interface **/ +// java <-> c++ interaction interface extern JNIEnv *jnienv; -/** - * do initialization required on android only - */ +// do initialization required on android only void initAndroid(); + void cleanupAndroid(); /** @@ -56,8 +55,8 @@ void initializePathsAndroid(); * @param editType type of texfield * (1==multiline text input; 2==single line text input; 3=password field) */ -void showInputDialog(const std::string& acceptButton, - const std::string& hint, const std::string& current, int editType); +void showInputDialog(const std::string &acceptButton, + const std::string &hint, const std::string ¤t, int editType); /** * WORKAROUND for not working callbacks from java -> c++ @@ -75,5 +74,4 @@ std::string getInputDialogValue(); float getDisplayDensity(); v2u32 getDisplaySize(); #endif - } -- cgit v1.2.3 From a9c3a423231e26ea3edee51d5f0bf949ca8e529b Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 17 May 2020 19:09:10 +0100 Subject: Add core.open_url() to main menu API (#8592) --- .../java/net/minetest/minetest/GameActivity.java | 6 ++ builtin/mainmenu/dlg_contentstore.lua | 113 ++++----------------- builtin/mainmenu/tab_credits.lua | 11 +- doc/menu_lua_api.txt | 5 + src/porting.cpp | 30 +++++- src/porting.h | 3 + src/porting_android.cpp | 12 +++ src/porting_android.h | 2 + src/script/lua_api/l_mainmenu.cpp | 9 ++ src/script/lua_api/l_mainmenu.h | 3 + 10 files changed, 98 insertions(+), 96 deletions(-) (limited to 'src/porting_android.cpp') diff --git a/build/android/app/src/main/java/net/minetest/minetest/GameActivity.java b/build/android/app/src/main/java/net/minetest/minetest/GameActivity.java index 02b61b598..635512569 100644 --- a/build/android/app/src/main/java/net/minetest/minetest/GameActivity.java +++ b/build/android/app/src/main/java/net/minetest/minetest/GameActivity.java @@ -22,6 +22,7 @@ package net.minetest.minetest; import android.app.NativeActivity; import android.content.Intent; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.view.View; @@ -117,4 +118,9 @@ public class GameActivity extends NativeActivity { public int getDisplayWidth() { return getResources().getDisplayMetrics().widthPixels; } + + public void openURL(String url) { + Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + startActivity(browserIntent); + } } diff --git a/builtin/mainmenu/dlg_contentstore.lua b/builtin/mainmenu/dlg_contentstore.lua index 3bc5f60bb..ce5c061c6 100644 --- a/builtin/mainmenu/dlg_contentstore.lua +++ b/builtin/mainmenu/dlg_contentstore.lua @@ -16,7 +16,6 @@ --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. local store = { packages = {}, packages_full = {} } -local package_dialog = {} -- Screenshot local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb" @@ -44,8 +43,6 @@ local filter_types_type = { } - - local function download_package(param) if core.download_file(param.package.url, param.filename) then return { @@ -195,74 +192,6 @@ local function get_screenshot(package) return defaulttexturedir .. "loading_screenshot.png" end - - -function package_dialog.get_formspec() - local package = package_dialog.package - - store.update_paths() - - local formspec = { - "size[9,4;true]", - "image[0,1;4.5,3;", core.formspec_escape(get_screenshot(package)), ']', - "label[3.8,1;", - minetest.colorize(mt_color_green, core.formspec_escape(package.title)), "\n", - minetest.colorize('#BFBFBF', "by " .. core.formspec_escape(package.author)), "]", - "textarea[4,2;5.3,2;;;", core.formspec_escape(package.short_description), "]", - "button[0,0;2,1;back;", fgettext("Back"), "]", - } - - if not package.path then - formspec[#formspec + 1] = "button[7,0;2,1;install;" - formspec[#formspec + 1] = fgettext("Install") - formspec[#formspec + 1] = "]" - elseif package.installed_release < package.release then - -- The install_ action also handles updating - formspec[#formspec + 1] = "button[7,0;2,1;install;" - formspec[#formspec + 1] = fgettext("Update") - formspec[#formspec + 1] = "]" - formspec[#formspec + 1] = "button[5,0;2,1;uninstall;" - formspec[#formspec + 1] = fgettext("Uninstall") - formspec[#formspec + 1] = "]" - else - formspec[#formspec + 1] = "button[7,0;2,1;uninstall;" - formspec[#formspec + 1] = fgettext("Uninstall") - formspec[#formspec + 1] = "]" - end - - return table.concat(formspec, "") -end - -function package_dialog.handle_submit(this, fields) - if fields.back then - this:delete() - return true - end - - if fields.install then - start_install(this, package_dialog.package) - return true - end - - if fields.uninstall then - local dlg_delmod = create_delete_content_dlg(package_dialog.package) - dlg_delmod:set_parent(this) - this:hide() - dlg_delmod:show() - return true - end - - return false -end - -function package_dialog.create(package) - package_dialog.package = package - return dialog_create("package_view", - package_dialog.get_formspec, - package_dialog.handle_submit, - nil) -end - function store.load() local tmpdir = os.tempfolder() local target = tmpdir .. DIR_DELIM .. "packages.json" @@ -462,44 +391,45 @@ function store.get_formspec(dlgdata) minetest.colorize("#BFBFBF", " by " .. package.author)) formspec[#formspec + 1] = "]" - -- description - if package.path and package.installed_release < package.release then - formspec[#formspec + 1] = "textarea[1.25,0.3;7.5,1;;;" - else - formspec[#formspec + 1] = "textarea[1.25,0.3;9,1;;;" - end - formspec[#formspec + 1] = core.formspec_escape(package.short_description) - formspec[#formspec + 1] = "]" - -- buttons + local description_width = 7.5 if not package.path then - formspec[#formspec + 1] = "button[9.9,0;1.5,1;install_" + formspec[#formspec + 1] = "button[8.4,0;1.5,1;install_" formspec[#formspec + 1] = tostring(i) formspec[#formspec + 1] = ";" formspec[#formspec + 1] = fgettext("Install") formspec[#formspec + 1] = "]" else if package.installed_release < package.release then + description_width = 6 + -- The install_ action also handles updating - formspec[#formspec + 1] = "button[8.4,0;1.5,1;install_" + formspec[#formspec + 1] = "button[6.9,0;1.5,1;install_" formspec[#formspec + 1] = tostring(i) formspec[#formspec + 1] = ";" formspec[#formspec + 1] = fgettext("Update") formspec[#formspec + 1] = "]" end - formspec[#formspec + 1] = "button[9.9,0;1.5,1;uninstall_" + formspec[#formspec + 1] = "button[8.4,0;1.5,1;uninstall_" formspec[#formspec + 1] = tostring(i) formspec[#formspec + 1] = ";" formspec[#formspec + 1] = fgettext("Uninstall") formspec[#formspec + 1] = "]" end - --formspec[#formspec + 1] = "button[9.9,0;1.5,1;view_" - --formspec[#formspec + 1] = tostring(i) - --formspec[#formspec + 1] = ";" - --formspec[#formspec + 1] = fgettext("View") - --formspec[#formspec + 1] = "]" + formspec[#formspec + 1] = "button[9.9,0;1.5,1;view_" + formspec[#formspec + 1] = tostring(i) + formspec[#formspec + 1] = ";" + formspec[#formspec + 1] = fgettext("View") + formspec[#formspec + 1] = "]" + + -- description + formspec[#formspec + 1] = "textarea[1.25,0.3;" + formspec[#formspec + 1] = tostring(description_width) + formspec[#formspec + 1] = ",1;;;" + formspec[#formspec + 1] = core.formspec_escape(package.short_description) + formspec[#formspec + 1] = "]" formspec[#formspec + 1] = "container_end[]" end @@ -576,10 +506,9 @@ function store.handle_submit(this, fields) end if fields["view_" .. i] then - local dlg = package_dialog.create(package) - dlg:set_parent(this) - this:hide() - dlg:show() + local url = ("%s/packages/%s?protocol_version=%d"):format( + core.settings:get("contentdb_url"), package.id, core.get_max_supp_proto()) + core.open_url(url) return true end end diff --git a/builtin/mainmenu/tab_credits.lua b/builtin/mainmenu/tab_credits.lua index 962d2a3b4..c2b7e503a 100644 --- a/builtin/mainmenu/tab_credits.lua +++ b/builtin/mainmenu/tab_credits.lua @@ -101,8 +101,8 @@ return { local logofile = defaulttexturedir .. "logo.png" local version = core.get_version() return "image[0.5,1;" .. core.formspec_escape(logofile) .. "]" .. - "label[0.5,3.2;" .. version.project .. " " .. version.string .. "]" .. - "label[0.5,3.5;http://minetest.net]" .. + "label[0.5,2.8;" .. version.project .. " " .. version.string .. "]" .. + "button[0.5,3;2,2;homepage;minetest.net]" .. "tablecolumns[color;text]" .. "tableoptions[background=#00000000;highlight=#00000000;border=false]" .. "table[3.5,-0.25;8.5,6.05;list_credits;" .. @@ -115,5 +115,10 @@ return { "#FFFF00," .. fgettext("Previous Contributors") .. ",," .. buildCreditList(previous_contributors) .. "," .. ";1]" - end + end, + cbf_button_handler = function(this, fields, name, tabdata) + if fields.homepage then + core.open_url("https://www.minetest.net") + end + end, } diff --git a/doc/menu_lua_api.txt b/doc/menu_lua_api.txt index 8f5460acb..485c50110 100644 --- a/doc/menu_lua_api.txt +++ b/doc/menu_lua_api.txt @@ -234,6 +234,11 @@ core.get_min_supp_proto() core.get_max_supp_proto() ^ returns the maximum supported network protocol version +Other: +core.open_url(url) +^ opens the URL in a web browser, returns false on failure. +^ Must begin with http:// or https:// + Async: core.handle_async(async_job,parameters,finished) ^ execute a function asynchronously diff --git a/src/porting.cpp b/src/porting.cpp index c0381ad06..ef1640467 100644 --- a/src/porting.cpp +++ b/src/porting.cpp @@ -33,22 +33,28 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include + #include #endif #if !defined(_WIN32) #include #include + #if !defined(__ANDROID__) + #include + #endif #endif #if defined(__hpux) #define _PSTAT64 #include #endif +#if defined(__ANDROID__) + #include "porting_android.h" +#endif #include "config.h" #include "debug.h" #include "filesys.h" #include "log.h" #include "util/string.h" -#include "settings.h" #include #include #include @@ -697,6 +703,28 @@ int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...) return c; } +bool openURL(const std::string &url) +{ + if ((url.substr(0, 7) != "http://" && url.substr(0, 8) != "https://") || + url.find_first_of("\r\n") != std::string::npos) { + errorstream << "Invalid url: " << url << std::endl; + return false; + } + +#if defined(_WIN32) + return (intptr_t)ShellExecuteA(NULL, NULL, url.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32; +#elif defined(__ANDROID__) + openURLAndroid(url); + return true; +#elif defined(__APPLE__) + const char *argv[] = {"open", url.c_str(), NULL}; + return posix_spawnp(NULL, "open", NULL, NULL, (char**)argv, environ) == 0; +#else + const char *argv[] = {"xdg-open", url.c_str(), NULL}; + return posix_spawnp(NULL, "xdg-open", NULL, NULL, (char**)argv, environ) == 0; +#endif +} + // Load performance counter frequency only once at startup #ifdef _WIN32 diff --git a/src/porting.h b/src/porting.h index 4d30a5970..f50f0a950 100644 --- a/src/porting.h +++ b/src/porting.h @@ -329,6 +329,9 @@ bool secure_rand_fill_buf(void *buf, size_t len); void attachOrCreateConsole(); int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...); + +bool openURL(const std::string &url); + } // namespace porting #ifdef __ANDROID__ diff --git a/src/porting_android.cpp b/src/porting_android.cpp index 2c91df235..41b521ec2 100644 --- a/src/porting_android.cpp +++ b/src/porting_android.cpp @@ -213,6 +213,18 @@ void showInputDialog(const std::string &acceptButton, const std::string &hint, jacceptButton, jhint, jcurrent, jeditType); } +void openURLAndroid(const std::string &url) +{ + jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURL", + "(Ljava/lang/String;)V"); + + FATAL_ERROR_IF(url_open == nullptr, + "porting::openURLAndroid unable to find java openURL method"); + + jstring jurl = jnienv->NewStringUTF(url.c_str()); + jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl); +} + int getInputDialogState() { jmethodID dialogstate = jnienv->GetMethodID(nativeActivity, diff --git a/src/porting_android.h b/src/porting_android.h index 42f90b60b..6eb054041 100644 --- a/src/porting_android.h +++ b/src/porting_android.h @@ -58,6 +58,8 @@ void initializePathsAndroid(); void showInputDialog(const std::string &acceptButton, const std::string &hint, const std::string ¤t, int editType); +void openURLAndroid(const std::string &url); + /** * WORKAROUND for not working callbacks from java -> c++ * get current state of input dialog diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index a76e9f079..f32c477c2 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -1063,6 +1063,14 @@ int ModApiMainMenu::l_get_max_supp_proto(lua_State *L) return 1; } +/******************************************************************************/ +int ModApiMainMenu::l_open_url(lua_State *L) +{ + std::string url = luaL_checkstring(L, 1); + lua_pushboolean(L, porting::openURL(url)); + return 1; +} + /******************************************************************************/ int ModApiMainMenu::l_do_async_callback(lua_State *L) { @@ -1125,6 +1133,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top) API_FCT(get_screen_info); API_FCT(get_min_supp_proto); API_FCT(get_max_supp_proto); + API_FCT(open_url); API_FCT(do_async_callback); } diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h index b2ca49320..5a16b3bfe 100644 --- a/src/script/lua_api/l_mainmenu.h +++ b/src/script/lua_api/l_mainmenu.h @@ -145,6 +145,9 @@ private: static int l_get_max_supp_proto(lua_State *L); + // other + static int l_open_url(lua_State *L); + // async static int l_do_async_callback(lua_State *L); -- cgit v1.2.3