-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_window.cpp
More file actions
224 lines (195 loc) · 4.81 KB
/
Copy pathhtml_window.cpp
File metadata and controls
224 lines (195 loc) · 4.81 KB
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
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <cassert>
#include <mutex>
#include <atomic>
#include <cef_app.h>
#include <cef_client.h>
#include <cef_parser.h>
#include <cef_render_handler.h>
#include <cef_life_span_handler.h>
#include <cef_load_handler.h>
#include <wrapper/cef_helpers.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Image.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/fl_draw.H>
#include "html_window.h"
std::string GetDataURI(const std::string& data, const std::string& mime_type)
{
return "data:" + mime_type + ";base64," + CefURIEncode(CefBase64Encode(data.data(), data.size()), false).ToString();
}
void html_win_shutdown(Fl_Widget *w, void *v)
{
HTML_Win *html = (HTML_Win *)v;
delete html;
}
HTML_Win::HTML_Win(char *in_url, char *in_html, int transparent_background, char *in_extra_css, char *in_extra_js_once, char *in_extra_js_always, int ww, int hh)
{
width = ww;
height = hh;
renderHandler = nullptr;
browserClient = nullptr;
browser = nullptr;
raw = NULL;
load(in_url, in_html, transparent_background, in_extra_css, in_extra_js_once, in_extra_js_always);
}
HTML_Win::~HTML_Win()
{
shutdown();
}
void HTML_Win::resize(int ww, int hh)
{
renderHandler->resize(ww, hh);
browser->GetHost()->WasResized();
ww = renderHandler->m_width;
hh = renderHandler->m_height;
width = ww;
height = hh;
}
void bgr2rgb(unsigned char *ptr, unsigned char *out, int ww, int hh)
{
int xx, yy;
unsigned char *p1 = ptr;
unsigned char *p2 = out;
for(xx = 0;xx < ww;xx++)
{
for(yy = 0;yy < hh;yy++)
{
unsigned char b = *p1;
unsigned char g = *(p1 + 1);
unsigned char r = *(p1 + 2);
*p2 = r;
*(p2 + 1) = g;
*(p2 + 2) = b;
p1 += 4;
p2 += 3;
}
}
}
void HTML_Win::Draw()
{
if(renderHandler != nullptr)
{
if(renderHandler->raw != NULL)
{
int ww = renderHandler->use_w;
int hh = renderHandler->use_h;
raw = renderHandler->raw;
}
}
}
void my_html_window_cb(void *v)
{
HTML_Win *win = (HTML_Win *)v;
CefDoMessageLoopWork();
if(win->renderHandler->redraw_flag == 1)
{
win->Draw();
win->renderHandler->redraw_flag = 0;
}
Fl::repeat_timeout(0.03, my_html_window_cb, win);
}
extern "C" {
int initialize_cef(int html, int argc, char *argv[])
{
CefMainArgs args(argc, argv);
int result = CefExecuteProcess(args, nullptr, nullptr);
if(result >= 0)
{
exit(result);
}
if(html == 1)
{
CefSettings settings;
settings.windowless_rendering_enabled = true;
if(!CefInitialize(args, settings, nullptr, nullptr))
{
return EXIT_FAILURE;
}
}
return(0);
}
}
int HTML_Win::load(char *url, char *html_source, int transparent_background, char *in_extra_css, char *in_extra_js_once, char *in_extra_js_always)
{
renderHandler = new RenderHandler(width, height);
assert(renderHandler != nullptr);
browserClient = new BrowserClient(renderHandler);
assert(browserClient != nullptr);
browserClient->transparent_background = transparent_background;
browserClient->extra_css = in_extra_css;
browserClient->extra_js_once = in_extra_js_once;
browserClient->extra_js_always = in_extra_js_always;
CefBrowserSettings browserSettings;
browserSettings.windowless_frame_rate = 60;
CefWindowInfo window_info;
window_info.SetAsWindowless(true);
if(url != NULL)
{
browser = CefBrowserHost::CreateBrowserSync(window_info, browserClient.get(), url, browserSettings, nullptr, nullptr);
}
else
{
browser = CefBrowserHost::CreateBrowserSync(window_info, browserClient.get(), "about:blank", browserSettings, nullptr, nullptr);
std::string cow = GetDataURI(html_source, "text/html");
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
frame->LoadURL(cow);
}
assert(browser != nullptr);
Fl::add_timeout(0.03, my_html_window_cb, this);
return(0);
}
int HTML_Win::shutdown()
{
Fl::remove_timeout(my_html_window_cb, this);
browser->GetHost()->CloseBrowser(false);
while(!browserClient->closeAllowed())
{
CefDoMessageLoopWork();
usleep(10000);
}
browser = nullptr;
browserClient = nullptr;
renderHandler = nullptr;
return(0);
}
extern "C" {
void shutdown_cef()
{
CefShutdown();
}
}
extern "C" {
void delete_html(HTML_Win *html)
{
delete html;
}
}
extern "C" {
HTML_Win *MakeHTMLWindow(char *in_url, char *in_html, int transparent_background, char *in_extra_css, char *in_extra_js_once, char *in_extra_js_always, int ww, int hh)
{
HTML_Win *win = new HTML_Win(in_url, in_html, transparent_background, in_extra_css, in_extra_js_once, in_extra_js_always, ww, hh);
return(win);
}
}
extern "C" {
void get_image_cef(HTML_Win *win)
{
CefDoMessageLoopWork();
if(win->renderHandler->redraw_flag == 1)
{
win->Draw();
win->renderHandler->redraw_flag = 0;
}
}
}