ModMap.c revision 61b2299d
1/* $Xorg: ModMap.c,v 1.4 2001/02/09 02:03:34 xorgcvs Exp $ */
2/*
3
4Copyright 1986, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27/* $XFree86: xc/lib/X11/ModMap.c,v 1.4 2001/12/14 19:54:03 dawes Exp $ */
28
29#define NEED_REPLIES
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include "Xlibint.h"
34
35XModifierKeymap *
36XGetModifierMapping(register Display *dpy)
37{
38    xGetModifierMappingReply rep;
39    register xReq *req;
40    unsigned long nbytes;
41    XModifierKeymap *res;
42
43    LockDisplay(dpy);
44    GetEmptyReq(GetModifierMapping, req);
45    (void) _XReply (dpy, (xReply *)&rep, 0, xFalse);
46
47    nbytes = (unsigned long)rep.length << 2;
48    res = (XModifierKeymap *) Xmalloc(sizeof (XModifierKeymap));
49    if (res) res->modifiermap = (KeyCode *) Xmalloc ((unsigned) nbytes);
50    if ((! res) || (! res->modifiermap)) {
51	if (res) Xfree((char *) res);
52	res = (XModifierKeymap *) NULL;
53	_XEatData(dpy, nbytes);
54    } else {
55	_XReadPad(dpy, (char *) res->modifiermap, (long) nbytes);
56	res->max_keypermod = rep.numKeyPerModifier;
57    }
58
59    UnlockDisplay(dpy);
60    SyncHandle();
61    return (res);
62}
63
64/*
65 *	Returns:
66 *	0	Success
67 *	1	Busy - one or more old or new modifiers are down
68 *	2	Failed - one or more new modifiers unacceptable
69 */
70int
71XSetModifierMapping(
72    register Display *dpy,
73    register XModifierKeymap *modifier_map)
74{
75    register xSetModifierMappingReq *req;
76    xSetModifierMappingReply rep;
77    int         mapSize = modifier_map->max_keypermod << 3;	/* 8 modifiers */
78
79    LockDisplay(dpy);
80    GetReqExtra(SetModifierMapping, mapSize, req);
81
82    req->numKeyPerModifier = modifier_map->max_keypermod;
83
84    memcpy((char *) NEXTPTR(req,xSetModifierMappingReq),
85	   (char *) modifier_map->modifiermap,
86	   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 = (XModifierKeymap *) Xmalloc((sizeof (XModifierKeymap)));
99    if (res) {
100	res->max_keypermod = keyspermodifier;
101	res->modifiermap = (keyspermodifier > 0 ?
102			    (KeyCode *) Xmalloc((unsigned) (8 * keyspermodifier))
103			    : (KeyCode *) NULL);
104	if (keyspermodifier && (res->modifiermap == NULL)) {
105	    Xfree((char *) 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((char *) map->modifiermap);
119	Xfree((char *) 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