Skip to content

Commit 7d22079

Browse files
committed
Moving Extensions To App Level
1 parent 876942b commit 7d22079

3 files changed

Lines changed: 65 additions & 87 deletions

File tree

examples/C/call_js_from_c/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ void my_function_count(webui_event_t* e) {
3737

3838
// Run JavaScript (Quick Way)
3939
webui_run(e->window, js);
40+
41+
// Alternatively, Using `webui_extensions.h`
42+
// webui_run_fmt(e->window, "SetCount(%d);", count);
4043
}
4144

4245
int main() {

include/webui_extensions.h

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
WebUI Library Extras
2+
WebUI Library
33
https://webui.me
44
https://github.com/webui-dev/webui
55
Copyright (c) 2020-2025 Hassan Draga.
@@ -11,31 +11,12 @@
1111
#ifndef _WEBUI_EXTENSIONS_H
1212
#define _WEBUI_EXTENSIONS_H
1313

14+
#include "webui.h"
15+
1416
#ifdef __cplusplus
1517
extern "C" {
1618
#endif
1719

18-
/**
19-
* This is to help the compiler identify whether to compile the extensions API funtions or not
20-
* - If you are including the webui.c file to your source file directly,
21-
* and wish to use the extensions API functions,
22-
* please include this header file before including webui.c,
23-
* or define this macro manually before including webui.c
24-
* - In other cases, as long as the webui.c file is not included directly,
25-
* you might need to define this macro in your compiler settings
26-
*/
27-
#ifndef WEBUI_EXTENSION_API
28-
#define WEBUI_EXTENSION_API
29-
#endif
30-
31-
#ifndef WEBUI_EXPORT
32-
#define WEBUI_EXPORT
33-
#warning "WEBUI_EXPORT not defined; Please include webui.h before webui_extensions.h"
34-
#endif
35-
36-
#include <stdbool.h>
37-
#include <stddef.h>
38-
3920
/**
4021
* @brief Construct a JavaScript string from a format string,
4122
* then execute it without waiting for the response. All clients.
@@ -51,7 +32,31 @@ extern "C" {
5132
*
5233
* @example webui_run_fmt(myWindow, "alert('Hello %s');", "World");
5334
*/
54-
WEBUI_EXPORT void webui_run_fmt(size_t window, const char* fmt, ...);
35+
void webui_run_fmt(size_t window, const char* fmt, ...) {
36+
37+
int len;
38+
char* buf;
39+
40+
// Precalculate actual string length
41+
va_list args;
42+
va_start(args, fmt);
43+
len = vsnprintf(NULL, 0, fmt, args);
44+
va_end(args);
45+
46+
if (len < 1) return;
47+
48+
buf = (char*)webui_malloc(len + 1);
49+
if (buf == NULL) return;
50+
51+
// Format and execute
52+
va_start(args, fmt);
53+
vsnprintf(buf, len + 1, fmt, args);
54+
va_end(args);
55+
56+
webui_run(window, buf);
57+
58+
webui_free((void*)buf);
59+
}
5560

5661
/**
5762
* @brief Construct a JavaScript string from a format string,
@@ -75,12 +80,42 @@ WEBUI_EXPORT void webui_run_fmt(size_t window, const char* fmt, ...);
7580
* @example bool err = webui_script_fmt(myWindow, 0, myBuffer, myBufferSize,
7681
* "return %d + %d;", 4, 6);
7782
*/
78-
WEBUI_EXPORT bool webui_script_fmt(size_t window, size_t timeout,
79-
char* buffer, size_t buffer_length,
80-
const char* fmt, ...);
83+
bool webui_script_fmt(
84+
size_t window,
85+
size_t timeout,
86+
char* buffer,
87+
size_t buffer_length,
88+
const char* fmt, ...) {
89+
90+
int len;
91+
char* buf;
92+
bool status;
93+
94+
// Precalculate actual string length
95+
va_list args;
96+
va_start(args, fmt);
97+
len = vsnprintf(NULL, 0, fmt, args);
98+
va_end(args);
99+
100+
if (len < 1) return false;
101+
102+
buf = (char*)webui_malloc(len + 1);
103+
if (buf == NULL) return false;
104+
105+
// Format and execute
106+
va_start(args, fmt);
107+
vsnprintf(buf, len + 1, fmt, args);
108+
va_end(args);
109+
110+
status = webui_script(window, buf, timeout, buffer, buffer_length);
111+
112+
webui_free((void*)buf);
113+
114+
return status;
115+
}
81116

82117
#ifdef __cplusplus
83118
}
84119
#endif
85120

86-
#endif /*_WEBUI_EXTENSIONS_H */
121+
#endif /* _WEBUI_EXTENSIONS_H */

src/webui.c

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -847,33 +847,6 @@ void webui_run(size_t window, const char* script) {
847847
_webui_send_all(win, 0, WEBUI_CMD_JS_QUICK, script, js_len);
848848
}
849849

850-
#ifdef WEBUI_EXTENSION_API
851-
void webui_run_fmt(size_t window, const char* fmt, ...) {
852-
int len;
853-
char* buf;
854-
855-
// Precalculate actual string length
856-
va_list args;
857-
va_start(args, fmt);
858-
len = vsnprintf(NULL, 0, fmt, args);
859-
va_end(args);
860-
861-
if (len < 1) return;
862-
863-
buf = (char*)_webui_malloc(len + 1);
864-
if (buf == NULL) return;
865-
866-
// Format and execute
867-
va_start(args, fmt);
868-
vsnprintf(buf, len + 1, fmt, args);
869-
va_end(args);
870-
871-
webui_run(window, buf);
872-
873-
_webui_free_mem((void*)buf);
874-
}
875-
#endif /* WEBUI_EXTENSION_API */
876-
877850
void webui_set_close_handler_wv(size_t window, bool(*close_handler)(size_t window)) {
878851

879852
// Initialization
@@ -1092,39 +1065,6 @@ bool webui_script(size_t window, const char* script, size_t timeout,
10921065
return webui_script_client(&e, script, timeout, buffer, buffer_length);
10931066
}
10941067

1095-
#ifdef WEBUI_EXTENSION_API
1096-
bool webui_script_fmt(
1097-
size_t window, size_t timeout,
1098-
char* buffer, size_t buffer_length,
1099-
const char* fmt, ...) {
1100-
int len;
1101-
char* buf;
1102-
bool status;
1103-
1104-
// Precalculate actual string length
1105-
va_list args;
1106-
va_start(args, fmt);
1107-
len = vsnprintf(NULL, 0, fmt, args);
1108-
va_end(args);
1109-
1110-
if (len < 1) return false;
1111-
1112-
buf = (char*)_webui_malloc(len + 1);
1113-
if (buf == NULL) return false;
1114-
1115-
// Format and execute
1116-
va_start(args, fmt);
1117-
vsnprintf(buf, len + 1, fmt, args);
1118-
va_end(args);
1119-
1120-
status = webui_script(window, buf, timeout, buffer, buffer_length);
1121-
1122-
_webui_free_mem((void*)buf);
1123-
1124-
return status;
1125-
}
1126-
#endif /* WEBUI_EXTENSION_API */
1127-
11281068
static uint32_t _webui_generate_random_uint32() {
11291069
uint32_t timestamp = (uint32_t) time(NULL);
11301070
// Get the higher 16 bits

0 commit comments

Comments
 (0)