|
| 1 | +/* |
| 2 | + WebUI Library 2.4.0 |
| 3 | + http://webui.me |
| 4 | + https://github.com/webui-dev/webui |
| 5 | + Copyright (c) 2020-2023 Hassan Draga. |
| 6 | + Licensed under MIT License. |
| 7 | + All rights reserved. |
| 8 | + Canada. |
| 9 | +*/ |
| 10 | + |
| 11 | +#ifndef _WEBUI_HPP |
| 12 | +#define _WEBUI_HPP |
| 13 | + |
| 14 | +// C++ STD |
| 15 | +#include <array> |
| 16 | +#include <string> |
| 17 | +#include <string_view> |
| 18 | + |
| 19 | +// WebUI C Header |
| 20 | +extern "C" { |
| 21 | +#include "webui.h" |
| 22 | +} |
| 23 | + |
| 24 | +namespace webui { |
| 25 | + |
| 26 | +static constexpr int DISCONNECTED = 0; // 0. Window disconnection event |
| 27 | +static constexpr int CONNECTED = 1; // 1. Window connection event |
| 28 | +static constexpr int MOUSE_CLICK = 2; // 2. Mouse click event |
| 29 | +static constexpr int NAVIGATION = 3; // 3. Window navigation event |
| 30 | +static constexpr int CALLBACKS = 4; // 4. Function call event |
| 31 | + |
| 32 | +class window { |
| 33 | + private: |
| 34 | + size_t webui_window{webui_new_window()}; |
| 35 | + |
| 36 | + public: |
| 37 | + // Event Struct |
| 38 | + class event : public webui_event_t { |
| 39 | + // Window object constructor that |
| 40 | + // initializes the reference, This |
| 41 | + // is to avoid creating copies. |
| 42 | + event(webui::window& window_obj, webui_event_t c_e) : webui_event_t(c_e) { |
| 43 | + |
| 44 | + reinterpret_cast<webui_event_t*>(this)->window = window_obj.webui_window; |
| 45 | + } |
| 46 | + |
| 47 | + public: |
| 48 | + class handler { |
| 49 | + |
| 50 | + public: |
| 51 | + using callback_t = void (*)(event*); |
| 52 | + |
| 53 | + private: |
| 54 | + static inline std::array<callback_t, 512> callback_list{}; |
| 55 | + |
| 56 | + // List of window objects: webui::window |
| 57 | + static inline std::array<webui::window*, 512> window_list{}; |
| 58 | + |
| 59 | + public: |
| 60 | + handler() = delete; |
| 61 | + handler(const handler&) = delete; |
| 62 | + handler(handler&&) = delete; |
| 63 | + handler& operator=(const handler&) = delete; |
| 64 | + handler& operator=(handler&&) = delete; |
| 65 | + ~handler() = delete; |
| 66 | + |
| 67 | + static void add(size_t id, webui::window* win, callback_t func) { |
| 68 | + // Save window object |
| 69 | + window_list[id] = win; |
| 70 | + // Save callback |
| 71 | + callback_list[id] = func; |
| 72 | + } |
| 73 | + |
| 74 | + static void handle(webui_event_t* c_e) { |
| 75 | + // Get the binded unique ID |
| 76 | + const size_t id = c_e->bind_id; |
| 77 | + if (id > 0) { |
| 78 | + // Create a new event struct |
| 79 | + event e(*window_list[id], *c_e); |
| 80 | + // Call the user callback |
| 81 | + if (callback_list[id] != nullptr) |
| 82 | + callback_list[id](&e); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + static webui::window& get_window(const size_t index) { return *window_list[index]; } |
| 87 | + }; |
| 88 | + |
| 89 | + // Get an argument as integer at a specific index. |
| 90 | + long long int get_int(size_t index = 0) { return webui_get_int_at(this, index); } |
| 91 | + |
| 92 | + // Get the size in bytes of an argument at a specific index. |
| 93 | + size_t get_size(size_t index = 0) { return webui_get_size_at(this, index); } |
| 94 | + |
| 95 | + // Get an argument as string at a specific index. |
| 96 | + std::string get_string(size_t index = 0) { return std::string{webui_get_string_at(this, index)}; } |
| 97 | + |
| 98 | + // Get an argument as string_view at a specific index. |
| 99 | + std::string_view get_string_view(size_t index = 0) { |
| 100 | + return std::string_view{webui_get_string_at(this, index)}; |
| 101 | + } |
| 102 | + |
| 103 | + // Get an argument as boolean at a specific index. |
| 104 | + bool get_bool(size_t index = 0) { return webui_get_bool_at(this, index); } |
| 105 | + |
| 106 | + // Return the response to JavaScript as integer. |
| 107 | + void return_int(long long int n) { webui_return_int(this, n); } |
| 108 | + |
| 109 | + // Return the response to JavaScript as string. |
| 110 | + void return_string(const std::string_view s) { webui_return_string(this, s.data()); } |
| 111 | + |
| 112 | + // Return the response to JavaScript as boolean. |
| 113 | + void return_bool(bool b) { webui_return_bool(this, b); } |
| 114 | + |
| 115 | + webui::window& get_window() { return event::handler::get_window(window); } |
| 116 | + |
| 117 | + size_t get_type() const { return event_type; } |
| 118 | + |
| 119 | + std::string_view get_element() const { return std::string_view{element}; } |
| 120 | + |
| 121 | + size_t number() const { return event_number; } |
| 122 | + }; |
| 123 | + |
| 124 | + // Bind a specific html element click event with a function. Empty element means all events. |
| 125 | + void bind(const std::string_view element, event::handler::callback_t func) { |
| 126 | + // Get unique ID |
| 127 | + const size_t id = webui_bind(webui_window, element.data(), event::handler::handle); |
| 128 | + event::handler::add(id, this, func); |
| 129 | + } |
| 130 | + |
| 131 | + // Show a window using a embedded HTML, or a file. If the window is already opened then it will be |
| 132 | + // refreshed. |
| 133 | + bool show(const std::string_view content) const { return webui_show(webui_window, content.data()); } |
| 134 | + |
| 135 | + // Same as show(). But with a specific web browser. |
| 136 | + bool show_browser(const std::string_view content, unsigned int browser) const { |
| 137 | + return webui_show_browser(webui_window, content.data(), browser); |
| 138 | + } |
| 139 | + |
| 140 | + // Set the window in Kiosk mode (Full screen) |
| 141 | + void set_kiosk(bool status) const { webui_set_kiosk(webui_window, status); } |
| 142 | + |
| 143 | + // Close a specific window only. The window object will still exist. |
| 144 | + void close() const { webui_close(webui_window); } |
| 145 | + |
| 146 | + // Close a specific window and free all memory resources. |
| 147 | + void destroy() const { webui_destroy(webui_window); } |
| 148 | + |
| 149 | + // Check a specific window if it's still running |
| 150 | + bool is_shown() const { return webui_is_shown(webui_window); } |
| 151 | + |
| 152 | + // Set the default embedded HTML favicon |
| 153 | + void set_icon(const std::string_view icon, const std::string_view icon_type) const { |
| 154 | + webui_set_icon(webui_window, icon.data(), icon_type.data()); |
| 155 | + } |
| 156 | + |
| 157 | + // Safely send raw data to the UI |
| 158 | + void send_raw(const std::string_view function, const void* raw, size_t size) const { |
| 159 | + webui_send_raw(webui_window, function.data(), raw, size); |
| 160 | + } |
| 161 | + |
| 162 | + // Run the window in hidden mode |
| 163 | + void set_hide(bool status) const { webui_set_hide(webui_window, status); } |
| 164 | + |
| 165 | + // Set window size |
| 166 | + void set_size(unsigned int width, unsigned int height) const { webui_set_size(webui_window, width, height); } |
| 167 | + |
| 168 | + // Set a custom web-server network port to be used by WebUI. This can be useful to determine the HTTP link of `webui.js` |
| 169 | + // in case you are trying to use WebUI with an external web-server like NGNIX |
| 170 | + void set_size(size_t port) const { webui_set_port(webui_window, port); } |
| 171 | + |
| 172 | + // Set window position |
| 173 | + void set_position(unsigned int x, unsigned int y) const { webui_set_position(webui_window, x, y); } |
| 174 | + |
| 175 | + // Delete a specific window web-browser local folder profile. |
| 176 | + void webui_delete_profile(size_t window) const { webui_delete_profile(webui_window); } |
| 177 | + |
| 178 | + // Get the ID of the parent process (The web browser may create another process for the window). |
| 179 | + size_t get_parent_process_id() const { return webui_get_parent_process_id(webui_window); } |
| 180 | + |
| 181 | + // Get the ID of the last child process spawned by the browser. |
| 182 | + size_t get_child_process_id() const { return webui_get_child_process_id(webui_window); } |
| 183 | + |
| 184 | + // Set the web-server root folder path for this specific window. |
| 185 | + bool set_root_folder(const std::string_view path) const { |
| 186 | + return webui_set_root_folder(webui_window, path.data()); |
| 187 | + } |
| 188 | + |
| 189 | + // Set a custom handler to serve files. |
| 190 | + void set_file_handler(const void* (*handler)(const char* filename, int* length)) const { |
| 191 | + webui_set_file_handler(webui_window, handler); |
| 192 | + } |
| 193 | + |
| 194 | + // Set the web browser profile to use. An empty `name` and `path` means the default user profile. Need |
| 195 | + // to be called before `webui_show()`. |
| 196 | + void set_profile(const std::string_view name = {""}, const std::string_view path = {""}) const { |
| 197 | + webui_set_profile(webui_window, name.data(), path.data()); |
| 198 | + } |
| 199 | + |
| 200 | + // Get the full current URL |
| 201 | + std::string_view get_url() const { return std::string_view{webui_get_url(webui_window)}; } |
| 202 | + |
| 203 | + // Navigate to a specific URL. |
| 204 | + void navigate(const std::string_view url) const { webui_navigate(webui_window, url.data()); } |
| 205 | + |
| 206 | + // -- JavaScript ---------------------- |
| 207 | + |
| 208 | + // Quickly run a JavaScript (no response waiting). |
| 209 | + void run(const std::string_view script) const { webui_run(webui_window, script.data()); } |
| 210 | + |
| 211 | + // Run a JavaScript, and get the response back (Make sure your local buffer can hold the response). |
| 212 | + bool script(const std::string_view script, unsigned int timeout, char* buffer, size_t buffer_length) const { |
| 213 | + return webui_script(webui_window, script.data(), timeout, buffer, buffer_length); |
| 214 | + } |
| 215 | + |
| 216 | + // Chose between Deno and Nodejs runtime for .js and .ts files. |
| 217 | + void set_runtime(unsigned int runtime) const { webui_set_runtime(webui_window, runtime); } |
| 218 | +}; |
| 219 | + |
| 220 | +// Wait until all opened windows get closed. |
| 221 | +inline void wait() { webui_wait(); } |
| 222 | + |
| 223 | +// Close all opened windows. wait() will break. |
| 224 | +inline void exit() { webui_exit(); } |
| 225 | + |
| 226 | +// Set the web-server root folder path for all windows. |
| 227 | +inline bool set_default_root_folder(const std::string_view path) { return webui_set_default_root_folder(path.data()); } |
| 228 | + |
| 229 | +// Set the maximum time in seconds to wait for browser to start |
| 230 | +inline void set_timeout(unsigned int second) { webui_set_timeout(second); } |
| 231 | + |
| 232 | +// Base64 encoding. Use this to safely send text based data to the UI. If it fails it will return NULL. |
| 233 | +inline std::string encode(const std::string_view str) { return std::string{webui_encode(str.data())}; } |
| 234 | + |
| 235 | +// Base64 decoding. Use this to safely decode received Base64 text from the UI. If it fails it will return NULL. |
| 236 | +inline std::string decode(const std::string_view str) { return std::string{webui_decode(str.data())}; } |
| 237 | + |
| 238 | +// Set the SSL/TLS certificate and the private key content, both in PEM format. |
| 239 | +// This works only with `webui-2-secure` library. If set empty WebUI will generate a self-signed certificate. |
| 240 | +inline void set_tls_certificate(const std::string_view certificate_pem, const std::string_view private_key_pem) { |
| 241 | + webui_set_tls_certificate(certificate_pem.data(), private_key_pem.data()); } |
| 242 | + |
| 243 | +// Safely free a buffer allocated by WebUI, for example when using webui_encode(). |
| 244 | +inline void free(void* ptr) { webui_free(ptr); } |
| 245 | + |
| 246 | +// Safely free a buffer allocated by WebUI, for example when using webui_encode(). |
| 247 | +inline void* malloc(size_t size) { return webui_malloc(size); } |
| 248 | + |
| 249 | +// Free all memory resources. Should be called only at the end. |
| 250 | +inline void clean() { webui_clean(); } |
| 251 | + |
| 252 | +// Delete all local web-browser profiles folder. It should called at the end. |
| 253 | +inline void delete_all_profiles() { webui_delete_all_profiles(); } |
| 254 | +} // namespace webui |
| 255 | + |
| 256 | +#endif /* _WEBUI_HPP */ |
0 commit comments