2023-04-26 20:08:57 +03:00
|
|
|
/*
|
2023-05-05 04:41:03 +03:00
|
|
|
WebUI Library 2.3.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-06 00:41:20 +03:00
|
|
|
const int DISCONNECTED = 0; // 0. Window disconnection event
|
|
|
|
const int CONNECTED = 1; // 1. Window connection event
|
|
|
|
const int MULTI_CONNECTION = 2; // 2. New window connection event
|
|
|
|
const int UNWANTED_CONNECTION = 3; // 3. New unwanted window connection event
|
|
|
|
const int MOUSE_CLICK = 4; // 4. Mouse click event
|
|
|
|
const int NAVIGATION = 5; // 5. Window navigation event
|
|
|
|
const int CALLBACKS = 6; // 6. Function call event
|
|
|
|
|
|
|
|
class window {
|
|
|
|
private:
|
2023-05-24 14:27:22 +03:00
|
|
|
size_t webui_window {webui_new_window()};
|
|
|
|
|
2023-05-06 00:41:20 +03:00
|
|
|
public:
|
2023-05-24 14:27:22 +03:00
|
|
|
// Event struct
|
|
|
|
struct event : public webui_event_t{
|
|
|
|
using webui_event_t::webui_event_t;
|
|
|
|
webui::window& window; // The window object
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
window(window_obj) {
|
|
|
|
reinterpret_cast<webui_event_t*>(this)->window = window_obj.webui_window;
|
|
|
|
}
|
|
|
|
|
|
|
|
class handler{
|
|
|
|
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;
|
|
|
|
handler& operator=(const handler&) = delete;
|
|
|
|
handler& operator=(handler&&) = delete;
|
|
|
|
~handler() = delete;
|
|
|
|
|
|
|
|
static void add(size_t id, webui::window* win, callback_t func){
|
|
|
|
window_list[id] = win;
|
|
|
|
|
|
|
|
// Save callback
|
|
|
|
callback_list[id] = func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle(webui_event_t* c_e) {
|
|
|
|
|
|
|
|
// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
|
|
|
|
const size_t id = webui_interface_get_bind_id(c_e->window, c_e->element);
|
|
|
|
|
|
|
|
if(id < 1){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse argument as integer.
|
|
|
|
long long int get_int() {
|
|
|
|
return webui_get_int(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse argument as string.
|
|
|
|
std::string get_string() {
|
|
|
|
return std::string{webui_get_string(this)};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse argument as boolean.
|
|
|
|
bool get_bool() {
|
|
|
|
return webui_get_bool(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 boolean.
|
|
|
|
void return_bool(bool b) {
|
|
|
|
webui_return_bool(this, b);
|
|
|
|
}
|
|
|
|
};
|
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);
|
2023-05-06 00:41:20 +03:00
|
|
|
|
2023-05-24 14:27:22 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Close a specific window.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Set the window in Kiosk mode (Full screen)
|
2023-05-24 01:06:37 +03:00
|
|
|
void set_kiosk(bool status) const {
|
2023-05-24 14:27:22 +03:00
|
|
|
webui_set_kiosk(webui_window, status);
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Other ---------------------------
|
|
|
|
// 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-24 01:06:37 +03:00
|
|
|
void set_icon(const std::string_view icon, const std::string& icon_type) const {
|
2023-05-24 14:27:22 +03:00
|
|
|
webui_set_icon(webui_window, icon.data(), icon_type.c_str());
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the window URL to be re-used in normal web browsers
|
2023-05-24 01:06:37 +03:00
|
|
|
void set_multi_access(bool status) const {
|
2023-05-24 14:27:22 +03:00
|
|
|
webui_set_multi_access(webui_window, status);
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- JavaScript ----------------------
|
|
|
|
// Quickly run a JavaScript (no response waiting).
|
2023-05-24 01:06:37 +03:00
|
|
|
void run(const std::string_view script) const {
|
2023-05-24 14:27:22 +03:00
|
|
|
webui_run(webui_window, script.data());
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a JavaScript, and get the response back (Make sure your local buffer can hold the response).
|
2023-05-24 01:06:37 +03:00
|
|
|
bool script(const std::string_view script, unsigned int timeout, char* buffer, size_t buffer_length) const {
|
2023-05-24 14:27:22 +03:00
|
|
|
return webui_script(webui_window, script.data(), timeout, buffer, buffer_length);
|
2023-05-06 00:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chose between Deno and Nodejs runtime for .js and .ts files.
|
2023-05-24 01:06:37 +03:00
|
|
|
void set_runtime(unsigned int runtime) const {
|
2023-05-24 14:27:22 +03:00
|
|
|
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-24 01:06:37 +03:00
|
|
|
inline void exit(){
|
2023-05-06 20:02:34 +03:00
|
|
|
webui_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
std::string ret = std::string(webui_encode(str.data()));
|
2023-05-09 14:48:27 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
std::string ret = std::string(webui_decode(str.data()));
|
2023-05-09 14:48:27 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-26 20:08:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _WEBUI_HPP */
|