-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwayland.c
229 lines (192 loc) · 6.48 KB
/
wayland.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#define _POSIX_C_SOURCE 200112L
#include <wayland-server-core.h>
#include <globals.h> // libswvkc-wl
#include <core/keyboard.h>
#include <core/wl_surface.h>
#include <core/wl_pointer.h>
#include <core/subsurface.h>
#include <extensions/linux-dmabuf-unstable-v1/zwp_linux_dmabuf_v1.h>
#include <extensions/linux-explicit-synchronization-v1/zwp_linux_surface_synchronization_v1.h>
#include <extensions/xdg_shell/xdg_surface.h>
#include <extensions/xdg_shell/xdg_toplevel.h>
#include <util/util.h>
#include "gbm_.h"
#include "atomic.h"
#include "modeset.h" // WARNING: probably bad design, try to decouple (if possible)
#include "legacy_wl_drm_.h"
#include <assert.h>
#include <stdlib.h>
static struct wl_display *D;
static struct keyboard * keyboard_g;
static struct surface* surface_g;
struct bufstate {
struct gbm_bo *bo;
uint32_t fb_id;
};
void wayland_init() {
D = wl_display_create();
assert(D);
setenv("XDG_RUNTIME_DIR", "/tmp", 0);
const char *socket = wl_display_add_socket_auto(D);
assert(socket);
setenv("WAYLAND_DISPLAY", socket, 0);
create_globals(D, 1, NULL);
legacy_wl_drm_init(D, gbm_get_device());
}
void wayland_flush() {
wl_display_flush_clients(D);
}
int wayland_get_fd() {
struct wl_event_loop *el = wl_display_get_event_loop(D);
assert(el);
return wl_event_loop_get_fd(el);
}
void wayland_read() {
struct wl_event_loop *el = wl_display_get_event_loop(D);
assert(el);
wl_event_loop_dispatch(el, 0);
// We don't assert its return code because the first time it returns -1...
}
void wayland_send_key_and_mods(uint32_t key, uint32_t state, unsigned int mods_depressed, unsigned int mods_latched, unsigned int mods_locked, unsigned int group) {
if (keyboard_g) {
keyboard_send_key(keyboard_g, key, state);
keyboard_send_modifiers(keyboard_g, mods_depressed, mods_latched, mods_locked, group);
}
}
static void shmbuf(struct wl_resource *buffer) {
struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get(buffer);
uint32_t width = wl_shm_buffer_get_width(shm_buffer);
uint32_t height = wl_shm_buffer_get_height(shm_buffer);
uint32_t stride = wl_shm_buffer_get_stride(shm_buffer);
//uint32_t format = wl_shm_buffer_get_format(shm_buffer);
uint8_t *data = wl_shm_buffer_get_data(shm_buffer);
wl_shm_buffer_begin_access(shm_buffer);
modeset_copy(stride, width, height, data);
wl_shm_buffer_end_access(shm_buffer);
wl_buffer_send_release(buffer);
}
static struct wl_resource *to_release[2];
static void dmabuf(struct wl_resource *dmabuf) {
struct bufstate *bufstate = wl_buffer_dmabuf_get_subsystem_object(dmabuf,
SUBSYSTEM_DRM);
atomic_commit(bufstate->fb_id);
if (!to_release[0])
to_release[0] = dmabuf;
else if (!to_release[1])
to_release[1] = dmabuf;
else
assert(false);
}
/*
* Handle events produced by Wayland objects: `object`_`event`_notify
*/
#include <stdio.h>
void surface_map_notify(struct surface *surface, void *user_data) {
if (surface->role == ROLE_CURSOR)
return;
if (surface->role == ROLE_SUBSURFACE)
return;
struct wl_array array;
wl_array_init(&array); //Need the currently pressed keys
struct wl_client *client = wl_resource_get_client(surface->resource);
struct wl_resource *keyboard = NULL;
keyboard = util_wl_client_get_keyboard(client);
if (keyboard)
wl_keyboard_send_enter(keyboard, 0, surface->resource, &array);
struct wl_resource *pointer = NULL;
pointer = util_wl_client_get_pointer(client);
if (pointer) {
wl_pointer_send_enter(pointer, 1, surface->resource,
wl_fixed_from_int(0), wl_fixed_from_int(0));
if (wl_resource_get_version(pointer) >= WL_POINTER_FRAME_SINCE_VERSION)
wl_pointer_send_frame(pointer);
}
surface_g = surface;
}
void surface_unmap_notify(struct surface *surface, void *user_data) {
}
void surface_contents_update_notify(struct surface *surface, void *user_data) {
struct wl_resource *buffer = surface->current->buffer;
/*
* If the buffer has been detached, do nothing
*/
if (!buffer)
return;
if (surface->role == ROLE_CURSOR) {
// TODO
return;
}
if (wl_buffer_is_dmabuf(buffer)) {
dmabuf(buffer);
} else {
shmbuf(buffer);
// still not vblank driven here
if (surface && surface->frame) {
wl_callback_send_done(surface->frame, 0);
wl_resource_destroy(surface->frame);
surface->frame = 0;
}
}
}
void xdg_toplevel_init_notify(struct xdg_toplevel_data *xdg_toplevel, void
*user_data) {
struct wl_array array;
wl_array_init(&array);
int32_t *state1 = wl_array_add(&array, sizeof(int32_t));
*state1 = XDG_TOPLEVEL_STATE_ACTIVATED;
int32_t *state2 = wl_array_add(&array, sizeof(int32_t));
*state2 = XDG_TOPLEVEL_STATE_MAXIMIZED;
xdg_toplevel_send_configure(xdg_toplevel->resource, modeset_get_width(),
modeset_get_height(), &array);
xdg_surface_send_configure(xdg_toplevel->xdg_surface_data->self, 0);
}
void buffer_dmabuf_create_notify(struct wl_buffer_dmabuf_data *dmabuf, void
*user_data) {
uint32_t num_planes = 1;
int32_t *fds = dmabuf->fds;
uint32_t width = dmabuf->width;
uint32_t height = dmabuf->height;
uint32_t format = dmabuf->format;
uint32_t *strides = dmabuf->strides;
uint32_t *offsets = dmabuf->offsets;
uint64_t *mods = dmabuf->modifiers;
struct gbm_bo *bo = gbm_import_from_dmabuf(num_planes, fds, width, height, format, strides, offsets, mods[0]);
// TODO: migrate to drmPrimeFDToHandle (but refcounting issues?)
assert(bo);
struct bufstate *bufstate = malloc(sizeof(*bufstate));
bufstate->bo = bo;
bufstate->fb_id = modeset_add_fb(width, height, format, gbm_get_handle(bo), strides[0], offsets[0], mods[0]);
dmabuf->subsystem_object[SUBSYSTEM_DRM] = bufstate;
}
void buffer_dmabuf_destroy_notify(struct wl_buffer_dmabuf_data *dmabuf, void
*user_data) {
struct bufstate *bufstate = dmabuf->subsystem_object[SUBSYSTEM_DRM];
// TODO: Not sure if we can destroy it immediately
modeset_rem_fb(bufstate->fb_id);
gbm_destroy(bufstate->bo);
free(bufstate);
}
#include "xkb.h"
void keyboard_init_notify(struct keyboard *keyboard, void *user_data) {
int32_t fd = xkb_get_keymap_fd();
uint32_t size = xkb_get_keymap_size();
keyboard_send_keymap(keyboard, fd, size);
keyboard_g = keyboard; //WARNING
}
void page_flip_handler(int fd, unsigned int sequence, unsigned int
tv_sec, unsigned int tv_usec, void *user_data) {
if (surface_g && surface_g->frame) {
uint32_t ms = tv_sec * 1000 + tv_usec / 1000;
wl_callback_send_done(surface_g->frame, ms);
wl_resource_destroy(surface_g->frame);
surface_g->frame = 0;
}
if (to_release[1]) {
wl_buffer_send_release(to_release[1]);
to_release[1] = NULL;
}
if (to_release[0]) {
to_release[1] = to_release[0];
to_release[0] = NULL;
}
}