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#ifdef HAVE_CONFIG_H 281ab64890Smrg#include <config.h> 291ab64890Smrg#endif 301ab64890Smrg#include "Xlibint.h" 31f2d49d05Smrg#include "reallocarray.h" 32eb411b4bSmrg#include <limits.h> 331ab64890Smrg 341ab64890SmrgXModifierKeymap * 351ab64890SmrgXGetModifierMapping(register Display *dpy) 3661b2299dSmrg{ 371ab64890Smrg xGetModifierMappingReply rep; 38bd8e7fb0Smrg _X_UNUSED register xReq *req; 391ab64890Smrg unsigned long nbytes; 401ab64890Smrg XModifierKeymap *res; 411ab64890Smrg 421ab64890Smrg LockDisplay(dpy); 431ab64890Smrg GetEmptyReq(GetModifierMapping, req); 441ab64890Smrg (void) _XReply (dpy, (xReply *)&rep, 0, xFalse); 451ab64890Smrg 46dac667f7Smrg if (rep.length < (INT_MAX >> 2) && 47dac667f7Smrg (rep.length >> 1) == rep.numKeyPerModifier) { 48eb411b4bSmrg nbytes = (unsigned long)rep.length << 2; 49eb411b4bSmrg res = Xmalloc(sizeof (XModifierKeymap)); 50eb411b4bSmrg if (res) 51eb411b4bSmrg res->modifiermap = Xmalloc (nbytes); 52eb411b4bSmrg } else 53eb411b4bSmrg res = NULL; 541ab64890Smrg if ((! res) || (! res->modifiermap)) { 55cf2acddeSmrg Xfree(res); 561ab64890Smrg res = (XModifierKeymap *) NULL; 57eb411b4bSmrg _XEatDataWords(dpy, rep.length); 581ab64890Smrg } else { 591ab64890Smrg _XReadPad(dpy, (char *) res->modifiermap, (long) nbytes); 601ab64890Smrg res->max_keypermod = rep.numKeyPerModifier; 611ab64890Smrg } 621ab64890Smrg 631ab64890Smrg UnlockDisplay(dpy); 641ab64890Smrg SyncHandle(); 651ab64890Smrg return (res); 661ab64890Smrg} 671ab64890Smrg 681ab64890Smrg/* 691ab64890Smrg * Returns: 70c555af55Smrg * MappingSuccess (0) Success 71c555af55Smrg * MappingBusy (1) Busy - one or more old or new modifiers are down 72c555af55Smrg * MappingFailed (2) Failed - one or more new modifiers unacceptable 731ab64890Smrg */ 741ab64890Smrgint 751ab64890SmrgXSetModifierMapping( 761ab64890Smrg register Display *dpy, 771ab64890Smrg register XModifierKeymap *modifier_map) 781ab64890Smrg{ 791ab64890Smrg register xSetModifierMappingReq *req; 801ab64890Smrg xSetModifierMappingReply rep; 811ab64890Smrg int mapSize = modifier_map->max_keypermod << 3; /* 8 modifiers */ 821ab64890Smrg 831ab64890Smrg LockDisplay(dpy); 84c555af55Smrg GetReq(SetModifierMapping, req); 85c555af55Smrg req->length += mapSize >> 2; 861ab64890Smrg req->numKeyPerModifier = modifier_map->max_keypermod; 871ab64890Smrg 88bd8e7fb0Smrg Data(dpy, (const char *)modifier_map->modifiermap, mapSize); 891ab64890Smrg 901ab64890Smrg (void) _XReply(dpy, (xReply *) & rep, 911ab64890Smrg (SIZEOF(xSetModifierMappingReply) - SIZEOF(xReply)) >> 2, xTrue); 921ab64890Smrg UnlockDisplay(dpy); 931ab64890Smrg SyncHandle(); 941ab64890Smrg return (rep.success); 951ab64890Smrg} 961ab64890Smrg 971ab64890SmrgXModifierKeymap * 981ab64890SmrgXNewModifiermap(int keyspermodifier) 991ab64890Smrg{ 100eb411b4bSmrg XModifierKeymap *res = Xmalloc((sizeof (XModifierKeymap))); 1011ab64890Smrg if (res) { 1021ab64890Smrg res->max_keypermod = keyspermodifier; 1031ab64890Smrg res->modifiermap = (keyspermodifier > 0 ? 104f2d49d05Smrg Xmallocarray(keyspermodifier, 8) 1051ab64890Smrg : (KeyCode *) NULL); 1061ab64890Smrg if (keyspermodifier && (res->modifiermap == NULL)) { 107c555af55Smrg Xfree(res); 1081ab64890Smrg return (XModifierKeymap *) NULL; 1091ab64890Smrg } 1101ab64890Smrg } 1111ab64890Smrg return (res); 1121ab64890Smrg} 1131ab64890Smrg 1141ab64890Smrg 1151ab64890Smrgint 1161ab64890SmrgXFreeModifiermap(XModifierKeymap *map) 1171ab64890Smrg{ 1181ab64890Smrg if (map) { 119cf2acddeSmrg Xfree(map->modifiermap); 120c555af55Smrg Xfree(map); 1211ab64890Smrg } 1221ab64890Smrg return 1; 1231ab64890Smrg} 1241ab64890Smrg 1251ab64890SmrgXModifierKeymap * 1261ab64890SmrgXInsertModifiermapEntry(XModifierKeymap *map, 1271ab64890Smrg#if NeedWidePrototypes 1281ab64890Smrg unsigned int keycode, 1291ab64890Smrg#else 1301ab64890Smrg KeyCode keycode, 1311ab64890Smrg#endif 1321ab64890Smrg int modifier) 1331ab64890Smrg{ 1341ab64890Smrg XModifierKeymap *newmap; 1351ab64890Smrg int i, 1361ab64890Smrg row = modifier * map->max_keypermod, 1371ab64890Smrg newrow, 1381ab64890Smrg lastrow; 1391ab64890Smrg 1401ab64890Smrg for (i=0; i<map->max_keypermod; i++) { 1411ab64890Smrg if (map->modifiermap[ row+i ] == keycode) 1421ab64890Smrg return(map); /* already in the map */ 1431ab64890Smrg if (map->modifiermap[ row+i ] == 0) { 1441ab64890Smrg map->modifiermap[ row+i ] = keycode; 1451ab64890Smrg return(map); /* we added it without stretching the map */ 1461ab64890Smrg } 14761b2299dSmrg } 1481ab64890Smrg 1491ab64890Smrg /* stretch the map */ 1501ab64890Smrg if ((newmap = XNewModifiermap(map->max_keypermod+1)) == NULL) 1511ab64890Smrg return (XModifierKeymap *) NULL; 1521ab64890Smrg newrow = row = 0; 1531ab64890Smrg lastrow = newmap->max_keypermod * 8; 1541ab64890Smrg while (newrow < lastrow) { 1551ab64890Smrg for (i=0; i<map->max_keypermod; i++) 1561ab64890Smrg newmap->modifiermap[ newrow+i ] = map->modifiermap[ row+i ]; 1571ab64890Smrg newmap->modifiermap[ newrow+i ] = 0; 1581ab64890Smrg row += map->max_keypermod; 1591ab64890Smrg newrow += newmap->max_keypermod; 1601ab64890Smrg } 1611ab64890Smrg (void) XFreeModifiermap(map); 1621ab64890Smrg newrow = newmap->max_keypermod * modifier + newmap->max_keypermod - 1; 1631ab64890Smrg newmap->modifiermap[ newrow ] = keycode; 1641ab64890Smrg return(newmap); 1651ab64890Smrg} 1661ab64890Smrg 1671ab64890SmrgXModifierKeymap * 1681ab64890SmrgXDeleteModifiermapEntry(XModifierKeymap *map, 1691ab64890Smrg#if NeedWidePrototypes 1701ab64890Smrg unsigned int keycode, 1711ab64890Smrg#else 1721ab64890Smrg KeyCode keycode, 1731ab64890Smrg#endif 1741ab64890Smrg int modifier) 1751ab64890Smrg{ 1761ab64890Smrg int i, 1771ab64890Smrg row = modifier * map->max_keypermod; 1781ab64890Smrg 1791ab64890Smrg for (i=0; i<map->max_keypermod; i++) { 1801ab64890Smrg if (map->modifiermap[ row+i ] == keycode) 1811ab64890Smrg map->modifiermap[ row+i ] = 0; 1821ab64890Smrg } 1831ab64890Smrg /* should we shrink the map?? */ 1841ab64890Smrg return (map); 1851ab64890Smrg} 186