diff --git a/include/webui.h b/include/webui.h index 96180d7b..934b99b1 100644 --- a/include/webui.h +++ b/include/webui.h @@ -1,5 +1,5 @@ /* - WebUI Library 2.4.0 + WebUI Library 2.4.0-Beta http://webui.me https://github.com/webui-dev/webui Copyright (c) 2020-2023 Hassan Draga. @@ -21,13 +21,13 @@ // Dynamic Library Exports #if defined(_MSC_VER) || defined(__TINYC__) - #ifndef WEBUI_EXPORT - #define WEBUI_EXPORT __declspec(dllexport) - #endif + #ifndef WEBUI_EXPORT + #define WEBUI_EXPORT __declspec(dllexport) + #endif #else - #ifndef WEBUI_EXPORT - #define WEBUI_EXPORT extern - #endif + #ifndef WEBUI_EXPORT + #define WEBUI_EXPORT extern + #endif #endif // -- C STD --------------------------- @@ -44,106 +44,106 @@ #include #include #if defined(__GNUC__) || defined(__TINYC__) - #include + #include #endif // -- Windows ------------------------- #ifdef _WIN32 - #ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN - #endif - #include - #include - #include + #ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #endif + #include + #include + #include - #include - #include - #include - #include - #include - #define WEBUI_GET_CURRENT_DIR _getcwd - #define WEBUI_FILE_EXIST _access - #define WEBUI_POPEN _popen - #define WEBUI_PCLOSE _pclose - #define WEBUI_MAX_PATH MAX_PATH + #include + #include + #include + #include + #include + #define WEBUI_GET_CURRENT_DIR _getcwd + #define WEBUI_FILE_EXIST _access + #define WEBUI_POPEN _popen + #define WEBUI_PCLOSE _pclose + #define WEBUI_MAX_PATH MAX_PATH #endif // -- Linux --------------------------- #ifdef __linux__ - #include - #include - #include - #include - #include - #include - #include - #include - #include - #define WEBUI_GET_CURRENT_DIR getcwd - #define WEBUI_FILE_EXIST access - #define WEBUI_POPEN popen - #define WEBUI_PCLOSE pclose - #define WEBUI_MAX_PATH PATH_MAX + #include + #include + #include + #include + #include + #include + #include + #include + #include + #define WEBUI_GET_CURRENT_DIR getcwd + #define WEBUI_FILE_EXIST access + #define WEBUI_POPEN popen + #define WEBUI_PCLOSE pclose + #define WEBUI_MAX_PATH PATH_MAX #endif // -- Apple --------------------------- #ifdef __APPLE__ - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #define WEBUI_GET_CURRENT_DIR getcwd - #define WEBUI_FILE_EXIST access - #define WEBUI_POPEN popen - #define WEBUI_PCLOSE pclose - #define WEBUI_MAX_PATH PATH_MAX + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #define WEBUI_GET_CURRENT_DIR getcwd + #define WEBUI_FILE_EXIST access + #define WEBUI_POPEN popen + #define WEBUI_PCLOSE pclose + #define WEBUI_MAX_PATH PATH_MAX #endif // -- Enums --------------------------- enum webui_browsers { - NoBrowser = 0, // 0. No web browser - AnyBrowser = 1, // 1. Default recommended web browser - Chrome, // 2. Google Chrome - Firefox, // 3. Mozilla Firefox - Edge, // 4. Microsoft Edge - Safari, // 5. Apple Safari - Chromium, // 6. The Chromium Project - Opera, // 7. Opera Browser - Brave, // 8. The Brave Browser - Vivaldi, // 9. The Vivaldi Browser - Epic, // 10. The Epic Browser - Yandex, // 11. The Yandex Browser - ChromiumBased, // 12. Any Chromium based browser + NoBrowser = 0, // 0. No web browser + AnyBrowser = 1, // 1. Default recommended web browser + Chrome, // 2. Google Chrome + Firefox, // 3. Mozilla Firefox + Edge, // 4. Microsoft Edge + Safari, // 5. Apple Safari + Chromium, // 6. The Chromium Project + Opera, // 7. Opera Browser + Brave, // 8. The Brave Browser + Vivaldi, // 9. The Vivaldi Browser + Epic, // 10. The Epic Browser + Yandex, // 11. The Yandex Browser + ChromiumBased, // 12. Any Chromium based browser }; enum webui_runtimes { - None = 0, // 0. Prevent WebUI from using any runtime for .js and .ts files - Deno, // 1. Use Deno runtime for .js and .ts files - NodeJS, // 2. Use Nodejs runtime for .js files + None = 0, // 0. Prevent WebUI from using any runtime for .js and .ts files + Deno, // 1. Use Deno runtime for .js and .ts files + NodeJS, // 2. Use Nodejs runtime for .js files }; enum webui_events { - WEBUI_EVENT_DISCONNECTED = 0, // 0. Window disconnection event - WEBUI_EVENT_CONNECTED, // 1. Window connection event - WEBUI_EVENT_MOUSE_CLICK, // 2. Mouse click event - WEBUI_EVENT_NAVIGATION, // 3. Window navigation event - WEBUI_EVENT_CALLBACK, // 4. Function call event + WEBUI_EVENT_DISCONNECTED = 0, // 0. Window disconnection event + WEBUI_EVENT_CONNECTED, // 1. Window connection event + WEBUI_EVENT_MOUSE_CLICK, // 2. Mouse click event + WEBUI_EVENT_NAVIGATION, // 3. Window navigation event + WEBUI_EVENT_CALLBACK, // 4. Function call event }; // -- Structs ------------------------- typedef struct webui_event_t { - size_t window; // The window object number - size_t event_type; // Event type - char* element; // HTML element ID - size_t event_number; // Internal WebUI - size_t bind_id; // Bind ID + size_t window; // The window object number + size_t event_type; // Event type + char* element; // HTML element ID + size_t event_number; // Internal WebUI + size_t bind_id; // Bind ID } webui_event_t; // -- Definitions --------------------- @@ -340,7 +340,7 @@ WEBUI_EXPORT char* webui_encode(const char* str); * * @param str The string to decode (Should be null terminated) * - * @example webui_encode("SGVsbG8="); + * @example webui_decode("SGVsbG8="); */ WEBUI_EXPORT char* webui_decode(const char* str); @@ -359,7 +359,7 @@ WEBUI_EXPORT void webui_free(void* ptr); * * @param size The size of memory in bytes * - * @example webui_malloc(1024); + * @example char* myBuffer = (char*)webui_malloc(1024); */ WEBUI_EXPORT void* webui_malloc(size_t size); @@ -533,47 +533,163 @@ WEBUI_EXPORT bool webui_set_tls_certificate(const char* certificate_pem, const c // -- JavaScript ---------------------- -// Run JavaScript without waiting for the response. +/** + * @brief Run JavaScript without waiting for the response. + * + * @param window The window number + * @param script The JavaScript to be run + * + * @example webui_run(myWindow, "alert('Hello');"); + */ WEBUI_EXPORT void webui_run(size_t window, const char* script); -// Run JavaScript and get the response back (Make sure your local buffer can -// hold the response). -WEBUI_EXPORT bool webui_script(size_t window, const char* script, size_t timeout, char* buffer, size_t buffer_length); +/** + * @brief Run JavaScript and get the response back. + * Make sure your local buffer can hold the response. + * + * @param window The window number + * @param script The JavaScript to be run + * @param timeout The execution timeout + * @param buffer The local buffer to hold the response + * @param buffer_length The local buffer size + * + * @return Returns True if there is no execution error + * + * @example bool err = webui_script(myWindow, "return 4 + 6;", 0, myBuffer, myBufferSize); + */ +WEBUI_EXPORT bool webui_script(size_t window, const char* script, size_t timeout, + char* buffer, size_t buffer_length); -// Chose between Deno and Nodejs as runtime for .js and .ts files. +/** + * @brief Chose between Deno and Nodejs as runtime for .js and .ts files. + * + * @param window The window number + * @param runtime Deno | Nodejs + * + * @example webui_set_runtime(myWindow, Deno); + */ WEBUI_EXPORT void webui_set_runtime(size_t window, size_t runtime); -// Get an argument as integer at a specific index +/** + * @brief Get an argument as integer at a specific index + * + * @param e The event struct + * @param index The argument position starting from 0 + * + * @return Returns argument as integer + * + * @example long long int myNum = webui_get_int_at(e, 0); + */ WEBUI_EXPORT long long int webui_get_int_at(webui_event_t* e, size_t index); -// Get the first argument as integer +/** + * @brief Get the first argument as integer + * + * @param e The event struct + * + * @return Returns argument as integer + * + * @example long long int myNum = webui_get_int(e); + */ WEBUI_EXPORT long long int webui_get_int(webui_event_t* e); -// Get an argument as string at a specific index +/** + * @brief Get an argument as string at a specific index + * + * @param e The event struct + * @param index The argument position starting from 0 + * + * @return Returns argument as string + * + * @example const char* myStr = webui_get_string_at(e, 0); + */ WEBUI_EXPORT const char* webui_get_string_at(webui_event_t* e, size_t index); -// Get the first argument as string +/** + * @brief Get the first argument as string + * + * @param e The event struct + * + * @return Returns argument as string + * + * @example const char* myStr = webui_get_string(e); + */ WEBUI_EXPORT const char* webui_get_string(webui_event_t* e); -// Get an argument as boolean at a specific index +/** + * @brief Get an argument as boolean at a specific index + * + * @param e The event struct + * @param index The argument position starting from 0 + * + * @return Returns argument as boolean + * + * @example bool myBool = webui_get_bool_at(e, 0); + */ WEBUI_EXPORT bool webui_get_bool_at(webui_event_t* e, size_t index); -// Get the first argument as boolean +/** + * @brief Get the first argument as boolean + * + * @param e The event struct + * + * @return Returns argument as boolean + * + * @example bool myBool = webui_get_bool(e); + */ WEBUI_EXPORT bool webui_get_bool(webui_event_t* e); -// Get the size in bytes of an argument at a specific index +/** + * @brief Get the size in bytes of an argument at a specific index + * + * @param e The event struct + * @param index The argument position starting from 0 + * + * @return Returns size in bytes + * + * @example size_t argLen = webui_get_size_at(e, 0); + */ WEBUI_EXPORT size_t webui_get_size_at(webui_event_t* e, size_t index); -// Get size in bytes of the first argument +/** + * @brief Get size in bytes of the first argument + * + * @param e The event struct + * + * @return Returns size in bytes + * + * @example size_t argLen = webui_get_size(e); + */ WEBUI_EXPORT size_t webui_get_size(webui_event_t* e); -// Return the response to JavaScript as integer. +/** + * @brief Return the response to JavaScript as integer. + * + * @param e The event struct + * @param n The integer to be send to JavaScript + * + * @example webui_return_int(e, 123); + */ WEBUI_EXPORT void webui_return_int(webui_event_t* e, long long int n); -// Return the response to JavaScript as string. +/** + * @brief Return the response to JavaScript as string. + * + * @param e The event struct + * @param n The string to be send to JavaScript + * + * @example webui_return_string(e, "Response..."); + */ WEBUI_EXPORT void webui_return_string(webui_event_t* e, const char* s); -// Return the response to JavaScript as boolean. +/** + * @brief Return the response to JavaScript as boolean. + * + * @param e The event struct + * @param n The boolean to be send to JavaScript + * + * @example webui_return_bool(e, true); + */ WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b); // -- Wrapper's Interface ------------- @@ -581,26 +697,89 @@ WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b); // Bind a specific html element click event with a function. Empty element means // all events. This replaces `webui_bind()`. The func is (Window, EventType, // Element, EventNumber, BindID). -WEBUI_EXPORT size_t webui_interface_bind(size_t window, const char* element, - void (*func)(size_t, size_t, char*, size_t, size_t)); +/** + * @brief Bind a specific HTML element click event with a function. Empty element means all events. + * + * @param window The window number + * @param element The element ID + * @param func The callback as myFunc(Window, EventType, Element, EventNumber, BindID) + * + * @return Returns unique bind ID + * + * @example size_t id = webui_interface_bind(myWindow, "myID", myCallback); + */ +WEBUI_EXPORT size_t webui_interface_bind(size_t window, const char* element, + void (*func)(size_t, size_t, char*, size_t, size_t)); -// When using `webui_interface_bind()`, you may need this function to easily set -// your callback response. +/** + * @brief When using `webui_interface_bind()`, you may need this function to easily set a response. + * + * @param window The window number + * @param event_number The event number + * @param response The response as string to be send to JavaScript + * + * @example webui_interface_set_response(myWindow, e->event_number, "Response..."); + */ WEBUI_EXPORT void webui_interface_set_response(size_t window, size_t event_number, const char* response); -// Check if the app still running. +/** + * @brief Check if the app still running. + * + * @return Returns True if app is running + * + * @example bool status = webui_interface_is_app_running(); + */ WEBUI_EXPORT bool webui_interface_is_app_running(void); -// Get a unique window ID. +/** + * @brief Get a unique window ID. + * + * @param window The window number + * + * @return Returns the unique window ID as integer + * + * @example size_t id = webui_interface_get_window_id(myWindow); + */ WEBUI_EXPORT size_t webui_interface_get_window_id(size_t window); -// Get an argument as string at a specific index +/** + * @brief Get an argument as string at a specific index + * + * @param window The window number + * @param event_number The event number + * @param index The argument position + * + * @return Returns argument as string + * + * @example const char* myStr = webui_interface_get_string_at(myWindow, e->event_number, 0); + */ WEBUI_EXPORT const char* webui_interface_get_string_at(size_t window, size_t event_number, size_t index); -// Get an argument as integer at a specific index +/** + * @brief Get an argument as integer at a specific index + * + * @param window The window number + * @param event_number The event number + * @param index The argument position + * + * @return Returns argument as integer + * + * @example long long int myNum = webui_interface_get_int_at(myWindow, e->event_number, 0); + */ WEBUI_EXPORT long long int webui_interface_get_int_at(size_t window, size_t event_number, size_t index); // Get an argument as boolean at a specific index +/** + * @brief Get an argument as boolean at a specific index + * + * @param window The window number + * @param event_number The event number + * @param index The argument position + * + * @return Returns argument as boolean + * + * @example bool myBool = webui_interface_get_bool_at(myWindow, e->event_number, 0); + */ WEBUI_EXPORT bool webui_interface_get_bool_at(size_t window, size_t event_number, size_t index); #endif /* _WEBUI_H */ diff --git a/include/webui.hpp b/include/webui.hpp index e12285bb..53013594 100644 --- a/include/webui.hpp +++ b/include/webui.hpp @@ -1,5 +1,5 @@ /* - WebUI Library 2.4.0 + WebUI Library 2.4.0-Beta http://webui.me https://github.com/webui-dev/webui Copyright (c) 2020-2023 Hassan Draga. @@ -30,187 +30,191 @@ static constexpr int NAVIGATION = 3; // 3. Window navigation event static constexpr int CALLBACKS = 4; // 4. Function call event class window { - private: - size_t webui_window{webui_new_window()}; + private: + size_t webui_window{webui_new_window()}; - public: - // Event Struct - class event : public webui_event_t { - // Window object constructor that - // initializes the reference, This - // is to avoid creating copies. - event(webui::window& window_obj, webui_event_t c_e) : webui_event_t(c_e) { + public: + // Event Struct + class event : public webui_event_t { + // Window object constructor that + // initializes the reference, This + // is to avoid creating copies. + event(webui::window& window_obj, webui_event_t c_e) : webui_event_t(c_e) { - reinterpret_cast(this)->window = window_obj.webui_window; - } + reinterpret_cast(this)->window = window_obj.webui_window; + } - public: - class handler { + public: + class handler { - public: - using callback_t = void (*)(event*); + public: + using callback_t = void (*)(event*); - private: - static inline std::array callback_list{}; + private: + static inline std::array callback_list{}; - // List of window objects: webui::window - static inline std::array window_list{}; + // List of window objects: webui::window + static inline std::array window_list{}; - public: - handler() = delete; - handler(const handler&) = delete; - handler(handler&&) = delete; - handler& operator=(const handler&) = delete; - handler& operator=(handler&&) = delete; - ~handler() = delete; + public: + handler() = delete; + handler(const handler&) = delete; + handler(handler&&) = delete; + handler& operator=(const handler&) = delete; + handler& operator=(handler&&) = delete; + ~handler() = delete; - static void add(size_t id, webui::window* win, callback_t func) { - // Save window object - window_list[id] = win; - // Save callback - callback_list[id] = func; - } + static void add(size_t id, webui::window* win, callback_t func) { + // Save window object + window_list[id] = win; + // Save callback + callback_list[id] = func; + } - static void handle(webui_event_t* c_e) { - // Get the binded unique ID - const size_t id = c_e->bind_id; - if (id > 0) { - // Create a new event struct - event e(*window_list[id], *c_e); - // Call the user callback - if (callback_list[id] != nullptr) - callback_list[id](&e); - } - } + static void handle(webui_event_t* c_e) { + // Get the binded unique ID + const size_t id = c_e->bind_id; + if (id > 0) { + // Create a new event struct + event e(*window_list[id], *c_e); + // Call the user callback + if (callback_list[id] != nullptr) + callback_list[id](&e); + } + } - static webui::window& get_window(const size_t index) { return *window_list[index]; } - }; + static webui::window& get_window(const size_t index) { return *window_list[index]; } + }; - // Get an argument as integer at a specific index. - long long int get_int(size_t index = 0) { return webui_get_int_at(this, index); } + // Get an argument as integer at a specific index. + long long int get_int(size_t index = 0) { return webui_get_int_at(this, index); } - // Get the size in bytes of an argument at a specific index. - size_t get_size(size_t index = 0) { return webui_get_size_at(this, index); } + // Get the size in bytes of an argument at a specific index. + size_t get_size(size_t index = 0) { return webui_get_size_at(this, index); } - // Get an argument as string at a specific index. - std::string get_string(size_t index = 0) { return std::string{webui_get_string_at(this, index)}; } + // Get an argument as string at a specific index. + std::string get_string(size_t index = 0) { return std::string{webui_get_string_at(this, index)}; } - // Get an argument as string_view at a specific index. - std::string_view get_string_view(size_t index = 0) { - return std::string_view{webui_get_string_at(this, index)}; - } + // Get an argument as string_view at a specific index. + std::string_view get_string_view(size_t index = 0) { + return std::string_view{webui_get_string_at(this, index)}; + } - // Get an argument as boolean at a specific index. - bool get_bool(size_t index = 0) { return webui_get_bool_at(this, index); } + // Get an argument as boolean at a specific index. + bool get_bool(size_t index = 0) { return webui_get_bool_at(this, index); } - // Return the response to JavaScript as integer. - void return_int(long long int n) { webui_return_int(this, n); } + // Return the response to JavaScript as integer. + void return_int(long long int n) { webui_return_int(this, n); } - // Return the response to JavaScript as string. - void return_string(const std::string_view s) { webui_return_string(this, s.data()); } + // Return the response to JavaScript as string. + void return_string(const std::string_view s) { webui_return_string(this, s.data()); } - // Return the response to JavaScript as boolean. - void return_bool(bool b) { webui_return_bool(this, b); } + // Return the response to JavaScript as boolean. + void return_bool(bool b) { webui_return_bool(this, b); } - webui::window& get_window() { return event::handler::get_window(window); } + webui::window& get_window() { return event::handler::get_window(window); } - size_t get_type() const { return event_type; } + size_t get_type() const { return event_type; } - std::string_view get_element() const { return std::string_view{element}; } + std::string_view get_element() const { return std::string_view{element}; } - size_t number() const { return event_number; } - }; + size_t number() const { return event_number; } + }; - // Bind a specific html element click event with a function. Empty element means all events. - void bind(const std::string_view element, event::handler::callback_t func) { - // Get unique ID - const size_t id = webui_bind(webui_window, element.data(), event::handler::handle); - event::handler::add(id, this, func); - } + // Bind a specific html element click event with a function. Empty element means all events. + void bind(const std::string_view element, event::handler::callback_t func) { + // Get unique ID + const size_t id = webui_bind(webui_window, element.data(), event::handler::handle); + event::handler::add(id, this, func); + } - // Show a window using a embedded HTML, or a file. If the window is already opened then it will be - // refreshed. - bool show(const std::string_view content) const { return webui_show(webui_window, content.data()); } + // Show a window using a embedded HTML, or a file. If the window is already opened then it will be + // refreshed. + bool show(const std::string_view content) const { return webui_show(webui_window, content.data()); } - // Same as show(). But with a specific web browser. - bool show_browser(const std::string_view content, unsigned int browser) const { - return webui_show_browser(webui_window, content.data(), browser); - } + // Same as show(). But with a specific web browser. + bool show_browser(const std::string_view content, unsigned int browser) const { + return webui_show_browser(webui_window, content.data(), browser); + } - // Set the window in Kiosk mode (Full screen) - void set_kiosk(bool status) const { webui_set_kiosk(webui_window, status); } + // Set the window in Kiosk mode (Full screen) + void set_kiosk(bool status) const { webui_set_kiosk(webui_window, status); } - // Close a specific window only. The window object will still exist. - void close() const { webui_close(webui_window); } + // Close a specific window only. The window object will still exist. + void close() const { webui_close(webui_window); } - // Close a specific window and free all memory resources. - void destroy() const { webui_destroy(webui_window); } + // Close a specific window and free all memory resources. + void destroy() const { webui_destroy(webui_window); } - // Check a specific window if it's still running - bool is_shown() const { return webui_is_shown(webui_window); } + // Check a specific window if it's still running + bool is_shown() const { return webui_is_shown(webui_window); } - // Set the default embedded HTML favicon - void set_icon(const std::string_view icon, const std::string_view icon_type) const { - webui_set_icon(webui_window, icon.data(), icon_type.data()); - } + // Set the default embedded HTML favicon + void set_icon(const std::string_view icon, const std::string_view icon_type) const { + webui_set_icon(webui_window, icon.data(), icon_type.data()); + } - // Safely send raw data to the UI - void send_raw(const std::string_view function, const void* raw, size_t size) const { - webui_send_raw(webui_window, function.data(), raw, size); - } + // Safely send raw data to the UI + void send_raw(const std::string_view function, const void* raw, size_t size) const { + webui_send_raw(webui_window, function.data(), raw, size); + } - // Run the window in hidden mode - void set_hide(bool status) const { webui_set_hide(webui_window, status); } + // Run the window in hidden mode + void set_hide(bool status) const { webui_set_hide(webui_window, status); } - // Set window size - void set_size(unsigned int width, unsigned int height) const { webui_set_size(webui_window, width, height); } + // Set window size + void set_size(unsigned int width, unsigned int height) const { webui_set_size(webui_window, width, height); } - // Set window position - void set_position(unsigned int x, unsigned int y) const { webui_set_position(webui_window, x, y); } + // Set a custom web-server network port to be used by WebUI. This can be useful to determine the HTTP link of `webui.js` + // in case you are trying to use WebUI with an external web-server like NGNIX + void set_size(size_t port) const { webui_set_port(webui_window, port); } - // Delete a specific window web-browser local folder profile. - void webui_delete_profile(size_t window) const { webui_delete_profile(webui_window); } + // Set window position + void set_position(unsigned int x, unsigned int y) const { webui_set_position(webui_window, x, y); } - // Get the ID of the parent process (The web browser may create another process for the window). - size_t get_parent_process_id() const { return webui_get_parent_process_id(webui_window); } + // Delete a specific window web-browser local folder profile. + void webui_delete_profile(size_t window) const { webui_delete_profile(webui_window); } - // Get the ID of the last child process spawned by the browser. - size_t get_child_process_id() const { return webui_get_child_process_id(webui_window); } + // Get the ID of the parent process (The web browser may create another process for the window). + size_t get_parent_process_id() const { return webui_get_parent_process_id(webui_window); } - // Set the web-server root folder path for this specific window. - bool set_root_folder(const std::string_view path) const { - return webui_set_root_folder(webui_window, path.data()); - } + // Get the ID of the last child process spawned by the browser. + size_t get_child_process_id() const { return webui_get_child_process_id(webui_window); } - // Set a custom handler to serve files. - void set_file_handler(const void* (*handler)(const char* filename, int* length)) const { - webui_set_file_handler(webui_window, handler); - } + // Set the web-server root folder path for this specific window. + bool set_root_folder(const std::string_view path) const { + return webui_set_root_folder(webui_window, path.data()); + } - // Set the web browser profile to use. An empty `name` and `path` means the default user profile. Need - // to be called before `webui_show()`. - void set_profile(const std::string_view name = {""}, const std::string_view path = {""}) const { - webui_set_profile(webui_window, name.data(), path.data()); - } + // Set a custom handler to serve files. + void set_file_handler(const void* (*handler)(const char* filename, int* length)) const { + webui_set_file_handler(webui_window, handler); + } - // Get the full current URL - std::string_view get_url() const { return std::string_view{webui_get_url(webui_window)}; } + // Set the web browser profile to use. An empty `name` and `path` means the default user profile. Need + // to be called before `webui_show()`. + void set_profile(const std::string_view name = {""}, const std::string_view path = {""}) const { + webui_set_profile(webui_window, name.data(), path.data()); + } - // Navigate to a specific URL. - void navigate(const std::string_view url) const { webui_navigate(webui_window, url.data()); } + // Get the full current URL + std::string_view get_url() const { return std::string_view{webui_get_url(webui_window)}; } - // -- JavaScript ---------------------- + // Navigate to a specific URL. + void navigate(const std::string_view url) const { webui_navigate(webui_window, url.data()); } - // Quickly run a JavaScript (no response waiting). - void run(const std::string_view script) const { webui_run(webui_window, script.data()); } + // -- JavaScript ---------------------- - // Run a JavaScript, and get the response back (Make sure your local buffer can hold the response). - bool script(const std::string_view script, unsigned int timeout, char* buffer, size_t buffer_length) const { - return webui_script(webui_window, script.data(), timeout, buffer, buffer_length); - } + // Quickly run a JavaScript (no response waiting). + void run(const std::string_view script) const { webui_run(webui_window, script.data()); } - // Chose between Deno and Nodejs runtime for .js and .ts files. - void set_runtime(unsigned int runtime) const { webui_set_runtime(webui_window, runtime); } + // Run a JavaScript, and get the response back (Make sure your local buffer can hold the response). + bool script(const std::string_view script, unsigned int timeout, char* buffer, size_t buffer_length) const { + return webui_script(webui_window, script.data(), timeout, buffer, buffer_length); + } + + // Chose between Deno and Nodejs runtime for .js and .ts files. + void set_runtime(unsigned int runtime) const { webui_set_runtime(webui_window, runtime); } }; // Wait until all opened windows get closed. @@ -231,6 +235,11 @@ inline std::string encode(const std::string_view str) { return std::string{webui // Base64 decoding. Use this to safely decode received Base64 text from the UI. If it fails it will return NULL. inline std::string decode(const std::string_view str) { return std::string{webui_decode(str.data())}; } +// Set the SSL/TLS certificate and the private key content, both in PEM format. +// This works only with `webui-2-secure` library. If set empty WebUI will generate a self-signed certificate. +inline void set_tls_certificate(const std::string_view certificate_pem, const std::string_view private_key_pem) { + webui_set_tls_certificate(certificate_pem.data(), private_key_pem.data()); } + // Safely free a buffer allocated by WebUI, for example when using webui_encode(). inline void free(void* ptr) { webui_free(ptr); }