Commit 5535f155 authored by Kristian Høgsberg's avatar Kristian Høgsberg
Browse files

Switch protocol to using serial numbers for ordering events and requests

The wayland protocol, as X, uses timestamps to match up certain
requests with input events.  The problem is that sometimes we need to
send out an event that doesn't have a corresponding timestamped input
event.  For example, the pointer focus surface goes away and new
surface needs to receive a pointer enter event.  These events are
normally timestamped with the evdev event timestamp, but in this case,
we don't have a evdev timestamp.  So we have to go to gettimeofday (or
clock_gettime()) and then we don't know if it's coming from the same
time source etc.

However for all these cases we don't need a real time timestamp, we
just need a serial number that encodes the order of events inside the
server.  So we introduce a serial number mechanism that we can use to
order events.  We still need real-time timestamps for actual input
device events (motion, buttons, keys, touch), to be able to reason
about double-click speed and movement speed so events that correspond to user input carry both a serial number and a timestamp.

The serial number also give us a mechanism to key together events that
are "logically the same" such as a unicode event and a keycode event,
or a motion event and a relative event from a raw device.
parent a13aab4e
......@@ -109,7 +109,7 @@
<interface name="wl_callback" version="1">
<event name="done">
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
</event>
</interface>
......@@ -239,7 +239,7 @@
NULL for not accepted. Use for feedback during drag and drop.
</description>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="type" type="string"/>
</request>
......@@ -311,7 +311,7 @@
data source that provides the data for the eventual data
transfer. The origin surface is the surface where the drag
originates and the client must have an active implicit grab
that matches the timestamp. The icon surface is an optional
that matches the serial. The icon surface is an optional
(can be nil) surface that provides an icon to be moved around
with the cursor. Initially, the top-left corner of the icon
surface is placed at the cursor hotspot, but subsequent
......@@ -320,12 +320,12 @@
<arg name="source" type="object" interface="wl_data_source"/>
<arg name="origin" type="object" interface="wl_surface"/>
<arg name="icon" type="object" interface="wl_surface"/>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
</request>
<request name="set_selection">
<arg name="source" type="object" interface="wl_data_source"/>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
</request>
<event name="data_offer">
......@@ -343,7 +343,7 @@
</event>
<event name="enter">
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="surface" type="object" interface="wl_surface"/>
<arg name="x" type="int"/>
<arg name="y" type="int"/>
......@@ -405,7 +405,7 @@
<request name="move">
<arg name="input_device" type="object" interface="wl_input_device"/>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
</request>
<enum name="resize">
......@@ -422,7 +422,7 @@
<request name="resize">
<arg name="input_device" type="object" interface="wl_input_device"/>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="edges" type="uint"/>
</request>
......@@ -517,7 +517,7 @@
</description>
<arg name="input_device" type="object" interface="wl_input_device"/>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="parent" type="object" interface="wl_shell_surface"/>
<arg name="x" type="int"/>
<arg name="y" type="int"/>
......@@ -547,7 +547,6 @@
received.
</description>
<arg name="time" type="uint"/>
<arg name="edges" type="uint"/>
<arg name="width" type="int"/>
<arg name="height" type="int"/>
......@@ -663,7 +662,7 @@
clients surfaces.
</description>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="buffer" type="object" interface="wl_buffer"/>
<arg name="hotspot_x" type="int"/>
<arg name="hotspot_y" type="int"/>
......@@ -686,6 +685,7 @@
of the click is given by the last motion or pointer_focus event.
</description>
<arg name="serial" type="uint"/>
<arg name="time" type="uint"/>
<arg name="button" type="uint"/>
<arg name="state" type="uint"/>
......@@ -712,6 +712,7 @@
A key was pressed or released.
</description>
<arg name="serial" type="uint"/>
<arg name="time" type="uint"/>
<arg name="key" type="uint"/>
<arg name="state" type="uint"/>
......@@ -725,7 +726,7 @@
event by setting an appropriate pointer image.
</description>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="surface" type="object" interface="wl_surface"/>
<arg name="surface_x" type="int"/>
<arg name="surface_y" type="int"/>
......@@ -734,22 +735,23 @@
<event name="pointer_leave">
<description summary="pointer leave event">
</description>
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="surface" type="object" interface="wl_surface"/>
</event>
<event name="keyboard_enter">
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="surface" type="object" interface="wl_surface"/>
<arg name="keys" type="array"/>
</event>
<event name="keyboard_leave">
<arg name="time" type="uint"/>
<arg name="serial" type="uint"/>
<arg name="surface" type="object" interface="wl_surface"/>
</event>
<event name="touch_down">
<arg name="serial" type="uint"/>
<arg name="time" type="uint"/>
<arg name="surface" type="object" interface="wl_surface"/>
<arg name="id" type="int" />
......@@ -758,6 +760,7 @@
</event>
<event name="touch_up">
<arg name="serial" type="uint"/>
<arg name="time" type="uint"/>
<arg name="id" type="int" />
</event>
......
......@@ -29,7 +29,7 @@
static void
data_offer_accept(struct wl_client *client, struct wl_resource *resource,
uint32_t time, const char *mime_type)
uint32_t serial, const char *mime_type)
{
struct wl_data_offer *offer = resource->data;
......@@ -58,7 +58,7 @@ data_offer_receive(struct wl_client *client, struct wl_resource *resource,
static void
data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
{
wl_resource_destroy(resource, 0);
wl_resource_destroy(resource);
}
static void
......@@ -78,7 +78,7 @@ static const struct wl_data_offer_interface data_offer_interface = {
static void
destroy_offer_data_source(struct wl_listener *listener,
struct wl_resource *resource, uint32_t time)
struct wl_resource *resource)
{
struct wl_data_offer *offer;
......@@ -146,7 +146,7 @@ data_source_offer(struct wl_client *client,
static void
data_source_destroy(struct wl_client *client, struct wl_resource *resource)
{
wl_resource_destroy(resource, 0);
wl_resource_destroy(resource);
}
static struct wl_data_source_interface data_source_interface = {
......@@ -169,7 +169,7 @@ find_resource(struct wl_list *list, struct wl_client *client)
static void
destroy_drag_focus(struct wl_listener *listener,
struct wl_resource *resource, uint32_t time)
struct wl_resource *resource)
{
struct wl_input_device *device =
container_of(listener, struct wl_input_device,
......@@ -179,12 +179,14 @@ destroy_drag_focus(struct wl_listener *listener,
}
static void
drag_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
drag_grab_focus(struct wl_pointer_grab *grab,
struct wl_surface *surface, int32_t x, int32_t y)
{
struct wl_input_device *device =
container_of(grab, struct wl_input_device, drag_grab);
struct wl_resource *resource, *offer;
struct wl_display *display;
uint32_t serial;
if (device->drag_focus_resource) {
wl_data_device_send_leave(device->drag_focus_resource);
......@@ -197,10 +199,13 @@ drag_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
resource = find_resource(&device->drag_resource_list,
surface->resource.client);
if (surface && resource) {
display = wl_client_get_display(resource->client);
serial = wl_display_next_serial(display);
offer = wl_data_source_send_offer(device->drag_data_source,
resource);
wl_data_device_send_enter(resource, time, &surface->resource,
wl_data_device_send_enter(resource, serial, &surface->resource,
x, y, offer);
device->drag_focus = surface;
......@@ -225,7 +230,7 @@ drag_grab_motion(struct wl_pointer_grab *grab,
}
static void
data_device_end_drag_grab(struct wl_input_device *device, uint32_t time)
data_device_end_drag_grab(struct wl_input_device *device)
{
struct wl_resource *surface_resource;
struct wl_surface_interface *implementation;
......@@ -240,7 +245,7 @@ data_device_end_drag_grab(struct wl_input_device *device, uint32_t time)
wl_list_remove(&device->drag_icon_listener.link);
}
wl_input_device_end_pointer_grab(device, time);
wl_input_device_end_pointer_grab(device);
device->drag_data_source = NULL;
device->drag_surface = NULL;
......@@ -258,7 +263,7 @@ drag_grab_button(struct wl_pointer_grab *grab,
wl_data_device_send_drop(device->drag_focus_resource);
if (device->button_count == 0 && state == 0) {
data_device_end_drag_grab(device, time);
data_device_end_drag_grab(device);
wl_list_remove(&device->drag_data_source_listener.link);
}
}
......@@ -271,19 +276,19 @@ static const struct wl_pointer_grab_interface drag_grab_interface = {
static void
destroy_data_device_source(struct wl_listener *listener,
struct wl_resource *resource, uint32_t time)
struct wl_resource *resource)
{
struct wl_input_device *device;
device = container_of(listener, struct wl_input_device,
drag_data_source_listener);
data_device_end_drag_grab(device, time);
data_device_end_drag_grab(device);
}
static void
destroy_data_device_icon(struct wl_listener *listener,
struct wl_resource *resource, uint32_t time)
struct wl_resource *resource)
{
struct wl_input_device *device;
......@@ -297,7 +302,7 @@ static void
data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
struct wl_resource *source_resource,
struct wl_resource *origin_resource,
struct wl_resource *icon_resource, uint32_t time)
struct wl_resource *icon_resource, uint32_t serial)
{
struct wl_input_device *device = resource->data;
struct wl_listener *listener, *tmp;
......@@ -322,15 +327,15 @@ data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
wl_list_for_each_safe(listener, tmp,
&device->drag_icon_listener_list, link)
listener->func(listener, icon_resource, time);
listener->func(listener, icon_resource);
}
wl_input_device_start_pointer_grab(device, &device->drag_grab, time);
wl_input_device_start_pointer_grab(device, &device->drag_grab);
}
static void
destroy_selection_data_source(struct wl_listener *listener,
struct wl_resource *resource, uint32_t time)
struct wl_resource *resource)
{
struct wl_input_device *device =
container_of(listener, struct wl_input_device,
......@@ -349,7 +354,7 @@ destroy_selection_data_source(struct wl_listener *listener,
WL_EXPORT void
wl_input_device_set_selection(struct wl_input_device *device,
struct wl_data_source *source, uint32_t time)
struct wl_data_source *source)
{
struct wl_resource *data_device, *focus, *offer;
struct wl_selection_listener *listener, *next;
......@@ -386,13 +391,13 @@ wl_input_device_set_selection(struct wl_input_device *device,
static void
data_device_set_selection(struct wl_client *client,
struct wl_resource *resource,
struct wl_resource *source_resource, uint32_t time)
struct wl_resource *source_resource, uint32_t serial)
{
if (!source_resource)
return;
wl_input_device_set_selection(resource->data,
source_resource->data, time);
/* FIXME: Store serial and check against incoming serial here. */
wl_input_device_set_selection(resource->data, source_resource->data);
}
static const struct wl_data_device_interface data_device_interface = {
......
......@@ -433,7 +433,7 @@ wl_display_get_fd(struct wl_display *display,
}
static void
sync_callback(void *data, struct wl_callback *callback, uint32_t time)
sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
{
int *done = data;
......
......@@ -72,6 +72,7 @@ struct wl_display {
int run;
uint32_t id;
uint32_t serial;
struct wl_list global_list;
struct wl_list socket_list;
......@@ -382,23 +383,22 @@ destroy_resource(void *element, void *data)
{
struct wl_resource *resource = element;
struct wl_listener *l, *next;
uint32_t *time = data;
wl_list_for_each_safe(l, next, &resource->destroy_listener_list, link)
l->func(l, resource, *time);
l->func(l, resource);
if (resource->destroy)
resource->destroy(resource);
}
WL_EXPORT void
wl_resource_destroy(struct wl_resource *resource, uint32_t time)
wl_resource_destroy(struct wl_resource *resource)
{
struct wl_client *client = resource->client;
uint32_t id;
id = resource->object.id;
destroy_resource(resource, &time);
destroy_resource(resource, NULL);
if (id < WL_SERVER_ID_START) {
if (client->display_resource) {
......@@ -414,12 +414,12 @@ wl_resource_destroy(struct wl_resource *resource, uint32_t time)
WL_EXPORT void
wl_client_destroy(struct wl_client *client)
{
uint32_t time = 0;
uint32_t serial = 0;
printf("disconnect from client %p\n", client);
wl_client_flush(client);
wl_map_for_each(&client->objects, destroy_resource, &time);
wl_map_for_each(&client->objects, destroy_resource, &serial);
wl_map_release(&client->objects);
wl_event_source_remove(client->source);
wl_connection_destroy(client->connection);
......@@ -429,7 +429,7 @@ wl_client_destroy(struct wl_client *client)
static void
lose_pointer_focus(struct wl_listener *listener,
struct wl_resource *resource, uint32_t time)
struct wl_resource *resource)
{
struct wl_input_device *device =
container_of(listener, struct wl_input_device,
......@@ -440,7 +440,7 @@ lose_pointer_focus(struct wl_listener *listener,
static void
lose_keyboard_focus(struct wl_listener *listener,
struct wl_resource *resource, uint32_t time)
struct wl_resource *resource)
{
struct wl_input_device *device =
container_of(listener, struct wl_input_device,
......@@ -450,7 +450,7 @@ lose_keyboard_focus(struct wl_listener *listener,
}
static void
default_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
default_grab_focus(struct wl_pointer_grab *grab,
struct wl_surface *surface, int32_t x, int32_t y)
{
struct wl_input_device *device = grab->input_device;
......@@ -458,7 +458,7 @@ default_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
if (device->button_count > 0)
return;
wl_input_device_set_pointer_focus(device, surface, time, x, y);
wl_input_device_set_pointer_focus(device, surface, x, y);
}
static void
......@@ -478,14 +478,18 @@ default_grab_button(struct wl_pointer_grab *grab,
{
struct wl_input_device *device = grab->input_device;
struct wl_resource *resource;
uint32_t serial;
resource = device->pointer_focus_resource;
if (resource)
wl_input_device_send_button(resource, time, button, state);
if (resource) {
serial = wl_display_next_serial(resource->client->display);
wl_input_device_send_button(resource, serial, time,
button, state);
}
if (device->button_count == 0 && state == 0)
wl_input_device_set_pointer_focus(device,
device->current, time,
device->current,
device->current_x,
device->current_y);
}
......@@ -499,14 +503,17 @@ static const struct wl_pointer_grab_interface
static void
default_grab_key(struct wl_keyboard_grab *grab,
uint32_t time, uint32_t key, int32_t state)
uint32_t time, uint32_t key, int32_t state)
{
struct wl_input_device *device = grab->input_device;
struct wl_resource *resource;
uint32_t serial;
resource = device->keyboard_focus_resource;
if (resource)
wl_input_device_send_key(resource, time, key, state);
if (resource) {
serial = wl_display_next_serial(resource->client->display);
wl_input_device_send_key(resource, serial, time, key, state);
}
}
static const struct wl_keyboard_grab_interface
......@@ -574,70 +581,75 @@ find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
WL_EXPORT void
wl_input_device_set_pointer_focus(struct wl_input_device *device,
struct wl_surface *surface,
uint32_t time,
int32_t sx, int32_t sy)
{
struct wl_resource *resource;
uint32_t serial;
if (device->pointer_focus == surface)
return;
if (device->pointer_focus_resource) {
wl_input_device_send_pointer_leave(
device->pointer_focus_resource,
time, &device->pointer_focus->resource);
resource = device->pointer_focus_resource;
if (resource) {
serial = wl_display_next_serial(resource->client->display);
wl_input_device_send_pointer_leave(resource, serial,
&device->pointer_focus->resource);
wl_list_remove(&device->pointer_focus_listener.link);
}
resource = find_resource_for_surface(&device->resource_list, surface);
if (resource) {
wl_input_device_send_pointer_enter(resource, time,
serial = wl_display_next_serial(resource->client->display);
wl_input_device_send_pointer_enter(resource, serial,
&surface->resource,
sx, sy);
wl_list_insert(resource->destroy_listener_list.prev,
&device->pointer_focus_listener.link);
device->pointer_focus_serial = serial;
}
device->pointer_focus_resource = resource;
device->pointer_focus = surface;
device->pointer_focus_time = time;
device->default_pointer_grab.focus = surface;
}
WL_EXPORT void
wl_input_device_set_keyboard_focus(struct wl_input_device *device,
struct wl_surface *surface,
uint32_t time)
struct wl_surface *surface)
{
struct wl_resource *resource;
uint32_t serial;
if (device->keyboard_focus == surface)
return;
if (device->keyboard_focus_resource) {
wl_input_device_send_keyboard_leave(
device->keyboard_focus_resource,
time, &device->keyboard_focus->resource);
resource = device->keyboard_focus_resource;
serial = wl_display_next_serial(resource->client->display);
wl_input_device_send_keyboard_leave(resource, serial,
&device->keyboard_focus->resource);
wl_list_remove(&device->keyboard_focus_listener.link);
}
resource = find_resource_for_surface(&device->resource_list, surface);
if (resource) {
wl_input_device_send_keyboard_enter(resource, time,
serial = wl_display_next_serial(resource->client->display);
wl_input_device_send_keyboard_enter(resource, serial,
&surface->resource,
&device->keys);
wl_list_insert(resource->destroy_listener_list.prev,
&device->keyboard_focus_listener.link);
device->keyboard_focus_serial = serial;
}
device->keyboard_focus_resource = resource;
device->keyboard_focus = surface;
device->keyboard_focus_time = time;
}
WL_EXPORT void
wl_input_device_start_keyboard_grab(struct wl_input_device *device,
struct wl_keyboard_grab *grab, uint32_t time)
struct wl_keyboard_grab *grab)
{
device->keyboard_grab = grab;
grab->input_device = device;
......@@ -645,14 +657,14 @@ wl_input_device_start_keyboard_grab(struct wl_input_device *device,
}
WL_EXPORT void
wl_input_device_end_keyboard_grab(struct wl_input_device *device, uint32_t time)
wl_input_device_end_keyboard_grab(struct wl_input_device *device)
{
device->keyboard_grab = &device->default_keyboard_grab;
}
WL_EXPORT void
wl_input_device_start_pointer_grab(struct wl_input_device *device,
struct wl_pointer_grab *grab, uint32_t time)
struct wl_pointer_grab *grab)
{
const struct wl_pointer_grab_interface *interface;
......@@ -661,18 +673,18 @@ wl_input_device_start_pointer_grab(struct wl_input_device *device,
grab->input_device = device;
if (device->current)
interface->focus(device->pointer_grab, time, device->current,
interface->focus(device->pointer_grab, device->current,
device->current_x, device->current_y);
}
WL_EXPORT void
wl_input_device_end_pointer_grab(struct wl_input_device *device, uint32_t time)
wl_input_device_end_pointer_grab(struct wl_input_device *device)
{
const struct wl_pointer_grab_interface *interface;
device->pointer_grab = &device->default_pointer_grab;
interface = device->pointer_grab->interface;
interface->focus(device->pointer_grab, time, device->current,
interface->focus(device->pointer_grab, device->current,
device->current_x, device->current_y);
}
......@@ -701,11 +713,13 @@ display_sync(struct wl_client *client,
struct wl_resource *resource, uint32_t id)
{
struct wl_resource *callback;
uint32_t serial;
callback = wl_client_add_object(client,
&wl_callback_interface, NULL, id, NULL);
wl_callback_send_done(callback, 0);
wl_resource_destroy(callback, 0);
serial = wl_display_get_serial(client->display);
wl_callback_send_done(callback, serial);
wl_resource_destroy(callback);
}
struct wl_display_interface display_interface = {
......@@ -830,6 +844,20 @@ wl_display_remove_global(struct wl_display *display, struct wl_global *global)
free(global);
}
WL_EXPORT uint32_t
wl_display_get_serial(struct wl_display *display)
{
return display->serial;
}
WL_EXPORT uint32_t
wl_display_next_serial(struct wl_display *display)
{
display->serial++;
return display->serial;
}
WL_EXPORT struct wl_event_loop *
wl_display_get_event_loop(struct wl_display *display)
{
......
......@@ -96,6 +96,9 @@ struct wl_global *wl_display_add_global(struct wl_display *display,
void wl_display_remove_global(struct wl_display *display,
struct wl_global *global);
uint32_t wl_display_get_serial(struct wl_display *display);
uint32_t wl_display_next_serial(struct wl_display *display);
struct wl_client *wl_client_create(struct wl_display *display, int fd);
void wl_client_destroy(struct wl_client *client);
void wl_client_flush(struct wl_client *client);
......@@ -129,7 +132,7 @@ struct wl_buffer {
struct wl_listener {
struct wl_list link;