2022-09-25 02:35:56 +03:00
/*
2023-05-05 04:41:03 +03:00
WebUI Library 2.3 .0
2023-04-17 22:43:46 +03:00
http : //webui.me
https : //github.com/alifcommunity/webui
Copyright ( c ) 2020 - 2023 Hassan Draga .
2023-05-05 04:41:03 +03:00
Licensed under MIT License .
2023-04-17 22:43:46 +03:00
All rights reserved .
Canada .
2022-09-25 02:35:56 +03:00
*/
# ifndef _WEBUI_H
# define _WEBUI_H
2023-05-05 04:41:03 +03:00
# define WEBUI_VERSION "2.3.0"
2023-05-07 20:54:15 +03:00
# define WEBUI_MAX_IDS (512)
2023-04-17 22:43:46 +03:00
// Dynamic Library Exports
2022-09-25 02:35:56 +03:00
# if defined(_MSC_VER) || defined(__TINYC__)
2023-04-17 22:43:46 +03:00
# ifndef WEBUI_EXPORT
# define WEBUI_EXPORT __declspec(dllexport)
# endif
2022-09-25 02:35:56 +03:00
# else
2023-04-17 22:43:46 +03:00
# ifndef WEBUI_EXPORT
# define WEBUI_EXPORT extern
# endif
2022-09-25 02:35:56 +03:00
# endif
2022-10-20 02:48:47 +03:00
// -- C STD ---------------------------
2022-09-25 02:35:56 +03:00
# include <stdbool.h>
# include <inttypes.h>
# include <assert.h>
# include <stdint.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <ctype.h>
# include <stddef.h>
# include <time.h>
# include <errno.h>
2022-12-12 18:13:27 +03:00
# include <math.h>
2022-09-25 02:35:56 +03:00
# if defined(__GNUC__) || defined(__TINYC__)
# include <dirent.h>
# endif
2022-10-20 02:48:47 +03:00
// -- Windows -------------------------
2022-09-25 02:35:56 +03:00
# ifdef _WIN32
2022-11-08 07:33:03 +03:00
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
2022-09-25 02:35:56 +03:00
# include <windows.h>
2022-11-08 07:33:03 +03:00
# include <winsock2.h>
2022-09-25 02:35:56 +03:00
# include <ws2tcpip.h>
# include <direct.h>
# include <io.h>
# include <tchar.h>
2023-05-05 04:41:03 +03:00
# include <tlhelp32.h>
2022-09-25 02:35:56 +03:00
# define WEBUI_GET_CURRENT_DIR _getcwd
# define WEBUI_FILE_EXIST _access
2022-09-29 02:41:24 +03:00
# define WEBUI_POPEN _popen
# define WEBUI_PCLOSE _pclose
2022-10-24 03:06:15 +03:00
# define WEBUI_MAX_PATH MAX_PATH
2022-09-25 02:35:56 +03:00
# endif
2023-04-17 22:43:46 +03:00
2022-10-20 02:48:47 +03:00
// -- Linux ---------------------------
2022-09-25 02:35:56 +03:00
# ifdef __linux__
2023-04-25 00:44:27 +03:00
# include <unistd.h>
2023-04-17 22:43:46 +03:00
# include <pthread.h>
2022-09-25 02:35:56 +03:00
# include <unistd.h>
2022-10-24 03:06:15 +03:00
# include <limits.h>
# include <dirent.h>
# include <sys/socket.h>
# include <fcntl.h>
# include <poll.h>
2023-04-17 22:43:46 +03:00
# include <sys/time.h>
2023-05-07 20:54:15 +03:00
# include <signal.h>
2022-09-25 02:35:56 +03:00
# define WEBUI_GET_CURRENT_DIR getcwd
2022-10-24 03:06:15 +03:00
# define WEBUI_FILE_EXIST access
2022-09-29 02:41:24 +03:00
# define WEBUI_POPEN popen
# define WEBUI_PCLOSE pclose
2022-10-24 03:06:15 +03:00
# define WEBUI_MAX_PATH PATH_MAX
2022-09-25 02:35:56 +03:00
# endif
2023-04-17 22:43:46 +03:00
2023-03-28 23:50:10 +03:00
// -- Apple ---------------------------
# ifdef __APPLE__
2023-04-17 22:43:46 +03:00
# include <pthread.h>
2023-03-28 23:50:10 +03:00
# include <unistd.h>
# include <limits.h>
# include <dirent.h>
# include <sys/socket.h>
# include <fcntl.h>
# include <poll.h>
2023-04-26 00:56:57 +03:00
# include <sys/syslimits.h>
2023-04-17 22:43:46 +03:00
# include <sys/time.h>
2023-04-25 00:44:27 +03:00
# include <sys/sysctl.h>
2023-05-07 20:54:15 +03:00
# include <signal.h>
2023-03-28 23:50:10 +03:00
# 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
2022-10-19 07:38:54 +03:00
2023-04-17 22:43:46 +03:00
// -- Enums ---------------------------
enum webui_browsers {
AnyBrowser = 0 , // 0. Default recommended web browser
Chrome , // 1. Google Chrome
Firefox , // 2. Mozilla Firefox
Edge , // 3. Microsoft Edge
Safari , // 4. Apple Safari
Chromium , // 5. The Chromium Project
Opera , // 6. Opera Browser
Brave , // 7. The Brave Browser
Vivaldi , // 8. The Vivaldi Browser
Epic , // 9. The Epic Browser
Yandex , // 10. The Yandex 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
} ;
enum webui_events {
WEBUI_EVENT_DISCONNECTED = 0 , // 0. Window disconnection event
WEBUI_EVENT_CONNECTED , // 1. Window connection event
WEBUI_EVENT_MULTI_CONNECTION , // 2. New window connection event
WEBUI_EVENT_UNWANTED_CONNECTION , // 3. New unwanted window connection event
WEBUI_EVENT_MOUSE_CLICK , // 4. Mouse click event
WEBUI_EVENT_NAVIGATION , // 5. Window navigation event
WEBUI_EVENT_CALLBACK , // 6. Function call event
} ;
2022-10-30 04:41:20 +03:00
// -- Structs -------------------------
typedef struct webui_event_t {
2023-05-06 00:41:20 +03:00
size_t window ; // The window object number
2023-05-07 20:54:15 +03:00
size_t event_type ; // Event type
2023-04-29 20:46:11 +03:00
char * element ; // HTML element ID
2023-04-17 22:43:46 +03:00
char * data ; // JavaScript data
2023-05-07 20:54:15 +03:00
size_t event_number ; // Internal WebUI
2022-10-30 04:41:20 +03:00
} webui_event_t ;
2022-09-25 02:35:56 +03:00
2022-10-20 02:48:47 +03:00
// -- Definitions ---------------------
2023-04-17 22:43:46 +03:00
// Create a new webui window object.
2023-05-05 04:41:03 +03:00
WEBUI_EXPORT size_t webui_new_window ( void ) ;
2023-05-07 20:54:15 +03:00
// Create a new webui window object.
WEBUI_EXPORT void webui_new_window_id ( size_t window_number ) ;
2023-04-17 22:43:46 +03:00
// Bind a specific html element click event with a function. Empty element means all events.
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT size_t webui_bind ( size_t window , const char * element , void ( * func ) ( webui_event_t * e ) ) ;
2023-04-17 22:43:46 +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-05 04:41:03 +03:00
WEBUI_EXPORT bool webui_show ( size_t window , const char * content ) ;
2023-04-17 22:43:46 +03:00
// Same as webui_show(). But with a specific web browser.
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT bool webui_show_browser ( size_t window , const char * content , size_t browser ) ;
2023-05-04 03:18:56 +03:00
// Set the window in Kiosk mode (Full screen)
2023-05-05 04:41:03 +03:00
WEBUI_EXPORT void webui_set_kiosk ( size_t window , bool status ) ;
2023-04-17 22:43:46 +03:00
// Wait until all opened windows get closed.
WEBUI_EXPORT void webui_wait ( void ) ;
2023-05-07 20:54:15 +03:00
// Close a specific window only. The window object will still exist.
2023-05-05 04:41:03 +03:00
WEBUI_EXPORT void webui_close ( size_t window ) ;
2023-05-07 20:54:15 +03:00
// Close a specific window and free all memory resources.
WEBUI_EXPORT void webui_destroy ( size_t window ) ;
2023-04-17 22:43:46 +03:00
// Close all opened windows. webui_wait() will break.
WEBUI_EXPORT void webui_exit ( void ) ;
WebUI v2.1.0
#New Features
* Supporting more web browsers
* Search for web browsers in Windows Reg.
* Using the same show() function for HTML script, files, and refresh content
* Support Chrome on macOS
#Breaking code
* Switching from `webui_show(win, html, browser);` to `webui_show(win, content);`, and it's used for HTML, files, and reload the window
* Removing `webui_show_cpy()`, `webui_refresh()`, `webui_refresh_cpy()`
2023-03-29 06:00:09 +03:00
2023-04-17 22:43:46 +03:00
// -- Other ---------------------------
2023-04-26 00:56:57 +03:00
// Check a specific window if it's still running
2023-05-05 04:41:03 +03:00
WEBUI_EXPORT bool webui_is_shown ( size_t window ) ;
2023-04-26 00:56:57 +03:00
// Set the maximum time in seconds to wait for browser to start
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT void webui_set_timeout ( size_t second ) ;
2023-04-26 00:56:57 +03:00
// Set the default embedded HTML favicon
2023-05-05 04:41:03 +03:00
WEBUI_EXPORT void webui_set_icon ( size_t window , const char * icon , const char * icon_type ) ;
2023-04-26 00:56:57 +03:00
// Allow the window URL to be re-used in normal web browsers
2023-05-05 04:41:03 +03:00
WEBUI_EXPORT void webui_set_multi_access ( size_t window , bool status ) ;
WebUI v2.1.0
#New Features
* Supporting more web browsers
* Search for web browsers in Windows Reg.
* Using the same show() function for HTML script, files, and refresh content
* Support Chrome on macOS
#Breaking code
* Switching from `webui_show(win, html, browser);` to `webui_show(win, content);`, and it's used for HTML, files, and reload the window
* Removing `webui_show_cpy()`, `webui_refresh()`, `webui_refresh_cpy()`
2023-03-29 06:00:09 +03:00
2023-04-17 22:43:46 +03:00
// -- JavaScript ----------------------
2023-04-26 00:56:57 +03:00
// Run JavaScript quickly with no waiting for the response.
2023-05-09 06:03:41 +03:00
WEBUI_EXPORT void webui_run ( size_t window , const char * script ) ;
2023-04-17 22:43:46 +03:00
// Run a JavaScript, and get the response back (Make sure your local buffer can hold the response).
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT bool webui_script ( size_t window , const char * script , size_t timeout , char * buffer , size_t buffer_length ) ;
2023-04-17 22:43:46 +03:00
// Chose between Deno and Nodejs runtime for .js and .ts files.
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT void webui_set_runtime ( size_t window , size_t runtime ) ;
2023-04-17 22:43:46 +03:00
// Parse argument as integer.
WEBUI_EXPORT long long int webui_get_int ( webui_event_t * e ) ;
// Parse argument as string.
WEBUI_EXPORT const char * webui_get_string ( webui_event_t * e ) ;
// Parse argument as boolean.
WEBUI_EXPORT bool webui_get_bool ( webui_event_t * e ) ;
// Return the response to JavaScript as integer.
WEBUI_EXPORT void webui_return_int ( webui_event_t * e , long long int n ) ;
// Return the response to JavaScript as string.
WEBUI_EXPORT void webui_return_string ( webui_event_t * e , char * s ) ;
// Return the response to JavaScript as boolean.
WEBUI_EXPORT void webui_return_bool ( webui_event_t * e , bool b ) ;
2023-05-09 06:03:41 +03:00
// Base64 encoding. Use this to safely send text based data to the UI. If it fails it will return NULL.
WEBUI_EXPORT char * webui_encode ( const char * str ) ;
// Base64 decoding. Use this to safely decode received Base64 text from the UI. If it fails it will return NULL.
WEBUI_EXPORT char * webui_decode ( const char * str ) ;
// Safely free a buffer allocated by WebUI, for example when using webui_encode().
WEBUI_EXPORT void webui_free ( void * ptr ) ;
2022-09-25 02:35:56 +03:00
2022-10-20 02:48:47 +03:00
// -- Interface -----------------------
2023-04-29 20:46:11 +03:00
// 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)
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT size_t webui_interface_bind ( size_t window , const char * element , void ( * func ) ( size_t , size_t , char * , char * , size_t ) ) ;
2023-04-29 20:46:11 +03:00
// When using `webui_interface_bind()` you may need this function to easily set your callback response.
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT void webui_interface_set_response ( size_t window , size_t event_number , const char * response ) ;
2023-04-17 22:43:46 +03:00
// 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
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT size_t webui_interface_get_window_id ( size_t window ) ;
2023-05-06 00:41:20 +03:00
// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
2023-05-07 20:54:15 +03:00
WEBUI_EXPORT size_t webui_interface_get_bind_id ( size_t window , const char * element ) ;
2022-09-25 02:35:56 +03:00
# endif /* _WEBUI_H */