aboutsummaryrefslogtreecommitdiff
path: root/wayland.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2023-06-29 13:38:42 +0000
committerDaniel Eklöf <daniel@ekloef.se>2023-06-29 13:38:42 +0000
commita44a0b4ebe728625ca39c741b5c8fbfd48f9fe9f (patch)
tree520900e18bca59dccd345bf0c9d4af45a67e5233 /wayland.c
parentcorrect csd section entry: hide-when-maximized (diff)
parentwayland: surface_scale(): reset buffer scale when using fractional scaling (diff)
Merge branch 'fractional-scaling'
Diffstat (limited to 'wayland.c')
-rw-r--r--wayland.c311
1 files changed, 245 insertions, 66 deletions
diff --git a/wayland.c b/wayland.c
index 51161cf0..c9693200 100644
--- a/wayland.c
+++ b/wayland.c
@@ -32,12 +32,12 @@
#include "xmalloc.h"
static void
-csd_reload_font(struct wl_window *win, int old_scale)
+csd_reload_font(struct wl_window *win, float old_scale)
{
struct terminal *term = win->term;
const struct config *conf = term->conf;
- const int scale = term->scale;
+ const float scale = term->scale;
bool enable_csd = win->csd_mode == CSD_YES && !win->is_fullscreen;
if (!enable_csd)
@@ -52,10 +52,10 @@ csd_reload_font(struct wl_window *win, int old_scale)
patterns[i] = conf->csd.font.arr[i].pattern;
char pixelsize[32];
- snprintf(pixelsize, sizeof(pixelsize),
- "pixelsize=%u", conf->csd.title_height * scale * 1 / 2);
+ snprintf(pixelsize, sizeof(pixelsize), "pixelsize=%u",
+ (int)round(conf->csd.title_height * scale * 1 / 2));
- LOG_DBG("loading CSD font \"%s:%s\" (old-scale=%d, scale=%d)",
+ LOG_DBG("loading CSD font \"%s:%s\" (old-scale=%.2f, scale=%.2f)",
patterns[0], pixelsize, old_scale, scale);
win->csd.font = fcft_from_name(conf->csd.font.count, patterns, pixelsize);
@@ -74,12 +74,12 @@ csd_instantiate(struct wl_window *win)
for (size_t i = CSD_SURF_MINIMIZE; i < CSD_SURF_COUNT; i++) {
bool ret = wayl_win_subsurface_new_with_custom_parent(
- win, win->csd.surface[CSD_SURF_TITLE].surf, &win->csd.surface[i],
+ win, win->csd.surface[CSD_SURF_TITLE].surface.surf, &win->csd.surface[i],
true);
xassert(ret);
}
- csd_reload_font(win, -1);
+ csd_reload_font(win, -1.);
}
static void
@@ -187,8 +187,12 @@ seat_destroy(struct seat *seat)
if (seat->pointer.theme != NULL)
wl_cursor_theme_destroy(seat->pointer.theme);
- if (seat->pointer.surface != NULL)
- wl_surface_destroy(seat->pointer.surface);
+ if (seat->pointer.surface.surf != NULL)
+ wl_surface_destroy(seat->pointer.surface.surf);
+#if defined(HAVE_FRACTIONAL_SCALE)
+ if (seat->pointer.surface.viewport != NULL)
+ wp_viewport_destroy(seat->pointer.surface.viewport);
+#endif
if (seat->pointer.xcursor_callback != NULL)
wl_callback_destroy(seat->pointer.xcursor_callback);
@@ -288,27 +292,46 @@ seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
if (caps & WL_SEAT_CAPABILITY_POINTER) {
if (seat->wl_pointer == NULL) {
- xassert(seat->pointer.surface == NULL);
- seat->pointer.surface = wl_compositor_create_surface(seat->wayl->compositor);
+ xassert(seat->pointer.surface.surf == NULL);
+ seat->pointer.surface.surf =
+ wl_compositor_create_surface(seat->wayl->compositor);
- if (seat->pointer.surface == NULL) {
+ if (seat->pointer.surface.surf == NULL) {
LOG_ERR("%s: failed to create pointer surface", seat->name);
return;
}
+#if defined(HAVE_FRACTIONAL_SCALE)
+ xassert(seat->pointer.surface.viewport == NULL);
+ seat->pointer.surface.viewport = wp_viewporter_get_viewport(
+ seat->wayl->viewporter, seat->pointer.surface.surf);
+
+ if (seat->pointer.surface.viewport == NULL) {
+ LOG_ERR("%s: failed to create pointer viewport", seat->name);
+ wl_surface_destroy(seat->pointer.surface.surf);
+ seat->pointer.surface.surf = NULL;
+ return;
+ }
+#endif
+
seat->wl_pointer = wl_seat_get_pointer(wl_seat);
wl_pointer_add_listener(seat->wl_pointer, &pointer_listener, seat);
}
} else {
if (seat->wl_pointer != NULL) {
wl_pointer_release(seat->wl_pointer);
- wl_surface_destroy(seat->pointer.surface);
+ wl_surface_destroy(seat->pointer.surface.surf);
+
+#if defined(HAVE_FRACTIONAL_SCALE)
+ wp_viewport_destroy(seat->pointer.surface.viewport);
+ seat->pointer.surface.viewport = NULL;
+#endif
if (seat->pointer.theme != NULL)
wl_cursor_theme_destroy(seat->pointer.theme);
seat->wl_pointer = NULL;
- seat->pointer.surface = NULL;
+ seat->pointer.surface.surf = NULL;
seat->pointer.theme = NULL;
seat->pointer.cursor = NULL;
}
@@ -334,7 +357,7 @@ update_term_for_output_change(struct terminal *term)
if (tll_length(term->window->on_outputs) == 0)
return;
- int old_scale = term->scale;
+ float old_scale = term->scale;
render_resize(term, term->width / term->scale, term->height / term->scale);
term_font_dpi_changed(term, old_scale);
@@ -350,11 +373,6 @@ update_terms_on_monitor(struct monitor *mon)
tll_foreach(wayl->terms, it) {
struct terminal *term = it->item;
- if (term->conf->dpi_aware == DPI_AWARE_AUTO) {
- update_term_for_output_change(term);
- continue;
- }
-
tll_foreach(term->window->on_outputs, it2) {
if (it2->item == mon) {
update_term_for_output_change(term);
@@ -848,7 +866,7 @@ xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
* anytime soon. Some compositors require a commit in
* combination with an ack - make them happy.
*/
- wl_surface_commit(win->surface);
+ wl_surface_commit(win->surface.surf);
}
if (wasnt_configured)
@@ -1121,6 +1139,27 @@ handle_global(void *data, struct wl_registry *registry,
}
#endif
+#if defined(HAVE_FRACTIONAL_SCALE)
+ else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
+ const uint32_t required = 1;
+ if (!verify_iface_version(interface, version, required))
+ return;
+
+ wayl->viewporter = wl_registry_bind(
+ wayl->registry, name, &wp_viewporter_interface, required);
+ }
+
+ else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
+ const uint32_t required = 1;
+ if (!verify_iface_version(interface, version, required))
+ return;
+
+ wayl->fractional_scale_manager = wl_registry_bind(
+ wayl->registry, name,
+ &wp_fractional_scale_manager_v1_interface, required);
+ }
+#endif
+
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
else if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) {
const uint32_t required = 1;
@@ -1204,7 +1243,7 @@ handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
if (seat->wl_keyboard != NULL)
keyboard_listener.leave(
- seat, seat->wl_keyboard, -1, seat->kbd_focus->window->surface);
+ seat, seat->wl_keyboard, -1, seat->kbd_focus->window->surface.surf);
}
if (seat->mouse_focus != NULL) {
@@ -1214,7 +1253,7 @@ handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
if (seat->wl_pointer != NULL)
pointer_listener.leave(
- seat, seat->wl_pointer, -1, seat->mouse_focus->window->surface);
+ seat, seat->wl_pointer, -1, seat->mouse_focus->window->surface.surf);
}
seat_destroy(seat);
@@ -1347,6 +1386,14 @@ wayl_init(struct fdm *fdm, struct key_binding_manager *key_binding_manager,
"bell.urgent will fall back to coloring the window margins red");
}
+#if defined(HAVE_FRACTIONAL_SCALE)
+ if (wayl->fractional_scale_manager == NULL || wayl->viewporter == NULL) {
+#else
+ if (true) {
+#endif
+ LOG_WARN("fractional scaling not available");
+ }
+
if (presentation_timings && wayl->presentation == NULL) {
LOG_ERR("presentation time interface not implemented by compositor");
goto out;
@@ -1435,6 +1482,12 @@ wayl_destroy(struct wayland *wayl)
zwp_text_input_manager_v3_destroy(wayl->text_input_manager);
#endif
+#if defined(HAVE_FRACTIONAL_SCALE)
+ if (wayl->fractional_scale_manager != NULL)
+ wp_fractional_scale_manager_v1_destroy(wayl->fractional_scale_manager);
+ if (wayl->viewporter != NULL)
+ wp_viewporter_destroy(wayl->viewporter);
+#endif
#if defined(HAVE_XDG_ACTIVATION)
if (wayl->xdg_activation != NULL)
xdg_activation_v1_destroy(wayl->xdg_activation);
@@ -1469,6 +1522,24 @@ wayl_destroy(struct wayland *wayl)
free(wayl);
}
+#if defined(HAVE_FRACTIONAL_SCALE)
+static void fractional_scale_preferred_scale(
+ void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1,
+ uint32_t scale)
+{
+ struct wl_window *win = data;
+ win->scale = (float)scale / 120.;
+ win->have_preferred_scale = true;
+
+ LOG_DBG("fractional scale: %.3f", win->scale);
+ update_term_for_output_change(win->term);
+}
+
+static const struct wp_fractional_scale_v1_listener fractional_scale_listener = {
+ .preferred_scale = &fractional_scale_preferred_scale,
+};
+#endif
+
struct wl_window *
wayl_win_init(struct terminal *term, const char *token)
{
@@ -1485,21 +1556,34 @@ wayl_win_init(struct terminal *term, const char *token)
win->csd_mode = CSD_UNKNOWN;
win->csd.move_timeout_fd = -1;
win->resize_timeout_fd = -1;
+ win->scale = -1.;
win->wm_capabilities.maximize = true;
win->wm_capabilities.minimize = true;
- win->surface = wl_compositor_create_surface(wayl->compositor);
- if (win->surface == NULL) {
+ win->surface.surf = wl_compositor_create_surface(wayl->compositor);
+ if (win->surface.surf == NULL) {
LOG_ERR("failed to create wayland surface");
goto out;
}
wayl_win_alpha_changed(win);
- wl_surface_add_listener(win->surface, &surface_listener, win);
+ wl_surface_add_listener(win->surface.surf, &surface_listener, win);
- win->xdg_surface = xdg_wm_base_get_xdg_surface(wayl->shell, win->surface);
+#if defined(HAVE_FRACTIONAL_SCALE)
+ if (wayl->fractional_scale_manager != NULL && wayl->viewporter != NULL) {
+ win->surface.viewport = wp_viewporter_get_viewport(wayl->viewporter, win->surface.surf);
+
+ win->fractional_scale =
+ wp_fractional_scale_manager_v1_get_fractional_scale(
+ wayl->fractional_scale_manager, win->surface.surf);
+ wp_fractional_scale_v1_add_listener(
+ win->fractional_scale, &fractional_scale_listener, win);
+ }
+#endif
+
+ win->xdg_surface = xdg_wm_base_get_xdg_surface(wayl->shell, win->surface.surf);
xdg_surface_add_listener(win->xdg_surface, &xdg_surface_listener, win);
win->xdg_toplevel = xdg_surface_get_toplevel(win->xdg_surface);
@@ -1532,12 +1616,12 @@ wayl_win_init(struct terminal *term, const char *token)
LOG_WARN("no decoration manager available - using CSDs unconditionally");
}
- wl_surface_commit(win->surface);
+ wl_surface_commit(win->surface.surf);
#if defined(HAVE_XDG_ACTIVATION)
/* Complete XDG startup notification */
if (token)
- xdg_activation_v1_activate(wayl->xdg_activation, token, win->surface);
+ xdg_activation_v1_activate(wayl->xdg_activation, token, win->surface.surf);
#endif
if (!wayl_win_subsurface_new(win, &win->overlay, false)) {
@@ -1587,33 +1671,33 @@ wayl_win_destroy(struct wl_window *win)
* nor mouse focus).
*/
- if (win->render_timer.surf != NULL) {
- wl_surface_attach(win->render_timer.surf, NULL, 0, 0);
- wl_surface_commit(win->render_timer.surf);
+ if (win->render_timer.surface.surf != NULL) {
+ wl_surface_attach(win->render_timer.surface.surf, NULL, 0, 0);
+ wl_surface_commit(win->render_timer.surface.surf);
}
- if (win->scrollback_indicator.surf != NULL) {
- wl_surface_attach(win->scrollback_indicator.surf, NULL, 0, 0);
- wl_surface_commit(win->scrollback_indicator.surf);
+ if (win->scrollback_indicator.surface.surf != NULL) {
+ wl_surface_attach(win->scrollback_indicator.surface.surf, NULL, 0, 0);
+ wl_surface_commit(win->scrollback_indicator.surface.surf);
}
/* Scrollback search */
- if (win->search.surf != NULL) {
- wl_surface_attach(win->search.surf, NULL, 0, 0);
- wl_surface_commit(win->search.surf);
+ if (win->search.surface.surf != NULL) {
+ wl_surface_attach(win->search.surface.surf, NULL, 0, 0);
+ wl_surface_commit(win->search.surface.surf);
}
/* URLs */
tll_foreach(win->urls, it) {
- wl_surface_attach(it->item.surf.surf, NULL, 0, 0);
- wl_surface_commit(it->item.surf.surf);
+ wl_surface_attach(it->item.surf.surface.surf, NULL, 0, 0);
+ wl_surface_commit(it->item.surf.surface.surf);
}
/* CSD */
for (size_t i = 0; i < ALEN(win->csd.surface); i++) {
- if (win->csd.surface[i].surf != NULL) {
- wl_surface_attach(win->csd.surface[i].surf, NULL, 0, 0);
- wl_surface_commit(win->csd.surface[i].surf);
+ if (win->csd.surface[i].surface.surf != NULL) {
+ wl_surface_attach(win->csd.surface[i].surface.surf, NULL, 0, 0);
+ wl_surface_commit(win->csd.surface[i].surface.surf);
}
}
@@ -1621,8 +1705,8 @@ wayl_win_destroy(struct wl_window *win)
/* Main window */
win->unmapped = true;
- wl_surface_attach(win->surface, NULL, 0, 0);
- wl_surface_commit(win->surface);
+ wl_surface_attach(win->surface.surf, NULL, 0, 0);
+ wl_surface_commit(win->surface.surf);
wayl_roundtrip(win->term->wl);
tll_free(win->on_outputs);
@@ -1653,6 +1737,12 @@ wayl_win_destroy(struct wl_window *win)
tll_remove(win->xdg_tokens, it);
}
#endif
+#if defined(HAVE_FRACTIONAL_SCALE)
+ if (win->fractional_scale != NULL)
+ wp_fractional_scale_v1_destroy(win->fractional_scale);
+ if (win->surface.viewport != NULL)
+ wp_viewport_destroy(win->surface.viewport);
+#endif
if (win->frame_callback != NULL)
wl_callback_destroy(win->frame_callback);
if (win->xdg_toplevel_decoration != NULL)
@@ -1661,8 +1751,8 @@ wayl_win_destroy(struct wl_window *win)
xdg_toplevel_destroy(win->xdg_toplevel);
if (win->xdg_surface != NULL)
xdg_surface_destroy(win->xdg_surface);
- if (win->surface != NULL)
- wl_surface_destroy(win->surface);
+ if (win->surface.surf != NULL)
+ wl_surface_destroy(win->surface.surf);
wayl_roundtrip(win->term->wl);
@@ -1672,7 +1762,7 @@ wayl_win_destroy(struct wl_window *win)
}
bool
-wayl_reload_xcursor_theme(struct seat *seat, int new_scale)
+wayl_reload_xcursor_theme(struct seat *seat, float new_scale)
{
if (seat->pointer.theme != NULL && seat->pointer.scale == new_scale) {
/* We already have a theme loaded, and the scale hasn't changed */
@@ -1705,7 +1795,7 @@ wayl_reload_xcursor_theme(struct seat *seat, int new_scale)
const char *xcursor_theme = getenv("XCURSOR_THEME");
- LOG_INFO("cursor theme: %s, size: %d, scale: %d",
+ LOG_INFO("cursor theme: %s, size: %d, scale: %.2f",
xcursor_theme ? xcursor_theme : "(null)",
xcursor_size, new_scale);
@@ -1789,6 +1879,68 @@ wayl_roundtrip(struct wayland *wayl)
wayl_flush(wayl);
}
+
+bool
+wayl_fractional_scaling(const struct wayland *wayl)
+{
+#if defined(HAVE_FRACTIONAL_SCALE)
+ return wayl->fractional_scale_manager != NULL;
+#else
+ return false;
+#endif
+}
+
+void
+wayl_surface_scale_explicit_width_height(
+ const struct wl_window *win, const struct wayl_surface *surf,
+ int width, int height, float scale)
+{
+
+ if (wayl_fractional_scaling(win->term->wl) && win->have_preferred_scale) {
+#if defined(HAVE_FRACTIONAL_SCALE)
+ LOG_DBG("scaling by a factor of %.2f using fractional scaling "
+ "(width=%d, height=%d) ", scale, width, height);
+
+ wl_surface_set_buffer_scale(surf->surf, 1);
+ wp_viewport_set_destination(
+ surf->viewport,
+ round((float)width / scale),
+ round((float)height / scale));
+#else
+ BUG("wayl_fraction_scaling() returned true, "
+ "but fractional scaling was not available at compile time").
+#endif
+ } else {
+ LOG_DBG("scaling by a factor of %.2f using legacy mode "
+ "(width=%d, height=%d)", scale, width, height);
+
+ xassert(scale == floor(scale));
+
+ const int iscale = (int)scale;
+ xassert(width % iscale == 0);
+ xassert(height % iscale == 0);
+
+ wl_surface_set_buffer_scale(surf->surf, iscale);
+ }
+}
+
+void
+wayl_surface_scale(const struct wl_window *win, const struct wayl_surface *surf,
+ const struct buffer *buf, float scale)
+{
+ wayl_surface_scale_explicit_width_height(
+ win, surf, buf->width, buf->height, scale);
+}
+
+void
+wayl_win_scale(struct wl_window *win, const struct buffer *buf)
+{
+ const struct terminal *term = win->term;
+ const float scale = term->scale;
+
+ wayl_surface_scale(win, &win->surface, buf, scale);
+}
+
void
wayl_win_alpha_changed(struct wl_window *win)
{
@@ -1800,11 +1952,11 @@ wayl_win_alpha_changed(struct wl_window *win)
if (region != NULL) {
wl_region_add(region, 0, 0, INT32_MAX, INT32_MAX);
- wl_surface_set_opaque_region(win->surface, region);
+ wl_surface_set_opaque_region(win->surface.surf, region);
wl_region_destroy(region);
}
} else
- wl_surface_set_opaque_region(win->surface, NULL);
+ wl_surface_set_opaque_region(win->surface.surf, NULL);
}
#if defined(HAVE_XDG_ACTIVATION)
@@ -1815,7 +1967,7 @@ activation_token_for_urgency_done(const char *token, void *data)
struct wayland *wayl = win->term->wl;
win->urgency_token_is_pending = false;
- xdg_activation_v1_activate(wayl->xdg_activation, token, win->surface);
+ xdg_activation_v1_activate(wayl->xdg_activation, token, win->surface.surf);
}
#endif /* HAVE_XDG_ACTIVATION */
@@ -1860,27 +2012,43 @@ wayl_win_csd_borders_visible(const struct wl_window *win)
bool
wayl_win_subsurface_new_with_custom_parent(
struct wl_window *win, struct wl_surface *parent,
- struct wl_surf_subsurf *surf, bool allow_pointer_input)
+ struct wayl_sub_surface *surf, bool allow_pointer_input)
{
struct wayland *wayl = win->term->wl;
- surf->surf = NULL;
+ surf->surface.surf = NULL;
surf->sub = NULL;
struct wl_surface *main_surface
= wl_compositor_create_surface(wayl->compositor);
- if (main_surface == NULL)
+ if (main_surface == NULL) {
+ LOG_ERR("failed to instantiate surface for sub-surface");
return false;
+ }
struct wl_subsurface *sub = wl_subcompositor_get_subsurface(
wayl->sub_compositor, main_surface, parent);
if (sub == NULL) {
+ LOG_ERR("failed to instantiate sub-surface");
wl_surface_destroy(main_surface);
return false;
}
+#if defined(HAVE_FRACTIONAL_SCALE)
+ struct wp_viewport *viewport = NULL;
+ if (wayl->fractional_scale_manager != NULL && wayl->viewporter != NULL) {
+ viewport = wp_viewporter_get_viewport(wayl->viewporter, main_surface);
+ if (viewport == NULL) {
+ LOG_ERR("failed to instantiate viewport for sub-surface");
+ wl_subsurface_destroy(sub);
+ wl_surface_destroy(main_surface);
+ return false;
+ }
+ }
+#endif
+
wl_surface_set_user_data(main_surface, win);
wl_subsurface_set_sync(sub);
@@ -1892,31 +2060,42 @@ wayl_win_subsurface_new_with_custom_parent(
wl_region_destroy(empty);
}
- surf->surf = main_surface;
+ surf->surface.surf = main_surface;
surf->sub = sub;
+#if defined(HAVE_FRACTIONAL_SCALE)
+ surf->surface.viewport = viewport;
+#endif
return true;
}
bool
-wayl_win_subsurface_new(struct wl_window *win, struct wl_surf_subsurf *surf,
+wayl_win_subsurface_new(struct wl_window *win, struct wayl_sub_surface *surf,
bool allow_pointer_input)
{
return wayl_win_subsurface_new_with_custom_parent(
- win, win->surface, surf, allow_pointer_input);
+ win, win->surface.surf, surf, allow_pointer_input);
}
void
-wayl_win_subsurface_destroy(struct wl_surf_subsurf *surf)
+wayl_win_subsurface_destroy(struct wayl_sub_surface *surf)
{
if (surf == NULL)
return;
- if (surf->sub != NULL)
- wl_subsurface_destroy(surf->sub);
- if (surf->surf != NULL)
- wl_surface_destroy(surf->surf);
- surf->surf = NULL;
- surf->sub = NULL;
+#if defined(HAVE_FRACTIONAL_SCALE)
+ if (surf->surface.viewport != NULL) {
+ wp_viewport_destroy(surf->surface.viewport);
+ surf->surface.viewport = NULL;
+ }
+#endif
+ if (surf->sub != NULL) {
+ wl_subsurface_destroy(surf->sub);
+ surf->sub = NULL;
+ }
+ if (surf->surface.surf != NULL) {
+ wl_surface_destroy(surf->surface.surf);
+ surf->surface.surf = NULL;
+ }
}
#if defined(HAVE_XDG_ACTIVATION)
@@ -1981,7 +2160,7 @@ wayl_get_activation_token(
if (seat != NULL && serial != 0)
xdg_activation_token_v1_set_serial(token, serial, seat->wl_seat);
- xdg_activation_token_v1_set_surface(token, win->surface);
+ xdg_activation_token_v1_set_surface(token, win->surface.surf);
xdg_activation_token_v1_add_listener(token, &activation_token_listener, ctx);
xdg_activation_token_v1_commit(token);
return true;