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