pH/sommelier/sommelier-display.cc

134 lines
4.8 KiB
C++
Raw Normal View History

// Copyright 2018 The ChromiumOS Authors
2019-09-20 18:57:28 -04:00
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sommelier.h" // NOLINT(build/include_directory)
#include "sommelier-tracing.h" // NOLINT(build/include_directory)
2019-09-20 18:57:28 -04:00
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client.h>
static void sl_registry_bind(struct wl_client* client,
struct wl_resource* resource,
uint32_t name,
const char* interface,
uint32_t version,
uint32_t id) {
TRACE_EVENT("display", "sl_registry_bind");
struct sl_host_registry* host =
static_cast<sl_host_registry*>(wl_resource_get_user_data(resource));
2019-09-20 18:57:28 -04:00
struct sl_global* global;
wl_list_for_each(global, &host->ctx->globals, link) {
if (global->name == name)
break;
}
assert(sl_client_supports_interface(host->ctx, client, global->interface));
2019-09-20 18:57:28 -04:00
assert(&global->link != &host->ctx->globals);
assert(version != 0);
assert(global->version >= version);
global->bind(client, global->data, version, id);
}
static const struct wl_registry_interface sl_registry_implementation = {
sl_registry_bind};
static void sl_sync_callback_done(void* data,
struct wl_callback* callback,
uint32_t serial) {
TRACE_EVENT("display", "sl_sync_callback_done");
struct sl_host_callback* host =
static_cast<sl_host_callback*>(wl_callback_get_user_data(callback));
2019-09-20 18:57:28 -04:00
wl_callback_send_done(host->resource, serial);
wl_resource_destroy(host->resource);
}
static const struct wl_callback_listener sl_sync_callback_listener = {
sl_sync_callback_done};
static void sl_host_callback_destroy(struct wl_resource* resource) {
struct sl_host_callback* host =
static_cast<sl_host_callback*>(wl_resource_get_user_data(resource));
2019-09-20 18:57:28 -04:00
wl_callback_destroy(host->proxy);
wl_resource_set_user_data(resource, NULL);
delete host;
2019-09-20 18:57:28 -04:00
}
static void sl_display_sync(struct wl_client* client,
struct wl_resource* resource,
uint32_t id) {
struct sl_context* ctx =
static_cast<sl_context*>(wl_resource_get_user_data(resource));
struct sl_host_callback* host_callback = new sl_host_callback();
2019-09-20 18:57:28 -04:00
host_callback->resource =
wl_resource_create(client, &wl_callback_interface, 1, id);
wl_resource_set_implementation(host_callback->resource, NULL, host_callback,
sl_host_callback_destroy);
host_callback->proxy = wl_display_sync(ctx->display);
wl_callback_add_listener(host_callback->proxy, &sl_sync_callback_listener,
host_callback);
}
static void sl_destroy_host_registry(struct wl_resource* resource) {
struct sl_host_registry* host =
static_cast<sl_host_registry*>(wl_resource_get_user_data(resource));
2019-09-20 18:57:28 -04:00
wl_list_remove(&host->link);
delete host;
2019-09-20 18:57:28 -04:00
}
static void sl_display_get_registry(struct wl_client* client,
struct wl_resource* resource,
uint32_t id) {
struct sl_context* ctx =
static_cast<sl_context*>(wl_resource_get_user_data(resource));
2019-09-20 18:57:28 -04:00
struct sl_global* global;
struct sl_host_registry* host_registry = new sl_host_registry();
2019-09-20 18:57:28 -04:00
host_registry->ctx = ctx;
host_registry->resource =
wl_resource_create(client, &wl_registry_interface, 1, id);
wl_list_insert(&ctx->registries, &host_registry->link);
wl_resource_set_implementation(host_registry->resource,
&sl_registry_implementation, host_registry,
sl_destroy_host_registry);
wl_list_for_each(global, &ctx->globals, link) {
if (sl_client_supports_interface(ctx, client, global->interface)) {
wl_resource_post_event(host_registry->resource, WL_REGISTRY_GLOBAL,
global->name, global->interface->name,
global->version);
}
2019-09-20 18:57:28 -04:00
}
}
static const struct wl_display_interface sl_display_implementation = {
sl_display_sync, sl_display_get_registry};
static enum wl_iterator_result sl_set_implementation(
struct wl_resource* resource, void* user_data) {
struct sl_context* ctx = (struct sl_context*)user_data;
if (strcmp(wl_resource_get_class(resource), "wl_display") == 0) {
wl_resource_set_implementation(resource, &sl_display_implementation, ctx,
NULL);
return WL_ITERATOR_STOP;
}
return WL_ITERATOR_CONTINUE;
}
void sl_set_display_implementation(struct sl_context* ctx,
struct wl_client* client) {
2019-09-20 18:57:28 -04:00
// Find display resource and set implementation.
wl_client_for_each_resource(client, sl_set_implementation, ctx);
2019-09-20 18:57:28 -04:00
}