webview-sys 0.3.1

Rust native ffi bindings for webview
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
#include "webview.h"

#include <atomic>
#include <cstring>
#include <functional>
#include <future>
#include <map>
#include <string>
#include <vector>

#define WIN32_LEAN_AND_MEAN
#include <objbase.h>
#include <windows.h>
#include <wingdi.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Web.UI.Interop.h>

#pragma comment(lib, "windowsapp")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32")

namespace webview {
using dispatch_fn_t = std::function<void()>;
using msg_cb_t = std::function<void(const char* msg)>;

inline std::string url_encode(std::string s)
{
    std::string encoded;
    for (unsigned int i = 0; i < s.length(); i++) {
        auto c = s[i];
        if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
            encoded = encoded + c;
        } else {
            char hex[4];
            snprintf(hex, sizeof(hex), "%%%02x", c);
            encoded = encoded + hex;
        }
    }
    return encoded;
}

inline std::string url_decode(std::string s)
{
    std::string decoded;
    for (unsigned int i = 0; i < s.length(); i++) {
        if (s[i] == '%') {
            int n;
            sscanf(s.substr(i + 1, 2).c_str(), "%x", &n);
            decoded = decoded + static_cast<char>(n);
            i = i + 2;
        } else if (s[i] == '+') {
            decoded = decoded + ' ';
        } else {
            decoded = decoded + s[i];
        }
    }
    return decoded;
}

inline std::string html_from_uri(std::string s)
{
    if (s.substr(0, 15) == "data:text/html,") {
        return url_decode(s.substr(15));
    }
    return "";
}

inline int json_parse_c(const char* s, size_t sz, const char* key, size_t keysz,
    const char** value, size_t* valuesz)
{
    enum {
        JSON_STATE_VALUE,
        JSON_STATE_LITERAL,
        JSON_STATE_STRING,
        JSON_STATE_ESCAPE,
        JSON_STATE_UTF8
    } state
        = JSON_STATE_VALUE;
    const char* k = NULL;
    int index = 1;
    int depth = 0;
    int utf8_bytes = 0;

    if (key == NULL) {
        index = keysz;
        keysz = 0;
    }

    *value = NULL;
    *valuesz = 0;

    for (; sz > 0; s++, sz--) {
        enum {
            JSON_ACTION_NONE,
            JSON_ACTION_START,
            JSON_ACTION_END,
            JSON_ACTION_START_STRUCT,
            JSON_ACTION_END_STRUCT
        } action
            = JSON_ACTION_NONE;
        unsigned char c = *s;
        switch (state) {
        case JSON_STATE_VALUE:
            if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ',' || c == ':') {
                continue;
            } else if (c == '"') {
                action = JSON_ACTION_START;
                state = JSON_STATE_STRING;
            } else if (c == '{' || c == '[') {
                action = JSON_ACTION_START_STRUCT;
            } else if (c == '}' || c == ']') {
                action = JSON_ACTION_END_STRUCT;
            } else if (c == 't' || c == 'f' || c == 'n' || c == '-'
                || (c >= '0' && c <= '9')) {
                action = JSON_ACTION_START;
                state = JSON_STATE_LITERAL;
            } else {
                return -1;
            }
            break;
        case JSON_STATE_LITERAL:
            if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ',' || c == ']'
                || c == '}' || c == ':') {
                state = JSON_STATE_VALUE;
                s--;
                sz++;
                action = JSON_ACTION_END;
            } else if (c < 32 || c > 126) {
                return -1;
            } // fallthrough
        case JSON_STATE_STRING:
            if (c < 32 || (c > 126 && c < 192)) {
                return -1;
            } else if (c == '"') {
                action = JSON_ACTION_END;
                state = JSON_STATE_VALUE;
            } else if (c == '\\') {
                state = JSON_STATE_ESCAPE;
            } else if (c >= 192 && c < 224) {
                utf8_bytes = 1;
                state = JSON_STATE_UTF8;
            } else if (c >= 224 && c < 240) {
                utf8_bytes = 2;
                state = JSON_STATE_UTF8;
            } else if (c >= 240 && c < 247) {
                utf8_bytes = 3;
                state = JSON_STATE_UTF8;
            } else if (c >= 128 && c < 192) {
                return -1;
            }
            break;
        case JSON_STATE_ESCAPE:
            if (c == '"' || c == '\\' || c == '/' || c == 'b' || c == 'f' || c == 'n'
                || c == 'r' || c == 't' || c == 'u') {
                state = JSON_STATE_STRING;
            } else {
                return -1;
            }
            break;
        case JSON_STATE_UTF8:
            if (c < 128 || c > 191) {
                return -1;
            }
            utf8_bytes--;
            if (utf8_bytes == 0) {
                state = JSON_STATE_STRING;
            }
            break;
        default:
            return -1;
        }

        if (action == JSON_ACTION_END_STRUCT) {
            depth--;
        }

        if (depth == 1) {
            if (action == JSON_ACTION_START || action == JSON_ACTION_START_STRUCT) {
                if (index == 0) {
                    *value = s;
                } else if (keysz > 0 && index == 1) {
                    k = s;
                } else {
                    index--;
                }
            } else if (action == JSON_ACTION_END || action == JSON_ACTION_END_STRUCT) {
                if (*value != NULL && index == 0) {
                    *valuesz = (size_t)(s + 1 - *value);
                    return 0;
                } else if (keysz > 0 && k != NULL) {
                    if (keysz == (size_t)(s - k - 1) && memcmp(key, k + 1, keysz) == 0) {
                        index = 0;
                    } else {
                        index = 2;
                    }
                    k = NULL;
                }
            }
        }

        if (action == JSON_ACTION_START_STRUCT) {
            depth++;
        }
    }
    return -1;
}

