GetPntMap.c revision b4ee4795
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#define NEED_REPLIES 29#ifdef HAVE_CONFIG_H 30#include <config.h> 31#endif 32#include "Xlibint.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 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 nbytes = (long)rep.length << 2; 59 60 /* Don't count on the server returning a valid value */ 61 if (nbytes > sizeof mapping) { 62 remainder = nbytes - sizeof mapping; 63 nbytes = sizeof mapping; 64 } 65 _XRead (dpy, (char *)mapping, nbytes); 66 /* don't return more data than the user asked for. */ 67 if (rep.nElts) { 68 memcpy ((char *) map, (char *) mapping, 69 MIN((int)rep.nElts, nmaps) ); 70 } 71 72 if (remainder) 73 _XEatData(dpy, (unsigned long)remainder); 74 75 UnlockDisplay(dpy); 76 SyncHandle(); 77 return ((int) rep.nElts); 78} 79 80KeySym * 81XGetKeyboardMapping (Display *dpy, 82#if NeedWidePrototypes 83 unsigned int first_keycode, 84#else 85 KeyCode first_keycode, 86#endif 87 int count, 88 int *keysyms_per_keycode) 89{ 90 long nbytes; 91 unsigned long nkeysyms; 92 register KeySym *mapping = NULL; 93 xGetKeyboardMappingReply rep; 94 register xGetKeyboardMappingReq *req; 95 96 LockDisplay(dpy); 97 GetReq(GetKeyboardMapping, req); 98 req->firstKeyCode = first_keycode; 99 req->count = count; 100 if (! _XReply(dpy, (xReply *)&rep, 0, xFalse)) { 101 UnlockDisplay(dpy); 102 SyncHandle(); 103 return (KeySym *) NULL; 104 } 105 106 nkeysyms = (unsigned long) rep.length; 107 if (nkeysyms > 0) { 108 nbytes = nkeysyms * sizeof (KeySym); 109 mapping = (KeySym *) Xmalloc ((unsigned) nbytes); 110 nbytes = nkeysyms << 2; 111 if (! mapping) { 112 _XEatData(dpy, (unsigned long) nbytes); 113 UnlockDisplay(dpy); 114 SyncHandle(); 115 return (KeySym *) NULL; 116 } 117 _XRead32 (dpy, (long *) mapping, nbytes); 118 } 119 *keysyms_per_keycode = rep.keySymsPerKeyCode; 120 UnlockDisplay(dpy); 121 SyncHandle(); 122 return (mapping); 123} 124 125