ModMap.c revision bd8e7fb0
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"
31eb411b4bSmrg#include <limits.h>
321ab64890Smrg
331ab64890SmrgXModifierKeymap *
341ab64890SmrgXGetModifierMapping(register Display *dpy)
3561b2299dSmrg{
361ab64890Smrg    xGetModifierMappingReply rep;
37bd8e7fb0Smrg    _X_UNUSED register xReq *req;
381ab64890Smrg    unsigned long nbytes;
391ab64890Smrg    XModifierKeymap *res;
401ab64890Smrg
411ab64890Smrg    LockDisplay(dpy);
421ab64890Smrg    GetEmptyReq(GetModifierMapping, req);
431ab64890Smrg    (void) _XReply (dpy, (xReply *)&rep, 0, xFalse);
441ab64890Smrg
45dac667f7Smrg    if (rep.length < (INT_MAX >> 2) &&
46dac667f7Smrg	(rep.length >> 1) == rep.numKeyPerModifier) {
47eb411b4bSmrg	nbytes = (unsigned long)rep.length << 2;
48eb411b4bSmrg	res = Xmalloc(sizeof (XModifierKeymap));
49eb411b4bSmrg	if (res)
50eb411b4bSmrg	    res->modifiermap = Xmalloc (nbytes);
51eb411b4bSmrg    } else
52eb411b4bSmrg	res = NULL;
531ab64890Smrg    if ((! res) || (! res->modifiermap)) {
54cf2acddeSmrg	Xfree(res);
551ab64890Smrg	res = (XModifierKeymap *) NULL;
56eb411b4bSmrg	_XEatDataWords(dpy, rep.length);
571ab64890Smrg    } else {
581ab64890Smrg	_XReadPad(dpy, (char *) res->modifiermap, (long) nbytes);
591ab64890Smrg	res->max_keypermod = rep.numKeyPerModifier;
601ab64890Smrg    }
611ab64890Smrg
621ab64890Smrg    UnlockDisplay(dpy);
631ab64890Smrg    SyncHandle();
641ab64890Smrg    return (res);
651ab64890Smrg}
661ab64890Smrg
671ab64890Smrg/*
681ab64890Smrg *	Returns:
69c555af55Smrg *	MappingSuccess (0)	Success
70c555af55Smrg *	MappingBusy (1) 	Busy - one or more old or new modifiers are down
71c555af55Smrg *	MappingFailed (2)	Failed - one or more new modifiers unacceptable
721ab64890Smrg */
731ab64890Smrgint
741ab64890SmrgXSetModifierMapping(
751ab64890Smrg    register Display *dpy,
761ab64890Smrg    register XModifierKeymap *modifier_map)
771ab64890Smrg{
781ab64890Smrg    register xSetModifierMappingReq *req;
791ab64890Smrg    xSetModifierMappingReply rep;
801ab64890Smrg    int         mapSize = modifier_map->max_keypermod << 3;	/* 8 modifiers */
811ab64890Smrg
821ab64890Smrg    LockDisplay(dpy);
83c555af55Smrg    GetReq(SetModifierMapping, req);
84c555af55Smrg    req->length += mapSize >> 2;
851ab64890Smrg    req->numKeyPerModifier = modifier_map->max_keypermod;
861ab64890Smrg
87bd8e7fb0Smrg    Data(dpy, (const char *)modifier_map->modifiermap, mapSize);
881ab64890Smrg
891ab64890Smrg    (void) _XReply(dpy, (xReply *) & rep,
901ab64890Smrg	(SIZEOF(xSetModifierMappingReply) - SIZEOF(xReply)) >> 2, xTrue);
911ab64890Smrg    UnlockDisplay(dpy);
921ab64890Smrg    SyncHandle();
931ab64890Smrg    return (rep.success);
941ab64890Smrg}
951ab64890Smrg
961ab64890SmrgXModifierKeymap *
971ab64890SmrgXNewModifiermap(int keyspermodifier)
981ab64890Smrg{
99eb411b4bSmrg    XModifierKeymap *res = Xmalloc((sizeof (XModifierKeymap)));
1001ab64890Smrg    if (res) {
1011ab64890Smrg	res->max_keypermod = keyspermodifier;
1021ab64890Smrg	res->modifiermap = (keyspermodifier > 0 ?
103eb411b4bSmrg			    Xmalloc(8 * keyspermodifier)
1041ab64890Smrg			    : (KeyCode *) NULL);
1051ab64890Smrg	if (keyspermodifier && (res->modifiermap == NULL)) {
106c555af55Smrg	    Xfree(res);
1071ab64890Smrg	    return (XModifierKeymap *) NULL;
1081ab64890Smrg	}
1091ab64890Smrg    }
1101ab64890Smrg    return (res);
1111ab64890Smrg}
1121ab64890Smrg
1131ab64890Smrg
1141ab64890Smrgint
1151ab64890SmrgXFreeModifiermap(XModifierKeymap *map)
1161ab64890Smrg{
1171ab64890Smrg    if (map) {
118cf2acddeSmrg        Xfree(map->modifiermap);
119c555af55Smrg	Xfree(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