libei/src/libei-connection-setup.c
Peter Hutterer 03bc5a1d6d ei: handle server-sent interface versions correctly
When we receive a ei_connection_setup.interface_version event, update
the supported version number for that interface to the correct minimum.
2023-03-03 11:21:26 +10:00

188 lines
6.7 KiB
C

/* SPDX-License-Identifier: MIT */
/*
* Copyright © 2023 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN connection_setup WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <errno.h>
#include <stdbool.h>
#include "util-bits.h"
#include "util-macros.h"
#include "util-mem.h"
#include "util-io.h"
#include "util-strings.h"
#include "util-version.h"
#include "libei-private.h"
#include "ei-proto.h"
static void
ei_connection_setup_destroy(struct ei_connection_setup *connection_setup)
{
struct ei *ei = ei_connection_setup_get_context(connection_setup);
ei_unregister_object(ei, &connection_setup->proto_object);
}
OBJECT_IMPLEMENT_REF(ei_connection_setup);
OBJECT_IMPLEMENT_UNREF_CLEANUP(ei_connection_setup);
OBJECT_IMPLEMENT_GETTER(ei_connection_setup, user_data, void*);
OBJECT_IMPLEMENT_SETTER(ei_connection_setup, user_data, void*);
OBJECT_IMPLEMENT_GETTER_AS_REF(ei_connection_setup, proto_object, const struct brei_object *);
static
OBJECT_IMPLEMENT_CREATE(ei_connection_setup);
static
OBJECT_IMPLEMENT_PARENT(ei_connection_setup, ei);
struct ei*
ei_connection_setup_get_context(struct ei_connection_setup *connection_setup)
{
assert(connection_setup);
return ei_connection_setup_parent(connection_setup);
}
uint32_t
ei_connection_setup_get_version(struct ei_connection_setup *connection_setup)
{
return connection_setup->proto_object.version;
}
static int
ei_connection_setup_initialize(struct ei_connection_setup *setup, uint32_t version)
{
struct ei *ei = ei_connection_setup_get_context(setup);
if (version >= EI_CONNECTION_SETUP_REQUEST_CONTEXT_TYPE_SINCE_VERSION)
ei_connection_setup_request_context_type(setup,
ei->is_sender ?
EI_CONNECTION_SETUP_CONTEXT_TYPE_SENDER :
EI_CONNECTION_SETUP_CONTEXT_TYPE_RECEIVER);
if (version >= EI_CONNECTION_SETUP_REQUEST_NAME_SINCE_VERSION)
ei_connection_setup_request_name(setup, ei->name);
if (version >= EI_CONNECTION_SETUP_REQUEST_INTERFACE_VERSION_SINCE_VERSION) {
struct ei_interface_versions *v = &ei->interface_versions;
ei_connection_setup_request_interface_version(setup, "ei_connection_setup", v->ei_connection_setup);
ei_connection_setup_request_interface_version(setup, "ei_connection", v->ei_connection);
ei_connection_setup_request_interface_version(setup, "ei_callback", v->ei_callback);
ei_connection_setup_request_interface_version(setup, "ei_pingpong", v->ei_pingpong);
ei_connection_setup_request_interface_version(setup, "ei_seat", v->ei_seat);
ei_connection_setup_request_interface_version(setup, "ei_device", v->ei_device);
ei_connection_setup_request_interface_version(setup, "ei_pointer", v->ei_pointer);
ei_connection_setup_request_interface_version(setup, "ei_keyboard", v->ei_keyboard);
ei_connection_setup_request_interface_version(setup, "ei_touchscreen", v->ei_touchscreen);
}
ei_connection_setup_request_done(setup);
return 0;
}
static int
handle_msg_interface_version(struct ei_connection_setup *setup, const char *name, uint32_t version)
{
struct ei *ei = ei_connection_setup_get_context(setup);
struct ei_interface_versions *v = &ei->interface_versions;
if (streq(name, "ei_connection_setup")) {
uint32_t min_version = min(version, ei->interface_versions.ei_connection_setup);
v->ei_connection_setup = min_version;
/* Now upgrade our protocol object to the server version (if applicable) */
setup->proto_object.version = min_version;
/* Now send all the bits we need to send */
ei_connection_setup_initialize(setup, min_version);
}
#define VERSION_UPDATE(iface_) if (streq(name, #iface_)) v->iface_ = min(version, v->iface_);
else VERSION_UPDATE(ei_connection)
else VERSION_UPDATE(ei_callback)
else VERSION_UPDATE(ei_pingpong)
else VERSION_UPDATE(ei_seat)
else VERSION_UPDATE(ei_device)
else VERSION_UPDATE(ei_pointer)
else VERSION_UPDATE(ei_keyboard)
else VERSION_UPDATE(ei_touchscreen)
#undef VERSION_UPDATE
return 0;
}
static void
connected(struct ei_connection *connection, void *user_data)
{
struct ei *ei = ei_connection_get_context(connection);
/* If we get here, the server didn't immediately disconnect us */
if (ei->state == EI_STATE_DISCONNECTED)
return;
ei_connected(ei);
}
static int
handle_msg_connection(struct ei_connection_setup *setup, uint32_t id, uint32_t version)
{
struct ei *ei = ei_connection_setup_get_context(setup);
assert(setup == ei->connection_setup);
/* we're done with our connection setup, drop it */
ei_connection_setup_unref(steal(&ei->connection_setup));
ei->connection = ei_connection_new(ei, id, version);
ei->state = EI_STATE_CONNECTING;
/* Send a sync on the connection - EIS should immediately send a
* disconnect event where applicable, so if we get through to our
* sync callback, we didn't immediately get disconnected */
ei_connection_sync(ei->connection, connected, NULL);
return 0;
}
static const struct ei_connection_setup_interface interface = {
.interface_version = handle_msg_interface_version,
.connection = handle_msg_connection,
};
const struct ei_connection_setup_interface *
ei_connection_setup_get_interface(struct ei_connection_setup *connection_setup) {
return &interface;
}
struct ei_connection_setup *
ei_connection_setup_new(struct ei *ei, uint32_t version)
{
struct ei_connection_setup *connection_setup = ei_connection_setup_create(&ei->object);
connection_setup->proto_object.id = ei_get_new_id(ei);
assert(connection_setup->proto_object.id == 0); /* Special object */
connection_setup->proto_object.implementation = connection_setup;
connection_setup->proto_object.interface = &ei_connection_setup_proto_interface;
connection_setup->proto_object.version = version;
ei_register_object(ei, &connection_setup->proto_object);
return connection_setup; /* ref owned by caller */
}