-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomium.cpp
342 lines (300 loc) · 9.36 KB
/
omium.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
#include <omega.h>
#include <omega/PythonInterpreterWrapper.h>
#include <cef_app.h>
#include <cef_client.h>
#include <cef_render_handler.h>
#include <string>
using namespace omega;
///////////////////////////////////////////////////////////////////////////////
class RenderHandler : public CefRenderHandler
{
public:
RenderHandler(PixelData* pixels):
myPixels(pixels)
{
}
public:
bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect)
{
rect = CefRect(0, 0, myPixels->getWidth(), myPixels->getHeight());
return true;
}
void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height)
{
byte* pixels = myPixels->map();
memcpy(pixels, buffer, width*height * 4);
myPixels->unmap();
myPixels->setDirty();
}
private:
PixelData* myPixels;
public:
IMPLEMENT_REFCOUNTING(RenderHandler);
};
///////////////////////////////////////////////////////////////////////////////
class BrowserClient : public CefClient
{
public:
BrowserClient(RenderHandler *renderHandler)
: myRenderHandler(renderHandler)
{ }
virtual CefRefPtr<CefRenderHandler> GetRenderHandler()
{
return myRenderHandler;
}
CefRefPtr<CefRenderHandler> myRenderHandler;
public:
IMPLEMENT_REFCOUNTING(BrowserClient);
};
///////////////////////////////////////////////////////////////////////////////
class BrowserApp : public CefApp
{
public:
virtual void OnBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr< CefCommandLine > command_line)
{
// Enable gpu on child process
command_line->AppendSwitch("disable-gpu");
}
IMPLEMENT_REFCOUNTING(BrowserApp);
};
///////////////////////////////////////////////////////////////////////////////
class Omium : public EngineModule
{
public:
static Omium* instance;
static Omium* getInstance() { return instance; }
public:
Omium() : EngineModule("omium"),
myInitialized(false),
myFocused(false),
myZoomLevel(1)
{
ModuleServices::addModule(this);
setPriority(EngineModule::PriorityHigh);
resize(600, 400);
initialize();
}
~Omium()
{
if(myInitialized)
{
myBrowser = NULL;
myBrowserClient = NULL;
myRenderHandler = NULL;
CefShutdown();
}
}
void open(const char* url)
{
if(myBrowser != NULL)
{
myBrowser->GetMainFrame()->LoadURL(url);
}
else
{
CefWindowInfo window_info;
CefBrowserSettings browserSettings;
browserSettings.webgl = STATE_DISABLED;
RenderHandler* renderHandler = new RenderHandler(myPixels);
//browserSettings.windowless_frame_rate = 60; // 30 is default
// in linux set a gtk widget, in windows a hwnd. If not available set nullptr - may cause some render errors, in context-menu and plugins.
//std::size_t windowHandle = 0;
//renderSystem->getAutoCreatedWindow()->getCustomAttribute("WINDOW", &windowHandle);
window_info.SetAsWindowless(NULL, true);
myBrowserClient = new BrowserClient(renderHandler);
myBrowser = CefBrowserHost::CreateBrowserSync(window_info, myBrowserClient.get(), url, browserSettings, NULL);
myBrowser->GetHost()->SetZoomLevel(myZoomLevel);
}
}
virtual void initializeRenderer(Renderer* r);
virtual void update(const UpdateContext& context)
{
CefDoMessageLoopWork();
}
virtual void handleEvent(const Event& e)
{
if(myBrowser != NULL)
{
CefRefPtr<CefBrowserHost> host = myBrowser->GetHost();
// Handle focus switches
if(e.getServiceType() == Service::Pointer)
{
CefMouseEvent me;
me.x = e.getPosition()[0];
me.y = e.getPosition()[1];
if(e.getType() == Event::Down)
{
// If we click on something opaque, the browser intercepts
// this mouse event
myPixels->beginPixelAccess();
bool focusState = myPixels->getPixelA(me.x, me.y) > 0;
myPixels->endPixelAccess();
if(focusState != myFocused)
{
if(myFocusChangedCommand.size() > 0)
{
PythonInterpreter* pi = SystemManager::instance()->getScriptInterpreter();
pi->queueCommand(myFocusChangedCommand);
}
myFocused = focusState;
}
}
else if(e.getType() == Event::Move)
{
host->SendMouseMoveEvent(me, false);
}
}
if(myFocused)
{
if(e.getServiceType() == static_cast<enum Service::ServiceType>(Event::ServiceTypeKeyboard))
{
e.setProcessed();
uint sKeyFlags;
//omsg("Keyboard Event");
bool isDown = (e.getType() == Event::Down);
// We assume the keyboard event contains the native scancode in the extra
// data int array
if(e.getExtraDataType() == Event::ExtraDataIntArray)
{
sendKeyEvent(e.getExtraDataInt(0), host, isDown);
}
char c;
if(e.getChar(&c)) {
sendCharEvent(c, (int)(c), host, e.getFlags());
}
}
else if(e.getServiceType() == Service::Pointer)
{
CefMouseEvent me;
me.x = e.getPosition()[0];
me.y = e.getPosition()[1];
e.setProcessed();
if(e.getType() == Event::Down || e.getType() == Event::Up)
{
CefBrowserHost::MouseButtonType mbt = MBT_LEFT;
if(e.isFlagSet(Event::Right)) mbt = MBT_RIGHT;
else if(e.isFlagSet(Event::Middle)) mbt = MBT_MIDDLE;
host->SendMouseClickEvent(me, mbt, e.getType() == Event::Up, 1);
}
}
}
}
}
PixelData* getPixels() { return myPixels; }
void sendCharEvent(char character, int code, CefBrowserHost * host, uint flags) {
CefKeyEvent ce;
ce.windows_key_code = code;
ce.modifiers = flags;
ce.character = character;
ce.type = KEYEVENT_CHAR;
host->SendKeyEvent(ce);
}
void sendKeyEvent(int code, CefBrowserHost * host, bool isDown) {
CefKeyEvent ce;
//ce.windows_key_code = code;
ce.native_key_code = code;
ce.focus_on_editable_field = true;
if (isDown) {
ce.type = KEYEVENT_KEYDOWN;
} else {
ce.type = KEYEVENT_KEYUP;
}
// HACK: only send keydown events (otherwise special key input like arrows
// and backspace repeat)
if(isDown) host->SendKeyEvent(ce);
}
void resize(int width, int height)
{
myWidth = width;
myHeight = height;
if(myPixels.isNull())
{
myPixels = new PixelData(PixelData::FormatBgra, myWidth, myHeight);
}
else
{
myPixels->resize(myWidth, myHeight);
}
if(myBrowser != NULL)
{
myBrowser->GetHost()->WasResized();
}
}
void setZoom(float zoom)
{
myZoomLevel = zoom;
if(myBrowser != NULL) myBrowser->GetHost()->SetZoomLevel(zoom);
}
bool isFocused() { return myFocused; }
void setFocusChangedCommand(const String& cmd) { myFocusChangedCommand = cmd; }
private:
void initialize()
{
myInitialized = true;
CefMainArgs args;
int result = CefExecuteProcess(args, NULL, NULL);
if(result >= 0)
{
omsg("Exited child CEF3 process");
}
CefSettings settings;
settings.windowless_rendering_enabled = 1;
settings.single_process = 1;
#ifdef OMEGA_OS_OSX
String execdir;
String execname;
StringUtils::splitFilename(ogetexecpath(), execname, execdir);
execdir = StringUtils::replaceAll(execdir, "./", "");
String respath = ostr("%1%Resources", %execdir);
oflog(Verbose, "[omium::initialize] resources path: %1%", %respath);
CefString(&settings.resources_dir_path).FromASCII(respath.c_str());
#endif
//CefString(&settings.browser_subprocess_path).FromASCII("omium_process");
if(!CefInitialize(args, settings, new BrowserApp(), NULL))
{
oerror("CefInitialize failed");
}
}
bool myInitialized;
CefRefPtr<CefBrowser> myBrowser;
CefRefPtr<BrowserClient> myBrowserClient;
CefRefPtr<RenderHandler> myRenderHandler;
Ref<PixelData> myPixels;
int myWidth;
int myHeight;
float myZoomLevel;
bool myFocused;
String myFocusChangedCommand;
};
///////////////////////////////////////////////////////////////////////////////
class OmiumRenderPass : public RenderPass
{
public:
OmiumRenderPass(Renderer* r, Omium* o) : RenderPass(r, "OmiumRenderPass"),
myOwner(o)
{}
void render(Renderer* client, const DrawContext& context)
{
}
private:
Omium* myOwner;
};
///////////////////////////////////////////////////////////////////////////////
void Omium::initializeRenderer(Renderer* r)
{
r->addRenderPass(new OmiumRenderPass(r, this));
}
Omium* Omium::instance = NULL;
BOOST_PYTHON_MODULE(omium)
{
PYAPI_REF_BASE_CLASS(Omium)
PYAPI_STATIC_REF_GETTER(Omium, getInstance)
PYAPI_METHOD(Omium, resize)
PYAPI_REF_GETTER(Omium, getPixels)
PYAPI_METHOD(Omium, open)
PYAPI_METHOD(Omium, setZoom)
PYAPI_METHOD(Omium, setFocusChangedCommand)
PYAPI_METHOD(Omium, isFocused)
;
Omium::instance = new Omium();
}