Window ID Based Creation

# New:

* New API: `void webui_new_window_id(size_t window_number)`
* New API: `void webui_destroy(size_t window)`

_Please see C serve_a_folder example on how to use webui_new_window_id()_

# Changes

* To keep consistency with window IDs, all `unsigned int` changed to `size_t`
This commit is contained in:
Hassan DRAGA 2023-05-07 13:54:15 -04:00
parent 3e1a636288
commit aacc8b0ddd
29 changed files with 605 additions and 365 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -12,6 +12,7 @@
#define _WEBUI_H
#define WEBUI_VERSION "2.3.0"
#define WEBUI_MAX_IDS (512)
// Dynamic Library Exports
#if defined(_MSC_VER) || defined(__TINYC__)
@ -71,6 +72,7 @@
#include <fcntl.h>
#include <poll.h>
#include <sys/time.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -90,6 +92,7 @@
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -131,27 +134,31 @@ enum webui_events {
// -- Structs -------------------------
typedef struct webui_event_t {
size_t window; // The window object number
unsigned int event_type; // Event type
size_t event_type; // Event type
char* element; // HTML element ID
char* data; // JavaScript data
unsigned int event_number; // Internal WebUI
size_t event_number; // Internal WebUI
} webui_event_t;
// -- Definitions ---------------------
// Create a new webui window object.
WEBUI_EXPORT size_t webui_new_window(void);
// Create a new webui window object.
WEBUI_EXPORT void webui_new_window_id(size_t window_number);
// Bind a specific html element click event with a function. Empty element means all events.
WEBUI_EXPORT unsigned int webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
WEBUI_EXPORT size_t webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
// Show a window using a embedded HTML, or a file. If the window is already opened then it will be refreshed.
WEBUI_EXPORT bool webui_show(size_t window, const char* content);
// Same as webui_show(). But with a specific web browser.
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, unsigned int browser);
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, size_t browser);
// Set the window in Kiosk mode (Full screen)
WEBUI_EXPORT void webui_set_kiosk(size_t window, bool status);
// Wait until all opened windows get closed.
WEBUI_EXPORT void webui_wait(void);
// Close a specific window.
// Close a specific window only. The window object will still exist.
WEBUI_EXPORT void webui_close(size_t window);
// Close a specific window and free all memory resources.
WEBUI_EXPORT void webui_destroy(size_t window);
// Close all opened windows. webui_wait() will break.
WEBUI_EXPORT void webui_exit(void);
@ -159,7 +166,7 @@ WEBUI_EXPORT void webui_exit(void);
// Check a specific window if it's still running
WEBUI_EXPORT bool webui_is_shown(size_t window);
// Set the maximum time in seconds to wait for browser to start
WEBUI_EXPORT void webui_set_timeout(unsigned int second);
WEBUI_EXPORT void webui_set_timeout(size_t second);
// Set the default embedded HTML favicon
WEBUI_EXPORT void webui_set_icon(size_t window, const char* icon, const char* icon_type);
// Allow the window URL to be re-used in normal web browsers
@ -169,9 +176,9 @@ WEBUI_EXPORT void webui_set_multi_access(size_t window, bool status);
// Run JavaScript quickly with no waiting for the response.
WEBUI_EXPORT bool webui_run(size_t window, const char* script);
// Run a 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, unsigned int timeout, char* buffer, size_t buffer_length);
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 runtime for .js and .ts files.
WEBUI_EXPORT void webui_set_runtime(size_t window, unsigned int runtime);
WEBUI_EXPORT void webui_set_runtime(size_t window, size_t runtime);
// Parse argument as integer.
WEBUI_EXPORT long long int webui_get_int(webui_event_t* e);
// Parse argument as string.
@ -187,14 +194,14 @@ WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b);
// -- Interface -----------------------
// Bind a specific html element click event with a function. Empty element means all events. This replace webui_bind(). The func is (Window, EventType, Element, Data, EventNumber)
WEBUI_EXPORT unsigned int webui_interface_bind(size_t window, const char* element, void (*func)(size_t, unsigned int, char*, char*, unsigned int));
WEBUI_EXPORT size_t webui_interface_bind(size_t window, const char* element, void (*func)(size_t, size_t, char*, char*, size_t));
// When using `webui_interface_bind()` you may need this function to easily set your callback response.
WEBUI_EXPORT void webui_interface_set_response(size_t window, unsigned int event_number, const char* response);
WEBUI_EXPORT void webui_interface_set_response(size_t window, size_t event_number, const char* response);
// Check if the app still running or not. This replace webui_wait().
WEBUI_EXPORT bool webui_interface_is_app_running(void);
// Get window unique ID
WEBUI_EXPORT unsigned int webui_interface_get_window_id(size_t window);
WEBUI_EXPORT size_t webui_interface_get_window_id(size_t window);
// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
WEBUI_EXPORT unsigned int webui_interface_get_bind_id(size_t window, const char* element);
WEBUI_EXPORT size_t webui_interface_get_bind_id(size_t window, const char* element);
#endif /* _WEBUI_H */

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -12,6 +12,7 @@
#define _WEBUI_H
#define WEBUI_VERSION "2.3.0"
#define WEBUI_MAX_IDS (512)
// Dynamic Library Exports
#if defined(_MSC_VER) || defined(__TINYC__)
@ -71,6 +72,7 @@
#include <fcntl.h>
#include <poll.h>
#include <sys/time.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -90,6 +92,7 @@
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -131,27 +134,31 @@ enum webui_events {
// -- Structs -------------------------
typedef struct webui_event_t {
size_t window; // The window object number
unsigned int event_type; // Event type
size_t event_type; // Event type
char* element; // HTML element ID
char* data; // JavaScript data
unsigned int event_number; // Internal WebUI
size_t event_number; // Internal WebUI
} webui_event_t;
// -- Definitions ---------------------
// Create a new webui window object.
WEBUI_EXPORT size_t webui_new_window(void);
// Create a new webui window object.
WEBUI_EXPORT void webui_new_window_id(size_t window_number);
// Bind a specific html element click event with a function. Empty element means all events.
WEBUI_EXPORT unsigned int webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
WEBUI_EXPORT size_t webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
// Show a window using a embedded HTML, or a file. If the window is already opened then it will be refreshed.
WEBUI_EXPORT bool webui_show(size_t window, const char* content);
// Same as webui_show(). But with a specific web browser.
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, unsigned int browser);
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, size_t browser);
// Set the window in Kiosk mode (Full screen)
WEBUI_EXPORT void webui_set_kiosk(size_t window, bool status);
// Wait until all opened windows get closed.
WEBUI_EXPORT void webui_wait(void);
// Close a specific window.
// Close a specific window only. The window object will still exist.
WEBUI_EXPORT void webui_close(size_t window);
// Close a specific window and free all memory resources.
WEBUI_EXPORT void webui_destroy(size_t window);
// Close all opened windows. webui_wait() will break.
WEBUI_EXPORT void webui_exit(void);
@ -159,7 +166,7 @@ WEBUI_EXPORT void webui_exit(void);
// Check a specific window if it's still running
WEBUI_EXPORT bool webui_is_shown(size_t window);
// Set the maximum time in seconds to wait for browser to start
WEBUI_EXPORT void webui_set_timeout(unsigned int second);
WEBUI_EXPORT void webui_set_timeout(size_t second);
// Set the default embedded HTML favicon
WEBUI_EXPORT void webui_set_icon(size_t window, const char* icon, const char* icon_type);
// Allow the window URL to be re-used in normal web browsers
@ -169,9 +176,9 @@ WEBUI_EXPORT void webui_set_multi_access(size_t window, bool status);
// Run JavaScript quickly with no waiting for the response.
WEBUI_EXPORT bool webui_run(size_t window, const char* script);
// Run a 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, unsigned int timeout, char* buffer, size_t buffer_length);
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 runtime for .js and .ts files.
WEBUI_EXPORT void webui_set_runtime(size_t window, unsigned int runtime);
WEBUI_EXPORT void webui_set_runtime(size_t window, size_t runtime);
// Parse argument as integer.
WEBUI_EXPORT long long int webui_get_int(webui_event_t* e);
// Parse argument as string.
@ -187,14 +194,14 @@ WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b);
// -- Interface -----------------------
// Bind a specific html element click event with a function. Empty element means all events. This replace webui_bind(). The func is (Window, EventType, Element, Data, EventNumber)
WEBUI_EXPORT unsigned int webui_interface_bind(size_t window, const char* element, void (*func)(size_t, unsigned int, char*, char*, unsigned int));
WEBUI_EXPORT size_t webui_interface_bind(size_t window, const char* element, void (*func)(size_t, size_t, char*, char*, size_t));
// When using `webui_interface_bind()` you may need this function to easily set your callback response.
WEBUI_EXPORT void webui_interface_set_response(size_t window, unsigned int event_number, const char* response);
WEBUI_EXPORT void webui_interface_set_response(size_t window, size_t event_number, const char* response);
// Check if the app still running or not. This replace webui_wait().
WEBUI_EXPORT bool webui_interface_is_app_running(void);
// Get window unique ID
WEBUI_EXPORT unsigned int webui_interface_get_window_id(size_t window);
WEBUI_EXPORT size_t webui_interface_get_window_id(size_t window);
// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
WEBUI_EXPORT unsigned int webui_interface_get_bind_id(size_t window, const char* element);
WEBUI_EXPORT size_t webui_interface_get_bind_id(size_t window, const char* element);
#endif /* _WEBUI_H */

