libei/src/libei-connection-setup.c
2023-03-03 11:21:26 +10:00

167 lines
5.8 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_TYPE_SINCE_VERSION)
ei_connection_setup_request_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) {
ei_connection_setup_request_interface_version(setup, "ei_connection_setup", VERSION_V(1));
ei_connection_setup_request_interface_version(setup, "ei_connection", VERSION_V(1));
ei_connection_setup_request_interface_version(setup, "ei_callback", VERSION_V(1));
ei_connection_setup_request_interface_version(setup, "ei_seat", VERSION_V(1));
ei_connection_setup_request_interface_version(setup, "ei_device", VERSION_V(1));
ei_connection_setup_request_interface_version(setup, "ei_pointer", VERSION_V(1));
ei_connection_setup_request_interface_version(setup, "ei_keyboard", VERSION_V(1));
ei_connection_setup_request_interface_version(setup, "ei_touchscreen", VERSION_V(1));
}
ei_connection_setup_request_done(setup);
return 0;
}
static int
handle_msg_version(struct ei_connection_setup *setup, uint32_t version)
{
uint32_t min_version = min(version, setup->proto_object.version);
setup->proto_object.version = min_version;
/* Now send all the bits we need to send */
ei_connection_setup_initialize(setup, version);
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 = {
.version = handle_msg_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 */
}