1From 48c41795fe5f02f7f4051b0e0849e36a2fb8edee Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Fri, 23 Sep 2022 17:24:12 +0800
4Subject: [PATCH 77/79] HACK: Honour cursor-size config
5
6By scaling the cursor surface.
7
8Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
9---
10 compositor/main.c             |  4 ++++
11 include/libweston/libweston.h |  4 ++++
12 libweston/backend-drm/drm.c   |  3 +++
13 libweston/compositor.c        | 36 +++++++++++++++++++++-------
14 libweston/input.c             | 44 +++++++++++++++++++++++++++++++----
15 5 files changed, 79 insertions(+), 12 deletions(-)
16
17diff --git a/compositor/main.c b/compositor/main.c
18index 558537e..039307b 100644
19--- a/compositor/main.c
20+++ b/compositor/main.c
21@@ -3542,6 +3542,10 @@ wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
22 	wet.compositor->exit = handle_exit;
23 	wet.compositor->warm_up = warm_up;
24
25+	section = weston_config_get_section(config, "shell", NULL, NULL);
26+	weston_config_section_get_int(section, "cursor-size",
27+				      &wet.compositor->cursor_size, 0);
28+
29 	weston_compositor_log_capabilities(wet.compositor);
30
31 	server_socket = getenv("WAYLAND_SERVER_SOCKET");
32diff --git a/include/libweston/libweston.h b/include/libweston/libweston.h
33index 26804e2..2d15924 100644
34--- a/include/libweston/libweston.h
35+++ b/include/libweston/libweston.h
36@@ -578,6 +578,8 @@ struct weston_pointer {
37 	struct wl_listener output_destroy_listener;
38
39 	struct wl_list timestamps_list;
40+
41+	float scale;
42 };
43
44 /** libinput style calibration matrix
45@@ -1210,6 +1212,8 @@ struct weston_compositor {
46
47 	bool warm_up;
48 	uint32_t pending_fade_out;
49+
50+	int cursor_size;
51 };
52
53 struct weston_buffer {
54diff --git a/libweston/backend-drm/drm.c b/libweston/backend-drm/drm.c
55index ed28956..d632a27 100644
56--- a/libweston/backend-drm/drm.c
57+++ b/libweston/backend-drm/drm.c
58@@ -4226,6 +4226,9 @@ drm_backend_create(struct weston_compositor *compositor,
59 		goto err_udev_dev;
60 	}
61
62+	if (compositor->cursor_size)
63+		b->cursor_width = b->cursor_height = compositor->cursor_size;
64+
65 	wl_list_init(&b->plane_list);
66 	create_sprites(b);
67
68diff --git a/libweston/compositor.c b/libweston/compositor.c
69index 79a72bd..6e917f3 100644
70--- a/libweston/compositor.c
71+++ b/libweston/compositor.c
72@@ -757,8 +757,14 @@ weston_transformed_coord(int width, int height,
73 		break;
74 	}
75
76-	*bx *= scale;
77-	*by *= scale;
78+	/* HACK: Use -scale as 1/scale */
79+	if (scale < 0) {
80+		*bx /= -scale;
81+		*by /= -scale;
82+	} else {
83+		*bx *= scale;
84+		*by *= scale;
85+	}
86 }
87
88 /** Transform a rectangle to buffer coordinates
89@@ -2143,22 +2149,31 @@ convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
90 				uint32_t transform,
91 				int32_t scale)
92 {
93-	assert(scale > 0);
94+	assert(scale);
95+
96+	/* HACK: Use -scale as 1/scale */
97+	if (scale < 0) {
98+		width *= -scale;
99+		height *= -scale;
100+	} else {
101+		width /= scale;
102+		height /= scale;
103+	}
104
105 	switch (transform) {
106 	case WL_OUTPUT_TRANSFORM_NORMAL:
107 	case WL_OUTPUT_TRANSFORM_180:
108 	case WL_OUTPUT_TRANSFORM_FLIPPED:
109 	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
110-		*width_out = width / scale;
111-		*height_out = height / scale;
112+		*width_out = width;
113+		*height_out = height;
114 		break;
115 	case WL_OUTPUT_TRANSFORM_90:
116 	case WL_OUTPUT_TRANSFORM_270:
117 	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
118 	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
119-		*width_out = height / scale;
120-		*height_out = width / scale;
121+		*width_out = height;
122+		*height_out = width;
123 		break;
124 	default:
125 		assert(0 && "invalid transform");
126@@ -3734,6 +3749,7 @@ weston_surface_build_buffer_matrix(const struct weston_surface *surface,
127 {
128 	const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
129 	double src_width, src_height, dest_width, dest_height;
130+	float scale = vp->buffer.scale;
131
132 	weston_matrix_init(matrix);
133
134@@ -3801,7 +3817,11 @@ weston_surface_build_buffer_matrix(const struct weston_surface *surface,
135 		break;
136 	}
137
138-	weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
139+	/* HACK: Use -scale as 1/scale */
140+	if (scale < 0)
141+		scale = 1.0 / -scale;
142+
143+	weston_matrix_scale(matrix, scale, scale, 1);
144 }
145
146 /**
147diff --git a/libweston/input.c b/libweston/input.c
148index 6438b52..9c140aa 100644
149--- a/libweston/input.c
150+++ b/libweston/input.c
151@@ -1723,8 +1723,8 @@ weston_pointer_move_to(struct weston_pointer *pointer,
152
153 	if (pointer->sprite) {
154 		weston_view_set_position(pointer->sprite,
155-					 ix - pointer->hotspot_x,
156-					 iy - pointer->hotspot_y);
157+					 ix - pointer->hotspot_x * pointer->scale,
158+					 iy - pointer->hotspot_y * pointer->scale);
159 		weston_view_schedule_repaint(pointer->sprite);
160 	}
161
162@@ -2677,6 +2677,36 @@ pointer_cursor_surface_get_label(struct weston_surface *surface,
163 	return snprintf(buf, len, "cursor");
164 }
165
166+static void
167+pointer_cursor_scale(struct weston_pointer *pointer,
168+		     struct weston_surface *surface)
169+{
170+	struct weston_compositor *compositor = surface->compositor;
171+	float scale;
172+
173+	if (!compositor->cursor_size || !surface->width ||
174+	    surface->width == compositor->cursor_size)
175+		return;
176+
177+	if (surface->buffer_viewport.buffer.scale != 1)
178+		return;
179+
180+	if (compositor->cursor_size > surface->width) {
181+		scale = compositor->cursor_size / surface->width;
182+
183+		/* HACK: Use -scale as 1/scale */
184+		surface->buffer_viewport.buffer.scale = -scale;
185+	} else {
186+		scale = 1.0 / (surface->width / compositor->cursor_size);
187+		surface->buffer_viewport.buffer.scale = 1 / scale;
188+	}
189+
190+	surface->width *= scale;
191+	surface->height *= scale;
192+
193+	pointer->scale = scale;
194+}
195+
196 static void
197 pointer_cursor_surface_committed(struct weston_surface *es,
198 				 int32_t dx, int32_t dy)
199@@ -2689,11 +2719,13 @@ pointer_cursor_surface_committed(struct weston_surface *es,
200
201 	assert(es == pointer->sprite->surface);
202
203+	pointer_cursor_scale(pointer, es);
204+
205 	pointer->hotspot_x -= dx;
206 	pointer->hotspot_y -= dy;
207
208-	x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
209-	y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
210+	x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x * pointer->scale;
211+	y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y * pointer->scale;
212
213 	weston_view_set_position(pointer->sprite, x, y);
214
215@@ -2764,6 +2796,8 @@ pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
216 		pointer->sprite = weston_view_create(surface);
217 	}
218
219+	pointer_cursor_scale(pointer, surface);
220+
221 	pointer->hotspot_x = x;
222 	pointer->hotspot_y = y;
223
224@@ -3374,6 +3408,8 @@ weston_seat_init_pointer(struct weston_seat *seat)
225
226 	seat_send_updated_caps(seat);
227
228+	pointer->scale = 1.0;
229+
230 	return 0;
231 }
232
233--
2342.20.1
235
236