diff --git a/examples/C/public_network_access/GNUmakefile b/examples/C/public_network_access/GNUmakefile new file mode 100644 index 00000000..fae54ddb --- /dev/null +++ b/examples/C/public_network_access/GNUmakefile @@ -0,0 +1,141 @@ +# WebUI C Example + +# == 1. VARIABLES ============================================================= + +MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) +PROJECT_DIR := $(dir $(MAKEFILE_PATH))/../../../ +TARGET := $(firstword $(MAKECMDGOALS)) +LIB_DIR := $(PROJECT_DIR)/dist +ifeq ($(TARGET), debug) +LIB_DIR := $(LIB_DIR)/debug +endif +INCLUDE_DIR := $(PROJECT_DIR)/include +WEBUI_LIB_NAME = webui-2 +ifeq ($(WEBUI_USE_TLS), 1) +WEBUI_LIB_NAME = webui-2-secure +endif + +# ARGS +# Set a compiler when running on Linux via `make CC=gcc` / `make CC=clang` +CC = gcc +# Build the WebUI library if running via `make BUILD_LIB=true` +BUILD_LIB ?= + +# BUILD FLAGS +STATIC_BUILD_FLAGS = main.c -I"$(INCLUDE_DIR)" -L"$(LIB_DIR)" +DYN_BUILD_FLAGS = main.c -I"$(INCLUDE_DIR)" -L"$(LIB_DIR)" + +# Platform conditions +ifeq ($(OS),Windows_NT) + # Windows + PLATFORM := windows + SHELL := CMD + STATIC_BUILD_FLAGS += -l$(WEBUI_LIB_NAME)-static -lws2_32 -Wall -luser32 -static + COPY_LIB_CMD := @copy "$(LIB_DIR)\$(WEBUI_LIB_NAME).dll" "$(WEBUI_LIB_NAME).dll" + DYN_BUILD_FLAGS += "$(WEBUI_LIB_NAME).dll" -lws2_32 -Wall -luser32 + STATIC_OUT := main.exe + DYN_OUT := main-dyn.exe + LWS2_OPT := -lws2_32 + STRIP_OPT := --strip-all + CONSOLE_APP := -Wl,-subsystem=console + GUI_APP := -Wl,-subsystem=windows +else + STATIC_BUILD_FLAGS += -lpthread -lm -l$(WEBUI_LIB_NAME)-static + DYN_BUILD_FLAGS += -lpthread -lm + STATIC_OUT := main + DYN_OUT := main-dyn + CONSOLE_APP := + GUI_APP := + ifeq ($(shell uname),Darwin) + # MacOS + PLATFORM := macos + CC = clang + COPY_LIB_CMD := @cp "$(LIB_DIR)/$(WEBUI_LIB_NAME).dylib" "$(WEBUI_LIB_NAME).dylib" + DYN_BUILD_FLAGS += "./$(WEBUI_LIB_NAME).dylib" + else + # Linux + PLATFORM := linux + COPY_LIB_CMD := @cp "$(LIB_DIR)/$(WEBUI_LIB_NAME).so" "$(WEBUI_LIB_NAME).so" + DYN_BUILD_FLAGS += "./$(WEBUI_LIB_NAME).so" + STRIP_OPT := --strip-all + ifeq ($(CC),clang) + LLVM_OPT := llvm- + endif + endif +endif + +# == 2.TARGETS ================================================================ + +all: release + +debug: --validate-args +ifeq ($(BUILD_LIB),true) + @cd "$(PROJECT_DIR)" && $(MAKE) debug +endif +# Static with Debug info +ifneq ($(WEBUI_USE_TLS), 1) + @echo "Build C Example ($(CC) debug static)..." + @$(CC) -g $(CONSOLE_APP) $(STATIC_BUILD_FLAGS) $(LWS2_OPT) -o $(STATIC_OUT) +endif +# Dynamic with Debug info + @echo "Build C Example ($(CC) debug dynamic)..." + $(COPY_LIB_CMD) + @$(CC) -g $(CONSOLE_APP) $(DYN_BUILD_FLAGS) $(LWS2_OPT) -o $(DYN_OUT) +# Clean +ifeq ($(PLATFORM),windows) + @- del *.o >nul 2>&1 +else + @- rm -f *.o + @- rm -rf *.dSYM # macOS +endif + @echo "Done." + +release: --validate-args +ifeq ($(BUILD_LIB),true) + @cd "$(PROJECT_DIR)" && $(MAKE) +endif +# Static Release +ifneq ($(WEBUI_USE_TLS), 1) + @echo "Build C Example ($(CC) release static)..." + @$(CC) -Os $(GUI_APP) $(STATIC_BUILD_FLAGS) $(LWS2_OPT) -o $(STATIC_OUT) + @$(LLVM_OPT)strip $(STRIP_OPT) $(STATIC_OUT) +endif +# Dynamic Release + @echo "Build C Example ($(CC) release dynamic)..." + $(COPY_LIB_CMD) + @$(CC) $(GUI_APP) $(DYN_BUILD_FLAGS) $(LWS2_OPT) -o $(DYN_OUT) + @$(LLVM_OPT)strip $(STRIP_OPT) $(DYN_OUT) +# Clean +ifeq ($(PLATFORM),windows) + @- del *.o >nul 2>&1 +else + @- rm -f *.o + @- rm -rf *.dSYM # macOS +endif + @echo "Done." + +clean: --clean-$(PLATFORM) + +# INTERNAL TARGETS + +--validate-args: +ifneq ($(filter $(CC),gcc clang aarch64-linux-gnu-gcc arm-linux-gnueabihf-gcc musl-gcc),$(CC)) +$(error Invalid compiler specified: `$(CC)`) +endif + +--clean-linux: --clean-unix + +--clean-macos: --clean-unix + +--clean-unix: + - rm -f *.o + - rm -f *.a + - rm -f *.so + - rm -f *.dylib + - rm -rf *.dSYM + +--clean-windows: + - del *.o >nul 2>&1 + - del *.dll >nul 2>&1 + - del *.a >nul 2>&1 + - del *.exe >nul 2>&1 diff --git a/examples/C/public_network_access/Makefile b/examples/C/public_network_access/Makefile new file mode 100644 index 00000000..b3d5da5d --- /dev/null +++ b/examples/C/public_network_access/Makefile @@ -0,0 +1,65 @@ +# WebUI C Example +# Windows - Microsoft Visual C + +SHELL = CMD +LIB_DIR = ../../../dist +INCLUDE_DIR = ../../../include +WEBUI_LIB_NAME = webui-2 +!IF "$(WEBUI_USE_TLS)" == "1" +WEBUI_LIB_NAME = webui-2-secure +!ENDIF + +# Build the WebUI library if running `nmake BUILD_LIB=true` +BUILD_LIB = + +all: release + +debug: +!IF "$(BUILD_LIB)" == "true" + @cd "$(LIB_DIR)" && cd .. && $(MAKE) debug +!ENDIF +# Static with Debug info +!IF "$(WEBUI_USE_TLS)" != "1" + @echo Build C Example (Static Debug)... + @cl /Zi main.c /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)\debug" /MACHINE:X64 /SUBSYSTEM:CONSOLE $(WEBUI_LIB_NAME)-static.lib user32.lib Advapi32.lib Shell32.lib /OUT:main.exe 1>NUL 2>&1 +!ENDIF +# Dynamic with Debug info + @echo Build C Example (Dynamic Debug)... + @copy "$(LIB_DIR)\debug\$(WEBUI_LIB_NAME).dll" "$(WEBUI_LIB_NAME).dll" + @cl /Zi main.c /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)\debug" /MACHINE:X64 /SUBSYSTEM:CONSOLE $(WEBUI_LIB_NAME).lib user32.lib Advapi32.lib Shell32.lib /OUT:main-dyn.exe 1>NUL 2>&1 +# Clean + @- del *.exp >nul 2>&1 + @- del *.ilk >nul 2>&1 + @- del *.lib >nul 2>&1 + @- del *.obj >nul 2>&1 + @- del *.pdb >nul 2>&1 + @echo Done. + +release: +!IF "$(BUILD_LIB)" == "true" + @cd "$(LIB_DIR)" && cd .. && $(MAKE) +!ENDIF +# Static Release +!IF "$(WEBUI_USE_TLS)" != "1" + @echo Build C Example (Static Release)... + @cl main.c /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)" /MACHINE:X64 /SUBSYSTEM:WINDOWS $(WEBUI_LIB_NAME)-static.lib user32.lib Advapi32.lib Shell32.lib /OUT:main.exe 1>NUL 2>&1 +!ENDIF +# Dynamic Release + @echo Build C Example (Dynamic Release)... + @copy "$(LIB_DIR)\$(WEBUI_LIB_NAME).dll" "$(WEBUI_LIB_NAME).dll" + @cl main.c /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)" /MACHINE:X64 /SUBSYSTEM:WINDOWS $(WEBUI_LIB_NAME).lib user32.lib Advapi32.lib Shell32.lib /OUT:main-dyn.exe 1>NUL 2>&1 +# Clean + @- del *.exp >nul 2>&1 + @- del *.ilk >nul 2>&1 + @- del *.lib >nul 2>&1 + @- del *.obj >nul 2>&1 + @- del *.pdb >nul 2>&1 + @echo Done. + +clean: + - del *.obj >nul 2>&1 + - del *.ilk >nul 2>&1 + - del *.pdb >nul 2>&1 + - del *.exp >nul 2>&1 + - del *.exe >nul 2>&1 + - del *.lib >nul 2>&1 diff --git a/examples/C/public_network_access/main.c b/examples/C/public_network_access/main.c new file mode 100644 index 00000000..7789b62a --- /dev/null +++ b/examples/C/public_network_access/main.c @@ -0,0 +1,121 @@ +// Public Network Access Example + +#include "webui.h" + +// Windows +size_t private_window = 0; +size_t public_window = 0; + +void app_exit(webui_event_t* e) { + webui_exit(); +} + +void public_window_events(webui_event_t* e) { + if (e->event_type == WEBUI_EVENT_CONNECTED) { + // New connection + webui_run(private_window, "document.getElementById(\"Logs\").value += \"New connection.\\n\";"); + } + else if (e->event_type == WEBUI_EVENT_DISCONNECTED) { + // Disconnection + webui_run(private_window, "document.getElementById(\"Logs\").value += \"Disconnected.\\n\";"); + } +} + +int main() { + + // Main Private Window HTML + const char* private_html = "" + "" + " " + " " + " " + " Public Network Access Example" + " " + " " + " " + "

