Xcup.c 7.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/* $XFree86: xc/lib/Xext/Xcup.c,v 1.8 2002/10/16 02:19:22 dawes Exp $ */
/*

Copyright 1987, 1988, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/
/* $Xorg: Xcup.c,v 1.5 2001/02/09 02:03:49 xorgcvs Exp $ */

#ifdef WIN32
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/Xwindows.h>
#endif

#define NEED_EVENTS
#define NEED_REPLIES
#include <X11/Xlibint.h>
#include <X11/extensions/Xcupstr.h>
#include <X11/extensions/Xext.h>
#include <X11/extensions/extutil.h>

static XExtensionInfo _xcup_info_data;
static XExtensionInfo *xcup_info = &_xcup_info_data;
static char *xcup_extension_name = XCUPNAME;

/*****************************************************************************
 *                                                                           *
 *			   private utility routines                          *
 *                                                                           *
 *****************************************************************************/

static int close_display(Display *dpy, XExtCodes *codes);
static /* const */ XExtensionHooks xcup_extension_hooks = {
    NULL,				/* create_gc */
    NULL,				/* copy_gc */
    NULL,				/* flush_gc */
    NULL,				/* free_gc */
    NULL,				/* create_font */
    NULL,				/* free_font */
    close_display,			/* close_display */
    NULL,				/* wire_to_event */
    NULL,				/* event_to_wire */
    NULL,				/* error */
    NULL,				/* error_string */
};

static XEXT_GENERATE_FIND_DISPLAY (find_display, xcup_info, 
				   xcup_extension_name, 
				   &xcup_extension_hooks, 
				   0, NULL)

static XEXT_GENERATE_CLOSE_DISPLAY (close_display, xcup_info)


/*****************************************************************************
 *                                                                           *
 *		    public Xcup Extension routines                           *
 *                                                                           *
 *****************************************************************************/

Status
XcupQueryVersion(
    Display* dpy,
    int* major_version_return,
    int* minor_version_return)
{
    XExtDisplayInfo *info = find_display (dpy);
    xXcupQueryVersionReply rep;
    xXcupQueryVersionReq *req;

    XextCheckExtension (dpy, info, xcup_extension_name, False);

    LockDisplay(dpy);
    GetReq(XcupQueryVersion, req);
    req->reqType = info->codes->major_opcode;
    req->xcupReqType = X_XcupQueryVersion;
    req->client_major_version = XCUP_MAJOR_VERSION;
    req->client_minor_version = XCUP_MINOR_VERSION;
    if (!_XReply(dpy, (xReply *)&rep, 0, xTrue)) {
	UnlockDisplay(dpy);
	SyncHandle();
	return False;
    }
    *major_version_return = rep.server_major_version;
    *minor_version_return = rep.server_minor_version;
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

/* Win32 reserves 20 colormap entries for its desktop */
#ifndef TYP_RESERVED_ENTRIES
#define TYP_RESERVED_ENTRIES 20
#endif

Status 
XcupGetReservedColormapEntries(
    Display* dpy,
    int screen,
    XColor** colors_out,
    int* ncolors)
{
    XExtDisplayInfo *info = find_display (dpy);
    xXcupGetReservedColormapEntriesReply rep;
    xXcupGetReservedColormapEntriesReq *req;
    xColorItem rbuf[TYP_RESERVED_ENTRIES];

    *ncolors = 0;

    XextCheckExtension (dpy, info, xcup_extension_name, False);

    LockDisplay(dpy);
    GetReq(XcupGetReservedColormapEntries, req);
    req->reqType = info->codes->major_opcode;
    req->xcupReqType = X_XcupGetReservedColormapEntries;
    req->screen = screen;
    if (_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
	long nbytes;
	xColorItem* rbufp;
	int nentries = rep.length / 3;

	nbytes = nentries * SIZEOF (xColorItem);
	if (nentries > TYP_RESERVED_ENTRIES)
	    rbufp = (xColorItem*) Xmalloc (nbytes);
	else
	    rbufp = rbuf;

	if (rbufp == NULL) {
	    _XEatData (dpy, (unsigned long) nbytes);
	    UnlockDisplay (dpy);
	    SyncHandle ();
	    return False;
	}
	_XRead (dpy, (char*) rbufp, nbytes);

	*colors_out = (XColor*) Xmalloc (nentries  * sizeof (XColor));
	if (*colors_out) {
	    xColorItem* cs = (xColorItem *) rbufp;
	    XColor* cd = *colors_out;
	    int i;

	    *ncolors = nentries;
	    for (i = 0; i < *ncolors; i++, cd++) {
		cd->pixel = cs->pixel;
		cd->red = cs->red;
		cd->green = cs->green;
		cd->blue = cs->blue;
		cs = (xColorItem*) (((char*) cs) + SIZEOF(xColorItem));
	    }
	    if (rbufp != rbuf) XFree ((char*) rbufp);
	    UnlockDisplay(dpy);
    	    SyncHandle();
	    return True;
	}
	if (rbufp != rbuf) XFree ((char*) rbufp);
    }
    UnlockDisplay(dpy);
    SyncHandle();
    return False;
}

Status
XcupStoreColors(
    Display* dpy,
    Colormap colormap,
    XColor* colors_in_out,
    int ncolors)
{
    XExtDisplayInfo *info = find_display (dpy);
    xXcupStoreColorsReply rep;
    xXcupStoreColorsReq *req;
    xColorItem rbuf[256];
    xColorItem citem;
    int i;
    XColor* xcp;

    XextCheckExtension (dpy, info, xcup_extension_name, False);

    LockDisplay(dpy);
    GetReq(XcupStoreColors, req);
    req->reqType = info->codes->major_opcode;
    req->xcupReqType = X_XcupStoreColors;
    req->cmap = colormap;
    req->length += (ncolors * SIZEOF(xColorItem)) >> 2;

    for (i = 0, xcp = colors_in_out; i < ncolors; i++, xcp++) {
	citem.pixel = xcp->pixel;
	citem.red = xcp->red;
	citem.green = xcp->green;
	citem.blue = xcp->blue;

	/* note that xColorItem doesn't contain all 16-bit quantities, so
	   we can't use Data16 */
	Data(dpy, (char *)&citem, (long) SIZEOF(xColorItem));
    }

    if (_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
	long nbytes;
	xColorItem* rbufp;
	xColorItem* cs;
	int nentries = rep.length / 3;

	nbytes = nentries * SIZEOF (xColorItem);

	if (nentries != ncolors) {
	    _XEatData (dpy, (unsigned long) nbytes);
	    UnlockDisplay (dpy);
	    SyncHandle ();
	    return False;
	}

	if (ncolors > 256)
	    rbufp = (xColorItem*) Xmalloc (nbytes);
	else
	    rbufp = rbuf;

	if (rbufp == NULL) {
	    _XEatData (dpy, (unsigned long) nbytes);
	    UnlockDisplay (dpy);
	    SyncHandle ();
	    return False;

	}

	_XRead (dpy, (char*) rbufp, nbytes);

	for (i = 0, xcp = colors_in_out, cs = rbufp; i < ncolors; i++, xcp++, cs++) {
	    xcp->pixel = cs->pixel;
	    xcp->red = cs->red;
	    xcp->green = cs->green;
	    xcp->blue = cs->blue;
	    xcp->flags = cs->flags;
	}
	if (rbufp != rbuf) XFree ((char*)rbufp);

	UnlockDisplay(dpy);
	SyncHandle();
	return True;
    }
    UnlockDisplay(dpy);
    SyncHandle();
    return False;
}