Cursor.c revision 521070a0
14456fccdSmrg/*
2521070a0Smrg * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
34456fccdSmrg *
4521070a0Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5521070a0Smrg * copy of this software and associated documentation files (the "Software"),
6521070a0Smrg * to deal in the Software without restriction, including without limitation
7521070a0Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8521070a0Smrg * and/or sell copies of the Software, and to permit persons to whom the
9521070a0Smrg * Software is furnished to do so, subject to the following conditions:
104456fccdSmrg *
11521070a0Smrg * The above copyright notice and this permission notice (including the next
12521070a0Smrg * paragraph) shall be included in all copies or substantial portions of the
13521070a0Smrg * Software.
144456fccdSmrg *
15521070a0Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16521070a0Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17521070a0Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18521070a0Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19521070a0Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20521070a0Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21521070a0Smrg * DEALINGS IN THE SOFTWARE.
22521070a0Smrg */
23521070a0Smrg/*
244456fccdSmrg * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
254456fccdSmrg *
264456fccdSmrg * Permission to use, copy, modify, distribute, and sell this software and its
274456fccdSmrg * documentation for any purpose is hereby granted without fee, provided that
284456fccdSmrg * the above copyright notice appear in all copies and that both that
294456fccdSmrg * copyright notice and this permission notice appear in supporting
304456fccdSmrg * documentation, and that the name of Keith Packard not be used in
314456fccdSmrg * advertising or publicity pertaining to distribution of the software without
324456fccdSmrg * specific, written prior permission.  Keith Packard makes no
334456fccdSmrg * representations about the suitability of this software for any purpose.  It
344456fccdSmrg * is provided "as is" without express or implied warranty.
354456fccdSmrg *
364456fccdSmrg * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
374456fccdSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
384456fccdSmrg * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
394456fccdSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
404456fccdSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
414456fccdSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
424456fccdSmrg * PERFORMANCE OF THIS SOFTWARE.
434456fccdSmrg */
444456fccdSmrg
454456fccdSmrg#ifdef HAVE_CONFIG_H
464456fccdSmrg#include <config.h>
474456fccdSmrg#endif
484456fccdSmrg#include "Xfixesint.h"
494456fccdSmrg
504456fccdSmrgvoid
514456fccdSmrgXFixesSelectCursorInput (Display	*dpy,
524456fccdSmrg			 Window		win,
534456fccdSmrg			 unsigned long	eventMask)
544456fccdSmrg{
554456fccdSmrg    XFixesExtDisplayInfo	    *info = XFixesFindDisplay (dpy);
564456fccdSmrg    xXFixesSelectCursorInputReq	    *req;
574456fccdSmrg
584456fccdSmrg    XFixesSimpleCheckExtension (dpy, info);
594456fccdSmrg
604456fccdSmrg    LockDisplay (dpy);
614456fccdSmrg    GetReq (XFixesSelectCursorInput, req);
624456fccdSmrg    req->reqType = info->codes->major_opcode;
634456fccdSmrg    req->xfixesReqType = X_XFixesSelectCursorInput;
644456fccdSmrg    req->window = win;
654456fccdSmrg    req->eventMask = eventMask;
664456fccdSmrg    UnlockDisplay (dpy);
674456fccdSmrg    SyncHandle ();
684456fccdSmrg}
694456fccdSmrg
704456fccdSmrgXFixesCursorImage *
714456fccdSmrgXFixesGetCursorImage (Display *dpy)
724456fccdSmrg{
734456fccdSmrg    XFixesExtDisplayInfo		*info = XFixesFindDisplay (dpy);
744456fccdSmrg    xXFixesGetCursorImageAndNameReq	*req;
754456fccdSmrg    xXFixesGetCursorImageAndNameReply	rep;
764456fccdSmrg    int					npixels;
774456fccdSmrg    int					nbytes_name;
784456fccdSmrg    int					nbytes, nread, rlength;
794456fccdSmrg    XFixesCursorImage			*image;
804456fccdSmrg    char				*name;
814456fccdSmrg
8242d69509Smrg    XFixesCheckExtension (dpy, info, NULL);
834456fccdSmrg    LockDisplay (dpy);
844456fccdSmrg    GetReq (XFixesGetCursorImageAndName, req);
854456fccdSmrg    req->reqType = info->codes->major_opcode;
864456fccdSmrg    if (info->major_version >= 2)
874456fccdSmrg	req->xfixesReqType = X_XFixesGetCursorImageAndName;
884456fccdSmrg    else
894456fccdSmrg	req->xfixesReqType = X_XFixesGetCursorImage;
904456fccdSmrg    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
914456fccdSmrg    {
924456fccdSmrg	UnlockDisplay (dpy);
934456fccdSmrg	SyncHandle ();
9442d69509Smrg	return NULL;
954456fccdSmrg    }
964456fccdSmrg    if (info->major_version < 2)
974456fccdSmrg    {
984456fccdSmrg	rep.cursorName = None;
994456fccdSmrg	rep.nbytes = 0;
1004456fccdSmrg    }
1014456fccdSmrg    npixels = rep.width * rep.height;
1024456fccdSmrg    nbytes_name = rep.nbytes;
1034456fccdSmrg    /* reply data length */
1044456fccdSmrg    nbytes = (long) rep.length << 2;
1054456fccdSmrg    /* bytes of actual data in the reply */
1064456fccdSmrg    nread = (npixels << 2) + nbytes_name;
1074456fccdSmrg    /* size of data returned to application */
1084456fccdSmrg    rlength = (sizeof (XFixesCursorImage) +
1094456fccdSmrg	       npixels * sizeof (unsigned long) +
1104456fccdSmrg	       nbytes_name + 1);
1114456fccdSmrg
1124456fccdSmrg    image = (XFixesCursorImage *) Xmalloc (rlength);
1134456fccdSmrg    if (!image)
1144456fccdSmrg    {
1154456fccdSmrg	_XEatData (dpy, nbytes);
1164456fccdSmrg	UnlockDisplay (dpy);
1174456fccdSmrg	SyncHandle ();
11842d69509Smrg	return NULL;
1194456fccdSmrg    }
1204456fccdSmrg    image->x = rep.x;
1214456fccdSmrg    image->y = rep.y;
1224456fccdSmrg    image->width = rep.width;
1234456fccdSmrg    image->height = rep.height;
1244456fccdSmrg    image->xhot = rep.xhot;
1254456fccdSmrg    image->yhot = rep.yhot;
1264456fccdSmrg    image->cursor_serial = rep.cursorSerial;
1274456fccdSmrg    image->pixels = (unsigned long *) (image + 1);
1284456fccdSmrg    image->atom = rep.cursorName;
1294456fccdSmrg    name = (char *) (image->pixels + npixels);
1304456fccdSmrg    image->name = name;
1314456fccdSmrg    _XRead32 (dpy, (long *) image->pixels, npixels << 2);
1324456fccdSmrg    _XRead (dpy, name, nbytes_name);
1334456fccdSmrg    name[nbytes_name] = '\0';	/* null-terminate */
1344456fccdSmrg    /* skip any padding */
1354456fccdSmrg    if(nbytes > nread)
1364456fccdSmrg    {
1374456fccdSmrg	_XEatData (dpy, (unsigned long) (nbytes - nread));
1384456fccdSmrg    }
1394456fccdSmrg    UnlockDisplay (dpy);
1404456fccdSmrg    SyncHandle ();
1414456fccdSmrg    return image;
1424456fccdSmrg}
1434456fccdSmrg
1444456fccdSmrgvoid
1454456fccdSmrgXFixesSetCursorName (Display *dpy, Cursor cursor, const char *name)
1464456fccdSmrg{
1474456fccdSmrg    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
1484456fccdSmrg    xXFixesSetCursorNameReq	*req;
1494456fccdSmrg    int				nbytes = strlen (name);
1504456fccdSmrg
1514456fccdSmrg    XFixesSimpleCheckExtension (dpy, info);
1524456fccdSmrg    if (info->major_version < 2)
1534456fccdSmrg	return;
1544456fccdSmrg    LockDisplay (dpy);
1554456fccdSmrg    GetReq (XFixesSetCursorName, req);
1564456fccdSmrg    req->reqType = info->codes->major_opcode;
1574456fccdSmrg    req->xfixesReqType = X_XFixesSetCursorName;
1584456fccdSmrg    req->cursor = cursor;
1594456fccdSmrg    req->nbytes = nbytes;
1604456fccdSmrg    req->length += (nbytes + 3) >> 2;
1614456fccdSmrg    Data (dpy, name, nbytes);
1624456fccdSmrg    UnlockDisplay (dpy);
1634456fccdSmrg    SyncHandle ();
1644456fccdSmrg}
1654456fccdSmrg
1664456fccdSmrgconst char *
1674456fccdSmrgXFixesGetCursorName (Display *dpy, Cursor cursor, Atom *atom)
1684456fccdSmrg{
1694456fccdSmrg    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
1704456fccdSmrg    xXFixesGetCursorNameReq	*req;
1714456fccdSmrg    xXFixesGetCursorNameReply	rep;
1724456fccdSmrg    char			*name;
1734456fccdSmrg
17442d69509Smrg    XFixesCheckExtension (dpy, info, NULL);
1754456fccdSmrg    if (info->major_version < 2)
17642d69509Smrg	return NULL;
1774456fccdSmrg    LockDisplay (dpy);
1784456fccdSmrg    GetReq (XFixesGetCursorName, req);
1794456fccdSmrg    req->reqType = info->codes->major_opcode;
1804456fccdSmrg    req->xfixesReqType = X_XFixesGetCursorName;
1814456fccdSmrg    req->cursor = cursor;
1824456fccdSmrg    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
1834456fccdSmrg    {
1844456fccdSmrg	UnlockDisplay (dpy);
1854456fccdSmrg	SyncHandle ();
18642d69509Smrg	return NULL;
1874456fccdSmrg    }
1884456fccdSmrg    *atom = rep.atom;
1894456fccdSmrg    if ((name = (char *) Xmalloc(rep.nbytes+1))) {
1904456fccdSmrg	_XReadPad(dpy, name, (long)rep.nbytes);
1914456fccdSmrg	name[rep.nbytes] = '\0';
1924456fccdSmrg    } else {
1934456fccdSmrg	_XEatData(dpy, (unsigned long) (rep.nbytes + 3) & ~3);
1944456fccdSmrg	name = (char *) NULL;
1954456fccdSmrg    }
1964456fccdSmrg    UnlockDisplay(dpy);
1974456fccdSmrg    SyncHandle();
1984456fccdSmrg    return(name);
1994456fccdSmrg}
2004456fccdSmrg
2014456fccdSmrgvoid
2024456fccdSmrgXFixesChangeCursor (Display *dpy, Cursor source, Cursor destination)
2034456fccdSmrg{
2044456fccdSmrg    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
2054456fccdSmrg    xXFixesChangeCursorReq	*req;
2064456fccdSmrg
2074456fccdSmrg    XFixesSimpleCheckExtension (dpy, info);
2084456fccdSmrg    if (info->major_version < 2)
2094456fccdSmrg	return;
2104456fccdSmrg    LockDisplay (dpy);
2114456fccdSmrg    GetReq (XFixesChangeCursor, req);
2124456fccdSmrg    req->reqType = info->codes->major_opcode;
2134456fccdSmrg    req->xfixesReqType = X_XFixesChangeCursor;
2144456fccdSmrg    req->source = source;
2154456fccdSmrg    req->destination = destination;
2164456fccdSmrg    UnlockDisplay(dpy);
2174456fccdSmrg    SyncHandle();
2184456fccdSmrg}
2194456fccdSmrg
2204456fccdSmrgvoid
2214456fccdSmrgXFixesChangeCursorByName (Display *dpy, Cursor source, const char *name)
2224456fccdSmrg{
2234456fccdSmrg    XFixesExtDisplayInfo	    *info = XFixesFindDisplay (dpy);
2244456fccdSmrg    xXFixesChangeCursorByNameReq    *req;
2254456fccdSmrg    int				    nbytes = strlen (name);
2264456fccdSmrg
2274456fccdSmrg    XFixesSimpleCheckExtension (dpy, info);
2284456fccdSmrg    if (info->major_version < 2)
2294456fccdSmrg	return;
2304456fccdSmrg    LockDisplay (dpy);
2314456fccdSmrg    GetReq (XFixesChangeCursorByName, req);
2324456fccdSmrg    req->reqType = info->codes->major_opcode;
2334456fccdSmrg    req->xfixesReqType = X_XFixesChangeCursorByName;
2344456fccdSmrg    req->source = source;
2354456fccdSmrg    req->nbytes = nbytes;
2364456fccdSmrg    req->length += (nbytes + 3) >> 2;
2374456fccdSmrg    Data (dpy, name, nbytes);
2384456fccdSmrg    UnlockDisplay(dpy);
2394456fccdSmrg    SyncHandle();
2404456fccdSmrg}
2414456fccdSmrg
2424456fccdSmrgvoid
2434456fccdSmrgXFixesHideCursor (Display *dpy, Window win)
2444456fccdSmrg{
2454456fccdSmrg    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
2464456fccdSmrg    xXFixesHideCursorReq	*req;
2474456fccdSmrg
2484456fccdSmrg    XFixesSimpleCheckExtension (dpy, info);
2494456fccdSmrg    if (info->major_version < 4)
2504456fccdSmrg	return;
2514456fccdSmrg    LockDisplay (dpy);
2524456fccdSmrg    GetReq (XFixesHideCursor, req);
2534456fccdSmrg    req->reqType = info->codes->major_opcode;
2544456fccdSmrg    req->xfixesReqType = X_XFixesHideCursor;
2554456fccdSmrg    req->window = win;
2564456fccdSmrg    UnlockDisplay (dpy);
2574456fccdSmrg    SyncHandle ();
2584456fccdSmrg}
2594456fccdSmrg
2604456fccdSmrgvoid
2614456fccdSmrgXFixesShowCursor (Display *dpy, Window win)
2624456fccdSmrg{
2634456fccdSmrg    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
2644456fccdSmrg    xXFixesShowCursorReq	*req;
2654456fccdSmrg
2664456fccdSmrg    XFixesSimpleCheckExtension (dpy, info);
2674456fccdSmrg    if (info->major_version < 4)
2684456fccdSmrg	return;
2694456fccdSmrg    LockDisplay (dpy);
2704456fccdSmrg    GetReq (XFixesShowCursor, req);
2714456fccdSmrg    req->reqType = info->codes->major_opcode;
2724456fccdSmrg    req->xfixesReqType = X_XFixesShowCursor;
2734456fccdSmrg    req->window = win;
2744456fccdSmrg    UnlockDisplay (dpy);
2754456fccdSmrg    SyncHandle ();
2764456fccdSmrg}
277