neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

peer.patch (21364B)


      1 peer: a screen another program shows
      2 
      3 SWC_PEER_SOCKET makes swc draw into shared memory and hand each frame to
      4 the program listening there, which sends back touch, keys and the mouse:
      5 for a host that holds the real display, such as an application of another
      6 system. one frame is in flight at a time and none is drawn while the peer
      7 says it is hidden. see libswc/peer_protocol.h.
      8 
      9 --- a/libswc/backend.h
     10 +++ b/libswc/backend.h
     11 @@ -20,6 +20,8 @@
     12  
     13  	/* backend feeds the seat, no input devices */
     14  	bool virtual_input;
     15 +	/* and fingers with it */
     16 +	bool virtual_touch;
     17  
     18  	/* optional, unset means the built-in video backend */
     19  	bool (*create_screens)(struct wl_list *screens);
     20 --- a/libswc/seat.h
     21 +++ b/libswc/seat.h
     22 @@ -30,6 +30,7 @@
     23  	struct pointer *pointer;
     24  	struct keyboard *keyboard;
     25  	struct data_device *data_device;
     26 +	struct touch *touch;
     27  };
     28  
     29  struct swc_seat *
     30 --- a/libswc/seat.c
     31 +++ b/libswc/seat.c
     32 @@ -79,7 +79,6 @@
     33  
     34  	struct wl_listener keyboard_focus_listener;
     35  	struct pointer pointer;
     36 -	struct touch *touch;
     37  	struct wl_listener data_device_listener;
     38  
     39  	struct wl_global *global;
     40 @@ -173,7 +172,7 @@
     41  {
     42  	struct seat *seat = wl_resource_get_user_data(resource);
     43  
     44 -	if (!touch_bind(seat->touch, client, wl_resource_get_version(resource),
     45 +	if (!touch_bind(seat->base.touch, client, wl_resource_get_version(resource),
     46  	                id)) {
     47  		wl_resource_post_no_memory(resource);
     48  	}
     49 @@ -306,23 +305,23 @@
     50  			y = wl_fixed_from_double(rect->y +
     51  			    libinput_event_touch_get_y_transformed(event.touch, rect->height));
     52  			if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TOUCH_DOWN) {
     53 -				touch_handle_down(seat->touch, time,
     54 +				touch_handle_down(seat->base.touch, time,
     55  				                  libinput_event_touch_get_seat_slot(event.touch), x, y);
     56  			} else {
     57 -				touch_handle_motion(seat->touch, time,
     58 +				touch_handle_motion(seat->base.touch, time,
     59  				                    libinput_event_touch_get_seat_slot(event.touch), x, y);
     60  			}
     61  			break;
     62  		case LIBINPUT_EVENT_TOUCH_UP:
     63  			event.touch = libinput_event_get_touch_event(generic_event);
     64 -			touch_handle_up(seat->touch, libinput_event_touch_get_time(event.touch),
     65 +			touch_handle_up(seat->base.touch, libinput_event_touch_get_time(event.touch),
     66  			                libinput_event_touch_get_seat_slot(event.touch));
     67  			break;
     68  		case LIBINPUT_EVENT_TOUCH_FRAME:
     69 -			touch_handle_frame(seat->touch);
     70 +			touch_handle_frame(seat->base.touch);
     71  			break;
     72  		case LIBINPUT_EVENT_TOUCH_CANCEL:
     73 -			touch_handle_cancel(seat->touch);
     74 +			touch_handle_cancel(seat->base.touch);
     75  			break;
     76  		case LIBINPUT_EVENT_KEYBOARD_KEY:
     77  			event.k = libinput_event_get_keyboard_event(generic_event);
     78 @@ -571,7 +570,7 @@
     79  	}
     80  	seat->base.pointer = &seat->pointer;
     81  
     82 -	if (!(seat->touch = touch_create(&seat->pointer))) {
     83 +	if (!(seat->base.touch = touch_create(&seat->pointer))) {
     84  		ERROR("Could not initialize touch\n");
     85  		goto error6;
     86  	}
     87 @@ -580,7 +579,10 @@
     88  	seat->libinput_source = NULL;
     89  	if (swc.backend->virtual_input) {
     90  		update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD |
     91 -		                              WL_SEAT_CAPABILITY_POINTER);
     92 +		                              WL_SEAT_CAPABILITY_POINTER |
     93 +		                              (swc.backend->virtual_touch
     94 +		                                   ? WL_SEAT_CAPABILITY_TOUCH
     95 +		                                   : 0));
     96  	} else if (!initialize_libinput(seat)) {
     97  		goto error7;
     98  	}
     99 @@ -588,7 +590,7 @@
    100  	return &seat->base;
    101  
    102  error7:
    103 -	touch_destroy(seat->touch);
    104 +	touch_destroy(seat->base.touch);
    105  error6:
    106  	pointer_finalize(&seat->pointer);
    107  error5:
    108 @@ -618,7 +620,7 @@
    109  #endif
    110  	}
    111  
    112 -	touch_destroy(seat->touch);
    113 +	touch_destroy(seat->base.touch);
    114  	pointer_finalize(&seat->pointer);
    115  	keyboard_destroy(seat->base.keyboard);
    116  	data_device_destroy(seat->base.data_device);
    117 --- a/libswc/swc.c
    118 +++ b/libswc/swc.c
    119 @@ -37,6 +37,9 @@
    120  #include "keyboard.h"
    121  #include "launch.h"
    122  #include "layer_shell.h"
    123 +#ifdef ENABLE_PEER
    124 +#include "peer.h"
    125 +#endif
    126  #ifdef ENABLE_NESTED
    127  #include "nested.h"
    128  #endif
    129 @@ -265,6 +268,11 @@
    130  static bool
    131  backend_initialize(void)
    132  {
    133 +#ifdef ENABLE_PEER
    134 +	if (peer_requested()) {
    135 +		return peer_initialize();
    136 +	}
    137 +#endif
    138  #ifdef ENABLE_NESTED
    139  	if (nested_requested()) {
    140  		return nested_initialize();
    141 --- a/libswc/meson.build
    142 +++ b/libswc/meson.build
    143 @@ -55,6 +55,9 @@
    144  if have_nested
    145    libswc_src += files('nested.c')
    146  endif
    147 +if have_peer
    148 +  libswc_src += files('peer.c')
    149 +endif
    150  
    151  foreach proto, proto_src : protos
    152  	libswc_src += proto_src
    153 --- a/meson.build
    154 +++ b/meson.build
    155 @@ -144,6 +144,17 @@
    156    requires_private += wayland_client
    157  endif
    158  
    159 +# the peer backend feeds the libinput seat, touch and all, and shares its
    160 +# screen through a memfd
    161 +have_peer = (not get_option('peer').disabled() and input_backend == 'libinput'
    162 +  and cc.has_function('memfd_create', prefix: '#include <sys/mman.h>', args: '-D_GNU_SOURCE'))
    163 +if get_option('peer').enabled() and not have_peer
    164 +  error('The peer backend needs the libinput input backend and memfd_create')
    165 +endif
    166 +if have_peer
    167 +  add_project_arguments('-DENABLE_PEER=1', language: 'c')
    168 +endif
    169 +
    170  subdir('protocol')
    171  subdir('cursor')
    172  subdir('launch')
    173 --- a/meson.options
    174 +++ b/meson.options
    175 @@ -6,5 +6,6 @@
    176  option('wsdisplay-device', type: 'string', value: '', description: 'wsdisplay framebuffer device (empty uses the VT selected by swc-launch)')
    177  option('udev', type: 'feature', value: 'auto', description: 'libinput libudev support')
    178  option('nested', type: 'feature', value: 'auto', description: 'Backend that runs swc inside another Wayland compositor')
    179 +option('peer', type: 'feature', value: 'auto', description: 'Backend whose screen another program shows, over a socket')
    180  option('extra', type: 'boolean', value: true, description: 'Build swc utility applications')
    181  option('example', type: 'boolean', value: false, description: 'Build swc example compositor')
    182 --- /dev/null
    183 +++ b/libswc/peer.c
    184 @@ -0,0 +1,506 @@
    185 +/* swc: libswc/peer.c
    186 + *
    187 + * the screen drawn in software into shared memory and shown by a peer, which
    188 + * sends back the input: a program of another system holding the real display,
    189 + * such as an application of it. see peer_protocol.h
    190 + */
    191 +
    192 +#include "peer.h"
    193 +#include "backend.h"
    194 +#include "compositor.h"
    195 +#include "internal.h"
    196 +#include "keyboard.h"
    197 +#include "output.h"
    198 +#include "peer_protocol.h"
    199 +#include "pointer.h"
    200 +#include "screen.h"
    201 +#include "seat.h"
    202 +#include "surface.h"
    203 +#include "touch.h"
    204 +#include "util.h"
    205 +
    206 +#include <errno.h>
    207 +#include <fcntl.h>
    208 +#include <pixman.h>
    209 +#include <stddef.h>
    210 +#include <stdio.h>
    211 +#include <stdlib.h>
    212 +#include <string.h>
    213 +#include <sys/mman.h>
    214 +#include <sys/socket.h>
    215 +#include <sys/un.h>
    216 +#include <unistd.h>
    217 +#include <wld/pixman.h>
    218 +#include <wld/wld.h>
    219 +
    220 +/* a screen past this is a peer's mistake, not a size to allocate */
    221 +#define SIZE_LIMIT 16384
    222 +
    223 +static struct {
    224 +	struct swc_backend backend;
    225 +	int fd;
    226 +	struct wl_event_source *source;
    227 +
    228 +	struct screen *screen;
    229 +	uint32_t width, height;
    230 +	/* the buffer shared with the peer, and what it has not been sent */
    231 +	uint32_t *pixels;
    232 +	size_t size;
    233 +	bool whole;
    234 +
    235 +	bool in_flight; /* a frame sent and not yet done with */
    236 +	bool frame_due; /* done, but the frame callback waits for it to be seen */
    237 +	bool shown;
    238 +
    239 +	/* a message read in part */
    240 +	struct swc_peer_message in;
    241 +	size_t got;
    242 +} peer;
    243 +
    244 +static bool
    245 +send_message(const struct swc_peer_message *message, int descriptor)
    246 +{
    247 +	union {
    248 +		struct cmsghdr header;
    249 +		char space[CMSG_SPACE(sizeof(int))];
    250 +	} control;
    251 +	struct iovec iov = { (void *)message, sizeof(*message) };
    252 +	struct msghdr msg = { 0 };
    253 +	struct cmsghdr *header;
    254 +	size_t sent = 0;
    255 +	ssize_t n;
    256 +
    257 +	msg.msg_iov = &iov;
    258 +	msg.msg_iovlen = 1;
    259 +	if (descriptor >= 0) {
    260 +		msg.msg_control = control.space;
    261 +		msg.msg_controllen = sizeof(control.space);
    262 +		header = CMSG_FIRSTHDR(&msg);
    263 +		header->cmsg_level = SOL_SOCKET;
    264 +		header->cmsg_type = SCM_RIGHTS;
    265 +		header->cmsg_len = CMSG_LEN(sizeof(int));
    266 +		memcpy(CMSG_DATA(header), &descriptor, sizeof(int));
    267 +	}
    268 +	while (sent < sizeof(*message)) {
    269 +		n = sendmsg(peer.fd, &msg, MSG_NOSIGNAL);
    270 +		if (n < 0) {
    271 +			if (errno == EINTR) {
    272 +				continue;
    273 +			}
    274 +			ERROR("peer: cannot send: %s\n", strerror(errno));
    275 +			return false;
    276 +		}
    277 +		sent += (size_t)n;
    278 +		iov.iov_base = (char *)message + sent;
    279 +		iov.iov_len = sizeof(*message) - sent;
    280 +		msg.msg_control = NULL;
    281 +		msg.msg_controllen = 0;
    282 +	}
    283 +	return true;
    284 +}
    285 +
    286 +/* a new buffer of width by height, handed to the peer */
    287 +static bool
    288 +share_buffer(uint32_t width, uint32_t height)
    289 +{
    290 +	struct swc_peer_message message = { SWC_PEER_BUFFER, { 0 } };
    291 +	size_t size = (size_t)width * height * 4;
    292 +	uint32_t *pixels;
    293 +	char path[32];
    294 +	int fd, shown = -1;
    295 +
    296 +	fd = memfd_create("swc-peer", MFD_CLOEXEC);
    297 +	if (fd < 0 || ftruncate(fd, (off_t)size) < 0) {
    298 +		ERROR("peer: cannot make the screen buffer: %s\n", strerror(errno));
    299 +		goto fail;
    300 +	}
    301 +	/* the peer only shows the buffer, so it gets a descriptor that cannot
    302 +	 * write it: a confined peer may be refused one that can */
    303 +	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
    304 +	shown = open(path, O_RDONLY | O_CLOEXEC);
    305 +	if (shown < 0) {
    306 +		ERROR("peer: cannot open the screen buffer to be read: %s\n", strerror(errno));
    307 +		goto fail;
    308 +	}
    309 +	pixels = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    310 +	if (pixels == MAP_FAILED) {
    311 +		ERROR("peer: cannot map the screen buffer: %s\n", strerror(errno));
    312 +		goto fail;
    313 +	}
    314 +	if (peer.pixels) {
    315 +		munmap(peer.pixels, peer.size);
    316 +	}
    317 +	peer.pixels = pixels;
    318 +	peer.size = size;
    319 +	peer.width = width;
    320 +	peer.height = height;
    321 +	peer.whole = true;
    322 +	peer.in_flight = false;
    323 +
    324 +	message.arg[0] = (int32_t)width;
    325 +	message.arg[1] = (int32_t)height;
    326 +	message.arg[2] = (int32_t)width * 4;
    327 +	if (!send_message(&message, shown)) {
    328 +		goto fail;
    329 +	}
    330 +	close(shown);
    331 +	close(fd);
    332 +	return true;
    333 +
    334 +fail:
    335 +	if (shown >= 0) {
    336 +		close(shown);
    337 +	}
    338 +	if (fd >= 0) {
    339 +		close(fd);
    340 +	}
    341 +	return false;
    342 +}
    343 +
    344 +/* the composited frame's damaged rows into the shared buffer */
    345 +static int
    346 +plane_attach(struct view *view, struct wld_buffer *buffer)
    347 +{
    348 +	const pixman_region32_t *damage = compositor_frame_damage();
    349 +	struct swc_peer_message message = { SWC_PEER_FRAME, { 0 } };
    350 +	pixman_region32_t region;
    351 +	const pixman_box32_t *boxes, *extents;
    352 +	uint32_t width, height;
    353 +	int32_t y;
    354 +	int i, n;
    355 +
    356 +	(void)view;
    357 +	if (!buffer || !wld_map(buffer)) {
    358 +		return 0;
    359 +	}
    360 +	width = MIN(buffer->width, peer.width);
    361 +	height = MIN(buffer->height, peer.height);
    362 +	pixman_region32_init(&region);
    363 +	if (peer.whole || !damage) {
    364 +		pixman_region32_union_rect(&region, &region, 0, 0, width, height);
    365 +		peer.whole = false;
    366 +	} else {
    367 +		pixman_region32_copy(&region, (pixman_region32_t *)damage);
    368 +		pixman_region32_intersect_rect(&region, &region, 0, 0, width, height);
    369 +	}
    370 +	boxes = pixman_region32_rectangles(&region, &n);
    371 +	for (i = 0; i < n; ++i) {
    372 +		for (y = boxes[i].y1; y < boxes[i].y2; ++y) {
    373 +			memcpy(peer.pixels + (size_t)y * peer.width + boxes[i].x1,
    374 +			       (const uint8_t *)buffer->map + (size_t)y * buffer->pitch +
    375 +			           (size_t)boxes[i].x1 * 4,
    376 +			       (size_t)(boxes[i].x2 - boxes[i].x1) * 4);
    377 +		}
    378 +	}
    379 +	wld_unmap(buffer);
    380 +
    381 +	extents = pixman_region32_extents(&region);
    382 +	message.arg[0] = extents->x1;
    383 +	message.arg[1] = extents->y1;
    384 +	message.arg[2] = extents->x2 - extents->x1;
    385 +	message.arg[3] = extents->y2 - extents->y1;
    386 +	pixman_region32_fini(&region);
    387 +	peer.in_flight = true;
    388 +	send_message(&message, -1);
    389 +	return 0;
    390 +}
    391 +
    392 +static bool
    393 +plane_update(struct view *view)
    394 +{
    395 +	(void)view;
    396 +	return true;
    397 +}
    398 +
    399 +static bool
    400 +plane_move(struct view *view, int32_t x, int32_t y)
    401 +{
    402 +	view_set_position(view, x, y);
    403 +	return true;
    404 +}
    405 +
    406 +static const struct view_impl plane_impl = {
    407 +    .update = plane_update,
    408 +    .attach = plane_attach,
    409 +    .move = plane_move,
    410 +};
    411 +
    412 +static void
    413 +next_frame(void)
    414 +{
    415 +	if (!peer.shown) {
    416 +		peer.frame_due = true;
    417 +		return;
    418 +	}
    419 +	peer.frame_due = false;
    420 +	view_frame(&peer.screen->planes.primary.view, get_time());
    421 +}
    422 +
    423 +static bool
    424 +valid_size(int32_t width, int32_t height)
    425 +{
    426 +	return width > 0 && height > 0 && width <= SIZE_LIMIT && height <= SIZE_LIMIT;
    427 +}
    428 +
    429 +static void
    430 +handle_message(const struct swc_peer_message *message)
    431 +{
    432 +	const int32_t *arg = message->arg;
    433 +	uint32_t time = get_time();
    434 +	wl_fixed_t x, y;
    435 +
    436 +	switch (message->type) {
    437 +	case SWC_PEER_SIZE:
    438 +		if (valid_size(arg[0], arg[1]) &&
    439 +		    ((uint32_t)arg[0] != peer.width || (uint32_t)arg[1] != peer.height) &&
    440 +		    share_buffer((uint32_t)arg[0], (uint32_t)arg[1])) {
    441 +			screen_resize(peer.screen, peer.width, peer.height);
    442 +			next_frame();
    443 +		}
    444 +		break;
    445 +	case SWC_PEER_DONE:
    446 +		if (peer.in_flight) {
    447 +			peer.in_flight = false;
    448 +			next_frame();
    449 +		}
    450 +		break;
    451 +	case SWC_PEER_SHOWN:
    452 +		peer.shown = arg[0] != 0;
    453 +		if (peer.shown) {
    454 +			/* what was lost while unseen: the whole screen again */
    455 +			peer.whole = true;
    456 +			compositor_damage_all();
    457 +			if (peer.frame_due) {
    458 +				next_frame();
    459 +			}
    460 +		}
    461 +		break;
    462 +	case SWC_PEER_TOUCH:
    463 +		if (!swc.seat || !swc.seat->touch) {
    464 +			break;
    465 +		}
    466 +		x = wl_fixed_from_int(arg[2]);
    467 +		y = wl_fixed_from_int(arg[3]);
    468 +		if (arg[1] == SWC_PEER_DOWN) {
    469 +			touch_handle_down(swc.seat->touch, time, arg[0], x, y);
    470 +		} else if (arg[1] == SWC_PEER_MOVE) {
    471 +			touch_handle_motion(swc.seat->touch, time, arg[0], x, y);
    472 +		} else if (arg[1] == SWC_PEER_UP) {
    473 +			touch_handle_up(swc.seat->touch, time, arg[0]);
    474 +		} else {
    475 +			touch_handle_cancel(swc.seat->touch);
    476 +		}
    477 +		break;
    478 +	case SWC_PEER_TOUCH_FRAME:
    479 +		if (swc.seat && swc.seat->touch) {
    480 +			touch_handle_frame(swc.seat->touch);
    481 +		}
    482 +		break;
    483 +	case SWC_PEER_KEY:
    484 +		if (swc.seat) {
    485 +			keyboard_handle_key(swc.seat->keyboard, time, (uint32_t)arg[0],
    486 +			                    arg[1] ? WL_KEYBOARD_KEY_STATE_PRESSED
    487 +			                           : WL_KEYBOARD_KEY_STATE_RELEASED);
    488 +		}
    489 +		break;
    490 +	case SWC_PEER_MOTION:
    491 +		if (swc.seat) {
    492 +			pointer_handle_absolute_motion(swc.seat->pointer, time,
    493 +			                               wl_fixed_from_int(arg[0]),
    494 +			                               wl_fixed_from_int(arg[1]));
    495 +			pointer_handle_frame(swc.seat->pointer);
    496 +		}
    497 +		break;
    498 +	case SWC_PEER_BUTTON:
    499 +		if (swc.seat) {
    500 +			pointer_handle_button(swc.seat->pointer, time, (uint32_t)arg[0],
    501 +			                      arg[1] ? WL_POINTER_BUTTON_STATE_PRESSED
    502 +			                             : WL_POINTER_BUTTON_STATE_RELEASED);
    503 +			pointer_handle_frame(swc.seat->pointer);
    504 +		}
    505 +		break;
    506 +	case SWC_PEER_SCROLL:
    507 +		if (swc.seat && arg[0] >= 0 && arg[0] <= 1) {
    508 +			pointer_handle_axis(swc.seat->pointer, time, (uint32_t)arg[0],
    509 +			                    WL_POINTER_AXIS_SOURCE_CONTINUOUS, arg[1], 0);
    510 +			pointer_handle_frame(swc.seat->pointer);
    511 +		}
    512 +		break;
    513 +	}
    514 +}
    515 +
    516 +static int
    517 +handle_data(int fd, uint32_t mask, void *data)
    518 +{
    519 +	ssize_t n;
    520 +
    521 +	(void)data;
    522 +	if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR) && !(mask & WL_EVENT_READABLE)) {
    523 +		wl_display_terminate(swc.display);
    524 +		return 0;
    525 +	}
    526 +	for (;;) {
    527 +		n = recv(fd, (char *)&peer.in + peer.got, sizeof(peer.in) - peer.got,
    528 +		         MSG_DONTWAIT);
    529 +		if (n < 0 && errno == EINTR) {
    530 +			continue;
    531 +		}
    532 +		if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
    533 +			break;
    534 +		}
    535 +		if (n <= 0) {
    536 +			/* the peer is gone: the session has nowhere to show */
    537 +			wl_event_source_remove(peer.source);
    538 +			peer.source = NULL;
    539 +			wl_display_terminate(swc.display);
    540 +			break;
    541 +		}
    542 +		peer.got += (size_t)n;
    543 +		if (peer.got == sizeof(peer.in)) {
    544 +			peer.got = 0;
    545 +			handle_message(&peer.in);
    546 +		}
    547 +	}
    548 +	return 0;
    549 +}
    550 +
    551 +static bool
    552 +create_screens(struct wl_list *screens)
    553 +{
    554 +	struct output *output;
    555 +
    556 +	output = output_new_fb(peer.width, peer.height, "peer");
    557 +	if (!output) {
    558 +		return false;
    559 +	}
    560 +	peer.screen = screen_new_virtual(output, &plane_impl);
    561 +	if (!peer.screen) {
    562 +		output_destroy(output);
    563 +		return false;
    564 +	}
    565 +	output->screen = peer.screen;
    566 +	wl_list_insert(screens, &peer.screen->link);
    567 +	return true;
    568 +}
    569 +
    570 +static void
    571 +start(void)
    572 +{
    573 +	peer.source = wl_event_loop_add_fd(swc.event_loop, peer.fd,
    574 +	                                   WL_EVENT_READABLE, &handle_data, NULL);
    575 +	if (!peer.source) {
    576 +		ERROR("peer: cannot watch the socket\n");
    577 +		wl_display_terminate(swc.display);
    578 +		return;
    579 +	}
    580 +	/* no console to switch to it: it is the one screen from the start */
    581 +	swc_activate();
    582 +}
    583 +
    584 +static void
    585 +finalize(void)
    586 +{
    587 +	if (peer.source) {
    588 +		wl_event_source_remove(peer.source);
    589 +	}
    590 +	if (peer.backend.renderer) {
    591 +		wld_destroy_renderer(peer.backend.renderer);
    592 +	}
    593 +	if (peer.backend.context) {
    594 +		wld_destroy_context(peer.backend.context);
    595 +	}
    596 +	if (peer.pixels) {
    597 +		munmap(peer.pixels, peer.size);
    598 +	}
    599 +	close(peer.fd);
    600 +}
    601 +
    602 +static int
    603 +connect_to(const char *path)
    604 +{
    605 +	struct sockaddr_un address = { .sun_family = AF_UNIX };
    606 +	size_t length = strlen(path);
    607 +	int fd;
    608 +
    609 +	if (length == 0 || length >= sizeof(address.sun_path)) {
    610 +		ERROR("peer: SWC_PEER_SOCKET is empty or too long\n");
    611 +		return -1;
    612 +	}
    613 +	memcpy(address.sun_path, path, length);
    614 +	if (path[0] == '@') {
    615 +		address.sun_path[0] = '\0';
    616 +	} else {
    617 +		length++;
    618 +	}
    619 +	fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
    620 +	if (fd < 0 || connect(fd, (struct sockaddr *)&address,
    621 +	                      (socklen_t)(offsetof(struct sockaddr_un, sun_path) + length)) < 0) {
    622 +		ERROR("peer: cannot connect to %s: %s\n", path, strerror(errno));
    623 +		if (fd >= 0) {
    624 +			close(fd);
    625 +		}
    626 +		return -1;
    627 +	}
    628 +	return fd;
    629 +}
    630 +
    631 +bool
    632 +peer_requested(void)
    633 +{
    634 +	return getenv("SWC_PEER_SOCKET") != NULL;
    635 +}
    636 +
    637 +bool
    638 +peer_initialize(void)
    639 +{
    640 +	struct swc_peer_message message;
    641 +	size_t got = 0;
    642 +	ssize_t n;
    643 +
    644 +	peer.fd = connect_to(getenv("SWC_PEER_SOCKET"));
    645 +	if (peer.fd < 0) {
    646 +		return false;
    647 +	}
    648 +	/* the screen's size, before there is a screen */
    649 +	while (got < sizeof(message)) {
    650 +		n = recv(peer.fd, (char *)&message + got, sizeof(message) - got, 0);
    651 +		if (n < 0 && errno == EINTR) {
    652 +			continue;
    653 +		}
    654 +		if (n <= 0) {
    655 +			ERROR("peer: it said no size\n");
    656 +			goto fail;
    657 +		}
    658 +		got += (size_t)n;
    659 +	}
    660 +	if (message.type != SWC_PEER_SIZE || !valid_size(message.arg[0], message.arg[1])) {
    661 +		ERROR("peer: it said no size\n");
    662 +		goto fail;
    663 +	}
    664 +	if (!share_buffer((uint32_t)message.arg[0], (uint32_t)message.arg[1])) {
    665 +		goto fail;
    666 +	}
    667 +	peer.shown = true;
    668 +
    669 +	peer.backend.context = wld_pixman_create_context();
    670 +	if (!peer.backend.context) {
    671 +		goto fail;
    672 +	}
    673 +	peer.backend.renderer = wld_create_renderer(peer.backend.context);
    674 +	if (!peer.backend.renderer) {
    675 +		goto fail;
    676 +	}
    677 +	peer.backend.cursor_width = 64;
    678 +	peer.backend.cursor_height = 64;
    679 +	peer.backend.virtual_input = true;
    680 +	peer.backend.virtual_touch = true;
    681 +	peer.backend.create_screens = create_screens;
    682 +	peer.backend.start = start;
    683 +	peer.backend.finalize = finalize;
    684 +	swc.backend = &peer.backend;
    685 +	return true;
    686 +
    687 +fail:
    688 +	finalize();
    689 +	return false;
    690 +}
    691 --- /dev/null
    692 +++ b/libswc/peer.h
    693 @@ -0,0 +1,16 @@
    694 +/* swc: libswc/peer.h
    695 + *
    696 + * a screen shown by another program over a socket, see peer_protocol.h
    697 + */
    698 +
    699 +#ifndef SWC_PEER_H
    700 +#define SWC_PEER_H
    701 +
    702 +#include <stdbool.h>
    703 +
    704 +bool
    705 +peer_requested(void);
    706 +bool
    707 +peer_initialize(void);
    708 +
    709 +#endif
    710 --- /dev/null
    711 +++ b/libswc/peer_protocol.h
    712 @@ -0,0 +1,59 @@
    713 +/* swc: libswc/peer_protocol.h
    714 + *
    715 + * the peer backend and the program that shows its frames and sends it input,
    716 + * its peer, over a stream socket: SWC_PEER_SOCKET, a path, or @name in
    717 + * linux's abstract namespace. the peer listens, swc connects. every message
    718 + * is one struct swc_peer_message in the machine's byte order; the one from
    719 + * swc that carries a descriptor, open to be read only, carries it with its
    720 + * first byte (SCM_RIGHTS)
    721 + *
    722 + * the peer's first message is SWC_PEER_SIZE. swc then sends SWC_PEER_BUFFER
    723 + * and, as the screen changes, SWC_PEER_FRAME. it draws the next frame only
    724 + * after SWC_PEER_DONE, and none while the peer says it is hidden. the
    725 + * session ends when the socket closes
    726 + */
    727 +
    728 +#ifndef SWC_PEER_PROTOCOL_H
    729 +#define SWC_PEER_PROTOCOL_H
    730 +
    731 +#include <stdint.h>
    732 +
    733 +enum {
    734 +	/* from swc: a shared memory descriptor holding the screen, one
    735 +	 * XRGB8888 pixel a 32 bit word: arg width, height, bytes per row.
    736 +	 * it replaces any before it */
    737 +	SWC_PEER_BUFFER = 1,
    738 +	/* from swc: the buffer changed in x y width height. the peer reads it
    739 +	 * until it answers SWC_PEER_DONE */
    740 +	SWC_PEER_FRAME = 2,
    741 +
    742 +	/* from the peer: the screen's size in pixels, at the start and when
    743 +	 * it changes: arg width, height */
    744 +	SWC_PEER_SIZE = 16,
    745 +	/* the last frame has been taken */
    746 +	SWC_PEER_DONE = 17,
    747 +	/* arg 1 when the screen is seen, 0 when it is not */
    748 +	SWC_PEER_SHOWN = 18,
    749 +	/* arg slot, phase (SWC_PEER_DOWN...), x, y in screen pixels */
    750 +	SWC_PEER_TOUCH = 19,
    751 +	/* the touches since the last one happened together */
    752 +	SWC_PEER_TOUCH_FRAME = 20,
    753 +	/* arg linux evdev code, 1 pressed or 0 let go */
    754 +	SWC_PEER_KEY = 21,
    755 +	/* a mouse: arg x, y in screen pixels */
    756 +	SWC_PEER_MOTION = 22,
    757 +	/* arg linux evdev button, 1 pressed or 0 let go */
    758 +	SWC_PEER_BUTTON = 23,
    759 +	/* arg wl_pointer axis, the distance as wl_fixed_t */
    760 +	SWC_PEER_SCROLL = 24,
    761 +};
    762 +
    763 +/* phases of SWC_PEER_TOUCH */
    764 +enum { SWC_PEER_DOWN = 0, SWC_PEER_MOVE = 1, SWC_PEER_UP = 2, SWC_PEER_CANCEL = 3 };
    765 +
    766 +struct swc_peer_message {
    767 +	uint32_t type;
    768 +	int32_t arg[5];
    769 +};
    770 +
    771 +#endif