xref: /OK3568_Linux_fs/external/xserver/hw/kdrive/ephyr/ephyrcursor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2014 Red Hat, Inc.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun  * Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Author:
24*4882a593Smuzhiyun  *      Adam Jackson <ajax@redhat.com>
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
28*4882a593Smuzhiyun #include <dix-config.h>
29*4882a593Smuzhiyun #endif
30*4882a593Smuzhiyun #include "ephyr.h"
31*4882a593Smuzhiyun #include "ephyrlog.h"
32*4882a593Smuzhiyun #include "hostx.h"
33*4882a593Smuzhiyun #include "cursorstr.h"
34*4882a593Smuzhiyun #include <xcb/render.h>
35*4882a593Smuzhiyun #include <xcb/xcb_renderutil.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static DevPrivateKeyRec ephyrCursorPrivateKey;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun typedef struct _ephyrCursor {
40*4882a593Smuzhiyun     xcb_cursor_t cursor;
41*4882a593Smuzhiyun } ephyrCursorRec, *ephyrCursorPtr;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun static ephyrCursorPtr
ephyrGetCursor(CursorPtr cursor)44*4882a593Smuzhiyun ephyrGetCursor(CursorPtr cursor)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun     return dixGetPrivateAddr(&cursor->devPrivates, &ephyrCursorPrivateKey);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun static void
ephyrRealizeCoreCursor(EphyrScrPriv * scr,CursorPtr cursor)50*4882a593Smuzhiyun ephyrRealizeCoreCursor(EphyrScrPriv *scr, CursorPtr cursor)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun     ephyrCursorPtr hw = ephyrGetCursor(cursor);
53*4882a593Smuzhiyun     xcb_connection_t *conn = hostx_get_xcbconn();
54*4882a593Smuzhiyun     xcb_pixmap_t source, mask;
55*4882a593Smuzhiyun     xcb_image_t *image;
56*4882a593Smuzhiyun     xcb_gcontext_t gc;
57*4882a593Smuzhiyun     int w = cursor->bits->width, h = cursor->bits->height;
58*4882a593Smuzhiyun     uint32_t gcmask = XCB_GC_FUNCTION |
59*4882a593Smuzhiyun                       XCB_GC_PLANE_MASK |
60*4882a593Smuzhiyun                       XCB_GC_FOREGROUND |
61*4882a593Smuzhiyun                       XCB_GC_BACKGROUND |
62*4882a593Smuzhiyun                       XCB_GC_CLIP_MASK;
63*4882a593Smuzhiyun     uint32_t val[] = {
64*4882a593Smuzhiyun         XCB_GX_COPY,    /* function */
65*4882a593Smuzhiyun         ~0,             /* planemask */
66*4882a593Smuzhiyun         1L,             /* foreground */
67*4882a593Smuzhiyun         0L,             /* background */
68*4882a593Smuzhiyun         None,           /* clipmask */
69*4882a593Smuzhiyun     };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun     source = xcb_generate_id(conn);
72*4882a593Smuzhiyun     mask = xcb_generate_id(conn);
73*4882a593Smuzhiyun     xcb_create_pixmap(conn, 1, source, scr->win, w, h);
74*4882a593Smuzhiyun     xcb_create_pixmap(conn, 1, mask, scr->win, w, h);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun     gc = xcb_generate_id(conn);
77*4882a593Smuzhiyun     xcb_create_gc(conn, gc, source, gcmask, val);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun     image = xcb_image_create_native(conn, w, h, XCB_IMAGE_FORMAT_XY_BITMAP,
80*4882a593Smuzhiyun                                     1, NULL, ~0, NULL);
81*4882a593Smuzhiyun     image->data = cursor->bits->source;
82*4882a593Smuzhiyun     xcb_image_put(conn, source, gc, image, 0, 0, 0);
83*4882a593Smuzhiyun     xcb_image_destroy(image);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun     image = xcb_image_create_native(conn, w, h, XCB_IMAGE_FORMAT_XY_BITMAP,
86*4882a593Smuzhiyun                                     1, NULL, ~0, NULL);
87*4882a593Smuzhiyun     image->data = cursor->bits->mask;
88*4882a593Smuzhiyun     xcb_image_put(conn, mask, gc, image, 0, 0, 0);
89*4882a593Smuzhiyun     xcb_image_destroy(image);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun     xcb_free_gc(conn, gc);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun     hw->cursor = xcb_generate_id(conn);
94*4882a593Smuzhiyun     xcb_create_cursor(conn, hw->cursor, source, mask,
95*4882a593Smuzhiyun                       cursor->foreRed, cursor->foreGreen, cursor->foreBlue,
96*4882a593Smuzhiyun                       cursor->backRed, cursor->backGreen, cursor->backBlue,
97*4882a593Smuzhiyun                       cursor->bits->xhot, cursor->bits->yhot);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun     xcb_free_pixmap(conn, source);
100*4882a593Smuzhiyun     xcb_free_pixmap(conn, mask);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun static xcb_render_pictformat_t
get_argb_format(void)104*4882a593Smuzhiyun get_argb_format(void)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun     static xcb_render_pictformat_t format;
107*4882a593Smuzhiyun     if (format == None) {
108*4882a593Smuzhiyun         xcb_connection_t *conn = hostx_get_xcbconn();
109*4882a593Smuzhiyun         xcb_render_query_pict_formats_cookie_t cookie;
110*4882a593Smuzhiyun         xcb_render_query_pict_formats_reply_t *formats;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun         cookie = xcb_render_query_pict_formats(conn);
113*4882a593Smuzhiyun         formats =
114*4882a593Smuzhiyun             xcb_render_query_pict_formats_reply(conn, cookie, NULL);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun         format =
117*4882a593Smuzhiyun             xcb_render_util_find_standard_format(formats,
118*4882a593Smuzhiyun                                                  XCB_PICT_STANDARD_ARGB_32)->id;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun         free(formats);
121*4882a593Smuzhiyun     }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun     return format;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun static void
ephyrRealizeARGBCursor(EphyrScrPriv * scr,CursorPtr cursor)127*4882a593Smuzhiyun ephyrRealizeARGBCursor(EphyrScrPriv *scr, CursorPtr cursor)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun     ephyrCursorPtr hw = ephyrGetCursor(cursor);
130*4882a593Smuzhiyun     xcb_connection_t *conn = hostx_get_xcbconn();
131*4882a593Smuzhiyun     xcb_gcontext_t gc;
132*4882a593Smuzhiyun     xcb_pixmap_t source;
133*4882a593Smuzhiyun     xcb_render_picture_t picture;
134*4882a593Smuzhiyun     xcb_image_t *image;
135*4882a593Smuzhiyun     int w = cursor->bits->width, h = cursor->bits->height;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun     /* dix' storage is PICT_a8r8g8b8 */
138*4882a593Smuzhiyun     source = xcb_generate_id(conn);
139*4882a593Smuzhiyun     xcb_create_pixmap(conn, 32, source, scr->win, w, h);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun     gc = xcb_generate_id(conn);
142*4882a593Smuzhiyun     xcb_create_gc(conn, gc, source, 0, NULL);
143*4882a593Smuzhiyun     image = xcb_image_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP,
144*4882a593Smuzhiyun                                     32, NULL, ~0, NULL);
145*4882a593Smuzhiyun     image->data = (void *)cursor->bits->argb;
146*4882a593Smuzhiyun     xcb_image_put(conn, source, gc, image, 0, 0, 0);
147*4882a593Smuzhiyun     xcb_free_gc(conn, gc);
148*4882a593Smuzhiyun     xcb_image_destroy(image);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun     picture = xcb_generate_id(conn);
151*4882a593Smuzhiyun     xcb_render_create_picture(conn, picture, source, get_argb_format(),
152*4882a593Smuzhiyun                               0, NULL);
153*4882a593Smuzhiyun     xcb_free_pixmap(conn, source);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun     hw->cursor = xcb_generate_id(conn);
156*4882a593Smuzhiyun     xcb_render_create_cursor(conn, hw->cursor, picture,
157*4882a593Smuzhiyun                              cursor->bits->xhot, cursor->bits->yhot);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun     xcb_render_free_picture(conn, picture);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun static Bool
can_argb_cursor(void)163*4882a593Smuzhiyun can_argb_cursor(void)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun     static const xcb_render_query_version_reply_t *v;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun     if (!v)
168*4882a593Smuzhiyun         v = xcb_render_util_query_version(hostx_get_xcbconn());
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun     return v->major_version == 0 && v->minor_version >= 5;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun static Bool
ephyrRealizeCursor(DeviceIntPtr dev,ScreenPtr screen,CursorPtr cursor)174*4882a593Smuzhiyun ephyrRealizeCursor(DeviceIntPtr dev, ScreenPtr screen, CursorPtr cursor)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun     KdScreenPriv(screen);
177*4882a593Smuzhiyun     KdScreenInfo *kscr = pScreenPriv->screen;
178*4882a593Smuzhiyun     EphyrScrPriv *scr = kscr->driver;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun     if (cursor->bits->argb && can_argb_cursor())
181*4882a593Smuzhiyun         ephyrRealizeARGBCursor(scr, cursor);
182*4882a593Smuzhiyun     else
183*4882a593Smuzhiyun     {
184*4882a593Smuzhiyun         ephyrRealizeCoreCursor(scr, cursor);
185*4882a593Smuzhiyun     }
186*4882a593Smuzhiyun     return TRUE;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun static Bool
ephyrUnrealizeCursor(DeviceIntPtr dev,ScreenPtr screen,CursorPtr cursor)190*4882a593Smuzhiyun ephyrUnrealizeCursor(DeviceIntPtr dev, ScreenPtr screen, CursorPtr cursor)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun     ephyrCursorPtr hw = ephyrGetCursor(cursor);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun     if (hw->cursor) {
195*4882a593Smuzhiyun         xcb_free_cursor(hostx_get_xcbconn(), hw->cursor);
196*4882a593Smuzhiyun         hw->cursor = None;
197*4882a593Smuzhiyun     }
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun     return TRUE;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun static void
ephyrSetCursor(DeviceIntPtr dev,ScreenPtr screen,CursorPtr cursor,int x,int y)203*4882a593Smuzhiyun ephyrSetCursor(DeviceIntPtr dev, ScreenPtr screen, CursorPtr cursor, int x,
204*4882a593Smuzhiyun                int y)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun     KdScreenPriv(screen);
207*4882a593Smuzhiyun     KdScreenInfo *kscr = pScreenPriv->screen;
208*4882a593Smuzhiyun     EphyrScrPriv *scr = kscr->driver;
209*4882a593Smuzhiyun     uint32_t attr = None;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun     if (cursor)
212*4882a593Smuzhiyun         attr = ephyrGetCursor(cursor)->cursor;
213*4882a593Smuzhiyun     else
214*4882a593Smuzhiyun         attr = hostx_get_empty_cursor();
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun     xcb_change_window_attributes(hostx_get_xcbconn(), scr->win,
217*4882a593Smuzhiyun                                  XCB_CW_CURSOR, &attr);
218*4882a593Smuzhiyun     xcb_flush(hostx_get_xcbconn());
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun static void
ephyrMoveCursor(DeviceIntPtr dev,ScreenPtr screen,int x,int y)222*4882a593Smuzhiyun ephyrMoveCursor(DeviceIntPtr dev, ScreenPtr screen, int x, int y)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun static Bool
ephyrDeviceCursorInitialize(DeviceIntPtr dev,ScreenPtr screen)227*4882a593Smuzhiyun ephyrDeviceCursorInitialize(DeviceIntPtr dev, ScreenPtr screen)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun     return TRUE;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static void
ephyrDeviceCursorCleanup(DeviceIntPtr dev,ScreenPtr screen)233*4882a593Smuzhiyun ephyrDeviceCursorCleanup(DeviceIntPtr dev, ScreenPtr screen)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun miPointerSpriteFuncRec EphyrPointerSpriteFuncs = {
238*4882a593Smuzhiyun     ephyrRealizeCursor,
239*4882a593Smuzhiyun     ephyrUnrealizeCursor,
240*4882a593Smuzhiyun     ephyrSetCursor,
241*4882a593Smuzhiyun     ephyrMoveCursor,
242*4882a593Smuzhiyun     ephyrDeviceCursorInitialize,
243*4882a593Smuzhiyun     ephyrDeviceCursorCleanup
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun Bool
ephyrCursorInit(ScreenPtr screen)247*4882a593Smuzhiyun ephyrCursorInit(ScreenPtr screen)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun     if (!dixRegisterPrivateKey(&ephyrCursorPrivateKey, PRIVATE_CURSOR_BITS,
250*4882a593Smuzhiyun                                sizeof(ephyrCursorRec)))
251*4882a593Smuzhiyun         return FALSE;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun     miPointerInitialize(screen,
254*4882a593Smuzhiyun                         &EphyrPointerSpriteFuncs,
255*4882a593Smuzhiyun                         &ephyrPointerScreenFuncs, FALSE);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun     return TRUE;
258*4882a593Smuzhiyun }
259