XKBleds.c revision e9fcaa8a
1/************************************************************
2Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3
4Permission to use, copy, modify, and distribute this
5software and its documentation for any purpose and without
6fee is hereby granted, provided that the above copyright
7notice appear in all copies and that both that copyright
8notice and this permission notice appear in supporting
9documentation, and that the name of Silicon Graphics not be
10used in advertising or publicity pertaining to distribution
11of the software without specific prior written permission.
12Silicon Graphics makes no representation about the suitability
13of this software for any purpose. It is provided "as is"
14without any express or implied warranty.
15
16SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25********************************************************/
26
27#define NEED_MAP_READERS
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "Xlibint.h"
32#include <X11/extensions/XKBproto.h>
33#include "XKBlibint.h"
34
35Status
36XkbGetIndicatorState(Display *dpy,unsigned deviceSpec,unsigned *pStateRtrn)
37{
38    register xkbGetIndicatorStateReq *req;
39    xkbGetIndicatorStateReply	rep;
40    XkbInfoPtr xkbi;
41    Bool ok;
42
43    if ((dpy->flags & XlibDisplayNoXkb) ||
44	(!dpy->xkb_info && !XkbUseExtension(dpy,NULL,NULL)))
45	return BadAccess;
46    LockDisplay(dpy);
47    xkbi = dpy->xkb_info;
48    GetReq(kbGetIndicatorState, req);
49    req->reqType = xkbi->codes->major_opcode;
50    req->xkbReqType = X_kbGetIndicatorState;
51    req->deviceSpec = deviceSpec;
52    ok=_XReply(dpy, (xReply *)&rep, 0, xFalse);
53    if (ok && (pStateRtrn!=NULL))
54	*pStateRtrn= rep.state;
55    UnlockDisplay(dpy);
56    SyncHandle();
57    return (ok?Success:BadImplementation);
58}
59
60Status
61_XkbReadGetIndicatorMapReply(	Display *			dpy,
62				xkbGetIndicatorMapReply *	rep,
63				XkbDescPtr			xkb,
64				int	*			nread_rtrn)
65{
66XkbIndicatorPtr		leds;
67XkbReadBufferRec	buf;
68
69    if ((!xkb->indicators)&&(XkbAllocIndicatorMaps(xkb)!=Success))
70	return BadAlloc;
71    leds= xkb->indicators;
72
73    leds->phys_indicators = rep->realIndicators;
74    if (rep->length>0) {
75	register int left;
76	if (!_XkbInitReadBuffer(dpy,&buf,(int)rep->length*4))
77	    return BadAlloc;
78	if (nread_rtrn)
79	    *nread_rtrn= (int)rep->length*4;
80	if (rep->which) {
81	    register int i,bit;
82	    left= (int)rep->which;
83	    for (i=0,bit=1;(i<XkbNumIndicators)&&(left);i++,bit<<=1) {
84		if (left&bit) {
85		    xkbIndicatorMapWireDesc *wire;
86		    wire= (xkbIndicatorMapWireDesc *)
87			  _XkbGetReadBufferPtr(&buf,
88					SIZEOF(xkbIndicatorMapWireDesc));
89		    if (wire==NULL) {
90			_XkbFreeReadBuffer(&buf);
91			return BadAlloc;
92		    }
93		    leds->maps[i].flags= wire->flags;
94		    leds->maps[i].which_groups= wire->whichGroups;
95		    leds->maps[i].groups= wire->groups;
96		    leds->maps[i].which_mods= wire->whichMods;
97		    leds->maps[i].mods.mask= wire->mods;
98		    leds->maps[i].mods.real_mods= wire->realMods;
99		    leds->maps[i].mods.vmods= wire->virtualMods;
100		    leds->maps[i].ctrls= wire->ctrls;
101		    left&= ~bit;
102		}
103	    }
104	}
105	left= _XkbFreeReadBuffer(&buf);
106    }
107    return Success;
108}
109
110Bool
111XkbGetIndicatorMap(Display *dpy,unsigned long which,XkbDescPtr xkb)
112{
113    register xkbGetIndicatorMapReq *	req;
114    xkbGetIndicatorMapReply 		rep;
115    XkbInfoPtr 				xkbi;
116    Status				status;
117
118    if ((dpy->flags & XlibDisplayNoXkb) ||
119	(!dpy->xkb_info && !XkbUseExtension(dpy,NULL,NULL)))
120	return BadAccess;
121    if ((!which)||(!xkb))
122	return BadValue;
123
124    LockDisplay(dpy);
125    xkbi = dpy->xkb_info;
126    if (!xkb->indicators) {
127	xkb->indicators = _XkbTypedCalloc(1,XkbIndicatorRec);
128	if (!xkb->indicators) {
129	    UnlockDisplay(dpy);
130	    SyncHandle();
131	    return BadAlloc;
132	}
133    }
134    GetReq(kbGetIndicatorMap, req);
135    req->reqType = xkbi->codes->major_opcode;
136    req->xkbReqType = X_kbGetIndicatorMap;
137    req->deviceSpec = xkb->device_spec;
138    req->which = (CARD32)which;
139    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
140	UnlockDisplay(dpy);
141	SyncHandle();
142	return BadValue;
143    }
144    status= _XkbReadGetIndicatorMapReply(dpy,&rep,xkb,NULL);
145    UnlockDisplay(dpy);
146    SyncHandle();
147    return status;
148}
149
150Bool
151XkbSetIndicatorMap(Display *dpy,unsigned long which,XkbDescPtr xkb)
152{
153    register xkbSetIndicatorMapReq	*req;
154    register int i,bit;
155    int nMaps;
156    xkbIndicatorMapWireDesc *wire;
157    XkbInfoPtr xkbi;
158
159    if ((dpy->flags & XlibDisplayNoXkb) ||
160	(!dpy->xkb_info && !XkbUseExtension(dpy,NULL,NULL)))
161	return False;
162    if ((!xkb)||(!which)||(!xkb->indicators))
163	return False;
164    LockDisplay(dpy);
165    xkbi = dpy->xkb_info;
166    GetReq(kbSetIndicatorMap, req);
167    req->reqType = xkbi->codes->major_opcode;
168    req->xkbReqType = X_kbSetIndicatorMap;
169    req->deviceSpec = xkb->device_spec;
170    req->which = (CARD32)which;
171    for (i=nMaps=0,bit=1;i<32;i++,bit<<=1) {
172	if (which&bit)
173	    nMaps++;
174    }
175    req->length+= (nMaps*sizeof(XkbIndicatorMapRec))/4;
176    BufAlloc(xkbIndicatorMapWireDesc *,wire,
177				       (nMaps*SIZEOF(xkbIndicatorMapWireDesc)));
178    for (i=0,bit=1;i<32;i++,bit<<=1) {
179	if (which&bit) {
180	    wire->flags= xkb->indicators->maps[i].flags;
181	    wire->whichGroups= xkb->indicators->maps[i].which_groups;
182	    wire->groups= xkb->indicators->maps[i].groups;
183	    wire->whichMods= xkb->indicators->maps[i].which_mods;
184	    wire->mods= xkb->indicators->maps[i].mods.real_mods;
185	    wire->virtualMods= xkb->indicators->maps[i].mods.vmods;
186	    wire->ctrls= xkb->indicators->maps[i].ctrls;
187	    wire++;
188	}
189    }
190    UnlockDisplay(dpy);
191    SyncHandle();
192    return True;
193}
194
195Bool
196XkbGetNamedDeviceIndicator(	Display *		dpy,
197				unsigned		device,
198				unsigned		class,
199				unsigned		id,
200				Atom			name,
201				int	*		pNdxRtrn,
202    				Bool *			pStateRtrn,
203				XkbIndicatorMapPtr	pMapRtrn,
204				Bool *			pRealRtrn)
205{
206    register xkbGetNamedIndicatorReq *req;
207    xkbGetNamedIndicatorReply	rep;
208    XkbInfoPtr xkbi;
209
210    if ((dpy->flags & XlibDisplayNoXkb) || (name==None) ||
211	(!dpy->xkb_info && !XkbUseExtension(dpy,NULL,NULL)))
212	return False;
213    LockDisplay(dpy);
214    xkbi = dpy->xkb_info;
215    GetReq(kbGetNamedIndicator, req);
216    req->reqType = xkbi->codes->major_opcode;
217    req->xkbReqType = X_kbGetNamedIndicator;
218    req->deviceSpec = device;
219    req->ledClass = class;
220    req->ledID = id;
221    req->indicator = (CARD32)name;
222    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
223	UnlockDisplay(dpy);
224	SyncHandle();
225	return False;
226    }
227    UnlockDisplay(dpy);
228    SyncHandle();
229    if ((!rep.found)||(!rep.supported))
230	return False;
231    if (pNdxRtrn!=NULL)
232	*pNdxRtrn= rep.ndx;
233    if (pStateRtrn!=NULL)
234	*pStateRtrn= rep.on;
235    if (pMapRtrn!=NULL) {
236	pMapRtrn->flags= rep.flags;
237	pMapRtrn->which_groups= rep.whichGroups;
238	pMapRtrn->groups= rep.groups;
239	pMapRtrn->which_mods= rep.whichMods;
240	pMapRtrn->mods.mask= rep.mods;
241	pMapRtrn->mods.real_mods= rep.realMods;
242	pMapRtrn->mods.vmods= rep.virtualMods;
243	pMapRtrn->ctrls= rep.ctrls;
244    }
245    if (pRealRtrn!=NULL)
246	*pRealRtrn= rep.realIndicator;
247    return True;
248}
249
250Bool
251XkbGetNamedIndicator(	Display *		dpy,
252			Atom			name,
253			int	*		pNdxRtrn,
254    			Bool *			pStateRtrn,
255			XkbIndicatorMapPtr	pMapRtrn,
256			Bool *			pRealRtrn)
257{
258    return XkbGetNamedDeviceIndicator(dpy,XkbUseCoreKbd,
259					  XkbDfltXIClass,XkbDfltXIId,
260					  name,pNdxRtrn,pStateRtrn,
261					  pMapRtrn,pRealRtrn);
262}
263
264Bool
265XkbSetNamedDeviceIndicator(	Display *		dpy,
266				unsigned		device,
267				unsigned		class,
268				unsigned		id,
269				Atom			name,
270				Bool			changeState,
271				Bool			state,
272				Bool			createNewMap,
273				XkbIndicatorMapPtr	pMap)
274{
275    register xkbSetNamedIndicatorReq	*req;
276    XkbInfoPtr xkbi;
277
278    if ((dpy->flags & XlibDisplayNoXkb) || (name==None) ||
279 	(!dpy->xkb_info && !XkbUseExtension(dpy,NULL,NULL)))
280	return False;
281    LockDisplay(dpy);
282    xkbi = dpy->xkb_info;
283    GetReq(kbSetNamedIndicator, req);
284    req->reqType = xkbi->codes->major_opcode;
285    req->xkbReqType = X_kbSetNamedIndicator;
286    req->deviceSpec = device;
287    req->ledClass = class;
288    req->ledID = id;
289    req->indicator= (CARD32)name;
290    req->setState= changeState;
291    if (req->setState)
292	 req->on= state;
293    else req->on= False;
294    if (pMap!=NULL) {
295	req->setMap= True;
296	req->createMap= createNewMap;
297	req->flags= pMap->flags;
298	req->whichGroups= pMap->which_groups;
299	req->groups= pMap->groups;
300	req->whichMods= pMap->which_mods;
301	req->realMods= pMap->mods.real_mods;
302	req->virtualMods= pMap->mods.vmods;
303	req->ctrls= pMap->ctrls;
304    }
305    else {
306	req->setMap= 		False;
307	req->createMap= 	False;
308	req->flags= 		0;
309	req->whichGroups= 	0;
310	req->groups= 		0;
311	req->whichMods= 	0;
312	req->realMods= 		0;
313	req->virtualMods= 	0;
314	req->ctrls=		0;
315    }
316    UnlockDisplay(dpy);
317    SyncHandle();
318    return True;
319}
320
321Bool
322XkbSetNamedIndicator(	Display *		dpy,
323			Atom			name,
324			Bool			changeState,
325			Bool			state,
326			Bool			createNewMap,
327			XkbIndicatorMapPtr	pMap)
328{
329    return XkbSetNamedDeviceIndicator(dpy,XkbUseCoreKbd,
330					  XkbDfltXIClass,XkbDfltXIId,
331					  name,changeState,state,
332					  createNewMap,pMap);
333}
334