webui/include/webui.h

294 lines
9.7 KiB
C
Raw Normal View History

2022-09-25 02:35:56 +03:00
/*
2022-10-25 05:35:05 +03:00
WebUI Library 2.0.2
2022-09-25 02:35:56 +03:00
http://webui.me
https://github.com/alifcommunity/webui
Licensed under GNU General Public License v3.0.
Copyright (C)2022 Hassan DRAGA <https://github.com/hassandraga>.
*/
#ifndef _WEBUI_H
#define _WEBUI_H
#if defined(_MSC_VER) || defined(__TINYC__)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT extern
#endif
#define WEBUI_HEADER_SIGNATURE 0xFF // All packets should start with this 8bit
#define WEBUI_HEADER_JS 0xFE // Javascript result in frontend
#define WEBUI_HEADER_CLICK 0xFD // Click event
#define WEBUI_HEADER_SWITCH 0xFC // Frontend refresh
#define WEBUI_HEADER_CLOSE 0xFB // Close window
#define WEBUI_HEADER_CALL_FUNC 0xFA // Call a backend function
#define WEBUI_MAX_ARRAY (32) // Max thread, servers, windows..
#define WEBUI_MIN_PORT (8080) // Minimum socket port
#define WEBUI_MAX_PORT (8335) // Should be less than 65535
2022-10-19 07:38:54 +03:00
#define WEBUI_MAX_BUF (512000) // 512 Kb max dynamic memory allocation
2022-09-25 02:35:56 +03:00
#define WEBUI_DEFAULT_PATH "." // Default root path
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>
#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
// #include <SDKDDKVer.h> // Fix _WIN32_WINNT warning
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <direct.h>
#include <io.h>
#include <tchar.h>
#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 <pthread.h> // POSIX threading
#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>
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
2022-10-20 02:48:47 +03:00
// -- macOS ---------------------------
2022-09-25 02:35:56 +03:00
// ...
2022-10-19 07:38:54 +03:00
struct webui_window_t;
2022-10-25 05:35:05 +03:00
typedef struct webui_timer_t {
struct timespec start;
struct timespec now;
} webui_timer_t;
2022-09-25 02:35:56 +03:00
typedef struct webui_event_t {
unsigned int window_id;
unsigned int element_id;
char* element_name;
2022-10-19 07:38:54 +03:00
struct webui_window_t* window;
2022-09-25 02:35:56 +03:00
} webui_event_t;
typedef struct webui_window_core_t {
unsigned int window_number;
bool server_running;
bool connected;
bool server_handled;
bool multi_access;
bool server_root;
unsigned int server_port;
bool is_bind_all;
char* url;
2022-10-19 07:38:54 +03:00
void (*cb_all[1]) (webui_event_t* e);
2022-09-25 02:35:56 +03:00
const char* html;
2022-10-19 07:38:54 +03:00
const char* html_cpy;
2022-09-25 02:35:56 +03:00
const char* icon;
const char* icon_type;
unsigned int CurrentBrowser;
char* browser_path;
char* profile_path;
unsigned int connections;
2022-09-29 02:41:24 +03:00
unsigned int runtime;
2022-09-29 11:32:28 +03:00
bool detect_process_close;
#ifdef _WIN32
HANDLE server_thread;
#else
2022-10-24 03:06:15 +03:00
pthread_t server_thread;
2022-10-25 05:35:05 +03:00
#endif
2022-09-25 02:35:56 +03:00
} webui_window_core_t;
typedef struct webui_window_t {
webui_window_core_t core;
char* path;
} webui_window_t;
typedef struct webui_javascript_result_t {
bool error;
unsigned int length;
const char* data;
} webui_javascript_result_t;
typedef struct webui_javascript_t {
2022-10-20 02:48:47 +03:00
const char* script;
2022-09-25 02:35:56 +03:00
unsigned int timeout;
webui_javascript_result_t result;
} webui_javascript_t;
typedef struct webui_cb_t {
webui_window_t* win;
char* element_id;
char* element_name;
} webui_cb_t;
2022-09-29 11:32:28 +03:00
typedef struct webui_cmd_async_t {
webui_window_t* win;
char* cmd;
} webui_cmd_async_t;
2022-09-25 02:35:56 +03:00
typedef struct webui_custom_browser_t {
char* app;
char* arg;
bool auto_link;
} webui_custom_browser_t;
typedef struct webui_browser_t {
unsigned int any; // 0
unsigned int chrome; // 1
unsigned int firefox; // 2
unsigned int edge; // 3
unsigned int safari; // 4
unsigned int chromium; // 5
unsigned int custom; // 99
} webui_browser_t;
2022-09-29 02:41:24 +03:00
typedef struct webui_runtime_t {
unsigned int none; // 0
unsigned int deno; // 1
unsigned int nodejs; // 2
} webui_runtime_t;
2022-09-25 02:35:56 +03:00
typedef struct webui_t {
unsigned int servers;
unsigned int connections;
webui_custom_browser_t *custom_browser;
bool wait_for_socket_window;
char* html_elements[WEBUI_MAX_ARRAY];
unsigned int used_ports[WEBUI_MAX_ARRAY];
unsigned int last_window;
unsigned int startup_timeout;
bool use_timeout;
bool timeout_extra;
bool exit_now;
2022-10-19 07:38:54 +03:00
const char* run_responses[WEBUI_MAX_ARRAY];
2022-09-25 02:35:56 +03:00
bool run_done[WEBUI_MAX_ARRAY];
bool run_error[WEBUI_MAX_ARRAY];
unsigned int run_last_id;
struct mg_mgr* mg_mgrs[WEBUI_MAX_ARRAY];
struct mg_connection* mg_connections[WEBUI_MAX_ARRAY];
webui_browser_t browser;
2022-09-29 02:41:24 +03:00
webui_runtime_t runtime;
2022-09-25 02:35:56 +03:00
bool initialized;
2022-10-19 07:38:54 +03:00
void (*cb[WEBUI_MAX_ARRAY]) (webui_event_t* e);
2022-10-20 02:48:47 +03:00
void (*cb_int[WEBUI_MAX_ARRAY])(unsigned int, unsigned int, char*);
2022-09-29 02:41:24 +03:00
char* executable_path;
2022-09-25 02:35:56 +03:00
void *ptr_list[WEBUI_MAX_ARRAY];
unsigned int ptr_position;
2022-09-25 02:35:56 +03:00
size_t ptr_size[WEBUI_MAX_ARRAY];
} webui_t;
2022-10-20 02:48:47 +03:00
// -- Definitions ---------------------
2022-09-25 02:35:56 +03:00
EXPORT webui_t webui;
EXPORT void webui_loop();
EXPORT void webui_exit();
EXPORT bool webui_any_window_is_open();
EXPORT void webui_set_timeout(unsigned int second);
EXPORT webui_window_t* webui_new_window();
EXPORT bool webui_show(webui_window_t* win, const char* html, unsigned int browser);
2022-10-19 07:38:54 +03:00
EXPORT bool webui_copy_show(webui_window_t* win, const char* html, unsigned int browser);
2022-09-25 02:35:56 +03:00
EXPORT void webui_set_icon(webui_window_t* win, const char* icon_s, const char* type_s);
EXPORT void webui_allow_multi_access(webui_window_t* win, bool status);
EXPORT bool webui_set_root_folder(webui_window_t* win, const char* path);
2022-09-29 02:41:24 +03:00
EXPORT const char* webui_new_server(webui_window_t* win, const char* path, const char* index_html);
2022-09-25 02:35:56 +03:00
EXPORT void webui_close(webui_window_t* win);
EXPORT bool webui_is_show(webui_window_t* win);
EXPORT void webui_run_js(webui_window_t* win, webui_javascript_t* javascript);
2022-10-19 07:38:54 +03:00
EXPORT unsigned int webui_bind(webui_window_t* win, const char* element, void (*func) (webui_event_t* e));
EXPORT void webui_bind_all(webui_window_t* win, void (*func) (webui_event_t* e));
2022-09-29 02:41:24 +03:00
EXPORT bool webui_open(webui_window_t* win, const char* url, unsigned int browser);
2022-09-26 03:05:49 +03:00
EXPORT void webui_free_js(webui_javascript_t* javascript);
2022-09-29 11:32:28 +03:00
EXPORT void webui_runtime(webui_window_t* win, unsigned int runtime);
EXPORT void webui_detect_process_close(webui_window_t* win, bool status);
2022-09-25 02:35:56 +03:00
2022-10-20 02:48:47 +03:00
// -- Interface -----------------------
// To help other languages to use WebUI
typedef struct webui_javascript_int_t {
char* script;
unsigned int timeout;
bool error;
unsigned int length;
const char* data;
} webui_javascript_int_t;
EXPORT unsigned int webui_bind_int(webui_window_t* win, const char* element, void (*func)(unsigned int, unsigned int, char*));
EXPORT void webui_run_js_int(webui_window_t* win, const char* script, unsigned int timeout, bool* error, unsigned int* length, char* data);
EXPORT void webui_run_js_int_struct(webui_window_t* win, webui_javascript_int_t* js_int);
2022-09-25 02:35:56 +03:00
// Core
EXPORT void _webui_ini();
EXPORT unsigned int _webui_get_cb_index(char* element);
EXPORT unsigned int _webui_set_cb_index(char* element);
EXPORT unsigned int _webui_get_free_port();
EXPORT unsigned int _webui_get_new_window_number();
EXPORT void _webui_wait_for_startup();
EXPORT void _webui_free_port(unsigned int port);
EXPORT void _webui_set_custom_browser(webui_custom_browser_t* p);
EXPORT char* _webui_get_current_path();
EXPORT void _webui_window_receive(webui_window_t* win, const char* packet, size_t len);
EXPORT void _webui_window_send(webui_window_t* win, char* packet, size_t packets_size);
EXPORT void _webui_window_event(webui_window_t* win, char* element_id, char* element);
2022-10-19 07:38:54 +03:00
EXPORT unsigned int _webui_window_get_number(webui_window_t* win);
2022-09-25 02:35:56 +03:00
EXPORT void _webui_window_open(webui_window_t* win, char* link, unsigned int browser);
EXPORT int _webui_cmd_sync(char* cmd, bool show);
EXPORT int _webui_cmd_async(char* cmd, bool show);
2022-09-30 04:03:57 +03:00
EXPORT int _webui_run_browser(webui_window_t* win, char* cmd);
2022-09-25 02:35:56 +03:00
EXPORT void _webui_browser_clean();
EXPORT bool _webui_browser_exist(webui_window_t* win, unsigned int browser);
2022-10-19 07:38:54 +03:00
EXPORT const char* _webui_browser_get_temp_path(unsigned int browser);
2022-09-30 04:03:57 +03:00
EXPORT bool _webui_folder_exist(char* folder);
2022-09-25 02:35:56 +03:00
EXPORT bool _webui_browser_create_profile_folder(webui_window_t* win, unsigned int browser);
2022-09-29 02:41:24 +03:00
EXPORT bool _webui_browser_start_edge(webui_window_t* win, const char* address);
EXPORT bool _webui_browser_start_firefox(webui_window_t* win, const char* address);
EXPORT bool _webui_browser_start_custom(webui_window_t* win, const char* address);
EXPORT bool _webui_browser_start_chrome(webui_window_t* win, const char* address);
EXPORT bool _webui_browser_start(webui_window_t* win, const char* address, unsigned int browser);
2022-10-25 05:35:05 +03:00
EXPORT long _webui_timer_diff(struct timespec *start, struct timespec *end);
EXPORT void _webui_timer_start(webui_timer_t* t);
EXPORT bool _webui_timer_is_end(webui_timer_t* t, unsigned int ms);
EXPORT void _webui_timer_clock_gettime(struct timespec *spec);
2022-09-25 02:35:56 +03:00
#ifdef _WIN32
EXPORT DWORD WINAPI _webui_cb(LPVOID _arg);
2022-09-30 04:03:57 +03:00
EXPORT DWORD WINAPI _webui_run_browser_task(LPVOID _arg);
EXPORT int _webui_system_win32(char* cmd, bool show);
2022-09-25 02:35:56 +03:00
#else
2022-10-24 03:06:15 +03:00
EXPORT void* _webui_cb(void* _arg);
EXPORT void* _webui_run_browser_task(void* _arg);
2022-09-25 02:35:56 +03:00
#endif
#endif /* _WEBUI_H */