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" 32258a0ebeSmrg#include "reallocarray.h" 33eb411b4bSmrg#include <limits.h> 341ab64890Smrg 351ab64890Smrg#ifdef MIN /* some systems define this in <sys/param.h> */ 361ab64890Smrg#undef MIN 371ab64890Smrg#endif 381ab64890Smrg#define MIN(a, b) ((a) < (b) ? (a) : (b)) 391ab64890Smrg 401ab64890Smrgint XGetPointerMapping ( 411ab64890Smrg register Display *dpy, 421ab64890Smrg unsigned char *map, /* RETURN */ 431ab64890Smrg int nmaps) 441ab64890Smrg 451ab64890Smrg{ 461ab64890Smrg unsigned char mapping[256]; /* known fixed size */ 47eb411b4bSmrg unsigned long nbytes, remainder = 0; 481ab64890Smrg xGetPointerMappingReply rep; 493233502eSmrg _X_UNUSED register xReq *req; 501ab64890Smrg 511ab64890Smrg LockDisplay(dpy); 521ab64890Smrg GetEmptyReq(GetPointerMapping, req); 531ab64890Smrg if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) { 541ab64890Smrg UnlockDisplay(dpy); 551ab64890Smrg SyncHandle(); 561ab64890Smrg return 0; 571ab64890Smrg } 581ab64890Smrg 591ab64890Smrg /* Don't count on the server returning a valid value */ 60eb411b4bSmrg if (rep.length >= (INT_MAX >> 2)) { 61eb411b4bSmrg _XEatDataWords(dpy, rep.length); 62eb411b4bSmrg UnlockDisplay(dpy); 63eb411b4bSmrg SyncHandle(); 64eb411b4bSmrg return 0; 65eb411b4bSmrg } 66eb411b4bSmrg 67eb411b4bSmrg nbytes = (unsigned long) rep.length << 2; 681ab64890Smrg if (nbytes > sizeof mapping) { 691ab64890Smrg remainder = nbytes - sizeof mapping; 701ab64890Smrg nbytes = sizeof mapping; 711ab64890Smrg } 721ab64890Smrg _XRead (dpy, (char *)mapping, nbytes); 731ab64890Smrg /* don't return more data than the user asked for. */ 741ab64890Smrg if (rep.nElts) { 7561b2299dSmrg memcpy ((char *) map, (char *) mapping, 761ab64890Smrg MIN((int)rep.nElts, nmaps) ); 771ab64890Smrg } 781ab64890Smrg 7961b2299dSmrg if (remainder) 80eb411b4bSmrg _XEatData(dpy, remainder); 811ab64890Smrg 821ab64890Smrg UnlockDisplay(dpy); 831ab64890Smrg SyncHandle(); 841ab64890Smrg return ((int) rep.nElts); 851ab64890Smrg} 861ab64890Smrg 871ab64890SmrgKeySym * 881ab64890SmrgXGetKeyboardMapping (Display *dpy, 891ab64890Smrg#if NeedWidePrototypes 901ab64890Smrg unsigned int first_keycode, 911ab64890Smrg#else 921ab64890Smrg KeyCode first_keycode, 931ab64890Smrg#endif 941ab64890Smrg int count, 951ab64890Smrg int *keysyms_per_keycode) 961ab64890Smrg{ 97eb411b4bSmrg unsigned long nbytes; 98eb411b4bSmrg CARD32 nkeysyms; 991ab64890Smrg register KeySym *mapping = NULL; 1001ab64890Smrg xGetKeyboardMappingReply rep; 1011ab64890Smrg register xGetKeyboardMappingReq *req; 1021ab64890Smrg 1031ab64890Smrg LockDisplay(dpy); 1041ab64890Smrg GetReq(GetKeyboardMapping, req); 1051ab64890Smrg req->firstKeyCode = first_keycode; 1061ab64890Smrg req->count = count; 1071ab64890Smrg if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) { 1081ab64890Smrg UnlockDisplay(dpy); 1091ab64890Smrg SyncHandle(); 1101ab64890Smrg return (KeySym *) NULL; 1111ab64890Smrg } 1121ab64890Smrg 113eb411b4bSmrg nkeysyms = rep.length; 1141ab64890Smrg if (nkeysyms > 0) { 115eb411b4bSmrg if (nkeysyms < (INT_MAX / sizeof (KeySym))) { 116258a0ebeSmrg mapping = Xmallocarray (nkeysyms, sizeof (KeySym)); 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