View File

@ -51,16 +51,6 @@ namespace webui {
// List of window objects: webui::window
webui::window* window_list[512];
// Wait until all opened windows get closed.
void wait(void) {
webui_wait();
}
// Close all opened windows. wait() will break.
void exit(void) {
webui_exit();
}
// Event handler
// WebUI is written in C. So there is no way
// to make C call a C++ class member. That's
@ -151,11 +141,6 @@ namespace webui {
return webui_is_shown(this->webui_window);
}
// Set the maximum time in seconds to wait for browser to start
void set_timeout(unsigned int second) {
webui_set_timeout(second);
}
// Set the default embedded HTML favicon
void set_icon(std::string icon, std::string icon_type) {
webui_set_icon(this->webui_window, icon.c_str(), icon_type.c_str());
@ -227,6 +212,21 @@ namespace webui {
delete c_e;
}
};
// Wait until all opened windows get closed.
void wait(void) {
webui_wait();
}
// Close all opened windows. wait() will break.
void exit(void) {
webui_exit();
}
// Set the maximum time in seconds to wait for browser to start
void set_timeout(unsigned int second) {
webui_set_timeout(second);
}
}
#endif /* _WEBUI_HPP */

View File

@ -12,6 +12,7 @@
#define _WEBUI_H
#define WEBUI_VERSION "2.3.0"
#define WEBUI_MAX_IDS (512)
// Dynamic Library Exports
#if defined(_MSC_VER) || defined(__TINYC__)
@ -71,6 +72,7 @@
#include <fcntl.h>
#include <poll.h>
#include <sys/time.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -90,6 +92,7 @@
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -131,27 +134,31 @@ enum webui_events {
// -- Structs -------------------------
typedef struct webui_event_t {
size_t window; // The window object number
unsigned int event_type; // Event type
size_t event_type; // Event type
char* element; // HTML element ID
char* data; // JavaScript data
unsigned int event_number; // Internal WebUI
size_t event_number; // Internal WebUI
} webui_event_t;
// -- Definitions ---------------------
// Create a new webui window object.
WEBUI_EXPORT size_t webui_new_window(void);
// Create a new webui window object.
WEBUI_EXPORT void webui_new_window_id(size_t window_number);
// Bind a specific html element click event with a function. Empty element means all events.
WEBUI_EXPORT unsigned int webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
WEBUI_EXPORT size_t webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
// Show a window using a embedded HTML, or a file. If the window is already opened then it will be refreshed.
WEBUI_EXPORT bool webui_show(size_t window, const char* content);
// Same as webui_show(). But with a specific web browser.
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, unsigned int browser);
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, size_t browser);
// Set the window in Kiosk mode (Full screen)
WEBUI_EXPORT void webui_set_kiosk(size_t window, bool status);
// Wait until all opened windows get closed.
WEBUI_EXPORT void webui_wait(void);
// Close a specific window.
// Close a specific window only. The window object will still exist.
WEBUI_EXPORT void webui_close(size_t window);
// Close a specific window and free all memory resources.
WEBUI_EXPORT void webui_destroy(size_t window);
// Close all opened windows. webui_wait() will break.
WEBUI_EXPORT void webui_exit(void);
@ -159,7 +166,7 @@ WEBUI_EXPORT void webui_exit(void);
// Check a specific window if it's still running
WEBUI_EXPORT bool webui_is_shown(size_t window);
// Set the maximum time in seconds to wait for browser to start
WEBUI_EXPORT void webui_set_timeout(unsigned int second);
WEBUI_EXPORT void webui_set_timeout(size_t second);
// Set the default embedded HTML favicon
WEBUI_EXPORT void webui_set_icon(size_t window, const char* icon, const char* icon_type);
// Allow the window URL to be re-used in normal web browsers
@ -169,9 +176,9 @@ WEBUI_EXPORT void webui_set_multi_access(size_t window, bool status);
// Run JavaScript quickly with no waiting for the response.
WEBUI_EXPORT bool webui_run(size_t window, const char* script);
// Run a 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, unsigned int timeout, char* buffer, size_t buffer_length);
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 runtime for .js and .ts files.
WEBUI_EXPORT void webui_set_runtime(size_t window, unsigned int runtime);
WEBUI_EXPORT void webui_set_runtime(size_t window, size_t runtime);
// Parse argument as integer.
WEBUI_EXPORT long long int webui_get_int(webui_event_t* e);
// Parse argument as string.
@ -187,14 +194,14 @@ WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b);
// -- Interface -----------------------
// Bind a specific html element click event with a function. Empty element means all events. This replace webui_bind(). The func is (Window, EventType, Element, Data, EventNumber)
WEBUI_EXPORT unsigned int webui_interface_bind(size_t window, const char* element, void (*func)(size_t, unsigned int, char*, char*, unsigned int));
WEBUI_EXPORT size_t webui_interface_bind(size_t window, const char* element, void (*func)(size_t, size_t, char*, char*, size_t));
// When using `webui_interface_bind()` you may need this function to easily set your callback response.
WEBUI_EXPORT void webui_interface_set_response(size_t window, unsigned int event_number, const char* response);
WEBUI_EXPORT void webui_interface_set_response(size_t window, size_t event_number, const char* response);
// Check if the app still running or not. This replace webui_wait().
WEBUI_EXPORT bool webui_interface_is_app_running(void);
// Get window unique ID
WEBUI_EXPORT unsigned int webui_interface_get_window_id(size_t window);
WEBUI_EXPORT size_t webui_interface_get_window_id(size_t window);
// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
WEBUI_EXPORT unsigned int webui_interface_get_bind_id(size_t window, const char* element);
WEBUI_EXPORT size_t webui_interface_get_bind_id(size_t window, const char* element);
#endif /* _WEBUI_H */

