neuswc

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

commit fe0b645658caa96c931467ccd11d25dbe84dff0c
parent 2fd33ae65046dd40dc7eb738a0d670597c46b568
Author: cowmonk <cowmonk@based.pt>
Date:   Wed,  2 Dec 2026 12:53:37 -0700

apply newest MICE patches

new: decoration, tablets, cross, kms, touch, peer
updated: compositor, input
also includes the damage, keys, outputs and warnings patches

Diffstat:
Mcursor/convert_font.c | 2++
Mcursor/meson.build | 2+-
Mlibswc/backend.h | 2++
Mlibswc/compositor.c | 31+++++++++++++++++++++++++++++++
Mlibswc/compositor.h | 12++++++++++++
Mlibswc/decor.c | 35+++++++++++++++++++++++++++++++++--
Mlibswc/drm.c | 50+++++++++++++++++++++++++++++++++++++++++---------
Mlibswc/fb.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
Mlibswc/fb.h | 8++++++--
Mlibswc/fb_fbdev.c | 44+++++++++++++++++++++++++++++++++++---------
Mlibswc/fb_wsdisplay.c | 46+++++++++++++++++++++++++++++++++++-----------
Mlibswc/keyboard.c | 23++++++++++++++++++++++-
Mlibswc/layer_shell.c | 71++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mlibswc/layer_shell.h | 6++++++
Mlibswc/meson.build | 5++++-
Alibswc/peer.c | 506+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alibswc/peer.h | 16++++++++++++++++
Alibswc/peer_protocol.h | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlibswc/pointer.c | 7++++++-
Mlibswc/primary_plane.c | 5+++--
Mlibswc/screen.c | 191++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Mlibswc/seat.c | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Mlibswc/seat.h | 1+
Mlibswc/swc.c | 23+++++++++++++++++++++++
Mlibswc/swc.h | 53++++++++++++++++++++++++++++++++++++++++++++++++++++-
Alibswc/touch.c | 389+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alibswc/touch.h | 41+++++++++++++++++++++++++++++++++++++++++
Mlibswc/window.c | 3++-
Mlibswc/xdg_decoration.c | 19+++++++++++++++++++
Mlibswc/xdg_shell.c | 6++++++
Mlibswc/xdg_shell.h | 5+++++
Mmeson.build | 11+++++++++++
Mmeson.options | 1+
Mpatches/compositor.patch | 106++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Apatches/cross.patch | 25+++++++++++++++++++++++++
Apatches/damage.patch | 413+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/decoration.patch | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpatches/input.patch | 62+++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Apatches/keys.patch | 142+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/kms.patch | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/outputs.patch | 275+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/peer.patch | 771+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/tablets.patch | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/touch.patch | 610++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/warnings.patch | 14++++++++++++++
45 files changed, 4426 insertions(+), 120 deletions(-)

