tauri.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2017 Serge Zaitsev, (c) 2019 Tauri Apps
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef WEBVIEW_H
  25. #define WEBVIEW_H
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #ifdef WEBVIEW_STATIC
  30. #define WEBVIEW_API static
  31. #else
  32. #define WEBVIEW_API extern
  33. #endif
  34. #include <stdint.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <stdarg.h>
  38. struct webview;
  39. typedef void (*webview_external_invoke_cb_t)(struct webview *w,
  40. const char *arg);
  41. struct webview {
  42. const char *url;
  43. const char *title;
  44. int width;
  45. int height;
  46. int resizable;
  47. int debug;
  48. webview_external_invoke_cb_t external_invoke_cb;
  49. struct webview_priv priv;
  50. void *userdata;
  51. };
  52. enum webview_dialog_type {
  53. WEBVIEW_DIALOG_TYPE_OPEN = 0,
  54. WEBVIEW_DIALOG_TYPE_SAVE = 1,
  55. WEBVIEW_DIALOG_TYPE_ALERT = 2
  56. };
  57. #define WEBVIEW_DIALOG_FLAG_FILE (0 << 0)
  58. #define WEBVIEW_DIALOG_FLAG_DIRECTORY (1 << 0)
  59. #define WEBVIEW_DIALOG_FLAG_INFO (1 << 1)
  60. #define WEBVIEW_DIALOG_FLAG_WARNING (2 << 1)
  61. #define WEBVIEW_DIALOG_FLAG_ERROR (3 << 1)
  62. #define WEBVIEW_DIALOG_FLAG_ALERT_MASK (3 << 1)
  63. typedef void (*webview_dispatch_fn)(struct webview *w, void *arg);
  64. struct webview_dispatch_arg {
  65. webview_dispatch_fn fn;
  66. struct webview *w;
  67. void *arg;
  68. };
  69. #define DEFAULT_URL \
  70. "data:text/" \
  71. "html,%3C%21DOCTYPE%20html%3E%0A%3Chtml%20lang=%22en%22%3E%0A%3Chead%3E%" \
  72. "3Cmeta%20charset=%22utf-8%22%3E%3Cmeta%20http-equiv=%22X-UA-Compatible%22%" \
  73. "20content=%22IE=edge%22%3E%3C%2Fhead%3E%0A%3Cbody%3E%3Cdiv%20id=%22app%22%" \
  74. "3E%3C%2Fdiv%3E%3Cscript%20type=%22text%2Fjavascript%22%3E%3C%2Fscript%3E%" \
  75. "3C%2Fbody%3E%0A%3C%2Fhtml%3E"
  76. #define CSS_INJECT_FUNCTION \
  77. "(function(e){var " \
  78. "t=document.createElement('style'),d=document.head||document." \
  79. "getElementsByTagName('head')[0];t.setAttribute('type','text/" \
  80. "css'),t.styleSheet?t.styleSheet.cssText=e:t.appendChild(document." \
  81. "createTextNode(e)),d.appendChild(t)})"
  82. static const char *webview_check_url(const char *url) {
  83. if (url == NULL || strlen(url) == 0) {
  84. return DEFAULT_URL;
  85. }
  86. return url;
  87. }
  88. WEBVIEW_API int webview(const char *title, const char *url, int width,
  89. int height, int resizable);
  90. WEBVIEW_API int webview_init(struct webview *w);
  91. WEBVIEW_API int webview_loop(struct webview *w, int blocking);
  92. WEBVIEW_API int webview_eval(struct webview *w, const char *js);
  93. WEBVIEW_API int webview_inject_css(struct webview *w, const char *css);
  94. WEBVIEW_API void webview_set_title(struct webview *w, const char *title);
  95. WEBVIEW_API void webview_set_fullscreen(struct webview *w, int fullscreen);
  96. WEBVIEW_API void webview_set_color(struct webview *w, uint8_t r, uint8_t g,
  97. uint8_t b, uint8_t a);
  98. WEBVIEW_API void webview_dialog(struct webview *w,
  99. enum webview_dialog_type dlgtype, int flags,
  100. const char *title, const char *arg,
  101. char *result, size_t resultsz);
  102. WEBVIEW_API void webview_dispatch(struct webview *w, webview_dispatch_fn fn,
  103. void *arg);
  104. WEBVIEW_API void webview_terminate(struct webview *w);
  105. WEBVIEW_API void webview_exit(struct webview *w);
  106. WEBVIEW_API void webview_debug(const char *format, ...);
  107. WEBVIEW_API void webview_print_log(const char *s);
  108. #ifdef WEBVIEW_IMPLEMENTATION
  109. #undef WEBVIEW_IMPLEMENTATION
  110. WEBVIEW_API int webview(const char *title, const char *url, int width,
  111. int height, int resizable) {
  112. struct webview webview;
  113. memset(&webview, 0, sizeof(webview));
  114. webview.title = title;
  115. webview.url = url;
  116. webview.width = width;
  117. webview.height = height;
  118. webview.resizable = resizable;
  119. int r = webview_init(&webview);
  120. if (r != 0) {
  121. return r;
  122. }
  123. while (webview_loop(&webview, 1) == 0) {
  124. }
  125. webview_exit(&webview);
  126. return 0;
  127. }
  128. WEBVIEW_API void webview_debug(const char *format, ...) {
  129. char buf[4096];
  130. va_list ap;
  131. va_start(ap, format);
  132. vsnprintf(buf, sizeof(buf), format, ap);
  133. webview_print_log(buf);
  134. va_end(ap);
  135. }
  136. static int webview_js_encode(const char *s, char *esc, size_t n) {
  137. int r = 1; /* At least one byte for trailing zero */
  138. for (; *s; s++) {
  139. const unsigned char c = *s;
  140. if (c >= 0x20 && c < 0x80 && strchr("<>\\'\"", c) == NULL) {
  141. if (n > 0) {
  142. *esc++ = c;
  143. n--;
  144. }
  145. r++;
  146. } else {
  147. if (n > 0) {
  148. snprintf(esc, n, "\\x%02x", (int)c);
  149. esc += 4;
  150. n -= 4;
  151. }
  152. r += 4;
  153. }
  154. }
  155. return r;
  156. }
  157. WEBVIEW_API int webview_inject_css(struct webview *w, const char *css) {
  158. int n = webview_js_encode(css, NULL, 0);
  159. char *esc = (char *)calloc(1, sizeof(CSS_INJECT_FUNCTION) + n + 4);
  160. if (esc == NULL) {
  161. return -1;
  162. }
  163. char *js = (char *)calloc(1, n);
  164. webview_js_encode(css, js, n);
  165. snprintf(esc, sizeof(CSS_INJECT_FUNCTION) + n + 4, "%s(\"%s\")",
  166. CSS_INJECT_FUNCTION, js);
  167. int r = webview_eval(w, esc);
  168. free(js);
  169. free(esc);
  170. return r;
  171. }
  172. #endif /* WEBVIEW_IMPLEMENTATION */
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif /* WEBVIEW_H */