View File

@ -51,16 +51,6 @@ namespace webui {
// List of window objects: webui::window
webui::window* window_list[512];
// Wait until all opened windows get closed.
void wait(void) {
webui_wait();
}
// Close all opened windows. wait() will break.
void exit(void) {
webui_exit();
}
// Event handler
// WebUI is written in C. So there is no way
// to make C call a C++ class member. That's
@ -151,11 +141,6 @@ namespace webui {
return webui_is_shown(this->webui_window);
}
// Set the maximum time in seconds to wait for browser to start
void set_timeout(unsigned int second) {
webui_set_timeout(second);
}
// Set the default embedded HTML favicon
void set_icon(std::string icon, std::string icon_type) {
webui_set_icon(this->webui_window, icon.c_str(), icon_type.c_str());
@ -227,6 +212,21 @@ namespace webui {
delete c_e;
}
};
// Wait until all opened windows get closed.
void wait(void) {
webui_wait();
}
// Close all opened windows. wait() will break.
void exit(void) {
webui_exit();
}
// Set the maximum time in seconds to wait for browser to start
void set_timeout(unsigned int second) {
webui_set_timeout(second);
}
}
#endif /* _WEBUI_HPP */

View File

@ -2,8 +2,11 @@
#include "webui.h"
size_t my_window;
size_t my_second_window;
// Those defines is to avoid using global variables
// you can also use `const size_t MyWindow;`.
// A window ID can be between 1 and 512 (WEBUI_MAX_IDS)
#define MyWindow (1)
#define MySecondWindow (2)
void exit_app(webui_event_t* e) {
@ -42,29 +45,29 @@ void show_second_window(webui_event_t* e) {
// Show a new window, and navigate to `/second.html`
// if it's already open, then switch in the same window
webui_show(my_second_window, "second.html");
webui_show(MySecondWindow, "second.html");
}
int main() {
// Create new windows
my_window = webui_new_window();
my_second_window = webui_new_window();
webui_new_window_id(MyWindow);
webui_new_window_id(MySecondWindow);
// Bind HTML element IDs with a C functions
webui_bind(my_window, "SwitchToSecondPage", switch_to_second_page);
webui_bind(my_window, "OpenNewWindow", show_second_window);
webui_bind(my_window, "Exit", exit_app);
webui_bind(my_second_window, "Exit", exit_app);
webui_bind(MyWindow, "SwitchToSecondPage", switch_to_second_page);
webui_bind(MyWindow, "OpenNewWindow", show_second_window);
webui_bind(MyWindow, "Exit", exit_app);
webui_bind(MySecondWindow, "Exit", exit_app);
// Bind events
webui_bind(my_window, "", events);
webui_bind(MyWindow, "", events);
// Make Deno as the `.ts` and `.js` interpreter
webui_set_runtime(my_window, Deno);
webui_set_runtime(MyWindow, Deno);
// Show a new window
webui_show(my_window, "index.html"); // webui_show_browser(my_window, "index.html", Chrome);
webui_show(MyWindow, "index.html"); // webui_show_browser(MyWindow, "index.html", Chrome);
// Wait until all windows get closed
webui_wait();

View File

@ -12,6 +12,7 @@
#define _WEBUI_H
#define WEBUI_VERSION "2.3.0"
#define WEBUI_MAX_IDS (512)
// Dynamic Library Exports
#if defined(_MSC_VER) || defined(__TINYC__)
@ -71,6 +72,7 @@
#include <fcntl.h>
#include <poll.h>
#include <sys/time.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -90,6 +92,7 @@
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -131,27 +134,31 @@ enum webui_events {
// -- Structs -------------------------
typedef struct webui_event_t {
size_t window; // The window object number
unsigned int event_type; // Event type
size_t event_type; // Event type
char* element; // HTML element ID
char* data; // JavaScript data
unsigned int event_number; // Internal WebUI
size_t event_number; // Internal WebUI
} webui_event_t;
// -- Definitions ---------------------
// Create a new webui window object.
WEBUI_EXPORT size_t webui_new_window(void);
// Create a new webui window object.
WEBUI_EXPORT void webui_new_window_id(size_t window_number);
// Bind a specific html element click event with a function. Empty element means all events.
WEBUI_EXPORT unsigned int webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
WEBUI_EXPORT size_t webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
// Show a window using a embedded HTML, or a file. If the window is already opened then it will be refreshed.
WEBUI_EXPORT bool webui_show(size_t window, const char* content);
// Same as webui_show(). But with a specific web browser.
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, unsigned int browser);
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, size_t browser);
// Set the window in Kiosk mode (Full screen)
WEBUI_EXPORT void webui_set_kiosk(size_t window, bool status);
// Wait until all opened windows get closed.
WEBUI_EXPORT void webui_wait(void);
// Close a specific window.
// Close a specific window only. The window object will still exist.
WEBUI_EXPORT void webui_close(size_t window);
// Close a specific window and free all memory resources.
WEBUI_EXPORT void webui_destroy(size_t window);
// Close all opened windows. webui_wait() will break.
WEBUI_EXPORT void webui_exit(void);
@ -159,7 +166,7 @@ WEBUI_EXPORT void webui_exit(void);
// Check a specific window if it's still running
WEBUI_EXPORT bool webui_is_shown(size_t window);
// Set the maximum time in seconds to wait for browser to start
WEBUI_EXPORT void webui_set_timeout(unsigned int second);
WEBUI_EXPORT void webui_set_timeout(size_t second);
// Set the default embedded HTML favicon
WEBUI_EXPORT void webui_set_icon(size_t window, const char* icon, const char* icon_type);
// Allow the window URL to be re-used in normal web browsers
@ -169,9 +176,9 @@ WEBUI_EXPORT void webui_set_multi_access(size_t window, bool status);
// Run JavaScript quickly with no waiting for the response.
WEBUI_EXPORT bool webui_run(size_t window, const char* script);
// Run a 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, unsigned int timeout, char* buffer, size_t buffer_length);
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 runtime for .js and .ts files.
WEBUI_EXPORT void webui_set_runtime(size_t window, unsigned int runtime);
WEBUI_EXPORT void webui_set_runtime(size_t window, size_t runtime);
// Parse argument as integer.
WEBUI_EXPORT long long int webui_get_int(webui_event_t* e);
// Parse argument as string.
@ -187,14 +194,14 @@ WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b);
// -- Interface -----------------------
// Bind a specific html element click event with a function. Empty element means all events. This replace webui_bind(). The func is (Window, EventType, Element, Data, EventNumber)
WEBUI_EXPORT unsigned int webui_interface_bind(size_t window, const char* element, void (*func)(size_t, unsigned int, char*, char*, unsigned int));
WEBUI_EXPORT size_t webui_interface_bind(size_t window, const char* element, void (*func)(size_t, size_t, char*, char*, size_t));
// When using `webui_interface_bind()` you may need this function to easily set your callback response.
WEBUI_EXPORT void webui_interface_set_response(size_t window, unsigned int event_number, const char* response);
WEBUI_EXPORT void webui_interface_set_response(size_t window, size_t event_number, const char* response);
// Check if the app still running or not. This replace webui_wait().
WEBUI_EXPORT bool webui_interface_is_app_running(void);
// Get window unique ID
WEBUI_EXPORT unsigned int webui_interface_get_window_id(size_t window);
WEBUI_EXPORT size_t webui_interface_get_window_id(size_t window);
// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
WEBUI_EXPORT unsigned int webui_interface_get_bind_id(size_t window, const char* element);
WEBUI_EXPORT size_t webui_interface_get_bind_id(size_t window, const char* element);
#endif /* _WEBUI_H */

