diff --git a/snipplets/code/C/rest_server/.editorconfig b/snipplets/code/C/rest_server/.editorconfig new file mode 100644 index 0000000..d4b6d88 --- /dev/null +++ b/snipplets/code/C/rest_server/.editorconfig @@ -0,0 +1,2 @@ +[{*.c,*.h}] +indent_size = unset diff --git a/snipplets/code/C/unixsocket/.editorconfig b/snipplets/code/C/unixsocket/.editorconfig new file mode 100644 index 0000000..d4b6d88 --- /dev/null +++ b/snipplets/code/C/unixsocket/.editorconfig @@ -0,0 +1,2 @@ +[{*.c,*.h}] +indent_size = unset diff --git a/snipplets/code/C/webui/.editorconfig b/snipplets/code/C/webui/.editorconfig new file mode 100644 index 0000000..d4b6d88 --- /dev/null +++ b/snipplets/code/C/webui/.editorconfig @@ -0,0 +1,2 @@ +[{*.c,*.h}] +indent_size = unset diff --git a/snipplets/code/C/webui/call_c_from_js.c b/snipplets/code/C/webui/call_c_from_js.c new file mode 100644 index 0000000..1844d8c --- /dev/null +++ b/snipplets/code/C/webui/call_c_from_js.c @@ -0,0 +1,129 @@ +// Call C from JavaScript Example + +#include "webui.h" + +void my_function_string(webui_event_t* e) { + + // JavaScript: + // webui_fn('MyID_One', 'Hello'); + + const char* str = webui_get_string(e); + printf("my_function_string: %s\n", str); // Hello + + // Need Multiple Arguments? + // + // WebUI support only one argument. To get multiple arguments + // you can send a JSON string from JavaScript then decode it. + // Example: + // + // my_json = my_json_decoder(str); + // foo = my_json[0]; + // bar = my_json[1]; +} + +void my_function_integer(webui_event_t* e) { + + // JavaScript: + // webui_fn('MyID_Two', 123456789); + + long long number = webui_get_int(e); + printf("my_function_integer: %lld\n", number); // 123456789 +} + +void my_function_boolean(webui_event_t* e) { + + // JavaScript: + // webui_fn('MyID_Three', true); + + bool status = webui_get_bool(e); // True + if(status) + printf("my_function_boolean: True\n"); + else + printf("my_function_boolean: False\n"); +} + +void my_function_with_response(webui_event_t* e) { + + // JavaScript: + // const result = webui_fn('MyID_Four', number); + + long long number = webui_get_int(e); + number = number * 2; + printf("my_function_with_response: %lld\n", number); + + // Send back the response to JavaScript + webui_return_int(e, number); +} + +int main() { + + // HTML + const char* my_html = + "" + "
" + "Call C function with argument (See the logs in your terminal)
" + "Call C function and wait for the response
" + "