xref: /OK3568_Linux_fs/external/xserver/hw/xwayland/xwayland-cursor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2014 Intel Corporation
3*4882a593Smuzhiyun  * Copyright © 2011 Kristian Høgsberg
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Permission to use, copy, modify, distribute, and sell this software
6*4882a593Smuzhiyun  * and its documentation for any purpose is hereby granted without
7*4882a593Smuzhiyun  * fee, provided that the above copyright notice appear in all copies
8*4882a593Smuzhiyun  * and that both that copyright notice and this permission notice
9*4882a593Smuzhiyun  * appear in supporting documentation, and that the name of the
10*4882a593Smuzhiyun  * copyright holders not be used in advertising or publicity
11*4882a593Smuzhiyun  * pertaining to distribution of the software without specific,
12*4882a593Smuzhiyun  * written prior permission.  The copyright holders make no
13*4882a593Smuzhiyun  * representations about the suitability of this software for any
14*4882a593Smuzhiyun  * purpose.  It is provided "as is" without express or implied
15*4882a593Smuzhiyun  * warranty.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18*4882a593Smuzhiyun  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19*4882a593Smuzhiyun  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20*4882a593Smuzhiyun  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22*4882a593Smuzhiyun  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23*4882a593Smuzhiyun  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24*4882a593Smuzhiyun  * SOFTWARE.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include "xwayland.h"
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <mipointer.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun static DevPrivateKeyRec xwl_cursor_private_key;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static void
expand_source_and_mask(CursorPtr cursor,CARD32 * data)34*4882a593Smuzhiyun expand_source_and_mask(CursorPtr cursor, CARD32 *data)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun     CARD32 *p, d, fg, bg;
37*4882a593Smuzhiyun     CursorBitsPtr bits = cursor->bits;
38*4882a593Smuzhiyun     int x, y, stride, i, bit;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun     p = data;
41*4882a593Smuzhiyun     fg = ((cursor->foreRed & 0xff00) << 8) |
42*4882a593Smuzhiyun         (cursor->foreGreen & 0xff00) | (cursor->foreGreen >> 8);
43*4882a593Smuzhiyun     bg = ((cursor->backRed & 0xff00) << 8) |
44*4882a593Smuzhiyun         (cursor->backGreen & 0xff00) | (cursor->backGreen >> 8);
45*4882a593Smuzhiyun     stride = BitmapBytePad(bits->width);
46*4882a593Smuzhiyun     for (y = 0; y < bits->height; y++)
47*4882a593Smuzhiyun         for (x = 0; x < bits->width; x++) {
48*4882a593Smuzhiyun             i = y * stride + x / 8;
49*4882a593Smuzhiyun             bit = 1 << (x & 7);
50*4882a593Smuzhiyun             if (bits->source[i] & bit)
51*4882a593Smuzhiyun                 d = fg;
52*4882a593Smuzhiyun             else
53*4882a593Smuzhiyun                 d = bg;
54*4882a593Smuzhiyun             if (bits->mask[i] & bit)
55*4882a593Smuzhiyun                 d |= 0xff000000;
56*4882a593Smuzhiyun             else
57*4882a593Smuzhiyun                 d = 0x00000000;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun             *p++ = d;
60*4882a593Smuzhiyun         }
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun static Bool
xwl_realize_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor)64*4882a593Smuzhiyun xwl_realize_cursor(DeviceIntPtr device, ScreenPtr screen, CursorPtr cursor)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun     PixmapPtr pixmap;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun     pixmap = xwl_shm_create_pixmap(screen, cursor->bits->width,
69*4882a593Smuzhiyun                                    cursor->bits->height, 32, 0);
70*4882a593Smuzhiyun     dixSetPrivate(&cursor->devPrivates, &xwl_cursor_private_key, pixmap);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun     return TRUE;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun static Bool
xwl_unrealize_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor)76*4882a593Smuzhiyun xwl_unrealize_cursor(DeviceIntPtr device, ScreenPtr screen, CursorPtr cursor)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun     PixmapPtr pixmap;
79*4882a593Smuzhiyun     struct xwl_screen *xwl_screen;
80*4882a593Smuzhiyun     struct xwl_seat *xwl_seat;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun     pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
83*4882a593Smuzhiyun     if (!pixmap)
84*4882a593Smuzhiyun         return TRUE;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun     dixSetPrivate(&cursor->devPrivates, &xwl_cursor_private_key, NULL);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun     /* When called from FreeCursor(), device is always NULL */
89*4882a593Smuzhiyun     xwl_screen = xwl_screen_get(screen);
90*4882a593Smuzhiyun     xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
91*4882a593Smuzhiyun         if (cursor == xwl_seat->x_cursor)
92*4882a593Smuzhiyun             xwl_seat->x_cursor = NULL;
93*4882a593Smuzhiyun     }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun     return xwl_shm_destroy_pixmap(pixmap);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun static void
clear_cursor_frame_callback(struct xwl_cursor * xwl_cursor)99*4882a593Smuzhiyun clear_cursor_frame_callback(struct xwl_cursor *xwl_cursor)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun    if (xwl_cursor->frame_cb) {
102*4882a593Smuzhiyun        wl_callback_destroy (xwl_cursor->frame_cb);
103*4882a593Smuzhiyun        xwl_cursor->frame_cb = NULL;
104*4882a593Smuzhiyun    }
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun static void
frame_callback(void * data,struct wl_callback * callback,uint32_t time)108*4882a593Smuzhiyun frame_callback(void *data,
109*4882a593Smuzhiyun                struct wl_callback *callback,
110*4882a593Smuzhiyun                uint32_t time)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun     struct xwl_cursor *xwl_cursor = data;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun     clear_cursor_frame_callback(xwl_cursor);
115*4882a593Smuzhiyun     if (xwl_cursor->needs_update) {
116*4882a593Smuzhiyun         xwl_cursor->needs_update = FALSE;
117*4882a593Smuzhiyun         xwl_cursor->update_proc(xwl_cursor);
118*4882a593Smuzhiyun     }
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun static const struct wl_callback_listener frame_listener = {
122*4882a593Smuzhiyun     frame_callback
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun void
xwl_seat_set_cursor(struct xwl_seat * xwl_seat)126*4882a593Smuzhiyun xwl_seat_set_cursor(struct xwl_seat *xwl_seat)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun     struct xwl_cursor *xwl_cursor = &xwl_seat->cursor;
129*4882a593Smuzhiyun     PixmapPtr pixmap;
130*4882a593Smuzhiyun     CursorPtr cursor;
131*4882a593Smuzhiyun     int stride;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun     if (!xwl_seat->wl_pointer)
134*4882a593Smuzhiyun         return;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun     if (!xwl_seat->x_cursor) {
137*4882a593Smuzhiyun         wl_pointer_set_cursor(xwl_seat->wl_pointer,
138*4882a593Smuzhiyun                               xwl_seat->pointer_enter_serial, NULL, 0, 0);
139*4882a593Smuzhiyun         clear_cursor_frame_callback(xwl_cursor);
140*4882a593Smuzhiyun         xwl_cursor->needs_update = FALSE;
141*4882a593Smuzhiyun         return;
142*4882a593Smuzhiyun     }
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun     if (xwl_cursor->frame_cb) {
145*4882a593Smuzhiyun         xwl_cursor->needs_update = TRUE;
146*4882a593Smuzhiyun         return;
147*4882a593Smuzhiyun     }
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun     cursor = xwl_seat->x_cursor;
150*4882a593Smuzhiyun     pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
151*4882a593Smuzhiyun     if (!pixmap)
152*4882a593Smuzhiyun         return;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun     stride = cursor->bits->width * 4;
155*4882a593Smuzhiyun     if (cursor->bits->argb)
156*4882a593Smuzhiyun         memcpy(pixmap->devPrivate.ptr,
157*4882a593Smuzhiyun                cursor->bits->argb, cursor->bits->height * stride);
158*4882a593Smuzhiyun     else
159*4882a593Smuzhiyun         expand_source_and_mask(cursor, pixmap->devPrivate.ptr);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun     wl_pointer_set_cursor(xwl_seat->wl_pointer,
162*4882a593Smuzhiyun                           xwl_seat->pointer_enter_serial,
163*4882a593Smuzhiyun                           xwl_cursor->surface,
164*4882a593Smuzhiyun                           xwl_seat->x_cursor->bits->xhot,
165*4882a593Smuzhiyun                           xwl_seat->x_cursor->bits->yhot);
166*4882a593Smuzhiyun     wl_surface_attach(xwl_cursor->surface,
167*4882a593Smuzhiyun                       xwl_shm_pixmap_get_wl_buffer(pixmap), 0, 0);
168*4882a593Smuzhiyun     wl_surface_damage(xwl_cursor->surface, 0, 0,
169*4882a593Smuzhiyun                       xwl_seat->x_cursor->bits->width,
170*4882a593Smuzhiyun                       xwl_seat->x_cursor->bits->height);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun     xwl_cursor->frame_cb = wl_surface_frame(xwl_cursor->surface);
173*4882a593Smuzhiyun     wl_callback_add_listener(xwl_cursor->frame_cb, &frame_listener, xwl_cursor);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun     wl_surface_commit(xwl_cursor->surface);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun void
xwl_tablet_tool_set_cursor(struct xwl_tablet_tool * xwl_tablet_tool)179*4882a593Smuzhiyun xwl_tablet_tool_set_cursor(struct xwl_tablet_tool *xwl_tablet_tool)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun     struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
182*4882a593Smuzhiyun     struct xwl_cursor *xwl_cursor = &xwl_tablet_tool->cursor;
183*4882a593Smuzhiyun     PixmapPtr pixmap;
184*4882a593Smuzhiyun     CursorPtr cursor;
185*4882a593Smuzhiyun     int stride;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun     if (!xwl_seat->x_cursor) {
188*4882a593Smuzhiyun         zwp_tablet_tool_v2_set_cursor(xwl_tablet_tool->tool,
189*4882a593Smuzhiyun                                       xwl_tablet_tool->proximity_in_serial,
190*4882a593Smuzhiyun                                       NULL, 0, 0);
191*4882a593Smuzhiyun         clear_cursor_frame_callback(xwl_cursor);
192*4882a593Smuzhiyun         xwl_cursor->needs_update = FALSE;
193*4882a593Smuzhiyun         return;
194*4882a593Smuzhiyun     }
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun     if (xwl_cursor->frame_cb) {
197*4882a593Smuzhiyun         xwl_cursor->needs_update = TRUE;
198*4882a593Smuzhiyun         return;
199*4882a593Smuzhiyun     }
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun     cursor = xwl_seat->x_cursor;
202*4882a593Smuzhiyun     pixmap = dixGetPrivate(&cursor->devPrivates, &xwl_cursor_private_key);
203*4882a593Smuzhiyun     if (!pixmap)
204*4882a593Smuzhiyun         return;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun     stride = cursor->bits->width * 4;
207*4882a593Smuzhiyun     if (cursor->bits->argb)
208*4882a593Smuzhiyun         memcpy(pixmap->devPrivate.ptr,
209*4882a593Smuzhiyun                cursor->bits->argb, cursor->bits->height * stride);
210*4882a593Smuzhiyun     else
211*4882a593Smuzhiyun         expand_source_and_mask(cursor, pixmap->devPrivate.ptr);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun     zwp_tablet_tool_v2_set_cursor(xwl_tablet_tool->tool,
214*4882a593Smuzhiyun                                   xwl_tablet_tool->proximity_in_serial,
215*4882a593Smuzhiyun                                   xwl_cursor->surface,
216*4882a593Smuzhiyun                                   xwl_seat->x_cursor->bits->xhot,
217*4882a593Smuzhiyun                                   xwl_seat->x_cursor->bits->yhot);
218*4882a593Smuzhiyun     wl_surface_attach(xwl_cursor->surface,
219*4882a593Smuzhiyun                       xwl_shm_pixmap_get_wl_buffer(pixmap), 0, 0);
220*4882a593Smuzhiyun     wl_surface_damage(xwl_cursor->surface, 0, 0,
221*4882a593Smuzhiyun                       xwl_seat->x_cursor->bits->width,
222*4882a593Smuzhiyun                       xwl_seat->x_cursor->bits->height);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun     xwl_cursor->frame_cb = wl_surface_frame(xwl_cursor->surface);
225*4882a593Smuzhiyun     wl_callback_add_listener(xwl_cursor->frame_cb, &frame_listener, xwl_cursor);
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun     wl_surface_commit(xwl_cursor->surface);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun static void
xwl_set_cursor(DeviceIntPtr device,ScreenPtr screen,CursorPtr cursor,int x,int y)231*4882a593Smuzhiyun xwl_set_cursor(DeviceIntPtr device,
232*4882a593Smuzhiyun                ScreenPtr screen, CursorPtr cursor, int x, int y)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun     struct xwl_seat *xwl_seat;
235*4882a593Smuzhiyun     struct xwl_tablet_tool *xwl_tablet_tool;
236*4882a593Smuzhiyun     Bool cursor_visibility_changed;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun     xwl_seat = device->public.devicePrivate;
239*4882a593Smuzhiyun     if (xwl_seat == NULL)
240*4882a593Smuzhiyun         return;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun     cursor_visibility_changed = !!xwl_seat->x_cursor ^ !!cursor;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun     xwl_seat->x_cursor = cursor;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun     if (cursor_visibility_changed)
247*4882a593Smuzhiyun         xwl_seat_cursor_visibility_changed(xwl_seat);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun     xwl_seat_set_cursor(xwl_seat);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun     xorg_list_for_each_entry(xwl_tablet_tool, &xwl_seat->tablet_tools, link) {
252*4882a593Smuzhiyun         if (xwl_tablet_tool->proximity_in_serial != 0)
253*4882a593Smuzhiyun             xwl_tablet_tool_set_cursor(xwl_tablet_tool);
254*4882a593Smuzhiyun     }
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun static void
xwl_move_cursor(DeviceIntPtr device,ScreenPtr screen,int x,int y)258*4882a593Smuzhiyun xwl_move_cursor(DeviceIntPtr device, ScreenPtr screen, int x, int y)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun static Bool
xwl_device_cursor_initialize(DeviceIntPtr device,ScreenPtr screen)263*4882a593Smuzhiyun xwl_device_cursor_initialize(DeviceIntPtr device, ScreenPtr screen)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun     return TRUE;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun static void
xwl_device_cursor_cleanup(DeviceIntPtr device,ScreenPtr screen)269*4882a593Smuzhiyun xwl_device_cursor_cleanup(DeviceIntPtr device, ScreenPtr screen)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun static miPointerSpriteFuncRec xwl_pointer_sprite_funcs = {
274*4882a593Smuzhiyun     xwl_realize_cursor,
275*4882a593Smuzhiyun     xwl_unrealize_cursor,
276*4882a593Smuzhiyun     xwl_set_cursor,
277*4882a593Smuzhiyun     xwl_move_cursor,
278*4882a593Smuzhiyun     xwl_device_cursor_initialize,
279*4882a593Smuzhiyun     xwl_device_cursor_cleanup
280*4882a593Smuzhiyun };
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun static Bool
xwl_cursor_off_screen(ScreenPtr * ppScreen,int * x,int * y)283*4882a593Smuzhiyun xwl_cursor_off_screen(ScreenPtr *ppScreen, int *x, int *y)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun     return FALSE;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun static void
xwl_cross_screen(ScreenPtr pScreen,Bool entering)289*4882a593Smuzhiyun xwl_cross_screen(ScreenPtr pScreen, Bool entering)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun static void
xwl_pointer_warp_cursor(DeviceIntPtr pDev,ScreenPtr pScreen,int x,int y)294*4882a593Smuzhiyun xwl_pointer_warp_cursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun     miPointerWarpCursor(pDev, pScreen, x, y);
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun static miPointerScreenFuncRec xwl_pointer_screen_funcs = {
300*4882a593Smuzhiyun     xwl_cursor_off_screen,
301*4882a593Smuzhiyun     xwl_cross_screen,
302*4882a593Smuzhiyun     xwl_pointer_warp_cursor
303*4882a593Smuzhiyun };
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun Bool
xwl_screen_init_cursor(struct xwl_screen * xwl_screen)306*4882a593Smuzhiyun xwl_screen_init_cursor(struct xwl_screen *xwl_screen)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun     if (!dixRegisterPrivateKey(&xwl_cursor_private_key, PRIVATE_CURSOR_BITS, 0))
309*4882a593Smuzhiyun         return FALSE;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun     return miPointerInitialize(xwl_screen->screen,
312*4882a593Smuzhiyun                                &xwl_pointer_sprite_funcs,
313*4882a593Smuzhiyun                                &xwl_pointer_screen_funcs, TRUE);
314*4882a593Smuzhiyun }
315