View File

@ -12,6 +12,7 @@
#define _WEBUI_H
#define WEBUI_VERSION "2.3.0"
#define WEBUI_MAX_IDS (512)
// Dynamic Library Exports
#if defined(_MSC_VER) || defined(__TINYC__)
@ -71,6 +72,7 @@
#include <fcntl.h>
#include <poll.h>
#include <sys/time.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -90,6 +92,7 @@
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <signal.h>
#define WEBUI_GET_CURRENT_DIR getcwd
#define WEBUI_FILE_EXIST access
#define WEBUI_POPEN popen
@ -131,27 +134,31 @@ enum webui_events {
// -- Structs -------------------------
typedef struct webui_event_t {
size_t window; // The window object number
unsigned int event_type; // Event type
size_t event_type; // Event type
char* element; // HTML element ID
char* data; // JavaScript data
unsigned int event_number; // Internal WebUI
size_t event_number; // Internal WebUI
} webui_event_t;
// -- Definitions ---------------------
// Create a new webui window object.
WEBUI_EXPORT size_t webui_new_window(void);
// Create a new webui window object.
WEBUI_EXPORT void webui_new_window_id(size_t window_number);
// Bind a specific html element click event with a function. Empty element means all events.
WEBUI_EXPORT unsigned int webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
WEBUI_EXPORT size_t webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
// Show a window using a embedded HTML, or a file. If the window is already opened then it will be refreshed.
WEBUI_EXPORT bool webui_show(size_t window, const char* content);
// Same as webui_show(). But with a specific web browser.
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, unsigned int browser);
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, size_t browser);
// Set the window in Kiosk mode (Full screen)
WEBUI_EXPORT void webui_set_kiosk(size_t window, bool status);
// Wait until all opened windows get closed.
WEBUI_EXPORT void webui_wait(void);
// Close a specific window.
// Close a specific window only. The window object will still exist.
WEBUI_EXPORT void webui_close(size_t window);
// Close a specific window and free all memory resources.
WEBUI_EXPORT void webui_destroy(size_t window);
// Close all opened windows. webui_wait() will break.
WEBUI_EXPORT void webui_exit(void);
@ -159,7 +166,7 @@ WEBUI_EXPORT void webui_exit(void);
// Check a specific window if it's still running
WEBUI_EXPORT bool webui_is_shown(size_t window);
// Set the maximum time in seconds to wait for browser to start
WEBUI_EXPORT void webui_set_timeout(unsigned int second);
WEBUI_EXPORT void webui_set_timeout(size_t second);
// Set the default embedded HTML favicon
WEBUI_EXPORT void webui_set_icon(size_t window, const char* icon, const char* icon_type);
// Allow the window URL to be re-used in normal web browsers
@ -169,9 +176,9 @@ WEBUI_EXPORT void webui_set_multi_access(size_t window, bool status);
// Run JavaScript quickly with no waiting for the response.
WEBUI_EXPORT bool webui_run(size_t window, const char* script);
// Run a 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, unsigned int timeout, char* buffer, size_t buffer_length);
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 runtime for .js and .ts files.
WEBUI_EXPORT void webui_set_runtime(size_t window, unsigned int runtime);
WEBUI_EXPORT void webui_set_runtime(size_t window, size_t runtime);
// Parse argument as integer.
WEBUI_EXPORT long long int webui_get_int(webui_event_t* e);
// Parse argument as string.
@ -187,14 +194,14 @@ WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b);
// -- Interface -----------------------
// Bind a specific html element click event with a function. Empty element means all events. This replace webui_bind(). The func is (Window, EventType, Element, Data, EventNumber)
WEBUI_EXPORT unsigned int webui_interface_bind(size_t window, const char* element, void (*func)(size_t, unsigned int, char*, char*, unsigned int));
WEBUI_EXPORT size_t webui_interface_bind(size_t window, const char* element, void (*func)(size_t, size_t, char*, char*, size_t));
// When using `webui_interface_bind()` you may need this function to easily set your callback response.
WEBUI_EXPORT void webui_interface_set_response(size_t window, unsigned int event_number, const char* response);
WEBUI_EXPORT void webui_interface_set_response(size_t window, size_t event_number, const char* response);
// Check if the app still running or not. This replace webui_wait().
WEBUI_EXPORT bool webui_interface_is_app_running(void);
// Get window unique ID
WEBUI_EXPORT unsigned int webui_interface_get_window_id(size_t window);
WEBUI_EXPORT size_t webui_interface_get_window_id(size_t window);
// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
WEBUI_EXPORT unsigned int webui_interface_get_bind_id(size_t window, const char* element);
WEBUI_EXPORT size_t webui_interface_get_bind_id(size_t window, const char* element);
#endif /* _WEBUI_H */

