-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl-stripped-cpp-example.txt
412 lines (339 loc) · 12 KB
/
sdl-stripped-cpp-example.txt
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
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <include/cef_app.h>
#include <include/cef_base.h>
#include <include/cef_client.h>
#include <include/cef_render_handler.h>
#include <include/cef_life_span_handler.h>
#include <include/cef_load_handler.h>
#include <include/wrapper/cef_helpers.h>
#include "include/base/cef_build.h"
#include "include/base/cef_ref_counted.h"
class RenderHandler :
public CefRenderHandler
{
public:
RenderHandler(int w, int h) :
width(w),
height(h)
{
texture = nullptr;
}
~RenderHandler()
{
texture = nullptr;
renderer = nullptr;
}
void GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect)
{
rect = CefRect(0, 0, width, height);
}
void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList& dirtyRects, const void* buffer, int w, int h)
{
if (texture)
{
unsigned char* texture_data = NULL;
int texture_pitch = 0;
memcpy(texture_data, buffer, w * h * 4);
}
}
void resize(int w, int h)
{
texture = nullptr;
width = w;
height = h;
}
void render()
{
}
private:
int width;
int height;
void* renderer = nullptr;
void* texture = nullptr;
IMPLEMENT_REFCOUNTING(RenderHandler);
};
// for manual render handler
class BrowserClient :
public CefClient,
public CefLifeSpanHandler,
public CefLoadHandler
{
public:
BrowserClient(CefRefPtr<CefRenderHandler> ptr) :
handler(ptr)
{
}
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler()
{
return this;
}
virtual CefRefPtr<CefLoadHandler> GetLoadHandler()
{
return this;
}
virtual CefRefPtr<CefRenderHandler> GetRenderHandler()
{
return handler;
}
// CefLifeSpanHandler methods.
void OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
// Must be executed on the UI thread.
CEF_REQUIRE_UI_THREAD();
browser_id = browser->GetIdentifier();
}
bool DoClose(CefRefPtr<CefBrowser> browser)
{
// Must be executed on the UI thread.
CEF_REQUIRE_UI_THREAD();
// Closing the main window requires special handling. See the DoClose()
// documentation in the CEF header for a detailed description of this
// process.
if (browser->GetIdentifier() == browser_id)
{
// Set a flag to indicate that the window close should be allowed.
closing = true;
}
// Allow the close. For windowed browsers this will result in the OS close
// event being sent.
return false;
}
void OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
}
void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode)
{
std::cout << "OnLoadEnd(" << httpStatusCode << ")" << std::endl;
loaded = true;
}
bool OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefLoadHandler::ErrorCode errorCode, const CefString& failedUrl, CefString& errorText)
{
std::cout << "OnLoadError()" << std::endl;
loaded = true;
}
void OnLoadingStateChange(CefRefPtr<CefBrowser> browser, bool isLoading, bool canGoBack, bool canGoForward)
{
std::cout << "OnLoadingStateChange()" << std::endl;
}
void OnLoadStart(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame)
{
std::cout << "OnLoadStart()" << std::endl;
}
bool closeAllowed() const
{
return closing;
}
bool isLoaded() const
{
return loaded;
}
private:
int browser_id;
bool closing = false;
bool loaded = false;
CefRefPtr<CefRenderHandler> handler;
IMPLEMENT_REFCOUNTING(BrowserClient);
};
/*
CefBrowserHost::MouseButtonType translateMouseButton(SDL_MouseButtonEvent const &e)
{
CefBrowserHost::MouseButtonType result;
switch (e.button)
{
case SDL_BUTTON_LEFT:
case SDL_BUTTON_X1:
result = MBT_LEFT;
break;
case SDL_BUTTON_MIDDLE:
result = MBT_MIDDLE;
break;
case SDL_BUTTON_RIGHT:
case SDL_BUTTON_X2:
result = MBT_RIGHT;
break;
}
return result;
}*/
int cef_handler_setup()
{
CefMainArgs args;
CefSettings settings;
int exec_result = CefExecuteProcess(args, nullptr, nullptr);
// checkout CefApp, derive it and set it as second parameter, for more control on
// command args and resources.
if (exec_result >= 0) // child proccess has endend, so exit.
{
return exec_result;
}
else if (exec_result == -1)
{
// we are here in the father proccess.
}
// CefString(&settings.resources_dir_path).FromASCII("");
std::ostringstream ss;
// ss << SDL_GetBasePath() << "locales/";
CefString(&settings.locales_dir_path) = ss.str();
CefString(&settings.locales_dir_path).FromASCII("");
//CefString(&settings.resources_dir_path) = SDL_GetBasePath();
// checkout detailed settings options http://magpcss.org/ceforum/apidocs/projects/%28default%29/_cef_settings_t.html
// nearly all the settings can be set via args too.
// settings.multi_threaded_message_loop = true; // not supported, except windows
// CefString(&settings.browser_subprocess_path).FromASCII("sub_proccess path, by default uses and starts this executeable as child");
// CefString(&settings.cache_path).FromASCII("");
// CefString(&settings.log_file).FromASCII("");
// settings.log_severity = LOGSEVERITY_DEFAULT;
bool init_result = CefInitialize(args, settings, nullptr, nullptr);
// CefInitialize creates a sub-proccess and executes the same executeable, as calling CefInitialize, if not set different in settings.browser_subprocess_path
// if you create an extra program just for the childproccess you only have to call CefExecuteProcess(...) in it.
if (!init_result)
{
// handle error
return -1;
}
//Initialize SDL
/*if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1;
}*/
int width = 800;
int height = 600;
//auto window = SDL_CreateWindow("Render CEF with SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_RESIZABLE);
CefRefPtr<RenderHandler> renderHandler = new RenderHandler(width, height);
// create browser-window
CefRefPtr<CefBrowser> browser;
CefRefPtr<BrowserClient> browserClient;
{
CefWindowInfo window_info;
CefBrowserSettings browserSettings;
// browserSettings.windowless_frame_rate = 60; // 30 is default
window_info.SetAsWindowless(NULL); // false means no transparency (site background colour)
browserClient = new BrowserClient(renderHandler);
browser = CefBrowserHost::CreateBrowserSync(window_info, browserClient.get(), "http://www.google.com", browserSettings, nullptr, nullptr);
// inject user-input by calling - non-trivial for non-windows - checkout the cefclient source and the platform specific cpp, like cefclient_osr_widget_gtk.cpp for linux
// browser->GetHost()->SendKeyEvent(...);
// browser->GetHost()->SendMouseMoveEvent(...);
// browser->GetHost()->SendMouseClickEvent(...);
// browser->GetHost()->SendMouseWheelEvent(...);
}
bool shutdown = false;
bool js_executed = false;
int null_event = 0;
while (!browserClient->closeAllowed())
{
// send events to browser
while (!shutdown && null_event != 0)
{
switch (null_event)
{
case 0: { //SDL_QUIT
shutdown = true;
browser->GetHost()->CloseBrowser(false);
break; }
case 1: {//SDL_KEYDOWN:
CefKeyEvent event;
//event.modifiers = getKeyboardModifiers(e.key.keysym.mod);
//event.windows_key_code = getWindowsKeyCode(e.key.keysym);
event.type = KEYEVENT_RAWKEYDOWN;
browser->GetHost()->SendKeyEvent(event);
event.type = KEYEVENT_CHAR;
browser->GetHost()->SendKeyEvent(event);
break; }
case 2: {//SDL_KEYUP:
CefKeyEvent event;
//event.modifiers = getKeyboardModifiers(e.key.keysym.mod);
//event.windows_key_code = getWindowsKeyCode(e.key.keysym);
event.type = KEYEVENT_KEYUP;
browser->GetHost()->SendKeyEvent(event);
break; }
case 3: {//SDL_WINDOWEVENT:
int null_window_event = 0;
switch (null_window_event) {
case 0://SDL_WINDOWEVENT_SIZE_CHANGED:
//renderHandler->resize(e.window.data1, e.window.data2);
browser->GetHost()->WasResized();
break;
case 1://SDL_WINDOWEVENT_FOCUS_GAINED:
browser->GetHost()->SetFocus(true);
break;
case 2://SDL_WINDOWEVENT_FOCUS_LOST:
browser->GetHost()->SetFocus(false);
break;
case 3://SDL_WINDOWEVENT_HIDDEN:
case 4://SDL_WINDOWEVENT_MINIMIZED:
// Deprecated?
//browser->GetHost()->SetWindowVisibility(false);
browser->GetHost()->WasHidden(true);
break;
case 5://SDL_WINDOWEVENT_SHOWN:
case 6://SDL_WINDOWEVENT_RESTORED:
//browser->GetHost()->SetWindowVisibility(true);
browser->GetHost()->WasHidden(false);
break;
case 7://SDL_WINDOWEVENT_CLOSE:
//e.type = SDL_QUIT;
//SDL_PushEvent(&e);
break;
}
break; }
case 4: {//SDL_MOUSEMOTION:
CefMouseEvent event;
//event.x = e.motion.x;
//event.y = e.motion.y;
browser->GetHost()->SendMouseMoveEvent(event, false);
break; }
case 5: {//SDL_MOUSEBUTTONUP:
CefMouseEvent event;
//event.x = e.button.x;
//event.y = e.button.y;
// browser->GetHost()->SendMouseClickEvent(event, translateMouseButton(e.button), true, 1);
break; }
case 6: {//SDL_MOUSEBUTTONDOWN:
CefMouseEvent event;
//event.x = e.button.x;
//event.y = e.button.y;
//browser->GetHost()->SendMouseClickEvent(event, translateMouseButton(e.button), false, 1);
break; }
case 7: {//SDL_MOUSEWHEEL:
//int delta_x = e.wheel.x;
//int delta_y = e.wheel.y;
/*if (SDL_MOUSEWHEEL_FLIPPED == e.wheel.direction)
{
delta_y *= -1;
}
else
{
delta_x *= -1;
}*/
int delta_x = 0;
int delta_y = 0;
CefMouseEvent event;
browser->GetHost()->SendMouseWheelEvent(event, delta_x, delta_y);
break; }
}
}
if (!js_executed && browserClient->isLoaded())
{
js_executed = true;
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
frame->ExecuteJavaScript("alert('ExecuteJavaScript works!');", frame->GetURL(), 0);
}
// let browser process events
CefDoMessageLoopWork();
// render
//SDL_RenderClear(renderer);
renderHandler->render();
// Update screen
//SDL_RenderPresent(renderer);
}
browser = nullptr;
browserClient = nullptr;
renderHandler = nullptr;
CefShutdown();
//SDL_DestroyRenderer(renderer);
//SDL_DestroyWindow(window);
//SDL_Quit();
return 0;
}