1 /*
2 * Copyright © 2014 Intel Corporation
3 * Copyright © 2011 Kristian Høgsberg
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without
7 * fee, provided that the above copyright notice appear in all copies
8 * and that both that copyright notice and this permission notice
9 * appear in supporting documentation, and that the name of the
10 * copyright holders not be used in advertising or publicity
11 * pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no
13 * representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied
15 * warranty.
16 *
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 * SOFTWARE.
25 */
26
27 #include <xwayland-config.h>
28
29 #include "scrnintstr.h"
30 #include "servermd.h"
31 #include "cursorstr.h"
32 #include "inputstr.h"
33 #include "mipointer.h"
34
35 #include "xwayland-cursor.h"
36 #include "xwayland-input.h"
37 #include "xwayland-pixmap.h"
38 #include "xwayland-screen.h"
39 #include "xwayland-shm.h"
40 #include "xwayland-types.h"
41
42 #include "tablet-unstable-v2-client-protocol.h"
43
44 #define DELAYED_X_CURSOR_TIMEOUT 5 /* ms */
45
46 static DevPrivateKeyRec xwl_cursor_private_key;
47
48 static void
expand_source_and_mask(CursorPtr cursor,CARD32 * data)49 expand_source_and_mask(CursorPtr cursor, CARD32 *data)
50 {
51 CARD32 *p, d, fg, bg;
52 CursorBitsPtr bits = cursor->bits;
53 int x, y, stride, i, bit;
54
55 p = data;
56 fg = ((cursor->foreRed & 0xff00) << 8) |
57 (cursor->foreGreen & 0xff00) |
58 (cursor->foreBlue >> 8);
59 bg = ((cursor->backRed & 0xff00) << 8) |
60 (cursor->backGreen & 0xff00) |
61 (cursor->backBlue >> 8);
62 stride = BitmapBytePad(bits->width);
63 for (y = 0; y < bits->height; y++)
64 for (x = 0; x < bits->width; x++) {
65 i = y * stride + x / 8;
66 bit = 1 << (x & 7);
67 if (bits->source[i] & bit)
68 d = fg;
69 else
70 d = bg;
71 if (bits->mask[i] & bit)
72 d |= 0xff000000;
73 else
74 d = 0x00000000;
75
76 *p++ = d;
77 }
78 }
79
80 static Bool
xwl_realize_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor)81 xwl_realize_cursor(DeviceIntPtr device, ScreenPtr screen, CursorPtr cursor)
82 {
83 PixmapPtr pixmap;
84
85 pixmap = xwl_shm_create_pixmap(screen, cursor->bits->width,
86 cursor->bits->height, 32,
87 CREATE_PIXMAP_USAGE_BACKING_PIXMAP);
88 dixSetPrivate(&cursor->devPrivates, &xwl_cursor_private_key, pixmap);
89
90 return TRUE;
91 }
92
93 static Bool
xwl_unrealize_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor)94 xwl_unrealize_cursor(DeviceIntPtr device, ScreenPtr screen, CursorPtr cursor)
95 {
96 PixmapPtr pixmap;
97 struct xwl_screen *xwl_screen;
98 struct xwl_seat *xwl_seat;
99
100 pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
101 if (!pixmap)
102 return TRUE;
103
104 dixSetPrivate(&cursor->devPrivates, &xwl_cursor_private_key, NULL);
105
106 /* When called from FreeCursor(), device is always NULL */
107 xwl_screen = xwl_screen_get(screen);
108 xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
109 if (cursor == xwl_seat->x_cursor)
110 xwl_seat->x_cursor = NULL;
111 }
112
113 return xwl_shm_destroy_pixmap(pixmap);
114 }
115
116 static void
frame_callback(void * data,struct wl_callback * callback,uint32_t time)117 frame_callback(void *data,
118 struct wl_callback *callback,
119 uint32_t time)
120 {
121 struct xwl_cursor *xwl_cursor = data;
122
123 xwl_cursor_clear_frame_cb(xwl_cursor);
124 if (xwl_cursor->needs_update) {
125 xwl_cursor->needs_update = FALSE;
126 xwl_cursor->update_proc(xwl_cursor);
127 }
128 }
129
130 static const struct wl_callback_listener frame_listener = {
131 frame_callback
132 };
133
134 static void
xwl_cursor_buffer_release_callback(void * data)135 xwl_cursor_buffer_release_callback(void *data)
136 {
137 /* drop the reference we took in set_cursor */
138 xwl_shm_destroy_pixmap(data);
139 }
140
141 static void
xwl_cursor_copy_bits_to_pixmap(CursorPtr cursor,PixmapPtr pixmap)142 xwl_cursor_copy_bits_to_pixmap(CursorPtr cursor, PixmapPtr pixmap)
143 {
144 int stride;
145
146 stride = cursor->bits->width * 4;
147 if (cursor->bits->argb)
148 memcpy(pixmap->devPrivate.ptr,
149 cursor->bits->argb, cursor->bits->height * stride);
150 else
151 expand_source_and_mask(cursor, pixmap->devPrivate.ptr);
152 }
153
154 static void
xwl_cursor_attach_pixmap(struct xwl_seat * xwl_seat,struct xwl_cursor * xwl_cursor,PixmapPtr pixmap)155 xwl_cursor_attach_pixmap(struct xwl_seat *xwl_seat,
156 struct xwl_cursor *xwl_cursor, PixmapPtr pixmap)
157 {
158 struct wl_buffer *buffer;
159
160 buffer = xwl_shm_pixmap_get_wl_buffer(pixmap);
161 if (!buffer) {
162 ErrorF("cursor: Error getting buffer\n");
163 return;
164 }
165
166 wl_surface_attach(xwl_cursor->surface, buffer, 0, 0);
167 wl_surface_set_buffer_scale(xwl_cursor->surface,
168 xwl_seat->xwl_screen->global_output_scale);
169 xwl_surface_damage(xwl_seat->xwl_screen, xwl_cursor->surface, 0, 0,
170 xwl_seat->x_cursor->bits->width,
171 xwl_seat->x_cursor->bits->height);
172
173 xwl_cursor->frame_cb = wl_surface_frame(xwl_cursor->surface);
174 wl_callback_add_listener(xwl_cursor->frame_cb, &frame_listener, xwl_cursor);
175
176 /* Hold a reference on the pixmap until it's released by the compositor */
177 pixmap->refcnt++;
178 xwl_pixmap_set_buffer_release_cb(pixmap,
179 xwl_cursor_buffer_release_callback,
180 pixmap);
181
182 wl_surface_commit(xwl_cursor->surface);
183 }
184
185 Bool
xwl_cursor_clear_frame_cb(struct xwl_cursor * xwl_cursor)186 xwl_cursor_clear_frame_cb(struct xwl_cursor *xwl_cursor)
187 {
188 if (xwl_cursor->frame_cb) {
189 wl_callback_destroy(xwl_cursor->frame_cb);
190 xwl_cursor->frame_cb = NULL;
191 return TRUE;
192 }
193
194 return FALSE;
195 }
196
197 void
xwl_seat_set_cursor(struct xwl_seat * xwl_seat)198 xwl_seat_set_cursor(struct xwl_seat *xwl_seat)
199 {
200 struct xwl_screen *xwl_screen = xwl_seat->xwl_screen;
201 struct xwl_cursor *xwl_cursor = &xwl_seat->cursor;
202 PixmapPtr pixmap;
203 CursorPtr cursor;
204
205 if (!xwl_seat->wl_pointer)
206 return;
207
208 if (!xwl_seat->x_cursor) {
209 wl_pointer_set_cursor(xwl_seat->wl_pointer,
210 xwl_seat->pointer_enter_serial, NULL, 0, 0);
211 xwl_cursor_clear_frame_cb(xwl_cursor);
212 xwl_cursor->needs_update = FALSE;
213 return;
214 }
215
216 if (xwl_cursor->frame_cb) {
217 xwl_cursor->needs_update = TRUE;
218 return;
219 }
220
221 cursor = xwl_seat->x_cursor;
222 pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
223 if (!pixmap)
224 return;
225
226 xwl_cursor_copy_bits_to_pixmap(cursor, pixmap);
227
228 wl_pointer_set_cursor(xwl_seat->wl_pointer,
229 xwl_seat->pointer_enter_serial,
230 xwl_cursor->surface,
231 xwl_scale_to(xwl_screen, xwl_seat->x_cursor->bits->xhot),
232 xwl_scale_to(xwl_screen, xwl_seat->x_cursor->bits->yhot));
233
234 xwl_cursor_attach_pixmap(xwl_seat, xwl_cursor, pixmap);
235 }
236
237 void
xwl_tablet_tool_set_cursor(struct xwl_tablet_tool * xwl_tablet_tool)238 xwl_tablet_tool_set_cursor(struct xwl_tablet_tool *xwl_tablet_tool)
239 {
240 struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
241 struct xwl_screen *xwl_screen = xwl_seat->xwl_screen;
242 struct xwl_cursor *xwl_cursor = &xwl_tablet_tool->cursor;
243 PixmapPtr pixmap;
244 CursorPtr cursor;
245
246 if (!xwl_seat->x_cursor) {
247 zwp_tablet_tool_v2_set_cursor(xwl_tablet_tool->tool,
248 xwl_tablet_tool->proximity_in_serial,
249 NULL, 0, 0);
250 xwl_cursor_clear_frame_cb(xwl_cursor);
251 xwl_cursor->needs_update = FALSE;
252 return;
253 }
254
255 if (xwl_cursor->frame_cb) {
256 xwl_cursor->needs_update = TRUE;
257 return;
258 }
259
260 cursor = xwl_seat->x_cursor;
261 pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
262 if (!pixmap)
263 return;
264
265 xwl_cursor_copy_bits_to_pixmap(cursor, pixmap);
266
267 zwp_tablet_tool_v2_set_cursor(xwl_tablet_tool->tool,
268 xwl_tablet_tool->proximity_in_serial,
269 xwl_cursor->surface,
270 xwl_scale_to(xwl_screen, xwl_seat->x_cursor->bits->xhot),
271 xwl_scale_to(xwl_screen, xwl_seat->x_cursor->bits->yhot));
272
273 xwl_cursor_attach_pixmap(xwl_seat, xwl_cursor, pixmap);
274 }
275
276 void
xwl_cursor_release(struct xwl_cursor * xwl_cursor)277 xwl_cursor_release(struct xwl_cursor *xwl_cursor)
278 {
279 wl_surface_destroy(xwl_cursor->surface);
280 xwl_cursor_clear_frame_cb(xwl_cursor);
281 }
282
283 static void
xwl_seat_update_all_cursors(struct xwl_seat * xwl_seat)284 xwl_seat_update_all_cursors(struct xwl_seat *xwl_seat)
285 {
286 struct xwl_tablet_tool *xwl_tablet_tool;
287
288 xwl_seat_set_cursor(xwl_seat);
289
290 xorg_list_for_each_entry(xwl_tablet_tool, &xwl_seat->tablet_tools, link) {
291 if (xwl_tablet_tool->proximity_in_serial != 0)
292 xwl_tablet_tool_set_cursor(xwl_tablet_tool);
293 }
294
295 /* Clear delayed cursor if any */
296 xwl_seat->pending_x_cursor = NULL;
297 }
298
299 static void
xwl_seat_update_cursor_visibility(struct xwl_seat * xwl_seat)300 xwl_seat_update_cursor_visibility(struct xwl_seat *xwl_seat)
301 {
302 xwl_seat->x_cursor = xwl_seat->pending_x_cursor;
303 xwl_seat_cursor_visibility_changed(xwl_seat);
304 xwl_seat_update_all_cursors(xwl_seat);
305 }
306
307 static void
xwl_set_cursor_free_timer(struct xwl_seat * xwl_seat)308 xwl_set_cursor_free_timer(struct xwl_seat *xwl_seat)
309 {
310 if (xwl_seat->x_cursor_timer) {
311 TimerFree(xwl_seat->x_cursor_timer);
312 xwl_seat->x_cursor_timer = NULL;
313 }
314 }
315
316 static CARD32
xwl_set_cursor_timer_callback(OsTimerPtr timer,CARD32 time,void * arg)317 xwl_set_cursor_timer_callback(OsTimerPtr timer, CARD32 time, void *arg)
318 {
319 struct xwl_seat *xwl_seat = arg;
320
321 xwl_set_cursor_free_timer(xwl_seat);
322 xwl_seat_update_cursor_visibility(xwl_seat);
323
324 /* Don't re-arm the timer */
325 return 0;
326 }
327
328 static void
xwl_set_cursor_delayed(struct xwl_seat * xwl_seat,CursorPtr cursor)329 xwl_set_cursor_delayed(struct xwl_seat *xwl_seat, CursorPtr cursor)
330 {
331 xwl_seat->pending_x_cursor = cursor;
332
333 if (xwl_seat->x_cursor_timer == NULL) {
334 xwl_seat->x_cursor_timer = TimerSet(xwl_seat->x_cursor_timer,
335 0, DELAYED_X_CURSOR_TIMEOUT,
336 &xwl_set_cursor_timer_callback,
337 xwl_seat);
338 }
339 }
340
341 static void
xwl_set_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor,int x,int y)342 xwl_set_cursor(DeviceIntPtr device,
343 ScreenPtr screen, CursorPtr cursor, int x, int y)
344 {
345 struct xwl_seat *xwl_seat;
346 Bool cursor_visibility_changed;
347
348 xwl_seat = device->public.devicePrivate;
349 if (xwl_seat == NULL)
350 return;
351
352 cursor_visibility_changed = !!xwl_seat->x_cursor ^ !!cursor;
353
354 if (!cursor_visibility_changed) {
355 /* Cursor remains shown or hidden, apply the change immediately */
356 xwl_set_cursor_free_timer(xwl_seat);
357 xwl_seat->x_cursor = cursor;
358 xwl_seat_update_all_cursors(xwl_seat);
359 return;
360 }
361
362 xwl_seat->pending_x_cursor = cursor;
363 if (cursor) {
364 /* Cursor is being shown, delay the change until moved or timed out */
365 xwl_set_cursor_delayed(xwl_seat, cursor);
366 } else {
367 /* Cursor is being hidden, apply the change immediately */
368 xwl_seat_update_cursor_visibility(xwl_seat);
369 }
370 }
371
372 static void
xwl_move_cursor(DeviceIntPtr device,ScreenPtr screen,int x,int y)373 xwl_move_cursor(DeviceIntPtr device, ScreenPtr screen, int x, int y)
374 {
375 struct xwl_seat *xwl_seat;
376
377 xwl_seat = device->public.devicePrivate;
378 if (xwl_seat == NULL)
379 return;
380
381 xwl_set_cursor_free_timer(xwl_seat);
382
383 if (xwl_seat->pending_x_cursor)
384 xwl_seat_update_cursor_visibility(xwl_seat);
385 }
386
387 static Bool
xwl_device_cursor_initialize(DeviceIntPtr device,ScreenPtr screen)388 xwl_device_cursor_initialize(DeviceIntPtr device, ScreenPtr screen)
389 {
390 return TRUE;
391 }
392
393 static void
xwl_device_cursor_cleanup(DeviceIntPtr device,ScreenPtr screen)394 xwl_device_cursor_cleanup(DeviceIntPtr device, ScreenPtr screen)
395 {
396 struct xwl_seat *xwl_seat;
397
398 xwl_seat = device->public.devicePrivate;
399 if (xwl_seat)
400 xwl_set_cursor_free_timer(xwl_seat);
401 }
402
403 static miPointerSpriteFuncRec xwl_pointer_sprite_funcs = {
404 xwl_realize_cursor,
405 xwl_unrealize_cursor,
406 xwl_set_cursor,
407 xwl_move_cursor,
408 xwl_device_cursor_initialize,
409 xwl_device_cursor_cleanup
410 };
411
412 static Bool
xwl_cursor_off_screen(ScreenPtr * ppScreen,int * x,int * y)413 xwl_cursor_off_screen(ScreenPtr *ppScreen, int *x, int *y)
414 {
415 return FALSE;
416 }
417
418 static void
xwl_cross_screen(ScreenPtr pScreen,Bool entering)419 xwl_cross_screen(ScreenPtr pScreen, Bool entering)
420 {
421 }
422
423 static void
xwl_pointer_warp_cursor(DeviceIntPtr pDev,ScreenPtr pScreen,int x,int y)424 xwl_pointer_warp_cursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y)
425 {
426 miPointerWarpCursor(pDev, pScreen, x, y);
427 }
428
429 static miPointerScreenFuncRec xwl_pointer_screen_funcs = {
430 xwl_cursor_off_screen,
431 xwl_cross_screen,
432 xwl_pointer_warp_cursor
433 };
434
435 Bool
xwl_screen_init_cursor(struct xwl_screen * xwl_screen)436 xwl_screen_init_cursor(struct xwl_screen *xwl_screen)
437 {
438 if (!dixRegisterPrivateKey(&xwl_cursor_private_key, PRIVATE_CURSOR_BITS, 0))
439 return FALSE;
440
441 return miPointerInitialize(xwl_screen->screen,
442 &xwl_pointer_sprite_funcs,
443 &xwl_pointer_screen_funcs, TRUE);
444 }
445