GetPntMap.c revision 61b2299d
1/* $Xorg: GetPntMap.c,v 1.4 2001/02/09 02:03:33 xorgcvs Exp $ */
2/*
3
4Copyright 1986, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27
28/* $XFree86: xc/lib/X11/GetPntMap.c,v 1.6 2001/12/14 19:54:01 dawes Exp $ */
29
30#define NEED_REPLIES
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34#include "Xlibint.h"
35
36#ifdef MIN		/* some systems define this in <sys/param.h> */
37#undef MIN
38#endif
39#define MIN(a, b) ((a) < (b) ? (a) : (b))
40
41int XGetPointerMapping (
42    register Display *dpy,
43    unsigned char *map,	/* RETURN */
44    int nmaps)
45
46{
47    unsigned char mapping[256];	/* known fixed size */
48    long nbytes, remainder = 0;
49    xGetPointerMappingReply rep;
50    register xReq *req;
51
52    LockDisplay(dpy);
53    GetEmptyReq(GetPointerMapping, req);
54    if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) {
55	UnlockDisplay(dpy);
56	SyncHandle();
57	return 0;
58    }
59
60    nbytes = (long)rep.length << 2;
61
62    /* Don't count on the server returning a valid value */
63    if (nbytes > sizeof mapping) {
64	remainder = nbytes - sizeof mapping;
65	nbytes = sizeof mapping;
66    }
67    _XRead (dpy, (char *)mapping, nbytes);
68    /* don't return more data than the user asked for. */
69    if (rep.nElts) {
70	    memcpy ((char *) map, (char *) mapping,
71		MIN((int)rep.nElts, nmaps) );
72	}
73
74    if (remainder)
75	_XEatData(dpy, (unsigned long)remainder);
76
77    UnlockDisplay(dpy);
78    SyncHandle();
79    return ((int) rep.nElts);
80}
81
82KeySym *
83XGetKeyboardMapping (Display *dpy,
84#if NeedWidePrototypes
85			     unsigned int first_keycode,
86#else
87			     KeyCode first_keycode,
88#endif
89			     int count,
90			     int *keysyms_per_keycode)
91{
92    long nbytes;
93    unsigned long nkeysyms;
94    register KeySym *mapping = NULL;
95    xGetKeyboardMappingReply rep;
96    register xGetKeyboardMappingReq *req;
97
98    LockDisplay(dpy);
99    GetReq(GetKeyboardMapping, req);
100    req->firstKeyCode = first_keycode;
101    req->count = count;
102    if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) {
103	UnlockDisplay(dpy);
104	SyncHandle();
105	return (KeySym *) NULL;
106    }
107
108    nkeysyms = (unsigned long) rep.length;
109    if (nkeysyms > 0) {
110	nbytes = nkeysyms * sizeof (KeySym);
111	mapping = (KeySym *) Xmalloc ((unsigned) nbytes);
112	nbytes = nkeysyms << 2;
113	if (! mapping) {
114	    _XEatData(dpy, (unsigned long) nbytes);
115	    UnlockDisplay(dpy);
116	    SyncHandle();
117	    return (KeySym *) NULL;
118	}
119	_XRead32 (dpy, (long *) mapping, nbytes);
120    }
121    *keysyms_per_keycode = rep.keySymsPerKeyCode;
122    UnlockDisplay(dpy);
123    SyncHandle();
124    return (mapping);
125}
126
127