-
Notifications
You must be signed in to change notification settings - Fork 1
/
render_handler.c
390 lines (346 loc) · 13 KB
/
render_handler.c
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
#include "ecef.h"
#include <Ecore_X.h>
int GetControlCharacter(KeyboardCode windows_key_code, int shift);
KeyboardCode GdkEventToWindowsKeyCode(const Evas_Event_Key_Down* event);
KeyboardCode GetWindowsKeyCodeWithoutLocation(KeyboardCode key_code);
KeyboardCode KeyboardCodeFromXKeysym(unsigned int keysym);
void render_image_gl_setup(Browser *b, int w, int h);
void render_image_gl_paint(Browser *b);
#ifdef HAVE_SERVO
void render_image_gl_present(cef_render_handler_t *handler, cef_browser_t *browser);
#endif
void render_image_gl_setup(Browser *b, int w, int h);
static int
get_root_screen_rect(cef_render_handler_t *self, cef_browser_t *browser, cef_rect_t *rect)
{
ecore_x_window_size_get(ecore_x_window_root_first_get(), &rect->width, &rect->height);
return 1;
}
static int
get_view_rect(cef_render_handler_t *self, cef_browser_t *browser, cef_rect_t *rect)
{
ECef_Client *ec;
ec = browser_get_client(browser);
edje_object_part_geometry_get(elm_layout_edje_get(ec->layout), "ecef.sizer.browser", &rect->x, &rect->y, &rect->width, &rect->height);
//fprintf(stderr, "VIEW RECT: %dx%d\n", rect->width, rect->height);
return 1;
}
static int
get_screen_point(cef_render_handler_t *self, cef_browser_t *browser, int viewX, int viewY, int *screenX, int *screenY)
{
ECef_Client *client;
int x, y;
client = browser_get_client(browser);
evas_object_geometry_get(client->win, &x, &y, NULL, NULL);
*screenX = viewX + x;
*screenY = viewY + y;
return 1;
}
static void
paint(cef_render_handler_t *self, cef_browser_t *browser, cef_paint_element_type_t type,
size_t dirtyRectsCount, cef_rect_t const *dirtyRects, const void *buffer,
int width, int height)
{
ECef_Client *ec;
Evas_Object *img, *o;
size_t r;
Browser *b;
Eina_List *l;
Eina_Bool first;
ec = browser_get_client(browser);
b = browser_get(ec, browser);
first = (!b->pw) || (!b->ph);
b->pw = b->w, b->ph = b->h;
b->w = width, b->h = height;
img = b->img;
if (gl_avail && (!servo))
{
if (first || (b->w != b->pw) || (b->h != b->ph))
{
free(b->buffer);
b->buffer = malloc(b->w * b->h * 4);
}
memcpy(b->buffer, buffer, b->w * b->h * 4);
}
if (gl_avail)
render_image_gl_paint(b);
else
{
Eina_List *l;
o = elm_image_object_get(img);
evas_object_image_size_set(o, width, height);
evas_object_image_data_copy_set(o, (void*)buffer);
for (r = 0; r < dirtyRectsCount; r++)
evas_object_image_data_update_add(o, dirtyRects[r].x, dirtyRects[r].y, dirtyRects[r].width, dirtyRects[r].height);
EINA_LIST_FOREACH(b->clones, l, o)
{
o = elm_image_object_get(o);
evas_object_image_size_set(o, width, height);
evas_object_image_data_copy_set(o, (void*)buffer);
for (r = 0; r < dirtyRectsCount; r++)
evas_object_image_data_update_add(o, dirtyRects[r].x, dirtyRects[r].y, dirtyRects[r].width, dirtyRects[r].height);
}
if (first || (b->w != b->pw) || (b->h != b->ph))
{
elm_image_smooth_set(img, 0);
EINA_LIST_FOREACH(b->clones, l, o)
elm_image_smooth_set(o, 0);
}
}
EINA_LIST_FOREACH(b->clones, l, o)
evas_object_size_hint_aspect_set(o, EVAS_ASPECT_CONTROL_BOTH, b->w, b->h);
//fprintf(stderr, "PAINT %dx%d\n", width, height);
}
static void
init_handler(ECef_Client *ec)
{
cef_render_handler_t *render_handler;
render_handler = CEF_NEW_PTR(cef_render_handler_t, &ec->render_handler);
render_handler->on_paint = paint;
render_handler->get_root_screen_rect = get_root_screen_rect;
render_handler->get_view_rect = get_view_rect;
render_handler->get_screen_point = get_screen_point;
#ifdef HAVE_SERVO
render_handler->on_present = render_image_gl_present;
#endif
}
cef_render_handler_t *
client_render_handler_get(cef_client_t *client)
{
ECef_Client *ec = (void*)client;
if (!ec->render_handler)
init_handler(ec);
return ec->render_handler;
}
static void
render_image_mouse(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Down *ev, int up)
{
int x, y, mod, count = 1;
cef_browser_host_t *host;
cef_mouse_event_t event;
host = evas_object_data_get(obj, "browser_host");
if (!host) return;
if (ev->flags & EVAS_BUTTON_TRIPLE_CLICK)
count = 3;
else if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
count = 2;
mod = modifiers_get(ev->modifiers);
evas_object_geometry_get(obj, &x, &y, NULL, NULL);
event.x = ev->canvas.x - x, event.y = ev->canvas.y - y;
event.modifiers = mod;
if (evas_pointer_button_down_mask_get(e) & (1 << 0))
event.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
if (evas_pointer_button_down_mask_get(e) & (1 << 1))
event.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
if (evas_pointer_button_down_mask_get(e) & (1 << 2))
event.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
host->send_mouse_click_event(host, &event, ev->button - 1, up, count);
}
static void
render_image_mouse_down(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Down *ev)
{
render_image_mouse(ec, e, obj, ev, 0);
}
static void
render_image_mouse_up(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Down *ev)
{
render_image_mouse(ec, e, obj, ev, 1);
}
static void
render_image_mouse_move(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Move *ev)
{
int x, y, mod;
cef_browser_host_t *host;
cef_mouse_event_t event;
host = evas_object_data_get(obj, "browser_host");
mod = modifiers_get(ev->modifiers);
evas_object_geometry_get(obj, &x, &y, NULL, NULL);
event.x = ev->cur.canvas.x - x, event.y = ev->cur.canvas.y - y;
event.modifiers = mod;
if (ev->buttons & (1 << 0))
event.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
if (ev->buttons & (1 << 1))
event.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
if (ev->buttons & (1 << 2))
event.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
host->send_mouse_move_event(host, &event, 0);
}
static void
render_image_mouse_wheel(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Wheel *ev)
{
int x, y, mod, dx, dy;
cef_browser_host_t *host;
cef_mouse_event_t event;
host = evas_object_data_get(obj, "browser_host");
mod = modifiers_get(ev->modifiers);
if (mod && (!(mod ^ EVENTFLAG_CONTROL_DOWN))) //only ctrl held: zoom
{
double delta = 0;
if (ev->z < 0)
delta = 0.1;
if (ev->z > 0)
delta = -0.1;
host->set_zoom_level(host, host->get_zoom_level(host) + delta);
return;
}
evas_object_geometry_get(obj, &x, &y, NULL, NULL);
event.x = ev->canvas.x - x, event.y = ev->canvas.y - y;
event.modifiers = mod;
if (evas_pointer_button_down_mask_get(e) & (1 << 0))
event.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
if (evas_pointer_button_down_mask_get(e) & (1 << 1))
event.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
if (evas_pointer_button_down_mask_get(e) & (1 << 2))
event.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
dx = 0;
if (ev->z < 0)
dy = 40;
if (ev->z > 0)
dy = -40;
host->send_mouse_wheel_event(host, &event, dx, dy);
}
static void
render_image_mouse_move_out(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Out *ev)
{
int x, y, mod;
cef_browser_host_t *host;
cef_mouse_event_t event;
host = evas_object_data_get(obj, "browser_host");
mod = modifiers_get(ev->modifiers);
evas_object_geometry_get(obj, &x, &y, NULL, NULL);
event.x = ev->canvas.x - x, event.y = ev->canvas.y - y;
event.modifiers = mod;
if (evas_pointer_button_down_mask_get(e) & (1 << 0))
event.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
if (evas_pointer_button_down_mask_get(e) & (1 << 1))
event.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
if (evas_pointer_button_down_mask_get(e) & (1 << 2))
event.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
host->send_mouse_move_event(host, &event, 1);
}
static void
render_image_key(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Key_Down *ev, int up)
{
cef_browser_host_t *host;
cef_key_event_t event;
KeyboardCode windows_key_code;
Eina_Unicode *str;
host = evas_object_data_get(obj, "browser_host");
if (!host) return;
windows_key_code = GdkEventToWindowsKeyCode(ev);
event.windows_key_code = GetWindowsKeyCodeWithoutLocation(windows_key_code);
event.native_key_code = ev->keycode;
event.modifiers = modifiers_get(ev->modifiers);
/* ecore, y u no have keycode defines??? */
if ((ecore_x_keysym_get(ev->key) >= 0xff80 /* space */) && (ecore_x_keysym_get(ev->key) <= 0xffb9 /* kp_9 */))
event.modifiers |= EVENTFLAG_IS_KEY_PAD;
if (event.modifiers & EVENTFLAG_ALT_DOWN)
event.is_system_key = 1;
if (windows_key_code == VKEY_RETURN)
// We need to treat the enter key as a key press of character \r. This
// is apparently just how webkit handles it and what it expects.
event.unmodified_character = '\r';
else if (ev->string)
{
// FIXME: fix for non BMP chars
str = eina_unicode_utf8_to_unicode(ev->string, NULL);
if (str)
event.unmodified_character = *str;
free(str);
}
// If ctrl key is pressed down, then control character shall be input.
if (event.modifiers & EVENTFLAG_CONTROL_DOWN)
event.character = GetControlCharacter(windows_key_code, event.modifiers & EVENTFLAG_SHIFT_DOWN);
else
event.character = event.unmodified_character;
if (up)
{
event.type = KEYEVENT_KEYUP;
host->send_key_event(host, &event);
event.type = KEYEVENT_CHAR;
}
else
event.type = KEYEVENT_RAWKEYDOWN;
host->send_key_event(host, &event);
}
static void
render_image_key_down(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Key_Down *ev)
{
render_image_key(ec, e, obj, ev, 0);
}
static void
render_image_key_up(ECef_Client *ec, Evas *e, Evas_Object *obj, Evas_Event_Key_Down *ev)
{
render_image_key(ec, e, obj, ev, 1);
}
static void
render_image_geom(Browser *b, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
int x, y, w, h;
evas_object_geometry_get(b->img, &x, &y, &w, &h);
//fprintf(stderr, "IMG GEOM: %d,%d %dx%d\n", x, y, w, h);
}
void
render_image_new(ECef_Client *ec, Browser *b, cef_browser_host_t *host, int w, int h)
{
Evas_Object *i;
if (gl_avail)
{
b->img = i = elm_glview_version_add(ec->win, EVAS_GL_GLES_2_X);
if (!i) gl_avail = 0;
}
b->w = w, b->h = h;
if (!b->img)
b->img = i = elm_image_add(ec->win);
evas_object_name_set(i, "browser");
evas_object_resize(i, w, h);
evas_object_data_set(i, "browser_host", host);
evas_object_data_set(i, "Browser", b);
if (ec->current_page == b)
elm_object_part_content_set(ec->layout, "ecef.swallow.browser", i);
if (gl_avail)
render_image_gl_setup(b, w, h);
evas_object_event_callback_add(i, EVAS_CALLBACK_MOUSE_DOWN, (Evas_Object_Event_Cb)render_image_mouse_down, ec);
evas_object_event_callback_add(i, EVAS_CALLBACK_MOUSE_UP, (Evas_Object_Event_Cb)render_image_mouse_up, ec);
evas_object_event_callback_add(i, EVAS_CALLBACK_MOUSE_MOVE, (Evas_Object_Event_Cb)render_image_mouse_move, ec);
evas_object_event_callback_add(i, EVAS_CALLBACK_MOUSE_OUT, (Evas_Object_Event_Cb)render_image_mouse_move_out, ec);
evas_object_event_callback_add(i, EVAS_CALLBACK_MOUSE_WHEEL, (Evas_Object_Event_Cb)render_image_mouse_wheel, ec);
evas_object_event_callback_add(i, EVAS_CALLBACK_KEY_DOWN, (Evas_Object_Event_Cb)render_image_key_down, ec);
evas_object_event_callback_add(i, EVAS_CALLBACK_KEY_UP, (Evas_Object_Event_Cb)render_image_key_up, ec);
evas_object_event_callback_add(i, EVAS_CALLBACK_MOVE, (Evas_Object_Event_Cb)render_image_geom, b);
evas_object_event_callback_add(i, EVAS_CALLBACK_RESIZE, (Evas_Object_Event_Cb)render_image_geom, b);
evas_object_show(i);
}
static void
render_image_clone_del(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
Browser *b = data;
b->clones = eina_list_remove(b->clones, obj);
}
Evas_Object *
render_image_clone(Browser *b, Evas_Object *parent)
{
Evas_Object *img;
ECef_Client *ec;
ec = browser_get_client(b->browser);
img = elm_image_add(parent);
evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, b->w, b->h);
evas_object_event_callback_add(img, EVAS_CALLBACK_DEL, render_image_clone_del, b);
if (ec->clone_update_cb)
ec->clone_update_cb(b, img);
else if (!gl_avail)
{
Evas_Object *o;
void *px;
o = elm_image_object_get(img);
px = evas_object_image_data_get(elm_image_object_get(b->img), 0);
evas_object_image_size_set(o, b->w, b->h);
evas_object_image_data_copy_set(o, px);
evas_object_image_data_update_add(o, 0, 0, b->w, b->h);
evas_object_image_data_set(elm_image_object_get(b->img), px);
}
evas_object_name_set(img, "browser clone");
evas_object_data_set(img, "browser", b);
b->clones = eina_list_append(b->clones, img);
return img;
}