peer.c (12454B)
1 /* swc: libswc/peer.c 2 * 3 * the screen drawn in software into shared memory and shown by a peer, which 4 * sends back the input: a program of another system holding the real display, 5 * such as an application of it. see peer_protocol.h 6 */ 7 8 #include "peer.h" 9 #include "backend.h" 10 #include "compositor.h" 11 #include "internal.h" 12 #include "keyboard.h" 13 #include "output.h" 14 #include "peer_protocol.h" 15 #include "pointer.h" 16 #include "screen.h" 17 #include "seat.h" 18 #include "surface.h" 19 #include "touch.h" 20 #include "util.h" 21 22 #include <errno.h> 23 #include <fcntl.h> 24 #include <pixman.h> 25 #include <stddef.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 #include <sys/mman.h> 30 #include <sys/socket.h> 31 #include <sys/un.h> 32 #include <unistd.h> 33 #include <wld/pixman.h> 34 #include <wld/wld.h> 35 36 /* a screen past this is a peer's mistake, not a size to allocate */ 37 #define SIZE_LIMIT 16384 38 39 static struct { 40 struct swc_backend backend; 41 int fd; 42 struct wl_event_source *source; 43 44 struct screen *screen; 45 uint32_t width, height; 46 /* the buffer shared with the peer, and what it has not been sent */ 47 uint32_t *pixels; 48 size_t size; 49 bool whole; 50 51 bool in_flight; /* a frame sent and not yet done with */ 52 bool frame_due; /* done, but the frame callback waits for it to be seen */ 53 bool shown; 54 55 /* a message read in part */ 56 struct swc_peer_message in; 57 size_t got; 58 } peer; 59 60 static bool 61 send_message(const struct swc_peer_message *message, int descriptor) 62 { 63 union { 64 struct cmsghdr header; 65 char space[CMSG_SPACE(sizeof(int))]; 66 } control; 67 struct iovec iov = { (void *)message, sizeof(*message) }; 68 struct msghdr msg = { 0 }; 69 struct cmsghdr *header; 70 size_t sent = 0; 71 ssize_t n; 72 73 msg.msg_iov = &iov; 74 msg.msg_iovlen = 1; 75 if (descriptor >= 0) { 76 msg.msg_control = control.space; 77 msg.msg_controllen = sizeof(control.space); 78 header = CMSG_FIRSTHDR(&msg); 79 header->cmsg_level = SOL_SOCKET; 80 header->cmsg_type = SCM_RIGHTS; 81 header->cmsg_len = CMSG_LEN(sizeof(int)); 82 memcpy(CMSG_DATA(header), &descriptor, sizeof(int)); 83 } 84 while (sent < sizeof(*message)) { 85 n = sendmsg(peer.fd, &msg, MSG_NOSIGNAL); 86 if (n < 0) { 87 if (errno == EINTR) { 88 continue; 89 } 90 ERROR("peer: cannot send: %s\n", strerror(errno)); 91 return false; 92 } 93 sent += (size_t)n; 94 iov.iov_base = (char *)message + sent; 95 iov.iov_len = sizeof(*message) - sent; 96 msg.msg_control = NULL; 97 msg.msg_controllen = 0; 98 } 99 return true; 100 } 101 102 /* a new buffer of width by height, handed to the peer */ 103 static bool 104 share_buffer(uint32_t width, uint32_t height) 105 { 106 struct swc_peer_message message = { SWC_PEER_BUFFER, { 0 } }; 107 size_t size = (size_t)width * height * 4; 108 uint32_t *pixels; 109 char path[32]; 110 int fd, shown = -1; 111 112 fd = memfd_create("swc-peer", MFD_CLOEXEC); 113 if (fd < 0 || ftruncate(fd, (off_t)size) < 0) { 114 ERROR("peer: cannot make the screen buffer: %s\n", strerror(errno)); 115 goto fail; 116 } 117 /* the peer only shows the buffer, so it gets a descriptor that cannot 118 * write it: a confined peer may be refused one that can */ 119 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); 120 shown = open(path, O_RDONLY | O_CLOEXEC); 121 if (shown < 0) { 122 ERROR("peer: cannot open the screen buffer to be read: %s\n", strerror(errno)); 123 goto fail; 124 } 125 pixels = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 126 if (pixels == MAP_FAILED) { 127 ERROR("peer: cannot map the screen buffer: %s\n", strerror(errno)); 128 goto fail; 129 } 130 if (peer.pixels) { 131 munmap(peer.pixels, peer.size); 132 } 133 peer.pixels = pixels; 134 peer.size = size; 135 peer.width = width; 136 peer.height = height; 137 peer.whole = true; 138 peer.in_flight = false; 139 140 message.arg[0] = (int32_t)width; 141 message.arg[1] = (int32_t)height; 142 message.arg[2] = (int32_t)width * 4; 143 if (!send_message(&message, shown)) { 144 goto fail; 145 } 146 close(shown); 147 close(fd); 148 return true; 149 150 fail: 151 if (shown >= 0) { 152 close(shown); 153 } 154 if (fd >= 0) { 155 close(fd); 156 } 157 return false; 158 } 159 160 /* the composited frame's damaged rows into the shared buffer */ 161 static int 162 plane_attach(struct view *view, struct wld_buffer *buffer) 163 { 164 const pixman_region32_t *damage = compositor_frame_damage(); 165 struct swc_peer_message message = { SWC_PEER_FRAME, { 0 } }; 166 pixman_region32_t region; 167 const pixman_box32_t *boxes, *extents; 168 uint32_t width, height; 169 int32_t y; 170 int i, n; 171 172 (void)view; 173 if (!buffer || !wld_map(buffer)) { 174 return 0; 175 } 176 width = MIN(buffer->width, peer.width); 177 height = MIN(buffer->height, peer.height); 178 pixman_region32_init(®ion); 179 if (peer.whole || !damage) { 180 pixman_region32_union_rect(®ion, ®ion, 0, 0, width, height); 181 peer.whole = false; 182 } else { 183 pixman_region32_copy(®ion, (pixman_region32_t *)damage); 184 pixman_region32_intersect_rect(®ion, ®ion, 0, 0, width, height); 185 } 186 boxes = pixman_region32_rectangles(®ion, &n); 187 for (i = 0; i < n; ++i) { 188 for (y = boxes[i].y1; y < boxes[i].y2; ++y) { 189 memcpy(peer.pixels + (size_t)y * peer.width + boxes[i].x1, 190 (const uint8_t *)buffer->map + (size_t)y * buffer->pitch + 191 (size_t)boxes[i].x1 * 4, 192 (size_t)(boxes[i].x2 - boxes[i].x1) * 4); 193 } 194 } 195 wld_unmap(buffer); 196 197 extents = pixman_region32_extents(®ion); 198 message.arg[0] = extents->x1; 199 message.arg[1] = extents->y1; 200 message.arg[2] = extents->x2 - extents->x1; 201 message.arg[3] = extents->y2 - extents->y1; 202 pixman_region32_fini(®ion); 203 peer.in_flight = true; 204 send_message(&message, -1); 205 return 0; 206 } 207 208 static bool 209 plane_update(struct view *view) 210 { 211 (void)view; 212 return true; 213 } 214 215 static bool 216 plane_move(struct view *view, int32_t x, int32_t y) 217 { 218 view_set_position(view, x, y); 219 return true; 220 } 221 222 static const struct view_impl plane_impl = { 223 .update = plane_update, 224 .attach = plane_attach, 225 .move = plane_move, 226 }; 227 228 static void 229 next_frame(void) 230 { 231 if (!peer.shown) { 232 peer.frame_due = true; 233 return; 234 } 235 peer.frame_due = false; 236 view_frame(&peer.screen->planes.primary.view, get_time()); 237 } 238 239 static bool 240 valid_size(int32_t width, int32_t height) 241 { 242 return width > 0 && height > 0 && width <= SIZE_LIMIT && height <= SIZE_LIMIT; 243 } 244 245 static void 246 handle_message(const struct swc_peer_message *message) 247 { 248 const int32_t *arg = message->arg; 249 uint32_t time = get_time(); 250 wl_fixed_t x, y; 251 252 switch (message->type) { 253 case SWC_PEER_SIZE: 254 if (valid_size(arg[0], arg[1]) && 255 ((uint32_t)arg[0] != peer.width || (uint32_t)arg[1] != peer.height) && 256 share_buffer((uint32_t)arg[0], (uint32_t)arg[1])) { 257 screen_resize(peer.screen, peer.width, peer.height); 258 next_frame(); 259 } 260 break; 261 case SWC_PEER_DONE: 262 if (peer.in_flight) { 263 peer.in_flight = false; 264 next_frame(); 265 } 266 break; 267 case SWC_PEER_SHOWN: 268 peer.shown = arg[0] != 0; 269 if (peer.shown) { 270 /* what was lost while unseen: the whole screen again */ 271 peer.whole = true; 272 compositor_damage_all(); 273 if (peer.frame_due) { 274 next_frame(); 275 } 276 } 277 break; 278 case SWC_PEER_TOUCH: 279 if (!swc.seat || !swc.seat->touch) { 280 break; 281 } 282 x = wl_fixed_from_int(arg[2]); 283 y = wl_fixed_from_int(arg[3]); 284 if (arg[1] == SWC_PEER_DOWN) { 285 touch_handle_down(swc.seat->touch, time, arg[0], x, y); 286 } else if (arg[1] == SWC_PEER_MOVE) { 287 touch_handle_motion(swc.seat->touch, time, arg[0], x, y); 288 } else if (arg[1] == SWC_PEER_UP) { 289 touch_handle_up(swc.seat->touch, time, arg[0]); 290 } else { 291 touch_handle_cancel(swc.seat->touch); 292 } 293 break; 294 case SWC_PEER_TOUCH_FRAME: 295 if (swc.seat && swc.seat->touch) { 296 touch_handle_frame(swc.seat->touch); 297 } 298 break; 299 case SWC_PEER_KEY: 300 if (swc.seat) { 301 keyboard_handle_key(swc.seat->keyboard, time, (uint32_t)arg[0], 302 arg[1] ? WL_KEYBOARD_KEY_STATE_PRESSED 303 : WL_KEYBOARD_KEY_STATE_RELEASED); 304 } 305 break; 306 case SWC_PEER_MOTION: 307 if (swc.seat) { 308 pointer_handle_absolute_motion(swc.seat->pointer, time, 309 wl_fixed_from_int(arg[0]), 310 wl_fixed_from_int(arg[1])); 311 pointer_handle_frame(swc.seat->pointer); 312 } 313 break; 314 case SWC_PEER_BUTTON: 315 if (swc.seat) { 316 pointer_handle_button(swc.seat->pointer, time, (uint32_t)arg[0], 317 arg[1] ? WL_POINTER_BUTTON_STATE_PRESSED 318 : WL_POINTER_BUTTON_STATE_RELEASED); 319 pointer_handle_frame(swc.seat->pointer); 320 } 321 break; 322 case SWC_PEER_SCROLL: 323 if (swc.seat && arg[0] >= 0 && arg[0] <= 1) { 324 pointer_handle_axis(swc.seat->pointer, time, (uint32_t)arg[0], 325 WL_POINTER_AXIS_SOURCE_CONTINUOUS, arg[1], 0); 326 pointer_handle_frame(swc.seat->pointer); 327 } 328 break; 329 } 330 } 331 332 static int 333 handle_data(int fd, uint32_t mask, void *data) 334 { 335 ssize_t n; 336 337 (void)data; 338 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR) && !(mask & WL_EVENT_READABLE)) { 339 wl_display_terminate(swc.display); 340 return 0; 341 } 342 for (;;) { 343 n = recv(fd, (char *)&peer.in + peer.got, sizeof(peer.in) - peer.got, 344 MSG_DONTWAIT); 345 if (n < 0 && errno == EINTR) { 346 continue; 347 } 348 if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 349 break; 350 } 351 if (n <= 0) { 352 /* the peer is gone: the session has nowhere to show */ 353 wl_event_source_remove(peer.source); 354 peer.source = NULL; 355 wl_display_terminate(swc.display); 356 break; 357 } 358 peer.got += (size_t)n; 359 if (peer.got == sizeof(peer.in)) { 360 peer.got = 0; 361 handle_message(&peer.in); 362 } 363 } 364 return 0; 365 } 366 367 static bool 368 create_screens(struct wl_list *screens) 369 { 370 struct output *output; 371 372 output = output_new_fb(peer.width, peer.height, "peer"); 373 if (!output) { 374 return false; 375 } 376 peer.screen = screen_new_virtual(output, &plane_impl); 377 if (!peer.screen) { 378 output_destroy(output); 379 return false; 380 } 381 output->screen = peer.screen; 382 wl_list_insert(screens, &peer.screen->link); 383 return true; 384 } 385 386 static void 387 start(void) 388 { 389 peer.source = wl_event_loop_add_fd(swc.event_loop, peer.fd, 390 WL_EVENT_READABLE, &handle_data, NULL); 391 if (!peer.source) { 392 ERROR("peer: cannot watch the socket\n"); 393 wl_display_terminate(swc.display); 394 return; 395 } 396 /* no console to switch to it: it is the one screen from the start */ 397 swc_activate(); 398 } 399 400 static void 401 finalize(void) 402 { 403 if (peer.source) { 404 wl_event_source_remove(peer.source); 405 } 406 if (peer.backend.renderer) { 407 wld_destroy_renderer(peer.backend.renderer); 408 } 409 if (peer.backend.context) { 410 wld_destroy_context(peer.backend.context); 411 } 412 if (peer.pixels) { 413 munmap(peer.pixels, peer.size); 414 } 415 close(peer.fd); 416 } 417 418 static int 419 connect_to(const char *path) 420 { 421 struct sockaddr_un address = { .sun_family = AF_UNIX }; 422 size_t length = strlen(path); 423 int fd; 424 425 if (length == 0 || length >= sizeof(address.sun_path)) { 426 ERROR("peer: SWC_PEER_SOCKET is empty or too long\n"); 427 return -1; 428 } 429 memcpy(address.sun_path, path, length); 430 if (path[0] == '@') { 431 address.sun_path[0] = '\0'; 432 } else { 433 length++; 434 } 435 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); 436 if (fd < 0 || connect(fd, (struct sockaddr *)&address, 437 (socklen_t)(offsetof(struct sockaddr_un, sun_path) + length)) < 0) { 438 ERROR("peer: cannot connect to %s: %s\n", path, strerror(errno)); 439 if (fd >= 0) { 440 close(fd); 441 } 442 return -1; 443 } 444 return fd; 445 } 446 447 bool 448 peer_requested(void) 449 { 450 return getenv("SWC_PEER_SOCKET") != NULL; 451 } 452 453 bool 454 peer_initialize(void) 455 { 456 struct swc_peer_message message; 457 size_t got = 0; 458 ssize_t n; 459 460 peer.fd = connect_to(getenv("SWC_PEER_SOCKET")); 461 if (peer.fd < 0) { 462 return false; 463 } 464 /* the screen's size, before there is a screen */ 465 while (got < sizeof(message)) { 466 n = recv(peer.fd, (char *)&message + got, sizeof(message) - got, 0); 467 if (n < 0 && errno == EINTR) { 468 continue; 469 } 470 if (n <= 0) { 471 ERROR("peer: it said no size\n"); 472 goto fail; 473 } 474 got += (size_t)n; 475 } 476 if (message.type != SWC_PEER_SIZE || !valid_size(message.arg[0], message.arg[1])) { 477 ERROR("peer: it said no size\n"); 478 goto fail; 479 } 480 if (!share_buffer((uint32_t)message.arg[0], (uint32_t)message.arg[1])) { 481 goto fail; 482 } 483 peer.shown = true; 484 485 peer.backend.context = wld_pixman_create_context(); 486 if (!peer.backend.context) { 487 goto fail; 488 } 489 peer.backend.renderer = wld_create_renderer(peer.backend.context); 490 if (!peer.backend.renderer) { 491 goto fail; 492 } 493 peer.backend.cursor_width = 64; 494 peer.backend.cursor_height = 64; 495 peer.backend.virtual_input = true; 496 peer.backend.virtual_touch = true; 497 peer.backend.create_screens = create_screens; 498 peer.backend.start = start; 499 peer.backend.finalize = finalize; 500 swc.backend = &peer.backend; 501 return true; 502 503 fail: 504 finalize(); 505 return false; 506 }