|
0 |
#define _POSIX_C_SOURCE 199309L
|
|
1 |
#include <string.h>
|
|
2 |
#include <stdio.h>
|
|
3 |
#include <stdlib.h>
|
|
4 |
#include <time.h>
|
|
5 |
#include <inttypes.h>
|
|
6 |
#include <unistd.h>
|
|
7 |
#include <sys/mman.h>
|
|
8 |
#include <wayland-server.h>
|
|
9 |
#include <wlr/backend.h>
|
|
10 |
#include <wlr/backend/session.h>
|
|
11 |
#include <wlr/render.h>
|
|
12 |
#include <wlr/render/matrix.h>
|
|
13 |
#include <wlr/render/gles2.h>
|
|
14 |
#include <wlr/types/wlr_output.h>
|
|
15 |
#include <wlr/types/wlr_surface.h>
|
|
16 |
#include <wlr/types/wlr_wl_shell.h>
|
|
17 |
#include <wlr/types/wlr_xdg_shell_v6.h>
|
|
18 |
#include <wlr/types/wlr_seat.h>
|
|
19 |
#include <wlr/types/wlr_data_device_manager.h>
|
|
20 |
#include <wlr/types/wlr_gamma_control.h>
|
|
21 |
#include <wlr/types/wlr_screenshooter.h>
|
|
22 |
#include "wlr/types/wlr_compositor.h"
|
|
23 |
#include <wlr/xwayland.h>
|
|
24 |
#include <xkbcommon/xkbcommon.h>
|
|
25 |
#include <wlr/util/log.h>
|
|
26 |
#include "shared.h"
|
|
27 |
|
|
28 |
// TODO: move to common header?
|
|
29 |
int os_create_anonymous_file(off_t size);
|
|
30 |
|
|
31 |
struct sample_state {
|
|
32 |
struct wlr_renderer *renderer;
|
|
33 |
struct wlr_compositor *wlr_compositor;
|
|
34 |
struct wlr_wl_shell *wl_shell;
|
|
35 |
struct wlr_seat *wl_seat;
|
|
36 |
struct wlr_xdg_shell_v6 *xdg_shell;
|
|
37 |
struct wlr_data_device_manager *data_device_manager;
|
|
38 |
struct wl_resource *focus;
|
|
39 |
struct wl_listener keyboard_bound;
|
|
40 |
struct wlr_xwayland *xwayland;
|
|
41 |
struct wlr_gamma_control_manager *gamma_control_manager;
|
|
42 |
struct wlr_screenshooter *screenshooter;
|
|
43 |
int keymap_fd;
|
|
44 |
size_t keymap_size;
|
|
45 |
uint32_t serial;
|
|
46 |
};
|
|
47 |
|
|
48 |
/*
|
|
49 |
* Convert timespec to milliseconds
|
|
50 |
*/
|
|
51 |
static inline int64_t timespec_to_msec(const struct timespec *a) {
|
|
52 |
return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
|
|
53 |
}
|
|
54 |
|
|
55 |
static void output_frame_handle_surface(struct sample_state *sample,
|
|
56 |
struct wlr_output *wlr_output, struct timespec *ts,
|
|
57 |
struct wl_resource *_res) {
|
|
58 |
struct wlr_surface *surface = wl_resource_get_user_data(_res);
|
|
59 |
float matrix[16];
|
|
60 |
float transform[16];
|
|
61 |
wlr_surface_flush_damage(surface);
|
|
62 |
if (surface->texture->valid) {
|
|
63 |
wlr_matrix_translate(&transform, 200, 200, 0);
|
|
64 |
wlr_surface_get_matrix(surface, &matrix,
|
|
65 |
&wlr_output->transform_matrix, &transform);
|
|
66 |
wlr_render_with_matrix(sample->renderer, surface->texture, &matrix);
|
|
67 |
|
|
68 |
struct wlr_frame_callback *cb, *cnext;
|
|
69 |
wl_list_for_each_safe(cb, cnext, &surface->frame_callback_list, link) {
|
|
70 |
wl_callback_send_done(cb->resource, timespec_to_msec(ts));
|
|
71 |
wl_resource_destroy(cb->resource);
|
|
72 |
}
|
|
73 |
}
|
|
74 |
}
|
|
75 |
static void handle_output_frame(struct output_state *output, struct timespec *ts) {
|
|
76 |
struct compositor_state *state = output->compositor;
|
|
77 |
struct sample_state *sample = state->data;
|
|
78 |
struct wlr_output *wlr_output = output->output;
|
|
79 |
|
|
80 |
wlr_output_make_current(wlr_output);
|
|
81 |
wlr_renderer_begin(sample->renderer, wlr_output);
|
|
82 |
|
|
83 |
struct wlr_wl_shell_surface *wl_shell_surface;
|
|
84 |
wl_list_for_each(wl_shell_surface, &sample->wl_shell->surfaces, link) {
|
|
85 |
output_frame_handle_surface(sample, wlr_output, ts, wl_shell_surface->surface);
|
|
86 |
}
|
|
87 |
struct wlr_xdg_surface_v6 *xdg_surface;
|
|
88 |
wl_list_for_each(xdg_surface, &sample->xdg_shell->surfaces, link) {
|
|
89 |
output_frame_handle_surface(sample, wlr_output, ts, xdg_surface->surface);
|
|
90 |
}
|
|
91 |
struct wlr_x11_window *x11_window;
|
|
92 |
wl_list_for_each(x11_window, &sample->xwayland->displayable_windows, link) {
|
|
93 |
output_frame_handle_surface(sample, wlr_output, ts, x11_window->surface);
|
|
94 |
}
|
|
95 |
|
|
96 |
wlr_renderer_end(sample->renderer);
|
|
97 |
wlr_output_swap_buffers(wlr_output);
|
|
98 |
}
|
|
99 |
|
|
100 |
static void handle_keyboard_key(struct keyboard_state *keyboard, uint32_t keycode,
|
|
101 |
xkb_keysym_t sym, enum wlr_key_state key_state) {
|
|
102 |
struct compositor_state *state = keyboard->compositor;
|
|
103 |
struct sample_state *sample = state->data;
|
|
104 |
|
|
105 |
struct wl_resource *res = NULL;
|
|
106 |
struct wlr_seat_handle *seat_handle = NULL;
|
|
107 |
wl_list_for_each(res, &sample->wlr_compositor->surfaces, link) {
|
|
108 |
break;
|
|
109 |
}
|
|
110 |
|
|
111 |
if (res) {
|
|
112 |
seat_handle = wlr_seat_handle_for_client(sample->wl_seat,
|
|
113 |
wl_resource_get_client(res));
|
|
114 |
}
|
|
115 |
|
|
116 |
if (res != sample->focus && seat_handle && seat_handle->keyboard) {
|
|
117 |
struct wl_array keys;
|
|
118 |
wl_array_init(&keys);
|
|
119 |
wl_keyboard_send_enter(seat_handle->keyboard, ++sample->serial, res, &keys);
|
|
120 |
sample->focus = res;
|
|
121 |
}
|
|
122 |
|
|
123 |
if (seat_handle && seat_handle->keyboard) {
|
|
124 |
uint32_t depressed = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
125 |
XKB_STATE_MODS_DEPRESSED);
|
|
126 |
uint32_t latched = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
127 |
XKB_STATE_MODS_LATCHED);
|
|
128 |
uint32_t locked = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
129 |
XKB_STATE_MODS_LOCKED);
|
|
130 |
uint32_t group = xkb_state_serialize_layout(keyboard->xkb_state,
|
|
131 |
XKB_STATE_LAYOUT_EFFECTIVE);
|
|
132 |
wl_keyboard_send_modifiers(seat_handle->keyboard, ++sample->serial, depressed,
|
|
133 |
latched, locked, group);
|
|
134 |
wl_keyboard_send_key(seat_handle->keyboard, ++sample->serial, 0, keycode, key_state);
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
static void handle_keyboard_bound(struct wl_listener *listener, void *data) {
|
|
139 |
struct wlr_seat_handle *handle = data;
|
|
140 |
struct sample_state *state = wl_container_of(listener, state, keyboard_bound);
|
|
141 |
|
|
142 |
wl_keyboard_send_keymap(handle->keyboard, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
|
|
143 |
state->keymap_fd, state->keymap_size);
|
|
144 |
|
|
145 |
if (wl_resource_get_version(handle->keyboard) >= 2) {
|
|
146 |
wl_keyboard_send_repeat_info(handle->keyboard, 25, 600);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
int main() {
|
|
151 |
struct sample_state state = { 0 };
|
|
152 |
struct compositor_state compositor = { 0,
|
|
153 |
.data = &state,
|
|
154 |
.output_frame_cb = handle_output_frame,
|
|
155 |
};
|
|
156 |
compositor_init(&compositor);
|
|
157 |
|
|
158 |
state.renderer = wlr_gles2_renderer_create(compositor.backend);
|
|
159 |
if (!state.renderer) {
|
|
160 |
wlr_log(L_ERROR, "Could not start compositor, OOM");
|
|
161 |
exit(EXIT_FAILURE);
|
|
162 |
}
|
|
163 |
wl_display_init_shm(compositor.display);
|
|
164 |
state.wlr_compositor = wlr_compositor_create(compositor.display, state.renderer);
|
|
165 |
state.wl_shell = wlr_wl_shell_create(compositor.display);
|
|
166 |
state.xdg_shell = wlr_xdg_shell_v6_create(compositor.display);
|
|
167 |
state.data_device_manager = wlr_data_device_manager_create(compositor.display);
|
|
168 |
state.gamma_control_manager = wlr_gamma_control_manager_create(compositor.display);
|
|
169 |
state.screenshooter = wlr_screenshooter_create(compositor.display);
|
|
170 |
|
|
171 |
state.wl_seat = wlr_seat_create(compositor.display, "seat0");
|
|
172 |
state.keyboard_bound.notify = handle_keyboard_bound;
|
|
173 |
wl_signal_add(&state.wl_seat->events.keyboard_bound, &state.keyboard_bound);
|
|
174 |
wlr_seat_set_capabilities(state.wl_seat, WL_SEAT_CAPABILITY_KEYBOARD
|
|
175 |
| WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_TOUCH);
|
|
176 |
|
|
177 |
struct keyboard_state *kbstate;
|
|
178 |
wl_list_for_each(kbstate, &compositor.keyboards, link) {
|
|
179 |
char *keymap = xkb_keymap_get_as_string(kbstate->keymap,
|
|
180 |
XKB_KEYMAP_FORMAT_TEXT_V1);
|
|
181 |
state.keymap_size = strlen(keymap);
|
|
182 |
state.keymap_fd = os_create_anonymous_file(state.keymap_size);
|
|
183 |
void *ptr = mmap(NULL, state.keymap_size,
|
|
184 |
PROT_READ | PROT_WRITE,
|
|
185 |
MAP_SHARED, state.keymap_fd, 0);
|
|
186 |
strcpy(ptr, keymap);
|
|
187 |
free(keymap);
|
|
188 |
break;
|
|
189 |
}
|
|
190 |
state.xwayland = wlr_xwayland_create(compositor.display, state.wlr_compositor);
|
|
191 |
|
|
192 |
compositor.keyboard_key_cb = handle_keyboard_key;
|
|
193 |
|
|
194 |
wl_display_run(compositor.display);
|
|
195 |
|
|
196 |
wlr_xwayland_destroy(state.xwayland);
|
|
197 |
close(state.keymap_fd);
|
|
198 |
wlr_seat_destroy(state.wl_seat);
|
|
199 |
wlr_screenshooter_destroy(state.screenshooter);
|
|
200 |
wlr_gamma_control_manager_destroy(state.gamma_control_manager);
|
|
201 |
wlr_data_device_manager_destroy(state.data_device_manager);
|
|
202 |
wlr_xdg_shell_v6_destroy(state.xdg_shell);
|
|
203 |
wlr_wl_shell_destroy(state.wl_shell);
|
|
204 |
wlr_compositor_destroy(state.wlr_compositor);
|
|
205 |
wlr_renderer_destroy(state.renderer);
|
|
206 |
compositor_fini(&compositor);
|
|
207 |
}
|