broadway: Move event coordinate calculation to browser side

This commit is contained in:
Alexander Larsson 2011-03-11 21:10:23 +01:00
parent 0cefd54122
commit cc808ae063
2 changed files with 193 additions and 109 deletions

View File

@ -66,6 +66,7 @@ function createXHR()
var last_serial = 0;
var last_x = 0;
var last_y = 0;
var window_with_mouse = 0;
var surfaces = {};
var outstanding_commands = new Array();
var input_socket = null;
@ -293,24 +294,75 @@ function send_input(cmd, args)
}
}
function update_positions_from_event(ev) {
last_x = ev.pageX;
last_y = ev.pageY;
function get_document_coordinates(element)
{
var res = new Object();
res.x = element.offsetLeft;
res.y = element.offsetTop;
var offsetParent = element.offsetParent;
while (offsetParent != null) {
res.x += offsetParent.offsetLeft;
res.y += offsetParent.offsetTop;
offsetParent = offsetParent.offsetParent;
}
return res;
}
function getPositionsFromEvent(ev, relativeId) {
var res = Object();
res.root_x = ev.pageX;
res.root_y = ev.pageY;
res.win_x = ev.pageX;
res.win_y = ev.pageY;
if (relativeId != 0) {
var pos = get_document_coordinates(ev.target);
res.win_x = res.win_x - pos.x;
res.win_y = res.win_y - pos.y;
}
last_x = res.root_x;
last_y = res.root_y;
return res;
}
function on_mouse_move (ev) {
update_positions_from_event(ev);
send_input ("m", [get_surface_id(ev), last_x, last_y, ev.timeStamp]);
var id = get_surface_id(ev);
var pos = getPositionsFromEvent(ev, id);
send_input ("m", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp]);
}
function on_mouse_over (ev) {
var id = get_surface_id(ev);
var pos = getPositionsFromEvent(ev, id);
window_with_mouse = id;
if (window_with_mouse != 0) {
send_input ("e", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp]);
}
}
function on_mouse_out (ev) {
var id = get_surface_id(ev);
var pos = getPositionsFromEvent(ev, id);
if (id != 0) {
send_input ("l", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp]);
}
window_with_mouse = 0;
}
function on_mouse_down (ev) {
update_positions_from_event(ev);
send_input ("b", [get_surface_id(ev), last_x, last_y, ev.button, ev.timeStamp]);
var id = get_surface_id(ev);
var pos = getPositionsFromEvent(ev, id);
send_input ("b", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp, ev.button]);
}
function on_mouse_up (ev) {
update_positions_from_event(ev);
send_input ("B", [get_surface_id(ev), last_x, last_y, ev.button, ev.timeStamp]);
var id = get_surface_id(ev);
var pos = getPositionsFromEvent(ev, id);
send_input ("B", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp, ev.button]);
}
var last_key_down = 0;
@ -344,11 +396,15 @@ function cancel_event(ev)
function on_mouse_wheel(ev)
{
ev = ev ? ev : window.event;
var id = get_surface_id(ev);
var pos = getPositionsFromEvent(ev, id);
var offset = ev.detail ? ev.detail : ev.wheelDelta;
var dir = 0
if (offset > 0)
dir = 1;
send_input ("s", [get_surface_id(ev), ev.pageX, ev.pageY, dir, ev.timeStamp])
send_input ("s", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp, dir])
return cancel_event(ev);
}
@ -383,6 +439,8 @@ function connect()
}
document.oncontextmenu = function () { return false; }
document.onmousemove = on_mouse_move;
document.onmouseover = on_mouse_over;
document.onmouseout = on_mouse_out;
document.onmousedown = on_mouse_down;
document.onmouseup = on_mouse_up;
document.onkeydown = on_key_down;

View File

