webui/include/webui.h

485 lines
14 KiB
C
Raw Normal View History

2022-09-25 02:35:56 +03:00
/*
2023-08-20 00:49:08 +03:00
WebUI Library 2.4.0
http://webui.me
https://github.com/webui-dev/webui
Copyright (c) 2020-2023 Hassan Draga.
Licensed under MIT License.
All rights reserved.
Canada.
2022-09-25 02:35:56 +03:00
*/
#ifndef _WEBUI_H
#define _WEBUI_H
2023-08-20 00:49:08 +03:00
#define WEBUI_VERSION "2.4.0"
2023-09-12 01:09:42 +03:00
// Max windows, servers and threads
#define WEBUI_MAX_IDS (512)
// Dynamic Library Exports
2022-09-25 02:35:56 +03:00
#if defined(_MSC_VER) || defined(__TINYC__)
#ifndef WEBUI_EXPORT
#define WEBUI_EXPORT __declspec(dllexport)
#endif
2022-09-25 02:35:56 +03:00
#else
#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>
#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
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
2022-09-25 02:35:56 +03:00
#include <windows.h>
#include <winsock2.h>
2022-09-25 02:35:56 +03:00
#include <ws2tcpip.h>
#include <direct.h>
#include <io.h>
#include <tchar.h>
#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
2022-10-20 02:48:47 +03:00
// -- Linux ---------------------------
2022-09-25 02:35:56 +03:00
#ifdef __linux__
#include <unistd.h>
#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>
#include <sys/time.h>
#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
// -- Apple ---------------------------
#ifdef __APPLE__
#include <pthread.h>
#include <unistd.h>
#include <limits.h>
#include <dirent.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <poll.h>
#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
#define WEBUI_PCLOSE pclose
#define WEBUI_MAX_PATH PATH_MAX
#endif
2022-10-19 07:38:54 +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
ChromiumBased, // 11. Any Chromium based 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
};
// -- Structs -------------------------
typedef struct webui_event_t {
size_t window; // The window object number
size_t event_type; // Event type
char* element; // HTML element ID
char* data; // JavaScript data
size_t size; // JavaScript data len
size_t event_number; // Internal WebUI
} webui_event_t;
2022-09-25 02:35:56 +03:00
2022-10-20 02:48:47 +03:00
// -- Definitions ---------------------
2023-09-12 01:09:42 +03:00
/**
2023-09-13 22:32:14 +03:00
* @brief Create a new WebUI window object.
2023-09-12 01:09:42 +03:00
*
* @return Returns the window number.
*
2023-09-13 22:32:14 +03:00
* @example size_t myWindow = webui_new_window();
2023-09-12 01:09:42 +03:00
*/
WEBUI_EXPORT size_t webui_new_window(void);
2023-09-12 01:09:42 +03:00
/**
2023-09-13 22:32:14 +03:00
* @brief Create a new webui window object using a specified window number.
2023-09-12 01:09:42 +03:00
*
2023-09-13 22:32:14 +03:00
* @param window_number The window number (should be > 0, and < WEBUI_MAX_IDS)
2023-09-12 01:09:42 +03:00
*
2023-09-13 22:32:14 +03:00
* @return Returns the window number.
*
* @example size_t myWindow = webui_new_window_id(123);
2023-09-12 01:09:42 +03:00
*/
WEBUI_EXPORT size_t webui_new_window_id(size_t window_number);
/**
* @brief Get a free window number that can be used with `webui_new_window_id()`.
*
2023-09-13 22:32:14 +03:00
* @return Returns the first available free window number. Starting from 1.
*
* @example size_t myWindowNumber = webui_get_new_window_id();
2023-09-12 01:09:42 +03:00
*/
WEBUI_EXPORT size_t webui_get_new_window_id(void);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Bind a specific html element click event with a function. Empty element means all events.
*
* @param window The window number
* @param element The HTML ID
* @param func The callback function
*
* @return Returns a unique bind ID.
*
* @example webui_bind(myWindow, "myID", myFunction);
*/
WEBUI_EXPORT size_t webui_bind(size_t window, const char* element, void (*func)(webui_event_t* e));
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Show a window using embedded HTML, or a file. If the window is already open, it will be refreshed.
*
* @param window The window number
* @param content The HTML, Or a local file
*
* @return Returns True if showing the window is successed.
*
* @example webui_show(myWindow, "<html>...</html>"); | webui_show(myWindow, "index.html");
*/
WEBUI_EXPORT bool webui_show(size_t window, const char* content);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Same as `webui_show()`. But using a specific web browser.
*
* @param window The window number
* @param content The HTML, Or a local file
* @param browser The web browser to be used
*
* @return Returns True if showing the window is successed.
*
* @example webui_show_browser(myWindow, "<html>...</html>", Chrome); | webui_show(myWindow, "index.html", Firefox);
*/
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, size_t browser);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set the window in Kiosk mode (Full screen)
*
* @param window The window number
* @param status True or False
*
* @example webui_set_kiosk(myWindow, true);
*/
WEBUI_EXPORT void webui_set_kiosk(size_t window, bool status);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Wait until all opened windows get closed.
*
* @example webui_wait();
*/
WEBUI_EXPORT void webui_wait(void);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Close a specific window only. The window object will still exist.
*
* @param window The window number
*
* @example webui_close(myWindow);
*/
WEBUI_EXPORT void webui_close(size_t window);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Close a specific window and free all memory resources.
*
* @param window The window number
*
* @example webui_destroy(myWindow);
*/
WEBUI_EXPORT void webui_destroy(size_t window);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Close all open windows. `webui_wait()` will return (Break).
*
* @example webui_exit();
*/
WEBUI_EXPORT void webui_exit(void);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set the web-server root folder path for a specific window.
*
* @param window The window number
* @param path The local folder full path
*
* @example webui_set_root_folder(myWindow, "/home/Foo/Bar/");
*/
WEBUI_EXPORT bool webui_set_root_folder(size_t window, const char* path);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set the web-server root folder path for all windows. Should be used before `webui_show()`.
*
* @param path The local folder full path
*
* @example webui_set_default_root_folder("/home/Foo/Bar/");
*/
WEBUI_EXPORT bool webui_set_default_root_folder(const char* path);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set a custom handler to serve files.
*
* @param window The window number
* @param handler The handler function: `void myHandler(const char* filename, int* length)`
*
* @return Returns a unique bind ID.
*
* @example webui_set_file_handler(myWindow, myHandlerFunction);
*/
WEBUI_EXPORT void webui_set_file_handler(size_t window, const void* (*handler)(const char* filename, int* length));
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Check if the specified window is still running.
*
* @param window The window number
*
* @example webui_is_shown(myWindow);
*/
WEBUI_EXPORT bool webui_is_shown(size_t window);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set the maximum time in seconds to wait for the browser to start.
*
* @param second The timeout in seconds
*
* @example webui_set_timeout(30);
*/
WEBUI_EXPORT void webui_set_timeout(size_t second);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set the default embedded HTML favicon.
*
* @param window The window number
* @param icon The icon as string: `<svg>...</svg>`
* @param icon_type The icon type: `image/svg+xml`
*
* @example webui_set_icon(myWindow, "<svg>...</svg>", "image/svg+xml");
*/
WEBUI_EXPORT void webui_set_icon(size_t window, const char* icon, const char* icon_type);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Allow the window URL to be re-used in normal web browsers.
*
* @param window The window number
* @param status The status: True or False
*
* @example webui_set_multi_access(myWindow, true);
*/
WEBUI_EXPORT void webui_set_multi_access(size_t window, bool status);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Base64 encoding. Use this to safely send text based data to the UI. If it fails it will return NULL.
*
* @param str The string to encode (Should be null terminated)
*
* @example webui_encode("Hello");
*/
2023-09-12 00:18:31 +03:00
WEBUI_EXPORT char* webui_encode(const char* str);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Base64 decoding. Use this to safely decode received Base64 text from the UI. If it fails it will return NULL.
*
* @param str The string to decode (Should be null terminated)
*
* @example webui_encode("SGVsbG8=");
*/
2023-09-12 00:18:31 +03:00
WEBUI_EXPORT char* webui_decode(const char* str);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Safely free a buffer allocated by WebUI using `webui_malloc()`.
*
* @param ptr The buffer to be freed
*
* @example webui_free(myBuffer);
*/
2023-09-12 00:18:31 +03:00
WEBUI_EXPORT void webui_free(void* ptr);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Safely allocate memory using the WebUI memory management system. It can be safely freed using `webui_free()` at any time.
*
* @param size The size of memory in bytes
*
* @example webui_malloc(1024);
*/
2023-09-12 00:18:31 +03:00
WEBUI_EXPORT void* webui_malloc(size_t size);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Safely send raw data to the UI.
*
* @param window The window number
* @param function The JavaScript function to receive raw data: `function myFunc(myData){}`
* @param raw The raw data buffer
* @param size The raw data size in bytes
*
* @example webui_send_raw(myWindow, "myJavascriptFunction", myBuffer, 64);
*/
2023-09-12 00:18:31 +03:00
WEBUI_EXPORT void webui_send_raw(size_t window, const char* function, const void* raw, size_t size);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set a window in hidden mode. Should be called before `webui_show()`.
*
* @param window The window number
* @param status The status: True or False
*
* @example webui_set_hide(myWindow, True);
*/
2023-09-12 00:18:31 +03:00
WEBUI_EXPORT void webui_set_hide(size_t window, bool status);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set the window size.
*
* @param window The window number
* @param width The window width
* @param height The window height
*
* @example webui_set_size(myWindow, 800, 600);
*/
2023-09-12 00:18:31 +03:00
WEBUI_EXPORT void webui_set_size(size_t window, unsigned int width, unsigned int height);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Set the window position.
*
* @param window The window number
* @param x The window X
* @param y The window Y
*
* @example webui_set_position(myWindow, 100, 100);
*/
2023-09-12 00:18:31 +03:00
WEBUI_EXPORT void webui_set_position(size_t window, unsigned int x, unsigned int y);
/**
* @brief Set the web browser profile to use. An empty `name` and `path` means the default user profile. Need to be called before `webui_show()`.
*
* @param window The window number
* @param name The web browser profile name
* @param path The web browser profile full path
*
* @example webui_set_profile(myWindow, "Bar", "/Home/Foo/Bar"); | webui_set_profile(myWindow, "", "");
*/
WEBUI_EXPORT void webui_set_profile(size_t window, const char* name, const char* path);
/**
2023-09-21 23:36:11 +03:00
* @brief Get the full current URL
*
* @param window The window number
*
2023-09-21 23:36:11 +03:00
* @return Returns the full URL string
*
2023-09-21 23:36:11 +03:00
* @example const char* url = webui_get_url(myWindow);
*/
2023-09-21 23:36:11 +03:00
WEBUI_EXPORT const char* webui_get_url(size_t window);
// -- JavaScript ----------------------
2023-09-12 01:09:42 +03:00
2023-08-25 08:31:50 +03:00
// Run JavaScript without waiting for the response.
WEBUI_EXPORT void webui_run(size_t window, const char* script);
2023-09-12 01:09:42 +03:00
2023-08-25 08:31:50 +03:00
// Run 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, size_t timeout, char* buffer, size_t buffer_length);
2023-09-12 01:09:42 +03:00
2023-08-25 08:31:50 +03:00
// Chose between Deno and Nodejs as runtime for .js and .ts files.
WEBUI_EXPORT void webui_set_runtime(size_t window, size_t runtime);
2023-09-12 01:09:42 +03:00
// Parse argument as integer.
WEBUI_EXPORT long long int webui_get_int(webui_event_t* e);
2023-09-12 01:09:42 +03:00
// Parse argument as string.
WEBUI_EXPORT const char* webui_get_string(webui_event_t* e);
2023-09-12 01:09:42 +03:00
// Parse argument as boolean.
WEBUI_EXPORT bool webui_get_bool(webui_event_t* e);
2023-09-12 01:09:42 +03:00
// Return the response to JavaScript as integer.
WEBUI_EXPORT void webui_return_int(webui_event_t* e, long long int n);
2023-09-12 01:09:42 +03:00
// Return the response to JavaScript as string.
WEBUI_EXPORT void webui_return_string(webui_event_t* e, const char* s);
2023-09-12 01:09:42 +03:00
// Return the response to JavaScript as boolean.
WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b);
2022-09-25 02:35:56 +03:00
// Get process id (The web browser may create another process for the window)
WEBUI_EXPORT size_t webui_get_child_process_id(size_t window);
WEBUI_EXPORT size_t webui_get_parent_process_id(size_t window);
2023-09-12 00:18:31 +03:00
// -- Wrapper's Interface -------------
2023-09-12 01:09:42 +03:00
2023-08-25 08:31:50 +03:00
// Bind a specific html element click event with a function. Empty element means all events. This replaces `webui_bind()`. The func is (Window, EventType, Element, Data, DataSize, EventNumber).
WEBUI_EXPORT size_t webui_interface_bind(size_t window, const char* element, void (*func)(size_t, size_t, char*, char*, size_t, size_t));
2023-09-12 01:09:42 +03:00
2023-08-25 08:31:50 +03:00
// 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, size_t event_number, const char* response);
2023-09-12 01:09:42 +03:00
2023-09-13 22:32:14 +03:00
/**
* @brief Check if the app still running.
*
* @example if (webui_interface_is_app_running()) ...
*/
WEBUI_EXPORT bool webui_interface_is_app_running(void);
2023-09-12 01:09:42 +03:00
2023-08-25 08:31:50 +03:00
// Get a unique window ID.
WEBUI_EXPORT size_t webui_interface_get_window_id(size_t window);
2023-09-12 01:09:42 +03:00
// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
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 */