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 "reallocarray.h"
33#include <limits.h>
34
35#ifdef MIN		/* some systems define this in <sys/param.h> */
36#undef MIN
37#endif
38#define MIN(a, b) ((a) < (b) ? (a) : (b))
39
40int XGetPointerMapping (
41    register Display *dpy,
42    unsigned char *map,	/* RETURN */
43    int nmaps)
44
45{
46    unsigned char mapping[256];	/* known fixed size */
47    unsigned long nbytes, remainder = 0;
48    xGetPointerMappingReply rep;
49    _X_UNUSED register xReq *req;
50
51    LockDisplay(dpy);
52    GetEmptyReq(GetPointerMapping, req);
53    if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) {
54	UnlockDisplay(dpy);
55	SyncHandle();
56	return 0;
57    }
58
59    /* Don't count on the server returning a valid value */
60    if (rep.length >= (INT_MAX >> 2)) {
61	_XEatDataWords(dpy, rep.length);
62	UnlockDisplay(dpy);
63	SyncHandle();
64	return 0;
65    }
66
67    nbytes = (unsigned long) rep.length << 2;
68    if (nbytes > sizeof mapping) {
69	remainder = nbytes - sizeof mapping;
70	nbytes = sizeof mapping;
71    }
72    _XRead (dpy, (char *)mapping, nbytes);
73    /* don't return more data than the user asked for. */
74    if (rep.nElts) {
75	    memcpy ((char *) map, (char *) mapping,
76		MIN((int)rep.nElts, nmaps) );
77	}
78
79    if (remainder)
80	_XEatData(dpy, remainder);
81
82    UnlockDisplay(dpy);
83    SyncHandle();
84    return ((int) rep.nElts);
85}
86
87KeySym *
88XGetKeyboardMapping (Display *dpy,
89#if NeedWidePrototypes
90			     unsigned int first_keycode,
91#else
92			     KeyCode first_keycode,
93#endif
94			     int count,
95			     int *keysyms_per_keycode)
96{
97    unsigned long nbytes;
98    CARD32 nkeysyms;
99    register KeySym *mapping = NULL;
100    xGetKeyboardMappingReply rep;
101    register xGetKeyboardMappingReq *req;
102
103    LockDisplay(dpy);
104    GetReq(GetKeyboardMapping, req);
105    req->firstKeyCode = first_keycode;
106    req->count = count;
107    if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) {
108	UnlockDisplay(dpy);
109	SyncHandle();
110	return (KeySym *) NULL;
111    }
112
113    nkeysyms = rep.length;
114    if (nkeysyms > 0) {
115	if (nkeysyms < (INT_MAX / sizeof (KeySym))) {
116	    mapping = Xmallocarray (nkeysyms, sizeof (KeySym));
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