2023-04-26 20:08:57 +03:00
|
|
|
/*
|
2023-08-20 00:49:08 +03:00
|
|
|
WebUI Library 2.4.0
|
2023-04-26 20:08:57 +03:00
|
|
|
http://webui.me
|
2023-05-13 17:47:42 +03:00
|
|
|
https://github.com/webui-dev/webui
|
2023-04-26 20:08:57 +03:00
|
|
|
Copyright (c) 2020-2023 Hassan Draga.
|
2023-05-05 04:41:03 +03:00
|
|
|
Licensed under MIT License.
|
2023-04-26 20:08:57 +03:00
|
|
|
All rights reserved.
|
|
|
|
Canada.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _WEBUI_HPP
|
|
|
|
#define _WEBUI_HPP
|
|
|
|
|
|
|
|
// C++ STD
|
|
|
|
#include <string>
|
2023-05-24 01:06:37 +03:00
|
|
|
#include <string_view>
|
2023-05-24 14:27:22 +03:00
|
|
|
#include <array>
|
2023-04-26 20:08:57 +03:00
|
|
|
|
|
|
|
// WebUI C Header
|
|
|
|
extern "C" {
|
2023-05-06 00:41:20 +03:00
|
|
|
#include "webui.h"
|
2023-04-26 20:08:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace webui {
|
|
|
|
|
2023-05-27 23:33:36 +03:00
|
|
|
static constexpr int DISCONNECTED = 0; // 0. Window disconnection event
|
|
|
|
static constexpr int CONNECTED = 1; // 1. Window connection event
|
2023-10-07 00:25:47 +03:00
|
|
|
static constexpr int MOUSE_CLICK = 2; // 2. Mouse click event
|
|
|
|
static constexpr int NAVIGATION = 3; // 3. Window navigation event
|
|
|
|
static constexpr int CALLBACKS = 4; // 4. Function call event
|
2023-05-06 00:41:20 +03:00
|
|
|
|
|
|
|
class window {
|
|
|
|
private:
|
2023-05-25 02:48:28 +03:00
|
|
|
size_t webui_window {
|
|
|
|
webui_new_window()
|
|
|
|
};
|
2023-05-24 14:27:22 +03:00
|
|
|
|
2023-05-06 00:41:20 +03:00
|
|
|
public:
|
2023-05-25 02:48:28 +03:00
|
|
|
// Event Struct
|
2023-05-27 23:33:36 +03:00
|
|
|
class event : public webui_event_t {
|
2023-05-24 14:27:22 +03:00
|
|
|
// Window object constructor that
|
|
|
|
// initializes the reference, This
|
|
|
|
// is to avoid creating copies.
|
2023-05-25 02:48:28 +03:00
|
|
|
event(webui::window& window_obj, webui_event_t c_e) : webui_event_t(c_e) {
|
|
|
|
|
2023-05-25 01:46:33 +03:00
|
|
|
reinterpret_cast<webui_event_t*>(this)->window = window_obj.webui_window;
|
|
|
|
}
|
2023-05-27 23:33:36 +03:00
|
|
|
public:
|
2023-05-24 14:27:22 +03:00
|
|
|
|
2023-05-25 02:48:28 +03:00
|
|
|
class handler {
|
|
|
|
|
2023-05-24 14:27:22 +03:00
|
|
|
public:
|
|
|
|
using callback_t = void(*)(event*);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static inline std::array<callback_t, 512> callback_list{};
|
|
|
|
|
|
|
|
// List of window objects: webui::window
|
|
|
|
static inline std::array<webui::window*, 512> window_list{};
|
|
|
|
|
|
|
|
public:
|
|
|
|
handler() = delete;
|
|
|
|
handler(const handler&) = delete;
|
|
|
|
handler(handler&&) = delete;
|
2023-05-25 02:48:28 +03:00
|
|
|
handler& operator = (const handler&) = delete;
|
|
|
|
handler& operator = (handler&&) = delete;
|
2023-05-24 14:27:22 +03:00
|
|
|
~handler() = delete;
|
|
|
|
|
2023-05-25 02:48:28 +03:00
|
|
|
static void add(size_t id, webui::window* win, callback_t func) {
|
2023-10-07 00:25:47 +03:00
|
|
|
// Save window object
|
2023-05-24 14:27:22 +03:00
|
|
|
window_list[id] = win;
|
|
|
|
// Save callback
|
|
|
|
callback_list[id] = func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle(webui_event_t* c_e) {
|
2023-10-07 00:25:47 +03:00
|
|
|
// 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);
|
2023-05-24 14:27:22 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-25 22:45:42 +03:00
|
|
|
|
|
|
|
static webui::window& get_window(const size_t index){
|
|
|
|
return *window_list[index];
|
|
|
|
}
|
2023-05-24 14:27:22 +03:00
|
|
|
};
|
|
|
|
|
2023-10-07 00:25:47 +03:00
|
|
|
// 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);
|
2023-05-24 14:27:22 +03:00
|
|
|
}
|
|
|
|
|
2023-10-07 00:25:47 +03:00
|
|
|
// 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);
|
2023-05-24 14:27:22 +03:00
|
|
|
}
|
|
|
|
|
2023-10-07 00:25:47 +03:00
|
|
|
// 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)};
|
2023-05-27 23:33:36 +03:00
|
|
|
}
|
|
|
|
|
2023-10-07 00:25:47 +03:00
|
|
|
// 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);
|
2023-05-24 14:27:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the response to JavaScript as integer.
|
2023-05-25 02:48:28 +03:00
|
|
|
void return_int(long long int n) {
|
2023-05-24 14:27:22 +03:00
|
|
|
webui_return_int(this, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the response to JavaScript as string.
|
2023-05-25 02:48:28 +03:00
|
|
|
void return_string(const std::string_view s) {
|
2023-05-24 14:27:22 +03:00
|
|
|
webui_return_string(this, s.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the response to JavaScript as boolean.
|
2023-05-25 02:48:28 +03:00
|
|
|
void return_bool(bool b) {
|
2023-05-24 14:27:22 +03:00
|
|
|
webui_return_bool(this, b);
|
|
|
|
}
|
2023-05-25 22:45:42 +03:00
|
|
|
|
|
|
|
webui::window& get_window(){
|
|
|
|
return event::handler::get_window(window);
|
|
|
|
}
|
2023-05-27 23:33:36 +03:00
|
|
|
|
|
|
|
size_t get_type() const {
|
|
|
|
return event_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view get_element() const {
|
|
|
|
return std::string_view{element};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t number() const {
|
|
|
|
return event_number;
|
|
|
|
}
|
2023-05-24 14:27:22 +03:00
|
|
|
};
|
2023-05-06 00:41:20 +03:00
|
|
|
|
|
|
|
// Bind a specific html element click event with a function. Empty element means all events.
|
2023-05-24 14:27:22 +03:00
|
|
|
void bind(const std::string_view element, event::handler::callback_t func) {
|
2023-05-06 00:41:20 +03:00
|
|
|
// Get unique ID
|
2023-05-24 14:27:22 +03:00
|
|
|
const size_t id = webui_bind(webui_window, element.data(), event::handler::handle);
|
|
|
|
event::handler::add(id, this, func);
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show a window using a embedded HTML, or a file. If the window is already opened then it will be refreshed.
|
2023-05-24 01:06:37 +03:00
|
|
|
bool show(const std::string_view content) const {
|
2023-05-24 14:27:22 +03:00
|
|
|
return webui_show(webui_window, content.data());
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Same as show(). But with a specific web browser.
|
2023-05-24 01:06:37 +03:00
|
|
|
bool show_browser(const std::string_view content, unsigned int browser) const {
|
2023-05-24 14:27:22 +03:00
|
|
|
return webui_show_browser(webui_window, content.data(), browser);
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
2023-09-25 18:55:31 +03:00
|
|
|
// 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.
|
2023-05-24 01:06:37 +03:00
|
|
|
void close() const {
|
2023-05-24 14:27:22 +03:00
|
|
|
webui_close(webui_window);
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
2023-09-25 18:55:31 +03:00
|
|
|
// Close a specific window and free all memory resources.
|
|
|
|
void destroy() const {
|
|
|
|
webui_destroy(webui_window);
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check a specific window if it's still running
|
2023-05-24 01:06:37 +03:00
|
|
|
bool is_shown() const {
|
2023-05-24 14:27:22 +03:00
|
|
|
return webui_is_shown(webui_window);
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the default embedded HTML favicon
|
2023-05-27 23:33:36 +03:00
|
|
|
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());
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 21:36:25 +03:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2023-08-29 01:53:32 +03:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2023-09-25 18:55:31 +03:00
|
|
|
// 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 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2023-09-22 11:51:52 +03:00
|
|
|
// 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 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 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the full current URL
|
|
|
|
std::string_view get_url() const {
|
|
|
|
return std::string_view{webui_get_url(webui_window)};
|
|
|
|
}
|
2023-10-07 00:25:47 +03:00
|
|
|
|
|
|
|
// Navigate to a specific URL.
|
|
|
|
void navigate(const std::string_view url) const {
|
|
|
|
webui_navigate(webui_window, url.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- JavaScript ----------------------
|
|
|
|
|
|
|
|
// Quickly run a JavaScript (no response waiting).
|
|
|
|
void run(const std::string_view script) const {
|
|
|
|
webui_run(webui_window, script.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2023-05-06 00:41:20 +03:00
|
|
|
};
|
2023-05-06 20:02:34 +03:00
|
|
|
|
|
|
|
// Wait until all opened windows get closed.
|
2023-05-24 01:06:37 +03:00
|
|
|
inline void wait() {
|
2023-05-06 20:02:34 +03:00
|
|
|
webui_wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close all opened windows. wait() will break.
|
2023-05-25 02:48:28 +03:00
|
|
|
inline void exit() {
|
2023-05-06 20:02:34 +03:00
|
|
|
webui_exit();
|
|
|
|
}
|
|
|
|
|
2023-09-25 18:55:31 +03:00
|
|
|
// Set the web-server root folder path for all windows.
|
|
|
|
inline bool set_default_root_folder(const std::string_view path){
|
|
|
|
return webui_set_default_root_folder(path.data());
|
|
|
|
}
|
|
|
|
|
2023-05-06 20:02:34 +03:00
|
|
|
// Set the maximum time in seconds to wait for browser to start
|
2023-05-24 01:06:37 +03:00
|
|
|
inline void set_timeout(unsigned int second) {
|
2023-05-06 20:02:34 +03:00
|
|
|
webui_set_timeout(second);
|
|
|
|
}
|
2023-05-09 14:48:27 +03:00
|
|
|
|
|
|
|
// Base64 encoding. Use this to safely send text based data to the UI. If it fails it will return NULL.
|
2023-05-24 01:06:37 +03:00
|
|
|
inline std::string encode(const std::string_view str) {
|
2023-05-27 23:33:36 +03:00
|
|
|
return std::string{webui_encode(str.data())};
|
2023-05-09 14:48:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Base64 decoding. Use this to safely decode received Base64 text from the UI. If it fails it will return NULL.
|
2023-05-24 01:06:37 +03:00
|
|
|
inline std::string decode(const std::string_view str) {
|
2023-05-27 23:33:36 +03:00
|
|
|
return std::string{webui_decode(str.data())};
|
2023-05-09 14:48:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Safely free a buffer allocated by WebUI, for example when using webui_encode().
|
2023-05-24 01:06:37 +03:00
|
|
|
inline void free(void* ptr) {
|
2023-05-09 14:48:27 +03:00
|
|
|
webui_free(ptr);
|
|
|
|
}
|
2023-09-22 11:51:52 +03:00
|
|
|
|
2023-09-25 18:55:31 +03:00
|
|
|
// Safely free a buffer allocated by WebUI, for example when using webui_encode().
|
|
|
|
inline void* malloc(size_t size) {
|
|
|
|
return webui_malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free all memory resources. Should be called only at the end.
|
|
|
|
inline void clean() {
|
|
|
|
webui_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all local web-browser profiles folder. It should called at the end.
|
|
|
|
inline void delete_all_profiles() {
|
|
|
|
webui_delete_all_profiles();
|
2023-09-22 11:51:52 +03:00
|
|
|
}
|
2023-04-26 20:08:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _WEBUI_HPP */
|