Skip to content

Commit c9087c3

Browse files
committed
Adding webui_interface_set_response_file_handler
1 parent 4dcf4ce commit c9087c3

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

include/webui.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,17 @@ WEBUI_EXPORT void webui_set_file_handler(size_t window, const void* (*handler)(c
463463
*/
464464
WEBUI_EXPORT void webui_set_file_handler_window(size_t window, const void* (*handler)(size_t window, const char* filename, int* length));
465465

466+
/**
467+
* @brief Use this API to set a file handler response if your backend need async
468+
* response for `webui_set_file_handler()`.
469+
*
470+
* @param window The window number
471+
* @param response The response buffer
472+
* @param length The response size
473+
*
474+
* @example webui_interface_set_response_file_handler(myWindow, buffer, 1024);
475+
*/
476+
WEBUI_EXPORT void webui_interface_set_response_file_handler(size_t window, const void* response, int length);
466477

467478
/**
468479
* @brief Check if the specified window is still running.

src/webui.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ typedef struct _webui_window_t {
339339
size_t process_id;
340340
const void*(*files_handler)(const char* filename, int* length);
341341
const void*(*files_handler_window)(size_t window, const char* filename, int* length);
342+
const void* file_handler_async_response;
343+
int file_handler_async_len;
344+
bool file_handler_async_done;
342345
webui_event_inf_t* events[WEBUI_MAX_IDS];
343346
size_t events_count;
344347
bool is_public;
@@ -3675,6 +3678,34 @@ void webui_interface_set_response(size_t window, size_t event_number, const char
36753678
#endif
36763679
}
36773680

3681+
void webui_interface_set_response_file_handler(size_t window, const void* response, int length) {
3682+
3683+
#ifdef WEBUI_LOG
3684+
printf("[User] webui_interface_set_response_file_handler()\n");
3685+
printf("[User] webui_interface_set_response_file_handler() -> window #%zu\n", window);
3686+
printf("[User] webui_interface_set_response_file_handler() -> Response %zu bytes\n", length);
3687+
#endif
3688+
3689+
// Initialization
3690+
_webui_init();
3691+
3692+
// Dereference
3693+
if (_webui_mutex_is_exit_now(WEBUI_MUTEX_NONE) || _webui.wins[window] == NULL)
3694+
return;
3695+
_webui_window_t* win = _webui.wins[window];
3696+
3697+
// Set the response
3698+
win->file_handler_async_response = response;
3699+
win->file_handler_async_len = length;
3700+
3701+
// Async response
3702+
if (_webui.config.asynchronous_response) {
3703+
_webui_mutex_lock(&_webui.mutex_async_response);
3704+
win->file_handler_async_done = true;
3705+
_webui_mutex_unlock(&_webui.mutex_async_response);
3706+
}
3707+
}
3708+
36783709
bool webui_interface_is_app_running(void) {
36793710

36803711
#ifdef WEBUI_LOG
@@ -4494,10 +4525,28 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
44944525
printf("[Call]\n");
44954526
#endif
44964527

4528+
// Async response ini
4529+
if (_webui.config.asynchronous_response) {
4530+
win->file_handler_async_response = NULL;
4531+
win->file_handler_async_len = 0;
4532+
win->file_handler_async_done = false;
4533+
}
4534+
44974535
// True if we pass the window num to the handler, false otherwise.
44984536
int is_file_handler_window = win->files_handler_window != NULL;
44994537
const void* callback_resp = is_file_handler_window ? win->files_handler_window(win->num, url, (int*)&length) : win->files_handler(url, (int*)&length);
45004538

4539+
// Async response wait
4540+
if (_webui.config.asynchronous_response) {
4541+
bool done = false;
4542+
while (!done) {
4543+
_webui_sleep(10);
4544+
_webui_mutex_lock(&_webui.mutex_async_response);
4545+
if(win->file_handler_async_done) done = true;
4546+
_webui_mutex_unlock(&_webui.mutex_async_response);
4547+
}
4548+
}
4549+
45014550
if (callback_resp != NULL) {
45024551

45034552
// File content found

0 commit comments

Comments
 (0)