ModMap.c revision 61b2299d
11ab64890Smrg/* $Xorg: ModMap.c,v 1.4 2001/02/09 02:03:34 xorgcvs Exp $ */ 21ab64890Smrg/* 31ab64890Smrg 41ab64890SmrgCopyright 1986, 1998 The Open Group 51ab64890Smrg 61ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its 71ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that 81ab64890Smrgthe above copyright notice appear in all copies and that both that 91ab64890Smrgcopyright notice and this permission notice appear in supporting 101ab64890Smrgdocumentation. 111ab64890Smrg 121ab64890SmrgThe above copyright notice and this permission notice shall be included in 131ab64890Smrgall copies or substantial portions of the Software. 141ab64890Smrg 151ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 161ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 171ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 181ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 191ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 201ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 211ab64890Smrg 221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be 231ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings 241ab64890Smrgin this Software without prior written authorization from The Open Group. 251ab64890Smrg 261ab64890Smrg*/ 271ab64890Smrg/* $XFree86: xc/lib/X11/ModMap.c,v 1.4 2001/12/14 19:54:03 dawes Exp $ */ 281ab64890Smrg 291ab64890Smrg#define NEED_REPLIES 301ab64890Smrg#ifdef HAVE_CONFIG_H 311ab64890Smrg#include <config.h> 321ab64890Smrg#endif 331ab64890Smrg#include "Xlibint.h" 341ab64890Smrg 351ab64890SmrgXModifierKeymap * 361ab64890SmrgXGetModifierMapping(register Display *dpy) 3761b2299dSmrg{ 381ab64890Smrg xGetModifierMappingReply rep; 391ab64890Smrg register xReq *req; 401ab64890Smrg unsigned long nbytes; 411ab64890Smrg XModifierKeymap *res; 421ab64890Smrg 431ab64890Smrg LockDisplay(dpy); 441ab64890Smrg GetEmptyReq(GetModifierMapping, req); 451ab64890Smrg (void) _XReply (dpy, (xReply *)&rep, 0, xFalse); 461ab64890Smrg 471ab64890Smrg nbytes = (unsigned long)rep.length << 2; 481ab64890Smrg res = (XModifierKeymap *) Xmalloc(sizeof (XModifierKeymap)); 491ab64890Smrg if (res) res->modifiermap = (KeyCode *) Xmalloc ((unsigned) nbytes); 501ab64890Smrg if ((! res) || (! res->modifiermap)) { 511ab64890Smrg if (res) Xfree((char *) res); 521ab64890Smrg res = (XModifierKeymap *) NULL; 531ab64890Smrg _XEatData(dpy, nbytes); 541ab64890Smrg } else { 551ab64890Smrg _XReadPad(dpy, (char *) res->modifiermap, (long) nbytes); 561ab64890Smrg res->max_keypermod = rep.numKeyPerModifier; 571ab64890Smrg } 581ab64890Smrg 591ab64890Smrg UnlockDisplay(dpy); 601ab64890Smrg SyncHandle(); 611ab64890Smrg return (res); 621ab64890Smrg} 631ab64890Smrg 641ab64890Smrg/* 651ab64890Smrg * Returns: 661ab64890Smrg * 0 Success 671ab64890Smrg * 1 Busy - one or more old or new modifiers are down 681ab64890Smrg * 2 Failed - one or more new modifiers unacceptable 691ab64890Smrg */ 701ab64890Smrgint 711ab64890SmrgXSetModifierMapping( 721ab64890Smrg register Display *dpy, 731ab64890Smrg register XModifierKeymap *modifier_map) 741ab64890Smrg{ 751ab64890Smrg register xSetModifierMappingReq *req; 761ab64890Smrg xSetModifierMappingReply rep; 771ab64890Smrg int mapSize = modifier_map->max_keypermod << 3; /* 8 modifiers */ 781ab64890Smrg 791ab64890Smrg LockDisplay(dpy); 801ab64890Smrg GetReqExtra(SetModifierMapping, mapSize, req); 811ab64890Smrg 821ab64890Smrg req->numKeyPerModifier = modifier_map->max_keypermod; 831ab64890Smrg 841ab64890Smrg memcpy((char *) NEXTPTR(req,xSetModifierMappingReq), 851ab64890Smrg (char *) modifier_map->modifiermap, 861ab64890Smrg mapSize); 871ab64890Smrg 881ab64890Smrg (void) _XReply(dpy, (xReply *) & rep, 891ab64890Smrg (SIZEOF(xSetModifierMappingReply) - SIZEOF(xReply)) >> 2, xTrue); 901ab64890Smrg UnlockDisplay(dpy); 911ab64890Smrg SyncHandle(); 921ab64890Smrg return (rep.success); 931ab64890Smrg} 941ab64890Smrg 951ab64890SmrgXModifierKeymap * 961ab64890SmrgXNewModifiermap(int keyspermodifier) 971ab64890Smrg{ 981ab64890Smrg XModifierKeymap *res = (XModifierKeymap *) Xmalloc((sizeof (XModifierKeymap))); 991ab64890Smrg if (res) { 1001ab64890Smrg res->max_keypermod = keyspermodifier; 1011ab64890Smrg res->modifiermap = (keyspermodifier > 0 ? 1021ab64890Smrg (KeyCode *) Xmalloc((unsigned) (8 * keyspermodifier)) 1031ab64890Smrg : (KeyCode *) NULL); 1041ab64890Smrg if (keyspermodifier && (res->modifiermap == NULL)) { 1051ab64890Smrg Xfree((char *) res); 1061ab64890Smrg return (XModifierKeymap *) NULL; 1071ab64890Smrg } 1081ab64890Smrg } 1091ab64890Smrg return (res); 1101ab64890Smrg} 1111ab64890Smrg 1121ab64890Smrg 1131ab64890Smrgint 1141ab64890SmrgXFreeModifiermap(XModifierKeymap *map) 1151ab64890Smrg{ 1161ab64890Smrg if (map) { 1171ab64890Smrg if (map->modifiermap) 1181ab64890Smrg Xfree((char *) map->modifiermap); 1191ab64890Smrg Xfree((char *) map); 1201ab64890Smrg } 1211ab64890Smrg return 1; 1221ab64890Smrg} 1231ab64890Smrg 1241ab64890SmrgXModifierKeymap * 1251ab64890SmrgXInsertModifiermapEntry(XModifierKeymap *map, 1261ab64890Smrg#if NeedWidePrototypes 1271ab64890Smrg unsigned int keycode, 1281ab64890Smrg#else 1291ab64890Smrg KeyCode keycode, 1301ab64890Smrg#endif 1311ab64890Smrg int modifier) 1321ab64890Smrg{ 1331ab64890Smrg XModifierKeymap *newmap; 1341ab64890Smrg int i, 1351ab64890Smrg row = modifier * map->max_keypermod, 1361ab64890Smrg newrow, 1371ab64890Smrg lastrow; 1381ab64890Smrg 1391ab64890Smrg for (i=0; i<map->max_keypermod; i++) { 1401ab64890Smrg if (map->modifiermap[ row+i ] == keycode) 1411ab64890Smrg return(map); /* already in the map */ 1421ab64890Smrg if (map->modifiermap[ row+i ] == 0) { 1431ab64890Smrg map->modifiermap[ row+i ] = keycode; 1441ab64890Smrg return(map); /* we added it without stretching the map */ 1451ab64890Smrg } 14661b2299dSmrg } 1471ab64890Smrg 1481ab64890Smrg /* stretch the map */ 1491ab64890Smrg if ((newmap = XNewModifiermap(map->max_keypermod+1)) == NULL) 1501ab64890Smrg return (XModifierKeymap *) NULL; 1511ab64890Smrg newrow = row = 0; 1521ab64890Smrg lastrow = newmap->max_keypermod * 8; 1531ab64890Smrg while (newrow < lastrow) { 1541ab64890Smrg for (i=0; i<map->max_keypermod; i++) 1551ab64890Smrg newmap->modifiermap[ newrow+i ] = map->modifiermap[ row+i ]; 1561ab64890Smrg newmap->modifiermap[ newrow+i ] = 0; 1571ab64890Smrg row += map->max_keypermod; 1581ab64890Smrg newrow += newmap->max_keypermod; 1591ab64890Smrg } 1601ab64890Smrg (void) XFreeModifiermap(map); 1611ab64890Smrg newrow = newmap->max_keypermod * modifier + newmap->max_keypermod - 1; 1621ab64890Smrg newmap->modifiermap[ newrow ] = keycode; 1631ab64890Smrg return(newmap); 1641ab64890Smrg} 1651ab64890Smrg 1661ab64890SmrgXModifierKeymap * 1671ab64890SmrgXDeleteModifiermapEntry(XModifierKeymap *map, 1681ab64890Smrg#if NeedWidePrototypes 1691ab64890Smrg unsigned int keycode, 1701ab64890Smrg#else 1711ab64890Smrg KeyCode keycode, 1721ab64890Smrg#endif 1731ab64890Smrg int modifier) 1741ab64890Smrg{ 1751ab64890Smrg int i, 1761ab64890Smrg row = modifier * map->max_keypermod; 1771ab64890Smrg 1781ab64890Smrg for (i=0; i<map->max_keypermod; i++) { 1791ab64890Smrg if (map->modifiermap[ row+i ] == keycode) 1801ab64890Smrg map->modifiermap[ row+i ] = 0; 1811ab64890Smrg } 1821ab64890Smrg /* should we shrink the map?? */ 1831ab64890Smrg return (map); 1841ab64890Smrg} 185