diff --git a/cursor/convert_font.c b/cursor/convert_font.c @@ -26,6 +26,8 @@ * http://fontforge.org/pcf-format.html */ +#define _POSIX_C_SOURCE 200809L /* strdup, as a tool of the build machine has no project flags */ + #include <assert.h> #include <fcntl.h> #include <stdint.h> diff --git a/cursor/meson.build b/cursor/meson.build @@ -1,4 +1,4 @@ -tool = executable('convert_cont', 'convert_font.c') +tool = executable('convert_cont', 'convert_font.c', native: true) cursor = custom_target('cursor_data_h', input: 'cursor.pcf', diff --git a/libswc/backend.h b/libswc/backend.h @@ -20,6 +20,8 @@ struct swc_backend { /* backend feeds the seat, no input devices */ bool virtual_input; + /* and fingers with it */ + bool virtual_touch; /* optional, unset means the built-in video backend */ bool (*create_screens)(struct wl_list *screens); diff --git a/libswc/compositor.c b/libswc/compositor.c @@ -118,6 +118,8 @@ static struct pointer_handler pointer_handler = { static struct { struct wl_list views; pixman_region32_t damage, opaque; + /* what the frame being presented changed, in its screen's coordinates */ + pixman_region32_t frame_damage; struct wl_listener swc_listener; /* A mask of screens that have been repainted but are waiting on a page @@ -805,6 +807,23 @@ schedule_updates(uint32_t screens) compositor.scheduled_updates |= screens; } +const pixman_region32_t * +compositor_frame_damage(void) +{ + return &compositor.frame_damage; +} + +void +compositor_damage_rect(int32_t x, int32_t y, uint32_t width, uint32_t height) +{ + if (!compositor.initialized || !width || !height) { + return; + } + pixman_region32_union_rect(&compositor.damage, &compositor.damage, x, y, + width, height); + schedule_updates(-1); +} + void compositor_damage_all(void) { @@ -1378,6 +1397,12 @@ compositor_chrome_at(int32_t x, int32_t y) return NULL; } +struct compositor_view * +compositor_content_view_at(int32_t x, int32_t y) +{ + return compositor_chrome_at(x, y) ? NULL : compositor_view_at(x, y); +} + static struct compositor_view * window_view(struct compositor_view *view) { @@ -2000,6 +2025,10 @@ update_screen(struct screen *screen) pixman_region32_intersect_rect(&damage, &compositor.damage, geom->x, geom->y, geom->width, geom->height); pixman_region32_translate(&damage, -geom->x, -geom->y); + if (compositor.zoom != 1.0f) { + pixman_region32_union_rect(&damage, &damage, 0, 0, geom->width, geom->height); + } + pixman_region32_copy(&compositor.frame_damage, &damage); total_damage = wld_surface_damage(target->surface, &damage); /* Don't repaint the screen if it is waiting for a page flip. */ @@ -2228,6 +2257,7 @@ compositor_initialize(void) } pixman_region32_init(&compositor.damage); pixman_region32_init(&compositor.opaque); + pixman_region32_init(&compositor.frame_damage); wl_list_init(&compositor.views); wl_signal_init(&swc_compositor.signal.new_surface); compositor.swc_listener.notify = &handle_swc_event; @@ -2263,6 +2293,7 @@ compositor_finalize(void) } decor_finalize(); pixman_region32_fini(&compositor.damage); + pixman_region32_fini(&compositor.frame_damage); pixman_region32_fini(&compositor.opaque); wl_global_destroy(compositor.global); } diff --git a/libswc/compositor.h b/libswc/compositor.h @@ -63,6 +63,13 @@ void compositor_finalize(void); void compositor_damage_all(void); +/* adds a rectangle of the global space to what the next frame repaints */ +void +compositor_damage_rect(int32_t x, int32_t y, uint32_t width, uint32_t height); +/* what the frame being presented changed, in its screen's coordinates: + * a backend that copies the frame copies only this */ +const pixman_region32_t * +compositor_frame_damage(void); struct compositor_view { struct view base; @@ -154,6 +161,11 @@ void compositor_view_restack(struct compositor_view *view, struct compositor_view *sibling, bool above); +/* the view whose content is under x, y, NULL on a window's decoration or + * border, which are the compositor's and not the client's */ +struct compositor_view * +compositor_content_view_at(int32_t x, int32_t y); + /* topmost view taking input at x, y, or NULL */ struct compositor_view * compositor_view_at(int32_t x, int32_t y); diff --git a/libswc/decor.c b/libswc/decor.c @@ -9,10 +9,40 @@ #include "window.h" #include <pixman.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <wld/wld.h> +/* A decor font: a file when the name is a path ("/a/font.ttf:pixelsize=12", + * 12 pixels when no size is given), else a fontconfig pattern, which only a + * wld built with fontconfig can find. */ +static struct wld_font * +open_font(struct wld_font_context *context, const char *name) +{ + char path[4096]; + const char *size; + double px = 12; + size_t len; + + if (name[0] != '/') { +#ifdef WLD_FONTCONFIG + return wld_font_open_name(context, name); +#else + return NULL; +#endif + } + size = strstr(name, ":pixelsize="); + len = size ? (size_t)(size - name) : strlen(name); + if (len >= sizeof path) + return NULL; + memcpy(path, name, len); + path[len] = '\0'; + if (size) + px = strtod(size + strlen(":pixelsize="), NULL); + return wld_font_open_file(context, path, 0, px > 0 ? px : 12); +} + #define DEFAULT_DECOR_FONT "sans-serif:size=10" static struct wld_font_context *font_context; @@ -330,7 +360,8 @@ get_tiled_decor_buffer(struct decor_part_buffer *part, uint32_t width, buffer_height = (height + 255) & ~255U; } - if (buffer_width > SIZE_MAX / 4) { + /* in 64 bits, so the check means something where size_t has 32 */ + if ((uint64_t)buffer_width * 4 > SIZE_MAX) { return NULL; } row_size = (size_t)buffer_width * 4; @@ -741,7 +772,7 @@ decor_view_set(struct compositor_view *view, const struct swc_decor *decor) } else if (font_name) { owned_font_name = strdup(font_name); if (owned_font_name && font_context) { - font = wld_font_open_name(font_context, font_name); + font = open_font(font_context, font_name); } } diff --git a/libswc/drm.c b/libswc/drm.c @@ -134,6 +134,27 @@ select_card(const struct dirent *entry) return sscanf(entry->d_name, "card%u", &num) == 1; } +/* whether the card drives a display, by the connectors sysfs lists under it + * (card0-DSI-1). a render only card, as many a phone's GPU is beside its + * display controller, has none */ +static bool +has_connectors(const char *card) +{ + struct dirent *entry; + size_t len = strlen(card); + bool found = false; + DIR *dir; + + if (!(dir = opendir("/sys/class/drm"))) { + return false; + } + while (!found && (entry = readdir(dir))) { + found = !strncmp(entry->d_name, card, len) && entry->d_name[len] == '-'; + } + closedir(dir); + return found; +} + static bool find_primary_drm_device(char *path, size_t size) { @@ -142,6 +163,7 @@ find_primary_drm_device(char *path, size_t size) unsigned index; FILE *file; unsigned char boot_vga; + bool any_connected = false; num_cards = scandir("/dev/dri", &cards, &select_card, &alphasort); @@ -150,6 +172,16 @@ find_primary_drm_device(char *path, size_t size) } for (index = 0; index < num_cards; ++index) { + any_connected = any_connected || has_connectors(cards[index]->d_name); + } + + for (index = 0; index < num_cards; ++index) { + /* where sysfs says which cards have outputs, only those do */ + if (any_connected && !has_connectors(cards[index]->d_name)) { + free(cards[index]); + continue; + } + snprintf(path, size, "/sys/class/drm/%s/device/boot_vga", cards[index]->d_name); @@ -294,11 +326,9 @@ drm_initialize(void) swc.backend->cursor_width = swc.drm->cursor_w; swc.backend->cursor_height = swc.drm->cursor_h; + /* a display controller on its own has no render node: clients are then + * not offered wl_drm */ drm.path = drmGetRenderDeviceNameFromFd(swc.drm->fd); - if (!drm.path) { - ERROR("Could not determine render node path\n"); - goto error1; - } if (!(swc.drm->context = wld_drm_create_context(swc.drm->fd))) { ERROR("Could not create WLD DRM context\n"); @@ -321,11 +351,13 @@ drm_initialize(void) } if (!wld_drm_is_dumb(swc.drm->context)) { - drm.global = wl_global_create(swc.display, &wl_drm_interface, 2, NULL, - &bind_drm); - if (!drm.global) { - ERROR("Could not create wl_drm global\n"); - goto error4; + if (drm.path) { + drm.global = wl_global_create(swc.display, &wl_drm_interface, 2, + NULL, &bind_drm); + if (!drm.global) { + ERROR("Could not create wl_drm global\n"); + goto error4; + } } drm.dmabuf = swc_dmabuf_create(swc.display); diff --git a/libswc/fb.c b/libswc/fb.c @@ -14,7 +14,7 @@ struct swc_fb swc_fb; -/* for the cursor */ +/* for the cursor */ static uint32_t blend(uint32_t background, uint32_t foreground) { @@ -92,51 +92,92 @@ fb_create_screens(struct wl_list *screens) return true; } +/* the frame's rectangle r into the copy, the cursor blended over it */ +static void +copy_box(struct wld_buffer *buffer, const pixman_box32_t *r, + struct wld_buffer *cursor, const pixman_box32_t *c) +{ + int32_t y, x, x1, x2; + + for (y = r->y1; y < r->y2; ++y) { + uint32_t *source = (uint32_t *)((uint8_t *)buffer->map + + (size_t)y * buffer->pitch); + uint32_t *destination = (uint32_t *)((uint8_t *)swc_fb.pixels + + (size_t)y * swc_fb.pitch); + + memcpy(destination + r->x1, source + r->x1, + (size_t)(r->x2 - r->x1) * sizeof *destination); + if (!cursor || y < c->y1 || y >= c->y2) { + continue; + } + x1 = MAX(r->x1, c->x1); + x2 = MIN(r->x2, c->x2); + for (x = x1; x < x2; ++x) { + uint32_t foreground = *(uint32_t *)((uint8_t *)cursor->map + + (size_t)(y - c->y1) * cursor->pitch + (size_t)(x - c->x1) * 4); + destination[x] = blend(destination[x], foreground); + } + } +} + bool -fb_present(struct wld_buffer *buffer, int32_t origin_x, int32_t origin_y) +fb_present(struct wld_buffer *buffer, int32_t origin_x, int32_t origin_y, + const pixman_region32_t *damage) { + /* where the cursor was drawn last, put back from the frame this time */ + static pixman_box32_t drawn; + static bool whole_drawn; struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; struct wld_buffer *cursor = NULL; - int32_t cursor_x = 0, cursor_y = 0; - uint32_t width, height, x, y; - bool cursor_mapped = false; + pixman_box32_t c = { 0, 0, 0, 0 }, *boxes; + pixman_region32_t region; + uint32_t width, height; + int i, n; + bool ok; if (!buffer || !wld_map(buffer)) { return false; } - if (pointer && pointer->cursor.view.buffer) { + if (pointer && pointer->cursor.view.buffer && wld_map(pointer->cursor.buffer)) { cursor = pointer->cursor.buffer; - cursor_x = pointer->cursor.view.geometry.x - origin_x; - cursor_y = pointer->cursor.view.geometry.y - origin_y; - cursor_mapped = wld_map(cursor); + c.x1 = pointer->cursor.view.geometry.x - origin_x; + c.y1 = pointer->cursor.view.geometry.y - origin_y; + c.x2 = c.x1 + (int32_t)cursor->width; + c.y2 = c.y1 + (int32_t)cursor->height; } width = buffer->width < swc_fb.width ? buffer->width : swc_fb.width; height = buffer->height < swc_fb.height ? buffer->height : swc_fb.height; - if (width != swc_fb.width || height != swc_fb.height) { - memset(swc_fb.pixels, 0, swc_fb.height * swc_fb.pitch); - } - for (y = 0; y < height; ++y) { - uint32_t *source = (uint32_t *)((uint8_t *)buffer->map + - (size_t)y * buffer->pitch); - uint32_t *destination = (uint32_t *)((uint8_t *)swc_fb.pixels + - (size_t)y * swc_fb.pitch); - for (x = 0; x < width; ++x) { - uint32_t pixel = source[x]; - int32_t cx = (int32_t)x - cursor_x; - int32_t cy = (int32_t)y - cursor_y; - if (cursor_mapped && cx >= 0 && cy >= 0 && - (uint32_t)cx < cursor->width && (uint32_t)cy < cursor->height) { - uint32_t foreground = *(uint32_t *)((uint8_t *)cursor->map + - (size_t)cy * cursor->pitch + (size_t)cx * 4); - pixel = blend(pixel, foreground); - } - destination[x] = pixel; + pixman_region32_init(&region); + /* the first frame, or one that does not fill the screen, is copied whole */ + if (width != swc_fb.width || height != swc_fb.height || !whole_drawn || !damage) { + if (width != swc_fb.width || height != swc_fb.height) { + memset(swc_fb.pixels, 0, swc_fb.height * swc_fb.pitch); } + pixman_region32_init_rect(&region, 0, 0, swc_fb.width, swc_fb.height); + whole_drawn = width == swc_fb.width && height == swc_fb.height; + } else { + pixman_region32_copy(&region, damage); + pixman_region32_union_rect(&region, &region, drawn.x1, drawn.y1, + drawn.x2 - drawn.x1, drawn.y2 - drawn.y1); + pixman_region32_union_rect(&region, &region, c.x1, c.y1, + c.x2 - c.x1, c.y2 - c.y1); + } + pixman_region32_intersect_rect(&region, &region, 0, 0, width, height); + + boxes = pixman_region32_rectangles(&region, &n); + for (i = 0; i < n; ++i) { + copy_box(buffer, &boxes[i], cursor, &c); } - if (cursor_mapped) { + drawn = c; + if (cursor) { wld_unmap(cursor); } wld_unmap(buffer); - return framebuffer_present(&swc_fb); + if (width != swc_fb.width || height != swc_fb.height) { + pixman_region32_union_rect(&region, &region, 0, 0, swc_fb.width, swc_fb.height); + } + ok = framebuffer_present(&swc_fb, &region); + pixman_region32_fini(&region); + return ok; } diff --git a/libswc/fb.h b/libswc/fb.h @@ -3,6 +3,7 @@ #include "backend.h" +#include <pixman.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -23,11 +24,14 @@ extern struct swc_fb swc_fb; bool fb_initialize(void); void fb_finalize(void); bool fb_create_screens(struct wl_list *screens); -bool fb_present(struct wld_buffer *buffer, int32_t x, int32_t y); +/* damage is what the frame changed, NULL when unknown */ +bool fb_present(struct wld_buffer *buffer, int32_t x, int32_t y, + const pixman_region32_t *damage); bool framebuffer_initialize(struct swc_fb *fb); void framebuffer_finalize(struct swc_fb *fb); -bool framebuffer_present(struct swc_fb *fb); +/* the copy's pixels within region onto the screen */ +bool framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region); const char *framebuffer_name(void); #endif diff --git a/libswc/fb_fbdev.c b/libswc/fb_fbdev.c @@ -167,18 +167,44 @@ store_pixel(uint8_t *destination, uint32_t pixel) } } +/* a pixel of the copy is the screen's own: rows go over whole */ +static bool +native_is_xrgb(void) +{ + return fbdev.bits_per_pixel == 32 && fbdev.red.offset == 16 && + fbdev.red.length == 8 && fbdev.green.offset == 8 && + fbdev.green.length == 8 && fbdev.blue.offset == 0 && + fbdev.blue.length == 8; +} + bool -framebuffer_present(struct swc_fb *fb) +framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region) { uint32_t bytes = fbdev.bits_per_pixel / 8; - uint32_t x, y; - - for (y = 0; y < fb->height; ++y) { - uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + y * fb->pitch); - uint8_t *destination = (uint8_t *)fbdev.memory + fbdev.memory_offset + - (size_t)y * fbdev.pitch; - for (x = 0; x < fb->width; ++x) { - store_pixel(destination + (size_t)x * bytes, source[x]); + bool same = native_is_xrgb(); + const pixman_box32_t *boxes; + int32_t x, y; + int i, n; + + boxes = pixman_region32_rectangles((pixman_region32_t *)region, &n); + for (i = 0; i < n; ++i) { + const pixman_box32_t *b = &boxes[i]; + + for (y = b->y1; y < b->y2; ++y) { + uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + + (size_t)y * fb->pitch); + uint8_t *destination = (uint8_t *)fbdev.memory + + fbdev.memory_offset + + (size_t)y * fbdev.pitch; + + if (same) { + memcpy(destination + (size_t)b->x1 * 4, source + b->x1, + (size_t)(b->x2 - b->x1) * 4); + continue; + } + for (x = b->x1; x < b->x2; ++x) { + store_pixel(destination + (size_t)x * bytes, source[x]); + } } } return true; diff --git a/libswc/fb_wsdisplay.c b/libswc/fb_wsdisplay.c @@ -331,20 +331,44 @@ store_pixel(uint8_t *destination, uint32_t pixel) } } +/* a pixel of the copy is the screen's own: rows go over whole */ +static bool +native_is_xrgb(void) +{ + return wsdisplay.bits_per_pixel == 32 && wsdisplay.red.offset == 16 && + wsdisplay.red.length == 8 && wsdisplay.green.offset == 8 && + wsdisplay.green.length == 8 && wsdisplay.blue.offset == 0 && + wsdisplay.blue.length == 8; +} + bool -framebuffer_present(struct swc_fb *fb) +framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region) { uint32_t bytes = (wsdisplay.bits_per_pixel + 7) / 8; - uint32_t x, y; - - for (y = 0; y < fb->height; ++y) { - uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + - (size_t)y * fb->pitch); - uint8_t *destination = (uint8_t *)wsdisplay.memory + - wsdisplay.memory_offset + - (size_t)y * wsdisplay.pitch; - for (x = 0; x < fb->width; ++x) { - store_pixel(destination + (size_t)x * bytes, source[x]); + bool same = native_is_xrgb(); + const pixman_box32_t *boxes; + int32_t x, y; + int i, n; + + boxes = pixman_region32_rectangles((pixman_region32_t *)region, &n); + for (i = 0; i < n; ++i) { + const pixman_box32_t *b = &boxes[i]; + + for (y = b->y1; y < b->y2; ++y) { + uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + + (size_t)y * fb->pitch); + uint8_t *destination = (uint8_t *)wsdisplay.memory + + wsdisplay.memory_offset + + (size_t)y * wsdisplay.pitch; + + if (same) { + memcpy(destination + (size_t)b->x1 * 4, source + b->x1, + (size_t)(b->x2 - b->x1) * 4); + continue; + } + for (x = b->x1; x < b->x2; ++x) { + store_pixel(destination + (size_t)x * bytes, source[x]); + } } } return true; diff --git a/libswc/keyboard.c b/libswc/keyboard.c @@ -37,6 +37,7 @@ #include <fcntl.h> #include <limits.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <unistd.h> @@ -229,6 +230,23 @@ error0: return false; } +/* The keymap in the file SWC_KEYMAP_FILE names, NULL when there is none. */ +static struct xkb_keymap * +keymap_from_file(struct xkb_context *context) +{ + const char *path = getenv("SWC_KEYMAP_FILE"); + struct xkb_keymap *map; + FILE *file; + + if (!path || !*path || !(file = fopen(path, "r"))) + return NULL; + map = xkb_keymap_new_from_file(context, file, XKB_KEYMAP_FORMAT_TEXT_V1, 0); + fclose(file); + if (!map) + WARNING("Could not compile the keymap in %s\n", path); + return map; +} + struct keyboard * keyboard_create(struct xkb_rule_names *names) { @@ -246,7 +264,10 @@ keyboard_create(struct xkb_rule_names *names) goto error1; } - if (!(xkb->keymap.map = + /* a whole keymap from a file, when the session names one, needs no xkb + * data of the system's; names need the rules and files they name */ + if (!(xkb->keymap.map = keymap_from_file(xkb->context)) && + !(xkb->keymap.map = xkb_keymap_new_from_names(xkb->context, names, 0))) { ERROR("Could not create XKB keymap\n"); goto error2; diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c @@ -387,6 +387,73 @@ static const struct zwlr_layer_surface_v1_interface layer_surface_impl = { .set_exclusive_edge = set_exclusive_edge, }; +/* an exclusive surface has every key while it is mapped; the view a + * window asked for meanwhile gets them when it goes */ +static struct { + struct layer_surface *holder; + struct compositor_view *wanted; + struct wl_listener wanted_destroy; +} keys; + +static void +handle_wanted_destroy(struct wl_listener *listener, void *data) +{ + (void)data; + wl_list_remove(&listener->link); + keys.wanted = NULL; +} + +static void +want_keys(struct compositor_view *view) +{ + if (keys.wanted) { + wl_list_remove(&keys.wanted_destroy.link); + } + keys.wanted = view; + if (view) { + keys.wanted_destroy.notify = handle_wanted_destroy; + wl_signal_add(&view->destroy_signal, &keys.wanted_destroy); + } +} + +static void +hold_keys(struct layer_surface *surface) +{ + struct compositor_view *focus = swc.seat->keyboard->focus.view; + + /* a second exclusive surface over the first gives back what the first held */ + if (!keys.holder) { + want_keys(focus); + } + keys.holder = surface; + keyboard_set_focus(swc.seat->keyboard, surface->view); +} + +static void +release_keys(struct layer_surface *surface) +{ + struct compositor_view *view = keys.wanted; + + if (keys.holder != surface) { + return; + } + keys.holder = NULL; + want_keys(NULL); + if (swc.seat->keyboard->focus.view == surface->view) { + keyboard_set_focus(swc.seat->keyboard, view); + } +} + +bool +layer_shell_hold_keys(struct compositor_view *view) +{ + if (!keys.holder) { + return false; + } + want_keys(view); + return true; +} + static void handle_attach(struct view_handler *handler) { @@ -402,10 +469,11 @@ handle_attach(struct view_handler *handler) /* on demand waits for a press, see layer_shell_pressed */ if (surface->current.keyboard_interactivity == ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE) { - keyboard_set_focus(swc.seat->keyboard, surface->view); + hold_keys(surface); } } } else if (surface->mapped) { + release_keys(surface); compositor_view_hide(surface->view); } @@ -535,6 +603,7 @@ destroy_layer_surface(struct wl_resource *resource) wl_list_remove(&surface->surface_destroy_listener.link); wl_list_remove(&surface->surface_commit_listener.link); wl_list_remove(&surface->modifier.link); + release_keys(surface); compositor_view_destroy(surface->view); if (had_screen) { screen_update_usable_geometry(surface->screen); diff --git a/libswc/layer_shell.h b/libswc/layer_shell.h @@ -1,6 +1,8 @@ #ifndef SWC_LAYER_SHELL_H #define SWC_LAYER_SHELL_H +#include <stdbool.h> + struct wl_display; struct wl_global; @@ -18,4 +20,8 @@ struct compositor_view; void layer_shell_pressed(struct compositor_view *view); +/* true while an exclusive surface has the keys: view gets them after it */ +bool +layer_shell_hold_keys(struct compositor_view *view); + #endif diff --git a/libswc/meson.build b/libswc/meson.build @@ -40,7 +40,7 @@ libswc_src = files( 'xdg_output.c', 'xdg_shell.c', - input_backend == 'libinput' ? 'seat.c' : [], + input_backend == 'libinput' ? ['seat.c', 'touch.c'] : [], input_backend == 'wscons' ? 'seat_ws.c' : [], input_backend == 'wscons' and os == 'openbsd' ? 'seat_ws_openbsd.c' : [], @@ -55,6 +55,9 @@ libswc_src += [cursor, launch_protocol] if have_nested libswc_src += files('nested.c') endif +if have_peer + libswc_src += files('peer.c') +endif foreach proto, proto_src : protos libswc_src += proto_src diff --git a/libswc/peer.c b/libswc/peer.c @@ -0,0 +1,506 @@ +/* swc: libswc/peer.c + * + * the screen drawn in software into shared memory and shown by a peer, which + * sends back the input: a program of another system holding the real display, + * such as an application of it. see peer_protocol.h + */ + +#include "peer.h" +#include "backend.h" +#include "compositor.h" +#include "internal.h" +#include "keyboard.h" +#include "output.h" +#include "peer_protocol.h" +#include "pointer.h" +#include "screen.h" +#include "seat.h" +#include "surface.h" +#include "touch.h" +#include "util.h" + +#include <errno.h> +#include <fcntl.h> +#include <pixman.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <wld/pixman.h> +#include <wld/wld.h> + +/* a screen past this is a peer's mistake, not a size to allocate */ +#define SIZE_LIMIT 16384 + +static struct { + struct swc_backend backend; + int fd; + struct wl_event_source *source; + + struct screen *screen; + uint32_t width, height; + /* the buffer shared with the peer, and what it has not been sent */ + uint32_t *pixels; + size_t size; + bool whole; + + bool in_flight; /* a frame sent and not yet done with */ + bool frame_due; /* done, but the frame callback waits for it to be seen */ + bool shown; + + /* a message read in part */ + struct swc_peer_message in; + size_t got; +} peer; + +static bool +send_message(const struct swc_peer_message *message, int descriptor) +{ + union { + struct cmsghdr header; + char space[CMSG_SPACE(sizeof(int))]; + } control; + struct iovec iov = { (void *)message, sizeof(*message) }; + struct msghdr msg = { 0 }; + struct cmsghdr *header; + size_t sent = 0; + ssize_t n; + + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + if (descriptor >= 0) { + msg.msg_control = control.space; + msg.msg_controllen = sizeof(control.space); + header = CMSG_FIRSTHDR(&msg); + header->cmsg_level = SOL_SOCKET; + header->cmsg_type = SCM_RIGHTS; + header->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(header), &descriptor, sizeof(int)); + } + while (sent < sizeof(*message)) { + n = sendmsg(peer.fd, &msg, MSG_NOSIGNAL); + if (n < 0) { + if (errno == EINTR) { + continue; + } + ERROR("peer: cannot send: %s\n", strerror(errno)); + return false; + } + sent += (size_t)n; + iov.iov_base = (char *)message + sent; + iov.iov_len = sizeof(*message) - sent; + msg.msg_control = NULL; + msg.msg_controllen = 0; + } + return true; +} + +/* a new buffer of width by height, handed to the peer */ +static bool +share_buffer(uint32_t width, uint32_t height) +{ + struct swc_peer_message message = { SWC_PEER_BUFFER, { 0 } }; + size_t size = (size_t)width * height * 4; + uint32_t *pixels; + char path[32]; + int fd, shown = -1; + + fd = memfd_create("swc-peer", MFD_CLOEXEC); + if (fd < 0 || ftruncate(fd, (off_t)size) < 0) { + ERROR("peer: cannot make the screen buffer: %s\n", strerror(errno)); + goto fail; + } + /* the peer only shows the buffer, so it gets a descriptor that cannot + * write it: a confined peer may be refused one that can */ + snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); + shown = open(path, O_RDONLY | O_CLOEXEC); + if (shown < 0) { + ERROR("peer: cannot open the screen buffer to be read: %s\n", strerror(errno)); + goto fail; + } + pixels = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (pixels == MAP_FAILED) { + ERROR("peer: cannot map the screen buffer: %s\n", strerror(errno)); + goto fail; + } + if (peer.pixels) { + munmap(peer.pixels, peer.size); + } + peer.pixels = pixels; + peer.size = size; + peer.width = width; + peer.height = height; + peer.whole = true; + peer.in_flight = false; + + message.arg[0] = (int32_t)width; + message.arg[1] = (int32_t)height; + message.arg[2] = (int32_t)width * 4; + if (!send_message(&message, shown)) { + goto fail; + } + close(shown); + close(fd); + return true; + +fail: + if (shown >= 0) { + close(shown); + } + if (fd >= 0) { + close(fd); + } + return false; +} + +/* the composited frame's damaged rows into the shared buffer */ +static int +plane_attach(struct view *view, struct wld_buffer *buffer) +{ + const pixman_region32_t *damage = compositor_frame_damage(); + struct swc_peer_message message = { SWC_PEER_FRAME, { 0 } }; + pixman_region32_t region; + const pixman_box32_t *boxes, *extents; + uint32_t width, height; + int32_t y; + int i, n; + + (void)view; + if (!buffer || !wld_map(buffer)) { + return 0; + } + width = MIN(buffer->width, peer.width); + height = MIN(buffer->height, peer.height); + pixman_region32_init(&region); + if (peer.whole || !damage) { + pixman_region32_union_rect(&region, &region, 0, 0, width, height); + peer.whole = false; + } else { + pixman_region32_copy(&region, (pixman_region32_t *)damage); + pixman_region32_intersect_rect(&region, &region, 0, 0, width, height); + } + boxes = pixman_region32_rectangles(&region, &n); + for (i = 0; i < n; ++i) { + for (y = boxes[i].y1; y < boxes[i].y2; ++y) { + memcpy(peer.pixels + (size_t)y * peer.width + boxes[i].x1, + (const uint8_t *)buffer->map + (size_t)y * buffer->pitch + + (size_t)boxes[i].x1 * 4, + (size_t)(boxes[i].x2 - boxes[i].x1) * 4); + } + } + wld_unmap(buffer); + + extents = pixman_region32_extents(&region); + message.arg[0] = extents->x1; + message.arg[1] = extents->y1; + message.arg[2] = extents->x2 - extents->x1; + message.arg[3] = extents->y2 - extents->y1; + pixman_region32_fini(&region); + peer.in_flight = true; + send_message(&message, -1); + return 0; +} + +static bool +plane_update(struct view *view) +{ + (void)view; + return true; +} + +static bool +plane_move(struct view *view, int32_t x, int32_t y) +{ + view_set_position(view, x, y); + return true; +} + +static const struct view_impl plane_impl = { + .update = plane_update, + .attach = plane_attach, + .move = plane_move, +}; + +static void +next_frame(void) +{ + if (!peer.shown) { + peer.frame_due = true; + return; + } + peer.frame_due = false; + view_frame(&peer.screen->planes.primary.view, get_time()); +} + +static bool +valid_size(int32_t width, int32_t height) +{ + return width > 0 && height > 0 && width <= SIZE_LIMIT && height <= SIZE_LIMIT; +} + +static void +handle_message(const struct swc_peer_message *message) +{ + const int32_t *arg = message->arg; + uint32_t time = get_time(); + wl_fixed_t x, y; + + switch (message->type) { + case SWC_PEER_SIZE: + if (valid_size(arg[0], arg[1]) && + ((uint32_t)arg[0] != peer.width || (uint32_t)arg[1] != peer.height) && + share_buffer((uint32_t)arg[0], (uint32_t)arg[1])) { + screen_resize(peer.screen, peer.width, peer.height); + next_frame(); + } + break; + case SWC_PEER_DONE: + if (peer.in_flight) { + peer.in_flight = false; + next_frame(); + } + break; + case SWC_PEER_SHOWN: + peer.shown = arg[0] != 0; + if (peer.shown) { + /* what was lost while unseen: the whole screen again */ + peer.whole = true; + compositor_damage_all(); + if (peer.frame_due) { + next_frame(); + } + } + break; + case SWC_PEER_TOUCH: + if (!swc.seat || !swc.seat->touch) { + break; + } + x = wl_fixed_from_int(arg[2]); + y = wl_fixed_from_int(arg[3]); + if (arg[1] == SWC_PEER_DOWN) { + touch_handle_down(swc.seat->touch, time, arg[0], x, y); + } else if (arg[1] == SWC_PEER_MOVE) { + touch_handle_motion(swc.seat->touch, time, arg[0], x, y); + } else if (arg[1] == SWC_PEER_UP) { + touch_handle_up(swc.seat->touch, time, arg[0]); + } else { + touch_handle_cancel(swc.seat->touch); + } + break; + case SWC_PEER_TOUCH_FRAME: + if (swc.seat && swc.seat->touch) { + touch_handle_frame(swc.seat->touch); + } + break; + case SWC_PEER_KEY: + if (swc.seat) { + keyboard_handle_key(swc.seat->keyboard, time, (uint32_t)arg[0], + arg[1] ? WL_KEYBOARD_KEY_STATE_PRESSED + : WL_KEYBOARD_KEY_STATE_RELEASED); + } + break; + case SWC_PEER_MOTION: + if (swc.seat) { + pointer_handle_absolute_motion(swc.seat->pointer, time, + wl_fixed_from_int(arg[0]), + wl_fixed_from_int(arg[1])); + pointer_handle_frame(swc.seat->pointer); + } + break; + case SWC_PEER_BUTTON: + if (swc.seat) { + pointer_handle_button(swc.seat->pointer, time, (uint32_t)arg[0], + arg[1] ? WL_POINTER_BUTTON_STATE_PRESSED + : WL_POINTER_BUTTON_STATE_RELEASED); + pointer_handle_frame(swc.seat->pointer); + } + break; + case SWC_PEER_SCROLL: + if (swc.seat && arg[0] >= 0 && arg[0] <= 1) { + pointer_handle_axis(swc.seat->pointer, time, (uint32_t)arg[0], + WL_POINTER_AXIS_SOURCE_CONTINUOUS, arg[1], 0); + pointer_handle_frame(swc.seat->pointer); + } + break; + } +} + +static int +handle_data(int fd, uint32_t mask, void *data) +{ + ssize_t n; + + (void)data; + if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR) && !(mask & WL_EVENT_READABLE)) { + wl_display_terminate(swc.display); + return 0; + } + for (;;) { + n = recv(fd, (char *)&peer.in + peer.got, sizeof(peer.in) - peer.got, + MSG_DONTWAIT); + if (n < 0 && errno == EINTR) { + continue; + } + if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { + break; + } + if (n <= 0) { + /* the peer is gone: the session has nowhere to show */ + wl_event_source_remove(peer.source); + peer.source = NULL; + wl_display_terminate(swc.display); + break; + } + peer.got += (size_t)n; + if (peer.got == sizeof(peer.in)) { + peer.got = 0; + handle_message(&peer.in); + } + } + return 0; +} + +static bool +create_screens(struct wl_list *screens) +{ + struct output *output; + + output = output_new_fb(peer.width, peer.height, "peer"); + if (!output) { + return false; + } + peer.screen = screen_new_virtual(output, &plane_impl); + if (!peer.screen) { + output_destroy(output); + return false; + } + output->screen = peer.screen; + wl_list_insert(screens, &peer.screen->link); + return true; +} + +static void +start(void) +{ + peer.source = wl_event_loop_add_fd(swc.event_loop, peer.fd, + WL_EVENT_READABLE, &handle_data, NULL); + if (!peer.source) { + ERROR("peer: cannot watch the socket\n"); + wl_display_terminate(swc.display); + return; + } + /* no console to switch to it: it is the one screen from the start */ + swc_activate(); +} + +static void +finalize(void) +{ + if (peer.source) { + wl_event_source_remove(peer.source); + } + if (peer.backend.renderer) { + wld_destroy_renderer(peer.backend.renderer); + } + if (peer.backend.context) { + wld_destroy_context(peer.backend.context); + } + if (peer.pixels) { + munmap(peer.pixels, peer.size); + } + close(peer.fd); +} + +static int +connect_to(const char *path) +{ + struct sockaddr_un address = { .sun_family = AF_UNIX }; + size_t length = strlen(path); + int fd; + + if (length == 0 || length >= sizeof(address.sun_path)) { + ERROR("peer: SWC_PEER_SOCKET is empty or too long\n"); + return -1; + } + memcpy(address.sun_path, path, length); + if (path[0] == '@') { + address.sun_path[0] = '\0'; + } else { + length++; + } + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd < 0 || connect(fd, (struct sockaddr *)&address, + (socklen_t)(offsetof(struct sockaddr_un, sun_path) + length)) < 0) { + ERROR("peer: cannot connect to %s: %s\n", path, strerror(errno)); + if (fd >= 0) { + close(fd); + } + return -1; + } + return fd; +} + +bool +peer_requested(void) +{ + return getenv("SWC_PEER_SOCKET") != NULL; +} + +bool +peer_initialize(void) +{ + struct swc_peer_message message; + size_t got = 0; + ssize_t n; + + peer.fd = connect_to(getenv("SWC_PEER_SOCKET")); + if (peer.fd < 0) { + return false; + } + /* the screen's size, before there is a screen */ + while (got < sizeof(message)) { + n = recv(peer.fd, (char *)&message + got, sizeof(message) - got, 0); + if (n < 0 && errno == EINTR) { + continue; + } + if (n <= 0) { + ERROR("peer: it said no size\n"); + goto fail; + } + got += (size_t)n; + } + if (message.type != SWC_PEER_SIZE || !valid_size(message.arg[0], message.arg[1])) { + ERROR("peer: it said no size\n"); + goto fail; + } + if (!share_buffer((uint32_t)message.arg[0], (uint32_t)message.arg[1])) { + goto fail; + } + peer.shown = true; + + peer.backend.context = wld_pixman_create_context(); + if (!peer.backend.context) { + goto fail; + } + peer.backend.renderer = wld_create_renderer(peer.backend.context); + if (!peer.backend.renderer) { + goto fail; + } + peer.backend.cursor_width = 64; + peer.backend.cursor_height = 64; + peer.backend.virtual_input = true; + peer.backend.virtual_touch = true; + peer.backend.create_screens = create_screens; + peer.backend.start = start; + peer.backend.finalize = finalize; + swc.backend = &peer.backend; + return true; + +fail: + finalize(); + return false; +} diff --git a/libswc/peer.h b/libswc/peer.h @@ -0,0 +1,16 @@ +/* swc: libswc/peer.h + * + * a screen shown by another program over a socket, see peer_protocol.h + */ + +#ifndef SWC_PEER_H +#define SWC_PEER_H + +#include <stdbool.h> + +bool +peer_requested(void); +bool +peer_initialize(void); + +#endif diff --git a/libswc/peer_protocol.h b/libswc/peer_protocol.h @@ -0,0 +1,59 @@ +/* swc: libswc/peer_protocol.h + * + * the peer backend and the program that shows its frames and sends it input, + * its peer, over a stream socket: SWC_PEER_SOCKET, a path, or @name in + * linux's abstract namespace. the peer listens, swc connects. every message + * is one struct swc_peer_message in the machine's byte order; the one from + * swc that carries a descriptor, open to be read only, carries it with its + * first byte (SCM_RIGHTS) + * + * the peer's first message is SWC_PEER_SIZE. swc then sends SWC_PEER_BUFFER + * and, as the screen changes, SWC_PEER_FRAME. it draws the next frame only + * after SWC_PEER_DONE, and none while the peer says it is hidden. the + * session ends when the socket closes + */ + +#ifndef SWC_PEER_PROTOCOL_H +#define SWC_PEER_PROTOCOL_H + +#include <stdint.h> + +enum { + /* from swc: a shared memory descriptor holding the screen, one + * XRGB8888 pixel a 32 bit word: arg width, height, bytes per row. + * it replaces any before it */ + SWC_PEER_BUFFER = 1, + /* from swc: the buffer changed in x y width height. the peer reads it + * until it answers SWC_PEER_DONE */ + SWC_PEER_FRAME = 2, + + /* from the peer: the screen's size in pixels, at the start and when + * it changes: arg width, height */ + SWC_PEER_SIZE = 16, + /* the last frame has been taken */ + SWC_PEER_DONE = 17, + /* arg 1 when the screen is seen, 0 when it is not */ + SWC_PEER_SHOWN = 18, + /* arg slot, phase (SWC_PEER_DOWN...), x, y in screen pixels */ + SWC_PEER_TOUCH = 19, + /* the touches since the last one happened together */ + SWC_PEER_TOUCH_FRAME = 20, + /* arg linux evdev code, 1 pressed or 0 let go */ + SWC_PEER_KEY = 21, + /* a mouse: arg x, y in screen pixels */ + SWC_PEER_MOTION = 22, + /* arg linux evdev button, 1 pressed or 0 let go */ + SWC_PEER_BUTTON = 23, + /* arg wl_pointer axis, the distance as wl_fixed_t */ + SWC_PEER_SCROLL = 24, +}; + +/* phases of SWC_PEER_TOUCH */ +enum { SWC_PEER_DOWN = 0, SWC_PEER_MOVE = 1, SWC_PEER_UP = 2, SWC_PEER_CANCEL = 3 }; + +struct swc_peer_message { + uint32_t type; + int32_t arg[5]; +}; + +#endif diff --git a/libswc/pointer.c b/libswc/pointer.c @@ -255,6 +255,7 @@ move(struct view *view, int32_t x, int32_t y) #ifdef ENABLE_DRM struct screen *screen; #endif + struct swc_rectangle old = view->geometry; if (view_set_position(view, x, y)) { view_update_screens(view); @@ -275,7 +276,11 @@ move(struct view *view, int32_t x, int32_t y) return true; } #endif - compositor_damage_all(); + /* a backend that draws the cursor into the frame repaints only where it + * was and where it is */ + compositor_damage_rect(old.x, old.y, old.width, old.height); + compositor_damage_rect(view->geometry.x, view->geometry.y, + view->geometry.width, view->geometry.height); return true; } diff --git a/libswc/primary_plane.c b/libswc/primary_plane.c @@ -22,8 +22,8 @@ */ #include "primary_plane.h" -#include "event.h" #include "compositor.h" +#include "event.h" #ifdef ENABLE_DRM #include "drm.h" #endif @@ -95,7 +95,8 @@ attach_drm(struct primary_plane *plane, struct wld_buffer *buffer) static int attach_fb(struct primary_plane *plane, struct wld_buffer *buffer) { - if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y)) { + if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y, + compositor_frame_damage())) { return -1; } wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); diff --git a/libswc/screen.c b/libswc/screen.c @@ -44,6 +44,7 @@ #include "swc-server-protocol.h" #include <stdlib.h> +#include <wayland-server.h> #define INTERNAL(s) ((struct screen *)(s)) @@ -276,12 +277,51 @@ screen_new_virtual(struct output *output, const struct view_impl *impl) return screen; } +/* the pointer may go anywhere on a screen and nowhere else */ +static void +pointer_region_update(void) +{ + struct screen *screen; + pixman_region32_t region; + + if (!swc.seat) { + return; + } + pixman_region32_init(&region); + wl_list_for_each(screen, &swc.screens, link) + pixman_region32_union_rect(&region, &region, + screen->base.geometry.x, + screen->base.geometry.y, + screen->base.geometry.width, + screen->base.geometry.height); + pointer_set_region(swc.seat->pointer, &region); + pixman_region32_fini(&region); +} + +/* the screen's area changed: what depends on it follows, and the window + * manager is told */ +static void +screen_changed(struct screen *screen, bool resized) +{ + if (resized) { + compositor_screen_resize(screen); + } else { + compositor_damage_all(); + } + pointer_region_update(); + /* a change can leave origin and edges of the usable area as they + * were, so it is told anyway */ + screen->base.usable_geometry.width = 0; + screen_update_usable_geometry(screen); + if (screen->handler->geometry_changed) { + screen->handler->geometry_changed(screen->handler_data); + } +} + void screen_resize(struct screen *screen, uint32_t width, uint32_t height) { struct output *output; - struct screen *other; - pixman_region32_t region; screen->planes.primary.view.geometry.width = width; screen->planes.primary.view.geometry.height = height; @@ -293,23 +333,140 @@ screen_resize(struct screen *screen, uint32_t width, uint32_t height) wl_list_for_each(output, &screen->outputs, link) output_set_size(output, width, height); - compositor_screen_resize(screen); - - if (swc.seat) { - pixman_region32_init(&region); - wl_list_for_each(other, &swc.screens, link) - pixman_region32_union_rect(&region, &region, - other->base.geometry.x, - other->base.geometry.y, - other->base.geometry.width, - other->base.geometry.height); - pointer_set_region(swc.seat->pointer, &region); - pixman_region32_fini(&region); + screen_changed(screen, true); +} + +static struct output * +screen_output(struct screen *screen) +{ + struct output *output; + + if (wl_list_empty(&screen->outputs)) { + return NULL; } + return wl_container_of(screen->outputs.next, output, link); +} - /* a resize can leave origin and edges unchanged, notify anyway */ - screen->base.usable_geometry.width = 0; - screen_update_usable_geometry(screen); +EXPORT const char * +swc_screen_name(struct swc_screen *base) +{ + struct output *output = screen_output(INTERNAL(base)); + + return output ? output->name : ""; +} + +EXPORT enum swc_screen_kind +swc_screen_kind(struct swc_screen *base) +{ + struct screen *screen = INTERNAL(base); + + if (screen->virtual) { + return SWC_SCREEN_WINDOW; + } +#ifdef ENABLE_DRM + if (swc.video == SWC_VIDEO_DRM) { + return SWC_SCREEN_DISPLAY; + } +#endif + return SWC_SCREEN_FRAMEBUFFER; +} + +EXPORT int +swc_screen_modes(struct swc_screen *base, struct swc_mode *out, int max) +{ + struct screen *screen = INTERNAL(base); + struct output *output = screen_output(screen); + struct mode *mode; + int n = 0; + + if (!output) { + return 0; + } + wl_array_for_each(mode, &output->modes) + { + if (n < max) { + out[n] = (struct swc_mode){ + .width = mode->width, + .height = mode->height, + .refresh = mode->refresh, + .preferred = mode->preferred, + .current = mode_equal(&screen->planes.primary.mode, mode), + }; + } + n++; + } + return n; +} + +EXPORT bool +swc_screen_set_mode(struct swc_screen *base, uint32_t width, uint32_t height, + uint32_t refresh) +{ + struct screen *screen = INTERNAL(base); + struct output *output = screen_output(screen); + struct mode *mode, *best = NULL; + struct wl_resource *resource; + + if (!output || swc_screen_kind(base) != SWC_SCREEN_DISPLAY) { + return false; + } + wl_array_for_each(mode, &output->modes) + { + if (mode->width != width || mode->height != height) { + continue; + } + if (refresh ? mode->refresh == refresh + : !best || mode->refresh > best->refresh) { + best = mode; + } + } + if (!best) { + return false; + } +#ifdef ENABLE_DRM + screen->planes.primary.mode = *best; + screen->planes.primary.need_modeset = true; +#endif + screen->planes.primary.view.geometry.width = width; + screen->planes.primary.view.geometry.height = height; + screen->base.geometry.width = width; + screen->base.geometry.height = height; + wl_resource_for_each(resource, &output->resources) + { + wl_output_send_mode(resource, + WL_OUTPUT_MODE_CURRENT | + (best->preferred ? WL_OUTPUT_MODE_PREFERRED : 0), + best->width, best->height, best->refresh); + if (wl_resource_get_version(resource) >= 2) { + wl_output_send_done(resource); + } + } + screen_changed(screen, true); + return true; +} + +EXPORT void +swc_screen_move(struct swc_screen *base, int32_t x, int32_t y) +{ + struct screen *screen = INTERNAL(base); + struct output *output = screen_output(screen); + struct wl_resource *resource; + + view_move(&screen->planes.primary.view, x, y); + screen->base.geometry.x = x; + screen->base.geometry.y = y; + if (output) { + wl_resource_for_each(resource, &output->resources) + { + wl_output_send_geometry(resource, x, y, output->physical_width, + output->physical_height, 0, "unknown", + "unknown", WL_OUTPUT_TRANSFORM_NORMAL); + if (wl_resource_get_version(resource) >= 2) { + wl_output_send_done(resource); + } + } + } + screen_changed(screen, false); } void diff --git a/libswc/seat.c b/libswc/seat.c @@ -32,6 +32,7 @@ #include "pointer.h" #include "screen.h" #include "surface.h" +#include "touch.h" #include "util.h" #include <dirent.h> @@ -169,7 +170,12 @@ get_keyboard(struct wl_client *client, struct wl_resource *resource, static void get_touch(struct wl_client *client, struct wl_resource *resource, uint32_t id) { - /* XXX: Implement */ + struct seat *seat = wl_resource_get_user_data(resource); + + if (!touch_bind(seat->base.touch, client, wl_resource_get_version(resource), + id)) { + wl_resource_post_no_memory(resource); + } } static const struct wl_seat_interface seat_impl = { @@ -240,13 +246,16 @@ device_capabilities(struct libinput_device *device) if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { capabilities |= WL_SEAT_CAPABILITY_KEYBOARD; } - if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { + /* a tablet's pen drives the pointer (see tablet_tool below) */ + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER) || + libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { capabilities |= WL_SEAT_CAPABILITY_POINTER; } - /* TODO: Add touch device support - * if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) - * capabilities |= WL_SEAT_CAPABILITY_TOUCH; - */ + /* a finger drives the pointer where no client takes it (touch.c), so a + * touchscreen is a pointer too */ + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { + capabilities |= WL_SEAT_CAPABILITY_TOUCH | WL_SEAT_CAPABILITY_POINTER; + } return capabilities; } @@ -262,6 +271,8 @@ handle_libinput_data(int fd, uint32_t mask, void *data) union { struct libinput_event_keyboard *k; struct libinput_event_pointer *p; + struct libinput_event_tablet_tool *t; + struct libinput_event_touch *touch; } event; wl_fixed_t x, y, value; uint32_t time, key, state; @@ -282,6 +293,36 @@ handle_libinput_data(int fd, uint32_t mask, void *data) swc.manager->new_device(device); } break; + case LIBINPUT_EVENT_TOUCH_DOWN: + case LIBINPUT_EVENT_TOUCH_MOTION: + /* the panel's own coordinates are the first screen's */ + screen = wl_container_of(swc.screens.next, screen, link); + rect = &screen->base.geometry; + event.touch = libinput_event_get_touch_event(generic_event); + time = libinput_event_touch_get_time(event.touch); + x = wl_fixed_from_double(rect->x + + libinput_event_touch_get_x_transformed(event.touch, rect->width)); + y = wl_fixed_from_double(rect->y + + libinput_event_touch_get_y_transformed(event.touch, rect->height)); + if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TOUCH_DOWN) { + touch_handle_down(seat->base.touch, time, + libinput_event_touch_get_seat_slot(event.touch), x, y); + } else { + touch_handle_motion(seat->base.touch, time, + libinput_event_touch_get_seat_slot(event.touch), x, y); + } + break; + case LIBINPUT_EVENT_TOUCH_UP: + event.touch = libinput_event_get_touch_event(generic_event); + touch_handle_up(seat->base.touch, libinput_event_touch_get_time(event.touch), + libinput_event_touch_get_seat_slot(event.touch)); + break; + case LIBINPUT_EVENT_TOUCH_FRAME: + touch_handle_frame(seat->base.touch); + break; + case LIBINPUT_EVENT_TOUCH_CANCEL: + touch_handle_cancel(seat->base.touch); + break; case LIBINPUT_EVENT_KEYBOARD_KEY: event.k = libinput_event_get_keyboard_event(generic_event); time = libinput_event_keyboard_get_time(event.k); @@ -311,6 +352,42 @@ handle_libinput_data(int fd, uint32_t mask, void *data) pointer_handle_absolute_motion(&seat->pointer, time, x, y); pointer_handle_frame(&seat->pointer); break; + /* A tablet's pen as the pointer until clients are told of tablets: + * where it points, its tip the left button and its barrel's two + * buttons the middle and right ones, as a mouse would have them. */ + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + screen = wl_container_of(swc.screens.next, screen, link); + rect = &screen->base.geometry; + event.t = libinput_event_get_tablet_tool_event(generic_event); + time = libinput_event_tablet_tool_get_time(event.t); + x = wl_fixed_from_double( + libinput_event_tablet_tool_get_x_transformed(event.t, rect->width)); + y = wl_fixed_from_double( + libinput_event_tablet_tool_get_y_transformed(event.t, rect->height)); + pointer_handle_absolute_motion(&seat->pointer, time, x, y); + if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TABLET_TOOL_TIP) { + pointer_handle_button(&seat->pointer, time, BTN_LEFT, + libinput_event_tablet_tool_get_tip_state(event.t) == + LIBINPUT_TABLET_TOOL_TIP_DOWN + ? WL_POINTER_BUTTON_STATE_PRESSED + : WL_POINTER_BUTTON_STATE_RELEASED); + } + pointer_handle_frame(&seat->pointer); + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + event.t = libinput_event_get_tablet_tool_event(generic_event); + time = libinput_event_tablet_tool_get_time(event.t); + key = libinput_event_tablet_tool_get_button(event.t); + key = key == BTN_STYLUS ? BTN_MIDDLE : key == BTN_STYLUS2 ? BTN_RIGHT : key; + pointer_handle_button(&seat->pointer, time, key, + libinput_event_tablet_tool_get_button_state(event.t) == + LIBINPUT_BUTTON_STATE_PRESSED + ? WL_POINTER_BUTTON_STATE_PRESSED + : WL_POINTER_BUTTON_STATE_RELEASED); + pointer_handle_frame(&seat->pointer); + break; case LIBINPUT_EVENT_POINTER_BUTTON: event.p = libinput_event_get_pointer_event(generic_event); time = libinput_event_pointer_get_time(event.p); @@ -493,17 +570,27 @@ seat_create(struct wl_display *display, const char *seat_name) } seat->base.pointer = &seat->pointer; + if (!(seat->base.touch = touch_create(&seat->pointer))) { + ERROR("Could not initialize touch\n"); + goto error6; + } + seat->libinput = NULL; seat->libinput_source = NULL; if (swc.backend->virtual_input) { update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD | - WL_SEAT_CAPABILITY_POINTER); + WL_SEAT_CAPABILITY_POINTER | + (swc.backend->virtual_touch + ? WL_SEAT_CAPABILITY_TOUCH + : 0)); } else if (!initialize_libinput(seat)) { - goto error6; + goto error7; } return &seat->base; +error7: + touch_destroy(seat->base.touch); error6: pointer_finalize(&seat->pointer); error5: @@ -533,6 +620,7 @@ seat_destroy(struct swc_seat *seat_base) #endif } + touch_destroy(seat->base.touch); pointer_finalize(&seat->pointer); keyboard_destroy(seat->base.keyboard); data_device_destroy(seat->base.data_device); diff --git a/libswc/seat.h b/libswc/seat.h @@ -30,6 +30,7 @@ struct swc_seat { struct pointer *pointer; struct keyboard *keyboard; struct data_device *data_device; + struct touch *touch; }; struct swc_seat * diff --git a/libswc/swc.c b/libswc/swc.c @@ -37,6 +37,9 @@ #include "keyboard.h" #include "launch.h" #include "layer_shell.h" +#ifdef ENABLE_PEER +#include "peer.h" +#endif #ifdef ENABLE_NESTED #include "nested.h" #endif @@ -265,6 +268,11 @@ video_initialize(void) static bool backend_initialize(void) { +#ifdef ENABLE_PEER + if (peer_requested()) { + return peer_initialize(); + } +#endif #ifdef ENABLE_NESTED if (nested_requested()) { return nested_initialize(); @@ -506,6 +514,21 @@ swc_set_keyboard_layout(const char *layout, const char *variant, return keyboard_set_names(swc.seat->keyboard, &names); } +EXPORT bool +swc_set_keyboard_keymap(const char *keymap) +{ + struct xkb_keymap *map; + bool ok; + + if (!(map = xkb_keymap_new_from_string(swc.seat->keyboard->xkb.context, + keymap, XKB_KEYMAP_FORMAT_TEXT_V1, + 0))) + return false; + ok = keyboard_set_keymap(swc.seat->keyboard, map); + xkb_keymap_unref(map); + return ok; +} + EXPORT void swc_set_keyboard_repeat(int32_t rate, int32_t delay) { diff --git a/libswc/swc.h b/libswc/swc.h @@ -302,6 +302,48 @@ void swc_screen_set_handler(struct swc_screen *screen, const struct swc_screen_handler *handler, void *data); +/* what a screen shows on, which says what may be changed about it */ +enum swc_screen_kind { + /* a display: its modes are the hardware's, any of them may be set */ + SWC_SCREEN_DISPLAY, + /* a window of another compositor: its size is that window's */ + SWC_SCREEN_WINDOW, + /* a framebuffer: it has the one mode it was set to */ + SWC_SCREEN_FRAMEBUFFER, +}; + +struct swc_mode { + uint32_t width, height; + /* in mHz */ + uint32_t refresh; + bool preferred, current; +}; + +/* the name of the screen's output, as wl_output names it (HDMI-A-1) */ +const char * +swc_screen_name(struct swc_screen *screen); + +enum swc_screen_kind +swc_screen_kind(struct swc_screen *screen); + +/* the modes the screen may be set to, at most max into out; how many + * there are */ +int +swc_screen_modes(struct swc_screen *screen, struct swc_mode *out, int max); + +/* sets the screen to the mode of that size and refresh, a refresh of 0 + * meaning the highest the size has. the change is made with the next + * frame; geometry_changed follows. false when the screen has no such mode + * or its modes are not the compositor's to set */ +bool +swc_screen_set_mode(struct swc_screen *screen, uint32_t width, + uint32_t height, uint32_t refresh); + +/* places the screen's top left corner at x, y of the space the screens + * share; geometry_changed follows */ +void +swc_screen_move(struct swc_screen *screen, int32_t x, int32_t y); + /* }}} */ /* Windows {{{ */ @@ -617,7 +659,8 @@ enum swc_decor_align { * for left and right edges, text is drawn as stacked glyphs, one glyph * per row. * - * the font field accepts some fontconfig pattern such as "sans-serif:size=10". + * the font field is a font file ("/a/font.ttf:pixelsize=12"), or a + * fontconfig pattern such as "sans-serif:size=10" where wld has fontconfig. * if font is NULL, a default font is used. */ struct swc_decor_text { @@ -757,6 +800,14 @@ swc_set_keyboard_layout(const char *layout, const char *variant, const char *options); /** + * Change the keyboard to a whole keymap, XKB's text format, for a layout + * that is not named in the system's rules. False, the old keymap kept, when + * the text makes none. + */ +bool +swc_set_keyboard_keymap(const char *keymap); + +/** * Change the repeat rate (characters per second) and delay (ms), which * clients are told again. */ diff --git a/libswc/touch.c b/libswc/touch.c @@ -0,0 +1,389 @@ +/* swc: libswc/touch.c + * + * a finger on a surface whose client has bound wl_touch is that client's, + * from down to up, wherever it moves. a finger anywhere else (a client with + * no touch, a window's decoration, the empty screen) works the pointer + * instead, one finger at a time: a tap is a click of the left button, a + * finger that moves drags with it, and one held still is a click of the + * right button + */ + +#include "touch.h" +#include "compositor.h" +#include "internal.h" +#include "layer_shell.h" +#include "pointer.h" +#include "surface.h" +#include "util.h" +#include "window.h" + +#include <stdlib.h> + +#if defined(__linux__) +#include <linux/input.h> +#elif defined(__FreeBSD__) +#include <dev/evdev/input.h> /* mirrors linux/input.h functionality */ +#endif + +/* fingers followed at once: more are ignored */ +#define POINTS 16 +/* how far a finger moves before it drags, and how long it is held still + * before it is a right click, as touch systems commonly have them */ +#define SLOP_PX 8 +#define HOLD_MS 500 + +struct point { + int32_t slot; + /* NULL when the slot is free or the surface went away */ + struct compositor_view *view; + struct wl_client *client; + struct wl_listener view_destroy; + bool used; +}; + +enum emulation { + NOT_EMULATING, + WAITING, /* down, nothing pressed yet */ + DRAGGING, /* the left button held */ + SPENT, /* the hold has clicked: only the lift is left */ +}; + +struct touch { + struct pointer *pointer; + struct wl_list resources; + struct point points[POINTS]; + + enum emulation emulation; + int32_t slot; + wl_fixed_t x, y; + uint32_t time; + struct wl_event_source *hold; +}; + +static void +click(struct touch *touch, uint32_t time, uint32_t button) +{ + pointer_handle_button(touch->pointer, time, button, + WL_POINTER_BUTTON_STATE_PRESSED); + pointer_handle_button(touch->pointer, time, button, + WL_POINTER_BUTTON_STATE_RELEASED); +} + +static int +handle_hold(void *data) +{ + struct touch *touch = data; + + if (touch->emulation == WAITING) { + click(touch, touch->time + HOLD_MS, BTN_RIGHT); + pointer_handle_frame(touch->pointer); + touch->emulation = SPENT; + } + return 0; +} + +static void +point_release(struct point *point) +{ + if (point->view) { + wl_list_remove(&point->view_destroy.link); + point->view = NULL; + } + point->used = false; +} + +static void +handle_view_destroy(struct wl_listener *listener, void *data) +{ + struct point *point = wl_container_of(listener, point, view_destroy); + + (void)data; + wl_list_remove(&point->view_destroy.link); + point->view = NULL; +} + +static struct point * +find_point(struct touch *touch, int32_t slot) +{ + size_t i; + + for (i = 0; i < POINTS; ++i) { + if (touch->points[i].used && touch->points[i].slot == slot) { + return &touch->points[i]; + } + } + return NULL; +} + +static bool +takes_touch(struct touch *touch, struct wl_client *client) +{ + struct wl_resource *resource; + + wl_resource_for_each(resource, &touch->resources) + { + if (wl_resource_get_client(resource) == client) { + return true; + } + } + return false; +} + +/* the position on the surface of a point of the global space */ +static void +on_surface(struct compositor_view *view, wl_fixed_t x, wl_fixed_t y, + wl_fixed_t *sx, wl_fixed_t *sy) +{ + *sx = x - wl_fixed_from_int(view->base.geometry.x - view->buffer_offset_x); + *sy = y - wl_fixed_from_int(view->base.geometry.y - view->buffer_offset_y); +} + +static void +emulate_down(struct touch *touch, uint32_t time, int32_t slot, wl_fixed_t x, + wl_fixed_t y) +{ + if (touch->emulation != NOT_EMULATING) { + return; + } + touch->emulation = WAITING; + touch->slot = slot; + touch->x = x; + touch->y = y; + touch->time = time; + pointer_handle_absolute_motion(touch->pointer, time, x, y); + pointer_handle_frame(touch->pointer); + wl_event_source_timer_update(touch->hold, HOLD_MS); +} + +void +touch_handle_down(struct touch *touch, uint32_t time, int32_t slot, + wl_fixed_t x, wl_fixed_t y) +{ + struct compositor_view *view; + struct wl_client *client; + struct wl_resource *resource; + struct point *point = NULL; + wl_fixed_t sx, sy; + uint32_t serial; + size_t i; + + view = compositor_content_view_at(wl_fixed_to_int(x), wl_fixed_to_int(y)); + client = view ? wl_resource_get_client(view->surface->resource) : NULL; + if (!client || !takes_touch(touch, client)) { + emulate_down(touch, time, slot, x, y); + return; + } + + for (i = 0; !point && i < POINTS; ++i) { + if (!touch->points[i].used) { + point = &touch->points[i]; + } + } + if (!point) { + return; + } + point->used = true; + point->slot = slot; + point->view = view; + point->client = client; + point->view_destroy.notify = handle_view_destroy; + wl_signal_add(&view->destroy_signal, &point->view_destroy); + + /* a touch focuses and raises as a press does */ + layer_shell_pressed(view); + raise_window(view); + if (view->window) { + window_pointer_entered(view->window); + } + + on_surface(view, x, y, &sx, &sy); + serial = wl_display_next_serial(swc.display); + wl_resource_for_each(resource, &touch->resources) + { + if (wl_resource_get_client(resource) == client) { + wl_touch_send_down(resource, serial, time, view->surface->resource, + slot, sx, sy); + } + } +} + +void +touch_handle_motion(struct touch *touch, uint32_t time, int32_t slot, + wl_fixed_t x, wl_fixed_t y) +{ + struct point *point = find_point(touch, slot); + struct wl_resource *resource; + wl_fixed_t sx, sy, dx, dy; + + if (point) { + if (!point->view) { + return; + } + on_surface(point->view, x, y, &sx, &sy); + wl_resource_for_each(resource, &touch->resources) + { + if (wl_resource_get_client(resource) == point->client) { + wl_touch_send_motion(resource, time, slot, sx, sy); + } + } + return; + } + + if (touch->emulation == NOT_EMULATING || touch->slot != slot) { + return; + } + if (touch->emulation == WAITING) { + dx = x - touch->x; + dy = y - touch->y; + if (abs(wl_fixed_to_int(dx)) < SLOP_PX && + abs(wl_fixed_to_int(dy)) < SLOP_PX) { + return; + } + /* pressed where the finger went down, then dragged */ + wl_event_source_timer_update(touch->hold, 0); + pointer_handle_button(touch->pointer, time, BTN_LEFT, + WL_POINTER_BUTTON_STATE_PRESSED); + touch->emulation = DRAGGING; + } + if (touch->emulation == DRAGGING) { + pointer_handle_absolute_motion(touch->pointer, time, x, y); + } +} + +void +touch_handle_up(struct touch *touch, uint32_t time, int32_t slot) +{ + struct point *point = find_point(touch, slot); + struct wl_resource *resource; + uint32_t serial; + + if (point) { + serial = wl_display_next_serial(swc.display); + wl_resource_for_each(resource, &touch->resources) + { + if (wl_resource_get_client(resource) == point->client) { + wl_touch_send_up(resource, serial, time, slot); + /* an up may come with no frame after it */ + wl_touch_send_frame(resource); + } + } + point_release(point); + return; + } + + if (touch->emulation == NOT_EMULATING || touch->slot != slot) { + return; + } + wl_event_source_timer_update(touch->hold, 0); + if (touch->emulation == WAITING) { + click(touch, time, BTN_LEFT); + } else if (touch->emulation == DRAGGING) { + pointer_handle_button(touch->pointer, time, BTN_LEFT, + WL_POINTER_BUTTON_STATE_RELEASED); + } + touch->emulation = NOT_EMULATING; + pointer_handle_frame(touch->pointer); +} + +void +touch_handle_frame(struct touch *touch) +{ + struct wl_resource *resource; + size_t i; + + wl_resource_for_each(resource, &touch->resources) + { + for (i = 0; i < POINTS; ++i) { + if (touch->points[i].used && + touch->points[i].client == wl_resource_get_client(resource)) { + wl_touch_send_frame(resource); + break; + } + } + } + if (touch->emulation != NOT_EMULATING) { + pointer_handle_frame(touch->pointer); + } +} + +void +touch_handle_cancel(struct touch *touch) +{ + struct wl_resource *resource; + size_t i; + + wl_resource_for_each(resource, &touch->resources) + { + for (i = 0; i < POINTS; ++i) { + if (touch->points[i].used && + touch->points[i].client == wl_resource_get_client(resource)) { + wl_touch_send_cancel(resource); + break; + } + } + } + for (i = 0; i < POINTS; ++i) { + if (touch->points[i].used) { + point_release(&touch->points[i]); + } + } + if (touch->emulation == DRAGGING) { + pointer_handle_button(touch->pointer, touch->time, BTN_LEFT, + WL_POINTER_BUTTON_STATE_RELEASED); + pointer_handle_frame(touch->pointer); + } + wl_event_source_timer_update(touch->hold, 0); + touch->emulation = NOT_EMULATING; +} + +static const struct wl_touch_interface touch_impl = { + .release = destroy_resource, +}; + +bool +touch_bind(struct touch *touch, struct wl_client *client, uint32_t version, + uint32_t id) +{ + struct wl_resource *resource; + + resource = wl_resource_create(client, &wl_touch_interface, version, id); + if (!resource) { + return false; + } + wl_resource_set_implementation(resource, &touch_impl, touch, + &remove_resource); + wl_list_insert(&touch->resources, wl_resource_get_link(resource)); + return true; +} + +struct touch * +touch_create(struct pointer *pointer) +{ + struct touch *touch = calloc(1, sizeof(*touch)); + + if (!touch) { + return NULL; + } + touch->pointer = pointer; + wl_list_init(&touch->resources); + touch->hold = wl_event_loop_add_timer(swc.event_loop, &handle_hold, touch); + if (!touch->hold) { + free(touch); + return NULL; + } + return touch; +} + +void +touch_destroy(struct touch *touch) +{ + size_t i; + + for (i = 0; i < POINTS; ++i) { + if (touch->points[i].used) { + point_release(&touch->points[i]); + } + } + wl_event_source_remove(touch->hold); + free(touch); +} diff --git a/libswc/touch.h b/libswc/touch.h @@ -0,0 +1,41 @@ +/* swc: libswc/touch.h + * + * touchscreens: wl_touch for the clients that bind it, and the first finger + * as the pointer for everything else, the window decorations included + */ + +#ifndef SWC_TOUCH_H +#define SWC_TOUCH_H + +#include <stdbool.h> +#include <stdint.h> +#include <wayland-server.h> + +struct pointer; +struct touch; + +struct touch * +touch_create(struct pointer *pointer); +void +touch_destroy(struct touch *touch); + +bool +touch_bind(struct touch *touch, struct wl_client *client, uint32_t version, + uint32_t id); + +/* slot tells fingers apart while they are down, x and y are in the global + * space. a frame ends what happened at one time */ +void +touch_handle_down(struct touch *touch, uint32_t time, int32_t slot, + wl_fixed_t x, wl_fixed_t y); +void +touch_handle_motion(struct touch *touch, uint32_t time, int32_t slot, + wl_fixed_t x, wl_fixed_t y); +void +touch_handle_up(struct touch *touch, uint32_t time, int32_t slot); +void +touch_handle_frame(struct touch *touch); +void +touch_handle_cancel(struct touch *touch); + +#endif diff --git a/libswc/window.c b/libswc/window.c @@ -26,6 +26,7 @@ #include "event.h" #include "internal.h" #include "keyboard.h" +#include "layer_shell.h" #include "seat.h" #include "surface.h" #include "swc.h" @@ -242,7 +243,7 @@ swc_window_focus(struct swc_window *base) struct compositor_view *new = window ? window->view : NULL, *old = swc.seat->keyboard->focus.view; - if (new == old) { + if (new == old || layer_shell_hold_keys(new)) { return; } diff --git a/libswc/xdg_decoration.c b/libswc/xdg_decoration.c @@ -23,23 +23,41 @@ #include "xdg_decoration.h" #include "util.h" +#include "xdg_shell.h" #include "xdg-decoration-unstable-v1-server-protocol.h" #include <wayland-server.h> struct xdg_toplevel_decoration { struct wl_resource *resource; + struct wl_resource *toplevel; struct wl_listener toplevel_destroy_listener; }; +/* The mode is always the server's. A mode asked for is answered with it and + * then a configure of the toplevel, as the protocol has it: a client may + * wait for that configure to know its decoration settled before it maps. */ +static void +answer_mode(struct wl_resource *resource) +{ + struct xdg_toplevel_decoration *decoration = + wl_resource_get_user_data(resource); + + zxdg_toplevel_decoration_v1_send_configure( + resource, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); + xdg_toplevel_send_state(decoration->toplevel); +} + static void set_mode(struct wl_client *client, struct wl_resource *resource, uint32_t mode) { + answer_mode(resource); } static void unset_mode(struct wl_client *client, struct wl_resource *resource) { + answer_mode(resource); } static const struct zxdg_toplevel_decoration_v1_interface decoration_impl = { @@ -83,6 +101,7 @@ get_toplevel_decoration(struct wl_client *client, struct wl_resource *resource, if (!decoration->resource) { goto error1; } + decoration->toplevel = toplevel_resource; decoration->toplevel_destroy_listener.notify = &handle_toplevel_destroy; wl_resource_add_destroy_listener(toplevel_resource, &decoration->toplevel_destroy_listener); diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c @@ -364,6 +364,12 @@ send_state(struct xdg_toplevel *toplevel) send_configure(toplevel, geometry->width, geometry->height); } +void +xdg_toplevel_send_state(struct wl_resource *resource) +{ + send_state(wl_resource_get_user_data(resource)); +} + static void configure(struct window *window, uint32_t width, uint32_t height) { diff --git a/libswc/xdg_shell.h b/libswc/xdg_shell.h @@ -47,4 +47,9 @@ xdg_popups_focus_changed(struct wl_client *client); struct window * xdg_toplevel_window(struct wl_resource *resource); +/* the toplevel's configure sent again as it stands: what follows a change + * the client asked for in an extension, such as its decoration mode */ +void +xdg_toplevel_send_state(struct wl_resource *resource); + #endif diff --git a/meson.build b/meson.build @@ -144,6 +144,17 @@ if have_nested requires_private += wayland_client endif +# the peer backend feeds the libinput seat, touch and all, and shares its +# screen through a memfd +have_peer = (not get_option('peer').disabled() and input_backend == 'libinput' + and cc.has_function('memfd_create', prefix: '#include <sys/mman.h>', args: '-D_GNU_SOURCE')) +if get_option('peer').enabled() and not have_peer + error('The peer backend needs the libinput input backend and memfd_create') +endif +if have_peer + add_project_arguments('-DENABLE_PEER=1', language: 'c') +endif + subdir('protocol') subdir('cursor') subdir('launch') diff --git a/meson.options b/meson.options @@ -6,5 +6,6 @@ option('fbdev-device', type: 'string', value: '/dev/fb0', description: 'Linux fr option('wsdisplay-device', type: 'string', value: '', description: 'wsdisplay framebuffer device (empty uses the VT selected by swc-launch)') option('udev', type: 'feature', value: 'auto', description: 'libinput libudev support') option('nested', type: 'feature', value: 'auto', description: 'Backend that runs swc inside another Wayland compositor') +option('peer', type: 'feature', value: 'auto', description: 'Backend whose screen another program shows, over a socket') option('extra', type: 'boolean', value: true, description: 'Build swc utility applications') option('example', type: 'boolean', value: false, description: 'Build swc example compositor') diff --git a/patches/compositor.patch b/patches/compositor.patch @@ -1,4 +1,4 @@ -Subject: backends, z order, backdrops +Subject: backends, z order, backdrops, a keymap given as text, decor fonts from files diff --git a/libswc/backend.h b/libswc/backend.h index 65432bc..8d0e7bf 100644 @@ -978,10 +978,51 @@ index 678c362..2442a8c 100644 #endif diff --git a/libswc/decor.c b/libswc/decor.c -index 5e6eca8..c402933 100644 +index 5e6eca8..8a7f481 100644 --- a/libswc/decor.c +++ b/libswc/decor.c -@@ -731,7 +731,14 @@ decor_view_set(struct compositor_view *view, const struct swc_decor *decor) +@@ -9,10 +9,40 @@ + #include "window.h" + + #include <pixman.h> ++#include <stdio.h> + #include <stdlib.h> + #include <string.h> + #include <wld/wld.h> + ++/* A decor font: a file when the name is a path ("/a/font.ttf:pixelsize=12", ++ * 12 pixels when no size is given), else a fontconfig pattern, which only a ++ * wld built with fontconfig can find. */ ++static struct wld_font * ++open_font(struct wld_font_context *context, const char *name) ++{ ++ char path[4096]; ++ const char *size; ++ double px = 12; ++ size_t len; ++ ++ if (name[0] != '/') { ++#ifdef WLD_FONTCONFIG ++ return wld_font_open_name(context, name); ++#else ++ return NULL; ++#endif ++ } ++ size = strstr(name, ":pixelsize="); ++ len = size ? (size_t)(size - name) : strlen(name); ++ if (len >= sizeof path) ++ return NULL; ++ memcpy(path, name, len); ++ path[len] = '\0'; ++ if (size) ++ px = strtod(size + strlen(":pixelsize="), NULL); ++ return wld_font_open_file(context, path, 0, px > 0 ? px : 12); ++} ++ + #define DEFAULT_DECOR_FONT "sans-serif:size=10" + + static struct wld_font_context *font_context; +@@ -731,10 +761,17 @@ decor_view_set(struct compositor_view *view, const struct swc_decor *decor) owned_string = strdup(text.string); } @@ -996,7 +1037,11 @@ index 5e6eca8..c402933 100644 + } else if (font_name) { owned_font_name = strdup(font_name); if (owned_font_name && font_context) { - font = wld_font_open_name(font_context, font_name); +- font = wld_font_open_name(font_context, font_name); ++ font = open_font(font_context, font_name); + } + } + diff --git a/libswc/fb.c b/libswc/fb.c index d3c332c..0755d94 100644 --- a/libswc/fb.c @@ -1064,7 +1109,7 @@ index d9602f4..4144a08 100644 if (send_fd(launch.socket, out_fd, request_iov, 1 + (size > 0)) == -1) { diff --git a/libswc/meson.build b/libswc/meson.build -index ba29ddc..e1ef27b 100644 +index 15fa57a..03b195a 100644 --- a/libswc/meson.build +++ b/libswc/meson.build @@ -3,6 +3,7 @@ install_headers('swc.h') @@ -1091,7 +1136,7 @@ index ba29ddc..e1ef27b 100644 'xdg_output.c', 'xdg_shell.c', -@@ -42,12 +45,16 @@ libswc_src = files( +@@ -43,12 +46,16 @@ libswc_src = files( xwayland.found() ? ['xserver.c', 'xwm.c'] : [], @@ -2806,7 +2851,7 @@ index d53b566..ee896ba 100644 screen_destroy(struct screen *screen); diff --git a/libswc/swc.c b/libswc/swc.c -index e48cd8d..ee131d4 100644 +index e48cd8d..01d679e 100644 --- a/libswc/swc.c +++ b/libswc/swc.c @@ -27,7 +27,8 @@ @@ -3042,7 +3087,7 @@ index e48cd8d..ee131d4 100644 error0: return false; } -@@ -361,10 +493,21 @@ swc_finalize(void) +@@ -361,10 +493,36 @@ swc_finalize(void) screens_finalize(); bindings_finalize(); shm_destroy(swc.shm); @@ -3065,13 +3110,28 @@ index e48cd8d..ee131d4 100644 + return keyboard_set_names(swc.seat->keyboard, &names); +} + ++EXPORT bool ++swc_set_keyboard_keymap(const char *keymap) ++{ ++ struct xkb_keymap *map; ++ bool ok; ++ ++ if (!(map = xkb_keymap_new_from_string(swc.seat->keyboard->xkb.context, ++ keymap, XKB_KEYMAP_FORMAT_TEXT_V1, ++ 0))) ++ return false; ++ ok = keyboard_set_keymap(swc.seat->keyboard, map); ++ xkb_keymap_unref(map); ++ return ok; ++} ++ +EXPORT void +swc_set_keyboard_repeat(int32_t rate, int32_t delay) +{ + keyboard_set_repeat(swc.seat->keyboard, rate, delay); } diff --git a/libswc/swc.h b/libswc/swc.h -index fc0fa9a..b6b0c4c 100644 +index fc0fa9a..8ab36a1 100644 --- a/libswc/swc.h +++ b/libswc/swc.h @@ -58,6 +58,25 @@ swc_cursor_position(int32_t *x, int32_t *y); @@ -3303,7 +3363,17 @@ index fc0fa9a..b6b0c4c 100644 /*window decor things*/ /** -@@ -567,6 +747,22 @@ swc_window_stack(struct swc_window *window, int32_t direction); +@@ -437,7 +617,8 @@ enum swc_decor_align { + * for left and right edges, text is drawn as stacked glyphs, one glyph + * per row. + * +- * the font field accepts some fontconfig pattern such as "sans-serif:size=10". ++ * the font field is a font file ("/a/font.ttf:pixelsize=12"), or a ++ * fontconfig pattern such as "sans-serif:size=10" where wld has fontconfig. + * if font is NULL, a default font is used. + */ + struct swc_decor_text { +@@ -567,6 +748,30 @@ swc_window_stack(struct swc_window *window, int32_t direction); */ extern int32_t swc_repeat_rate, swc_repeat_delay; @@ -3317,6 +3387,14 @@ index fc0fa9a..b6b0c4c 100644 + const char *options); + +/** ++ * Change the keyboard to a whole keymap, XKB's text format, for a layout ++ * that is not named in the system's rules. False, the old keymap kept, when ++ * the text makes none. ++ */ ++bool ++swc_set_keyboard_keymap(const char *keymap); ++ ++/** + * Change the repeat rate (characters per second) and delay (ms), which + * clients are told again. + */ @@ -3326,7 +3404,7 @@ index fc0fa9a..b6b0c4c 100644 /* Bindings {{{ */ enum { -@@ -577,6 +773,22 @@ enum { +@@ -577,6 +782,22 @@ enum { SWC_MOD_ANY = ~0 }; @@ -3350,7 +3428,7 @@ index fc0fa9a..b6b0c4c 100644 SWC_BINDING_KEY, SWC_BINDING_BUTTON, diff --git a/meson.build b/meson.build -index b37d6fa..73f589e 100644 +index 356dcbc..79dee81 100644 --- a/meson.build +++ b/meson.build @@ -39,15 +39,20 @@ requires_private = [ @@ -3395,7 +3473,7 @@ index b37d6fa..73f589e 100644 endif xwayland = video_backend == 'drm' \ ? dependency('xwayland', required: get_option('xwayland')) \ -@@ -121,6 +127,20 @@ if input_backend == 'libinput' +@@ -124,6 +130,20 @@ if input_backend == 'libinput' requires_private += [libinput, udev] endif @@ -3416,7 +3494,7 @@ index b37d6fa..73f589e 100644 subdir('protocol') subdir('cursor') subdir('launch') -@@ -136,7 +156,8 @@ endif +@@ -139,7 +159,8 @@ endif summary({ 'Xwayland support': xwayland.found(), diff --git a/patches/cross.patch b/patches/cross.patch @@ -0,0 +1,25 @@ +cursor: build convert_cont for the build machine + +it runs during the build to make cursor_data.h, so a cross build has to +make it for the machine it runs on, not the target. the project's +arguments are the target's, so the tool asks for strdup itself. + +--- a/cursor/meson.build ++++ b/cursor/meson.build +@@ -1,4 +1,4 @@ +-tool = executable('convert_cont', 'convert_font.c') ++tool = executable('convert_cont', 'convert_font.c', native: true) + + cursor = custom_target('cursor_data_h', + input: 'cursor.pcf', +--- a/cursor/convert_font.c ++++ b/cursor/convert_font.c +@@ -26,6 +26,8 @@ + * http://fontforge.org/pcf-format.html + */ + ++#define _POSIX_C_SOURCE 200809L /* strdup, as a tool of the build machine has no project flags */ ++ + #include <assert.h> + #include <fcntl.h> + #include <stdint.h> diff --git a/patches/damage.patch b/patches/damage.patch @@ -0,0 +1,413 @@ +diff --git a/libswc/compositor.c b/libswc/compositor.c +index 7056ed6..987d9a7 100644 +--- a/libswc/compositor.c ++++ b/libswc/compositor.c +@@ -118,6 +118,8 @@ static struct pointer_handler pointer_handler = { + static struct { + struct wl_list views; + pixman_region32_t damage, opaque; ++ /* what the frame being presented changed, in its screen's coordinates */ ++ pixman_region32_t frame_damage; + struct wl_listener swc_listener; + + /* A mask of screens that have been repainted but are waiting on a page +@@ -805,6 +807,23 @@ schedule_updates(uint32_t screens) + compositor.scheduled_updates |= screens; + } + ++const pixman_region32_t * ++compositor_frame_damage(void) ++{ ++ return &compositor.frame_damage; ++} ++ ++void ++compositor_damage_rect(int32_t x, int32_t y, uint32_t width, uint32_t height) ++{ ++ if (!compositor.initialized || !width || !height) { ++ return; ++ } ++ pixman_region32_union_rect(&compositor.damage, &compositor.damage, x, y, ++ width, height); ++ schedule_updates(-1); ++} ++ + void + compositor_damage_all(void) + { +@@ -1998,6 +2017,10 @@ update_screen(struct screen *screen) + pixman_region32_intersect_rect(&damage, &compositor.damage, geom->x, + geom->y, geom->width, geom->height); + pixman_region32_translate(&damage, -geom->x, -geom->y); ++ if (compositor.zoom != 1.0f) { ++ pixman_region32_union_rect(&damage, &damage, 0, 0, geom->width, geom->height); ++ } ++ pixman_region32_copy(&compositor.frame_damage, &damage); + total_damage = wld_surface_damage(target->surface, &damage); + + /* Don't repaint the screen if it is waiting for a page flip. */ +@@ -2226,6 +2249,7 @@ compositor_initialize(void) + } + pixman_region32_init(&compositor.damage); + pixman_region32_init(&compositor.opaque); ++ pixman_region32_init(&compositor.frame_damage); + wl_list_init(&compositor.views); + wl_signal_init(&swc_compositor.signal.new_surface); + compositor.swc_listener.notify = &handle_swc_event; +@@ -2261,6 +2285,7 @@ compositor_finalize(void) + } + decor_finalize(); + pixman_region32_fini(&compositor.damage); ++ pixman_region32_fini(&compositor.frame_damage); + pixman_region32_fini(&compositor.opaque); + wl_global_destroy(compositor.global); + } +diff --git a/libswc/compositor.h b/libswc/compositor.h +index 2442a8c..89d92f6 100644 +--- a/libswc/compositor.h ++++ b/libswc/compositor.h +@@ -63,6 +63,13 @@ void + compositor_finalize(void); + void + compositor_damage_all(void); ++/* adds a rectangle of the global space to what the next frame repaints */ ++void ++compositor_damage_rect(int32_t x, int32_t y, uint32_t width, uint32_t height); ++/* what the frame being presented changed, in its screen's coordinates: ++ * a backend that copies the frame copies only this */ ++const pixman_region32_t * ++compositor_frame_damage(void); + + struct compositor_view { + struct view base; +diff --git a/libswc/fb.c b/libswc/fb.c +index 0755d94..6166b83 100644 +--- a/libswc/fb.c ++++ b/libswc/fb.c +@@ -14,7 +14,7 @@ + + struct swc_fb swc_fb; + +-/* for the cursor */ ++/* for the cursor */ + static uint32_t + blend(uint32_t background, uint32_t foreground) + { +@@ -92,51 +92,92 @@ fb_create_screens(struct wl_list *screens) + return true; + } + ++/* the frame's rectangle r into the copy, the cursor blended over it */ ++static void ++copy_box(struct wld_buffer *buffer, const pixman_box32_t *r, ++ struct wld_buffer *cursor, const pixman_box32_t *c) ++{ ++ int32_t y, x, x1, x2; ++ ++ for (y = r->y1; y < r->y2; ++y) { ++ uint32_t *source = (uint32_t *)((uint8_t *)buffer->map + ++ (size_t)y * buffer->pitch); ++ uint32_t *destination = (uint32_t *)((uint8_t *)swc_fb.pixels + ++ (size_t)y * swc_fb.pitch); ++ ++ memcpy(destination + r->x1, source + r->x1, ++ (size_t)(r->x2 - r->x1) * sizeof *destination); ++ if (!cursor || y < c->y1 || y >= c->y2) { ++ continue; ++ } ++ x1 = MAX(r->x1, c->x1); ++ x2 = MIN(r->x2, c->x2); ++ for (x = x1; x < x2; ++x) { ++ uint32_t foreground = *(uint32_t *)((uint8_t *)cursor->map + ++ (size_t)(y - c->y1) * cursor->pitch + (size_t)(x - c->x1) * 4); ++ destination[x] = blend(destination[x], foreground); ++ } ++ } ++} ++ + bool +-fb_present(struct wld_buffer *buffer, int32_t origin_x, int32_t origin_y) ++fb_present(struct wld_buffer *buffer, int32_t origin_x, int32_t origin_y, ++ const pixman_region32_t *damage) + { ++ /* where the cursor was drawn last, put back from the frame this time */ ++ static pixman_box32_t drawn; ++ static bool whole_drawn; + struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; + struct wld_buffer *cursor = NULL; +- int32_t cursor_x = 0, cursor_y = 0; +- uint32_t width, height, x, y; +- bool cursor_mapped = false; ++ pixman_box32_t c = { 0, 0, 0, 0 }, *boxes; ++ pixman_region32_t region; ++ uint32_t width, height; ++ int i, n; ++ bool ok; + + if (!buffer || !wld_map(buffer)) { + return false; + } +- if (pointer && pointer->cursor.view.buffer) { ++ if (pointer && pointer->cursor.view.buffer && wld_map(pointer->cursor.buffer)) { + cursor = pointer->cursor.buffer; +- cursor_x = pointer->cursor.view.geometry.x - origin_x; +- cursor_y = pointer->cursor.view.geometry.y - origin_y; +- cursor_mapped = wld_map(cursor); ++ c.x1 = pointer->cursor.view.geometry.x - origin_x; ++ c.y1 = pointer->cursor.view.geometry.y - origin_y; ++ c.x2 = c.x1 + (int32_t)cursor->width; ++ c.y2 = c.y1 + (int32_t)cursor->height; + } + + width = buffer->width < swc_fb.width ? buffer->width : swc_fb.width; + height = buffer->height < swc_fb.height ? buffer->height : swc_fb.height; +- if (width != swc_fb.width || height != swc_fb.height) { +- memset(swc_fb.pixels, 0, swc_fb.height * swc_fb.pitch); +- } +- for (y = 0; y < height; ++y) { +- uint32_t *source = (uint32_t *)((uint8_t *)buffer->map + +- (size_t)y * buffer->pitch); +- uint32_t *destination = (uint32_t *)((uint8_t *)swc_fb.pixels + +- (size_t)y * swc_fb.pitch); +- for (x = 0; x < width; ++x) { +- uint32_t pixel = source[x]; +- int32_t cx = (int32_t)x - cursor_x; +- int32_t cy = (int32_t)y - cursor_y; +- if (cursor_mapped && cx >= 0 && cy >= 0 && +- (uint32_t)cx < cursor->width && (uint32_t)cy < cursor->height) { +- uint32_t foreground = *(uint32_t *)((uint8_t *)cursor->map + +- (size_t)cy * cursor->pitch + (size_t)cx * 4); +- pixel = blend(pixel, foreground); +- } +- destination[x] = pixel; ++ pixman_region32_init(&region); ++ /* the first frame, or one that does not fill the screen, is copied whole */ ++ if (width != swc_fb.width || height != swc_fb.height || !whole_drawn || !damage) { ++ if (width != swc_fb.width || height != swc_fb.height) { ++ memset(swc_fb.pixels, 0, swc_fb.height * swc_fb.pitch); + } ++ pixman_region32_init_rect(&region, 0, 0, swc_fb.width, swc_fb.height); ++ whole_drawn = width == swc_fb.width && height == swc_fb.height; ++ } else { ++ pixman_region32_copy(&region, damage); ++ pixman_region32_union_rect(&region, &region, drawn.x1, drawn.y1, ++ drawn.x2 - drawn.x1, drawn.y2 - drawn.y1); ++ pixman_region32_union_rect(&region, &region, c.x1, c.y1, ++ c.x2 - c.x1, c.y2 - c.y1); ++ } ++ pixman_region32_intersect_rect(&region, &region, 0, 0, width, height); ++ ++ boxes = pixman_region32_rectangles(&region, &n); ++ for (i = 0; i < n; ++i) { ++ copy_box(buffer, &boxes[i], cursor, &c); + } +- if (cursor_mapped) { ++ drawn = c; ++ if (cursor) { + wld_unmap(cursor); + } + wld_unmap(buffer); +- return framebuffer_present(&swc_fb); ++ if (width != swc_fb.width || height != swc_fb.height) { ++ pixman_region32_union_rect(&region, &region, 0, 0, swc_fb.width, swc_fb.height); ++ } ++ ok = framebuffer_present(&swc_fb, &region); ++ pixman_region32_fini(&region); ++ return ok; + } +diff --git a/libswc/fb.h b/libswc/fb.h +index 8d3273d..5e0cb14 100644 +--- a/libswc/fb.h ++++ b/libswc/fb.h +@@ -3,6 +3,7 @@ + + #include "backend.h" + ++#include <pixman.h> + #include <stdbool.h> + #include <stddef.h> + #include <stdint.h> +@@ -23,11 +24,14 @@ extern struct swc_fb swc_fb; + bool fb_initialize(void); + void fb_finalize(void); + bool fb_create_screens(struct wl_list *screens); +-bool fb_present(struct wld_buffer *buffer, int32_t x, int32_t y); ++/* damage is what the frame changed, NULL when unknown */ ++bool fb_present(struct wld_buffer *buffer, int32_t x, int32_t y, ++ const pixman_region32_t *damage); + + bool framebuffer_initialize(struct swc_fb *fb); + void framebuffer_finalize(struct swc_fb *fb); +-bool framebuffer_present(struct swc_fb *fb); ++/* the copy's pixels within region onto the screen */ ++bool framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region); + const char *framebuffer_name(void); + + #endif +diff --git a/libswc/fb_fbdev.c b/libswc/fb_fbdev.c +index abd25a0..c3fa65f 100644 +--- a/libswc/fb_fbdev.c ++++ b/libswc/fb_fbdev.c +@@ -167,18 +167,44 @@ store_pixel(uint8_t *destination, uint32_t pixel) + } + } + ++/* a pixel of the copy is the screen's own: rows go over whole */ ++static bool ++native_is_xrgb(void) ++{ ++ return fbdev.bits_per_pixel == 32 && fbdev.red.offset == 16 && ++ fbdev.red.length == 8 && fbdev.green.offset == 8 && ++ fbdev.green.length == 8 && fbdev.blue.offset == 0 && ++ fbdev.blue.length == 8; ++} ++ + bool +-framebuffer_present(struct swc_fb *fb) ++framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region) + { + uint32_t bytes = fbdev.bits_per_pixel / 8; +- uint32_t x, y; +- +- for (y = 0; y < fb->height; ++y) { +- uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + y * fb->pitch); +- uint8_t *destination = (uint8_t *)fbdev.memory + fbdev.memory_offset + +- (size_t)y * fbdev.pitch; +- for (x = 0; x < fb->width; ++x) { +- store_pixel(destination + (size_t)x * bytes, source[x]); ++ bool same = native_is_xrgb(); ++ const pixman_box32_t *boxes; ++ int32_t x, y; ++ int i, n; ++ ++ boxes = pixman_region32_rectangles((pixman_region32_t *)region, &n); ++ for (i = 0; i < n; ++i) { ++ const pixman_box32_t *b = &boxes[i]; ++ ++ for (y = b->y1; y < b->y2; ++y) { ++ uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + ++ (size_t)y * fb->pitch); ++ uint8_t *destination = (uint8_t *)fbdev.memory + ++ fbdev.memory_offset + ++ (size_t)y * fbdev.pitch; ++ ++ if (same) { ++ memcpy(destination + (size_t)b->x1 * 4, source + b->x1, ++ (size_t)(b->x2 - b->x1) * 4); ++ continue; ++ } ++ for (x = b->x1; x < b->x2; ++x) { ++ store_pixel(destination + (size_t)x * bytes, source[x]); ++ } + } + } + return true; +diff --git a/libswc/fb_wsdisplay.c b/libswc/fb_wsdisplay.c +index 03501d8..e891925 100644 +--- a/libswc/fb_wsdisplay.c ++++ b/libswc/fb_wsdisplay.c +@@ -331,20 +331,44 @@ store_pixel(uint8_t *destination, uint32_t pixel) + } + } + ++/* a pixel of the copy is the screen's own: rows go over whole */ ++static bool ++native_is_xrgb(void) ++{ ++ return wsdisplay.bits_per_pixel == 32 && wsdisplay.red.offset == 16 && ++ wsdisplay.red.length == 8 && wsdisplay.green.offset == 8 && ++ wsdisplay.green.length == 8 && wsdisplay.blue.offset == 0 && ++ wsdisplay.blue.length == 8; ++} ++ + bool +-framebuffer_present(struct swc_fb *fb) ++framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region) + { + uint32_t bytes = (wsdisplay.bits_per_pixel + 7) / 8; +- uint32_t x, y; +- +- for (y = 0; y < fb->height; ++y) { +- uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + +- (size_t)y * fb->pitch); +- uint8_t *destination = (uint8_t *)wsdisplay.memory + +- wsdisplay.memory_offset + +- (size_t)y * wsdisplay.pitch; +- for (x = 0; x < fb->width; ++x) { +- store_pixel(destination + (size_t)x * bytes, source[x]); ++ bool same = native_is_xrgb(); ++ const pixman_box32_t *boxes; ++ int32_t x, y; ++ int i, n; ++ ++ boxes = pixman_region32_rectangles((pixman_region32_t *)region, &n); ++ for (i = 0; i < n; ++i) { ++ const pixman_box32_t *b = &boxes[i]; ++ ++ for (y = b->y1; y < b->y2; ++y) { ++ uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + ++ (size_t)y * fb->pitch); ++ uint8_t *destination = (uint8_t *)wsdisplay.memory + ++ wsdisplay.memory_offset + ++ (size_t)y * wsdisplay.pitch; ++ ++ if (same) { ++ memcpy(destination + (size_t)b->x1 * 4, source + b->x1, ++ (size_t)(b->x2 - b->x1) * 4); ++ continue; ++ } ++ for (x = b->x1; x < b->x2; ++x) { ++ store_pixel(destination + (size_t)x * bytes, source[x]); ++ } + } + } + return true; +diff --git a/libswc/pointer.c b/libswc/pointer.c +index e785cbd..1c279fd 100644 +--- a/libswc/pointer.c ++++ b/libswc/pointer.c +@@ -253,6 +253,7 @@ move(struct view *view, int32_t x, int32_t y) + #ifdef ENABLE_DRM + struct screen *screen; + #endif ++ struct swc_rectangle old = view->geometry; + + if (view_set_position(view, x, y)) { + view_update_screens(view); +@@ -273,7 +274,11 @@ move(struct view *view, int32_t x, int32_t y) + return true; + } + #endif +- compositor_damage_all(); ++ /* a backend that draws the cursor into the frame repaints only where it ++ * was and where it is */ ++ compositor_damage_rect(old.x, old.y, old.width, old.height); ++ compositor_damage_rect(view->geometry.x, view->geometry.y, ++ view->geometry.width, view->geometry.height); + + return true; + } +diff --git a/libswc/primary_plane.c b/libswc/primary_plane.c +index 011cb39..cd8616c 100644 +--- a/libswc/primary_plane.c ++++ b/libswc/primary_plane.c +@@ -22,8 +22,8 @@ + */ + + #include "primary_plane.h" +-#include "event.h" + #include "compositor.h" ++#include "event.h" + #ifdef ENABLE_DRM + #include "drm.h" + #endif +@@ -95,7 +95,8 @@ attach_drm(struct primary_plane *plane, struct wld_buffer *buffer) + static int + attach_fb(struct primary_plane *plane, struct wld_buffer *buffer) + { +- if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y)) { ++ if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y, ++ compositor_frame_damage())) { + return -1; + } + wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); diff --git a/patches/decoration.patch b/patches/decoration.patch @@ -0,0 +1,87 @@ +Subject: xdg-decoration: a mode asked for is answered, then the toplevel configured + +diff --git a/libswc/xdg_decoration.c b/libswc/xdg_decoration.c +index c66f599..478ce4f 100644 +--- a/libswc/xdg_decoration.c ++++ b/libswc/xdg_decoration.c +@@ -23,23 +23,41 @@ + + #include "xdg_decoration.h" + #include "util.h" ++#include "xdg_shell.h" + + #include "xdg-decoration-unstable-v1-server-protocol.h" + #include <wayland-server.h> + + struct xdg_toplevel_decoration { + struct wl_resource *resource; ++ struct wl_resource *toplevel; + struct wl_listener toplevel_destroy_listener; + }; + ++/* The mode is always the server's. A mode asked for is answered with it and ++ * then a configure of the toplevel, as the protocol has it: a client may ++ * wait for that configure to know its decoration settled before it maps. */ ++static void ++answer_mode(struct wl_resource *resource) ++{ ++ struct xdg_toplevel_decoration *decoration = ++ wl_resource_get_user_data(resource); ++ ++ zxdg_toplevel_decoration_v1_send_configure( ++ resource, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); ++ xdg_toplevel_send_state(decoration->toplevel); ++} ++ + static void + set_mode(struct wl_client *client, struct wl_resource *resource, uint32_t mode) + { ++ answer_mode(resource); + } + + static void + unset_mode(struct wl_client *client, struct wl_resource *resource) + { ++ answer_mode(resource); + } + + static const struct zxdg_toplevel_decoration_v1_interface decoration_impl = { +@@ -83,6 +101,7 @@ get_toplevel_decoration(struct wl_client *client, struct wl_resource *resource, + if (!decoration->resource) { + goto error1; + } ++ decoration->toplevel = toplevel_resource; + decoration->toplevel_destroy_listener.notify = &handle_toplevel_destroy; + wl_resource_add_destroy_listener(toplevel_resource, + &decoration->toplevel_destroy_listener); +diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c +index 1e327ad..dd3b0ab 100644 +--- a/libswc/xdg_shell.c ++++ b/libswc/xdg_shell.c +@@ -364,6 +364,12 @@ send_state(struct xdg_toplevel *toplevel) + send_configure(toplevel, geometry->width, geometry->height); + } + ++void ++xdg_toplevel_send_state(struct wl_resource *resource) ++{ ++ send_state(wl_resource_get_user_data(resource)); ++} ++ + static void + configure(struct window *window, uint32_t width, uint32_t height) + { +diff --git a/libswc/xdg_shell.h b/libswc/xdg_shell.h +index de6997b..be00803 100644 +--- a/libswc/xdg_shell.h ++++ b/libswc/xdg_shell.h +@@ -47,4 +47,9 @@ xdg_popups_focus_changed(struct wl_client *client); + struct window * + xdg_toplevel_window(struct wl_resource *resource); + ++/* the toplevel's configure sent again as it stands: what follows a change ++ * the client asked for in an extension, such as its decoration mode */ ++void ++xdg_toplevel_send_state(struct wl_resource *resource); ++ + #endif diff --git a/patches/input.patch b/patches/input.patch @@ -1,4 +1,4 @@ -Subject: input, cursors, drag and drop +Subject: input, cursors, drag and drop, a keymap from a file diff --git a/libswc/cursor_shape.c b/libswc/cursor_shape.c new file mode 100644 @@ -994,7 +994,7 @@ index 6712190..f7242cc 100644 data_device_offer_selection(struct data_device *data_device, struct wl_client *client); diff --git a/libswc/keyboard.c b/libswc/keyboard.c -index af9a286..3e7e531 100644 +index af9a286..ffa0629 100644 --- a/libswc/keyboard.c +++ b/libswc/keyboard.c @@ -29,6 +29,7 @@ @@ -1005,7 +1005,51 @@ index af9a286..3e7e531 100644 #include "swc.h" #include "util.h" -@@ -304,6 +305,68 @@ keyboard_destroy(struct keyboard *keyboard) +@@ -36,6 +37,7 @@ + #include <fcntl.h> + #include <limits.h> + #include <stdio.h> ++#include <stdlib.h> + #include <string.h> + #include <sys/mman.h> + #include <unistd.h> +@@ -228,6 +230,23 @@ error0: + return false; + } + ++/* The keymap in the file SWC_KEYMAP_FILE names, NULL when there is none. */ ++static struct xkb_keymap * ++keymap_from_file(struct xkb_context *context) ++{ ++ const char *path = getenv("SWC_KEYMAP_FILE"); ++ struct xkb_keymap *map; ++ FILE *file; ++ ++ if (!path || !*path || !(file = fopen(path, "r"))) ++ return NULL; ++ map = xkb_keymap_new_from_file(context, file, XKB_KEYMAP_FORMAT_TEXT_V1, 0); ++ fclose(file); ++ if (!map) ++ WARNING("Could not compile the keymap in %s\n", path); ++ return map; ++} ++ + struct keyboard * + keyboard_create(struct xkb_rule_names *names) + { +@@ -245,7 +264,10 @@ keyboard_create(struct xkb_rule_names *names) + goto error1; + } + +- if (!(xkb->keymap.map = ++ /* a whole keymap from a file, when the session names one, needs no xkb ++ * data of the system's; names need the rules and files they name */ ++ if (!(xkb->keymap.map = keymap_from_file(xkb->context)) && ++ !(xkb->keymap.map = + xkb_keymap_new_from_names(xkb->context, names, 0))) { + ERROR("Could not create XKB keymap\n"); + goto error2; +@@ -304,6 +326,68 @@ keyboard_destroy(struct keyboard *keyboard) free(keyboard); } @@ -1074,7 +1118,7 @@ index af9a286..3e7e531 100644 bool keyboard_reset(struct keyboard *keyboard) { -@@ -328,8 +391,6 @@ keyboard_reset(struct keyboard *keyboard) +@@ -328,8 +412,6 @@ keyboard_reset(struct keyboard *keyboard) * handler. */ assert(keyboard->client_keys.size == 0); keyboard->keys.size = 0; @@ -1083,7 +1127,7 @@ index af9a286..3e7e531 100644 if (!(state = xkb_state_new(keyboard->xkb.keymap.map))) { ERROR("Failed to allocate new XKB state\n"); -@@ -338,6 +399,8 @@ keyboard_reset(struct keyboard *keyboard) +@@ -338,6 +420,8 @@ keyboard_reset(struct keyboard *keyboard) xkb_state_unref(keyboard->xkb.state); keyboard->xkb.state = state; @@ -1092,7 +1136,7 @@ index af9a286..3e7e531 100644 return true; } -@@ -348,6 +411,8 @@ keyboard_reset(struct keyboard *keyboard) +@@ -348,6 +432,8 @@ keyboard_reset(struct keyboard *keyboard) void keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view) { @@ -1101,7 +1145,7 @@ index af9a286..3e7e531 100644 input_focus_set(&keyboard->focus, view); } -@@ -393,6 +458,56 @@ keyboard_bind(struct keyboard *keyboard, +@@ -393,6 +479,56 @@ keyboard_bind(struct keyboard *keyboard, return client_resource; } @@ -1158,7 +1202,7 @@ index af9a286..3e7e531 100644 void keyboard_handle_key(struct keyboard *keyboard, uint32_t time, -@@ -400,7 +515,6 @@ keyboard_handle_key(struct keyboard *keyboard, +@@ -400,7 +536,6 @@ keyboard_handle_key(struct keyboard *keyboard, uint32_t state) { struct key *key; @@ -1166,7 +1210,7 @@ index af9a286..3e7e531 100644 enum xkb_key_direction direction; struct xkb *xkb = &keyboard->xkb; struct keyboard_handler *handler; -@@ -455,44 +569,47 @@ update_xkb_state: +@@ -455,44 +590,47 @@ update_xkb_state: state == WL_KEYBOARD_KEY_STATE_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP; xkb_state_update_key(xkb->state, XKB_KEY(value), direction); diff --git a/patches/keys.patch b/patches/keys.patch @@ -0,0 +1,142 @@ +diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c +index f167c8f..ffbed24 100644 +--- a/libswc/layer_shell.c ++++ b/libswc/layer_shell.c +@@ -387,6 +387,73 @@ static const struct zwlr_layer_surface_v1_interface layer_surface_impl = { + .set_exclusive_edge = set_exclusive_edge, + }; + ++/* an exclusive surface has every key while it is mapped; the view a ++ * window asked for meanwhile gets them when it goes */ ++static struct { ++ struct layer_surface *holder; ++ struct compositor_view *wanted; ++ struct wl_listener wanted_destroy; ++} keys; ++ ++static void ++handle_wanted_destroy(struct wl_listener *listener, void *data) ++{ ++ (void)data; ++ wl_list_remove(&listener->link); ++ keys.wanted = NULL; ++} ++ ++static void ++want_keys(struct compositor_view *view) ++{ ++ if (keys.wanted) { ++ wl_list_remove(&keys.wanted_destroy.link); ++ } ++ keys.wanted = view; ++ if (view) { ++ keys.wanted_destroy.notify = handle_wanted_destroy; ++ wl_signal_add(&view->destroy_signal, &keys.wanted_destroy); ++ } ++} ++ ++static void ++hold_keys(struct layer_surface *surface) ++{ ++ struct compositor_view *focus = swc.seat->keyboard->focus.view; ++ ++ /* a second exclusive surface over the first gives back what the first held */ ++ if (!keys.holder) { ++ want_keys(focus); ++ } ++ keys.holder = surface; ++ keyboard_set_focus(swc.seat->keyboard, surface->view); ++} ++ ++static void ++release_keys(struct layer_surface *surface) ++{ ++ struct compositor_view *view = keys.wanted; ++ ++ if (keys.holder != surface) { ++ return; ++ } ++ keys.holder = NULL; ++ want_keys(NULL); ++ if (swc.seat->keyboard->focus.view == surface->view) { ++ keyboard_set_focus(swc.seat->keyboard, view); ++ } ++} ++ ++bool ++layer_shell_hold_keys(struct compositor_view *view) ++{ ++ if (!keys.holder) { ++ return false; ++ } ++ want_keys(view); ++ return true; ++} ++ + static void + handle_attach(struct view_handler *handler) + { +@@ -402,10 +469,11 @@ handle_attach(struct view_handler *handler) + /* on demand waits for a press, see layer_shell_pressed */ + if (surface->current.keyboard_interactivity == + ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE) { +- keyboard_set_focus(swc.seat->keyboard, surface->view); ++ hold_keys(surface); + } + } + } else if (surface->mapped) { ++ release_keys(surface); + compositor_view_hide(surface->view); + } + +@@ -535,6 +603,7 @@ destroy_layer_surface(struct wl_resource *resource) + wl_list_remove(&surface->surface_destroy_listener.link); + wl_list_remove(&surface->surface_commit_listener.link); + wl_list_remove(&surface->modifier.link); ++ release_keys(surface); + compositor_view_destroy(surface->view); + if (had_screen) { + screen_update_usable_geometry(surface->screen); +diff --git a/libswc/layer_shell.h b/libswc/layer_shell.h +index 138db1a..5f3f53d 100644 +--- a/libswc/layer_shell.h ++++ b/libswc/layer_shell.h +@@ -1,6 +1,8 @@ + #ifndef SWC_LAYER_SHELL_H + #define SWC_LAYER_SHELL_H + ++#include <stdbool.h> ++ + struct wl_display; + struct wl_global; + +@@ -18,4 +20,8 @@ struct compositor_view; + void + layer_shell_pressed(struct compositor_view *view); + ++/* true while an exclusive surface has the keys: view gets them after it */ ++bool ++layer_shell_hold_keys(struct compositor_view *view); ++ + #endif +diff --git a/libswc/window.c b/libswc/window.c +index 0efd250..2e1cfa8 100644 +--- a/libswc/window.c ++++ b/libswc/window.c +@@ -26,6 +26,7 @@ + #include "event.h" + #include "internal.h" + #include "keyboard.h" ++#include "layer_shell.h" + #include "seat.h" + #include "surface.h" + #include "swc.h" +@@ -242,7 +243,7 @@ swc_window_focus(struct swc_window *base) + struct compositor_view *new = window ? window->view : NULL, + *old = swc.seat->keyboard->focus.view; + +- if (new == old) { ++ if (new == old || layer_shell_hold_keys(new)) { + return; + } + diff --git a/patches/kms.patch b/patches/kms.patch @@ -0,0 +1,95 @@ +drm: pick a card that drives a display, and no render node is fine + +a phone often has its GPU on a render only card beside the display +controller. with no boot_vga to go by, the first card was taken, the GPU. +the card is now one with connectors in sysfs where any card has them, and a +display controller without a render node is used, without wl_drm. + +--- a/libswc/drm.c ++++ b/libswc/drm.c +@@ -134,6 +134,27 @@ + return sscanf(entry->d_name, "card%u", &num) == 1; + } + ++/* whether the card drives a display, by the connectors sysfs lists under it ++ * (card0-DSI-1). a render only card, as many a phone's GPU is beside its ++ * display controller, has none */ ++static bool ++has_connectors(const char *card) ++{ ++ struct dirent *entry; ++ size_t len = strlen(card); ++ bool found = false; ++ DIR *dir; ++ ++ if (!(dir = opendir("/sys/class/drm"))) { ++ return false; ++ } ++ while (!found && (entry = readdir(dir))) { ++ found = !strncmp(entry->d_name, card, len) && entry->d_name[len] == '-'; ++ } ++ closedir(dir); ++ return found; ++} ++ + static bool + find_primary_drm_device(char *path, size_t size) + { +@@ -142,6 +163,7 @@ + unsigned index; + FILE *file; + unsigned char boot_vga; ++ bool any_connected = false; + + num_cards = scandir("/dev/dri", &cards, &select_card, &alphasort); + +@@ -150,6 +172,16 @@ + } + + for (index = 0; index < num_cards; ++index) { ++ any_connected = any_connected || has_connectors(cards[index]->d_name); ++ } ++ ++ for (index = 0; index < num_cards; ++index) { ++ /* where sysfs says which cards have outputs, only those do */ ++ if (any_connected && !has_connectors(cards[index]->d_name)) { ++ free(cards[index]); ++ continue; ++ } ++ + snprintf(path, size, "/sys/class/drm/%s/device/boot_vga", + cards[index]->d_name); + +@@ -294,11 +326,9 @@ + swc.backend->cursor_width = swc.drm->cursor_w; + swc.backend->cursor_height = swc.drm->cursor_h; + ++ /* a display controller on its own has no render node: clients are then ++ * not offered wl_drm */ + drm.path = drmGetRenderDeviceNameFromFd(swc.drm->fd); +- if (!drm.path) { +- ERROR("Could not determine render node path\n"); +- goto error1; +- } + + if (!(swc.drm->context = wld_drm_create_context(swc.drm->fd))) { + ERROR("Could not create WLD DRM context\n"); +@@ -321,11 +351,13 @@ + } + + if (!wld_drm_is_dumb(swc.drm->context)) { +- drm.global = wl_global_create(swc.display, &wl_drm_interface, 2, NULL, +- &bind_drm); +- if (!drm.global) { +- ERROR("Could not create wl_drm global\n"); +- goto error4; ++ if (drm.path) { ++ drm.global = wl_global_create(swc.display, &wl_drm_interface, 2, ++ NULL, &bind_drm); ++ if (!drm.global) { ++ ERROR("Could not create wl_drm global\n"); ++ goto error4; ++ } + } + + drm.dmabuf = swc_dmabuf_create(swc.display); diff --git a/patches/outputs.patch b/patches/outputs.patch @@ -0,0 +1,275 @@ +diff --git a/libswc/screen.c b/libswc/screen.c +index 54b1dc4..6d624d3 100644 +--- a/libswc/screen.c ++++ b/libswc/screen.c +@@ -44,6 +44,7 @@ + + #include "swc-server-protocol.h" + #include <stdlib.h> ++#include <wayland-server.h> + + #define INTERNAL(s) ((struct screen *)(s)) + +@@ -276,12 +277,51 @@ screen_new_virtual(struct output *output, const struct view_impl *impl) + return screen; + } + ++/* the pointer may go anywhere on a screen and nowhere else */ ++static void ++pointer_region_update(void) ++{ ++ struct screen *screen; ++ pixman_region32_t region; ++ ++ if (!swc.seat) { ++ return; ++ } ++ pixman_region32_init(&region); ++ wl_list_for_each(screen, &swc.screens, link) ++ pixman_region32_union_rect(&region, &region, ++ screen->base.geometry.x, ++ screen->base.geometry.y, ++ screen->base.geometry.width, ++ screen->base.geometry.height); ++ pointer_set_region(swc.seat->pointer, &region); ++ pixman_region32_fini(&region); ++} ++ ++/* the screen's area changed: what depends on it follows, and the window ++ * manager is told */ ++static void ++screen_changed(struct screen *screen, bool resized) ++{ ++ if (resized) { ++ compositor_screen_resize(screen); ++ } else { ++ compositor_damage_all(); ++ } ++ pointer_region_update(); ++ /* a change can leave origin and edges of the usable area as they ++ * were, so it is told anyway */ ++ screen->base.usable_geometry.width = 0; ++ screen_update_usable_geometry(screen); ++ if (screen->handler->geometry_changed) { ++ screen->handler->geometry_changed(screen->handler_data); ++ } ++} ++ + void + screen_resize(struct screen *screen, uint32_t width, uint32_t height) + { + struct output *output; +- struct screen *other; +- pixman_region32_t region; + + screen->planes.primary.view.geometry.width = width; + screen->planes.primary.view.geometry.height = height; +@@ -293,23 +333,140 @@ screen_resize(struct screen *screen, uint32_t width, uint32_t height) + wl_list_for_each(output, &screen->outputs, link) + output_set_size(output, width, height); + +- compositor_screen_resize(screen); +- +- if (swc.seat) { +- pixman_region32_init(&region); +- wl_list_for_each(other, &swc.screens, link) +- pixman_region32_union_rect(&region, &region, +- other->base.geometry.x, +- other->base.geometry.y, +- other->base.geometry.width, +- other->base.geometry.height); +- pointer_set_region(swc.seat->pointer, &region); +- pixman_region32_fini(&region); ++ screen_changed(screen, true); ++} ++ ++static struct output * ++screen_output(struct screen *screen) ++{ ++ struct output *output; ++ ++ if (wl_list_empty(&screen->outputs)) { ++ return NULL; + } ++ return wl_container_of(screen->outputs.next, output, link); ++} + +- /* a resize can leave origin and edges unchanged, notify anyway */ +- screen->base.usable_geometry.width = 0; +- screen_update_usable_geometry(screen); ++EXPORT const char * ++swc_screen_name(struct swc_screen *base) ++{ ++ struct output *output = screen_output(INTERNAL(base)); ++ ++ return output ? output->name : ""; ++} ++ ++EXPORT enum swc_screen_kind ++swc_screen_kind(struct swc_screen *base) ++{ ++ struct screen *screen = INTERNAL(base); ++ ++ if (screen->virtual) { ++ return SWC_SCREEN_WINDOW; ++ } ++#ifdef ENABLE_DRM ++ if (swc.video == SWC_VIDEO_DRM) { ++ return SWC_SCREEN_DISPLAY; ++ } ++#endif ++ return SWC_SCREEN_FRAMEBUFFER; ++} ++ ++EXPORT int ++swc_screen_modes(struct swc_screen *base, struct swc_mode *out, int max) ++{ ++ struct screen *screen = INTERNAL(base); ++ struct output *output = screen_output(screen); ++ struct mode *mode; ++ int n = 0; ++ ++ if (!output) { ++ return 0; ++ } ++ wl_array_for_each(mode, &output->modes) ++ { ++ if (n < max) { ++ out[n] = (struct swc_mode){ ++ .width = mode->width, ++ .height = mode->height, ++ .refresh = mode->refresh, ++ .preferred = mode->preferred, ++ .current = mode_equal(&screen->planes.primary.mode, mode), ++ }; ++ } ++ n++; ++ } ++ return n; ++} ++ ++EXPORT bool ++swc_screen_set_mode(struct swc_screen *base, uint32_t width, uint32_t height, ++ uint32_t refresh) ++{ ++ struct screen *screen = INTERNAL(base); ++ struct output *output = screen_output(screen); ++ struct mode *mode, *best = NULL; ++ struct wl_resource *resource; ++ ++ if (!output || swc_screen_kind(base) != SWC_SCREEN_DISPLAY) { ++ return false; ++ } ++ wl_array_for_each(mode, &output->modes) ++ { ++ if (mode->width != width || mode->height != height) { ++ continue; ++ } ++ if (refresh ? mode->refresh == refresh ++ : !best || mode->refresh > best->refresh) { ++ best = mode; ++ } ++ } ++ if (!best) { ++ return false; ++ } ++#ifdef ENABLE_DRM ++ screen->planes.primary.mode = *best; ++ screen->planes.primary.need_modeset = true; ++#endif ++ screen->planes.primary.view.geometry.width = width; ++ screen->planes.primary.view.geometry.height = height; ++ screen->base.geometry.width = width; ++ screen->base.geometry.height = height; ++ wl_resource_for_each(resource, &output->resources) ++ { ++ wl_output_send_mode(resource, ++ WL_OUTPUT_MODE_CURRENT | ++ (best->preferred ? WL_OUTPUT_MODE_PREFERRED : 0), ++ best->width, best->height, best->refresh); ++ if (wl_resource_get_version(resource) >= 2) { ++ wl_output_send_done(resource); ++ } ++ } ++ screen_changed(screen, true); ++ return true; ++} ++ ++EXPORT void ++swc_screen_move(struct swc_screen *base, int32_t x, int32_t y) ++{ ++ struct screen *screen = INTERNAL(base); ++ struct output *output = screen_output(screen); ++ struct wl_resource *resource; ++ ++ view_move(&screen->planes.primary.view, x, y); ++ screen->base.geometry.x = x; ++ screen->base.geometry.y = y; ++ if (output) { ++ wl_resource_for_each(resource, &output->resources) ++ { ++ wl_output_send_geometry(resource, x, y, output->physical_width, ++ output->physical_height, 0, "unknown", ++ "unknown", WL_OUTPUT_TRANSFORM_NORMAL); ++ if (wl_resource_get_version(resource) >= 2) { ++ wl_output_send_done(resource); ++ } ++ } ++ } ++ screen_changed(screen, false); + } + + void +diff --git a/libswc/swc.h b/libswc/swc.h +index b6b0c4c..b63fe98 100644 +--- a/libswc/swc.h ++++ b/libswc/swc.h +@@ -302,6 +302,48 @@ void + swc_screen_set_handler(struct swc_screen *screen, + const struct swc_screen_handler *handler, void *data); + ++/* what a screen shows on, which says what may be changed about it */ ++enum swc_screen_kind { ++ /* a display: its modes are the hardware's, any of them may be set */ ++ SWC_SCREEN_DISPLAY, ++ /* a window of another compositor: its size is that window's */ ++ SWC_SCREEN_WINDOW, ++ /* a framebuffer: it has the one mode it was set to */ ++ SWC_SCREEN_FRAMEBUFFER, ++}; ++ ++struct swc_mode { ++ uint32_t width, height; ++ /* in mHz */ ++ uint32_t refresh; ++ bool preferred, current; ++}; ++ ++/* the name of the screen's output, as wl_output names it (HDMI-A-1) */ ++const char * ++swc_screen_name(struct swc_screen *screen); ++ ++enum swc_screen_kind ++swc_screen_kind(struct swc_screen *screen); ++ ++/* the modes the screen may be set to, at most max into out; how many ++ * there are */ ++int ++swc_screen_modes(struct swc_screen *screen, struct swc_mode *out, int max); ++ ++/* sets the screen to the mode of that size and refresh, a refresh of 0 ++ * meaning the highest the size has. the change is made with the next ++ * frame; geometry_changed follows. false when the screen has no such mode ++ * or its modes are not the compositor's to set */ ++bool ++swc_screen_set_mode(struct swc_screen *screen, uint32_t width, ++ uint32_t height, uint32_t refresh); ++ ++/* places the screen's top left corner at x, y of the space the screens ++ * share; geometry_changed follows */ ++void ++swc_screen_move(struct swc_screen *screen, int32_t x, int32_t y); ++ + /* }}} */ + + /* Windows {{{ */ diff --git a/patches/peer.patch b/patches/peer.patch @@ -0,0 +1,771 @@ +peer: a screen another program shows + +SWC_PEER_SOCKET makes swc draw into shared memory and hand each frame to +the program listening there, which sends back touch, keys and the mouse: +for a host that holds the real display, such as an application of another +system. one frame is in flight at a time and none is drawn while the peer +says it is hidden. see libswc/peer_protocol.h. + +--- a/libswc/backend.h ++++ b/libswc/backend.h +@@ -20,6 +20,8 @@ + + /* backend feeds the seat, no input devices */ + bool virtual_input; ++ /* and fingers with it */ ++ bool virtual_touch; + + /* optional, unset means the built-in video backend */ + bool (*create_screens)(struct wl_list *screens); +--- a/libswc/seat.h ++++ b/libswc/seat.h +@@ -30,6 +30,7 @@ + struct pointer *pointer; + struct keyboard *keyboard; + struct data_device *data_device; ++ struct touch *touch; + }; + + struct swc_seat * +--- a/libswc/seat.c ++++ b/libswc/seat.c +@@ -79,7 +79,6 @@ + + struct wl_listener keyboard_focus_listener; + struct pointer pointer; +- struct touch *touch; + struct wl_listener data_device_listener; + + struct wl_global *global; +@@ -173,7 +172,7 @@ + { + struct seat *seat = wl_resource_get_user_data(resource); + +- if (!touch_bind(seat->touch, client, wl_resource_get_version(resource), ++ if (!touch_bind(seat->base.touch, client, wl_resource_get_version(resource), + id)) { + wl_resource_post_no_memory(resource); + } +@@ -306,23 +305,23 @@ + y = wl_fixed_from_double(rect->y + + libinput_event_touch_get_y_transformed(event.touch, rect->height)); + if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TOUCH_DOWN) { +- touch_handle_down(seat->touch, time, ++ touch_handle_down(seat->base.touch, time, + libinput_event_touch_get_seat_slot(event.touch), x, y); + } else { +- touch_handle_motion(seat->touch, time, ++ touch_handle_motion(seat->base.touch, time, + libinput_event_touch_get_seat_slot(event.touch), x, y); + } + break; + case LIBINPUT_EVENT_TOUCH_UP: + event.touch = libinput_event_get_touch_event(generic_event); +- touch_handle_up(seat->touch, libinput_event_touch_get_time(event.touch), ++ touch_handle_up(seat->base.touch, libinput_event_touch_get_time(event.touch), + libinput_event_touch_get_seat_slot(event.touch)); + break; + case LIBINPUT_EVENT_TOUCH_FRAME: +- touch_handle_frame(seat->touch); ++ touch_handle_frame(seat->base.touch); + break; + case LIBINPUT_EVENT_TOUCH_CANCEL: +- touch_handle_cancel(seat->touch); ++ touch_handle_cancel(seat->base.touch); + break; + case LIBINPUT_EVENT_KEYBOARD_KEY: + event.k = libinput_event_get_keyboard_event(generic_event); +@@ -571,7 +570,7 @@ + } + seat->base.pointer = &seat->pointer; + +- if (!(seat->touch = touch_create(&seat->pointer))) { ++ if (!(seat->base.touch = touch_create(&seat->pointer))) { + ERROR("Could not initialize touch\n"); + goto error6; + } +@@ -580,7 +579,10 @@ + seat->libinput_source = NULL; + if (swc.backend->virtual_input) { + update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD | +- WL_SEAT_CAPABILITY_POINTER); ++ WL_SEAT_CAPABILITY_POINTER | ++ (swc.backend->virtual_touch ++ ? WL_SEAT_CAPABILITY_TOUCH ++ : 0)); + } else if (!initialize_libinput(seat)) { + goto error7; + } +@@ -588,7 +590,7 @@ + return &seat->base; + + error7: +- touch_destroy(seat->touch); ++ touch_destroy(seat->base.touch); + error6: + pointer_finalize(&seat->pointer); + error5: +@@ -618,7 +620,7 @@ + #endif + } + +- touch_destroy(seat->touch); ++ touch_destroy(seat->base.touch); + pointer_finalize(&seat->pointer); + keyboard_destroy(seat->base.keyboard); + data_device_destroy(seat->base.data_device); +--- a/libswc/swc.c ++++ b/libswc/swc.c +@@ -37,6 +37,9 @@ + #include "keyboard.h" + #include "launch.h" + #include "layer_shell.h" ++#ifdef ENABLE_PEER ++#include "peer.h" ++#endif + #ifdef ENABLE_NESTED + #include "nested.h" + #endif +@@ -265,6 +268,11 @@ + static bool + backend_initialize(void) + { ++#ifdef ENABLE_PEER ++ if (peer_requested()) { ++ return peer_initialize(); ++ } ++#endif + #ifdef ENABLE_NESTED + if (nested_requested()) { + return nested_initialize(); +--- a/libswc/meson.build ++++ b/libswc/meson.build +@@ -55,6 +55,9 @@ + if have_nested + libswc_src += files('nested.c') + endif ++if have_peer ++ libswc_src += files('peer.c') ++endif + + foreach proto, proto_src : protos + libswc_src += proto_src +--- a/meson.build ++++ b/meson.build +@@ -144,6 +144,17 @@ + requires_private += wayland_client + endif + ++# the peer backend feeds the libinput seat, touch and all, and shares its ++# screen through a memfd ++have_peer = (not get_option('peer').disabled() and input_backend == 'libinput' ++ and cc.has_function('memfd_create', prefix: '#include <sys/mman.h>', args: '-D_GNU_SOURCE')) ++if get_option('peer').enabled() and not have_peer ++ error('The peer backend needs the libinput input backend and memfd_create') ++endif ++if have_peer ++ add_project_arguments('-DENABLE_PEER=1', language: 'c') ++endif ++ + subdir('protocol') + subdir('cursor') + subdir('launch') +--- a/meson.options ++++ b/meson.options +@@ -6,5 +6,6 @@ + option('wsdisplay-device', type: 'string', value: '', description: 'wsdisplay framebuffer device (empty uses the VT selected by swc-launch)') + option('udev', type: 'feature', value: 'auto', description: 'libinput libudev support') + option('nested', type: 'feature', value: 'auto', description: 'Backend that runs swc inside another Wayland compositor') ++option('peer', type: 'feature', value: 'auto', description: 'Backend whose screen another program shows, over a socket') + option('extra', type: 'boolean', value: true, description: 'Build swc utility applications') + option('example', type: 'boolean', value: false, description: 'Build swc example compositor') +--- /dev/null ++++ b/libswc/peer.c +@@ -0,0 +1,506 @@ ++/* swc: libswc/peer.c ++ * ++ * the screen drawn in software into shared memory and shown by a peer, which ++ * sends back the input: a program of another system holding the real display, ++ * such as an application of it. see peer_protocol.h ++ */ ++ ++#include "peer.h" ++#include "backend.h" ++#include "compositor.h" ++#include "internal.h" ++#include "keyboard.h" ++#include "output.h" ++#include "peer_protocol.h" ++#include "pointer.h" ++#include "screen.h" ++#include "seat.h" ++#include "surface.h" ++#include "touch.h" ++#include "util.h" ++ ++#include <errno.h> ++#include <fcntl.h> ++#include <pixman.h> ++#include <stddef.h> ++#include <stdio.h> ++#include <stdlib.h> ++#include <string.h> ++#include <sys/mman.h> ++#include <sys/socket.h> ++#include <sys/un.h> ++#include <unistd.h> ++#include <wld/pixman.h> ++#include <wld/wld.h> ++ ++/* a screen past this is a peer's mistake, not a size to allocate */ ++#define SIZE_LIMIT 16384 ++ ++static struct { ++ struct swc_backend backend; ++ int fd; ++ struct wl_event_source *source; ++ ++ struct screen *screen; ++ uint32_t width, height; ++ /* the buffer shared with the peer, and what it has not been sent */ ++ uint32_t *pixels; ++ size_t size; ++ bool whole; ++ ++ bool in_flight; /* a frame sent and not yet done with */ ++ bool frame_due; /* done, but the frame callback waits for it to be seen */ ++ bool shown; ++ ++ /* a message read in part */ ++ struct swc_peer_message in; ++ size_t got; ++} peer; ++ ++static bool ++send_message(const struct swc_peer_message *message, int descriptor) ++{ ++ union { ++ struct cmsghdr header; ++ char space[CMSG_SPACE(sizeof(int))]; ++ } control; ++ struct iovec iov = { (void *)message, sizeof(*message) }; ++ struct msghdr msg = { 0 }; ++ struct cmsghdr *header; ++ size_t sent = 0; ++ ssize_t n; ++ ++ msg.msg_iov = &iov; ++ msg.msg_iovlen = 1; ++ if (descriptor >= 0) { ++ msg.msg_control = control.space; ++ msg.msg_controllen = sizeof(control.space); ++ header = CMSG_FIRSTHDR(&msg); ++ header->cmsg_level = SOL_SOCKET; ++ header->cmsg_type = SCM_RIGHTS; ++ header->cmsg_len = CMSG_LEN(sizeof(int)); ++ memcpy(CMSG_DATA(header), &descriptor, sizeof(int)); ++ } ++ while (sent < sizeof(*message)) { ++ n = sendmsg(peer.fd, &msg, MSG_NOSIGNAL); ++ if (n < 0) { ++ if (errno == EINTR) { ++ continue; ++ } ++ ERROR("peer: cannot send: %s\n", strerror(errno)); ++ return false; ++ } ++ sent += (size_t)n; ++ iov.iov_base = (char *)message + sent; ++ iov.iov_len = sizeof(*message) - sent; ++ msg.msg_control = NULL; ++ msg.msg_controllen = 0; ++ } ++ return true; ++} ++ ++/* a new buffer of width by height, handed to the peer */ ++static bool ++share_buffer(uint32_t width, uint32_t height) ++{ ++ struct swc_peer_message message = { SWC_PEER_BUFFER, { 0 } }; ++ size_t size = (size_t)width * height * 4; ++ uint32_t *pixels; ++ char path[32]; ++ int fd, shown = -1; ++ ++ fd = memfd_create("swc-peer", MFD_CLOEXEC); ++ if (fd < 0 || ftruncate(fd, (off_t)size) < 0) { ++ ERROR("peer: cannot make the screen buffer: %s\n", strerror(errno)); ++ goto fail; ++ } ++ /* the peer only shows the buffer, so it gets a descriptor that cannot ++ * write it: a confined peer may be refused one that can */ ++ snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); ++ shown = open(path, O_RDONLY | O_CLOEXEC); ++ if (shown < 0) { ++ ERROR("peer: cannot open the screen buffer to be read: %s\n", strerror(errno)); ++ goto fail; ++ } ++ pixels = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); ++ if (pixels == MAP_FAILED) { ++ ERROR("peer: cannot map the screen buffer: %s\n", strerror(errno)); ++ goto fail; ++ } ++ if (peer.pixels) { ++ munmap(peer.pixels, peer.size); ++ } ++ peer.pixels = pixels; ++ peer.size = size; ++ peer.width = width; ++ peer.height = height; ++ peer.whole = true; ++ peer.in_flight = false; ++ ++ message.arg[0] = (int32_t)width; ++ message.arg[1] = (int32_t)height; ++ message.arg[2] = (int32_t)width * 4; ++ if (!send_message(&message, shown)) { ++ goto fail; ++ } ++ close(shown); ++ close(fd); ++ return true; ++ ++fail: ++ if (shown >= 0) { ++ close(shown); ++ } ++ if (fd >= 0) { ++ close(fd); ++ } ++ return false; ++} ++ ++/* the composited frame's damaged rows into the shared buffer */ ++static int ++plane_attach(struct view *view, struct wld_buffer *buffer) ++{ ++ const pixman_region32_t *damage = compositor_frame_damage(); ++ struct swc_peer_message message = { SWC_PEER_FRAME, { 0 } }; ++ pixman_region32_t region; ++ const pixman_box32_t *boxes, *extents; ++ uint32_t width, height; ++ int32_t y; ++ int i, n; ++ ++ (void)view; ++ if (!buffer || !wld_map(buffer)) { ++ return 0; ++ } ++ width = MIN(buffer->width, peer.width); ++ height = MIN(buffer->height, peer.height); ++ pixman_region32_init(&region); ++ if (peer.whole || !damage) { ++ pixman_region32_union_rect(&region, &region, 0, 0, width, height); ++ peer.whole = false; ++ } else { ++ pixman_region32_copy(&region, (pixman_region32_t *)damage); ++ pixman_region32_intersect_rect(&region, &region, 0, 0, width, height); ++ } ++ boxes = pixman_region32_rectangles(&region, &n); ++ for (i = 0; i < n; ++i) { ++ for (y = boxes[i].y1; y < boxes[i].y2; ++y) { ++ memcpy(peer.pixels + (size_t)y * peer.width + boxes[i].x1, ++ (const uint8_t *)buffer->map + (size_t)y * buffer->pitch + ++ (size_t)boxes[i].x1 * 4, ++ (size_t)(boxes[i].x2 - boxes[i].x1) * 4); ++ } ++ } ++ wld_unmap(buffer); ++ ++ extents = pixman_region32_extents(&region); ++ message.arg[0] = extents->x1; ++ message.arg[1] = extents->y1; ++ message.arg[2] = extents->x2 - extents->x1; ++ message.arg[3] = extents->y2 - extents->y1; ++ pixman_region32_fini(&region); ++ peer.in_flight = true; ++ send_message(&message, -1); ++ return 0; ++} ++ ++static bool ++plane_update(struct view *view) ++{ ++ (void)view; ++ return true; ++} ++ ++static bool ++plane_move(struct view *view, int32_t x, int32_t y) ++{ ++ view_set_position(view, x, y); ++ return true; ++} ++ ++static const struct view_impl plane_impl = { ++ .update = plane_update, ++ .attach = plane_attach, ++ .move = plane_move, ++}; ++ ++static void ++next_frame(void) ++{ ++ if (!peer.shown) { ++ peer.frame_due = true; ++ return; ++ } ++ peer.frame_due = false; ++ view_frame(&peer.screen->planes.primary.view, get_time()); ++} ++ ++static bool ++valid_size(int32_t width, int32_t height) ++{ ++ return width > 0 && height > 0 && width <= SIZE_LIMIT && height <= SIZE_LIMIT; ++} ++ ++static void ++handle_message(const struct swc_peer_message *message) ++{ ++ const int32_t *arg = message->arg; ++ uint32_t time = get_time(); ++ wl_fixed_t x, y; ++ ++ switch (message->type) { ++ case SWC_PEER_SIZE: ++ if (valid_size(arg[0], arg[1]) && ++ ((uint32_t)arg[0] != peer.width || (uint32_t)arg[1] != peer.height) && ++ share_buffer((uint32_t)arg[0], (uint32_t)arg[1])) { ++ screen_resize(peer.screen, peer.width, peer.height); ++ next_frame(); ++ } ++ break; ++ case SWC_PEER_DONE: ++ if (peer.in_flight) { ++ peer.in_flight = false; ++ next_frame(); ++ } ++ break; ++ case SWC_PEER_SHOWN: ++ peer.shown = arg[0] != 0; ++ if (peer.shown) { ++ /* what was lost while unseen: the whole screen again */ ++ peer.whole = true; ++ compositor_damage_all(); ++ if (peer.frame_due) { ++ next_frame(); ++ } ++ } ++ break; ++ case SWC_PEER_TOUCH: ++ if (!swc.seat || !swc.seat->touch) { ++ break; ++ } ++ x = wl_fixed_from_int(arg[2]); ++ y = wl_fixed_from_int(arg[3]); ++ if (arg[1] == SWC_PEER_DOWN) { ++ touch_handle_down(swc.seat->touch, time, arg[0], x, y); ++ } else if (arg[1] == SWC_PEER_MOVE) { ++ touch_handle_motion(swc.seat->touch, time, arg[0], x, y); ++ } else if (arg[1] == SWC_PEER_UP) { ++ touch_handle_up(swc.seat->touch, time, arg[0]); ++ } else { ++ touch_handle_cancel(swc.seat->touch); ++ } ++ break; ++ case SWC_PEER_TOUCH_FRAME: ++ if (swc.seat && swc.seat->touch) { ++ touch_handle_frame(swc.seat->touch); ++ } ++ break; ++ case SWC_PEER_KEY: ++ if (swc.seat) { ++ keyboard_handle_key(swc.seat->keyboard, time, (uint32_t)arg[0], ++ arg[1] ? WL_KEYBOARD_KEY_STATE_PRESSED ++ : WL_KEYBOARD_KEY_STATE_RELEASED); ++ } ++ break; ++ case SWC_PEER_MOTION: ++ if (swc.seat) { ++ pointer_handle_absolute_motion(swc.seat->pointer, time, ++ wl_fixed_from_int(arg[0]), ++ wl_fixed_from_int(arg[1])); ++ pointer_handle_frame(swc.seat->pointer); ++ } ++ break; ++ case SWC_PEER_BUTTON: ++ if (swc.seat) { ++ pointer_handle_button(swc.seat->pointer, time, (uint32_t)arg[0], ++ arg[1] ? WL_POINTER_BUTTON_STATE_PRESSED ++ : WL_POINTER_BUTTON_STATE_RELEASED); ++ pointer_handle_frame(swc.seat->pointer); ++ } ++ break; ++ case SWC_PEER_SCROLL: ++ if (swc.seat && arg[0] >= 0 && arg[0] <= 1) { ++ pointer_handle_axis(swc.seat->pointer, time, (uint32_t)arg[0], ++ WL_POINTER_AXIS_SOURCE_CONTINUOUS, arg[1], 0); ++ pointer_handle_frame(swc.seat->pointer); ++ } ++ break; ++ } ++} ++ ++static int ++handle_data(int fd, uint32_t mask, void *data) ++{ ++ ssize_t n; ++ ++ (void)data; ++ if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR) && !(mask & WL_EVENT_READABLE)) { ++ wl_display_terminate(swc.display); ++ return 0; ++ } ++ for (;;) { ++ n = recv(fd, (char *)&peer.in + peer.got, sizeof(peer.in) - peer.got, ++ MSG_DONTWAIT); ++ if (n < 0 && errno == EINTR) { ++ continue; ++ } ++ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { ++ break; ++ } ++ if (n <= 0) { ++ /* the peer is gone: the session has nowhere to show */ ++ wl_event_source_remove(peer.source); ++ peer.source = NULL; ++ wl_display_terminate(swc.display); ++ break; ++ } ++ peer.got += (size_t)n; ++ if (peer.got == sizeof(peer.in)) { ++ peer.got = 0; ++ handle_message(&peer.in); ++ } ++ } ++ return 0; ++} ++ ++static bool ++create_screens(struct wl_list *screens) ++{ ++ struct output *output; ++ ++ output = output_new_fb(peer.width, peer.height, "peer"); ++ if (!output) { ++ return false; ++ } ++ peer.screen = screen_new_virtual(output, &plane_impl); ++ if (!peer.screen) { ++ output_destroy(output); ++ return false; ++ } ++ output->screen = peer.screen; ++ wl_list_insert(screens, &peer.screen->link); ++ return true; ++} ++ ++static void ++start(void) ++{ ++ peer.source = wl_event_loop_add_fd(swc.event_loop, peer.fd, ++ WL_EVENT_READABLE, &handle_data, NULL); ++ if (!peer.source) { ++ ERROR("peer: cannot watch the socket\n"); ++ wl_display_terminate(swc.display); ++ return; ++ } ++ /* no console to switch to it: it is the one screen from the start */ ++ swc_activate(); ++} ++ ++static void ++finalize(void) ++{ ++ if (peer.source) { ++ wl_event_source_remove(peer.source); ++ } ++ if (peer.backend.renderer) { ++ wld_destroy_renderer(peer.backend.renderer); ++ } ++ if (peer.backend.context) { ++ wld_destroy_context(peer.backend.context); ++ } ++ if (peer.pixels) { ++ munmap(peer.pixels, peer.size); ++ } ++ close(peer.fd); ++} ++ ++static int ++connect_to(const char *path) ++{ ++ struct sockaddr_un address = { .sun_family = AF_UNIX }; ++ size_t length = strlen(path); ++ int fd; ++ ++ if (length == 0 || length >= sizeof(address.sun_path)) { ++ ERROR("peer: SWC_PEER_SOCKET is empty or too long\n"); ++ return -1; ++ } ++ memcpy(address.sun_path, path, length); ++ if (path[0] == '@') { ++ address.sun_path[0] = '\0'; ++ } else { ++ length++; ++ } ++ fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); ++ if (fd < 0 || connect(fd, (struct sockaddr *)&address, ++ (socklen_t)(offsetof(struct sockaddr_un, sun_path) + length)) < 0) { ++ ERROR("peer: cannot connect to %s: %s\n", path, strerror(errno)); ++ if (fd >= 0) { ++ close(fd); ++ } ++ return -1; ++ } ++ return fd; ++} ++ ++bool ++peer_requested(void) ++{ ++ return getenv("SWC_PEER_SOCKET") != NULL; ++} ++ ++bool ++peer_initialize(void) ++{ ++ struct swc_peer_message message; ++ size_t got = 0; ++ ssize_t n; ++ ++ peer.fd = connect_to(getenv("SWC_PEER_SOCKET")); ++ if (peer.fd < 0) { ++ return false; ++ } ++ /* the screen's size, before there is a screen */ ++ while (got < sizeof(message)) { ++ n = recv(peer.fd, (char *)&message + got, sizeof(message) - got, 0); ++ if (n < 0 && errno == EINTR) { ++ continue; ++ } ++ if (n <= 0) { ++ ERROR("peer: it said no size\n"); ++ goto fail; ++ } ++ got += (size_t)n; ++ } ++ if (message.type != SWC_PEER_SIZE || !valid_size(message.arg[0], message.arg[1])) { ++ ERROR("peer: it said no size\n"); ++ goto fail; ++ } ++ if (!share_buffer((uint32_t)message.arg[0], (uint32_t)message.arg[1])) { ++ goto fail; ++ } ++ peer.shown = true; ++ ++ peer.backend.context = wld_pixman_create_context(); ++ if (!peer.backend.context) { ++ goto fail; ++ } ++ peer.backend.renderer = wld_create_renderer(peer.backend.context); ++ if (!peer.backend.renderer) { ++ goto fail; ++ } ++ peer.backend.cursor_width = 64; ++ peer.backend.cursor_height = 64; ++ peer.backend.virtual_input = true; ++ peer.backend.virtual_touch = true; ++ peer.backend.create_screens = create_screens; ++ peer.backend.start = start; ++ peer.backend.finalize = finalize; ++ swc.backend = &peer.backend; ++ return true; ++ ++fail: ++ finalize(); ++ return false; ++} +--- /dev/null ++++ b/libswc/peer.h +@@ -0,0 +1,16 @@ ++/* swc: libswc/peer.h ++ * ++ * a screen shown by another program over a socket, see peer_protocol.h ++ */ ++ ++#ifndef SWC_PEER_H ++#define SWC_PEER_H ++ ++#include <stdbool.h> ++ ++bool ++peer_requested(void); ++bool ++peer_initialize(void); ++ ++#endif +--- /dev/null ++++ b/libswc/peer_protocol.h +@@ -0,0 +1,59 @@ ++/* swc: libswc/peer_protocol.h ++ * ++ * the peer backend and the program that shows its frames and sends it input, ++ * its peer, over a stream socket: SWC_PEER_SOCKET, a path, or @name in ++ * linux's abstract namespace. the peer listens, swc connects. every message ++ * is one struct swc_peer_message in the machine's byte order; the one from ++ * swc that carries a descriptor, open to be read only, carries it with its ++ * first byte (SCM_RIGHTS) ++ * ++ * the peer's first message is SWC_PEER_SIZE. swc then sends SWC_PEER_BUFFER ++ * and, as the screen changes, SWC_PEER_FRAME. it draws the next frame only ++ * after SWC_PEER_DONE, and none while the peer says it is hidden. the ++ * session ends when the socket closes ++ */ ++ ++#ifndef SWC_PEER_PROTOCOL_H ++#define SWC_PEER_PROTOCOL_H ++ ++#include <stdint.h> ++ ++enum { ++ /* from swc: a shared memory descriptor holding the screen, one ++ * XRGB8888 pixel a 32 bit word: arg width, height, bytes per row. ++ * it replaces any before it */ ++ SWC_PEER_BUFFER = 1, ++ /* from swc: the buffer changed in x y width height. the peer reads it ++ * until it answers SWC_PEER_DONE */ ++ SWC_PEER_FRAME = 2, ++ ++ /* from the peer: the screen's size in pixels, at the start and when ++ * it changes: arg width, height */ ++ SWC_PEER_SIZE = 16, ++ /* the last frame has been taken */ ++ SWC_PEER_DONE = 17, ++ /* arg 1 when the screen is seen, 0 when it is not */ ++ SWC_PEER_SHOWN = 18, ++ /* arg slot, phase (SWC_PEER_DOWN...), x, y in screen pixels */ ++ SWC_PEER_TOUCH = 19, ++ /* the touches since the last one happened together */ ++ SWC_PEER_TOUCH_FRAME = 20, ++ /* arg linux evdev code, 1 pressed or 0 let go */ ++ SWC_PEER_KEY = 21, ++ /* a mouse: arg x, y in screen pixels */ ++ SWC_PEER_MOTION = 22, ++ /* arg linux evdev button, 1 pressed or 0 let go */ ++ SWC_PEER_BUTTON = 23, ++ /* arg wl_pointer axis, the distance as wl_fixed_t */ ++ SWC_PEER_SCROLL = 24, ++}; ++ ++/* phases of SWC_PEER_TOUCH */ ++enum { SWC_PEER_DOWN = 0, SWC_PEER_MOVE = 1, SWC_PEER_UP = 2, SWC_PEER_CANCEL = 3 }; ++ ++struct swc_peer_message { ++ uint32_t type; ++ int32_t arg[5]; ++}; ++ ++#endif diff --git a/patches/tablets.patch b/patches/tablets.patch @@ -0,0 +1,68 @@ +Subject: seat: a tablet pen drives the pointer + +diff --git a/libswc/seat.c b/libswc/seat.c +index 02d43a0..76decec 100644 +--- a/libswc/seat.c ++++ b/libswc/seat.c +@@ -240,7 +240,9 @@ device_capabilities(struct libinput_device *device) + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { + capabilities |= WL_SEAT_CAPABILITY_KEYBOARD; + } +- if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { ++ /* a tablet's pen drives the pointer (see tablet_tool below) */ ++ if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER) || ++ libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { + capabilities |= WL_SEAT_CAPABILITY_POINTER; + } + /* TODO: Add touch device support +@@ -262,6 +264,7 @@ handle_libinput_data(int fd, uint32_t mask, void *data) + union { + struct libinput_event_keyboard *k; + struct libinput_event_pointer *p; ++ struct libinput_event_tablet_tool *t; + } event; + wl_fixed_t x, y, value; + uint32_t time, key, state; +@@ -311,6 +314,42 @@ handle_libinput_data(int fd, uint32_t mask, void *data) + pointer_handle_absolute_motion(&seat->pointer, time, x, y); + pointer_handle_frame(&seat->pointer); + break; ++ /* A tablet's pen as the pointer until clients are told of tablets: ++ * where it points, its tip the left button and its barrel's two ++ * buttons the middle and right ones, as a mouse would have them. */ ++ case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: ++ case LIBINPUT_EVENT_TABLET_TOOL_AXIS: ++ case LIBINPUT_EVENT_TABLET_TOOL_TIP: ++ screen = wl_container_of(swc.screens.next, screen, link); ++ rect = &screen->base.geometry; ++ event.t = libinput_event_get_tablet_tool_event(generic_event); ++ time = libinput_event_tablet_tool_get_time(event.t); ++ x = wl_fixed_from_double( ++ libinput_event_tablet_tool_get_x_transformed(event.t, rect->width)); ++ y = wl_fixed_from_double( ++ libinput_event_tablet_tool_get_y_transformed(event.t, rect->height)); ++ pointer_handle_absolute_motion(&seat->pointer, time, x, y); ++ if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TABLET_TOOL_TIP) { ++ pointer_handle_button(&seat->pointer, time, BTN_LEFT, ++ libinput_event_tablet_tool_get_tip_state(event.t) == ++ LIBINPUT_TABLET_TOOL_TIP_DOWN ++ ? WL_POINTER_BUTTON_STATE_PRESSED ++ : WL_POINTER_BUTTON_STATE_RELEASED); ++ } ++ pointer_handle_frame(&seat->pointer); ++ break; ++ case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: ++ event.t = libinput_event_get_tablet_tool_event(generic_event); ++ time = libinput_event_tablet_tool_get_time(event.t); ++ key = libinput_event_tablet_tool_get_button(event.t); ++ key = key == BTN_STYLUS ? BTN_MIDDLE : key == BTN_STYLUS2 ? BTN_RIGHT : key; ++ pointer_handle_button(&seat->pointer, time, key, ++ libinput_event_tablet_tool_get_button_state(event.t) == ++ LIBINPUT_BUTTON_STATE_PRESSED ++ ? WL_POINTER_BUTTON_STATE_PRESSED ++ : WL_POINTER_BUTTON_STATE_RELEASED); ++ pointer_handle_frame(&seat->pointer); ++ break; + case LIBINPUT_EVENT_POINTER_BUTTON: + event.p = libinput_event_get_pointer_event(generic_event); + time = libinput_event_pointer_get_time(event.p); diff --git a/patches/touch.patch b/patches/touch.patch @@ -0,0 +1,610 @@ +seat: touchscreens + +wl_touch for the clients that bind it, a finger staying with the surface it +went down on, focused and raised. elsewhere, decorations and clients +without touch included, the first finger is the pointer: a tap a left +click, a drag with the button held, a hold of half a second a right click. + +--- a/libswc/compositor.c ++++ b/libswc/compositor.c +@@ -1397,6 +1397,12 @@ + return NULL; + } + ++struct compositor_view * ++compositor_content_view_at(int32_t x, int32_t y) ++{ ++ return compositor_chrome_at(x, y) ? NULL : compositor_view_at(x, y); ++} ++ + static struct compositor_view * + window_view(struct compositor_view *view) + { +--- a/libswc/compositor.h ++++ b/libswc/compositor.h +@@ -161,6 +161,11 @@ + compositor_view_restack(struct compositor_view *view, + struct compositor_view *sibling, bool above); + ++/* the view whose content is under x, y, NULL on a window's decoration or ++ * border, which are the compositor's and not the client's */ ++struct compositor_view * ++compositor_content_view_at(int32_t x, int32_t y); ++ + /* topmost view taking input at x, y, or NULL */ + struct compositor_view * + compositor_view_at(int32_t x, int32_t y); +--- a/libswc/meson.build ++++ b/libswc/meson.build +@@ -40,7 +40,7 @@ + 'xdg_output.c', + 'xdg_shell.c', + +- input_backend == 'libinput' ? 'seat.c' : [], ++ input_backend == 'libinput' ? ['seat.c', 'touch.c'] : [], + input_backend == 'wscons' ? 'seat_ws.c' : [], + input_backend == 'wscons' and os == 'openbsd' ? 'seat_ws_openbsd.c' : [], + +--- a/libswc/seat.c ++++ b/libswc/seat.c +@@ -32,6 +32,7 @@ + #include "pointer.h" + #include "screen.h" + #include "surface.h" ++#include "touch.h" + #include "util.h" + + #include <dirent.h> +@@ -78,6 +79,7 @@ + + struct wl_listener keyboard_focus_listener; + struct pointer pointer; ++ struct touch *touch; + struct wl_listener data_device_listener; + + struct wl_global *global; +@@ -169,7 +171,12 @@ + static void + get_touch(struct wl_client *client, struct wl_resource *resource, uint32_t id) + { +- /* XXX: Implement */ ++ struct seat *seat = wl_resource_get_user_data(resource); ++ ++ if (!touch_bind(seat->touch, client, wl_resource_get_version(resource), ++ id)) { ++ wl_resource_post_no_memory(resource); ++ } + } + + static const struct wl_seat_interface seat_impl = { +@@ -245,10 +252,11 @@ + libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { + capabilities |= WL_SEAT_CAPABILITY_POINTER; + } +- /* TODO: Add touch device support +- * if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) +- * capabilities |= WL_SEAT_CAPABILITY_TOUCH; +- */ ++ /* a finger drives the pointer where no client takes it (touch.c), so a ++ * touchscreen is a pointer too */ ++ if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { ++ capabilities |= WL_SEAT_CAPABILITY_TOUCH | WL_SEAT_CAPABILITY_POINTER; ++ } + + return capabilities; + } +@@ -265,6 +273,7 @@ + struct libinput_event_keyboard *k; + struct libinput_event_pointer *p; + struct libinput_event_tablet_tool *t; ++ struct libinput_event_touch *touch; + } event; + wl_fixed_t x, y, value; + uint32_t time, key, state; +@@ -285,6 +294,36 @@ + swc.manager->new_device(device); + } + break; ++ case LIBINPUT_EVENT_TOUCH_DOWN: ++ case LIBINPUT_EVENT_TOUCH_MOTION: ++ /* the panel's own coordinates are the first screen's */ ++ screen = wl_container_of(swc.screens.next, screen, link); ++ rect = &screen->base.geometry; ++ event.touch = libinput_event_get_touch_event(generic_event); ++ time = libinput_event_touch_get_time(event.touch); ++ x = wl_fixed_from_double(rect->x + ++ libinput_event_touch_get_x_transformed(event.touch, rect->width)); ++ y = wl_fixed_from_double(rect->y + ++ libinput_event_touch_get_y_transformed(event.touch, rect->height)); ++ if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TOUCH_DOWN) { ++ touch_handle_down(seat->touch, time, ++ libinput_event_touch_get_seat_slot(event.touch), x, y); ++ } else { ++ touch_handle_motion(seat->touch, time, ++ libinput_event_touch_get_seat_slot(event.touch), x, y); ++ } ++ break; ++ case LIBINPUT_EVENT_TOUCH_UP: ++ event.touch = libinput_event_get_touch_event(generic_event); ++ touch_handle_up(seat->touch, libinput_event_touch_get_time(event.touch), ++ libinput_event_touch_get_seat_slot(event.touch)); ++ break; ++ case LIBINPUT_EVENT_TOUCH_FRAME: ++ touch_handle_frame(seat->touch); ++ break; ++ case LIBINPUT_EVENT_TOUCH_CANCEL: ++ touch_handle_cancel(seat->touch); ++ break; + case LIBINPUT_EVENT_KEYBOARD_KEY: + event.k = libinput_event_get_keyboard_event(generic_event); + time = libinput_event_keyboard_get_time(event.k); +@@ -532,17 +571,24 @@ + } + seat->base.pointer = &seat->pointer; + ++ if (!(seat->touch = touch_create(&seat->pointer))) { ++ ERROR("Could not initialize touch\n"); ++ goto error6; ++ } ++ + seat->libinput = NULL; + seat->libinput_source = NULL; + if (swc.backend->virtual_input) { + update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD | + WL_SEAT_CAPABILITY_POINTER); + } else if (!initialize_libinput(seat)) { +- goto error6; ++ goto error7; + } + + return &seat->base; + ++error7: ++ touch_destroy(seat->touch); + error6: + pointer_finalize(&seat->pointer); + error5: +@@ -572,6 +618,7 @@ + #endif + } + ++ touch_destroy(seat->touch); + pointer_finalize(&seat->pointer); + keyboard_destroy(seat->base.keyboard); + data_device_destroy(seat->base.data_device); +--- /dev/null ++++ b/libswc/touch.c +@@ -0,0 +1,389 @@ ++/* swc: libswc/touch.c ++ * ++ * a finger on a surface whose client has bound wl_touch is that client's, ++ * from down to up, wherever it moves. a finger anywhere else (a client with ++ * no touch, a window's decoration, the empty screen) works the pointer ++ * instead, one finger at a time: a tap is a click of the left button, a ++ * finger that moves drags with it, and one held still is a click of the ++ * right button ++ */ ++ ++#include "touch.h" ++#include "compositor.h" ++#include "internal.h" ++#include "layer_shell.h" ++#include "pointer.h" ++#include "surface.h" ++#include "util.h" ++#include "window.h" ++ ++#include <stdlib.h> ++ ++#if defined(__linux__) ++#include <linux/input.h> ++#elif defined(__FreeBSD__) ++#include <dev/evdev/input.h> /* mirrors linux/input.h functionality */ ++#endif ++ ++/* fingers followed at once: more are ignored */ ++#define POINTS 16 ++/* how far a finger moves before it drags, and how long it is held still ++ * before it is a right click, as touch systems commonly have them */ ++#define SLOP_PX 8 ++#define HOLD_MS 500 ++ ++struct point { ++ int32_t slot; ++ /* NULL when the slot is free or the surface went away */ ++ struct compositor_view *view; ++ struct wl_client *client; ++ struct wl_listener view_destroy; ++ bool used; ++}; ++ ++enum emulation { ++ NOT_EMULATING, ++ WAITING, /* down, nothing pressed yet */ ++ DRAGGING, /* the left button held */ ++ SPENT, /* the hold has clicked: only the lift is left */ ++}; ++ ++struct touch { ++ struct pointer *pointer; ++ struct wl_list resources; ++ struct point points[POINTS]; ++ ++ enum emulation emulation; ++ int32_t slot; ++ wl_fixed_t x, y; ++ uint32_t time; ++ struct wl_event_source *hold; ++}; ++ ++static void ++click(struct touch *touch, uint32_t time, uint32_t button) ++{ ++ pointer_handle_button(touch->pointer, time, button, ++ WL_POINTER_BUTTON_STATE_PRESSED); ++ pointer_handle_button(touch->pointer, time, button, ++ WL_POINTER_BUTTON_STATE_RELEASED); ++} ++ ++static int ++handle_hold(void *data) ++{ ++ struct touch *touch = data; ++ ++ if (touch->emulation == WAITING) { ++ click(touch, touch->time + HOLD_MS, BTN_RIGHT); ++ pointer_handle_frame(touch->pointer); ++ touch->emulation = SPENT; ++ } ++ return 0; ++} ++ ++static void ++point_release(struct point *point) ++{ ++ if (point->view) { ++ wl_list_remove(&point->view_destroy.link); ++ point->view = NULL; ++ } ++ point->used = false; ++} ++ ++static void ++handle_view_destroy(struct wl_listener *listener, void *data) ++{ ++ struct point *point = wl_container_of(listener, point, view_destroy); ++ ++ (void)data; ++ wl_list_remove(&point->view_destroy.link); ++ point->view = NULL; ++} ++ ++static struct point * ++find_point(struct touch *touch, int32_t slot) ++{ ++ size_t i; ++ ++ for (i = 0; i < POINTS; ++i) { ++ if (touch->points[i].used && touch->points[i].slot == slot) { ++ return &touch->points[i]; ++ } ++ } ++ return NULL; ++} ++ ++static bool ++takes_touch(struct touch *touch, struct wl_client *client) ++{ ++ struct wl_resource *resource; ++ ++ wl_resource_for_each(resource, &touch->resources) ++ { ++ if (wl_resource_get_client(resource) == client) { ++ return true; ++ } ++ } ++ return false; ++} ++ ++/* the position on the surface of a point of the global space */ ++static void ++on_surface(struct compositor_view *view, wl_fixed_t x, wl_fixed_t y, ++ wl_fixed_t *sx, wl_fixed_t *sy) ++{ ++ *sx = x - wl_fixed_from_int(view->base.geometry.x - view->buffer_offset_x); ++ *sy = y - wl_fixed_from_int(view->base.geometry.y - view->buffer_offset_y); ++} ++ ++static void ++emulate_down(struct touch *touch, uint32_t time, int32_t slot, wl_fixed_t x, ++ wl_fixed_t y) ++{ ++ if (touch->emulation != NOT_EMULATING) { ++ return; ++ } ++ touch->emulation = WAITING; ++ touch->slot = slot; ++ touch->x = x; ++ touch->y = y; ++ touch->time = time; ++ pointer_handle_absolute_motion(touch->pointer, time, x, y); ++ pointer_handle_frame(touch->pointer); ++ wl_event_source_timer_update(touch->hold, HOLD_MS); ++} ++ ++void ++touch_handle_down(struct touch *touch, uint32_t time, int32_t slot, ++ wl_fixed_t x, wl_fixed_t y) ++{ ++ struct compositor_view *view; ++ struct wl_client *client; ++ struct wl_resource *resource; ++ struct point *point = NULL; ++ wl_fixed_t sx, sy; ++ uint32_t serial; ++ size_t i; ++ ++ view = compositor_content_view_at(wl_fixed_to_int(x), wl_fixed_to_int(y)); ++ client = view ? wl_resource_get_client(view->surface->resource) : NULL; ++ if (!client || !takes_touch(touch, client)) { ++ emulate_down(touch, time, slot, x, y); ++ return; ++ } ++ ++ for (i = 0; !point && i < POINTS; ++i) { ++ if (!touch->points[i].used) { ++ point = &touch->points[i]; ++ } ++ } ++ if (!point) { ++ return; ++ } ++ point->used = true; ++ point->slot = slot; ++ point->view = view; ++ point->client = client; ++ point->view_destroy.notify = handle_view_destroy; ++ wl_signal_add(&view->destroy_signal, &point->view_destroy); ++ ++ /* a touch focuses and raises as a press does */ ++ layer_shell_pressed(view); ++ raise_window(view); ++ if (view->window) { ++ window_pointer_entered(view->window); ++ } ++ ++ on_surface(view, x, y, &sx, &sy); ++ serial = wl_display_next_serial(swc.display); ++ wl_resource_for_each(resource, &touch->resources) ++ { ++ if (wl_resource_get_client(resource) == client) { ++ wl_touch_send_down(resource, serial, time, view->surface->resource, ++ slot, sx, sy); ++ } ++ } ++} ++ ++void ++touch_handle_motion(struct touch *touch, uint32_t time, int32_t slot, ++ wl_fixed_t x, wl_fixed_t y) ++{ ++ struct point *point = find_point(touch, slot); ++ struct wl_resource *resource; ++ wl_fixed_t sx, sy, dx, dy; ++ ++ if (point) { ++ if (!point->view) { ++ return; ++ } ++ on_surface(point->view, x, y, &sx, &sy); ++ wl_resource_for_each(resource, &touch->resources) ++ { ++ if (wl_resource_get_client(resource) == point->client) { ++ wl_touch_send_motion(resource, time, slot, sx, sy); ++ } ++ } ++ return; ++ } ++ ++ if (touch->emulation == NOT_EMULATING || touch->slot != slot) { ++ return; ++ } ++ if (touch->emulation == WAITING) { ++ dx = x - touch->x; ++ dy = y - touch->y; ++ if (abs(wl_fixed_to_int(dx)) < SLOP_PX && ++ abs(wl_fixed_to_int(dy)) < SLOP_PX) { ++ return; ++ } ++ /* pressed where the finger went down, then dragged */ ++ wl_event_source_timer_update(touch->hold, 0); ++ pointer_handle_button(touch->pointer, time, BTN_LEFT, ++ WL_POINTER_BUTTON_STATE_PRESSED); ++ touch->emulation = DRAGGING; ++ } ++ if (touch->emulation == DRAGGING) { ++ pointer_handle_absolute_motion(touch->pointer, time, x, y); ++ } ++} ++ ++void ++touch_handle_up(struct touch *touch, uint32_t time, int32_t slot) ++{ ++ struct point *point = find_point(touch, slot); ++ struct wl_resource *resource; ++ uint32_t serial; ++ ++ if (point) { ++ serial = wl_display_next_serial(swc.display); ++ wl_resource_for_each(resource, &touch->resources) ++ { ++ if (wl_resource_get_client(resource) == point->client) { ++ wl_touch_send_up(resource, serial, time, slot); ++ /* an up may come with no frame after it */ ++ wl_touch_send_frame(resource); ++ } ++ } ++ point_release(point); ++ return; ++ } ++ ++ if (touch->emulation == NOT_EMULATING || touch->slot != slot) { ++ return; ++ } ++ wl_event_source_timer_update(touch->hold, 0); ++ if (touch->emulation == WAITING) { ++ click(touch, time, BTN_LEFT); ++ } else if (touch->emulation == DRAGGING) { ++ pointer_handle_button(touch->pointer, time, BTN_LEFT, ++ WL_POINTER_BUTTON_STATE_RELEASED); ++ } ++ touch->emulation = NOT_EMULATING; ++ pointer_handle_frame(touch->pointer); ++} ++ ++void ++touch_handle_frame(struct touch *touch) ++{ ++ struct wl_resource *resource; ++ size_t i; ++ ++ wl_resource_for_each(resource, &touch->resources) ++ { ++ for (i = 0; i < POINTS; ++i) { ++ if (touch->points[i].used && ++ touch->points[i].client == wl_resource_get_client(resource)) { ++ wl_touch_send_frame(resource); ++ break; ++ } ++ } ++ } ++ if (touch->emulation != NOT_EMULATING) { ++ pointer_handle_frame(touch->pointer); ++ } ++} ++ ++void ++touch_handle_cancel(struct touch *touch) ++{ ++ struct wl_resource *resource; ++ size_t i; ++ ++ wl_resource_for_each(resource, &touch->resources) ++ { ++ for (i = 0; i < POINTS; ++i) { ++ if (touch->points[i].used && ++ touch->points[i].client == wl_resource_get_client(resource)) { ++ wl_touch_send_cancel(resource); ++ break; ++ } ++ } ++ } ++ for (i = 0; i < POINTS; ++i) { ++ if (touch->points[i].used) { ++ point_release(&touch->points[i]); ++ } ++ } ++ if (touch->emulation == DRAGGING) { ++ pointer_handle_button(touch->pointer, touch->time, BTN_LEFT, ++ WL_POINTER_BUTTON_STATE_RELEASED); ++ pointer_handle_frame(touch->pointer); ++ } ++ wl_event_source_timer_update(touch->hold, 0); ++ touch->emulation = NOT_EMULATING; ++} ++ ++static const struct wl_touch_interface touch_impl = { ++ .release = destroy_resource, ++}; ++ ++bool ++touch_bind(struct touch *touch, struct wl_client *client, uint32_t version, ++ uint32_t id) ++{ ++ struct wl_resource *resource; ++ ++ resource = wl_resource_create(client, &wl_touch_interface, version, id); ++ if (!resource) { ++ return false; ++ } ++ wl_resource_set_implementation(resource, &touch_impl, touch, ++ &remove_resource); ++ wl_list_insert(&touch->resources, wl_resource_get_link(resource)); ++ return true; ++} ++ ++struct touch * ++touch_create(struct pointer *pointer) ++{ ++ struct touch *touch = calloc(1, sizeof(*touch)); ++ ++ if (!touch) { ++ return NULL; ++ } ++ touch->pointer = pointer; ++ wl_list_init(&touch->resources); ++ touch->hold = wl_event_loop_add_timer(swc.event_loop, &handle_hold, touch); ++ if (!touch->hold) { ++ free(touch); ++ return NULL; ++ } ++ return touch; ++} ++ ++void ++touch_destroy(struct touch *touch) ++{ ++ size_t i; ++ ++ for (i = 0; i < POINTS; ++i) { ++ if (touch->points[i].used) { ++ point_release(&touch->points[i]); ++ } ++ } ++ wl_event_source_remove(touch->hold); ++ free(touch); ++} +--- /dev/null ++++ b/libswc/touch.h +@@ -0,0 +1,41 @@ ++/* swc: libswc/touch.h ++ * ++ * touchscreens: wl_touch for the clients that bind it, and the first finger ++ * as the pointer for everything else, the window decorations included ++ */ ++ ++#ifndef SWC_TOUCH_H ++#define SWC_TOUCH_H ++ ++#include <stdbool.h> ++#include <stdint.h> ++#include <wayland-server.h> ++ ++struct pointer; ++struct touch; ++ ++struct touch * ++touch_create(struct pointer *pointer); ++void ++touch_destroy(struct touch *touch); ++ ++bool ++touch_bind(struct touch *touch, struct wl_client *client, uint32_t version, ++ uint32_t id); ++ ++/* slot tells fingers apart while they are down, x and y are in the global ++ * space. a frame ends what happened at one time */ ++void ++touch_handle_down(struct touch *touch, uint32_t time, int32_t slot, ++ wl_fixed_t x, wl_fixed_t y); ++void ++touch_handle_motion(struct touch *touch, uint32_t time, int32_t slot, ++ wl_fixed_t x, wl_fixed_t y); ++void ++touch_handle_up(struct touch *touch, uint32_t time, int32_t slot); ++void ++touch_handle_frame(struct touch *touch); ++void ++touch_handle_cancel(struct touch *touch); ++ ++#endif diff --git a/patches/warnings.patch b/patches/warnings.patch @@ -0,0 +1,14 @@ +diff --git a/libswc/decor.c b/libswc/decor.c +index c402933..ad8edf5 100644 +--- a/libswc/decor.c ++++ b/libswc/decor.c +@@ -330,7 +330,8 @@ get_tiled_decor_buffer(struct decor_part_buffer *part, uint32_t width, + buffer_height = (height + 255) & ~255U; + } + +- if (buffer_width > SIZE_MAX / 4) { ++ /* in 64 bits, so the check means something where size_t has 32 */ ++ if ((uint64_t)buffer_width * 4 > SIZE_MAX) { + return NULL; + } + row_size = (size_t)buffer_width * 4;