ModMap.c revision bd8e7fb0
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#ifdef HAVE_CONFIG_H 28#include <config.h> 29#endif 30#include "Xlibint.h" 31#include <limits.h> 32 33XModifierKeymap * 34XGetModifierMapping(register Display *dpy) 35{ 36 xGetModifierMappingReply rep; 37 _X_UNUSED register xReq *req; 38 unsigned long nbytes; 39 XModifierKeymap *res; 40 41 LockDisplay(dpy); 42 GetEmptyReq(GetModifierMapping, req); 43 (void) _XReply (dpy, (xReply *)&rep, 0, xFalse); 44 45 if (rep.length < (INT_MAX >> 2) && 46 (rep.length >> 1) == rep.numKeyPerModifier) { 47 nbytes = (unsigned long)rep.length << 2; 48 res = Xmalloc(sizeof (XModifierKeymap)); 49 if (res) 50 res->modifiermap = Xmalloc (nbytes); 51 } else 52 res = NULL; 53 if ((! res) || (! res->modifiermap)) { 54 Xfree(res); 55 res = (XModifierKeymap *) NULL; 56 _XEatDataWords(dpy, rep.length); 57 } else { 58 _XReadPad(dpy, (char *) res->modifiermap, (long) nbytes); 59 res->max_keypermod = rep.numKeyPerModifier; 60 } 61 62 UnlockDisplay(dpy); 63 SyncHandle(); 64 return (res); 65} 66 67/* 68 * Returns: 69 * MappingSuccess (0) Success 70 * MappingBusy (1) Busy - one or more old or new modifiers are down 71 * MappingFailed (2) Failed - one or more new modifiers unacceptable 72 */ 73int 74XSetModifierMapping( 75 register Display *dpy, 76 register XModifierKeymap *modifier_map) 77{ 78 register xSetModifierMappingReq *req; 79 xSetModifierMappingReply rep; 80 int mapSize = modifier_map->max_keypermod << 3; /* 8 modifiers */ 81 82 LockDisplay(dpy); 83 GetReq(SetModifierMapping, req); 84 req->length += mapSize >> 2; 85 req->numKeyPerModifier = modifier_map->max_keypermod; 86 87 Data(dpy, (const char *)modifier_map->modifiermap, mapSize); 88 89 (void) _XReply(dpy, (xReply *) & rep, 90 (SIZEOF(xSetModifierMappingReply) - SIZEOF(xReply)) >> 2, xTrue); 91 UnlockDisplay(dpy); 92 SyncHandle(); 93 return (rep.success); 94} 95 96XModifierKeymap * 97XNewModifiermap(int keyspermodifier) 98{ 99 XModifierKeymap *res = Xmalloc((sizeof (XModifierKeymap))); 100 if (res) { 101 res->max_keypermod = keyspermodifier; 102 res->modifiermap = (keyspermodifier > 0 ? 103 Xmalloc(8 * keyspermodifier) 104 : (KeyCode *) NULL); 105 if (keyspermodifier && (res->modifiermap == NULL)) { 106 Xfree(res); 107 return (XModifierKeymap *) NULL; 108 } 109 } 110 return (res); 111} 112 113 114int 115XFreeModifiermap(XModifierKeymap *map) 116{ 117 if (map) { 118 Xfree(map->modifiermap); 119 Xfree(map); 120 } 121 return 1; 122} 123 124XModifierKeymap * 125XInsertModifiermapEntry(XModifierKeymap *map, 126#if NeedWidePrototypes 127 unsigned int keycode, 128#else 129 KeyCode keycode, 130#endif 131 int modifier) 132{ 133 XModifierKeymap *newmap; 134 int i, 135 row = modifier * map->max_keypermod, 136 newrow, 137 lastrow; 138 139 for (i=0; i<map->max_keypermod; i++) { 140 if (map->modifiermap[ row+i ] == keycode) 141 return(map); /* already in the map */ 142 if (map->modifiermap[ row+i ] == 0) { 143 map->modifiermap[ row+i ] = keycode; 144 return(map); /* we added it without stretching the map */ 145 } 146 } 147 148 /* stretch the map */ 149 if ((newmap = XNewModifiermap(map->max_keypermod+1)) == NULL) 150 return (XModifierKeymap *) NULL; 151 newrow = row = 0; 152 lastrow = newmap->max_keypermod * 8; 153 while (newrow < lastrow) { 154 for (i=0; i<map->max_keypermod; i++) 155 newmap->modifiermap[ newrow+i ] = map->modifiermap[ row+i ]; 156 newmap->modifiermap[ newrow+i ] = 0; 157 row += map->max_keypermod; 158 newrow += newmap->max_keypermod; 159 } 160 (void) XFreeModifiermap(map); 161 newrow = newmap->max_keypermod * modifier + newmap->max_keypermod - 1; 162 newmap->modifiermap[ newrow ] = keycode; 163 return(newmap); 164} 165 166XModifierKeymap * 167XDeleteModifiermapEntry(XModifierKeymap *map, 168#if NeedWidePrototypes 169 unsigned int keycode, 170#else 171 KeyCode keycode, 172#endif 173 int modifier) 174{ 175 int i, 176 row = modifier * map->max_keypermod; 177 178 for (i=0; i<map->max_keypermod; i++) { 179 if (map->modifiermap[ row+i ] == keycode) 180 map->modifiermap[ row+i ] = 0; 181 } 182 /* should we shrink the map?? */ 183 return (map); 184} 185