xref: /OK3568_Linux_fs/external/xserver/hw/xwayland/xwayland-cursor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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.h"
28 
29 #include <mipointer.h>
30 
31 static DevPrivateKeyRec xwl_cursor_private_key;
32 
33 static void
expand_source_and_mask(CursorPtr cursor,CARD32 * data)34 expand_source_and_mask(CursorPtr cursor, CARD32 *data)
35 {
36     CARD32 *p, d, fg, bg;
37     CursorBitsPtr bits = cursor->bits;
38     int x, y, stride, i, bit;
39 
40     p = data;
41     fg = ((cursor->foreRed & 0xff00) << 8) |
42         (cursor->foreGreen & 0xff00) | (cursor->foreGreen >> 8);
43     bg = ((cursor->backRed & 0xff00) << 8) |
44         (cursor->backGreen & 0xff00) | (cursor->backGreen >> 8);
45     stride = BitmapBytePad(bits->width);
46     for (y = 0; y < bits->height; y++)
47         for (x = 0; x < bits->width; x++) {
48             i = y * stride + x / 8;
49             bit = 1 << (x & 7);
50             if (bits->source[i] & bit)
51                 d = fg;
52             else
53                 d = bg;
54             if (bits->mask[i] & bit)
55                 d |= 0xff000000;
56             else
57                 d = 0x00000000;
58 
59             *p++ = d;
60         }
61 }
62 
63 static Bool
xwl_realize_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor)64 xwl_realize_cursor(DeviceIntPtr device, ScreenPtr screen, CursorPtr cursor)
65 {
66     PixmapPtr pixmap;
67 
68     pixmap = xwl_shm_create_pixmap(screen, cursor->bits->width,
69                                    cursor->bits->height, 32, 0);
70     dixSetPrivate(&cursor->devPrivates, &xwl_cursor_private_key, pixmap);
71 
72     return TRUE;
73 }
74 
75 static Bool
xwl_unrealize_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor)76 xwl_unrealize_cursor(DeviceIntPtr device, ScreenPtr screen, CursorPtr cursor)
77 {
78     PixmapPtr pixmap;
79     struct xwl_screen *xwl_screen;
80     struct xwl_seat *xwl_seat;
81 
82     pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
83     if (!pixmap)
84         return TRUE;
85 
86     dixSetPrivate(&cursor->devPrivates, &xwl_cursor_private_key, NULL);
87 
88     /* When called from FreeCursor(), device is always NULL */
89     xwl_screen = xwl_screen_get(screen);
90     xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
91         if (cursor == xwl_seat->x_cursor)
92             xwl_seat->x_cursor = NULL;
93     }
94 
95     return xwl_shm_destroy_pixmap(pixmap);
96 }
97 
98 static void
clear_cursor_frame_callback(struct xwl_cursor * xwl_cursor)99 clear_cursor_frame_callback(struct xwl_cursor *xwl_cursor)
100 {
101    if (xwl_cursor->frame_cb) {
102        wl_callback_destroy (xwl_cursor->frame_cb);
103        xwl_cursor->frame_cb = NULL;
104    }
105 }
106 
107 static void
frame_callback(void * data,struct wl_callback * callback,uint32_t time)108 frame_callback(void *data,
109                struct wl_callback *callback,
110                uint32_t time)
111 {
112     struct xwl_cursor *xwl_cursor = data;
113 
114     clear_cursor_frame_callback(xwl_cursor);
115     if (xwl_cursor->needs_update) {
116         xwl_cursor->needs_update = FALSE;
117         xwl_cursor->update_proc(xwl_cursor);
118     }
119 }
120 
121 static const struct wl_callback_listener frame_listener = {
122     frame_callback
123 };
124 
125 void
xwl_seat_set_cursor(struct xwl_seat * xwl_seat)126 xwl_seat_set_cursor(struct xwl_seat *xwl_seat)
127 {
128     struct xwl_cursor *xwl_cursor = &xwl_seat->cursor;
129     PixmapPtr pixmap;
130     CursorPtr cursor;
131     int stride;
132 
133     if (!xwl_seat->wl_pointer)
134         return;
135 
136     if (!xwl_seat->x_cursor) {
137         wl_pointer_set_cursor(xwl_seat->wl_pointer,
138                               xwl_seat->pointer_enter_serial, NULL, 0, 0);
139         clear_cursor_frame_callback(xwl_cursor);
140         xwl_cursor->needs_update = FALSE;
141         return;
142     }
143 
144     if (xwl_cursor->frame_cb) {
145         xwl_cursor->needs_update = TRUE;
146         return;
147     }
148 
149     cursor = xwl_seat->x_cursor;
150     pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
151     if (!pixmap)
152         return;
153 
154     stride = cursor->bits->width * 4;
155     if (cursor->bits->argb)
156         memcpy(pixmap->devPrivate.ptr,
157                cursor->bits->argb, cursor->bits->height * stride);
158     else
159         expand_source_and_mask(cursor, pixmap->devPrivate.ptr);
160 
161     wl_pointer_set_cursor(xwl_seat->wl_pointer,
162                           xwl_seat->pointer_enter_serial,
163                           xwl_cursor->surface,
164                           xwl_seat->x_cursor->bits->xhot,
165                           xwl_seat->x_cursor->bits->yhot);
166     wl_surface_attach(xwl_cursor->surface,
167                       xwl_shm_pixmap_get_wl_buffer(pixmap), 0, 0);
168     wl_surface_damage(xwl_cursor->surface, 0, 0,
169                       xwl_seat->x_cursor->bits->width,
170                       xwl_seat->x_cursor->bits->height);
171 
172     xwl_cursor->frame_cb = wl_surface_frame(xwl_cursor->surface);
173     wl_callback_add_listener(xwl_cursor->frame_cb, &frame_listener, xwl_cursor);
174 
175     wl_surface_commit(xwl_cursor->surface);
176 }
177 
178 void
xwl_tablet_tool_set_cursor(struct xwl_tablet_tool * xwl_tablet_tool)179 xwl_tablet_tool_set_cursor(struct xwl_tablet_tool *xwl_tablet_tool)
180 {
181     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
182     struct xwl_cursor *xwl_cursor = &xwl_tablet_tool->cursor;
183     PixmapPtr pixmap;
184     CursorPtr cursor;
185     int stride;
186 
187     if (!xwl_seat->x_cursor) {
188         zwp_tablet_tool_v2_set_cursor(xwl_tablet_tool->tool,
189                                       xwl_tablet_tool->proximity_in_serial,
190                                       NULL, 0, 0);
191         clear_cursor_frame_callback(xwl_cursor);
192         xwl_cursor->needs_update = FALSE;
193         return;
194     }
195 
196     if (xwl_cursor->frame_cb) {
197         xwl_cursor->needs_update = TRUE;
198         return;
199     }
200 
201     cursor = xwl_seat->x_cursor;
202     pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
203     if (!pixmap)
204         return;
205 
206     stride = cursor->bits->width * 4;
207     if (cursor->bits->argb)
208         memcpy(pixmap->devPrivate.ptr,
209                cursor->bits->argb, cursor->bits->height * stride);
210     else
211         expand_source_and_mask(cursor, pixmap->devPrivate.ptr);
212 
213     zwp_tablet_tool_v2_set_cursor(xwl_tablet_tool->tool,
214                                   xwl_tablet_tool->proximity_in_serial,
215                                   xwl_cursor->surface,
216                                   xwl_seat->x_cursor->bits->xhot,
217                                   xwl_seat->x_cursor->bits->yhot);
218     wl_surface_attach(xwl_cursor->surface,
219                       xwl_shm_pixmap_get_wl_buffer(pixmap), 0, 0);
220     wl_surface_damage(xwl_cursor->surface, 0, 0,
221                       xwl_seat->x_cursor->bits->width,
222                       xwl_seat->x_cursor->bits->height);
223 
224     xwl_cursor->frame_cb = wl_surface_frame(xwl_cursor->surface);
225     wl_callback_add_listener(xwl_cursor->frame_cb, &frame_listener, xwl_cursor);
226 
227     wl_surface_commit(xwl_cursor->surface);
228 }
229 
230 static void
xwl_set_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor,int x,int y)231 xwl_set_cursor(DeviceIntPtr device,
232                ScreenPtr screen, CursorPtr cursor, int x, int y)
233 {
234     struct xwl_seat *xwl_seat;
235     struct xwl_tablet_tool *xwl_tablet_tool;
236     Bool cursor_visibility_changed;
237 
238     xwl_seat = device->public.devicePrivate;
239     if (xwl_seat == NULL)
240         return;
241 
242     cursor_visibility_changed = !!xwl_seat->x_cursor ^ !!cursor;
243 
244     xwl_seat->x_cursor = cursor;
245 
246     if (cursor_visibility_changed)
247         xwl_seat_cursor_visibility_changed(xwl_seat);
248 
249     xwl_seat_set_cursor(xwl_seat);
250 
251     xorg_list_for_each_entry(xwl_tablet_tool, &xwl_seat->tablet_tools, link) {
252         if (xwl_tablet_tool->proximity_in_serial != 0)
253             xwl_tablet_tool_set_cursor(xwl_tablet_tool);
254     }
255 }
256 
257 static void
xwl_move_cursor(DeviceIntPtr device,ScreenPtr screen,int x,int y)258 xwl_move_cursor(DeviceIntPtr device, ScreenPtr screen, int x, int y)
259 {
260 }
261 
262 static Bool
xwl_device_cursor_initialize(DeviceIntPtr device,ScreenPtr screen)263 xwl_device_cursor_initialize(DeviceIntPtr device, ScreenPtr screen)
264 {
265     return TRUE;
266 }
267 
268 static void
xwl_device_cursor_cleanup(DeviceIntPtr device,ScreenPtr screen)269 xwl_device_cursor_cleanup(DeviceIntPtr device, ScreenPtr screen)
270 {
271 }
272 
273 static miPointerSpriteFuncRec xwl_pointer_sprite_funcs = {
274     xwl_realize_cursor,
275     xwl_unrealize_cursor,
276     xwl_set_cursor,
277     xwl_move_cursor,
278     xwl_device_cursor_initialize,
279     xwl_device_cursor_cleanup
280 };
281 
282 static Bool
xwl_cursor_off_screen(ScreenPtr * ppScreen,int * x,int * y)283 xwl_cursor_off_screen(ScreenPtr *ppScreen, int *x, int *y)
284 {
285     return FALSE;
286 }
287 
288 static void
xwl_cross_screen(ScreenPtr pScreen,Bool entering)289 xwl_cross_screen(ScreenPtr pScreen, Bool entering)
290 {
291 }
292 
293 static void
xwl_pointer_warp_cursor(DeviceIntPtr pDev,ScreenPtr pScreen,int x,int y)294 xwl_pointer_warp_cursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y)
295 {
296     miPointerWarpCursor(pDev, pScreen, x, y);
297 }
298 
299 static miPointerScreenFuncRec xwl_pointer_screen_funcs = {
300     xwl_cursor_off_screen,
301     xwl_cross_screen,
302     xwl_pointer_warp_cursor
303 };
304 
305 Bool
xwl_screen_init_cursor(struct xwl_screen * xwl_screen)306 xwl_screen_init_cursor(struct xwl_screen *xwl_screen)
307 {
308     if (!dixRegisterPrivateKey(&xwl_cursor_private_key, PRIVATE_CURSOR_BITS, 0))
309         return FALSE;
310 
311     return miPointerInitialize(xwl_screen->screen,
312                                &xwl_pointer_sprite_funcs,
313                                &xwl_pointer_screen_funcs, TRUE);
314 }
315