@ -88,6 +88,34 @@ gdk_event_source_check (GSource *source)
return retval;
}
typedef struct {
int id;
int root_x;
int root_y;
int win_x;
int win_y;
guint64 time;
} PointerData;
static char *
parse_pointer_data (char *p, PointerData *data)
{
data->id = strtol (p, &p, 10);
p++; /* Skip , */
data->root_x = strtol (p, &p, 10);
p++; /* Skip , */
data->root_y = strtol (p, &p, 10);
p++; /* Skip , */
data->win_x = strtol (p, &p, 10);
p++; /* Skip , */
data->win_y = strtol (p, &p, 10);
p++; /* Skip , */
data->time = strtol(p, &p, 10);
return p;
}
void
_gdk_broadway_events_got_input (GdkDisplay *display,
const char *message)
@ -96,100 +124,110 @@ _gdk_broadway_events_got_input (GdkDisplay *display,
GdkScreen *screen;
GdkWindow *root, *window;
char *p;
int x, y, button, id, dir,key;
int button, dir,key;
guint32 serial;
guint64 time;
GdkEvent *event = NULL;
char cmd;
GList *node;
PointerData data;
screen = gdk_display_get_default_screen (display);
root = gdk_screen_get_root_window (screen);
p = (char *)message;
cmd = *p++;
serial = (guint32)strtol(p, &p, 10);
serial = (guint32)strtol (p, &p, 10);
p++; /* Skip , */
switch (cmd) {
case 'm':
id = strtol(p, &p, 10);
p++; /* Skip , */
x = strtol(p, &p, 10);
p++; /* Skip , */
y = strtol(p, &p, 10);
p++; /* Skip , */
time = strtol(p, &p, 10);
display_broadway->last_x = x;
display_broadway->last_y = y;
case 'e': /* Enter */
p = parse_pointer_data (p, &data);
window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (id));
display_broadway->last_x = data.root_x;
display_broadway->last_y = data.root_y;
if (display_broadway->mouse_in_toplevel != window)
window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
/* TODO: Unset when it dies */
display_broadway->mouse_in_toplevel = window;
if (window)
{
if (display_broadway->mouse_in_toplevel != NULL)
{
event = gdk_event_new (GDK_LEAVE_NOTIFY);
event->crossing.window = g_object_ref (display_broadway->mouse_in_toplevel);
event->crossing.time = time;
event->crossing.x = x - display_broadway->mouse_in_toplevel->x;
event->crossing.y = y - display_broadway->mouse_in_toplevel->y;
event->crossing.x_root = x;
event->crossing.y_root = y;
event->crossing.mode = GDK_CROSSING_NORMAL;
event->crossing.detail = GDK_NOTIFY_ANCESTOR;
gdk_event_set_device (event, display->core_pointer);
event = gdk_event_new (GDK_ENTER_NOTIFY);
event->crossing.window = g_object_ref (window);
event->crossing.time = data.time;
event->crossing.x = data.win_x;
event->crossing.y = data.win_y;
event->crossing.x_root = data.root_x;
event->crossing.y_root = data.root_y;
event->crossing.mode = GDK_CROSSING_NORMAL;
event->crossing.detail = GDK_NOTIFY_ANCESTOR;
gdk_event_set_device (event, display->core_pointer);
node = _gdk_event_queue_append (display, event);
_gdk_windowing_got_event (display, node, event, serial);
node = _gdk_event_queue_append (display, event);
_gdk_windowing_got_event (display, node, event, serial);
event = gdk_event_new (GDK_FOCUS_CHANGE);
event->focus_change.window = g_object_ref (display_broadway->mouse_in_toplevel);
event->focus_change.in = FALSE;
gdk_event_set_device (event, display->core_pointer);
event = gdk_event_new (GDK_FOCUS_CHANGE);
event->focus_change.window = g_object_ref (window);
event->focus_change.in = TRUE;
gdk_event_set_device (event, display->core_pointer);
node = _gdk_event_queue_append (display, event);
_gdk_windowing_got_event (display, node, event, serial);
}
/* TODO: Unset when it dies */
display_broadway->mouse_in_toplevel = window;
if (window)
{
event = gdk_event_new (GDK_ENTER_NOTIFY);
event->crossing.window = g_object_ref (window);
event->crossing.time = time;
event->crossing.x = x - window->x;
event->crossing.y = y - window->y;
event->crossing.x_root = x;
event->crossing.y_root = y;
event->crossing.mode = GDK_CROSSING_NORMAL;
event->crossing.detail = GDK_NOTIFY_ANCESTOR;
gdk_event_set_device (event, display->core_pointer);
node = _gdk_event_queue_append (display, event);
_gdk_windowing_got_event (display, node, event, serial);
event = gdk_event_new (GDK_FOCUS_CHANGE);
event->focus_change.window = g_object_ref (window);
event->focus_change.in = TRUE;
gdk_event_set_device (event, display->core_pointer);
node = _gdk_event_queue_append (display, event);
_gdk_windowing_got_event (display, node, event, serial);
}
node = _gdk_event_queue_append (display, event);
_gdk_windowing_got_event (display, node, event, serial);
}
break;
case 'l': /* Leave */
p = parse_pointer_data (p, &data);
display_broadway->last_x = data.root_x;
display_broadway->last_y = data.root_y;
window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
display_broadway->mouse_in_toplevel = NULL;
if (window)
{
event = gdk_event_new (GDK_LEAVE_NOTIFY);
event->crossing.window = g_object_ref (window);
event->crossing.time = data.time;
event->crossing.x = data.win_x;
event->crossing.y = data.win_y;
event->crossing.x_root = data.root_x;
event->crossing.y_root = data.root_y;
event->crossing.mode = GDK_CROSSING_NORMAL;
event->crossing.detail = GDK_NOTIFY_ANCESTOR;
gdk_event_set_device (event, display->core_pointer);
node = _gdk_event_queue_append (display, event);
_gdk_windowing_got_event (display, node, event, serial);
event = gdk_event_new (GDK_FOCUS_CHANGE);
event->focus_change.window = g_object_ref (window);
event->focus_change.in = FALSE;
gdk_event_set_device (event, display->core_pointer);
node = _gdk_event_queue_append (display, event);
_gdk_windowing_got_event (display, node, event, serial);
}
break;
case 'm': /* Mouse move */
p = parse_pointer_data (p, &data);
display_broadway->last_x = data.root_x;
display_broadway->last_y = data.root_y;
window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
if (window)
{
event = gdk_event_new (GDK_MOTION_NOTIFY);
event->motion.window = g_object_ref (window);
event->motion.time = time;
event->motion.x = x - window->x;
event->motion.y = y - window->y;
event->motion.x_root = x;
event->motion.y_root = y;
event->motion.time = data.time;
event->motion.x = data.win_x;
event->motion.y = data.win_y;
event->motion.x_root = data.root_x;
event->motion.y_root = data.root_y;
gdk_event_set_device (event, display->core_pointer);
node = _gdk_event_queue_append (display, event);
@ -199,29 +237,23 @@ _gdk_broadway_events_got_input (GdkDisplay *display,
break;
case 'b':
case 'B':
id = strtol(p, &p, 10);
p++; /* Skip , */
x = strtol(p, &p, 10);
p++; /* Skip , */
y = strtol(p, &p, 10);
p = parse_pointer_data (p, &data);
p++; /* Skip , */
button = strtol(p, &p, 10);
p++; /* Skip , */
time = strtol(p, &p, 10);
display_broadway->last_x = x;
display_broadway->last_y = y;
display_broadway->last_x = data.root_x;
display_broadway->last_y = data.root_y;
window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (id));
window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
if (window)
{
event = gdk_event_new (cmd == 'b' ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE);
event->button.window = g_object_ref (window);
event->button.time = time;
event->button.x = x - window->x;
event->button.y = y - window->y;
event->button.x_root = x;
event->button.y_root = y;
event->button.time = data.time;
event->button.x = data.win_x;
event->button.y = data.win_y;
event->button.x_root = data.root_x;
event->button.y_root = data.root_y;
event->button.button = button + 1;
gdk_event_set_device (event, display->core_pointer);
@ -231,29 +263,23 @@ _gdk_broadway_events_got_input (GdkDisplay *display,
break;
case 's':
id = strtol(p, &p, 10);
p++; /* Skip , */
x = strtol(p, &p, 10);
p++; /* Skip , */
y = strtol(p, &p, 10);
p = parse_pointer_data (p, &data);
p++; /* Skip , */
dir = strtol(p, &p, 10);
p++; /* Skip , */
time = strtol(p, &p, 10);
display_broadway->last_x = x;
display_broadway->last_y = y;
display_broadway->last_x = data.root_x;
display_broadway->last_y = data.root_y;
window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (id));
window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
if (window)
{
event = gdk_event_new (GDK_SCROLL);
event->scroll.window = g_object_ref (window);
event->scroll.time = time;
event->scroll.x = x - window->x;
event->scroll.y = y - window->y;
event->scroll.x_root = x;
event->scroll.y_root = y;
event->scroll.time = data.time;
event->scroll.x = data.win_x;
event->scroll.y = data.win_y;
event->scroll.x_root = data.root_x;
event->scroll.y_root = data.root_y;
event->scroll.direction = dir == 0 ? GDK_SCROLL_UP : GDK_SCROLL_DOWN;
gdk_event_set_device (event, display->core_pointer);