Codebase list wlroots / 1c8b72e
Add screenshooter skeleton emersion 6 years ago
6 changed file(s) with 315 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
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 }
0 #ifndef _WLR_SCREENSHOOTER_H
1 #define _WLR_SCREENSHOOTER_H
2 #include <wayland-server.h>
3
4 struct wlr_screenshooter {
5 struct wl_global *wl_global;
6
7 void *data;
8 };
9
10 struct wlr_screenshot {
11 struct wl_resource *resource;
12 struct wl_resource *output;
13
14 void* data;
15 };
16
17 struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display);
18 void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter);
19
20 #endif
2121
2222 protocols = [
2323 [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'],
24 'gamma-control.xml'
24 'gamma-control.xml',
25 'screenshooter.xml',
2526 ]
2627
2728 client_protocols = [
28 [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml']
29 [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'],
2930 ]
3031
3132 wl_protos_src = []
0 <protocol name="orbital_screenshooter">
1
2 <interface name="orbital_screenshooter" version="1">
3 <request name="shoot">
4 <arg name="id" type="new_id" interface="orbital_screenshot"/>
5 <arg name="output" type="object" interface="wl_output"/>
6 <arg name="buffer" type="object" interface="wl_buffer"/>
7 </request>
8 </interface>
9
10 <interface name="orbital_screenshot" version="1">
11 <event name="done">
12 </event>
13 </interface>
14
15 </protocol>
1919 'wlr_compositor.c',
2020 'wlr_box.c',
2121 'wlr_gamma_control.c',
22 'wlr_screenshooter.c',
2223 ),
2324 include_directories: wlr_inc,
2425 dependencies: [wayland_server, pixman, wlr_protos],
0 #include <assert.h>
1 #include <stdlib.h>
2 #include <wayland-server.h>
3 #include <wlr/types/wlr_screenshooter.h>
4 #include <wlr/types/wlr_output.h>
5 #include <wlr/util/log.h>
6 #include "screenshooter-protocol.h"
7
8 static void screenshooter_shoot(struct wl_client *client,
9 struct wl_resource *_screenshooter, uint32_t id,
10 struct wl_resource *_output, struct wl_resource *_buffer) {
11 struct wlr_screenshot *screenshot;
12 if (!(screenshot = calloc(1, sizeof(struct wlr_screenshot)))) {
13 return;
14 }
15 screenshot->output = _output;
16 screenshot->resource = wl_resource_create(client,
17 &orbital_screenshot_interface, wl_resource_get_version(_screenshooter), id);
18 wlr_log(L_DEBUG, "new screenshot %p (res %p)", screenshot, screenshot->resource);
19 wl_resource_set_implementation(screenshot->resource, NULL, screenshot, NULL);
20 // TODO: orbital_screenshot_send_done(screenshot->resource);
21 }
22
23 static struct orbital_screenshooter_interface screenshooter_impl = {
24 .shoot = screenshooter_shoot,
25 };
26
27 static void screenshooter_bind(struct wl_client *wl_client,
28 void *_screenshooter, uint32_t version, uint32_t id) {
29 struct wlr_screenshooter *screenshooter = _screenshooter;
30 assert(wl_client && screenshooter);
31 if (version > 1) {
32 wlr_log(L_ERROR, "Client requested unsupported screenshooter version, disconnecting");
33 wl_client_destroy(wl_client);
34 return;
35 }
36 struct wl_resource *wl_resource = wl_resource_create(wl_client,
37 &orbital_screenshooter_interface, version, id);
38 wl_resource_set_implementation(wl_resource, &screenshooter_impl,
39 screenshooter, NULL);
40 }
41
42 struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display) {
43 struct wlr_screenshooter *screenshooter =
44 calloc(1, sizeof(struct wlr_screenshooter));
45 if (!screenshooter) {
46 return NULL;
47 }
48 struct wl_global *wl_global = wl_global_create(display,
49 &orbital_screenshooter_interface, 1, screenshooter, screenshooter_bind);
50 if (!wl_global) {
51 free(screenshooter);
52 return NULL;
53 }
54 screenshooter->wl_global = wl_global;
55 return screenshooter;
56 }
57
58 void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter) {
59 if (!screenshooter) {
60 return;
61 }
62 // TODO: this segfault (wl_display->registry_resource_list is not init)
63 // wl_global_destroy(screenshooter->wl_global);
64 free(screenshooter);
65 }