This repository was archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsommelier-display.cc
157 lines (132 loc) · 6 KB
/
sommelier-display.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2018 The ChromiumOS Authors
// 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)
#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));
struct sl_global* global;
wl_list_for_each(global, &host->ctx->globals, link) {
if (global->name == name)
break;
}
// matches the behavior of libwayland's `registry_bind()`
if (!sl_client_supports_interface(host->ctx, client, global->interface)) {
wl_resource_post_error(resource, WL_DISPLAY_ERROR_IMPLEMENTATION,
"client doesn't support interface %s", interface);
} else if (&global->link == &host->ctx->globals) {
wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
"invalid global %s (%d)", interface, name);
} else if (strcmp(global->interface->name, interface) != 0) {
wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
"invalid interface for global %u: "
"have %s, wanted %s",
name, interface, global->interface->name);
} else if (version == 0) {
wl_resource_post_error(
resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
"invalid version for global %s (%d): 0 is not a valid version",
interface, name);
} else if (global->version < version) {
wl_resource_post_error(
resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
"invalid version for global %s (%d): have %d, wanted %d", interface,
name, global->version, version);
} else {
global->bind(client, global->data, version, id);
return;
}
// flush immediately on error before tearing down the session, otherwise
// client may never receive the descriptive error event.
wl_client_flush(client);
}
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));
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));
wl_callback_destroy(host->proxy);
wl_resource_set_user_data(resource, nullptr);
delete host;
}
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();
host_callback->resource =
wl_resource_create(client, &wl_callback_interface, 1, id);
wl_resource_set_implementation(host_callback->resource, nullptr,
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));
wl_list_remove(&host->link);
delete host;
}
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));
struct sl_global* global;
struct sl_host_registry* host_registry = new sl_host_registry();
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);
}
}
}
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,
nullptr);
return WL_ITERATOR_STOP;
}
return WL_ITERATOR_CONTINUE;
}
void sl_set_display_implementation(struct sl_context* ctx,
struct wl_client* client) {
// Find display resource and set implementation.
wl_client_for_each_resource(client, sl_set_implementation, ctx);
}