inline std::string json_escape(std::string s)
{
    // TODO: implement
    return '"' + s + '"';
}

inline int json_unescape(const char* s, size_t n, char* out)
{
    int r = 0;
    if (*s++ != '"') {
        return -1;
    }
    while (n > 2) {
        char c = *s;
        if (c == '\\') {
            s++;
            n--;
            switch (*s) {
            case 'b':
                c = '\b';
                break;
            case 'f':
                c = '\f';
                break;
            case 'n':
                c = '\n';
                break;
            case 'r':
                c = '\r';
                break;
            case 't':
                c = '\t';
                break;
            case '\\':
                c = '\\';
                break;
            case '/':
                c = '/';
                break;
            case '\"':
                c = '\"';
                break;
            default: // TODO: support unicode decoding
                return -1;
            }
        }
        if (out != NULL) {
            *out++ = c;
        }
        s++;
        n--;
        r++;
    }
    if (*s != '"') {
        return -1;
    }
    if (out != NULL) {
        *out = '\0';
    }
    return r;
}

inline std::string json_parse(std::string s, std::string key, int index)
{
    const char* value;
    size_t value_sz;
    if (key == "") {
        json_parse_c(s.c_str(), s.length(), nullptr, index, &value, &value_sz);
    } else {
        json_parse_c(s.c_str(), s.length(), key.c_str(), key.length(), &value, &value_sz);
    }
    if (value != nullptr) {
        if (value[0] != '"') {
            return std::string(value, value_sz);
        }
        int n = json_unescape(value, value_sz, nullptr);
        if (n > 0) {
            char* decoded = new char[n];
            json_unescape(value, value_sz, decoded);
            auto result = std::string(decoded, n);
            delete[] decoded;
            return result;
        }
    }
    return "";
}

LRESULT CALLBACK WebviewWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
class browser_window {
public:
    browser_window(msg_cb_t cb, const char* title, int width, int height, bool resizable)
        : m_cb(cb)
    {
        HINSTANCE hInstance = GetModuleHandle(nullptr);

        WNDCLASSEX wc;
        ZeroMemory(&wc, sizeof(WNDCLASSEX));
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.hInstance = hInstance;
        wc.lpfnWndProc = WebviewWndProc;
        wc.lpszClassName = "webview";
        RegisterClassEx(&wc);

        DWORD style = WS_OVERLAPPEDWINDOW;
        if (!resizable) {
            style = WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
        }

        RECT clientRect;
        RECT rect;
        rect.left = 0;
        rect.top = 0;
        rect.right = width;
        rect.bottom = height;
        AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 0);

        GetClientRect(GetDesktopWindow(), &clientRect);
        int left = (clientRect.right / 2) - ((rect.right - rect.left) / 2);
        int top = (clientRect.bottom / 2) - ((rect.bottom - rect.top) / 2);
        rect.right = rect.right - rect.left + left;
        rect.left = left;
        rect.bottom = rect.bottom - rect.top + top;
        rect.top = top;

