Skip to content

Commit 2aaf441

Browse files
authored
update: add new api (#75)
* update: add new api fix: malloc will check for success * update dependency
1 parent ceed5d4 commit 2aaf441

File tree

4 files changed

+141
-10
lines changed

4 files changed

+141
-10
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
.minimum_zig_version = "0.12.0",
55
.dependencies = .{
66
.webui = .{
7-
.url = "https://github.com/webui-dev/webui/archive/a44283b04d1ec0dee6544533d5a60a7e560634e9.tar.gz",
8-
.hash = "12206fc0555cec7364fba2adb1b9891face05acf14c5d3c54306cf85218b9e0cea4f",
7+
.url = "https://github.com/webui-dev/webui/archive/0ff3b1351b9e24be4463b1baf2c26966caeae74a.tar.gz",
8+
.hash = "12202382fb8300ffdb1e6f183d212a796b85321e8421162148137d04f2d470e8cf5b",
99
},
1010
},
1111
.paths = .{

examples/serve_a_folder/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ fn my_files_handler(filename: []const u8) ?[]const u8 {
106106
// Const static file example
107107
return test_txt;
108108
} else if (std.mem.eql(u8, filename, "/dynamic.html")) {
109-
const body = webui.malloc(1024);
109+
const body = webui.malloc(1024) catch unreachable;
110110
defer webui.free(body);
111-
const header_and_body = webui.malloc(1024);
111+
const header_and_body = webui.malloc(1024) catch unreachable;
112112

113113
count += 1;
114114

src/c.zig

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,3 +881,97 @@ pub extern fn webui_interface_get_size_at(
881881
event_number: usize,
882882
index: usize,
883883
) callconv(.C) usize;
884+
885+
/// @brief Show a window using embedded HTML, or a file. If the window is already
886+
/// open, it will be refreshed. Single client.
887+
///
888+
/// @param window The window number
889+
/// @param event_number The event number
890+
/// @param content The HTML, URL, Or a local file
891+
///
892+
/// @return Returns True if showing the window is successed.
893+
///
894+
/// @example webui_show_client(e, "<html>...</html>"); |
895+
/// webui_show_client(e, "index.html"); | webui_show_client(e, "http://...");
896+
pub extern fn webui_interface_show_client(
897+
window: usize,
898+
event_number: usize,
899+
content: [*:0]const u8,
900+
) callconv(.C) bool;
901+
902+
/// @brief Close a specific client.
903+
///
904+
/// @param window The window number
905+
/// @param event_number The event number
906+
///
907+
/// @example webui_close_client(e);
908+
pub extern fn webui_interface_close_client(
909+
window: usize,
910+
event_number: usize,
911+
) callconv(.C) void;
912+
913+
/// @brief Safely send raw data to the UI. Single client.
914+
///
915+
/// @param window The window number
916+
/// @param event_number The event number
917+
/// @param function The JavaScript function to receive raw data: `function
918+
/// myFunc(myData){}`
919+
/// @param raw The raw data buffer
920+
/// @param size The raw data size in bytes
921+
///
922+
/// @example webui_send_raw_client(e, "myJavaScriptFunc", myBuffer, 64);
923+
pub extern fn webui_interface_send_raw_client(
924+
window: usize,
925+
event_number: usize,
926+
function: [*:0]const u8,
927+
raw: [*c]const u8,
928+
size: usize,
929+
) callconv(.C) void;
930+
931+
/// @brief Navigate to a specific URL. Single client.
932+
///
933+
/// @param window The window number
934+
/// @param event_number The event number
935+
/// @param url Full HTTP URL
936+
///
937+
/// @example webui_navigate_client(e, "http://domain.com");
938+
pub extern fn webui_interface_navigate_client(
939+
window: usize,
940+
event_number: usize,
941+
url: [*:0]const u8,
942+
) callconv(.C) void;
943+
944+
/// @brief Run JavaScript without waiting for the response. Single client.
945+
///
946+
/// @param window The window number
947+
/// @param event_number The event number
948+
/// @param script The JavaScript to be run
949+
///
950+
/// @example webui_run_client(e, "alert('Hello');");
951+
pub extern fn webui_interface_run_client(
952+
window: usize,
953+
event_number: usize,
954+
script: [*:0]const u8,
955+
) callconv(.C) void;
956+
957+
/// @brief Run JavaScript and get the response back. Single client.
958+
/// Make sure your local buffer can hold the response.
959+
///
960+
/// @param window The window number
961+
/// @param event_number The event number
962+
/// @param script The JavaScript to be run
963+
/// @param timeout The execution timeout in seconds
964+
/// @param buffer The local buffer to hold the response
965+
/// @param buffer_length The local buffer size
966+
///
967+
/// @return Returns True if there is no execution error
968+
///
969+
/// @example bool err = webui_script_client(e, "return 4 + 6;", 0, myBuffer, myBufferSize);
970+
pub extern fn webui_interface_script_client(
971+
window: usize,
972+
event_number: usize,
973+
script: [*:0]const u8,
974+
timeout: usize,
975+
buffer: [*c]u8,
976+
buffer_length: usize,
977+
) callconv(.C) void;

src/webui.zig

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ pub fn free(buf: []const u8) void {
228228
/// Safely allocate memory using the WebUI memory management system
229229
/// it can be safely freed using `free()` at any time.
230230
/// In general, you should not use this function
231-
pub fn malloc(size: usize) []u8 {
232-
const ptr = c.webui_malloc(size).?; // TODO: Proper allocation failure check
231+
pub fn malloc(size: usize) ![]u8 {
232+
const ptr = c.webui_malloc(size) orelse return error.AllocateFailed;
233233
return @as([*]u8, @ptrCast(ptr))[0..size];
234234
}
235235

@@ -536,6 +536,41 @@ pub fn interfaceGetSizeAt(self: webui, event_number: usize, index: usize) usize
536536
return c.webui_interface_get_size_at(self.window_handle, event_number, index);
537537
}
538538

539+
// Show a window using embedded HTML, or a file. If the window is already
540+
pub fn interfaceShowClient(self: webui, event_number: usize, content: [:0]const u8) bool {
541+
return c.webui_interface_show_client(self.window_handle, event_number, content.ptr);
542+
}
543+
544+
// Close a specific client.
545+
pub fn interfaceCloseClient(self: webui, event_number: usize) void {
546+
c.webui_interface_close_client(self.window_handle, event_number);
547+
}
548+
549+
// Safely send raw data to the UI. Single client.
550+
pub fn interfaceSendRawClient(
551+
self: webui,
552+
event_number: usize,
553+
function: [:0]const u8,
554+
raw: []const u8,
555+
) void {
556+
c.webui_interface_send_raw_client(self.window_handle, event_number, function.ptr, raw.ptr, raw.len);
557+
}
558+
559+
// Navigate to a specific URL. Single client.
560+
pub fn interfaceNavigateClient(self: webui, event_number: usize, url: [:0]const u8) void {
561+
c.webui_interface_navigate_client(self.window_handle, event_number, url.ptr);
562+
}
563+
564+
// Run JavaScript without waiting for the response. Single client.
565+
pub fn interfaceRunClient(self: webui, event_number: usize, script_content: [:0]const u8) void {
566+
c.webui_interface_run_client(self.window_handle, event_number, script_content.ptr);
567+
}
568+
569+
// Run JavaScript and get the response back. Single client.
570+
pub fn interfaceScriptClient(self: webui, event_number: usize, script_content: [:0]const u8, timeout: usize, buffer: []u8) void {
571+
c.webui_interface_script_client(self.window_handle, event_number, script_content.ptr, timeout, buffer.ptr, buffer.len);
572+
}
573+
539574
/// a very convenient function for binding callback.
540575
/// you just need to pase a function to get param.
541576
/// no need to care webui param api.
@@ -752,10 +787,12 @@ pub const Config = enum(c_int) {
752787
/// root folder gets changed.
753788
/// Default: False
754789
folder_monitor,
755-
/// Allow multiple clients to connect to the same window,
756-
/// This is helpful for web apps (non-desktop software),
757-
/// Please see the documentation for more details.
758-
/// Default: False
790+
/// Allow or prevent WebUI from adding `webui_auth` cookies.
791+
/// WebUI uses these cookies to identify clients and block
792+
/// unauthorized access to the window content using a URL.
793+
/// Please keep this option to `True` if you want only a single
794+
/// client to access the window content.
795+
/// Default: True
759796
multi_client,
760797
/// Allow multiple clients to connect to the same window,
761798
/// This is helpful for web apps (non-desktop software),

0 commit comments

Comments
 (0)