GetPntMap.c revision 3233502e
11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1986, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg
281ab64890Smrg#ifdef HAVE_CONFIG_H
291ab64890Smrg#include <config.h>
301ab64890Smrg#endif
311ab64890Smrg#include "Xlibint.h"
32eb411b4bSmrg#include <limits.h>
331ab64890Smrg
341ab64890Smrg#ifdef MIN		/* some systems define this in <sys/param.h> */
351ab64890Smrg#undef MIN
361ab64890Smrg#endif
371ab64890Smrg#define MIN(a, b) ((a) < (b) ? (a) : (b))
381ab64890Smrg
391ab64890Smrgint XGetPointerMapping (
401ab64890Smrg    register Display *dpy,
411ab64890Smrg    unsigned char *map,	/* RETURN */
421ab64890Smrg    int nmaps)
431ab64890Smrg
441ab64890Smrg{
451ab64890Smrg    unsigned char mapping[256];	/* known fixed size */
46eb411b4bSmrg    unsigned long nbytes, remainder = 0;
471ab64890Smrg    xGetPointerMappingReply rep;
483233502eSmrg    _X_UNUSED register xReq *req;
491ab64890Smrg
501ab64890Smrg    LockDisplay(dpy);
511ab64890Smrg    GetEmptyReq(GetPointerMapping, req);
521ab64890Smrg    if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) {
531ab64890Smrg	UnlockDisplay(dpy);
541ab64890Smrg	SyncHandle();
551ab64890Smrg	return 0;
561ab64890Smrg    }
571ab64890Smrg
581ab64890Smrg    /* Don't count on the server returning a valid value */
59eb411b4bSmrg    if (rep.length >= (INT_MAX >> 2)) {
60eb411b4bSmrg	_XEatDataWords(dpy, rep.length);
61eb411b4bSmrg	UnlockDisplay(dpy);
62eb411b4bSmrg	SyncHandle();
63eb411b4bSmrg	return 0;
64eb411b4bSmrg    }
65eb411b4bSmrg
66eb411b4bSmrg    nbytes = (unsigned long) rep.length << 2;
671ab64890Smrg    if (nbytes > sizeof mapping) {
681ab64890Smrg	remainder = nbytes - sizeof mapping;
691ab64890Smrg	nbytes = sizeof mapping;
701ab64890Smrg    }
711ab64890Smrg    _XRead (dpy, (char *)mapping, nbytes);
721ab64890Smrg    /* don't return more data than the user asked for. */
731ab64890Smrg    if (rep.nElts) {
7461b2299dSmrg	    memcpy ((char *) map, (char *) mapping,
751ab64890Smrg		MIN((int)rep.nElts, nmaps) );
761ab64890Smrg	}
771ab64890Smrg
7861b2299dSmrg    if (remainder)
79eb411b4bSmrg	_XEatData(dpy, remainder);
801ab64890Smrg
811ab64890Smrg    UnlockDisplay(dpy);
821ab64890Smrg    SyncHandle();
831ab64890Smrg    return ((int) rep.nElts);
841ab64890Smrg}
851ab64890Smrg
861ab64890SmrgKeySym *
871ab64890SmrgXGetKeyboardMapping (Display *dpy,
881ab64890Smrg#if NeedWidePrototypes
891ab64890Smrg			     unsigned int first_keycode,
901ab64890Smrg#else
911ab64890Smrg			     KeyCode first_keycode,
921ab64890Smrg#endif
931ab64890Smrg			     int count,
941ab64890Smrg			     int *keysyms_per_keycode)
951ab64890Smrg{
96eb411b4bSmrg    unsigned long nbytes;
97eb411b4bSmrg    CARD32 nkeysyms;
981ab64890Smrg    register KeySym *mapping = NULL;
991ab64890Smrg    xGetKeyboardMappingReply rep;
1001ab64890Smrg    register xGetKeyboardMappingReq *req;
1011ab64890Smrg
1021ab64890Smrg    LockDisplay(dpy);
1031ab64890Smrg    GetReq(GetKeyboardMapping, req);
1041ab64890Smrg    req->firstKeyCode = first_keycode;
1051ab64890Smrg    req->count = count;
1061ab64890Smrg    if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) {
1071ab64890Smrg	UnlockDisplay(dpy);
1081ab64890Smrg	SyncHandle();
1091ab64890Smrg	return (KeySym *) NULL;
1101ab64890Smrg    }
1111ab64890Smrg
112eb411b4bSmrg    nkeysyms = rep.length;
1131ab64890Smrg    if (nkeysyms > 0) {
114eb411b4bSmrg	if (nkeysyms < (INT_MAX / sizeof (KeySym))) {
115eb411b4bSmrg	    nbytes = nkeysyms * sizeof (KeySym);
116eb411b4bSmrg	    mapping = Xmalloc (nbytes);
117eb411b4bSmrg	}
1181ab64890Smrg	if (! mapping) {
119eb411b4bSmrg	    _XEatDataWords(dpy, rep.length);
1201ab64890Smrg	    UnlockDisplay(dpy);
1211ab64890Smrg	    SyncHandle();
1221ab64890Smrg	    return (KeySym *) NULL;
1231ab64890Smrg	}
124eb411b4bSmrg	nbytes = nkeysyms << 2;
1251ab64890Smrg	_XRead32 (dpy, (long *) mapping, nbytes);
1261ab64890Smrg    }
1271ab64890Smrg    *keysyms_per_keycode = rep.keySymsPerKeyCode;
1281ab64890Smrg    UnlockDisplay(dpy);
1291ab64890Smrg    SyncHandle();
1301ab64890Smrg    return (mapping);
1311ab64890Smrg}
1321ab64890Smrg
133