neuswc

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

compositor.patch (106315B)


      1 Subject: backends, z order, backdrops, a keymap given as text, decor fonts from files
      2 
      3 diff --git a/libswc/backend.h b/libswc/backend.h
      4 index 65432bc..8d0e7bf 100644
      5 --- a/libswc/backend.h
      6 +++ b/libswc/backend.h
      7 @@ -1,17 +1,33 @@
      8  #ifndef SWC_BACKEND_H
      9  #define SWC_BACKEND_H
     10  
     11 +#include <stdbool.h>
     12  #include <stdint.h>
     13  
     14 +struct screen;
     15 +struct wl_list;
     16 +struct wld_buffer;
     17  struct wld_context;
     18  struct wld_renderer;
     19 +struct wld_surface;
     20  
     21 -/* rendering state shared by the drm and fb backends */
     22 +/* rendering state shared by every backend */
     23  struct swc_backend {
     24  	struct wld_context *context;
     25  	struct wld_renderer *renderer;
     26  	uint32_t cursor_width;
     27  	uint32_t cursor_height;
     28 +
     29 +	/* backend feeds the seat, no input devices */
     30 +	bool virtual_input;
     31 +
     32 +	/* optional, unset means the built-in video backend */
     33 +	bool (*create_screens)(struct wl_list *screens);
     34 +	struct wld_surface *(*create_target)(struct screen *screen);
     35 +	void (*set_cursor)(struct wld_buffer *buffer, int32_t hotspot_x,
     36 +	                   int32_t hotspot_y);
     37 +	void (*start)(void);
     38 +	void (*finalize)(void);
     39  };
     40  
     41  #endif
     42 diff --git a/libswc/compositor.c b/libswc/compositor.c
     43 index bb762f6..f10bcee 100644
     44 --- a/libswc/compositor.c
     45 +++ b/libswc/compositor.c
     46 @@ -107,6 +107,8 @@ handle_button(struct pointer_handler *handler, uint32_t time,
     47                struct button *button, uint32_t state);
     48  static void
     49  perform_update(void *data);
     50 +static void
     51 +schedule_updates(uint32_t screens);
     52  
     53  static struct pointer_handler pointer_handler = {
     54      .motion = handle_motion,
     55 @@ -126,6 +128,8 @@ static struct {
     56  	uint32_t scheduled_updates;
     57  
     58  	bool updating;
     59 +	/* The window whose decoration the pointer is over, if one. */
     60 +	struct window *chrome_window;
     61  	struct wl_global *global;
     62  	bool initialized;
     63  
     64 @@ -208,25 +212,33 @@ target_swap_buffers(struct target *target)
     65  	return view_attach(target->view, target->next_buffer);
     66  }
     67  
     68 +static struct wld_surface *
     69 +target_create_surface(struct screen *screen)
     70 +{
     71 +	struct swc_rectangle *geom = &screen->base.geometry;
     72 +
     73 +	if (swc.backend->create_target) {
     74 +		return swc.backend->create_target(screen);
     75 +	}
     76 +
     77 +	return wld_create_surface(swc.backend->context, geom->width, geom->height,
     78 +	                          WLD_FORMAT_XRGB8888,
     79 +#ifdef ENABLE_DRM
     80 +	                          swc.video == SWC_VIDEO_DRM ? WLD_DRM_FLAG_SCANOUT :
     81 +#endif
     82 +	                          WLD_FLAG_MAP);
     83 +}
     84 +
     85  static struct target *
     86  target_new(struct screen *screen)
     87  {
     88  	struct target *target;
     89 -	struct swc_rectangle *geom = &screen->base.geometry;
     90  
     91  	if (!(target = malloc(sizeof(*target)))) {
     92  		goto error0;
     93  	}
     94  
     95 -	target->surface =
     96 -	    wld_create_surface(swc.backend->context, geom->width, geom->height,
     97 -	                       WLD_FORMAT_XRGB8888,
     98 -#ifdef ENABLE_DRM
     99 -	                       WLD_DRM_FLAG_SCANOUT
    100 -#else
    101 -	                       WLD_FLAG_MAP
    102 -#endif
    103 -	    );
    104 +	target->surface = target_create_surface(screen);
    105  
    106  	if (!target->surface) {
    107  		goto error1;
    108 @@ -311,50 +323,177 @@ view_buffer_is_opaque(struct compositor_view *view)
    109  	return view->buffer_opaque;
    110  }
    111  
    112 +/* what the border goes around: the client, or with border.frame the decor too */
    113 +static struct swc_rectangle
    114 +frame_of(const struct compositor_view *view)
    115 +{
    116 +	const struct swc_rectangle *geom = &view->base.geometry;
    117 +	struct swc_rectangle frame = *geom;
    118 +
    119 +	if (view->border.frame) {
    120 +		frame.x -= (int32_t)view->decor.left;
    121 +		frame.y -= (int32_t)view->decor.top;
    122 +		frame.width += view->decor.left + view->decor.right;
    123 +		frame.height += view->decor.top + view->decor.bottom;
    124 +	}
    125 +	return frame;
    126 +}
    127 +
    128 +/* inner ring on the frame, outer ring on the inner, damaged parts only */
    129 +static void
    130 +draw_border(struct wld_renderer *renderer, const struct compositor_view *view,
    131 +            const pixman_region32_t *damage, const struct swc_rectangle *target)
    132 +{
    133 +	struct swc_rectangle frame = frame_of(view);
    134 +	int32_t in = (int32_t)view->border.inwidth;
    135 +	int32_t all = in + (int32_t)view->border.outwidth;
    136 +	pixman_region32_t frame_region, inner, outer;
    137 +
    138 +	if (!all) {
    139 +		return;
    140 +	}
    141 +	pixman_region32_init_rect(&frame_region, frame.x, frame.y, frame.width,
    142 +	                          frame.height);
    143 +	pixman_region32_init_rect(&inner, frame.x - in, frame.y - in,
    144 +	                          frame.width + 2 * in, frame.height + 2 * in);
    145 +	pixman_region32_init_rect(&outer, frame.x - all, frame.y - all,
    146 +	                          frame.width + 2 * all, frame.height + 2 * all);
    147 +	pixman_region32_subtract(&outer, &outer, &inner);
    148 +	pixman_region32_subtract(&inner, &inner, &frame_region);
    149 +	pixman_region32_intersect(&outer, &outer, (pixman_region32_t *)damage);
    150 +	pixman_region32_intersect(&inner, &inner, (pixman_region32_t *)damage);
    151 +	pixman_region32_translate(&outer, -target->x, -target->y);
    152 +	pixman_region32_translate(&inner, -target->x, -target->y);
    153 +	if (pixman_region32_not_empty(&outer)) {
    154 +		wld_fill_region(renderer, view->border.outcolor, &outer);
    155 +	}
    156 +	if (pixman_region32_not_empty(&inner)) {
    157 +		wld_fill_region(renderer, view->border.incolor, &inner);
    158 +	}
    159 +	pixman_region32_fini(&frame_region);
    160 +	pixman_region32_fini(&inner);
    161 +	pixman_region32_fini(&outer);
    162 +}
    163 +
    164 +/* a backdrop's area: the view's extents and the margin around them */
    165 +static pixman_box32_t
    166 +backdrop_box(const struct compositor_view *view)
    167 +{
    168 +	int32_t m = (int32_t)view->backdrop.margin;
    169 +
    170 +	return (pixman_box32_t){ view->extents.x1 - m, view->extents.y1 - m, view->extents.x2 + m,
    171 +	                         view->extents.y2 + m };
    172 +}
    173 +
    174 +/* what lies behind a view with a backdrop is read through its whole area,
    175 + * so a change anywhere in it repaints all of it, and that may reach another
    176 + * backdrop's area in turn */
    177 +static void
    178 +backdrop_damage(void)
    179 +{
    180 +	struct compositor_view *view;
    181 +	bool grew = true;
    182 +
    183 +	while (grew) {
    184 +		grew = false;
    185 +		wl_list_for_each (view, &compositor.views, link) {
    186 +			pixman_box32_t box = backdrop_box(view);
    187 +
    188 +			if (!view->visible || !view->backdrop.hook)
    189 +				continue;
    190 +			if (pixman_region32_contains_rectangle(&compositor.damage, &box) == PIXMAN_REGION_PART) {
    191 +				pixman_region32_union_rect(&compositor.damage, &compositor.damage, box.x1, box.y1,
    192 +				                           (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1));
    193 +				grew = true;
    194 +			}
    195 +		}
    196 +	}
    197 +}
    198 +
    199 +static void
    200 +backdrop_call(struct target *target, struct compositor_view *view, pixman_region32_t *damage)
    201 +{
    202 +	const struct swc_rectangle *tg = &target->view->geometry;
    203 +	pixman_box32_t box = backdrop_box(view), *boxes;
    204 +	pixman_region32_t area;
    205 +	struct swc_rectangle rects[64];
    206 +	struct swc_backdrop b = { 0 };
    207 +	int n;
    208 +
    209 +	pixman_region32_init_rect(&area, box.x1, box.y1, (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1));
    210 +	pixman_region32_intersect_rect(&area, &area, tg->x, tg->y, tg->width, tg->height);
    211 +	box = *pixman_region32_extents(&area);
    212 +	pixman_region32_intersect(&area, &area, damage);
    213 +	pixman_region32_subtract(&area, &area, &view->clip);
    214 +	boxes = pixman_region32_rectangles(&area, &n);
    215 +	if (n && (b.pixels = wld_target_map(swc.backend->renderer, &b.pitch, &b.format))) {
    216 +		b.area = (struct swc_rectangle){ box.x1 - tg->x, box.y1 - tg->y, (uint32_t)(box.x2 - box.x1),
    217 +		                                 (uint32_t)(box.y2 - box.y1) };
    218 +		/* more pieces than fit are given as the one box around them */
    219 +		if (n > 64) {
    220 +			box = *pixman_region32_extents(&area);
    221 +			boxes = &box;
    222 +			n = 1;
    223 +		}
    224 +		for (int i = 0; i < n; i++)
    225 +			rects[i] = (struct swc_rectangle){ boxes[i].x1 - tg->x, boxes[i].y1 - tg->y,
    226 +			                                   (uint32_t)(boxes[i].x2 - boxes[i].x1),
    227 +			                                   (uint32_t)(boxes[i].y2 - boxes[i].y1) };
    228 +		b.rects = rects;
    229 +		b.nrects = n;
    230 +		view->backdrop.hook(view->backdrop.data, &b);
    231 +		wld_target_unmap(swc.backend->renderer);
    232 +	}
    233 +	pixman_region32_fini(&area);
    234 +}
    235 +
    236 +void
    237 +compositor_view_set_backdrop(struct compositor_view *view, uint32_t margin,
    238 +                             void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data)
    239 +{
    240 +	pixman_box32_t box;
    241 +
    242 +	view->backdrop.margin = margin;
    243 +	view->backdrop.hook = hook;
    244 +	view->backdrop.data = data;
    245 +	box = backdrop_box(view);
    246 +	pixman_region32_union_rect(&compositor.damage, &compositor.damage, box.x1, box.y1,
    247 +	                           (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1));
    248 +	schedule_updates(view->base.screens);
    249 +}
    250 +
    251  static void
    252  repaint_view(struct target *target, struct compositor_view *view,
    253               pixman_region32_t *damage)
    254  {
    255 -	pixman_region32_t geom_region, buffer_region, border_region, view_damage,
    256 -	    buffer_damage, border_damage;
    257 +	pixman_region32_t buffer_region, view_damage, buffer_damage;
    258  	const struct swc_rectangle *geom = &view->base.geometry,
    259  	                           *target_geom = &target->view->geometry;
    260  	int32_t buf_x, buf_y;
    261  	uint32_t buf_w, buf_h;
    262 -	int64_t total_border;
    263  
    264  	if (!view->base.buffer) {
    265  		return;
    266  	}
    267 +	if (view->backdrop.hook)
    268 +		backdrop_call(target, view, damage);
    269  
    270  	buf_w = view->base.buffer->width;
    271  	buf_h = view->base.buffer->height;
    272  	buf_x = geom->x - view->buffer_offset_x;
    273  	buf_y = geom->y - view->buffer_offset_y;
    274  
    275 -	total_border =
    276 -	    (int64_t)view->border.outwidth + (int64_t)view->border.inwidth;
    277 -	pixman_region32_init_rect(&geom_region, geom->x, geom->y, geom->width,
    278 -	                          geom->height);
    279  	if (view->window) {
    280  		pixman_region32_init_rect(&buffer_region, geom->x, geom->y, geom->width,
    281  		                          geom->height);
    282  	} else {
    283  		pixman_region32_init_rect(&buffer_region, buf_x, buf_y, buf_w, buf_h);
    284  	}
    285 -	pixman_region32_init_rect(&border_region,
    286 -	                          geom->x - (int32_t)total_border,
    287 -	                          geom->y - (int32_t)total_border,
    288 -	                          geom->width + (uint32_t)(2 * total_border),
    289 -	                          geom->height + (uint32_t)(2 * total_border));
    290 -	pixman_region32_subtract(&border_region, &border_region, &geom_region);
    291  	pixman_region32_init_with_extents(&view_damage, &view->extents);
    292  	pixman_region32_init(&buffer_damage);
    293 -	pixman_region32_init(&border_damage);
    294  
    295  	pixman_region32_intersect(&view_damage, &view_damage, damage);
    296  	pixman_region32_subtract(&view_damage, &view_damage, &view->clip);
    297 -	pixman_region32_intersect(&border_damage, &view_damage, &border_region);
    298  	pixman_region32_intersect(&buffer_damage, &view_damage, &buffer_region);
    299  
    300  	if (pixman_region32_not_empty(&buffer_damage)) {
    301 @@ -396,45 +535,10 @@ repaint_view(struct target *target, struct compositor_view *view,
    302  		}
    303  	}
    304  
    305 +	draw_border(swc.backend->renderer, view, &view_damage, target_geom);
    306  	pixman_region32_fini(&view_damage);
    307  	pixman_region32_fini(&buffer_damage);
    308 -
    309 -	pixman_region32_t in_rect;
    310 -	pixman_region32_init_rect(&in_rect,
    311 -	                          geom->x - view->border.inwidth,
    312 -	                          geom->y - view->border.inwidth,
    313 -	                          geom->width + (2 * view->border.inwidth),
    314 -	                          geom->height + (2 * view->border.inwidth));
    315 -
    316 -	pixman_region32_t out_border;
    317 -	pixman_region32_init(&out_border);
    318 -	pixman_region32_subtract(&out_border, &border_damage, &in_rect);
    319 -
    320 -	pixman_region32_t in_border;
    321 -	pixman_region32_init(&in_border);
    322 -	pixman_region32_subtract(&in_border, &in_rect, &geom_region);
    323 -	pixman_region32_intersect(&in_border, &in_border, &border_damage);
    324 -
    325 -	pixman_region32_fini(&geom_region);
    326  	pixman_region32_fini(&buffer_region);
    327 -	pixman_region32_fini(&border_region);
    328 -
    329 -	/* Draw border */
    330 -	if (view->border.outwidth > 0 && pixman_region32_not_empty(&out_border)) {
    331 -		pixman_region32_translate(&out_border, -target_geom->x,
    332 -		                          -target_geom->y);
    333 -		wld_fill_region(swc.backend->renderer, view->border.outcolor, &out_border);
    334 -	}
    335 -
    336 -	if (view->border.inwidth > 0 && pixman_region32_not_empty(&in_border)) {
    337 -		pixman_region32_translate(&in_border, -target_geom->x, -target_geom->y);
    338 -		wld_fill_region(swc.backend->renderer, view->border.incolor, &in_border);
    339 -	}
    340 -
    341 -	pixman_region32_fini(&border_damage);
    342 -	pixman_region32_fini(&in_rect);
    343 -	pixman_region32_fini(&out_border);
    344 -	pixman_region32_fini(&in_border);
    345  
    346  	if ((view->decor.top || view->decor.right || view->decor.bottom ||
    347  	     view->decor.left)) {
    348 @@ -627,7 +731,7 @@ damage_view(struct compositor_view *view)
    349  }
    350  
    351  static void
    352 -update_extents(struct compositor_view *view)
    353 +update_extents(struct compositor_view *view, const struct wld_buffer *buffer)
    354  {
    355  	int64_t total_border =
    356  	    (int64_t)view->border.outwidth + (int64_t)view->border.inwidth;
    357 @@ -636,23 +740,20 @@ update_extents(struct compositor_view *view)
    358  	int64_t geom_w = view->base.geometry.width;
    359  	int64_t geom_h = view->base.geometry.height;
    360  
    361 -	int64_t border_x1 = geom_x - total_border;
    362 -	int64_t border_y1 = geom_y - total_border;
    363 -	int64_t border_x2 = geom_x + geom_w + total_border;
    364 -	int64_t border_y2 = geom_y + geom_h + total_border;
    365  	int64_t decor_x1 = geom_x - (int64_t)view->decor.left;
    366  	int64_t decor_y1 = geom_y - (int64_t)view->decor.top;
    367  	int64_t decor_x2 = geom_x + geom_w + (int64_t)view->decor.right;
    368  	int64_t decor_y2 = geom_y + geom_h + (int64_t)view->decor.bottom;
    369 +	int64_t border_x1 = (view->border.frame ? decor_x1 : geom_x) - total_border;
    370 +	int64_t border_y1 = (view->border.frame ? decor_y1 : geom_y) - total_border;
    371 +	int64_t border_x2 = (view->border.frame ? decor_x2 : geom_x + geom_w) + total_border;
    372 +	int64_t border_y2 = (view->border.frame ? decor_y2 : geom_y + geom_h) + total_border;
    373  
    374  	int64_t buffer_x1 = geom_x - view->buffer_offset_x;
    375  	int64_t buffer_y1 = geom_y - view->buffer_offset_y;
    376 -	int64_t buffer_x2 =
    377 -	    buffer_x1 +
    378 -	    (view->base.buffer ? view->base.buffer->width : (uint32_t)geom_w);
    379 +	int64_t buffer_x2 = buffer_x1 + (buffer ? buffer->width : (uint32_t)geom_w);
    380  	int64_t buffer_y2 =
    381 -	    buffer_y1 +
    382 -	    (view->base.buffer ? view->base.buffer->height : (uint32_t)geom_h);
    383 +	    buffer_y1 + (buffer ? buffer->height : (uint32_t)geom_h);
    384  
    385  	view->extents.x1 = clamp_i32(MIN(MIN(border_x1, decor_x1), buffer_x1));
    386  	view->extents.y1 = clamp_i32(MIN(MIN(border_y1, decor_y1), buffer_y1));
    387 @@ -1073,7 +1174,8 @@ attach(struct view *base, struct wld_buffer *buffer)
    388  	if (view_set_size(&view->base, new_width, new_height)) {
    389  		/* The view was resized. */
    390  		old_extents = view->extents;
    391 -		update_extents(view);
    392 +		/* base.buffer is still the old buffer here, size from the new one */
    393 +		update_extents(view, buffer);
    394  
    395  		if (view->visible) {
    396  			/* Damage the region that was newly uncovered or covered. */
    397 @@ -1099,7 +1201,19 @@ attach(struct view *base, struct wld_buffer *buffer)
    398  static bool
    399  move(struct view *base, int32_t x, int32_t y)
    400  {
    401 -	struct compositor_view *view = (void *)base;
    402 +	struct compositor_view *view = (void *)base, *other;
    403 +	int32_t dx = x - base->geometry.x, dy = y - base->geometry.y;
    404 +
    405 +	/* attached transients follow the parent */
    406 +	if (view->window && (dx || dy)) {
    407 +		wl_list_for_each(other, &compositor.views, link)
    408 +		{
    409 +			if (other->parent == view && other->attached && other->window) {
    410 +				view_move(&other->base, other->base.geometry.x + dx,
    411 +				          other->base.geometry.y + dy);
    412 +			}
    413 +		}
    414 +	}
    415  
    416  	if (view->visible) {
    417  		damage_below_view(view);
    418 @@ -1107,7 +1221,7 @@ move(struct view *base, int32_t x, int32_t y)
    419  	}
    420  
    421  	if (view_set_position(&view->base, x, y)) {
    422 -		update_extents(view);
    423 +		update_extents(view, view->base.buffer);
    424  
    425  		if (view->visible) {
    426  			/* Assume worst-case no clipping until we draw the next frame (in
    427 @@ -1142,12 +1256,12 @@ restack_view_for_layer(struct compositor_view *view, bool raise)
    428  			continue;
    429  		}
    430  
    431 -		if (other->stack_layer > view->stack_layer) {
    432 +		if (other->z > view->z) {
    433  			insert_after = &other->link;
    434  			continue;
    435  		}
    436  
    437 -		if (other->stack_layer == view->stack_layer) {
    438 +		if (other->z == view->z) {
    439  			found_same = true;
    440  			if (raise) {
    441  				insert_after = other->link.prev;
    442 @@ -1176,40 +1290,87 @@ restack_view_for_layer(struct compositor_view *view, bool raise)
    443  	wl_list_insert(insert_after, &view->link);
    444  }
    445  
    446 -static struct compositor_view *
    447 -view_at(int32_t x, int32_t y)
    448 +/* whether the point is on what the view shows and takes input there */
    449 +static bool
    450 +view_content_at(struct compositor_view *view, int32_t x, int32_t y)
    451  {
    452 -	struct compositor_view *view;
    453 -	struct swc_rectangle *geom;
    454 +	struct swc_rectangle *geom = &view->base.geometry;
    455  	struct swc_rectangle buffer_geom;
    456  
    457 +	if (view->window) {
    458 +		if (!rectangle_contains_point(geom, x, y)) {
    459 +			return false;
    460 +		}
    461 +	} else if (view->base.buffer) {
    462 +		buffer_geom.x = geom->x - view->buffer_offset_x;
    463 +		buffer_geom.y = geom->y - view->buffer_offset_y;
    464 +		buffer_geom.width = view->base.buffer->width;
    465 +		buffer_geom.height = view->base.buffer->height;
    466 +		if (!rectangle_contains_point(&buffer_geom, x, y)) {
    467 +			return false;
    468 +		}
    469 +	} else if (!rectangle_contains_point(geom, x, y)) {
    470 +		return false;
    471 +	}
    472 +
    473 +	return pixman_region32_contains_point(&view->surface->state.input,
    474 +	                                      x - geom->x + view->buffer_offset_x,
    475 +	                                      y - geom->y + view->buffer_offset_y,
    476 +	                                      NULL);
    477 +}
    478 +
    479 +struct compositor_view *
    480 +compositor_view_at(int32_t x, int32_t y)
    481 +{
    482 +	struct compositor_view *view;
    483 +
    484  	wl_list_for_each(view, &compositor.views, link)
    485  	{
    486 -		if (!view->visible) {
    487 +		if (!view->visible || view->no_input) {
    488  			continue;
    489  		}
    490 +		if (view_content_at(view, x, y)) {
    491 +			return view;
    492 +		}
    493 +	}
    494  
    495 -		geom = &view->base.geometry;
    496 -		if (view->window) {
    497 -			if (!rectangle_contains_point(geom, x, y)) {
    498 -				continue;
    499 -			}
    500 -		} else if (view->base.buffer) {
    501 -			buffer_geom.x = geom->x - view->buffer_offset_x;
    502 -			buffer_geom.y = geom->y - view->buffer_offset_y;
    503 -			buffer_geom.width = view->base.buffer->width;
    504 -			buffer_geom.height = view->base.buffer->height;
    505 -			if (!rectangle_contains_point(&buffer_geom, x, y)) {
    506 -				continue;
    507 -			}
    508 -		} else if (!rectangle_contains_point(geom, x, y)) {
    509 +	return NULL;
    510 +}
    511 +
    512 +/* whether the point is on the window's decoration or its border, which the
    513 + * pointer is not over for the client: the surface is not under it */
    514 +static bool
    515 +view_chrome_at(struct compositor_view *view, int32_t x, int32_t y)
    516 +{
    517 +	struct swc_rectangle *geom = &view->base.geometry;
    518 +	int64_t border = (int64_t)view->border.outwidth + (int64_t)view->border.inwidth;
    519 +
    520 +	if (!view->window) {
    521 +		return false;
    522 +	}
    523 +
    524 +	return x >= geom->x - (int64_t)view->decor.left - border &&
    525 +	       x < geom->x + (int64_t)geom->width + (int64_t)view->decor.right + border &&
    526 +	       y >= geom->y - (int64_t)view->decor.top - border &&
    527 +	       y < geom->y + (int64_t)geom->height + (int64_t)view->decor.bottom + border;
    528 +}
    529 +
    530 +/* the window whose decoration or border is the first thing under the point,
    531 + * in stacking order, NULL when it is anything else or nothing */
    532 +static struct compositor_view *
    533 +compositor_chrome_at(int32_t x, int32_t y)
    534 +{
    535 +	struct compositor_view *view;
    536 +
    537 +	wl_list_for_each(view, &compositor.views, link)
    538 +	{
    539 +		if (!view->visible || view->no_input) {
    540  			continue;
    541  		}
    542 -
    543 -		if (pixman_region32_contains_point(&view->surface->state.input,
    544 -		                                   x - geom->x + view->buffer_offset_x,
    545 -		                                   y - geom->y + view->buffer_offset_y,
    546 -		                                   NULL)) {
    547 +		if (view_content_at(view, x, y)) {
    548 +			return NULL;
    549 +		}
    550 +		if (view_chrome_at(view, x, y)) {
    551  			return view;
    552  		}
    553  	}
    554 @@ -1226,6 +1387,31 @@ window_view(struct compositor_view *view)
    555  	return (view && view->window) ? view : NULL;
    556  }
    557  
    558 +/* transients go above their parent, recursively */
    559 +static void
    560 +raise_transients(struct compositor_view *view)
    561 +{
    562 +	struct compositor_view *other, *children[32];
    563 +	unsigned n = 0;
    564 +
    565 +	wl_list_for_each(other, &compositor.views, link)
    566 +	{
    567 +		if (other->parent == view && other->window && other->visible &&
    568 +		    n < sizeof children / sizeof children[0]) {
    569 +			children[n++] = other;
    570 +		}
    571 +	}
    572 +	for (unsigned i = 0; i < n; i++) {
    573 +		/* a transient stays in its parent's layer, or it would be found
    574 +		 * among views of another */
    575 +		children[i]->z = view->z;
    576 +		wl_list_remove(&children[i]->link);
    577 +		wl_list_insert(view->link.prev, &children[i]->link);
    578 +		damage_view(children[i]);
    579 +		raise_transients(children[i]);
    580 +	}
    581 +}
    582 +
    583  void
    584  raise_window(struct compositor_view *view)
    585  {
    586 @@ -1250,13 +1436,13 @@ raise_window(struct compositor_view *view)
    587  			continue;
    588  		}
    589  
    590 -		if (other->stack_layer > STACK_LAYER_NORMAL ||
    591 +		if (other->z > view->z ||
    592  		    other->always_top) {
    593  			insert_after = &other->link;
    594  			continue;
    595  		}
    596  
    597 -		if (other->stack_layer < STACK_LAYER_NORMAL) {
    598 +		if (other->z < view->z) {
    599  			break;
    600  		}
    601  
    602 @@ -1268,6 +1454,7 @@ raise_window(struct compositor_view *view)
    603  	}
    604  
    605  	if (view == top_window) {
    606 +		raise_transients(view);
    607  		return;
    608  	}
    609  
    610 @@ -1282,22 +1469,23 @@ raise_window(struct compositor_view *view)
    611  	                           span_u32(view->extents.x1, view->extents.x2),
    612  	                           span_u32(view->extents.y1, view->extents.y2));
    613  	schedule_updates(screens);
    614 +	raise_transients(view);
    615  }
    616  
    617  void
    618  raise_window_top(struct compositor_view *view)
    619  {
    620 -	view->stack_layer = STACK_LAYER_OVERLAY;
    621 +	view->z = STACK_LAYER_OVERLAY;
    622  	restack_view_for_layer(view, true);
    623  	damage_view(view);
    624  	schedule_updates(view->base.screens);
    625  }
    626  
    627  void
    628 -compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer,
    629 +compositor_view_set_z(struct compositor_view *view, int32_t layer,
    630  	                            bool raise)
    631  {
    632 -	if (view->stack_layer == layer) {
    633 +	if (view->z == layer) {
    634  		if (raise) {
    635  			restack_view_for_layer(view, true);
    636  			damage_view(view);
    637 @@ -1307,7 +1495,7 @@ compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer,
    638  	}
    639  
    640  	damage_view(view);
    641 -	view->stack_layer = layer;
    642 +	view->z = layer;
    643  	restack_view_for_layer(view, raise);
    644  	damage_view(view);
    645  	schedule_updates(view->base.screens);
    646 @@ -1316,7 +1504,7 @@ compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer,
    647  EXPORT struct swc_window *
    648  swc_window_at(int32_t x, int32_t y)
    649  {
    650 -	struct compositor_view *view = window_view(view_at(x, y));
    651 +	struct compositor_view *view = window_view(compositor_view_at(x, y));
    652  
    653  	return view ? &view->window->base : NULL;
    654  }
    655 @@ -1426,7 +1614,9 @@ compositor_create_view(struct surface *surface)
    656  {
    657  	struct compositor_view *view;
    658  
    659 -	view = malloc(sizeof(*view));
    660 +	/* zeroed: a field set only on request (a backdrop hook) must not be
    661 +	 * what the memory held before */
    662 +	view = calloc(1, sizeof(*view));
    663  
    664  	if (!view) {
    665  		return NULL;
    666 @@ -1443,7 +1633,8 @@ compositor_create_view(struct surface *surface)
    667  	view->buffer_offset_y = 0;
    668  	view->visible = false;
    669  	view->always_top = false;
    670 -	view->stack_layer = STACK_LAYER_NORMAL;
    671 +	view->no_input = false;
    672 +	view->z = STACK_LAYER_NORMAL;
    673  	view->extents.x1 = 0;
    674  	view->extents.y1 = 0;
    675  	view->extents.x2 = 0;
    676 @@ -1612,7 +1803,7 @@ compositor_view_set_border_width(struct compositor_view *view,
    677  
    678  	/* XXX: Damage above surface for transparent surfaces? */
    679  
    680 -	update_extents(view);
    681 +	update_extents(view, view->base.buffer);
    682  
    683  	/* Damage old extents to prevent leftover border artifacts */
    684  	if (view->visible &&
    685 @@ -1634,6 +1825,32 @@ compositor_view_set_border_width(struct compositor_view *view,
    686  	update(&view->base);
    687  }
    688  
    689 +void
    690 +compositor_view_set_border_frame(struct compositor_view *view, bool frame)
    691 +{
    692 +	pixman_box32_t old_extents;
    693 +
    694 +	if (view->border.frame == frame) {
    695 +		return;
    696 +	}
    697 +
    698 +	old_extents = view->extents;
    699 +	view->border.frame = frame;
    700 +	update_extents(view, view->base.buffer);
    701 +
    702 +	/* damage the old border */
    703 +	if (view->visible) {
    704 +		pixman_region32_t old_region;
    705 +
    706 +		pixman_region32_init_with_extents(&old_region, &old_extents);
    707 +		pixman_region32_union(&compositor.damage, &compositor.damage,
    708 +		                      &old_region);
    709 +		pixman_region32_fini(&old_region);
    710 +	}
    711 +
    712 +	update(&view->base);
    713 +}
    714 +
    715  void
    716  compositor_view_set_border_color(struct compositor_view *view,
    717                                   uint32_t outcolor, uint32_t incolor)
    718 @@ -1664,7 +1881,7 @@ compositor_view_set_decor(struct compositor_view *view,
    719  	}
    720  
    721  	decor_view_set(view, decor);
    722 -	update_extents(view);
    723 +	update_extents(view, view->base.buffer);
    724  	update(&view->base);
    725  }
    726  
    727 @@ -1778,6 +1995,7 @@ update_screen(struct screen *screen)
    728  		return;
    729  	}
    730  
    731 +	backdrop_damage();
    732  	pixman_region32_init(&damage);
    733  	pixman_region32_intersect_rect(&damage, &compositor.damage, geom->x,
    734  	                               geom->y, geom->width, geom->height);
    735 @@ -1866,10 +2084,23 @@ handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx,
    736  		return false;
    737  	}
    738  
    739 -	struct compositor_view *view = view_at(x, y);
    740 +	pointer_motion_notify(x, y);
    741 +
    742 +	struct compositor_view *chrome = compositor_chrome_at(x, y);
    743 +	struct compositor_view *view = chrome ? NULL : compositor_view_at(x, y);
    744  
    745  	pointer_set_focus(swc.seat->pointer, view);
    746  
    747 +	/* The pointer over a window's decoration or border is over the window as
    748 +	 * much as over its surface: the window's handler is told as when the
    749 +	 * surface is entered, once until the pointer is anywhere else */
    750 +	if (chrome && chrome->window != compositor.chrome_window) {
    751 +		compositor.chrome_window = chrome->window;
    752 +		window_pointer_entered(chrome->window);
    753 +	} else if (!chrome) {
    754 +		compositor.chrome_window = NULL;
    755 +	}
    756 +
    757  	return false;
    758  }
    759  
    760 @@ -1887,7 +2118,7 @@ handle_button(struct pointer_handler *handler, uint32_t time,
    761  
    762  	int32_t x = wl_fixed_to_int(swc.seat->pointer->x);
    763  	int32_t y = wl_fixed_to_int(swc.seat->pointer->y);
    764 -	struct compositor_view *view = view_at(x, y);
    765 +	struct compositor_view *view = compositor_view_at(x, y);
    766  
    767  	pointer_set_focus(swc.seat->pointer, view);
    768  	raise_window(view);
    769 @@ -2046,6 +2277,31 @@ compositor_get_buffer(struct screen *screen)
    770  	return target->current_buffer;
    771  }
    772  
    773 +void
    774 +compositor_screen_resize(struct screen *screen)
    775 +{
    776 +	struct target *target = target_get(screen);
    777 +	struct wld_surface *surface;
    778 +
    779 +	if (!target) {
    780 +		return;
    781 +	}
    782 +
    783 +	if (!(surface = target_create_surface(screen))) {
    784 +		ERROR("Could not resize the target of screen %u\n", screen->id);
    785 +		return;
    786 +	}
    787 +
    788 +	/* a flip in flight ends on the old surface, forget its buffers */
    789 +	wld_destroy_surface(target->surface);
    790 +	target->surface = surface;
    791 +	target->current_buffer = NULL;
    792 +	target->next_buffer = NULL;
    793 +	compositor.pending_flips &= ~target->mask;
    794 +
    795 +	compositor_damage_all();
    796 +}
    797 +
    798  struct wld_buffer *
    799  compositor_render_to_shm(struct screen *screen)
    800  {
    801 @@ -2119,61 +2375,15 @@ compositor_render_to_shm(struct screen *screen)
    802  			pixman_region32_fini(&source_region);
    803  		}
    804  
    805 -		if ((view->border.outwidth > 0 || view->border.inwidth > 0) &&
    806 -		    view->base.buffer) {
    807 -			pixman_region32_t view_region, view_damage, border_damage;
    808 -			const struct swc_rectangle *geom = &view->base.geometry;
    809 -			const struct swc_rectangle *target_geom = &screen->base.geometry;
    810 +		if (view->base.buffer) {
    811 +			pixman_region32_t view_damage;
    812  
    813 -			pixman_region32_init_rect(&view_region, geom->x, geom->y,
    814 -			                          geom->width, geom->height);
    815  			pixman_region32_init_with_extents(&view_damage, &view->extents);
    816 -			pixman_region32_init(&border_damage);
    817 -
    818  			pixman_region32_intersect(&view_damage, &view_damage, &damage);
    819  			pixman_region32_subtract(&view_damage, &view_damage, &view->clip);
    820 -			pixman_region32_subtract(&border_damage, &view_damage,
    821 -			                         &view_region);
    822 -
    823 -			pixman_region32_t in_rect;
    824 -			pixman_region32_init_rect(&in_rect,
    825 -			                          geom->x - view->border.inwidth,
    826 -			                          geom->y - view->border.inwidth,
    827 -			                          geom->width + (2 * view->border.inwidth),
    828 -			                          geom->height +
    829 -			                              (2 * view->border.inwidth));
    830 -
    831 -			pixman_region32_t out_border;
    832 -			pixman_region32_init(&out_border);
    833 -			pixman_region32_subtract(&out_border, &border_damage, &in_rect);
    834 -
    835 -			pixman_region32_t in_border;
    836 -			pixman_region32_init(&in_border);
    837 -			pixman_region32_subtract(&in_border, &in_rect, &view_region);
    838 -			pixman_region32_intersect(&in_border, &in_border, &border_damage);
    839 -
    840 -			if (view->border.outwidth > 0 &&
    841 -			    pixman_region32_not_empty(&out_border)) {
    842 -				pixman_region32_translate(&out_border, -target_geom->x,
    843 -				                          -target_geom->y);
    844 -				wld_fill_region(swc.shm->renderer, view->border.outcolor,
    845 -				                &out_border);
    846 -			}
    847 -
    848 -			if (view->border.inwidth > 0 &&
    849 -			    pixman_region32_not_empty(&in_border)) {
    850 -				pixman_region32_translate(&in_border, -target_geom->x,
    851 -				                          -target_geom->y);
    852 -				wld_fill_region(swc.shm->renderer, view->border.incolor,
    853 -				                &in_border);
    854 -			}
    855 -
    856 -			pixman_region32_fini(&border_damage);
    857 -			pixman_region32_fini(&view_region);
    858 +			draw_border(swc.shm->renderer, view, &view_damage,
    859 +			            &screen->base.geometry);
    860  			pixman_region32_fini(&view_damage);
    861 -			pixman_region32_fini(&in_rect);
    862 -			pixman_region32_fini(&out_border);
    863 -			pixman_region32_fini(&in_border);
    864  		}
    865  
    866  		if (view->decor.top || view->decor.right || view->decor.bottom ||
    867 diff --git a/libswc/compositor.h b/libswc/compositor.h
    868 index 678c362..2442a8c 100644
    869 --- a/libswc/compositor.h
    870 +++ b/libswc/compositor.h
    871 @@ -24,6 +24,7 @@
    872  #ifndef SWC_COMPOSITOR_H
    873  #define SWC_COMPOSITOR_H
    874  
    875 +#include "swc.h"
    876  #include "view.h"
    877  
    878  #include <pixman.h>
    879 @@ -71,6 +72,8 @@ struct compositor_view {
    880  	bool buffer_opaque;
    881  	struct window *window;
    882  	struct compositor_view *parent;
    883 +	/* moves with its parent */
    884 +	bool attached;
    885  	int32_t buffer_offset_x;
    886  	int32_t buffer_offset_y;
    887  
    888 @@ -80,11 +83,21 @@ struct compositor_view {
    889  	/* Whether or not to make it always be on top of other windows */
    890  	bool always_top;
    891  
    892 +	/* a drag icon, never under the pointer */
    893 +	bool no_input;
    894 +
    895  	/* Global stacking layer for this view */
    896 -	uint32_t stack_layer;
    897 +	/* depth: a higher one is drawn over a lower, see swc_window_set_z */
    898 +	int32_t z;
    899  
    900  	/* The box that the surface covers (including it's border). */
    901  	pixman_box32_t extents;
    902 +	/* see swc_window_set_backdrop */
    903 +	struct {
    904 +		uint32_t margin;
    905 +		void (*hook)(void *data, const struct swc_backdrop *backdrop);
    906 +		void *data;
    907 +	} backdrop;
    908  
    909  	/* The region that is covered by opaque regions of surfaces above this
    910  	 * surface. */
    911 @@ -101,6 +114,9 @@ struct compositor_view {
    912  		uint32_t incolor;
    913  
    914  		bool damaged_border2;
    915 +
    916 +		/* border goes around the decor too */
    917 +		bool frame;
    918  	} border;
    919  
    920  	struct {
    921 @@ -138,6 +154,10 @@ void
    922  compositor_view_restack(struct compositor_view *view,
    923                          struct compositor_view *sibling, bool above);
    924  
    925 +/* topmost view taking input at x, y, or NULL */
    926 +struct compositor_view *
    927 +compositor_view_at(int32_t x, int32_t y);
    928 +
    929  void
    930  compositor_view_show(struct compositor_view *view);
    931  void
    932 @@ -147,6 +167,8 @@ void
    933  compositor_view_set_border_color(struct compositor_view *view,
    934                                   uint32_t outcolor, uint32_t incolor);
    935  void
    936 +compositor_view_set_border_frame(struct compositor_view *view, bool frame);
    937 +void
    938  compositor_view_set_border_width(struct compositor_view *view,
    939                                   uint32_t outwidth, uint32_t inwidth);
    940  void
    941 @@ -162,6 +184,10 @@ compositor_view_damage_decor(struct compositor_view *view);
    942  struct wld_buffer *
    943  compositor_get_buffer(struct screen *screen);
    944  
    945 +/* recreate the render target after the screen geometry changed */
    946 +void
    947 +compositor_screen_resize(struct screen *screen);
    948 +
    949  /**
    950   * render the compositor scene into a shm buffer
    951   * caller must free with wld_buffer_unreference()
    952 @@ -172,18 +198,21 @@ compositor_render_to_shm(struct screen *screen);
    953  void
    954  raise_window(struct compositor_view *view);
    955  void
    956 +compositor_view_set_backdrop(struct compositor_view *view, uint32_t margin,
    957 +                             void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data);
    958 +void
    959  raise_window_top(struct compositor_view *view);
    960  
    961  enum compositor_stack_layer {
    962 -	STACK_LAYER_BACKGROUND = 0,
    963 -	STACK_LAYER_BOTTOM = 1,
    964 -	STACK_LAYER_NORMAL = 2,
    965 -	STACK_LAYER_TOP = 3,
    966 -	STACK_LAYER_OVERLAY = 4,
    967 +	STACK_LAYER_BACKGROUND = SWC_Z_BACKGROUND,
    968 +	STACK_LAYER_BOTTOM = SWC_Z_BOTTOM,
    969 +	STACK_LAYER_NORMAL = SWC_Z_WINDOW,
    970 +	STACK_LAYER_TOP = SWC_Z_TOP,
    971 +	STACK_LAYER_OVERLAY = SWC_Z_OVERLAY,
    972  };
    973  
    974  void
    975 -compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer,
    976 +compositor_view_set_z(struct compositor_view *view, int32_t layer,
    977  	                            bool raise);
    978  
    979  #endif
    980 diff --git a/libswc/decor.c b/libswc/decor.c
    981 index 5e6eca8..8a7f481 100644
    982 --- a/libswc/decor.c
    983 +++ b/libswc/decor.c
    984 @@ -9,10 +9,40 @@
    985  #include "window.h"
    986  
    987  #include <pixman.h>
    988 +#include <stdio.h>
    989  #include <stdlib.h>
    990  #include <string.h>
    991  #include <wld/wld.h>
    992  
    993 +/* A decor font: a file when the name is a path ("/a/font.ttf:pixelsize=12",
    994 + * 12 pixels when no size is given), else a fontconfig pattern, which only a
    995 + * wld built with fontconfig can find. */
    996 +static struct wld_font *
    997 +open_font(struct wld_font_context *context, const char *name)
    998 +{
    999 +	char path[4096];
   1000 +	const char *size;
   1001 +	double px = 12;
   1002 +	size_t len;
   1003 +
   1004 +	if (name[0] != '/') {
   1005 +#ifdef WLD_FONTCONFIG
   1006 +		return wld_font_open_name(context, name);
   1007 +#else
   1008 +		return NULL;
   1009 +#endif
   1010 +	}
   1011 +	size = strstr(name, ":pixelsize=");
   1012 +	len = size ? (size_t)(size - name) : strlen(name);
   1013 +	if (len >= sizeof path)
   1014 +		return NULL;
   1015 +	memcpy(path, name, len);
   1016 +	path[len] = '\0';
   1017 +	if (size)
   1018 +		px = strtod(size + strlen(":pixelsize="), NULL);
   1019 +	return wld_font_open_file(context, path, 0, px > 0 ? px : 12);
   1020 +}
   1021 +
   1022  #define DEFAULT_DECOR_FONT "sans-serif:size=10"
   1023  
   1024  static struct wld_font_context *font_context;
   1025 @@ -731,10 +761,17 @@ decor_view_set(struct compositor_view *view, const struct swc_decor *decor)
   1026  		owned_string = strdup(text.string);
   1027  	}
   1028  
   1029 -	if (font_name) {
   1030 +	/* a font is opened once per name: a decor that changes only its
   1031 +	 * colours or parts keeps the face it has */
   1032 +	if (font_name && view->decor.font && streq(view->decor.font_name, font_name)) {
   1033 +		font = view->decor.font;
   1034 +		owned_font_name = view->decor.font_name;
   1035 +		view->decor.font = NULL;
   1036 +		view->decor.font_name = NULL;
   1037 +	} else if (font_name) {
   1038  		owned_font_name = strdup(font_name);
   1039  		if (owned_font_name && font_context) {
   1040 -			font = wld_font_open_name(font_context, font_name);
   1041 +			font = open_font(font_context, font_name);
   1042  		}
   1043  	}
   1044  
   1045 diff --git a/libswc/fb.c b/libswc/fb.c
   1046 index d3c332c..0755d94 100644
   1047 --- a/libswc/fb.c
   1048 +++ b/libswc/fb.c
   1049 @@ -81,7 +81,7 @@ fb_create_screens(struct wl_list *screens)
   1050  	if (!output) {
   1051  		return false;
   1052  	}
   1053 -	screen = screen_new(output);
   1054 +	screen = screen_new(0, output, NULL);
   1055  	if (!screen) {
   1056  		output_destroy(output);
   1057  		return false;
   1058 diff --git a/libswc/internal.h b/libswc/internal.h
   1059 index 54971c6..31fdd47 100644
   1060 --- a/libswc/internal.h
   1061 +++ b/libswc/internal.h
   1062 @@ -32,6 +32,15 @@ enum {
   1063  	SWC_EVENT_DEACTIVATED,
   1064  };
   1065  
   1066 +/* the video backend swc was started with, chosen at run time among those
   1067 + * that were built. a backend that supplies its own screens and target, such
   1068 + * as nested, leaves it unset */
   1069 +enum swc_video {
   1070 +	SWC_VIDEO_NONE,
   1071 +	SWC_VIDEO_DRM,
   1072 +	SWC_VIDEO_FB,
   1073 +};
   1074 +
   1075  struct swc {
   1076  	struct wl_display *display;
   1077  	struct wl_event_loop *event_loop;
   1078 @@ -48,6 +57,7 @@ struct swc {
   1079  	struct swc_drm *const drm;
   1080  #endif
   1081  	struct swc_backend *backend;
   1082 +	enum swc_video video;
   1083  	struct wl_global *data_device_manager;
   1084  	struct wl_global *kde_decoration_manager;
   1085  	struct wl_global *layer_shell;
   1086 @@ -57,6 +67,8 @@ struct swc {
   1087  	struct wl_global *select_manager;
   1088  	struct wl_global *subcompositor;
   1089  	struct wl_global *xdg_decoration_manager;
   1090 +	struct wl_global *xdg_dialog_manager;
   1091 +	struct wl_global *cursor_shape_manager;
   1092  	struct wl_global *xdg_output_manager;
   1093  	struct wl_global *xdg_shell;
   1094  
   1095 diff --git a/libswc/launch.c b/libswc/launch.c
   1096 index d9602f4..4144a08 100644
   1097 --- a/libswc/launch.c
   1098 +++ b/libswc/launch.c
   1099 @@ -116,6 +116,11 @@ send_request(struct swc_launch_request *request, const void *data, size_t size,
   1100  	    {.iov_base = event, .iov_len = sizeof(*event)},
   1101  	};
   1102  
   1103 +	/* no launcher socket: the zeroed fd would be stdin */
   1104 +	if (!launch.source) {
   1105 +		return false;
   1106 +	}
   1107 +
   1108  	request->serial = ++launch.next_serial;
   1109  
   1110  	if (send_fd(launch.socket, out_fd, request_iov, 1 + (size > 0)) == -1) {
   1111 diff --git a/libswc/meson.build b/libswc/meson.build
   1112 index 15fa57a..03b195a 100644
   1113 --- a/libswc/meson.build
   1114 +++ b/libswc/meson.build
   1115 @@ -3,6 +3,7 @@ install_headers('swc.h')
   1116  libswc_src = files(
   1117    'bindings.c',
   1118    'compositor.c',
   1119 +  'cursor_shape.c',
   1120    'data.c',
   1121    'data_device.c',
   1122    'data_device_manager.c',
   1123 @@ -21,6 +22,7 @@ libswc_src = files(
   1124    'region.c',
   1125    'screen.c',
   1126    'select.c',
   1127 +  'selection.c',
   1128    'snap.c',
   1129    'shell.c',
   1130    'shell_surface.c',
   1131 @@ -34,6 +36,7 @@ libswc_src = files(
   1132    'wayland_buffer.c',
   1133    'window.c',
   1134    'xdg_decoration.c',
   1135 +  'xdg_dialog.c',
   1136    'xdg_output.c',
   1137    'xdg_shell.c',
   1138  
   1139 @@ -43,12 +46,16 @@ libswc_src = files(
   1140  
   1141    xwayland.found() ? ['xserver.c', 'xwm.c'] : [],  
   1142  
   1143 -  video_backend == 'drm' ? ['dmabuf.c', 'drm.c', 'plane.c'] : [],
   1144 -  framebuffer_backend == 'fbdev' ? ['fb.c', 'fb_fbdev.c'] : [],
   1145 -  framebuffer_backend == 'wsdisplay' ? ['fb.c', 'fb_wsdisplay.c'] : [],
   1146 +  with_drm ? ['dmabuf.c', 'drm.c', 'plane.c'] : [],
   1147 +  with_fb and framebuffer_backend == 'fbdev' ? ['fb.c', 'fb_fbdev.c'] : [],
   1148 +  with_fb and framebuffer_backend == 'wsdisplay' ? ['fb.c', 'fb_wsdisplay.c'] : [],
   1149  )
   1150  libswc_src += [cursor, launch_protocol]
   1151  
   1152 +if have_nested
   1153 +  libswc_src += files('nested.c')
   1154 +endif
   1155 +
   1156  foreach proto, proto_src : protos
   1157  	libswc_src += proto_src
   1158  endforeach
   1159 diff --git a/libswc/nested.c b/libswc/nested.c
   1160 new file mode 100644
   1161 index 0000000..e44863a
   1162 --- /dev/null
   1163 +++ b/libswc/nested.c
   1164 @@ -0,0 +1,1135 @@
   1165 +/* nested backend: swc as a client of another compositor.
   1166 + * one xdg_toplevel is the screen, the parent seat feeds ours */
   1167 +
   1168 +#include "nested.h"
   1169 +#include "backend.h"
   1170 +#include "compositor.h"
   1171 +#include "internal.h"
   1172 +#include "keyboard.h"
   1173 +#include "launch/protocol.h"
   1174 +#include "output.h"
   1175 +#include "pointer.h"
   1176 +#include "screen.h"
   1177 +#include "seat.h"
   1178 +#include "swc.h"
   1179 +#include "util.h"
   1180 +#include "view.h"
   1181 +
   1182 +#include <errno.h>
   1183 +#include <limits.h>
   1184 +#include <stdio.h>
   1185 +#include <stdlib.h>
   1186 +#include <string.h>
   1187 +#include <strings.h>
   1188 +#include <sys/mman.h>
   1189 +#include <unistd.h>
   1190 +#include <wayland-client.h>
   1191 +#include <wld/wayland.h>
   1192 +#include <wld/wld.h>
   1193 +#include <xkbcommon/xkbcommon.h>
   1194 +
   1195 +#include "cursor-shape-v1-client-protocol.h"
   1196 +#include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h"
   1197 +#include "xdg-shell-client-protocol.h"
   1198 +
   1199 +#define NESTED_DEFAULT_WIDTH 1280
   1200 +#define NESTED_DEFAULT_HEIGHT 720
   1201 +#define NESTED_CURSOR_SIZE 64
   1202 +#define NESTED_OUTPUT_NAME "NESTED-1"
   1203 +#define NESTED_TITLE "swc"
   1204 +#define NESTED_CAPTURE_KEY "ctrl+alt+g"
   1205 +#define NESTED_FULLSCREEN_KEY "ctrl+alt+f"
   1206 +
   1207 +/* wl_surface.damage_buffer and wl_pointer.frame first appear here */
   1208 +#define COMPOSITOR_VERSION 4
   1209 +#define SEAT_VERSION 5
   1210 +#define OUTPUT_VERSION 2
   1211 +
   1212 +/* a key the parent window keeps for itself, never passed to clients */
   1213 +struct chord {
   1214 +	xkb_keysym_t keysym;
   1215 +	uint32_t modifiers;
   1216 +	char description[64];
   1217 +};
   1218 +
   1219 +static struct {
   1220 +	struct swc_backend backend;
   1221 +
   1222 +	struct wl_display *display;
   1223 +	struct wl_registry *registry;
   1224 +	struct wl_compositor *compositor;
   1225 +	uint32_t compositor_version;
   1226 +	struct xdg_wm_base *wm_base;
   1227 +	struct wl_output *output;
   1228 +	struct wl_seat *seat;
   1229 +	uint32_t seat_version;
   1230 +	struct wl_keyboard *keyboard;
   1231 +	struct wl_pointer *pointer;
   1232 +	struct zwp_keyboard_shortcuts_inhibit_manager_v1 *inhibit_manager;
   1233 +	struct zwp_keyboard_shortcuts_inhibitor_v1 *inhibitor;
   1234 +	struct wp_cursor_shape_manager_v1 *shape_manager;
   1235 +	struct wp_cursor_shape_device_v1 *shape_device;
   1236 +	struct wl_event_source *source;
   1237 +
   1238 +	struct wl_surface *surface;
   1239 +	struct xdg_surface *xdg_surface;
   1240 +	struct xdg_toplevel *toplevel;
   1241 +	struct wl_callback *frame;
   1242 +	struct screen *screen;
   1243 +	bool configured;
   1244 +	bool captured;
   1245 +	bool fullscreen;
   1246 +
   1247 +	/* the parent talks in logical units, swc in buffer pixels. every
   1248 +	 * size and coordinate crossing this file is converted by scale */
   1249 +	int32_t scale;
   1250 +	uint32_t width, height;
   1251 +	int32_t pending_width, pending_height;
   1252 +
   1253 +	struct {
   1254 +		struct wl_surface *surface;
   1255 +		struct wld_buffer *buffer;
   1256 +		int32_t hotspot_x, hotspot_y;
   1257 +		uint32_t enter_serial;
   1258 +		bool inside;
   1259 +		/* where the parent's pointer is, kept while released so a
   1260 +		 * capture puts ours there */
   1261 +		wl_fixed_t x, y;
   1262 +		/* the click that captured, its release goes nowhere either */
   1263 +		uint32_t swallowed;
   1264 +	} cursor;
   1265 +
   1266 +	struct {
   1267 +		enum wl_pointer_axis_source source;
   1268 +		int32_t discrete[2];
   1269 +	} axis;
   1270 +
   1271 +	/* parent keymap that arrived before the swc seat existed */
   1272 +	struct xkb_keymap *keymap;
   1273 +
   1274 +	struct chord capture_key, fullscreen_key;
   1275 +
   1276 +	/* the parent's modifier state, kept here while released so the nested
   1277 +	 * clients see none of it, and handed to swc on capture */
   1278 +	struct {
   1279 +		uint32_t depressed, latched, locked, group;
   1280 +	} mods;
   1281 +} nested;
   1282 +
   1283 +bool
   1284 +nested_requested(void)
   1285 +{
   1286 +	const char *backend = getenv("SWC_BACKEND");
   1287 +
   1288 +	if (backend) {
   1289 +		return strcmp(backend, "nested") == 0;
   1290 +	}
   1291 +	if (getenv(SWC_LAUNCH_SOCKET_ENV)) {
   1292 +		return false;
   1293 +	}
   1294 +	return getenv("WAYLAND_DISPLAY") != NULL;
   1295 +}
   1296 +
   1297 +static int32_t
   1298 +env_int(const char *name, int32_t fallback)
   1299 +{
   1300 +	const char *value = getenv(name);
   1301 +	long number;
   1302 +	char *end;
   1303 +
   1304 +	if (!value || !*value) {
   1305 +		return fallback;
   1306 +	}
   1307 +	number = strtol(value, &end, 10);
   1308 +	return *end == '\0' && number > 0 && number <= INT32_MAX
   1309 +	           ? (int32_t)number
   1310 +	           : fallback;
   1311 +}
   1312 +
   1313 +/* primary plane */
   1314 +
   1315 +static void
   1316 +handle_frame_done(void *data, struct wl_callback *callback, uint32_t time)
   1317 +{
   1318 +	wl_callback_destroy(callback);
   1319 +	nested.frame = NULL;
   1320 +	/* the clock of the parent is not ours, and clients compare this
   1321 +	 * against the timestamps swc puts on input events */
   1322 +	view_frame(&nested.screen->planes.primary.view, get_time());
   1323 +}
   1324 +
   1325 +static const struct wl_callback_listener frame_listener = {
   1326 +    .done = handle_frame_done,
   1327 +};
   1328 +
   1329 +static bool
   1330 +plane_update(struct view *view)
   1331 +{
   1332 +	return true;
   1333 +}
   1334 +
   1335 +static int
   1336 +plane_attach(struct view *view, struct wld_buffer *buffer)
   1337 +{
   1338 +	union wld_object object;
   1339 +
   1340 +	if (!buffer) {
   1341 +		return 0;
   1342 +	}
   1343 +	if (!wld_export(buffer, WLD_WAYLAND_OBJECT_BUFFER, &object)) {
   1344 +		ERROR("nested: could not export the screen buffer\n");
   1345 +		return -1;
   1346 +	}
   1347 +
   1348 +	if (nested.frame) {
   1349 +		wl_callback_destroy(nested.frame);
   1350 +	}
   1351 +	nested.frame = wl_surface_frame(nested.surface);
   1352 +	wl_callback_add_listener(nested.frame, &frame_listener, NULL);
   1353 +
   1354 +	wl_surface_set_buffer_scale(nested.surface, nested.scale);
   1355 +	wl_surface_attach(nested.surface, object.ptr, 0, 0);
   1356 +	if (nested.compositor_version >= COMPOSITOR_VERSION) {
   1357 +		wl_surface_damage_buffer(nested.surface, 0, 0, INT32_MAX, INT32_MAX);
   1358 +	} else {
   1359 +		wl_surface_damage(nested.surface, 0, 0, INT32_MAX, INT32_MAX);
   1360 +	}
   1361 +	wl_surface_commit(nested.surface);
   1362 +
   1363 +	return 0;
   1364 +}
   1365 +
   1366 +static bool
   1367 +plane_move(struct view *view, int32_t x, int32_t y)
   1368 +{
   1369 +	view_set_position(view, x, y);
   1370 +	return true;
   1371 +}
   1372 +
   1373 +static const struct view_impl plane_impl = {
   1374 +    .update = plane_update,
   1375 +    .attach = plane_attach,
   1376 +    .move = plane_move,
   1377 +};
   1378 +
   1379 +static struct wld_surface *
   1380 +create_target(struct screen *screen)
   1381 +{
   1382 +	const struct swc_rectangle *geometry = &screen->base.geometry;
   1383 +
   1384 +	return wld_wayland_create_surface(nested.backend.context, geometry->width,
   1385 +	                                  geometry->height, WLD_FORMAT_XRGB8888,
   1386 +	                                  WLD_FLAG_MAP, nested.surface);
   1387 +}
   1388 +
   1389 +/* cursor: show our cursor image as the parent cursor, no lag, no second cursor */
   1390 +
   1391 +static void
   1392 +cursor_commit(void)
   1393 +{
   1394 +	union wld_object object;
   1395 +
   1396 +	if (!nested.pointer || !nested.cursor.inside) {
   1397 +		return;
   1398 +	}
   1399 +	/* released, the window is the parent's like any other and shows the
   1400 +	 * parent's own cursor. a parent without cursor-shape gets ours */
   1401 +	if (!nested.captured && nested.shape_device) {
   1402 +		wp_cursor_shape_device_v1_set_shape(nested.shape_device, nested.cursor.enter_serial,
   1403 +		                                    WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
   1404 +		return;
   1405 +	}
   1406 +	if (!nested.cursor.buffer ||
   1407 +	    !wld_export(nested.cursor.buffer, WLD_WAYLAND_OBJECT_BUFFER, &object)) {
   1408 +		wl_pointer_set_cursor(nested.pointer, nested.cursor.enter_serial, NULL,
   1409 +		                      0, 0);
   1410 +		return;
   1411 +	}
   1412 +
   1413 +	wl_surface_set_buffer_scale(nested.cursor.surface, nested.scale);
   1414 +	wl_surface_attach(nested.cursor.surface, object.ptr, 0, 0);
   1415 +	wl_surface_damage(nested.cursor.surface, 0, 0, INT32_MAX, INT32_MAX);
   1416 +	wl_surface_commit(nested.cursor.surface);
   1417 +	wl_pointer_set_cursor(nested.pointer, nested.cursor.enter_serial,
   1418 +	                      nested.cursor.surface,
   1419 +	                      nested.cursor.hotspot_x / nested.scale,
   1420 +	                      nested.cursor.hotspot_y / nested.scale);
   1421 +}
   1422 +
   1423 +static void
   1424 +set_cursor(struct wld_buffer *buffer, int32_t hotspot_x, int32_t hotspot_y)
   1425 +{
   1426 +	nested.cursor.buffer = buffer;
   1427 +	nested.cursor.hotspot_x = hotspot_x;
   1428 +	nested.cursor.hotspot_y = hotspot_y;
   1429 +	cursor_commit();
   1430 +}
   1431 +
   1432 +/* input capture like a vm window: a click or the chord captures,
   1433 + * the chord releases. while captured the parent stops using its own
   1434 + * shortcuts. a parent without the inhibit protocol keeps them */
   1435 +
   1436 +static void
   1437 +update_title(void)
   1438 +{
   1439 +	char title[2 * sizeof(nested.capture_key.description) + 64];
   1440 +
   1441 +	snprintf(title, sizeof(title), NESTED_TITLE " (%s %s input, %s fullscreen)",
   1442 +	         nested.capture_key.description,
   1443 +	         nested.captured ? "releases" : "captures",
   1444 +	         nested.fullscreen_key.description);
   1445 +	xdg_toplevel_set_title(nested.toplevel, title);
   1446 +}
   1447 +
   1448 +static void
   1449 +capture(void)
   1450 +{
   1451 +	if (nested.captured) {
   1452 +		return;
   1453 +	}
   1454 +	nested.captured = true;
   1455 +	if (nested.inhibit_manager && nested.seat) {
   1456 +		nested.inhibitor =
   1457 +		    zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(
   1458 +		        nested.inhibit_manager, nested.surface, nested.seat);
   1459 +	}
   1460 +	if (swc.seat) {
   1461 +		keyboard_handle_modifiers(swc.seat->keyboard, nested.mods.depressed,
   1462 +		                          nested.mods.latched, nested.mods.locked,
   1463 +		                          nested.mods.group);
   1464 +	}
   1465 +	if (swc.seat && nested.cursor.inside) {
   1466 +		pointer_handle_absolute_motion(swc.seat->pointer, get_time(),
   1467 +		                               nested.cursor.x * nested.scale,
   1468 +		                               nested.cursor.y * nested.scale);
   1469 +		pointer_handle_frame(swc.seat->pointer);
   1470 +	}
   1471 +	cursor_commit();
   1472 +	update_title();
   1473 +}
   1474 +
   1475 +static void
   1476 +release(void)
   1477 +{
   1478 +	if (!nested.captured) {
   1479 +		return;
   1480 +	}
   1481 +	nested.captured = false;
   1482 +	if (nested.inhibitor) {
   1483 +		zwp_keyboard_shortcuts_inhibitor_v1_destroy(nested.inhibitor);
   1484 +		nested.inhibitor = NULL;
   1485 +	}
   1486 +	/* the chord that got us here is still held, and its release goes to
   1487 +	 * whatever the parent focuses next */
   1488 +	if (swc.seat) {
   1489 +		keyboard_reset(swc.seat->keyboard);
   1490 +	}
   1491 +	cursor_commit();
   1492 +	if (nested.toplevel) {
   1493 +		update_title();
   1494 +	}
   1495 +}
   1496 +
   1497 +static uint32_t
   1498 +modifier_from_name(const char *name)
   1499 +{
   1500 +	static const struct {
   1501 +		const char *name;
   1502 +		uint32_t modifier;
   1503 +	} names[] = {
   1504 +	    {"ctrl", SWC_MOD_CTRL},  {"control", SWC_MOD_CTRL},
   1505 +	    {"alt", SWC_MOD_ALT},    {"mod1", SWC_MOD_ALT},
   1506 +	    {"super", SWC_MOD_LOGO}, {"logo", SWC_MOD_LOGO},
   1507 +	    {"mod4", SWC_MOD_LOGO},  {"shift", SWC_MOD_SHIFT},
   1508 +	};
   1509 +	size_t i;
   1510 +
   1511 +	for (i = 0; i < ARRAY_LENGTH(names); i++) {
   1512 +		if (strcasecmp(name, names[i].name) == 0) {
   1513 +			return names[i].modifier;
   1514 +		}
   1515 +	}
   1516 +	return 0;
   1517 +}
   1518 +
   1519 +/* "ctrl+alt+g": all but the last token are modifiers.
   1520 + * the environment variable wins, "none" turns it off */
   1521 +static void
   1522 +parse_chord(struct chord *chord, const char *variable, const char *fallback)
   1523 +{
   1524 +	const char *spec = getenv(variable);
   1525 +	char buffer[sizeof(chord->description)];
   1526 +	char *token, *next, *save;
   1527 +
   1528 +	if (!spec || !*spec) {
   1529 +		spec = fallback;
   1530 +	}
   1531 +	snprintf(buffer, sizeof(buffer), "%s", spec);
   1532 +	snprintf(chord->description, sizeof(chord->description), "%s", spec);
   1533 +	chord->modifiers = 0;
   1534 +	chord->keysym = XKB_KEY_NoSymbol;
   1535 +
   1536 +	for (token = strtok_r(buffer, "+", &save); token; token = next) {
   1537 +		next = strtok_r(NULL, "+", &save);
   1538 +		if (next) {
   1539 +			chord->modifiers |= modifier_from_name(token);
   1540 +		} else {
   1541 +			chord->keysym =
   1542 +			    xkb_keysym_from_name(token, XKB_KEYSYM_CASE_INSENSITIVE);
   1543 +		}
   1544 +	}
   1545 +}
   1546 +
   1547 +/* match the keysym ignoring its level */
   1548 +static uint32_t
   1549 +held_modifiers(struct xkb_keymap *map)
   1550 +{
   1551 +	static const struct {
   1552 +		const char *name;
   1553 +		uint32_t mod;
   1554 +	} names[] = {
   1555 +	    {XKB_MOD_NAME_CTRL, SWC_MOD_CTRL},
   1556 +	    {XKB_MOD_NAME_ALT, SWC_MOD_ALT},
   1557 +	    {XKB_MOD_NAME_LOGO, SWC_MOD_LOGO},
   1558 +	    {XKB_MOD_NAME_SHIFT, SWC_MOD_SHIFT},
   1559 +	};
   1560 +	struct xkb_state *state = xkb_state_new(map);
   1561 +	uint32_t mods = 0;
   1562 +	size_t i;
   1563 +
   1564 +	if (!state) {
   1565 +		return 0;
   1566 +	}
   1567 +	xkb_state_update_mask(state, nested.mods.depressed, nested.mods.latched,
   1568 +	                      nested.mods.locked, 0, 0, nested.mods.group);
   1569 +	for (i = 0; i < ARRAY_LENGTH(names); i++) {
   1570 +		if (xkb_state_mod_name_is_active(state, names[i].name,
   1571 +		                                 XKB_STATE_MODS_EFFECTIVE) > 0) {
   1572 +			mods |= names[i].mod;
   1573 +		}
   1574 +	}
   1575 +	xkb_state_unref(state);
   1576 +	return mods;
   1577 +}
   1578 +
   1579 +/* match the keysym ignoring its level */
   1580 +static bool
   1581 +is_chord(const struct chord *chord, struct keyboard *keyboard, uint32_t key)
   1582 +{
   1583 +	struct xkb_keymap *map = keyboard->xkb.keymap.map;
   1584 +	const xkb_keysym_t *syms;
   1585 +
   1586 +	if (chord->keysym == XKB_KEY_NoSymbol ||
   1587 +	    (held_modifiers(map) & chord->modifiers) != chord->modifiers) {
   1588 +		return false;
   1589 +	}
   1590 +	if (xkb_keymap_key_get_syms_by_level(map, XKB_KEY(key), nested.mods.group, 0,
   1591 +	                                     &syms) < 1) {
   1592 +		return false;
   1593 +	}
   1594 +	return xkb_keysym_to_lower(syms[0]) == xkb_keysym_to_lower(chord->keysym);
   1595 +}
   1596 +
   1597 +static void
   1598 +toggle_fullscreen(void)
   1599 +{
   1600 +	if (nested.fullscreen) {
   1601 +		xdg_toplevel_unset_fullscreen(nested.toplevel);
   1602 +	} else {
   1603 +		xdg_toplevel_set_fullscreen(nested.toplevel, NULL);
   1604 +	}
   1605 +}
   1606 +
   1607 +/* keyboard */
   1608 +
   1609 +static void
   1610 +apply_keymap(void)
   1611 +{
   1612 +	if (!nested.keymap || !swc.seat) {
   1613 +		return;
   1614 +	}
   1615 +	if (!keyboard_set_keymap(swc.seat->keyboard, nested.keymap)) {
   1616 +		WARNING("nested: could not adopt the parent keymap\n");
   1617 +	}
   1618 +	xkb_keymap_unref(nested.keymap);
   1619 +	nested.keymap = NULL;
   1620 +}
   1621 +
   1622 +/* raw key codes mean the same only with the same keymap on both sides */
   1623 +static void
   1624 +handle_keyboard_keymap(void *data, struct wl_keyboard *keyboard,
   1625 +                       uint32_t format, int32_t fd, uint32_t size)
   1626 +{
   1627 +	struct xkb_context *context;
   1628 +	struct xkb_keymap *keymap = NULL;
   1629 +	char *string;
   1630 +
   1631 +	if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
   1632 +		close(fd);
   1633 +		return;
   1634 +	}
   1635 +	string = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
   1636 +	close(fd);
   1637 +	if (string == MAP_FAILED) {
   1638 +		return;
   1639 +	}
   1640 +	if ((context = xkb_context_new(XKB_CONTEXT_NO_FLAGS))) {
   1641 +		keymap = xkb_keymap_new_from_buffer(context, string,
   1642 +		                                    strnlen(string, size),
   1643 +		                                    XKB_KEYMAP_FORMAT_TEXT_V1,
   1644 +		                                    XKB_KEYMAP_COMPILE_NO_FLAGS);
   1645 +		xkb_context_unref(context);
   1646 +	}
   1647 +	munmap(string, size);
   1648 +	if (!keymap) {
   1649 +		WARNING("nested: could not compile the parent keymap\n");
   1650 +		return;
   1651 +	}
   1652 +
   1653 +	if (nested.keymap) {
   1654 +		xkb_keymap_unref(nested.keymap);
   1655 +	}
   1656 +	nested.keymap = keymap;
   1657 +	apply_keymap();
   1658 +}
   1659 +
   1660 +static void
   1661 +handle_keyboard_enter(void *data, struct wl_keyboard *keyboard,
   1662 +                      uint32_t serial, struct wl_surface *surface,
   1663 +                      struct wl_array *keys)
   1664 +{
   1665 +}
   1666 +
   1667 +/* keys held when focus leaves are never released, so reset them */
   1668 +static void
   1669 +handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
   1670 +                      uint32_t serial, struct wl_surface *surface)
   1671 +{
   1672 +	if (swc.seat) {
   1673 +		keyboard_reset(swc.seat->keyboard);
   1674 +	}
   1675 +}
   1676 +
   1677 +static void
   1678 +handle_keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial,
   1679 +                    uint32_t time, uint32_t key, uint32_t state)
   1680 +{
   1681 +	if (!swc.seat) {
   1682 +		return;
   1683 +	}
   1684 +	if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
   1685 +		if (is_chord(&nested.capture_key, swc.seat->keyboard, key)) {
   1686 +			if (nested.captured) {
   1687 +				release();
   1688 +			} else {
   1689 +				capture();
   1690 +			}
   1691 +			return;
   1692 +		}
   1693 +		if (is_chord(&nested.fullscreen_key, swc.seat->keyboard, key)) {
   1694 +			toggle_fullscreen();
   1695 +			return;
   1696 +		}
   1697 +	}
   1698 +	/* released, keys are the parent's. the chords above still see the
   1699 +	 * modifiers, which keep being tracked */
   1700 +	if (!nested.captured) {
   1701 +		return;
   1702 +	}
   1703 +	keyboard_handle_key(swc.seat->keyboard, get_time(), key, state);
   1704 +}
   1705 +
   1706 +/* locks, layout group and modifiers held at focus in */
   1707 +static void
   1708 +handle_keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
   1709 +                          uint32_t serial, uint32_t depressed,
   1710 +                          uint32_t latched, uint32_t locked, uint32_t group)
   1711 +{
   1712 +	nested.mods.depressed = depressed;
   1713 +	nested.mods.latched = latched;
   1714 +	nested.mods.locked = locked;
   1715 +	nested.mods.group = group;
   1716 +	if (swc.seat && nested.captured) {
   1717 +		keyboard_handle_modifiers(swc.seat->keyboard, depressed, latched,
   1718 +		                          locked, group);
   1719 +	}
   1720 +}
   1721 +
   1722 +static void
   1723 +handle_keyboard_repeat_info(void *data, struct wl_keyboard *keyboard,
   1724 +                            int32_t rate, int32_t delay)
   1725 +{
   1726 +}
   1727 +
   1728 +static const struct wl_keyboard_listener keyboard_listener = {
   1729 +    .keymap = handle_keyboard_keymap,
   1730 +    .enter = handle_keyboard_enter,
   1731 +    .leave = handle_keyboard_leave,
   1732 +    .key = handle_keyboard_key,
   1733 +    .modifiers = handle_keyboard_modifiers,
   1734 +    .repeat_info = handle_keyboard_repeat_info,
   1735 +};
   1736 +
   1737 +/* pointer */
   1738 +
   1739 +/* seats older than SEAT_VERSION send no pointer.frame, clients wait for one */
   1740 +static void
   1741 +frame_legacy(void)
   1742 +{
   1743 +	if (nested.seat_version < SEAT_VERSION) {
   1744 +		pointer_handle_frame(swc.seat->pointer);
   1745 +	}
   1746 +}
   1747 +
   1748 +static void
   1749 +handle_pointer_enter(void *data, struct wl_pointer *pointer, uint32_t serial,
   1750 +                     struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
   1751 +{
   1752 +	nested.cursor.inside = true;
   1753 +	nested.cursor.enter_serial = serial;
   1754 +	nested.cursor.x = x;
   1755 +	nested.cursor.y = y;
   1756 +	cursor_commit();
   1757 +	if (!swc.seat || !nested.captured) {
   1758 +		return;
   1759 +	}
   1760 +	pointer_handle_absolute_motion(swc.seat->pointer, get_time(),
   1761 +	                               x * nested.scale, y * nested.scale);
   1762 +	frame_legacy();
   1763 +}
   1764 +
   1765 +static void
   1766 +handle_pointer_leave(void *data, struct wl_pointer *pointer, uint32_t serial,
   1767 +                     struct wl_surface *surface)
   1768 +{
   1769 +	nested.cursor.inside = false;
   1770 +}
   1771 +
   1772 +static void
   1773 +handle_pointer_motion(void *data, struct wl_pointer *pointer, uint32_t time,
   1774 +                      wl_fixed_t x, wl_fixed_t y)
   1775 +{
   1776 +	nested.cursor.x = x;
   1777 +	nested.cursor.y = y;
   1778 +	if (!swc.seat || !nested.captured) {
   1779 +		return;
   1780 +	}
   1781 +	pointer_handle_absolute_motion(swc.seat->pointer, get_time(),
   1782 +	                               x * nested.scale, y * nested.scale);
   1783 +	frame_legacy();
   1784 +}
   1785 +
   1786 +static void
   1787 +handle_pointer_button(void *data, struct wl_pointer *pointer, uint32_t serial,
   1788 +                      uint32_t time, uint32_t button, uint32_t state)
   1789 +{
   1790 +	if (!swc.seat) {
   1791 +		return;
   1792 +	}
   1793 +	/* released, a click only captures, as in a virtual machine window */
   1794 +	if (!nested.captured) {
   1795 +		if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
   1796 +			nested.cursor.swallowed = button;
   1797 +			capture();
   1798 +		}
   1799 +		return;
   1800 +	}
   1801 +	if (button == nested.cursor.swallowed) {
   1802 +		nested.cursor.swallowed = 0;
   1803 +		if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
   1804 +			return;
   1805 +		}
   1806 +	}
   1807 +	pointer_handle_button(swc.seat->pointer, get_time(), button, state);
   1808 +	frame_legacy();
   1809 +}
   1810 +
   1811 +static void
   1812 +handle_pointer_axis(void *data, struct wl_pointer *pointer, uint32_t time,
   1813 +                    uint32_t axis, wl_fixed_t value)
   1814 +{
   1815 +	int32_t discrete = 0;
   1816 +
   1817 +	if (!swc.seat || !nested.captured) {
   1818 +		return;
   1819 +	}
   1820 +	if (axis < ARRAY_LENGTH(nested.axis.discrete)) {
   1821 +		discrete = nested.axis.discrete[axis];
   1822 +		nested.axis.discrete[axis] = 0;
   1823 +	}
   1824 +	pointer_handle_axis(swc.seat->pointer, get_time(), axis,
   1825 +	                    nested.axis.source, value, discrete * 120);
   1826 +	frame_legacy();
   1827 +}
   1828 +
   1829 +static void
   1830 +handle_pointer_frame(void *data, struct wl_pointer *pointer)
   1831 +{
   1832 +	nested.axis.source = WL_POINTER_AXIS_SOURCE_WHEEL;
   1833 +	if (swc.seat && nested.captured) {
   1834 +		pointer_handle_frame(swc.seat->pointer);
   1835 +	}
   1836 +}
   1837 +
   1838 +static void
   1839 +handle_pointer_axis_source(void *data, struct wl_pointer *pointer,
   1840 +                           uint32_t source)
   1841 +{
   1842 +	nested.axis.source = source;
   1843 +}
   1844 +
   1845 +static void
   1846 +handle_pointer_axis_stop(void *data, struct wl_pointer *pointer, uint32_t time,
   1847 +                         uint32_t axis)
   1848 +{
   1849 +}
   1850 +
   1851 +static void
   1852 +handle_pointer_axis_discrete(void *data, struct wl_pointer *pointer,
   1853 +                             uint32_t axis, int32_t discrete)
   1854 +{
   1855 +	if (axis < ARRAY_LENGTH(nested.axis.discrete)) {
   1856 +		nested.axis.discrete[axis] = discrete;
   1857 +	}
   1858 +}
   1859 +
   1860 +static const struct wl_pointer_listener pointer_listener = {
   1861 +    .enter = handle_pointer_enter,
   1862 +    .leave = handle_pointer_leave,
   1863 +    .motion = handle_pointer_motion,
   1864 +    .button = handle_pointer_button,
   1865 +    .axis = handle_pointer_axis,
   1866 +    .frame = handle_pointer_frame,
   1867 +    .axis_source = handle_pointer_axis_source,
   1868 +    .axis_stop = handle_pointer_axis_stop,
   1869 +    .axis_discrete = handle_pointer_axis_discrete,
   1870 +};
   1871 +
   1872 +/* seat */
   1873 +
   1874 +static void
   1875 +handle_seat_capabilities(void *data, struct wl_seat *seat,
   1876 +                         uint32_t capabilities)
   1877 +{
   1878 +	bool has_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
   1879 +	bool has_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER;
   1880 +
   1881 +	if (has_keyboard && !nested.keyboard) {
   1882 +		nested.keyboard = wl_seat_get_keyboard(seat);
   1883 +		wl_keyboard_add_listener(nested.keyboard, &keyboard_listener, NULL);
   1884 +	} else if (!has_keyboard && nested.keyboard) {
   1885 +		wl_keyboard_destroy(nested.keyboard);
   1886 +		nested.keyboard = NULL;
   1887 +	}
   1888 +
   1889 +	if (has_pointer && !nested.pointer) {
   1890 +		nested.pointer = wl_seat_get_pointer(seat);
   1891 +		wl_pointer_add_listener(nested.pointer, &pointer_listener, NULL);
   1892 +		if (nested.shape_manager)
   1893 +			nested.shape_device = wp_cursor_shape_manager_v1_get_pointer(
   1894 +			    nested.shape_manager, nested.pointer);
   1895 +	} else if (!has_pointer && nested.pointer) {
   1896 +		if (nested.shape_device) {
   1897 +			wp_cursor_shape_device_v1_destroy(nested.shape_device);
   1898 +			nested.shape_device = NULL;
   1899 +		}
   1900 +		wl_pointer_destroy(nested.pointer);
   1901 +		nested.pointer = NULL;
   1902 +		nested.cursor.inside = false;
   1903 +	}
   1904 +}
   1905 +
   1906 +static void
   1907 +handle_seat_name(void *data, struct wl_seat *seat, const char *name)
   1908 +{
   1909 +}
   1910 +
   1911 +static const struct wl_seat_listener seat_listener = {
   1912 +    .capabilities = handle_seat_capabilities,
   1913 +    .name = handle_seat_name,
   1914 +};
   1915 +
   1916 +/* output: only its scale matters */
   1917 +
   1918 +static void
   1919 +handle_output_geometry(void *data, struct wl_output *output, int32_t x,
   1920 +                       int32_t y, int32_t physical_width,
   1921 +                       int32_t physical_height, int32_t subpixel,
   1922 +                       const char *make, const char *model, int32_t transform)
   1923 +{
   1924 +}
   1925 +
   1926 +static void
   1927 +handle_output_mode(void *data, struct wl_output *output, uint32_t flags,
   1928 +                   int32_t width, int32_t height, int32_t refresh)
   1929 +{
   1930 +}
   1931 +
   1932 +static void
   1933 +handle_output_done(void *data, struct wl_output *output)
   1934 +{
   1935 +}
   1936 +
   1937 +static void
   1938 +handle_output_scale(void *data, struct wl_output *output, int32_t factor)
   1939 +{
   1940 +	/* SWC_NESTED_SCALE pins the scale. 1 on a dense display makes the
   1941 +	 * parent magnify us, which is what a session with no scaling of its
   1942 +	 * own wants */
   1943 +	if (factor > 0 && !nested.surface && !getenv("SWC_NESTED_SCALE")) {
   1944 +		nested.scale = factor;
   1945 +	}
   1946 +}
   1947 +
   1948 +static const struct wl_output_listener output_listener = {
   1949 +    .geometry = handle_output_geometry,
   1950 +    .mode = handle_output_mode,
   1951 +    .done = handle_output_done,
   1952 +    .scale = handle_output_scale,
   1953 +};
   1954 +
   1955 +/* shell surface */
   1956 +
   1957 +static void
   1958 +handle_wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial)
   1959 +{
   1960 +	xdg_wm_base_pong(wm_base, serial);
   1961 +}
   1962 +
   1963 +static const struct xdg_wm_base_listener wm_base_listener = {
   1964 +    .ping = handle_wm_base_ping,
   1965 +};
   1966 +
   1967 +static void
   1968 +resize(uint32_t width, uint32_t height)
   1969 +{
   1970 +	if (width == nested.width && height == nested.height) {
   1971 +		return;
   1972 +	}
   1973 +	nested.width = width;
   1974 +	nested.height = height;
   1975 +	if (nested.screen) {
   1976 +		screen_resize(nested.screen, width, height);
   1977 +	}
   1978 +}
   1979 +
   1980 +static void
   1981 +handle_xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
   1982 +                             uint32_t serial)
   1983 +{
   1984 +	xdg_surface_ack_configure(xdg_surface, serial);
   1985 +	nested.configured = true;
   1986 +
   1987 +	if (nested.pending_width > 0 && nested.pending_height > 0) {
   1988 +		resize((uint32_t)(nested.pending_width * nested.scale),
   1989 +		       (uint32_t)(nested.pending_height * nested.scale));
   1990 +	}
   1991 +	nested.pending_width = 0;
   1992 +	nested.pending_height = 0;
   1993 +}
   1994 +
   1995 +static const struct xdg_surface_listener xdg_surface_listener = {
   1996 +    .configure = handle_xdg_surface_configure,
   1997 +};
   1998 +
   1999 +/* a 0 here means the parent leaves the size to us, so keep ours */
   2000 +static void
   2001 +handle_toplevel_configure(void *data, struct xdg_toplevel *toplevel,
   2002 +                          int32_t width, int32_t height,
   2003 +                          struct wl_array *states)
   2004 +{
   2005 +	uint32_t *state;
   2006 +
   2007 +	nested.pending_width = width;
   2008 +	nested.pending_height = height;
   2009 +	nested.fullscreen = false;
   2010 +	wl_array_for_each(state, states) {
   2011 +		if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) {
   2012 +			nested.fullscreen = true;
   2013 +		}
   2014 +	}
   2015 +}
   2016 +
   2017 +static void
   2018 +handle_toplevel_close(void *data, struct xdg_toplevel *toplevel)
   2019 +{
   2020 +	wl_display_terminate(swc.display);
   2021 +}
   2022 +
   2023 +static const struct xdg_toplevel_listener toplevel_listener = {
   2024 +    .configure = handle_toplevel_configure,
   2025 +    .close = handle_toplevel_close,
   2026 +};
   2027 +
   2028 +static bool
   2029 +create_screens(struct wl_list *screens)
   2030 +{
   2031 +	struct output *output;
   2032 +	unsigned width = NESTED_DEFAULT_WIDTH, height = NESTED_DEFAULT_HEIGHT;
   2033 +	const char *size = getenv("SWC_NESTED_SIZE");
   2034 +
   2035 +	if (size && (sscanf(size, "%ux%u", &width, &height) != 2 || width == 0 ||
   2036 +	             height == 0)) {
   2037 +		WARNING("nested: ignoring malformed SWC_NESTED_SIZE '%s'\n", size);
   2038 +		width = NESTED_DEFAULT_WIDTH;
   2039 +		height = NESTED_DEFAULT_HEIGHT;
   2040 +	}
   2041 +	nested.width = width * (uint32_t)nested.scale;
   2042 +	nested.height = height * (uint32_t)nested.scale;
   2043 +
   2044 +	nested.surface = wl_compositor_create_surface(nested.compositor);
   2045 +	nested.cursor.surface = wl_compositor_create_surface(nested.compositor);
   2046 +	if (!nested.surface || !nested.cursor.surface) {
   2047 +		return false;
   2048 +	}
   2049 +	nested.xdg_surface =
   2050 +	    xdg_wm_base_get_xdg_surface(nested.wm_base, nested.surface);
   2051 +	xdg_surface_add_listener(nested.xdg_surface, &xdg_surface_listener, NULL);
   2052 +	nested.toplevel = xdg_surface_get_toplevel(nested.xdg_surface);
   2053 +	xdg_toplevel_add_listener(nested.toplevel, &toplevel_listener, NULL);
   2054 +	update_title();
   2055 +	xdg_toplevel_set_app_id(nested.toplevel, NESTED_TITLE);
   2056 +	wl_surface_commit(nested.surface);
   2057 +
   2058 +	/* attaching a buffer before the first configure is a protocol
   2059 +	 * error, and a tiling parent answers with the size it really gives
   2060 +	 * us, which has to be known before the screen exists */
   2061 +	while (!nested.configured) {
   2062 +		if (wl_display_dispatch(nested.display) < 0) {
   2063 +			ERROR("nested: lost the parent before the first configure\n");
   2064 +			return false;
   2065 +		}
   2066 +	}
   2067 +
   2068 +	output = output_new_fb(nested.width, nested.height, NESTED_OUTPUT_NAME);
   2069 +	if (!output) {
   2070 +		return false;
   2071 +	}
   2072 +	nested.screen = screen_new_virtual(output, &plane_impl);
   2073 +	if (!nested.screen) {
   2074 +		output_destroy(output);
   2075 +		return false;
   2076 +	}
   2077 +	output->screen = nested.screen;
   2078 +	wl_list_insert(screens, &nested.screen->link);
   2079 +
   2080 +	return true;
   2081 +}
   2082 +
   2083 +/* registry */
   2084 +
   2085 +static void
   2086 +handle_global(void *data, struct wl_registry *registry, uint32_t name,
   2087 +              const char *interface, uint32_t version)
   2088 +{
   2089 +	if (strcmp(interface, wl_compositor_interface.name) == 0) {
   2090 +		nested.compositor_version = MIN(version, COMPOSITOR_VERSION);
   2091 +		nested.compositor =
   2092 +		    wl_registry_bind(registry, name, &wl_compositor_interface,
   2093 +		                     nested.compositor_version);
   2094 +	} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
   2095 +		nested.wm_base =
   2096 +		    wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
   2097 +		xdg_wm_base_add_listener(nested.wm_base, &wm_base_listener, NULL);
   2098 +	} else if (strcmp(interface, wl_seat_interface.name) == 0 && !nested.seat) {
   2099 +		nested.seat_version = MIN(version, SEAT_VERSION);
   2100 +		nested.seat = wl_registry_bind(registry, name, &wl_seat_interface,
   2101 +		                               nested.seat_version);
   2102 +		wl_seat_add_listener(nested.seat, &seat_listener, NULL);
   2103 +	} else if (strcmp(interface, wl_output_interface.name) == 0 &&
   2104 +	           !nested.output) {
   2105 +		nested.output = wl_registry_bind(registry, name, &wl_output_interface,
   2106 +		                                 MIN(version, OUTPUT_VERSION));
   2107 +		wl_output_add_listener(nested.output, &output_listener, NULL);
   2108 +	} else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
   2109 +		nested.shape_manager = wl_registry_bind(
   2110 +		    registry, name, &wp_cursor_shape_manager_v1_interface, 1);
   2111 +	} else if (strcmp(interface,
   2112 +	                  zwp_keyboard_shortcuts_inhibit_manager_v1_interface
   2113 +	                      .name) == 0) {
   2114 +		nested.inhibit_manager = wl_registry_bind(
   2115 +		    registry, name,
   2116 +		    &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
   2117 +	}
   2118 +}
   2119 +
   2120 +static void
   2121 +handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
   2122 +{
   2123 +}
   2124 +
   2125 +static const struct wl_registry_listener registry_listener = {
   2126 +    .global = handle_global,
   2127 +    .global_remove = handle_global_remove,
   2128 +};
   2129 +
   2130 +/* the parent connection is in our event loop. a check source drains
   2131 + * replies already read, which nothing else wakes the loop for */
   2132 +static int
   2133 +handle_display(int fd, uint32_t mask, void *data)
   2134 +{
   2135 +	int count;
   2136 +
   2137 +	if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
   2138 +		ERROR("nested: the parent compositor went away\n");
   2139 +		wl_display_terminate(swc.display);
   2140 +		return 0;
   2141 +	}
   2142 +
   2143 +	count = mask & WL_EVENT_READABLE
   2144 +	            ? wl_display_dispatch(nested.display)
   2145 +	            : wl_display_dispatch_pending(nested.display);
   2146 +	if (count < 0 ||
   2147 +	    (wl_display_flush(nested.display) < 0 && errno != EAGAIN)) {
   2148 +		ERROR("nested: parent connection failed: %s\n", strerror(errno));
   2149 +		wl_display_terminate(swc.display);
   2150 +		return 0;
   2151 +	}
   2152 +	return count;
   2153 +}
   2154 +
   2155 +/* the seat exists only now, well after the parent sent its keymap */
   2156 +static void
   2157 +start(void)
   2158 +{
   2159 +	apply_keymap();
   2160 +	swc_activate();
   2161 +}
   2162 +
   2163 +static void
   2164 +finalize(void)
   2165 +{
   2166 +	release();
   2167 +	if (nested.keymap) {
   2168 +		xkb_keymap_unref(nested.keymap);
   2169 +	}
   2170 +	if (nested.source) {
   2171 +		wl_event_source_remove(nested.source);
   2172 +	}
   2173 +	if (nested.frame) {
   2174 +		wl_callback_destroy(nested.frame);
   2175 +	}
   2176 +	if (nested.toplevel) {
   2177 +		xdg_toplevel_destroy(nested.toplevel);
   2178 +	}
   2179 +	if (nested.xdg_surface) {
   2180 +		xdg_surface_destroy(nested.xdg_surface);
   2181 +	}
   2182 +	if (nested.surface) {
   2183 +		wl_surface_destroy(nested.surface);
   2184 +	}
   2185 +	if (nested.cursor.surface) {
   2186 +		wl_surface_destroy(nested.cursor.surface);
   2187 +	}
   2188 +	if (nested.backend.renderer) {
   2189 +		wld_destroy_renderer(nested.backend.renderer);
   2190 +	}
   2191 +	if (nested.backend.context) {
   2192 +		wld_destroy_context(nested.backend.context);
   2193 +	}
   2194 +	if (nested.shape_device) {
   2195 +		wp_cursor_shape_device_v1_destroy(nested.shape_device);
   2196 +	}
   2197 +	if (nested.pointer) {
   2198 +		wl_pointer_destroy(nested.pointer);
   2199 +	}
   2200 +	if (nested.keyboard) {
   2201 +		wl_keyboard_destroy(nested.keyboard);
   2202 +	}
   2203 +	if (nested.seat) {
   2204 +		wl_seat_destroy(nested.seat);
   2205 +	}
   2206 +	if (nested.output) {
   2207 +		wl_output_destroy(nested.output);
   2208 +	}
   2209 +	if (nested.inhibit_manager) {
   2210 +		zwp_keyboard_shortcuts_inhibit_manager_v1_destroy(
   2211 +		    nested.inhibit_manager);
   2212 +	}
   2213 +	if (nested.shape_manager) {
   2214 +		wp_cursor_shape_manager_v1_destroy(nested.shape_manager);
   2215 +	}
   2216 +	if (nested.wm_base) {
   2217 +		xdg_wm_base_destroy(nested.wm_base);
   2218 +	}
   2219 +	if (nested.compositor) {
   2220 +		wl_compositor_destroy(nested.compositor);
   2221 +	}
   2222 +	if (nested.registry) {
   2223 +		wl_registry_destroy(nested.registry);
   2224 +	}
   2225 +	if (nested.display) {
   2226 +		wl_display_disconnect(nested.display);
   2227 +	}
   2228 +	memset(&nested, 0, sizeof(nested));
   2229 +}
   2230 +
   2231 +bool
   2232 +nested_initialize(void)
   2233 +{
   2234 +	int32_t cursor_size;
   2235 +
   2236 +	nested.scale = env_int("SWC_NESTED_SCALE", 1);
   2237 +	nested.axis.source = WL_POINTER_AXIS_SOURCE_WHEEL;
   2238 +	parse_chord(&nested.capture_key, "SWC_NESTED_CAPTURE_KEY", NESTED_CAPTURE_KEY);
   2239 +	parse_chord(&nested.fullscreen_key, "SWC_NESTED_FULLSCREEN_KEY",
   2240 +	            NESTED_FULLSCREEN_KEY);
   2241 +
   2242 +	if (!(nested.display = wl_display_connect(NULL))) {
   2243 +		ERROR("nested: could not connect to the parent compositor\n");
   2244 +		goto error;
   2245 +	}
   2246 +	nested.registry = wl_display_get_registry(nested.display);
   2247 +	wl_registry_add_listener(nested.registry, &registry_listener, NULL);
   2248 +	/* the first roundtrip binds the globals, the second collects what
   2249 +	 * they announce on bind: the output scale and the seat capabilities */
   2250 +	if (wl_display_roundtrip(nested.display) < 0 ||
   2251 +	    wl_display_roundtrip(nested.display) < 0) {
   2252 +		ERROR("nested: parent connection failed during setup\n");
   2253 +		goto error;
   2254 +	}
   2255 +	if (!nested.compositor || !nested.wm_base) {
   2256 +		ERROR("nested: the parent lacks wl_compositor or xdg_wm_base\n");
   2257 +		goto error;
   2258 +	}
   2259 +
   2260 +	nested.backend.context =
   2261 +	    wld_wayland_create_context(nested.display, WLD_SHM, WLD_NONE);
   2262 +	if (!nested.backend.context) {
   2263 +		ERROR("nested: could not create a wld context on the parent\n");
   2264 +		goto error;
   2265 +	}
   2266 +	nested.backend.renderer = wld_create_renderer(nested.backend.context);
   2267 +	if (!nested.backend.renderer) {
   2268 +		ERROR("nested: could not create a wld renderer\n");
   2269 +		goto error;
   2270 +	}
   2271 +
   2272 +	/* the size of a buffer must be a multiple of its buffer scale */
   2273 +	cursor_size = NESTED_CURSOR_SIZE - NESTED_CURSOR_SIZE % nested.scale;
   2274 +	nested.backend.cursor_width = (uint32_t)cursor_size;
   2275 +	nested.backend.cursor_height = (uint32_t)cursor_size;
   2276 +	nested.backend.virtual_input = true;
   2277 +	nested.backend.create_screens = create_screens;
   2278 +	nested.backend.create_target = create_target;
   2279 +	nested.backend.set_cursor = set_cursor;
   2280 +	nested.backend.start = start;
   2281 +	nested.backend.finalize = finalize;
   2282 +
   2283 +	nested.source = wl_event_loop_add_fd(swc.event_loop,
   2284 +	                                     wl_display_get_fd(nested.display),
   2285 +	                                     WL_EVENT_READABLE, handle_display,
   2286 +	                                     NULL);
   2287 +	if (!nested.source) {
   2288 +		ERROR("nested: could not watch the parent connection\n");
   2289 +		goto error;
   2290 +	}
   2291 +	wl_event_source_check(nested.source);
   2292 +
   2293 +	swc.backend = &nested.backend;
   2294 +	return true;
   2295 +
   2296 +error:
   2297 +	finalize();
   2298 +	return false;
   2299 +}
   2300 diff --git a/libswc/nested.h b/libswc/nested.h
   2301 new file mode 100644
   2302 index 0000000..5c16e53
   2303 --- /dev/null
   2304 +++ b/libswc/nested.h
   2305 @@ -0,0 +1,16 @@
   2306 +/* nested backend: swc as a client of another compositor */
   2307 +
   2308 +#ifndef SWC_NESTED_H
   2309 +#define SWC_NESTED_H
   2310 +
   2311 +#include <stdbool.h>
   2312 +
   2313 +/* true to come up nested: SWC_BACKEND decides, else WAYLAND_DISPLAY without a launcher socket */
   2314 +bool
   2315 +nested_requested(void);
   2316 +
   2317 +/* connects to the parent and installs itself as swc.backend */
   2318 +bool
   2319 +nested_initialize(void);
   2320 +
   2321 +#endif
   2322 diff --git a/libswc/output.c b/libswc/output.c
   2323 index 8abcb24..a5ca71f 100644
   2324 --- a/libswc/output.c
   2325 +++ b/libswc/output.c
   2326 @@ -170,6 +170,25 @@ output_new_fb(uint32_t width, uint32_t height, const char *name)
   2327  	return output;
   2328  }
   2329  
   2330 +void
   2331 +output_set_size(struct output *output, uint32_t width, uint32_t height)
   2332 +{
   2333 +	struct wl_resource *resource;
   2334 +	struct mode *mode = output->preferred_mode;
   2335 +
   2336 +	mode->width = width;
   2337 +	mode->height = height;
   2338 +	wl_resource_for_each(resource, &output->resources)
   2339 +	{
   2340 +		wl_output_send_mode(resource,
   2341 +		                    WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED,
   2342 +		                    mode->width, mode->height, mode->refresh);
   2343 +		if (wl_resource_get_version(resource) >= 2) {
   2344 +			wl_output_send_done(resource);
   2345 +		}
   2346 +	}
   2347 +}
   2348 +
   2349  void
   2350  output_destroy(struct output *output)
   2351  {
   2352 diff --git a/libswc/output.h b/libswc/output.h
   2353 index 82d45cc..01c1dc0 100644
   2354 --- a/libswc/output.h
   2355 +++ b/libswc/output.h
   2356 @@ -40,6 +40,8 @@ output_new(drmModeConnector *connector);
   2357  struct output *
   2358  output_new_fb(uint32_t width, uint32_t height, const char *name);
   2359  void
   2360 +output_set_size(struct output *output, uint32_t width, uint32_t height);
   2361 +void
   2362  output_destroy(struct output *output);
   2363  
   2364  #endif
   2365 diff --git a/libswc/primary_plane.c b/libswc/primary_plane.c
   2366 index eda7f31..011cb39 100644
   2367 --- a/libswc/primary_plane.c
   2368 +++ b/libswc/primary_plane.c
   2369 @@ -23,10 +23,11 @@
   2370  
   2371  #include "primary_plane.h"
   2372  #include "event.h"
   2373 +#include "compositor.h"
   2374  #ifdef ENABLE_DRM
   2375  #include "drm.h"
   2376 -#else
   2377 -#include "compositor.h"
   2378 +#endif
   2379 +#ifdef ENABLE_FB
   2380  #include "fb.h"
   2381  #endif
   2382  #include "internal.h"
   2383 @@ -55,11 +56,10 @@ send_frame(void *data)
   2384  	view_frame(&plane->view, get_time());
   2385  }
   2386  
   2387 +#ifdef ENABLE_DRM
   2388  static int
   2389 -attach(struct view *view, struct wld_buffer *buffer)
   2390 +attach_drm(struct primary_plane *plane, struct wld_buffer *buffer)
   2391  {
   2392 -	struct primary_plane *plane = wl_container_of(view, plane, view);
   2393 -#ifdef ENABLE_DRM
   2394  	uint32_t fb;
   2395  	int ret;
   2396  
   2397 @@ -88,12 +88,35 @@ attach(struct view *view, struct wld_buffer *buffer)
   2398  	}
   2399  
   2400  	return 0;
   2401 -#else
   2402 -	if (!fb_present(buffer, view->geometry.x, view->geometry.y)) {
   2403 +}
   2404 +#endif
   2405 +
   2406 +#ifdef ENABLE_FB
   2407 +static int
   2408 +attach_fb(struct primary_plane *plane, struct wld_buffer *buffer)
   2409 +{
   2410 +	if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y)) {
   2411  		return -1;
   2412  	}
   2413  	wl_event_loop_add_idle(swc.event_loop, &send_frame, plane);
   2414  	return 0;
   2415 +}
   2416 +#endif
   2417 +
   2418 +static int
   2419 +attach(struct view *view, struct wld_buffer *buffer)
   2420 +{
   2421 +	struct primary_plane *plane = wl_container_of(view, plane, view);
   2422 +
   2423 +#ifdef ENABLE_DRM
   2424 +	if (plane->drm) {
   2425 +		return attach_drm(plane, buffer);
   2426 +	}
   2427 +#endif
   2428 +#ifdef ENABLE_FB
   2429 +	return attach_fb(plane, buffer);
   2430 +#else
   2431 +	return -1;
   2432  #endif
   2433  }
   2434  
   2435 @@ -129,24 +152,33 @@ handle_swc_event(struct wl_listener *listener, void *data)
   2436  	switch (event->type) {
   2437  	case SWC_EVENT_ACTIVATED:
   2438  #ifdef ENABLE_DRM
   2439 -		plane->need_modeset = true;
   2440 -#else
   2441 -		compositor_damage_all();
   2442 +		if (plane->drm) {
   2443 +			plane->need_modeset = true;
   2444 +			break;
   2445 +		}
   2446  #endif
   2447 +		compositor_damage_all();
   2448  		break;
   2449  	}
   2450  }
   2451  
   2452 -bool
   2453 -#ifdef ENABLE_DRM
   2454 -primary_plane_initialize(struct primary_plane *plane, uint32_t crtc,
   2455 -                         struct mode *mode, uint32_t *connectors,
   2456 -                         uint32_t num_connectors)
   2457 -#else
   2458 -primary_plane_initialize(struct primary_plane *plane, struct mode *mode)
   2459 -#endif
   2460 +static void
   2461 +initialize_common(struct primary_plane *plane, struct mode *mode)
   2462  {
   2463 +	view_initialize(&plane->view, &view_impl);
   2464 +	plane->view.geometry.width = mode->width;
   2465 +	plane->view.geometry.height = mode->height;
   2466 +	plane->mode = *mode;
   2467 +	plane->swc_listener.notify = &handle_swc_event;
   2468 +	wl_signal_add(&swc.event_signal, &plane->swc_listener);
   2469 +}
   2470 +
   2471  #ifdef ENABLE_DRM
   2472 +bool
   2473 +primary_plane_initialize_drm(struct primary_plane *plane, uint32_t crtc,
   2474 +                             struct mode *mode, uint32_t *connectors,
   2475 +                             uint32_t num_connectors)
   2476 +{
   2477  	uint32_t *plane_connectors;
   2478  
   2479  	if (!(plane->original_crtc_state = drmModeGetCrtc(swc.drm->fd, crtc))) {
   2480 @@ -166,43 +198,45 @@ primary_plane_initialize(struct primary_plane *plane, struct mode *mode)
   2481  
   2482  	memcpy(plane_connectors, connectors,
   2483  	       num_connectors * sizeof(connectors[0]));
   2484 +	plane->drm = true;
   2485  	plane->crtc = crtc;
   2486  	plane->need_modeset = true;
   2487 -	view_initialize(&plane->view, &view_impl);
   2488 -	plane->view.geometry.width = mode->width;
   2489 -	plane->view.geometry.height = mode->height;
   2490  	plane->drm_handler.page_flip = &handle_page_flip;
   2491 -	plane->swc_listener.notify = &handle_swc_event;
   2492 -	plane->mode = *mode;
   2493 -#else
   2494 -	view_initialize(&plane->view, &view_impl);
   2495 -	plane->view.geometry.width = mode->width;
   2496 -	plane->view.geometry.height = mode->height;
   2497 -	plane->mode = *mode;
   2498 -#endif
   2499 -	plane->swc_listener.notify = &handle_swc_event;
   2500 -	wl_signal_add(&swc.event_signal, &plane->swc_listener);
   2501 +	initialize_common(plane, mode);
   2502  
   2503  	return true;
   2504  
   2505 -#ifdef ENABLE_DRM
   2506  error1:
   2507  	drmModeFreeCrtc(plane->original_crtc_state);
   2508  error0:
   2509  	return false;
   2510 +}
   2511  #endif
   2512 +
   2513 +#ifdef ENABLE_FB
   2514 +bool
   2515 +primary_plane_initialize_fb(struct primary_plane *plane, struct mode *mode)
   2516 +{
   2517 +#ifdef ENABLE_DRM
   2518 +	plane->drm = false;
   2519 +#endif
   2520 +	initialize_common(plane, mode);
   2521 +	return true;
   2522  }
   2523 +#endif
   2524  
   2525  void
   2526  primary_plane_finalize(struct primary_plane *plane)
   2527  {
   2528  	wl_list_remove(&plane->swc_listener.link);
   2529  #ifdef ENABLE_DRM
   2530 -	wl_array_release(&plane->connectors);
   2531 -	drmModeCrtcPtr crtc = plane->original_crtc_state;
   2532 -	drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x,
   2533 -	               crtc->y, NULL, 0, &crtc->mode);
   2534 -	drmModeFreeCrtc(crtc);
   2535 +	if (plane->drm) {
   2536 +		wl_array_release(&plane->connectors);
   2537 +		drmModeCrtcPtr crtc = plane->original_crtc_state;
   2538 +		drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x,
   2539 +		               crtc->y, NULL, 0, &crtc->mode);
   2540 +		drmModeFreeCrtc(crtc);
   2541 +	}
   2542  #else
   2543  	(void)plane;
   2544  #endif
   2545 diff --git a/libswc/primary_plane.h b/libswc/primary_plane.h
   2546 index 55335ca..c9c8235 100644
   2547 --- a/libswc/primary_plane.h
   2548 +++ b/libswc/primary_plane.h
   2549 @@ -36,6 +36,8 @@
   2550  
   2551  struct primary_plane {
   2552  #ifdef ENABLE_DRM
   2553 +	/* driven through drm, not the framebuffer */
   2554 +	bool drm;
   2555  	uint32_t crtc;
   2556  	drmModeCrtcPtr original_crtc_state;
   2557  #endif
   2558 @@ -51,12 +53,13 @@ struct primary_plane {
   2559  
   2560  #ifdef ENABLE_DRM
   2561  bool
   2562 -primary_plane_initialize(struct primary_plane *plane, uint32_t crtc,
   2563 -                         struct mode *mode, uint32_t *connectors,
   2564 -                         uint32_t num_connectors);
   2565 -#else
   2566 +primary_plane_initialize_drm(struct primary_plane *plane, uint32_t crtc,
   2567 +                             struct mode *mode, uint32_t *connectors,
   2568 +                             uint32_t num_connectors);
   2569 +#endif
   2570 +#ifdef ENABLE_FB
   2571  bool
   2572 -primary_plane_initialize(struct primary_plane *plane, struct mode *mode);
   2573 +primary_plane_initialize_fb(struct primary_plane *plane, struct mode *mode);
   2574  #endif
   2575  void
   2576  primary_plane_finalize(struct primary_plane *plane);
   2577 diff --git a/libswc/screen.c b/libswc/screen.c
   2578 index c0f1eb0..54b1dc4 100644
   2579 --- a/libswc/screen.c
   2580 +++ b/libswc/screen.c
   2581 @@ -22,19 +22,24 @@
   2582   */
   2583  
   2584  #include "screen.h"
   2585 +#include "backend.h"
   2586 +#include "compositor.h"
   2587  #ifdef ENABLE_DRM
   2588  #include "drm.h"
   2589 -#else
   2590 +#endif
   2591 +#ifdef ENABLE_FB
   2592  #include "fb.h"
   2593  #endif
   2594  #include "event.h"
   2595  #include "internal.h"
   2596 +#include "layer_shell.h"
   2597  #include "mode.h"
   2598  #include "output.h"
   2599  #ifdef ENABLE_DRM
   2600  #include "plane.h"
   2601  #endif
   2602  #include "pointer.h"
   2603 +#include "seat.h"
   2604  #include "util.h"
   2605  
   2606  #include "swc-server-protocol.h"
   2607 @@ -66,18 +71,30 @@ swc_screen_set_handler(struct swc_screen *base,
   2608  	screen->handler_data = data;
   2609  }
   2610  
   2611 +/* the screens of the video backend swc started with */
   2612 +static bool
   2613 +create_default_screens(void)
   2614 +{
   2615 +#ifdef ENABLE_DRM
   2616 +	if (swc.video == SWC_VIDEO_DRM) {
   2617 +		return drm_create_screens(&swc.screens);
   2618 +	}
   2619 +#endif
   2620 +#ifdef ENABLE_FB
   2621 +	if (swc.video == SWC_VIDEO_FB) {
   2622 +		return fb_create_screens(&swc.screens);
   2623 +	}
   2624 +#endif
   2625 +	return false;
   2626 +}
   2627 +
   2628  bool
   2629  screens_initialize(void)
   2630  {
   2631  	wl_list_init(&swc.screens);
   2632  
   2633 -	if (!
   2634 -#ifdef ENABLE_DRM
   2635 -	    drm_create_screens(&swc.screens)
   2636 -#else
   2637 -	    fb_create_screens(&swc.screens)
   2638 -#endif
   2639 -	) {
   2640 +	if (!(swc.backend->create_screens ? swc.backend->create_screens(&swc.screens)
   2641 +	                                  : create_default_screens())) {
   2642  		return false;
   2643  	}
   2644  
   2645 @@ -115,11 +132,7 @@ bind_screen(struct wl_client *client, void *data, uint32_t version, uint32_t id)
   2646  }
   2647  
   2648  struct screen *
   2649 -#ifdef ENABLE_DRM
   2650  screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane)
   2651 -#else
   2652 -screen_new(struct output *output)
   2653 -#endif
   2654  {
   2655  	struct screen *screen;
   2656  	int32_t x = 0;
   2657 @@ -141,26 +154,33 @@ screen_new(struct output *output)
   2658  	}
   2659  
   2660  #ifdef ENABLE_DRM
   2661 -	screen->crtc = crtc;
   2662 -	if (!primary_plane_initialize(&screen->planes.primary,
   2663 -	                              crtc,
   2664 -	                              output->preferred_mode,
   2665 -	                              &output->connector,
   2666 -	                              1)) {
   2667 -		ERROR("Failed to initialize primary plane\n");
   2668 -		goto error2;
   2669 -	}
   2670 +	if (swc.video == SWC_VIDEO_DRM) {
   2671 +		screen->crtc = crtc;
   2672 +		if (!primary_plane_initialize_drm(&screen->planes.primary,
   2673 +		                                  crtc,
   2674 +		                                  output->preferred_mode,
   2675 +		                                  &output->connector,
   2676 +		                                  1)) {
   2677 +			ERROR("Failed to initialize primary plane\n");
   2678 +			goto error2;
   2679 +		}
   2680  
   2681 -	cursor_plane->screen = screen;
   2682 -	screen->planes.cursor = cursor_plane;
   2683 -#else
   2684 -	if (!primary_plane_initialize(&screen->planes.primary,
   2685 -	                              output->preferred_mode)) {
   2686 -		ERROR("Failed to initialize primary plane\n");
   2687 -		goto error2;
   2688 -	}
   2689 -	screen->planes.cursor = NULL;
   2690 +		cursor_plane->screen = screen;
   2691 +		screen->planes.cursor = cursor_plane;
   2692 +	} else
   2693 +#endif
   2694 +	{
   2695 +#ifdef ENABLE_FB
   2696 +		if (!primary_plane_initialize_fb(&screen->planes.primary,
   2697 +		                                 output->preferred_mode)) {
   2698 +			ERROR("Failed to initialize primary plane\n");
   2699 +			goto error2;
   2700 +		}
   2701  #endif
   2702 +		(void)crtc;
   2703 +		(void)cursor_plane;
   2704 +		screen->planes.cursor = NULL;
   2705 +	}
   2706  
   2707  	screen->handler = &null_handler;
   2708  	wl_signal_init(&screen->destroy_signal);
   2709 @@ -199,15 +219,99 @@ screen_destroy(struct screen *screen)
   2710  	wl_signal_emit(&screen->destroy_signal, NULL);
   2711  	wl_list_for_each_safe(output, next, &screen->outputs, link)
   2712  	    output_destroy(output);
   2713 -	primary_plane_finalize(&screen->planes.primary);
   2714 +	if (screen->virtual) {
   2715 +		view_finalize(&screen->planes.primary.view);
   2716 +	} else {
   2717 +		primary_plane_finalize(&screen->planes.primary);
   2718  #ifdef ENABLE_DRM
   2719 -	if (screen->planes.cursor) {
   2720 -		plane_destroy(screen->planes.cursor);
   2721 -	}
   2722 +		if (screen->planes.cursor) {
   2723 +			plane_destroy(screen->planes.cursor);
   2724 +		}
   2725  #endif
   2726 +	}
   2727  	free(screen);
   2728  }
   2729  
   2730 +struct screen *
   2731 +screen_new_virtual(struct output *output, const struct view_impl *impl)
   2732 +{
   2733 +	struct screen *screen;
   2734 +	int32_t x = 0;
   2735 +
   2736 +	wl_list_for_each(screen, &swc.screens, link) x =
   2737 +	    MAX(x, screen->base.geometry.x + screen->base.geometry.width);
   2738 +
   2739 +	if (!(screen = calloc(1, sizeof(*screen)))) {
   2740 +		return NULL;
   2741 +	}
   2742 +
   2743 +	screen->global = wl_global_create(
   2744 +	    swc.display, &swc_screen_interface, 1, screen, &bind_screen);
   2745 +	if (!screen->global) {
   2746 +		ERROR("Failed to create screen global\n");
   2747 +		free(screen);
   2748 +		return NULL;
   2749 +	}
   2750 +
   2751 +	screen->virtual = true;
   2752 +	view_initialize(&screen->planes.primary.view, impl);
   2753 +	screen->planes.primary.view.geometry.width = output->preferred_mode->width;
   2754 +	screen->planes.primary.view.geometry.height =
   2755 +	    output->preferred_mode->height;
   2756 +	screen->planes.primary.mode = *output->preferred_mode;
   2757 +
   2758 +	screen->handler = &null_handler;
   2759 +	wl_signal_init(&screen->destroy_signal);
   2760 +	wl_list_init(&screen->resources);
   2761 +	wl_list_init(&screen->outputs);
   2762 +	wl_list_insert(&screen->outputs, &output->link);
   2763 +	wl_list_init(&screen->modifiers);
   2764 +
   2765 +	view_move(&screen->planes.primary.view, x, 0);
   2766 +	screen->base.geometry = screen->planes.primary.view.geometry;
   2767 +	screen->base.usable_geometry = screen->base.geometry;
   2768 +
   2769 +	swc.manager->new_screen(&screen->base);
   2770 +
   2771 +	return screen;
   2772 +}
   2773 +
   2774 +void
   2775 +screen_resize(struct screen *screen, uint32_t width, uint32_t height)
   2776 +{
   2777 +	struct output *output;
   2778 +	struct screen *other;
   2779 +	pixman_region32_t region;
   2780 +
   2781 +	screen->planes.primary.view.geometry.width = width;
   2782 +	screen->planes.primary.view.geometry.height = height;
   2783 +	screen->planes.primary.mode.width = width;
   2784 +	screen->planes.primary.mode.height = height;
   2785 +	screen->base.geometry.width = width;
   2786 +	screen->base.geometry.height = height;
   2787 +
   2788 +	wl_list_for_each(output, &screen->outputs, link)
   2789 +	    output_set_size(output, width, height);
   2790 +
   2791 +	compositor_screen_resize(screen);
   2792 +
   2793 +	if (swc.seat) {
   2794 +		pixman_region32_init(&region);
   2795 +		wl_list_for_each(other, &swc.screens, link)
   2796 +		    pixman_region32_union_rect(&region, &region,
   2797 +		                               other->base.geometry.x,
   2798 +		                               other->base.geometry.y,
   2799 +		                               other->base.geometry.width,
   2800 +		                               other->base.geometry.height);
   2801 +		pointer_set_region(swc.seat->pointer, &region);
   2802 +		pixman_region32_fini(&region);
   2803 +	}
   2804 +
   2805 +	/* a resize can leave origin and edges unchanged, notify anyway */
   2806 +	screen->base.usable_geometry.width = 0;
   2807 +	screen_update_usable_geometry(screen);
   2808 +}
   2809 +
   2810  void
   2811  screen_update_usable_geometry(struct screen *screen)
   2812  {
   2813 @@ -239,6 +343,7 @@ screen_update_usable_geometry(struct screen *screen)
   2814  		screen->base.usable_geometry.width = extents->x2 - extents->x1;
   2815  		screen->base.usable_geometry.height = extents->y2 - extents->y1;
   2816  
   2817 +		layer_shell_usable_changed(screen);
   2818  		if (screen->handler->usable_geometry_changed) {
   2819  			screen->handler->usable_geometry_changed(screen->handler_data);
   2820  		}
   2821 diff --git a/libswc/screen.h b/libswc/screen.h
   2822 index d53b566..ee896ba 100644
   2823 --- a/libswc/screen.h
   2824 +++ b/libswc/screen.h
   2825 @@ -51,6 +51,8 @@ struct screen {
   2826  
   2827  	struct wl_signal destroy_signal;
   2828  	uint8_t id;
   2829 +	/* no hardware behind it: the backend owns the primary plane */
   2830 +	bool virtual;
   2831  #ifdef ENABLE_DRM
   2832  	uint32_t crtc;
   2833  #endif
   2834 @@ -73,13 +75,14 @@ screens_initialize(void);
   2835  void
   2836  screens_finalize(void);
   2837  
   2838 -#ifdef ENABLE_DRM
   2839 +/* crtc and cursor_plane are the drm backend's, the framebuffer's pass 0 and
   2840 + * NULL */
   2841  struct screen *
   2842  screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane);
   2843 -#else
   2844  struct screen *
   2845 -screen_new(struct output *output);
   2846 -#endif
   2847 +screen_new_virtual(struct output *output, const struct view_impl *impl);
   2848 +void
   2849 +screen_resize(struct screen *screen, uint32_t width, uint32_t height);
   2850  void
   2851  screen_destroy(struct screen *screen);
   2852  
   2853 diff --git a/libswc/swc.c b/libswc/swc.c
   2854 index e48cd8d..01d679e 100644
   2855 --- a/libswc/swc.c
   2856 +++ b/libswc/swc.c
   2857 @@ -27,7 +27,8 @@
   2858  #include "data_device_manager.h"
   2859  #ifdef ENABLE_DRM
   2860  #include "drm.h"
   2861 -#else
   2862 +#endif
   2863 +#ifdef ENABLE_FB
   2864  #include "fb.h"
   2865  #endif
   2866  #include "event.h"
   2867 @@ -36,6 +37,9 @@
   2868  #include "keyboard.h"
   2869  #include "launch.h"
   2870  #include "layer_shell.h"
   2871 +#ifdef ENABLE_NESTED
   2872 +#include "nested.h"
   2873 +#endif
   2874  #include "panel_manager.h"
   2875  #include "pointer.h"
   2876  #include "screen.h"
   2877 @@ -48,6 +52,8 @@
   2878  #include "util.h"
   2879  #include "window.h"
   2880  #include "xdg_decoration.h"
   2881 +#include "xdg_dialog.h"
   2882 +#include "cursor_shape.h"
   2883  #include "xdg_output.h"
   2884  #include "xdg_shell.h"
   2885  #ifdef ENABLE_XWAYLAND
   2886 @@ -166,6 +172,140 @@ swc_cursor_set_position(int32_t x, int32_t y)
   2887  	return true;
   2888  }
   2889  
   2890 +EXPORT bool
   2891 +swc_button(uint32_t button, uint32_t state)
   2892 +{
   2893 +	if (!swc.seat || !swc.seat->pointer) {
   2894 +		return false;
   2895 +	}
   2896 +
   2897 +	pointer_handle_button(swc.seat->pointer, get_time(), button, state);
   2898 +	pointer_handle_frame(swc.seat->pointer);
   2899 +
   2900 +	return true;
   2901 +}
   2902 +
   2903 +EXPORT bool
   2904 +swc_key(uint32_t key, uint32_t state)
   2905 +{
   2906 +	if (!swc.seat || !swc.seat->keyboard) {
   2907 +		return false;
   2908 +	}
   2909 +
   2910 +	keyboard_handle_key(swc.seat->keyboard, get_time(), key, state);
   2911 +
   2912 +	return true;
   2913 +}
   2914 +
   2915 +EXPORT bool
   2916 +swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level)
   2917 +{
   2918 +	struct xkb_keymap *map;
   2919 +	xkb_keycode_t code, max;
   2920 +	const xkb_keysym_t *syms;
   2921 +	uint32_t l;
   2922 +	int i, n;
   2923 +
   2924 +	if (!swc.seat || !swc.seat->keyboard) {
   2925 +		return false;
   2926 +	}
   2927 +
   2928 +	map = swc.seat->keyboard->xkb.keymap.map;
   2929 +	max = xkb_keymap_max_keycode(map);
   2930 +	/* bare keys first */
   2931 +	for (l = 0; l < 2; l++) {
   2932 +		for (code = xkb_keymap_min_keycode(map); code <= max; code++) {
   2933 +			n = xkb_keymap_key_get_syms_by_level(map, code, 0, l, &syms);
   2934 +			for (i = 0; i < n; i++) {
   2935 +				if (syms[i] != keysym) {
   2936 +					continue;
   2937 +				}
   2938 +				*key = code - 8;
   2939 +				*level = l;
   2940 +				return true;
   2941 +			}
   2942 +		}
   2943 +	}
   2944 +
   2945 +	return false;
   2946 +}
   2947 +
   2948 +/* whether SWC_BACKEND leaves the video backend called name open: drm or fb
   2949 + * name one, anything else, or nothing, lets swc try each that was built */
   2950 +static bool
   2951 +video_wanted(const char *name)
   2952 +{
   2953 +	const char *backend = getenv("SWC_BACKEND");
   2954 +
   2955 +	if (!backend || (strcmp(backend, "drm") != 0 && strcmp(backend, "fb") != 0)) {
   2956 +		return true;
   2957 +	}
   2958 +	return strcmp(backend, name) == 0;
   2959 +}
   2960 +
   2961 +/* the first video backend that was built, was asked for and comes up */
   2962 +static bool
   2963 +video_initialize(void)
   2964 +{
   2965 +#ifdef ENABLE_DRM
   2966 +	if (video_wanted("drm") && drm_initialize()) {
   2967 +		swc.video = SWC_VIDEO_DRM;
   2968 +		return true;
   2969 +	}
   2970 +#endif
   2971 +#ifdef ENABLE_FB
   2972 +	if (video_wanted("fb") && fb_initialize()) {
   2973 +		swc.video = SWC_VIDEO_FB;
   2974 +		return true;
   2975 +	}
   2976 +#endif
   2977 +	return false;
   2978 +}
   2979 +
   2980 +static bool
   2981 +backend_initialize(void)
   2982 +{
   2983 +#ifdef ENABLE_NESTED
   2984 +	if (nested_requested()) {
   2985 +		return nested_initialize();
   2986 +	}
   2987 +#endif
   2988 +
   2989 +	if (!launch_initialize()) {
   2990 +		ERROR("Could not connect to swc-launch\n");
   2991 +		return false;
   2992 +	}
   2993 +
   2994 +	if (!video_initialize()) {
   2995 +		ERROR("Could not initialize video backend\n");
   2996 +		launch_finalize();
   2997 +		return false;
   2998 +	}
   2999 +
   3000 +	return true;
   3001 +}
   3002 +
   3003 +static void
   3004 +backend_finalize(void)
   3005 +{
   3006 +	if (swc.backend->finalize) {
   3007 +		swc.backend->finalize();
   3008 +		return;
   3009 +	}
   3010 +
   3011 +#ifdef ENABLE_DRM
   3012 +	if (swc.video == SWC_VIDEO_DRM) {
   3013 +		drm_finalize();
   3014 +	}
   3015 +#endif
   3016 +#ifdef ENABLE_FB
   3017 +	if (swc.video == SWC_VIDEO_FB) {
   3018 +		fb_finalize();
   3019 +	}
   3020 +#endif
   3021 +	launch_finalize();
   3022 +}
   3023 +
   3024  EXPORT bool
   3025  swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop,
   3026                 const struct swc_manager *manager)
   3027 @@ -177,22 +317,10 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop,
   3028  	const char *default_seat = "seat0";
   3029  	wl_signal_init(&swc.event_signal);
   3030  
   3031 -	if (!launch_initialize()) {
   3032 -		ERROR("Could not connect to swc-launch\n");
   3033 +	if (!backend_initialize()) {
   3034  		goto error0;
   3035  	}
   3036  
   3037 -	if (!
   3038 -#ifdef ENABLE_DRM
   3039 -	    drm_initialize()
   3040 -#else
   3041 -	    fb_initialize()
   3042 -#endif
   3043 -	) {
   3044 -		ERROR("Could not initialize video backend\n");
   3045 -		goto error1;
   3046 -	}
   3047 -
   3048  	swc.shm = shm_create(display);
   3049  	if (!swc.shm) {
   3050  		ERROR("Could not initialize SHM\n");
   3051 @@ -244,6 +372,12 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop,
   3052  		goto error10;
   3053  	}
   3054  
   3055 +	/* optional */
   3056 +	swc.xdg_dialog_manager = xdg_dialog_manager_create(display);
   3057 +
   3058 +	/* optional */
   3059 +	swc.cursor_shape_manager = cursor_shape_manager_create(display);
   3060 +
   3061  	swc.xdg_decoration_manager = xdg_decoration_manager_create(display);
   3062  	if (!swc.xdg_decoration_manager) {
   3063  		ERROR("Could not initialize XDG decoration manager\n");
   3064 @@ -295,6 +429,10 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop,
   3065  
   3066  	setup_compositor();
   3067  
   3068 +	if (swc.backend->start) {
   3069 +		swc.backend->start();
   3070 +	}
   3071 +
   3072  	return true;
   3073  
   3074  error17:
   3075 @@ -330,13 +468,7 @@ error4:
   3076  error3:
   3077  	shm_destroy(swc.shm);
   3078  error2:
   3079 -#ifdef ENABLE_DRM
   3080 -	drm_finalize();
   3081 -#else
   3082 -	fb_finalize();
   3083 -#endif
   3084 -error1:
   3085 -	launch_finalize();
   3086 +	backend_finalize();
   3087  error0:
   3088  	return false;
   3089  }
   3090 @@ -361,10 +493,36 @@ swc_finalize(void)
   3091  	screens_finalize();
   3092  	bindings_finalize();
   3093  	shm_destroy(swc.shm);
   3094 -#ifdef ENABLE_DRM
   3095 -	drm_finalize();
   3096 -#else
   3097 -	fb_finalize();
   3098 -#endif
   3099 -	launch_finalize();
   3100 +	backend_finalize();
   3101 +}
   3102 +
   3103 +EXPORT bool
   3104 +swc_set_keyboard_layout(const char *layout, const char *variant,
   3105 +                        const char *options)
   3106 +{
   3107 +	struct xkb_rule_names names = {
   3108 +	    .layout = layout, .variant = variant, .options = options};
   3109 +
   3110 +	return keyboard_set_names(swc.seat->keyboard, &names);
   3111 +}
   3112 +
   3113 +EXPORT bool
   3114 +swc_set_keyboard_keymap(const char *keymap)
   3115 +{
   3116 +	struct xkb_keymap *map;
   3117 +	bool ok;
   3118 +
   3119 +	if (!(map = xkb_keymap_new_from_string(swc.seat->keyboard->xkb.context,
   3120 +	                                       keymap, XKB_KEYMAP_FORMAT_TEXT_V1,
   3121 +	                                       0)))
   3122 +		return false;
   3123 +	ok = keyboard_set_keymap(swc.seat->keyboard, map);
   3124 +	xkb_keymap_unref(map);
   3125 +	return ok;
   3126 +}
   3127 +
   3128 +EXPORT void
   3129 +swc_set_keyboard_repeat(int32_t rate, int32_t delay)
   3130 +{
   3131 +	keyboard_set_repeat(swc.seat->keyboard, rate, delay);
   3132  }
   3133 diff --git a/libswc/swc.h b/libswc/swc.h
   3134 index fc0fa9a..8ab36a1 100644
   3135 --- a/libswc/swc.h
   3136 +++ b/libswc/swc.h
   3137 @@ -58,6 +58,25 @@ swc_cursor_position(int32_t *x, int32_t *y);
   3138  bool
   3139  swc_cursor_set_position(int32_t x, int32_t y);
   3140  
   3141 +/**
   3142 + * Feed a button event to the seat, through the bindings.
   3143 + * button is a linux input code.
   3144 + */
   3145 +bool
   3146 +swc_button(uint32_t button, uint32_t state);
   3147 +
   3148 +/**
   3149 + * Feed a key event to the seat. key is an evdev code.
   3150 + */
   3151 +bool
   3152 +swc_key(uint32_t key, uint32_t state);
   3153 +
   3154 +/**
   3155 + * Find the key and level (0 bare, 1 shift) that produce keysym.
   3156 + */
   3157 +bool
   3158 +swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level);
   3159 +
   3160  /**
   3161   * Send a pointer button event to the currently focused client.
   3162   *
   3163 @@ -125,6 +144,74 @@ swc_set_cursor_image(enum swc_cursor_kind kind, const uint32_t *argb8888,
   3164  void
   3165  swc_clear_cursor_image(enum swc_cursor_kind kind);
   3166  
   3167 +/**
   3168 + * set the picture of a cursor by its name: "default", "text", "pointer",
   3169 + * "wait", "ew-resize" and the like, the names of CSS and of the
   3170 + * cursor-shape-v1 protocol, which clients ask for by. nothing is drawn
   3171 + * until a name is asked for, so a window manager with a theme gives
   3172 + * every name it has.
   3173 + *
   3174 + * `argb8888` is `width*height` pixels, kept by the caller as long as it
   3175 + * may be used
   3176 + */
   3177 +void
   3178 +swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888,
   3179 +                           uint32_t width, uint32_t height, int32_t hotspot_x,
   3180 +                           int32_t hotspot_y);
   3181 +
   3182 +/**
   3183 + * show the cursor of this name where no client has the pointer, over a
   3184 + * window's border for one, NULL for the usual
   3185 + */
   3186 +void
   3187 +swc_set_cursor_shape(const char *name);
   3188 +
   3189 +/**
   3190 + * be told where the pointer moves, x and y in compositor coordinates, so
   3191 + * a window manager can say what is under it. at most one, NULL to stop
   3192 + */
   3193 +void
   3194 +swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y),
   3195 +                               void *data);
   3196 +
   3197 +/**
   3198 + * the clipboard, for a compositor that keeps it itself. a client sets the
   3199 + * selection as it does in any compositor, and the handler is told which types
   3200 + * it offers (none when it is gone, the client exited or the selection was
   3201 + * cleared). what the compositor keeps it offers again as a selection of its
   3202 + * own, so the clipboard does not end with the client that copied
   3203 + */
   3204 +struct swc_selection_handler {
   3205 +	void (*changed)(void *data, const char *const *mime_types, unsigned count);
   3206 +};
   3207 +
   3208 +/* what answers a paste of a selection the compositor offers */
   3209 +struct swc_selection_source {
   3210 +	/* write the type to fd, which the caller closes */
   3211 +	void (*send)(void *data, const char *mime_type, int fd);
   3212 +	/* another client took the selection */
   3213 +	void (*cancelled)(void *data);
   3214 +};
   3215 +
   3216 +void
   3217 +swc_selection_set_handler(const struct swc_selection_handler *handler, void *data);
   3218 +
   3219 +/**
   3220 + * asks the client that holds the selection for a type, written to fd.
   3221 + * false when no client holds it or it does not offer the type. the caller
   3222 + * closes fd, the client has its own copy
   3223 + */
   3224 +bool
   3225 +swc_selection_request(const char *mime_type, int fd);
   3226 +
   3227 +/**
   3228 + * takes the selection, offering these types, answered by source. what was
   3229 + * the selection is cancelled
   3230 + */
   3231 +bool
   3232 +swc_selection_offer(const char *const *mime_types, unsigned count,
   3233 +                    const struct swc_selection_source *source, void *data);
   3234 +
   3235  /**
   3236   * draw [or update] a simple box overlay
   3237   *
   3238 @@ -267,6 +354,23 @@ struct swc_window_handler {
   3239  	 * the interactive resize will be honored.
   3240  	 */
   3241  	void (*resize)(void *data);
   3242 +
   3243 +	/**
   3244 +	 * Called when the window starts or stops blocking its parent.
   3245 +	 */
   3246 +	void (*modal_changed)(void *data);
   3247 +
   3248 +	/**
   3249 +	 * Called when the window size changes.
   3250 +	 */
   3251 +	void (*size_changed)(void *data);
   3252 +
   3253 +	/**
   3254 +	 * Called when the window asks to be shown fullscreen, or to stop being.
   3255 +	 *
   3256 +	 * The window manager decides, see swc_window_set_fullscreen.
   3257 +	 */
   3258 +	void (*fullscreen)(void *data, bool fullscreen);
   3259  };
   3260  
   3261  struct swc_window {
   3262 @@ -274,6 +378,8 @@ struct swc_window {
   3263  	char *app_id;
   3264  
   3265  	struct swc_window *parent;
   3266 +	/* the window says it blocks its parent */
   3267 +	bool modal;
   3268  	uint32_t motion_throttle_ms;
   3269  	uint32_t min_width;
   3270  	uint32_t min_height;
   3271 @@ -288,6 +394,41 @@ void
   3272  swc_window_set_handler(struct swc_window *window,
   3273                         const struct swc_window_handler *handler, void *data);
   3274  
   3275 +/* what lies behind a window while the screen is composited: the target's
   3276 + * pixels as drawn so far, pitch bytes a row in a WLD format, the window's
   3277 + * area with the margin asked for (in those pixels, clipped to them), and the
   3278 + * parts of it being repainted now, which every caller may read and write */
   3279 +struct swc_backdrop {
   3280 +	void *pixels;
   3281 +	uint32_t pitch, format;
   3282 +	struct swc_rectangle area;
   3283 +	const struct swc_rectangle *rects;
   3284 +	int nrects;
   3285 +};
   3286 +
   3287 +/* calls hook right before the window is drawn with what lies behind it, so
   3288 + * a window manager can read it or change it: glass, a shade, a lens. the
   3289 + * whole area and margin is drawn again whenever any of it changes, so the
   3290 + * pixels are always complete there. a NULL hook takes it away */
   3291 +void
   3292 +swc_window_set_backdrop(struct swc_window *window, uint32_t margin,
   3293 +                        void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data);
   3294 +
   3295 +/* depths: a view at a higher one is drawn over one at a lower. the
   3296 + * shells' layers stand at these, a window at SWC_Z_WINDOW until its manager
   3297 + * says otherwise, and every depth between them is the manager's: floating
   3298 + * windows over tiled ones, a fullscreen window over the panels. a window is
   3299 + * raised to the front of the views at its depth, and its transients share
   3300 + * it */
   3301 +#define SWC_Z_BACKGROUND (-3000)
   3302 +#define SWC_Z_BOTTOM     (-2000)
   3303 +#define SWC_Z_WINDOW     0
   3304 +#define SWC_Z_TOP        2000
   3305 +#define SWC_Z_OVERLAY    3000
   3306 +
   3307 +void
   3308 +swc_window_set_z(struct swc_window *window, int32_t z);
   3309 +
   3310  /**
   3311   * Request that the specified window close.
   3312   */
   3313 @@ -344,6 +485,24 @@ swc_window_set_tiled(struct swc_window *window);
   3314  void
   3315  swc_window_set_fullscreen(struct swc_window *window, struct swc_screen *screen);
   3316  
   3317 +/**
   3318 + * Make window a transient of parent, or of nothing when NULL.
   3319 + */
   3320 +void
   3321 +swc_window_set_parent(struct swc_window *window, struct swc_window *parent);
   3322 +
   3323 +/**
   3324 + * An attached transient moves with its parent.
   3325 + */
   3326 +void
   3327 +swc_window_set_attached(struct swc_window *window, bool attached);
   3328 +
   3329 +/**
   3330 + * Raise the window with its transients.
   3331 + */
   3332 +void
   3333 +swc_window_raise(struct swc_window *window);
   3334 +
   3335  /**
   3336   * Set the window's position.
   3337   *
   3338 @@ -400,6 +559,27 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color,
   3339                        uint32_t inner_border_width, uint32_t outer_border_color,
   3340                        uint32_t outer_border_width);
   3341  
   3342 +/**
   3343 + * What the border goes around.
   3344 + */
   3345 +enum swc_border_placement {
   3346 +	/**
   3347 +	 * The client area only.
   3348 +	 */
   3349 +	SWC_BORDER_CLIENT,
   3350 +	/**
   3351 +	 * The client area and its decor.
   3352 +	 */
   3353 +	SWC_BORDER_FRAME,
   3354 +};
   3355 +
   3356 +/**
   3357 + * Choose what the border goes around. The default is SWC_BORDER_CLIENT.
   3358 + */
   3359 +void
   3360 +swc_window_set_border_placement(struct swc_window *window,
   3361 +                                enum swc_border_placement placement);
   3362 +
   3363  /*window decor things*/
   3364  
   3365  /**
   3366 @@ -437,7 +617,8 @@ enum swc_decor_align {
   3367   * for left and right edges, text is drawn as stacked glyphs, one glyph
   3368   * per row.
   3369   *
   3370 - * the font field accepts some fontconfig pattern such as "sans-serif:size=10".
   3371 + * the font field is a font file ("/a/font.ttf:pixelsize=12"), or a
   3372 + * fontconfig pattern such as "sans-serif:size=10" where wld has fontconfig.
   3373   * if font is NULL, a default font is used.
   3374   */
   3375  struct swc_decor_text {
   3376 @@ -567,6 +748,30 @@ swc_window_stack(struct swc_window *window, int32_t direction);
   3377   */
   3378  extern int32_t swc_repeat_rate, swc_repeat_delay;
   3379  
   3380 +/**
   3381 + * Change the keyboard layout while running: names as XKB's, any of them
   3382 + * NULL for its default. False, the old layout kept, when they make no
   3383 + * keymap.
   3384 + */
   3385 +bool
   3386 +swc_set_keyboard_layout(const char *layout, const char *variant,
   3387 +                        const char *options);
   3388 +
   3389 +/**
   3390 + * Change the keyboard to a whole keymap, XKB's text format, for a layout
   3391 + * that is not named in the system's rules. False, the old keymap kept, when
   3392 + * the text makes none.
   3393 + */
   3394 +bool
   3395 +swc_set_keyboard_keymap(const char *keymap);
   3396 +
   3397 +/**
   3398 + * Change the repeat rate (characters per second) and delay (ms), which
   3399 + * clients are told again.
   3400 + */
   3401 +void
   3402 +swc_set_keyboard_repeat(int32_t rate, int32_t delay);
   3403 +
   3404  /* Bindings {{{ */
   3405  
   3406  enum {
   3407 @@ -577,6 +782,22 @@ enum {
   3408  	SWC_MOD_ANY = ~0
   3409  };
   3410  
   3411 +/**
   3412 + * Drag actions, as in wl_data_device_manager.dnd_action.
   3413 + */
   3414 +enum swc_drag_action {
   3415 +	SWC_DRAG_COPY = 1,
   3416 +	SWC_DRAG_MOVE = 2,
   3417 +	SWC_DRAG_ASK = 4,
   3418 +};
   3419 +
   3420 +/**
   3421 + * Set the modifiers that ask for an action during a drag, all held.
   3422 + * 0 disables it. Defaults: Ctrl copy, Shift move.
   3423 + */
   3424 +void
   3425 +swc_set_drag_modifiers(enum swc_drag_action action, uint32_t modifiers);
   3426 +
   3427  enum swc_binding_type {
   3428  	SWC_BINDING_KEY,
   3429  	SWC_BINDING_BUTTON,
   3430 diff --git a/meson.build b/meson.build
   3431 index 356dcbc..79dee81 100644
   3432 --- a/meson.build
   3433 +++ b/meson.build
   3434 @@ -39,15 +39,20 @@ requires_private = [
   3435  
   3436  video_backend = get_option('video')
   3437  framebuffer_backend = get_option('fb')
   3438 +# 'all' builds the drm and the framebuffer backends together, and swc picks
   3439 +# one when it starts
   3440 +with_drm = video_backend in ['drm', 'all']
   3441 +with_fb = video_backend in ['fb', 'all']
   3442  drm = disabler()
   3443 -if video_backend == 'drm'
   3444 -  if framebuffer_backend != 'auto'
   3445 -    error('-Dfb applies only when -Dvideo=fb')
   3446 +if with_drm
   3447 +  if video_backend == 'drm' and framebuffer_backend != 'auto'
   3448 +    error('-Dfb applies only when -Dvideo=fb or -Dvideo=all')
   3449    endif
   3450    drm = dependency('libdrm')
   3451    requires_private += drm
   3452    add_project_arguments('-DENABLE_DRM=1', language: 'c')
   3453 -elif video_backend == 'fb'
   3454 +endif
   3455 +if with_fb
   3456    if framebuffer_backend == 'auto'
   3457      if os == 'linux'
   3458        framebuffer_backend = 'fbdev'
   3459 @@ -58,6 +63,7 @@ elif video_backend == 'fb'
   3460      endif
   3461    endif
   3462  
   3463 +  add_project_arguments('-DENABLE_FB=1', language: 'c')
   3464    if framebuffer_backend == 'fbdev'
   3465      if os != 'linux'
   3466        error('The fbdev framebuffer interface is supported only on Linux')
   3467 @@ -78,7 +84,7 @@ elif video_backend == 'fb'
   3468  endif
   3469  
   3470  if video_backend != 'drm' and get_option('xwayland').enabled()
   3471 -  error('Xwayland requires the DRM video backend')
   3472 +  error('Xwayland requires the DRM video backend alone')
   3473  endif
   3474  xwayland = video_backend == 'drm' \
   3475    ? dependency('xwayland', required: get_option('xwayland')) \
   3476 @@ -124,6 +130,20 @@ if input_backend == 'libinput'
   3477    requires_private += [libinput, udev]
   3478  endif
   3479  
   3480 +# the nested backend replaces the display and the input devices at run
   3481 +# time, so it rides along in any build. it feeds the libinput seat, the
   3482 +# wscons one has no virtual input path
   3483 +wayland_client = dependency('wayland-client', required: get_option('nested'))
   3484 +have_nested = (wayland_client.found() and input_backend == 'libinput'
   3485 +  and cc.has_header('wld/wayland.h', dependencies: dependency('wld')))
   3486 +if get_option('nested').enabled() and not have_nested
   3487 +  error('The nested backend needs wayland-client, the libinput input backend, and wld built with Wayland support')
   3488 +endif
   3489 +if have_nested
   3490 +  add_project_arguments('-DENABLE_NESTED=1', language: 'c')
   3491 +  requires_private += wayland_client
   3492 +endif
   3493 +
   3494  subdir('protocol')
   3495  subdir('cursor')
   3496  subdir('launch')
   3497 @@ -139,7 +159,8 @@ endif
   3498  
   3499  summary({
   3500    'Xwayland support': xwayland.found(),
   3501 +  'Nested backend': have_nested,
   3502    'Input backend': input_backend,
   3503    'Video backend': video_backend,
   3504 -  'Framebuffer interface': video_backend == 'fb' ? framebuffer_backend : 'n/a',
   3505 +  'Framebuffer interface': with_fb ? framebuffer_backend : 'n/a',
   3506  }, bool_yn: true)
   3507 diff --git a/meson.options b/meson.options
   3508 index 98a42a1..09702bd 100644
   3509 --- a/meson.options
   3510 +++ b/meson.options
   3511 @@ -1,9 +1,10 @@
   3512  option('xwayland', type: 'feature', value: 'auto', description: 'Automatic Xwayland handling')
   3513  option('input', type: 'combo', value: 'auto', choices: ['auto', 'libinput', 'wscons'], description: 'Input backend to use')
   3514 -option('video', type: 'combo', value: 'drm', choices: ['drm', 'fb'], description: 'Video backend to use')
   3515 +option('video', type: 'combo', value: 'drm', choices: ['drm', 'fb', 'all'], description: 'Video backend to use: all builds drm and the framebuffer and picks one at run time')
   3516  option('fb', type: 'combo', value: 'auto', choices: ['auto', 'fbdev', 'wsdisplay'], description: 'Framebuffer interface to use')
   3517  option('fbdev-device', type: 'string', value: '/dev/fb0', description: 'Linux framebuffer device')
   3518  option('wsdisplay-device', type: 'string', value: '', description: 'wsdisplay framebuffer device (empty uses the VT selected by swc-launch)')
   3519  option('udev', type: 'feature', value: 'auto', description: 'libinput libudev support')
   3520 +option('nested', type: 'feature', value: 'auto', description: 'Backend that runs swc inside another Wayland compositor')
   3521  option('extra', type: 'boolean', value: true, description: 'Build swc utility applications')
   3522  option('example', type: 'boolean', value: false, description: 'Build swc example compositor')
   3523 diff --git a/protocol/meson.build b/protocol/meson.build
   3524 index c6a0e63..02129b3 100644
   3525 --- a/protocol/meson.build
   3526 +++ b/protocol/meson.build
   3527 @@ -13,6 +13,10 @@ foreach proto : [
   3528    wl.find_protocol('linux-dmabuf', state: 'unstable', version: 1),
   3529    wl.find_protocol('xdg-decoration', state: 'unstable', version: 1),
   3530    wl.find_protocol('xdg-output', state: 'unstable', version: 1),
   3531 +  wl.find_protocol('xdg-dialog', state: 'staging', version: 1),
   3532 +  wl.find_protocol('keyboard-shortcuts-inhibit', state: 'unstable', version: 1),
   3533 +  wl.find_protocol('cursor-shape', state: 'staging', version: 1),
   3534 +  wl.find_protocol('tablet', state: 'unstable', version: 2),
   3535  ]
   3536    protocols = wl.scan_xml(proto,
   3537      public: true,