WebUI - Public Network Access Example

" + "
" + " The second public window is configured to be accessible from
" + " any device in the public network.
" + "
" + " Second public window link:
" + "

...

" + " Second public window events:
" + " " + "
" + " " + " " + ""; + + // Public Window HTML + const char* public_html = "" + "" + " " + " " + " " + " Welcome to Public UI" + " " + " " + "

Welcome to Public UI!

" + " " + ""; + + // Create windows + private_window = webui_new_window(); + public_window = webui_new_window(); + + // App + webui_set_timeout(0); // Wait forever (never timeout) + + // Public Window + webui_set_public(public_window, true); // Make URL accessible from public networks + webui_bind(public_window, "", public_window_events); // Bind all events + webui_show_browser(public_window, public_html, NoBrowser); // Set public window HTML + const char* public_win_url = webui_get_url(public_window); // Get URL of public window + + // Main Private Window + webui_bind(private_window, "Exit", app_exit); // Bind exit button + webui_show(private_window, private_html); // Show the window + + // Set URL in the UI + char javascript[1024]; + sprintf(javascript, "document.getElementById('urlSpan').innerHTML = '%s';", public_win_url); + webui_run(private_window, javascript); + + // Wait until all windows get closed + webui_wait(); + + // Free all memory resources (Optional) + webui_clean(); + + return 0; +} + +#if defined(_MSC_VER) +int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) { return main(); } +#endif