        m_window = CreateWindowEx(0, "webview", title, style, rect.left, rect.top,
                     rect.right - rect.left, rect.bottom - rect.top,
                     HWND_DESKTOP, NULL, hInstance, (void *)this);

        SetWindowLongPtr(m_window, GWLP_USERDATA, (LONG_PTR)this);

        ShowWindow(m_window, SW_SHOW);
        UpdateWindow(m_window);
        SetFocus(m_window);
    }

    void run()
    {
        while (this->loop(true) == 0) {

        }
    }

    int loop(int blocking)
    {
        MSG msg;

        if (blocking) {
            if (GetMessage(&msg, nullptr, 0, 0) < 0) return 0;
        } else {
            if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) == 0) return 0;
        }

        if (msg.hwnd) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            return 0;
        }
        if (msg.message == WM_APP) {
            auto f = (dispatch_fn_t*)(msg.lParam);
            (*f)();
            delete f;
        } else if (msg.message == WM_QUIT) {
            return -1;
        }

        return 0;
    }

    void terminate() { PostQuitMessage(0); }
    void dispatch(dispatch_fn_t f)
    {
        PostThreadMessage(m_main_thread, WM_APP, 0, (LPARAM) new dispatch_fn_t(f));
    }

    void set_title(const char* title) { SetWindowText(m_window, title); }

    void set_size(int width, int height)
    {
        RECT r;
        r.left = 50;
        r.top = 50;
        r.right = width;
        r.bottom = height;
        AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, 0);
        SetWindowPos(m_window, NULL, r.left, r.top, r.right - r.left, r.bottom - r.top,
            SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
    }

    void set_fullscreen(bool fullscreen)
    {
        if (this->is_fullscreen == fullscreen) {
            return;
        }
        if (!this->is_fullscreen) {
            this->saved_style = GetWindowLong(this->m_window, GWL_STYLE);
            this->saved_ex_style = GetWindowLong(this->m_window, GWL_EXSTYLE);
            GetWindowRect(this->m_window, &this->saved_rect);
        }
        this->is_fullscreen = !!fullscreen;
        if (fullscreen) {
            MONITORINFO monitor_info;
            SetWindowLong(this->m_window, GWL_STYLE,
                        this->saved_style & ~(WS_CAPTION | WS_THICKFRAME));
            SetWindowLong(this->m_window, GWL_EXSTYLE,
                        this->saved_ex_style &
                            ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE |
                                WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
            monitor_info.cbSize = sizeof(monitor_info);
            GetMonitorInfo(MonitorFromWindow(this->m_window, MONITOR_DEFAULTTONEAREST),
                        &monitor_info);
            RECT r;
            r.left = monitor_info.rcMonitor.left;
            r.top = monitor_info.rcMonitor.top;
            r.right = monitor_info.rcMonitor.right;
            r.bottom = monitor_info.rcMonitor.bottom;
            SetWindowPos(this->m_window, NULL, r.left, r.top, r.right - r.left,
                        r.bottom - r.top,
                        SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
        } else {
            SetWindowLong(this->m_window, GWL_STYLE, this->saved_style);
            SetWindowLong(this->m_window, GWL_EXSTYLE, this->saved_ex_style);
            SetWindowPos(this->m_window, NULL, this->saved_rect.left,
                        this->saved_rect.top,
                        this->saved_rect.right - this->saved_rect.left,
                        this->saved_rect.bottom - this->saved_rect.top,
                        SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
        }
    }

    void set_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
    {
        HBRUSH brush = CreateSolidBrush(RGB(r, g, b));
        SetClassLongPtr(this->m_window, GCLP_HBRBACKGROUND, (LONG_PTR)brush);
    }

    // protected:
    virtual void resize() {}
    HWND m_window;
    DWORD m_main_thread = GetCurrentThreadId();
    msg_cb_t m_cb;

    bool is_fullscreen = false;
    DWORD saved_style = 0;
    DWORD saved_ex_style = 0;
    RECT saved_rect;
};

LRESULT CALLBACK WebviewWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    auto w = (browser_window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    switch (msg) {
    case WM_SIZE:
        w->resize();
        break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        w->terminate();
        break;
    default:
        return DefWindowProc(hwnd, msg, wp, lp);
    }
    return 0;
}

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Web::UI;
using namespace Windows::Web::UI::Interop;

class webview : public browser_window {
public:
    webview(webview_external_invoke_cb_t invoke_cb, const char* title, int width, int height, bool resizable, bool debug)
        : browser_window(std::bind(&webview::on_message, this, std::placeholders::_1), title, width, height, resizable)
        , invoke_cb(invoke_cb)
    {
        init_apartment(winrt::apartment_type::single_threaded);
        WebViewControlProcessOptions options;
        options.PrivateNetworkClientServerCapability(WebViewControlProcessCapabilityState::Enabled);
        m_process = WebViewControlProcess(options);
        auto op = m_process.CreateWebViewControlAsync(
            reinterpret_cast<int64_t>(m_window), Rect());
        if (op.Status() != AsyncStatus::Completed) {
            handle h(CreateEvent(nullptr, false, false, nullptr));
            op.Completed([h = h.get()](auto, auto) { SetEvent(h); });
            HANDLE hs[] = { h.get() };
            DWORD i;
            CoWaitForMultipleHandles(COWAIT_DISPATCH_WINDOW_MESSAGES
                    | COWAIT_DISPATCH_CALLS | COWAIT_INPUTAVAILABLE,
                INFINITE, 1, hs, &i);
        }
        m_webview = op.GetResults();
        m_webview.Settings().IsScriptNotifyAllowed(true);
        m_webview.IsVisible(true);
        m_webview.ScriptNotify([=](auto const& sender, auto const& args) {
            std::string s = winrt::to_string(args.Value());
            m_cb(s.c_str());
        });
        m_webview.NavigationStarting([=](auto const& sender, auto const& args) {
            m_webview.AddInitializeScript(winrt::to_hstring(init_js));
        });
        init("window.external.invoke = s => window.external.notify(s)");
        resize();
    }

    void navigate(const char* url)
    {
        std::string html = html_from_uri(url);
        if (html != "") {
            m_webview.NavigateToString(winrt::to_hstring(html.c_str()));
        } else {
            Uri uri(winrt::to_hstring(url));
            m_webview.Navigate(uri);
        }
    }
    void init(const char* js) { init_js = init_js + "(function(){" + js + "})();"; }
    void eval(const char* js)
    {
        m_webview.InvokeScriptAsync(
            L"eval", single_threaded_vector<hstring>({ winrt::to_hstring(js) }));
    }

    void* window() { return (void*)m_window; }

    void* get_user_data() { return this->user_data; }
    void set_user_data(void* user_data) { this->user_data = user_data; }
private:
    void on_message(const char* msg)
    {
        this->invoke_cb(this, msg);
    }

    void resize()
    {
        RECT r;
        GetClientRect(m_window, &r);
        Rect bounds(r.left, r.top, r.right - r.left, r.bottom - r.top);
        m_webview.Bounds(bounds);
    }
    WebViewControlProcess m_process;
    WebViewControl m_webview = nullptr;
    std::string init_js = "";

    void* user_data = nullptr;
    webview_external_invoke_cb_t invoke_cb;
};

} // namespace webview

static inline char *webview_from_utf16(WCHAR *ws) {
  int n = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
  char *s = (char *)GlobalAlloc(GMEM_FIXED, n);
  if (s == NULL) {
    return NULL;
  }
  WideCharToMultiByte(CP_UTF8, 0, ws, -1, s, n, NULL, NULL);
  return s;
}

static int webview_js_encode(const char *s, char *esc, size_t n) {
  int r = 1; /* At least one byte for trailing zero */
  for (; *s; s++) {
    const unsigned char c = *s;
    if (c >= 0x20 && c < 0x80 && strchr("<>\\'\"", c) == NULL) {
      if (n > 0) {
        *esc++ = c;
        n--;
      }
      r++;
    } else {
      if (n > 0) {
        snprintf(esc, n, "\\x%02x", (int)c);
        esc += 4;
        n -= 4;
      }
      r += 4;
    }
  }
  return r;
}

#include <shobjidl.h>

#ifdef __cplusplus
#define iid_ref(x) &(x)
#define iid_unref(x) *(x)
#else
#define iid_ref(x) (x)
#define iid_unref(x) (x)
#endif

/* These are missing parts from MinGW */
#ifndef __IFileDialog_INTERFACE_DEFINED__
#define __IFileDialog_INTERFACE_DEFINED__
enum _FILEOPENDIALOGOPTIONS {
  FOS_OVERWRITEPROMPT = 0x2,
  FOS_STRICTFILETYPES = 0x4,
  FOS_NOCHANGEDIR = 0x8,
  FOS_PICKFOLDERS = 0x20,
  FOS_FORCEFILESYSTEM = 0x40,
  FOS_ALLNONSTORAGEITEMS = 0x80,
  FOS_NOVALIDATE = 0x100,
  FOS_ALLOWMULTISELECT = 0x200,
  FOS_PATHMUSTEXIST = 0x800,
  FOS_FILEMUSTEXIST = 0x1000,
  FOS_CREATEPROMPT = 0x2000,
  FOS_SHAREAWARE = 0x4000,
  FOS_NOREADONLYRETURN = 0x8000,
  FOS_NOTESTFILECREATE = 0x10000,
  FOS_HIDEMRUPLACES = 0x20000,
  FOS_HIDEPINNEDPLACES = 0x40000,
  FOS_NODEREFERENCELINKS = 0x100000,
  FOS_DONTADDTORECENT = 0x2000000,
  FOS_FORCESHOWHIDDEN = 0x10000000,
  FOS_DEFAULTNOMINIMODE = 0x20000000,
  FOS_FORCEPREVIEWPANEON = 0x40000000
};
typedef DWORD FILEOPENDIALOGOPTIONS;
typedef enum FDAP { FDAP_BOTTOM = 0, FDAP_TOP = 1 } FDAP;
DEFINE_GUID(IID_IFileDialog, 0x42f85136, 0xdb7e, 0x439c, 0x85, 0xf1, 0xe4, 0x07,
            0x5d, 0x13, 0x5f, 0xc8);
typedef struct IFileDialogVtbl {
  BEGIN_INTERFACE
  HRESULT(STDMETHODCALLTYPE *QueryInterface)
  (IFileDialog *This, REFIID riid, void **ppvObject);
  ULONG(STDMETHODCALLTYPE *AddRef)(IFileDialog *This);
  ULONG(STDMETHODCALLTYPE *Release)(IFileDialog *This);
  HRESULT(STDMETHODCALLTYPE *Show)(IFileDialog *This, HWND hwndOwner);
  HRESULT(STDMETHODCALLTYPE *SetFileTypes)
  (IFileDialog *This, UINT cFileTypes, const COMDLG_FILTERSPEC *rgFilterSpec);
  HRESULT(STDMETHODCALLTYPE *SetFileTypeIndex)
  (IFileDialog *This, UINT iFileType);
  HRESULT(STDMETHODCALLTYPE *GetFileTypeIndex)
  (IFileDialog *This, UINT *piFileType);
  HRESULT(STDMETHODCALLTYPE *Advise)
  (IFileDialog *This, IFileDialogEvents *pfde, DWORD *pdwCookie);
  HRESULT(STDMETHODCALLTYPE *Unadvise)(IFileDialog *This, DWORD dwCookie);
  HRESULT(STDMETHODCALLTYPE *SetOptions)
  (IFileDialog *This, FILEOPENDIALOGOPTIONS fos);
  HRESULT(STDMETHODCALLTYPE *GetOptions)
  (IFileDialog *This, FILEOPENDIALOGOPTIONS *pfos);
  HRESULT(STDMETHODCALLTYPE *SetDefaultFolder)
  (IFileDialog *This, IShellItem *psi);
  HRESULT(STDMETHODCALLTYPE *SetFolder)(IFileDialog *This, IShellItem *psi);
  HRESULT(STDMETHODCALLTYPE *GetFolder)(IFileDialog *This, IShellItem **ppsi);
  HRESULT(STDMETHODCALLTYPE *GetCurrentSelection)
  (IFileDialog *This, IShellItem **ppsi);
  HRESULT(STDMETHODCALLTYPE *SetFileName)(IFileDialog *This, LPCWSTR pszName);
  HRESULT(STDMETHODCALLTYPE *GetFileName)(IFileDialog *This, LPWSTR *pszName);
  HRESULT(STDMETHODCALLTYPE *SetTitle)(IFileDialog *This, LPCWSTR pszTitle);
  HRESULT(STDMETHODCALLTYPE *SetOkButtonLabel)
  (IFileDialog *This, LPCWSTR pszText);
  HRESULT(STDMETHODCALLTYPE *SetFileNameLabel)
  (IFileDialog *This, LPCWSTR pszLabel);
  HRESULT(STDMETHODCALLTYPE *GetResult)(IFileDialog *This, IShellItem **ppsi);
  HRESULT(STDMETHODCALLTYPE *AddPlace)
  (IFileDialog *This, IShellItem *psi, FDAP fdap);
  HRESULT(STDMETHODCALLTYPE *SetDefaultExtension)
  (IFileDialog *This, LPCWSTR pszDefaultExtension);
  HRESULT(STDMETHODCALLTYPE *Close)(IFileDialog *This, HRESULT hr);
  HRESULT(STDMETHODCALLTYPE *SetClientGuid)(IFileDialog *This, REFGUID guid);
  HRESULT(STDMETHODCALLTYPE *ClearClientData)(IFileDialog *This);
  HRESULT(STDMETHODCALLTYPE *SetFilter)
  (IFileDialog *This, IShellItemFilter *pFilter);
  END_INTERFACE
} IFileDialogVtbl;
interface IFileDialog {
  CONST_VTBL IFileDialogVtbl *lpVtbl;
};
DEFINE_GUID(IID_IFileOpenDialog, 0xd57c7288, 0xd4ad, 0x4768, 0xbe, 0x02, 0x9d,
            0x96, 0x95, 0x32, 0xd9, 0x60);
DEFINE_GUID(IID_IFileSaveDialog, 0x84bccd23, 0x5fde, 0x4cdb, 0xae, 0xa4, 0xaf,
            0x64, 0xb8, 0x3d, 0x78, 0xab);
#endif


// HERE

WEBVIEW_API void webview_run(webview_t w)
{
    static_cast<webview::webview*>(w)->run();
}

WEBVIEW_API int webview_loop(webview_t w, int blocking)
{
    return static_cast<webview::webview*>(w)->loop(blocking);
}

WEBVIEW_API int webview_eval(webview_t w, const char *js)
{
    static_cast<webview::webview*>(w)->eval(js);
    return 0;
}

WEBVIEW_API int webview_inject_css(webview_t w, const char *css) {
  int n = webview_js_encode(css, NULL, 0);
  char *esc = (char *)calloc(1, sizeof(CSS_INJECT_FUNCTION) + n + 4);
  if (esc == NULL) {
    return -1;
  }
  char *js = (char *)calloc(1, n);
  webview_js_encode(css, js, n);
  snprintf(esc, sizeof(CSS_INJECT_FUNCTION) + n + 4, "%s(\"%s\")",
           CSS_INJECT_FUNCTION, js);
  int r = webview_eval(w, esc);
  free(js);
  free(esc);
  return r;
}

WEBVIEW_API void webview_set_title(webview_t w, const char *title)
{
    static_cast<webview::webview*>(w)->set_title(title);
}

WEBVIEW_API void webview_set_fullscreen(webview_t w, int fullscreen)
{
    static_cast<webview::webview*>(w)->set_fullscreen(fullscreen);
}

WEBVIEW_API void webview_set_color(webview_t w, uint8_t r, uint8_t g,
                                   uint8_t b, uint8_t a)
{
    static_cast<webview::webview*>(w)->set_color(r, g, b, a);
}

WEBVIEW_API void webview_dialog(webview_t w,
                    enum webview_dialog_type dlgtype, int flags,
                    const char *title, const char *arg,
                    char *result, size_t resultsz) {
  HWND hwnd = static_cast<webview::webview*>(w)->m_window;
  if (dlgtype == WEBVIEW_DIALOG_TYPE_OPEN ||
      dlgtype == WEBVIEW_DIALOG_TYPE_SAVE) {
    IFileDialog *dlg = NULL;
    IShellItem *res = NULL;
    WCHAR *ws = NULL;
    char *s = NULL;
    FILEOPENDIALOGOPTIONS opts = 0, add_opts = 0;
    if (dlgtype == WEBVIEW_DIALOG_TYPE_OPEN) {
      if (CoCreateInstance(
              iid_unref(&CLSID_FileOpenDialog), NULL, CLSCTX_INPROC_SERVER,
              iid_unref(&IID_IFileOpenDialog), (void **)&dlg) != S_OK) {
        goto error_dlg;
      }
      if (flags & WEBVIEW_DIALOG_FLAG_DIRECTORY) {
        add_opts |= FOS_PICKFOLDERS;
      }
      add_opts |= FOS_NOCHANGEDIR | FOS_ALLNONSTORAGEITEMS | FOS_NOVALIDATE |
                  FOS_PATHMUSTEXIST | FOS_FILEMUSTEXIST | FOS_SHAREAWARE |
                  FOS_NOTESTFILECREATE | FOS_NODEREFERENCELINKS |
                  FOS_FORCESHOWHIDDEN | FOS_DEFAULTNOMINIMODE;
    } else {
      if (CoCreateInstance(
              iid_unref(&CLSID_FileSaveDialog), NULL, CLSCTX_INPROC_SERVER,
              iid_unref(&IID_IFileSaveDialog), (void **)&dlg) != S_OK) {
        goto error_dlg;
      }
      add_opts |= FOS_OVERWRITEPROMPT | FOS_NOCHANGEDIR |
                  FOS_ALLNONSTORAGEITEMS | FOS_NOVALIDATE | FOS_SHAREAWARE |
                  FOS_NOTESTFILECREATE | FOS_NODEREFERENCELINKS |
                  FOS_FORCESHOWHIDDEN | FOS_DEFAULTNOMINIMODE;
    }
    if (dlg->GetOptions(&opts) != S_OK) {
      goto error_dlg;
    }
    opts &= ~FOS_NOREADONLYRETURN;
    opts |= add_opts;
    if (dlg->SetOptions(opts) != S_OK) {
      goto error_dlg;
    }
    if (dlg->Show(hwnd) != S_OK) {
      goto error_dlg;
    }
    if (dlg->GetResult(&res) != S_OK) {
      goto error_dlg;
    }
    if (res->GetDisplayName(SIGDN_FILESYSPATH, &ws) != S_OK) {
      goto error_result;
    }
    s = webview_from_utf16(ws);
    CoTaskMemFree(ws);
    if (!s) goto error_result;
    strncpy(result, s, resultsz);
    GlobalFree(s);
    result[resultsz - 1] = '\0';
  error_result:
    res->Release();
  error_dlg:
    dlg->Release();
    return;
  } else if (dlgtype == WEBVIEW_DIALOG_TYPE_ALERT) {
#if 0
    /* MinGW often doesn't contain TaskDialog, we'll use MessageBox for now */
    WCHAR *wtitle = webview_to_utf16(title);
    WCHAR *warg = webview_to_utf16(arg);
    TaskDialog(hwnd, NULL, NULL, wtitle, warg, 0, NULL, NULL);
    GlobalFree(warg);
    GlobalFree(wtitle);
#else
    UINT type = MB_OK;
    switch (flags & WEBVIEW_DIALOG_FLAG_ALERT_MASK) {
    case WEBVIEW_DIALOG_FLAG_INFO:
      type |= MB_ICONINFORMATION;
      break;
    case WEBVIEW_DIALOG_FLAG_WARNING:
      type |= MB_ICONWARNING;
      break;
    case WEBVIEW_DIALOG_FLAG_ERROR:
      type |= MB_ICONERROR;
      break;
    }
    MessageBox(hwnd, arg, title, type);
#endif
  }
}

WEBVIEW_API void webview_dispatch(webview_t w, webview_dispatch_fn fn,
                                  void *arg)
{
    static_cast<webview::webview*>(w)->dispatch([=]() { fn(w, arg); });
}

WEBVIEW_API void webview_terminate(webview_t w)
{
    static_cast<webview::webview*>(w)->terminate();
}

WEBVIEW_API void webview_exit(webview_t w)
{
    webview_terminate(w);
}

WEBVIEW_API void webview_debug(const char *format, ...)
{
    // TODO
}

WEBVIEW_API void webview_print_log(const char *s)
{
    // TODO
}

WEBVIEW_API void* webview_get_user_data(webview_t w)
{
    return static_cast<webview::webview*>(w)->get_user_data();
}

WEBVIEW_API webview_t webview_new(const char* title, const char* url, int width, int height, int resizable, int debug, webview_external_invoke_cb_t external_invoke_cb, void* userdata)
{
    auto w = new webview::webview(external_invoke_cb, title, width, height, resizable, debug);
    w->set_user_data(userdata);
    w->navigate(url);
	return w;
}

WEBVIEW_API void webview_free(webview_t w)
{
    delete static_cast<webview::webview*>(w);
}

WEBVIEW_API void webview_destroy(webview_t w)
{
    delete static_cast<webview::webview*>(w);
}