XKBCompat.c revision 61b2299d
11ab64890Smrg/* $Xorg: XKBCompat.c,v 1.3 2000/08/17 19:45:00 cpqbld Exp $ */
21ab64890Smrg/************************************************************
31ab64890SmrgCopyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, and distribute this
61ab64890Smrgsoftware and its documentation for any purpose and without
71ab64890Smrgfee is hereby granted, provided that the above copyright
81ab64890Smrgnotice appear in all copies and that both that copyright
91ab64890Smrgnotice and this permission notice appear in supporting
1061b2299dSmrgdocumentation, and that the name of Silicon Graphics not be
1161b2299dSmrgused in advertising or publicity pertaining to distribution
121ab64890Smrgof the software without specific prior written permission.
1361b2299dSmrgSilicon Graphics makes no representation about the suitability
141ab64890Smrgof this software for any purpose. It is provided "as is"
151ab64890Smrgwithout any express or implied warranty.
161ab64890Smrg
1761b2299dSmrgSILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
1861b2299dSmrgSOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
191ab64890SmrgAND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
2061b2299dSmrgGRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
2161b2299dSmrgDAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
2261b2299dSmrgDATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
231ab64890SmrgOR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
241ab64890SmrgTHE USE OR PERFORMANCE OF THIS SOFTWARE.
251ab64890Smrg
261ab64890Smrg********************************************************/
271ab64890Smrg/* $XFree86$ */
281ab64890Smrg
291ab64890Smrg#ifdef HAVE_CONFIG_H
301ab64890Smrg#include <config.h>
311ab64890Smrg#endif
321ab64890Smrg#include <stdio.h>
331ab64890Smrg#define NEED_REPLIES
341ab64890Smrg#define NEED_EVENTS
351ab64890Smrg#define	NEED_MAP_READERS
361ab64890Smrg#include "Xlibint.h"
371ab64890Smrg#include <X11/extensions/XKBproto.h>
381ab64890Smrg#include "XKBlibint.h"
391ab64890Smrg
401ab64890SmrgStatus
411ab64890Smrg_XkbReadGetCompatMapReply(	Display *		dpy,
421ab64890Smrg				xkbGetCompatMapReply *	rep,
431ab64890Smrg				XkbDescPtr		xkb,
441ab64890Smrg				int	*		nread_rtrn)
451ab64890Smrg{
461ab64890Smrgregister int 		i;
471ab64890SmrgXkbReadBufferRec	buf;
481ab64890Smrg
4961b2299dSmrg    if (!_XkbInitReadBuffer(dpy,&buf,(int)rep->length*4))
501ab64890Smrg	return BadAlloc;
511ab64890Smrg
521ab64890Smrg    if (nread_rtrn)
531ab64890Smrg	*nread_rtrn= (int)rep->length*4;
541ab64890Smrg
551ab64890Smrg    i= rep->firstSI+rep->nSI;
561ab64890Smrg    if ((!xkb->compat)&&
571ab64890Smrg	(XkbAllocCompatMap(xkb,XkbAllCompatMask,i)!=Success))
581ab64890Smrg	return BadAlloc;
591ab64890Smrg
601ab64890Smrg    if (rep->nSI!=0) {
611ab64890Smrg	XkbSymInterpretRec *syms;
621ab64890Smrg	xkbSymInterpretWireDesc *wire;
631ab64890Smrg
641ab64890Smrg	wire= (xkbSymInterpretWireDesc *)_XkbGetReadBufferPtr(&buf,
651ab64890Smrg				   rep->nSI*SIZEOF(xkbSymInterpretWireDesc));
661ab64890Smrg	if (wire==NULL)
671ab64890Smrg	    goto BAILOUT;
681ab64890Smrg	syms= &xkb->compat->sym_interpret[rep->firstSI];
691ab64890Smrg
701ab64890Smrg	for (i=0;i<rep->nSI;i++,syms++,wire++) {
711ab64890Smrg	    syms->sym= wire->sym;
721ab64890Smrg	    syms->mods= wire->mods;
731ab64890Smrg	    syms->match= wire->match;
741ab64890Smrg	    syms->virtual_mod= wire->virtualMod;
751ab64890Smrg	    syms->flags= wire->flags;
761ab64890Smrg	    syms->act= *((XkbAnyAction *)&wire->act);
771ab64890Smrg	}
781ab64890Smrg	xkb->compat->num_si+= rep->nSI;
791ab64890Smrg    }
801ab64890Smrg
811ab64890Smrg    if (rep->groups&XkbAllGroupsMask) {
821ab64890Smrg	register unsigned	bit,nGroups;
831ab64890Smrg	xkbModsWireDesc * 	wire;
841ab64890Smrg	for (i=0,nGroups=0,bit=1;i<XkbNumKbdGroups;i++,bit<<=1) {
851ab64890Smrg	    if (rep->groups&bit)
861ab64890Smrg		nGroups++;
871ab64890Smrg	}
881ab64890Smrg	wire= (xkbModsWireDesc *)_XkbGetReadBufferPtr(&buf,
891ab64890Smrg				  nGroups*SIZEOF(xkbModsWireDesc));
901ab64890Smrg	if (wire==NULL)
911ab64890Smrg	    goto BAILOUT;
921ab64890Smrg	for (i=0,bit=1;i<XkbNumKbdGroups;i++,bit<<=1) {
931ab64890Smrg	    if ((rep->groups&bit)==0)
941ab64890Smrg		continue;
951ab64890Smrg	    xkb->compat->groups[i].mask= wire->mask;
961ab64890Smrg	    xkb->compat->groups[i].real_mods= wire->realMods;
971ab64890Smrg	    xkb->compat->groups[i].vmods= wire->virtualMods;
981ab64890Smrg	    wire++;
991ab64890Smrg	}
1001ab64890Smrg    }
1011ab64890Smrg    i= _XkbFreeReadBuffer(&buf);
10261b2299dSmrg    if (i)
1031ab64890Smrg	fprintf(stderr,"CompatMapReply! Bad length (%d extra bytes)\n",i);
1041ab64890Smrg    if (i || buf.error)
1051ab64890Smrg	return BadLength;
1061ab64890Smrg    return Success;
1071ab64890SmrgBAILOUT:
1081ab64890Smrg    _XkbFreeReadBuffer(&buf);
1091ab64890Smrg    return BadLength;
1101ab64890Smrg}
1111ab64890Smrg
1121ab64890SmrgStatus
1131ab64890SmrgXkbGetCompatMap(Display *dpy,unsigned which,XkbDescPtr xkb)
1141ab64890Smrg{
1151ab64890Smrg    register xkbGetCompatMapReq *req;
1161ab64890Smrg    xkbGetCompatMapReply	 rep;
1171ab64890Smrg    Status			status;
1181ab64890Smrg    XkbInfoPtr xkbi;
1191ab64890Smrg
1201ab64890Smrg    if ( (!dpy) || (!xkb) || (dpy->flags & XlibDisplayNoXkb) ||
1211ab64890Smrg	((xkb->dpy!=NULL)&&(xkb->dpy!=dpy)) ||
1221ab64890Smrg	(!dpy->xkb_info && (!XkbUseExtension(dpy,NULL,NULL))))
1231ab64890Smrg	return BadAccess;
1241ab64890Smrg    LockDisplay(dpy);
1251ab64890Smrg    xkbi = dpy->xkb_info;
1261ab64890Smrg    GetReq(kbGetCompatMap, req);
1271ab64890Smrg    req->reqType = xkbi->codes->major_opcode;
1281ab64890Smrg    req->xkbReqType = X_kbGetCompatMap;
1291ab64890Smrg    req->deviceSpec = xkb->device_spec;
1301ab64890Smrg    if (which&XkbSymInterpMask)
1311ab64890Smrg	 req->getAllSI= True;
1321ab64890Smrg    else req->getAllSI= False;
1331ab64890Smrg    req->firstSI= req->nSI= 0;
1341ab64890Smrg
1351ab64890Smrg    if (which&XkbGroupCompatMask)
1361ab64890Smrg	 req->groups= XkbAllGroupsMask;
1371ab64890Smrg    else req->groups=  0;
1381ab64890Smrg
1391ab64890Smrg    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
1401ab64890Smrg	UnlockDisplay(dpy);
1411ab64890Smrg	SyncHandle();
1421ab64890Smrg	return BadLength;
1431ab64890Smrg    }
1441ab64890Smrg    if (xkb->dpy==NULL)
1451ab64890Smrg	xkb->dpy= dpy;
1461ab64890Smrg    if (xkb->device_spec==XkbUseCoreKbd)
1471ab64890Smrg	xkb->device_spec= rep.deviceID;
1481ab64890Smrg
1491ab64890Smrg    status = _XkbReadGetCompatMapReply(dpy,&rep,xkb,NULL);
1501ab64890Smrg    UnlockDisplay(dpy);
1511ab64890Smrg    SyncHandle();
1521ab64890Smrg    return status;
1531ab64890Smrg}
1541ab64890Smrg
1551ab64890Smrgstatic Bool
1561ab64890Smrg_XkbWriteSetCompatMap(Display *dpy,xkbSetCompatMapReq *req,XkbDescPtr xkb)
1571ab64890Smrg{
1581ab64890SmrgCARD16			firstSI;
1591ab64890SmrgCARD16			nSI;
1601ab64890Smrgint			size;
1611ab64890Smrgregister int 		i,nGroups;
1621ab64890Smrgregister unsigned	bit;
1631ab64890Smrgunsigned		groups;
1641ab64890Smrgchar *			buf;
1651ab64890Smrg
1661ab64890Smrg    firstSI = req->firstSI;
1671ab64890Smrg    nSI = req->nSI;
1681ab64890Smrg    size= nSI*SIZEOF(xkbSymInterpretWireDesc);
1691ab64890Smrg    nGroups= 0;
1701ab64890Smrg    groups= req->groups;
1711ab64890Smrg    if (groups&XkbAllGroupsMask) {
1721ab64890Smrg	for (i=0,bit=1;i<XkbNumKbdGroups;i++,bit<<=1) {
1731ab64890Smrg	    if (groups&bit)
1741ab64890Smrg		nGroups++;
1751ab64890Smrg	}
1761ab64890Smrg	size+= SIZEOF(xkbModsWireDesc)*nGroups;
1771ab64890Smrg    }
1781ab64890Smrg    req->length+= size/4;
1791ab64890Smrg    BufAlloc(char *,buf,size);
1801ab64890Smrg    if (!buf)
1811ab64890Smrg	return False;
1821ab64890Smrg
1831ab64890Smrg    if (nSI) {
1841ab64890Smrg	XkbSymInterpretPtr sym= &xkb->compat->sym_interpret[firstSI];
1851ab64890Smrg	xkbSymInterpretWireDesc *wire= (xkbSymInterpretWireDesc *)buf;
1861ab64890Smrg	for (i=0;i<nSI;i++,wire++,sym++) {
1871ab64890Smrg	    wire->sym= (CARD32)sym->sym;
1881ab64890Smrg	    wire->mods= sym->mods;
1891ab64890Smrg	    wire->match= sym->match;
1901ab64890Smrg	    wire->flags= sym->flags;
1911ab64890Smrg	    wire->virtualMod= sym->virtual_mod;
1921ab64890Smrg	    memcpy(&wire->act,&sym->act,sz_xkbActionWireDesc);
1931ab64890Smrg	}
1941ab64890Smrg	buf+= nSI*SIZEOF(xkbSymInterpretWireDesc);
1951ab64890Smrg    }
1961ab64890Smrg    if (groups&XkbAllGroupsMask) {
1971ab64890Smrg	xkbModsWireDesc *	out;
1981ab64890Smrg
1991ab64890Smrg	out= (xkbModsWireDesc *)buf;
2001ab64890Smrg	for (i=0,bit=1;i<XkbNumKbdGroups;i++,bit<<=1) {
2011ab64890Smrg	    if ((groups&bit)!=0) {
2021ab64890Smrg		out->mask= xkb->compat->groups[i].mask;
2031ab64890Smrg		out->realMods= xkb->compat->groups[i].real_mods;
2041ab64890Smrg		out->virtualMods=  xkb->compat->groups[i].vmods;
2051ab64890Smrg		out++;
2061ab64890Smrg	    }
2071ab64890Smrg	}
2081ab64890Smrg	buf+= nGroups*SIZEOF(xkbModsWireDesc);
2091ab64890Smrg    }
2101ab64890Smrg    return True;
2111ab64890Smrg}
2121ab64890Smrg
2131ab64890SmrgBool
2141ab64890SmrgXkbSetCompatMap(Display *dpy,unsigned which,XkbDescPtr xkb,Bool updateActions)
2151ab64890Smrg{
2161ab64890Smrg    register xkbSetCompatMapReq *req;
2171ab64890Smrg    Status		     ok;
2181ab64890Smrg    XkbInfoPtr xkbi;
2191ab64890Smrg
2201ab64890Smrg    if ((dpy->flags & XlibDisplayNoXkb) || (dpy!=xkb->dpy) ||
2211ab64890Smrg	(!dpy->xkb_info && !XkbUseExtension(dpy,NULL,NULL)))
2221ab64890Smrg	return False;
2231ab64890Smrg    if ((!xkb->compat) ||
2241ab64890Smrg	((which&XkbSymInterpMask)&&(!xkb->compat->sym_interpret)))
2251ab64890Smrg	return False;
2261ab64890Smrg    LockDisplay(dpy);
2271ab64890Smrg    xkbi = dpy->xkb_info;
2281ab64890Smrg    GetReq(kbSetCompatMap, req);
2291ab64890Smrg    req->reqType = xkbi->codes->major_opcode;
2301ab64890Smrg    req->xkbReqType = X_kbSetCompatMap;
2311ab64890Smrg    req->deviceSpec = xkb->device_spec;
2321ab64890Smrg    req->recomputeActions = updateActions;
2331ab64890Smrg    if (which&XkbSymInterpMask) {
2341ab64890Smrg	req->truncateSI = True;
2351ab64890Smrg	req->firstSI= 0;
2361ab64890Smrg	req->nSI= xkb->compat->num_si;
2371ab64890Smrg    }
2381ab64890Smrg    else {
2391ab64890Smrg	req->truncateSI = False;
2401ab64890Smrg	req->firstSI= 0;
2411ab64890Smrg	req->nSI= 0;
2421ab64890Smrg    }
2431ab64890Smrg    if (which&XkbGroupCompatMask)
2441ab64890Smrg	 req->groups= XkbAllGroupsMask;
2451ab64890Smrg    else req->groups=  0;
2461ab64890Smrg    ok= _XkbWriteSetCompatMap(dpy,req,xkb);
2471ab64890Smrg    UnlockDisplay(dpy);
2481ab64890Smrg    SyncHandle();
2491ab64890Smrg    return ok;
2501ab64890Smrg}
2511ab64890Smrg
252