This repository was archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebcore.cpp
417 lines (354 loc) · 12.5 KB
/
webcore.cpp
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
#include "webcore.h"
#include <osgGA/GUIEventAdapter>
#include "cefclient/client_handler.h"
#include "include/cef_render_handler.h"
#include "include/cef_runnable.h"
#include "include/cef_browser.h"
namespace app {
static const int max_string = 100;
static TCHAR szOSRWindowClass[max_string] = "CEFClientWindowClass";
struct WebCoreImpl
{
WebCoreImpl()
: hinst(0L), main_wnd(0L)
{
client_handler = new ClientHandler();
}
CefRefPtr<ClientHandler> client_handler;
HINSTANCE hinst;
HWND main_wnd;
};
// ----------------------------- WebView
struct WebViewImpl : public CefRenderHandler
{
WebViewImpl(WebCore* webcore_)
: webcore(webcore_->_i), width(640), height(480)
{
pbo = new osg::PixelBufferObject;
image = new osg::Image();
if (!image->getPixelBufferObject())
{
pbo->setCopyDataAndReleaseGLBufferObject(true);
pbo->setUsage(GL_DYNAMIC_DRAW_ARB);
image->setPixelBufferObject(pbo.get());
}
}
void resize(int w, int h)
{
width = w;
height = h;
CefRefPtr<CefBrowserHost> browser = get_browser_host();
if(browser.get() != 0L) {
browser->WasResized();
}
}
bool send_key_event(int key, bool key_down)
{
CefRefPtr<CefBrowserHost> browser = get_browser_host();
if(browser.get() == 0L) {
return false;
}
CefKeyEvent event;
//event.windows_key_code = wParam;
event.native_key_code = key;
event.is_system_key = false;
if (key_down)
event.type = KEYEVENT_RAWKEYDOWN;
else
event.type = KEYEVENT_KEYUP;
if (browser.get()) {
browser->SendKeyEvent(event);
return true;
}
else {
return false;
}
}
bool send_pointer_event( int x, int y, int button_mask )
{
CefRefPtr<CefBrowserHost> browser = get_browser_host();
if(browser.get() == 0L) {
return false;
}
switch ( button_mask )
{
case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
if ( !last_button_mask ) {
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
//mouse_event.modifiers = GetCefMouseModifiers(wParam);
CefBrowserHost::MouseButtonType btnType = MBT_LEFT;
int last_click_count = 1;
// Todo: try correct.
browser->SendMouseClickEvent(mouse_event, btnType, false, last_click_count);
}
else {
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
// mouse_event.modifiers = GetCefMouseModifiers(wParam);
browser->SendMouseMoveEvent(mouse_event, false);
}
break;
case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
if ( !last_button_mask ) {
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
//mouse_event.modifiers = GetCefMouseModifiers(wParam);
CefBrowserHost::MouseButtonType btnType = MBT_MIDDLE;
int last_click_count = 1;
// Todo: try correct.
browser->SendMouseClickEvent(mouse_event, btnType, false, last_click_count);
}
else {
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
// mouse_event.modifiers = GetCefMouseModifiers(wParam);
browser->SendMouseMoveEvent(mouse_event, false);
}
break;
case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
if ( !last_button_mask ) {
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
//mouse_event.modifiers = GetCefMouseModifiers(wParam);
CefBrowserHost::MouseButtonType btnType = MBT_RIGHT;
int last_click_count = 1;
// Todo: try correct.
browser->SendMouseClickEvent(mouse_event, btnType, false, last_click_count);
}
else {
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
// mouse_event.modifiers = GetCefMouseModifiers(wParam);
browser->SendMouseMoveEvent(mouse_event, false);
}
break;
default:
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
// mouse_event.modifiers = GetCefMouseModifiers(wParam);
browser->SendMouseMoveEvent(mouse_event, false);
break;
}
if ( last_button_mask!=0 && button_mask==0 )
{
switch ( last_button_mask )
{
case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
{
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
//mouse_event.modifiers = GetCefMouseModifiers(wParam);
CefBrowserHost::MouseButtonType btnType = MBT_LEFT;
int last_click_count = 1;
browser->SendMouseClickEvent(mouse_event, btnType, true, last_click_count);
}
break;
case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
{
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
//mouse_event.modifiers = GetCefMouseModifiers(wParam);
CefBrowserHost::MouseButtonType btnType = MBT_MIDDLE;
int last_click_count = 1;
// Todo: try correct.
browser->SendMouseClickEvent(mouse_event, btnType, true, last_click_count);
}
break;
case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
{
CefMouseEvent mouse_event;
mouse_event.x = x;
mouse_event.y = y;
//mouse_event.modifiers = GetCefMouseModifiers(wParam);
CefBrowserHost::MouseButtonType btnType = MBT_RIGHT;
int last_click_count = 1;
// Todo: try correct.
browser->SendMouseClickEvent(mouse_event, btnType, true, last_click_count);
}
break;
}
}
last_button_mask = button_mask;
return true;
}
// CefRenderHandler methods
virtual bool GetRootScreenRect(CefRefPtr<CefBrowser> browser,
CefRect& rect)
{
RECT window_rect = {0};
HWND root_window = ::GetDesktopWindow();
if (::GetWindowRect(root_window, &window_rect)) {
rect = CefRect(window_rect.left,
window_rect.top,
window_rect.right - window_rect.left,
window_rect.bottom - window_rect.top);
return true;
}
return false;
}
virtual bool GetViewRect(CefRefPtr<CefBrowser> browser,
CefRect& rect)
{
rect.x = rect.y = 0;
rect.width = width;
rect.height = height;
return true;
}
virtual bool GetScreenPoint(CefRefPtr<CefBrowser> browser,
int viewX,
int viewY,
int& screenX,
int& screenY)
{
// Convert the point from view coordinates to actual screen coordinates.
POINT screen_pt = {viewX, viewY};
screenX = screen_pt.x;
screenY = screen_pt.y;
return true;
}
virtual bool GetScreenInfo(CefRefPtr<CefBrowser> browser,
CefScreenInfo& screen_info)
{
screen_info.depth = 32;
screen_info.depth_per_component = 8;
return true;
}
virtual void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show)
{
}
virtual void OnPopupSize(CefRefPtr<CefBrowser> browser,
const CefRect& rect)
{
if (rect.width <= 0 || rect.height <= 0)
return;
}
virtual void OnPaint(CefRefPtr<CefBrowser> browser,
PaintElementType type,
const RectList& dirtyRects,
const void* buffer,
int width,
int height)
{
if(type == PET_VIEW) {
image->setImage( width, height, 1, 4, GL_BGRA, GL_UNSIGNED_BYTE,
(unsigned char*)(buffer), osg::Image::NO_DELETE );
}
}
virtual void OnCursorChange(CefRefPtr<CefBrowser> browser,
CefCursorHandle cursor)
{
}
///
// Called when the scroll offset has changed.
///
/*--cef()--*/
virtual void OnScrollOffsetChanged(CefRefPtr<CefBrowser> browser)
{
}
CefRefPtr<CefBrowserHost> get_browser_host()
{
CefRefPtr<CefBrowserHost> browser;
CefRefPtr<CefBrowser> browser_ = webcore->client_handler->GetBrowser();
if(browser_) {
browser = browser_->GetHost();
return browser;
}
else {
return 0L;
}
}
WebCoreImpl* webcore;
osg::ref_ptr<osg::Image> image;
osg::ref_ptr<osg::PixelBufferObject> pbo;
int width, height;
bool render_task_pending;
int last_button_mask;
IMPLEMENT_REFCOUNTING(WebViewImpl);
};
WebView::WebView(WebCore* webcore, int w, int h)
: _i(new WebViewImpl(webcore))
{
}
void WebView::load_url(const char* url)
{
}
void WebView::resize(int w, int h)
{
_i->resize(w,h);
}
bool WebView::send_pointer_event( int x, int y, int button_mask )
{
return _i->send_pointer_event(x,y,button_mask);
}
bool WebView::send_key_event(int key, bool key_down)
{
return _i->send_key_event(key, key_down);
return true;
}
int WebView::width() const
{
return _i->width;
}
int WebView::height() const
{
return _i->height;
}
osg::Image* WebView::get_image()
{
return _i->image;
}
WebView::~WebView()
{
delete _i;
}
// ----------------------------- WebCore
WebCore::WebCore()
: _i(new WebCoreImpl())
{
}
void WebCore::init(void* hinst, void* main_wnd)
{
_i->hinst = (HINSTANCE)hinst;
_i->main_wnd = (HWND)main_wnd;
}
WebCore::~WebCore()
{
if(_i) {
delete _i;
}
}
WebView* WebCore::create_view(int w, int h)
{
bool transparent = true;
WebView* web_view = new WebView(this, w, h);
_i->client_handler->set_render_handler(web_view->_i);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = w;
rect.bottom = h;
CefWindowInfo info;
CefBrowserSettings settings;
info.SetAsOffScreen(0L); // for popups: _i->main_wnd
settings.file_access_from_file_urls = STATE_ENABLED;
settings.plugins = STATE_DISABLED;
settings.webgl = STATE_DISABLED;
settings.java = STATE_DISABLED;
settings.caret_browsing = STATE_ENABLED;
info.SetTransparentPainting(transparent ? TRUE : FALSE);
// Creat the new child browser window
CefBrowserHost::CreateBrowser(info, _i->client_handler.get(),
_i->client_handler->get_startup_url(), settings);
return web_view;
}
}; // end of namespace app