File diff suppressed because it is too large Load Diff

View File

@ -39,27 +39,28 @@ typedef struct webui_event_core_t {
} webui_event_core_t;
typedef struct _webui_window_t {
unsigned int window_number;
size_t window_number;
bool server_running;
volatile bool connected;
bool html_handled;
bool server_handled;
bool multi_access;
bool is_embedded_html;
unsigned int server_port;
unsigned int ws_port;
size_t server_port;
size_t ws_port;
char* url;
const char* html;
const char* icon;
const char* icon_type;
unsigned int current_browser;
size_t current_browser;
char* browser_path;
char* profile_path;
unsigned int connections;
unsigned int runtime;
size_t connections;
size_t runtime;
bool has_events;
char* server_root_path;
bool kiosk_mode;
size_t process_id;
webui_event_core_t* event_core[WEBUI_MAX_ARRAY];
#ifdef _WIN32
HANDLE server_thread;
@ -69,12 +70,11 @@ typedef struct _webui_window_t {
} _webui_window_t;
typedef struct _webui_core_t {
volatile unsigned int servers;
volatile unsigned int connections;
volatile size_t servers;
volatile size_t connections;
char* html_elements[WEBUI_MAX_ARRAY];
unsigned int used_ports[WEBUI_MAX_ARRAY];
unsigned int last_window;
unsigned int startup_timeout;
size_t used_ports[WEBUI_MAX_ARRAY];
size_t startup_timeout;
volatile bool exit_now;
const char* run_responses[WEBUI_MAX_ARRAY];
volatile bool run_done[WEBUI_MAX_ARRAY];
@ -82,12 +82,12 @@ typedef struct _webui_core_t {
unsigned char run_last_id;
bool initialized;
void (*cb[WEBUI_MAX_ARRAY])(webui_event_t* e);
void (*cb_interface[WEBUI_MAX_ARRAY])(size_t, unsigned int, char*, char*, unsigned int);
void (*cb_interface[WEBUI_MAX_ARRAY])(size_t, size_t, char*, char*, size_t);
char* executable_path;
void *ptr_list[WEBUI_MAX_ARRAY];
unsigned int ptr_position;
size_t ptr_position;
size_t ptr_size[WEBUI_MAX_ARRAY];
unsigned int current_browser;
size_t current_browser;
struct mg_connection* mg_connections[WEBUI_MAX_ARRAY];
_webui_window_t* wins[WEBUI_MAX_ARRAY];
bool server_handled;
@ -96,10 +96,10 @@ typedef struct _webui_core_t {
typedef struct _webui_cb_arg_t {
// Event
_webui_window_t* window;
unsigned int event_type;
size_t event_type;
char* element;
char* data;
unsigned int event_number;
size_t event_number;
// Extras
char* webui_internal_id;
} _webui_cb_arg_t;
@ -114,9 +114,9 @@ typedef struct _webui_cmd_async_t {
static const char* webui_sep = "\\";
static DWORD WINAPI _webui_cb(LPVOID _arg);
static DWORD WINAPI _webui_run_browser_task(LPVOID _arg);
static int _webui_system_win32(char* cmd, bool show);
static int _webui_system_win32(_webui_window_t* win, char* cmd, bool show);
static int _webui_system_win32_out(const char *cmd, char **output, bool show);
static bool _webui_socket_test_listen_win32(unsigned int port_num);
static bool _webui_socket_test_listen_win32(size_t port_num);
static bool _webui_get_windows_reg_value(HKEY key, LPCWSTR reg, LPCWSTR value_name, char value[WEBUI_MAX_PATH]);
#define WEBUI_CB DWORD WINAPI _webui_cb(LPVOID _arg)
@ -133,27 +133,25 @@ typedef struct _webui_cmd_async_t {
#endif
static void _webui_init(void);
static bool _webui_show(_webui_window_t* window, const char* content, unsigned int browser);
static unsigned int _webui_get_cb_index(char* webui_internal_id);
static unsigned int _webui_set_cb_index(char* webui_internal_id);
static unsigned int _webui_get_free_port(void);
static unsigned int _webui_get_new_window_number(void);
static bool _webui_show(_webui_window_t* win, const char* content, size_t browser);
static size_t _webui_get_cb_index(char* webui_internal_id);
static size_t _webui_set_cb_index(char* webui_internal_id);
static size_t _webui_get_free_port(void);
static size_t _webui_get_new_window_number(void);
static void _webui_wait_for_startup(void);
static void _webui_free_port(unsigned int port);
static void _webui_free_port(size_t port);
static char* _webui_get_current_path(void);
static void _webui_window_receive(_webui_window_t* win, const char* packet, size_t len);
static void _webui_window_send(_webui_window_t* win, char* packet, size_t packets_size);
static void _webui_window_event(_webui_window_t* win, int event_type, char* element, char* data, unsigned int event_number, char* webui_internal_id);
static unsigned int _webui_window_get_number(_webui_window_t* win);
static void _webui_window_open(_webui_window_t* win, char* link, unsigned int browser);
static int _webui_cmd_sync(char* cmd, bool show);
static int _webui_cmd_async(char* cmd, bool show);
static void _webui_window_event(_webui_window_t* win, int event_type, char* element, char* data, size_t event_number, char* webui_internal_id);
static int _webui_cmd_sync(_webui_window_t* win, char* cmd, bool show);
static int _webui_cmd_async(_webui_window_t* win, char* cmd, bool show);
static int _webui_run_browser(_webui_window_t* win, char* cmd);
static void _webui_clean(void);
static bool _webui_browser_exist(_webui_window_t* win, unsigned int browser);
static const char* _webui_browser_get_temp_path(unsigned int browser);
static bool _webui_browser_exist(_webui_window_t* win, size_t browser);
static const char* _webui_browser_get_temp_path(size_t browser);
static bool _webui_folder_exist(char* folder);
static bool _webui_browser_create_profile_folder(_webui_window_t* win, unsigned int browser);
static bool _webui_browser_create_profile_folder(_webui_window_t* win, size_t browser);
static bool _webui_browser_start_chrome(_webui_window_t* win, const char* address);
static bool _webui_browser_start_edge(_webui_window_t* win, const char* address);
static bool _webui_browser_start_epic(_webui_window_t* win, const char* address);
@ -162,10 +160,10 @@ static bool _webui_browser_start_brave(_webui_window_t* win, const char* address
static bool _webui_browser_start_firefox(_webui_window_t* win, const char* address);
static bool _webui_browser_start_yandex(_webui_window_t* win, const char* address);
static bool _webui_browser_start_chromium(_webui_window_t* win, const char* address);
static bool _webui_browser_start(_webui_window_t* win, const char* address, unsigned int browser);
static bool _webui_browser_start(_webui_window_t* win, const char* address, size_t browser);
static long _webui_timer_diff(struct timespec *start, struct timespec *end);
static void _webui_timer_start(_webui_timer_t* t);
static bool _webui_timer_is_end(_webui_timer_t* t, unsigned int ms);
static bool _webui_timer_is_end(_webui_timer_t* t, size_t ms);
static void _webui_timer_clock_gettime(struct timespec *spec);
static bool _webui_set_root_folder(_webui_window_t* win, const char* path);
static const char* _webui_generate_js_bridge(_webui_window_t* win);
@ -174,20 +172,29 @@ static void _webui_free_mem(void* ptr);
static bool _webui_file_exist_mg(struct mg_connection *conn);
static bool _webui_file_exist(char* file);
static void _webui_free_all_mem(void);
static bool _webui_show_window(_webui_window_t* win, const char* content, bool is_embedded_html, unsigned int browser);
static bool _webui_show_window(_webui_window_t* win, const char* content, bool is_embedded_html, size_t browser);
static char* _webui_generate_internal_id(_webui_window_t* win, const char* element);
static bool _webui_is_empty(const char* s);
static size_t _webui_strlen(const char* s);
static unsigned char _webui_get_run_id(void);
static void* _webui_malloc(int size);
static void* _webui_malloc(size_t size);
static void _webui_sleep(long unsigned int ms);
static unsigned int _webui_find_the_best_browser(_webui_window_t* win);
static size_t _webui_find_the_best_browser(_webui_window_t* win);
static bool _webui_is_process_running(const char* process_name);
static unsigned int _webui_get_free_event_core_pos(_webui_window_t* win);
static void _webui_http_send(struct mg_connection *conn, const char *mime_type, const char* body);
static size_t _webui_get_free_event_core_pos(_webui_window_t* win);
static void _webui_print_hex(const char* data, size_t len);
static void _webui_print_ascii(const char* data, size_t len);
static void _webui_panic(void);
static void _webui_kill_pid(size_t pid);
static _webui_window_t* _webui_dereference_win_ptr(void* ptr);
static void _webui_http_send(struct mg_connection *conn, const char* mime_type, const char* body);
static int _webui_http_log(const struct mg_connection *conn, const char* message);
static int _webui_http_handler(struct mg_connection *conn, void *_win);
static int _webui_ws_connect_handler(const struct mg_connection *conn, void *_win);
static void _webui_ws_ready_handler(struct mg_connection *conn, void *_win);
static int _webui_ws_data_handler(struct mg_connection *conn, int opcode, char* data, size_t datasize, void *_win);
static void _webui_ws_close_handler(const struct mg_connection *conn, void *_win);
static WEBUI_SERVER_START;
static WEBUI_CB;