2023-06-23 15:01:40 +03:00
|
|
|
#include "loader.h"
|
|
|
|
#include "loader_applications.h"
|
|
|
|
#include <dialogs/dialogs.h>
|
|
|
|
#include <flipper_application/flipper_application.h>
|
|
|
|
#include <assets_icons.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <gui/view_holder.h>
|
|
|
|
#include <gui/modules/loading.h>
|
2023-08-02 06:58:39 +03:00
|
|
|
#include <dolphin/dolphin.h>
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
#include <lib/toolbox/path.h>
|
2023-06-23 15:01:40 +03:00
|
|
|
|
|
|
|
#define TAG "LoaderApplications"
|
|
|
|
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
#define JS_RUNNER_APP "JS Runner"
|
|
|
|
|
2023-06-23 15:01:40 +03:00
|
|
|
struct LoaderApplications {
|
|
|
|
FuriThread* thread;
|
|
|
|
void (*closed_cb)(void*);
|
|
|
|
void* context;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int32_t loader_applications_thread(void* p);
|
|
|
|
|
|
|
|
LoaderApplications* loader_applications_alloc(void (*closed_cb)(void*), void* context) {
|
|
|
|
LoaderApplications* loader_applications = malloc(sizeof(LoaderApplications));
|
|
|
|
loader_applications->thread =
|
2023-07-13 15:02:59 +03:00
|
|
|
furi_thread_alloc_ex(TAG, 768, loader_applications_thread, (void*)loader_applications);
|
2023-06-23 15:01:40 +03:00
|
|
|
loader_applications->closed_cb = closed_cb;
|
|
|
|
loader_applications->context = context;
|
|
|
|
furi_thread_start(loader_applications->thread);
|
|
|
|
return loader_applications;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loader_applications_free(LoaderApplications* loader_applications) {
|
|
|
|
furi_assert(loader_applications);
|
|
|
|
furi_thread_join(loader_applications->thread);
|
|
|
|
furi_thread_free(loader_applications->thread);
|
|
|
|
free(loader_applications);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
FuriString* file_path;
|
2023-06-23 15:01:40 +03:00
|
|
|
DialogsApp* dialogs;
|
|
|
|
Storage* storage;
|
2023-07-12 12:49:17 +03:00
|
|
|
Loader* loader;
|
|
|
|
|
|
|
|
Gui* gui;
|
|
|
|
ViewHolder* view_holder;
|
|
|
|
Loading* loading;
|
2023-06-23 15:01:40 +03:00
|
|
|
} LoaderApplicationsApp;
|
|
|
|
|
2024-03-25 13:53:32 +03:00
|
|
|
static LoaderApplicationsApp* loader_applications_app_alloc(void) {
|
2023-06-23 15:01:40 +03:00
|
|
|
LoaderApplicationsApp* app = malloc(sizeof(LoaderApplicationsApp)); //-V799
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
app->file_path = furi_string_alloc_set(EXT_PATH("apps"));
|
2023-06-23 15:01:40 +03:00
|
|
|
app->dialogs = furi_record_open(RECORD_DIALOGS);
|
|
|
|
app->storage = furi_record_open(RECORD_STORAGE);
|
2023-07-12 12:49:17 +03:00
|
|
|
app->loader = furi_record_open(RECORD_LOADER);
|
|
|
|
|
|
|
|
app->gui = furi_record_open(RECORD_GUI);
|
|
|
|
app->view_holder = view_holder_alloc();
|
|
|
|
app->loading = loading_alloc();
|
|
|
|
|
|
|
|
view_holder_attach_to_gui(app->view_holder, app->gui);
|
|
|
|
view_holder_set_view(app->view_holder, loading_get_view(app->loading));
|
|
|
|
|
2023-06-23 15:01:40 +03:00
|
|
|
return app;
|
|
|
|
} //-V773
|
|
|
|
|
2023-07-12 12:49:17 +03:00
|
|
|
static void loader_applications_app_free(LoaderApplicationsApp* app) {
|
|
|
|
furi_assert(app);
|
|
|
|
|
|
|
|
view_holder_free(app->view_holder);
|
|
|
|
loading_free(app->loading);
|
|
|
|
furi_record_close(RECORD_GUI);
|
|
|
|
|
|
|
|
furi_record_close(RECORD_LOADER);
|
2023-06-23 15:01:40 +03:00
|
|
|
furi_record_close(RECORD_DIALOGS);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
furi_string_free(app->file_path);
|
2023-07-12 12:49:17 +03:00
|
|
|
free(app);
|
2023-06-23 15:01:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool loader_applications_item_callback(
|
|
|
|
FuriString* path,
|
|
|
|
void* context,
|
|
|
|
uint8_t** icon_ptr,
|
|
|
|
FuriString* item_name) {
|
|
|
|
LoaderApplicationsApp* loader_applications_app = context;
|
|
|
|
furi_assert(loader_applications_app);
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
if(furi_string_end_with(path, ".fap")) {
|
|
|
|
return flipper_application_load_name_and_icon(
|
|
|
|
path, loader_applications_app->storage, icon_ptr, item_name);
|
|
|
|
} else {
|
|
|
|
path_extract_filename(path, item_name, false);
|
|
|
|
memcpy(*icon_ptr, icon_get_data(&I_js_script_10px), FAP_MANIFEST_MAX_ICON_SIZE);
|
|
|
|
return true;
|
|
|
|
}
|
2023-06-23 15:01:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool loader_applications_select_app(LoaderApplicationsApp* loader_applications_app) {
|
|
|
|
const DialogsFileBrowserOptions browser_options = {
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
.extension = ".fap|.js",
|
2023-06-23 15:01:40 +03:00
|
|
|
.skip_assets = true,
|
|
|
|
.icon = &I_unknown_10px,
|
|
|
|
.hide_ext = true,
|
|
|
|
.item_loader_callback = loader_applications_item_callback,
|
|
|
|
.item_loader_context = loader_applications_app,
|
|
|
|
.base_path = EXT_PATH("apps"),
|
|
|
|
};
|
|
|
|
|
|
|
|
return dialog_file_browser_show(
|
|
|
|
loader_applications_app->dialogs,
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
loader_applications_app->file_path,
|
|
|
|
loader_applications_app->file_path,
|
2023-06-23 15:01:40 +03:00
|
|
|
&browser_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define APPLICATION_STOP_EVENT 1
|
|
|
|
|
|
|
|
static void loader_pubsub_callback(const void* message, void* context) {
|
|
|
|
const LoaderEvent* event = message;
|
|
|
|
const FuriThreadId thread_id = (FuriThreadId)context;
|
|
|
|
|
|
|
|
if(event->type == LoaderEventTypeApplicationStopped) {
|
|
|
|
furi_thread_flags_set(thread_id, APPLICATION_STOP_EVENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
static void
|
|
|
|
loader_applications_start_app(LoaderApplicationsApp* app, const char* name, const char* args) {
|
2023-08-02 06:58:39 +03:00
|
|
|
dolphin_deed(DolphinDeedPluginStart);
|
|
|
|
|
2023-06-23 15:01:40 +03:00
|
|
|
// load app
|
|
|
|
FuriThreadId thread_id = furi_thread_get_current_id();
|
|
|
|
FuriPubSubSubscription* subscription =
|
2023-07-12 12:49:17 +03:00
|
|
|
furi_pubsub_subscribe(loader_get_pubsub(app->loader), loader_pubsub_callback, thread_id);
|
2023-06-23 15:01:40 +03:00
|
|
|
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
LoaderStatus status = loader_start_with_gui_error(app->loader, name, args);
|
2023-06-23 15:01:40 +03:00
|
|
|
|
|
|
|
if(status == LoaderStatusOk) {
|
|
|
|
furi_thread_flags_wait(APPLICATION_STOP_EVENT, FuriFlagWaitAny, FuriWaitForever);
|
|
|
|
}
|
|
|
|
|
2023-07-12 12:49:17 +03:00
|
|
|
furi_pubsub_unsubscribe(loader_get_pubsub(app->loader), subscription);
|
2023-06-23 15:01:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t loader_applications_thread(void* p) {
|
|
|
|
LoaderApplications* loader_applications = p;
|
2023-07-12 12:49:17 +03:00
|
|
|
LoaderApplicationsApp* app = loader_applications_app_alloc();
|
2023-06-23 15:01:40 +03:00
|
|
|
|
2023-07-12 12:49:17 +03:00
|
|
|
// start loading animation
|
|
|
|
view_holder_start(app->view_holder);
|
|
|
|
|
|
|
|
while(loader_applications_select_app(app)) {
|
[FL-3579, FL-3601, FL-3714] JavaScript runner (#3286)
* FBT: cdefines to env, libs order
* API: strtod, modf, itoa, calloc
* Apps: elk js
* Apps: mjs
* JS: scripts as assets
* mjs: composite resolver
* mjs: stack trace
* ELK JS example removed
* MJS thread, MJS lib modified to support script interruption
* JS console UI
* Module system, BadUSB bindings rework
* JS notifications, simple dialog, BadUSB demo
* Custom dialogs, dialog demo
* MJS as system library, some dirty hacks to make it compile
* Plugin-based js modules
* js_uart(BadUART) module
* js_uart: support for byte array arguments
* Script icon and various fixes
* File browser: multiple extensions filter, running js scripts from app loader
* Running js scripts from archive browser
* JS Runner as system app
* Example scripts moved to /ext/apps/Scripts
* JS bytecode listing generation
* MJS builtin printf cleanup
* JS examples cleanup
* mbedtls version fix
* Unused lib cleanup
* Making PVS happy & TODOs cleanup
* TODOs cleanup #2
* MJS: initial typed arrays support
* JS: fix mem leak in uart destructor
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-02-12 11:54:32 +03:00
|
|
|
if(!furi_string_end_with(app->file_path, ".js")) {
|
|
|
|
loader_applications_start_app(app, furi_string_get_cstr(app->file_path), NULL);
|
|
|
|
} else {
|
|
|
|
loader_applications_start_app(
|
|
|
|
app, JS_RUNNER_APP, furi_string_get_cstr(app->file_path));
|
|
|
|
}
|
2023-06-23 15:01:40 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 12:49:17 +03:00
|
|
|
// stop loading animation
|
|
|
|
view_holder_stop(app->view_holder);
|
|
|
|
|
|
|
|
loader_applications_app_free(app);
|
2023-06-23 15:01:40 +03:00
|
|
|
|
|
|
|
if(loader_applications->closed_cb) {
|
|
|
|
loader_applications->closed_cb(loader_applications->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|