ModMap.c revision c555af55
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 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 nbytes = (unsigned long)rep.length << 2; 47 res = Xmalloc(sizeof (XModifierKeymap)); 48 if (res) 49 res->modifiermap = Xmalloc (nbytes); 50 } else 51 res = NULL; 52 if ((! res) || (! res->modifiermap)) { 53 if (res) Xfree(res); 54 res = (XModifierKeymap *) NULL; 55 _XEatDataWords(dpy, rep.length); 56 } else { 57 _XReadPad(dpy, (char *) res->modifiermap, (long) nbytes); 58 res->max_keypermod = rep.numKeyPerModifier; 59 } 60 61 UnlockDisplay(dpy); 62 SyncHandle(); 63 return (res); 64} 65 66/* 67 * Returns: 68 * MappingSuccess (0) Success 69 * MappingBusy (1) Busy - one or more old or new modifiers are down 70 * MappingFailed (2) Failed - one or more new modifiers unacceptable 71 */ 72int 73XSetModifierMapping( 74 register Display *dpy, 75 register XModifierKeymap *modifier_map) 76{ 77 register xSetModifierMappingReq *req; 78 xSetModifierMappingReply rep; 79 int mapSize = modifier_map->max_keypermod << 3; /* 8 modifiers */ 80 81 LockDisplay(dpy); 82 GetReq(SetModifierMapping, req); 83 req->length += mapSize >> 2; 84 req->numKeyPerModifier = modifier_map->max_keypermod; 85 86 Data(dpy, modifier_map->modifiermap, mapSize); 87 88 (void) _XReply(dpy, (xReply *) & rep, 89 (SIZEOF(xSetModifierMappingReply) - SIZEOF(xReply)) >> 2, xTrue); 90 UnlockDisplay(dpy); 91 SyncHandle(); 92 return (rep.success); 93} 94 95XModifierKeymap * 96XNewModifiermap(int keyspermodifier) 97{ 98 XModifierKeymap *res = Xmalloc((sizeof (XModifierKeymap))); 99 if (res) { 100 res->max_keypermod = keyspermodifier; 101 res->modifiermap = (keyspermodifier > 0 ? 102 Xmalloc(8 * keyspermodifier) 103 : (KeyCode *) NULL); 104 if (keyspermodifier && (res->modifiermap == NULL)) { 105 Xfree(res); 106 return (XModifierKeymap *) NULL; 107 } 108 } 109 return (res); 110} 111 112 113int 114XFreeModifiermap(XModifierKeymap *map) 115{ 116 if (map) { 117 if (map->modifiermap) 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