XGetFCtl.c revision c43cc173
1/* $Xorg: XGetFCtl.c,v 1.4 2001/02/09 02:03:50 xorgcvs Exp $ */
2
3/************************************************************
4
5Copyright 1989, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
28
29			All Rights Reserved
30
31Permission to use, copy, modify, and distribute this software and its
32documentation for any purpose and without fee is hereby granted,
33provided that the above copyright notice appear in all copies and that
34both that copyright notice and this permission notice appear in
35supporting documentation, and that the name of Hewlett-Packard not be
36used in advertising or publicity pertaining to distribution of the
37software without specific, written prior permission.
38
39HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45SOFTWARE.
46
47********************************************************/
48/* $XFree86: xc/lib/Xi/XGetFCtl.c,v 3.3 2001/12/14 19:55:14 dawes Exp $ */
49
50/***********************************************************************
51 *
52 * XGetFeedbackControl - get the feedback attributes of an extension device.
53 *
54 */
55
56#include <X11/extensions/XI.h>
57#include <X11/extensions/XIproto.h>
58#include <X11/Xlibint.h>
59#include <X11/Xlib.h>
60#include <X11/extensions/XInput.h>
61#include <X11/extensions/extutil.h>
62#include "XIint.h"
63
64XFeedbackState *
65XGetFeedbackControl(dpy, dev, num_feedbacks)
66    register Display *dpy;
67    XDevice *dev;
68    int *num_feedbacks;
69{
70    int size = 0;
71    int nbytes, i;
72    XFeedbackState *Feedback = NULL;
73    XFeedbackState *Sav = NULL;
74    xFeedbackState *f = NULL;
75    xFeedbackState *sav = NULL;
76    xGetFeedbackControlReq *req;
77    xGetFeedbackControlReply rep;
78    XExtDisplayInfo *info = XInput_find_display(dpy);
79
80    LockDisplay(dpy);
81    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
82	return ((XFeedbackState *) NoSuchExtension);
83
84    GetReq(GetFeedbackControl, req);
85    req->reqType = info->codes->major_opcode;
86    req->ReqType = X_GetFeedbackControl;
87    req->deviceid = dev->device_id;
88
89    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
90	UnlockDisplay(dpy);
91	SyncHandle();
92	return (XFeedbackState *) NULL;
93    }
94    if (rep.length > 0) {
95	*num_feedbacks = rep.num_feedbacks;
96	nbytes = (long)rep.length << 2;
97	f = (xFeedbackState *) Xmalloc((unsigned)nbytes);
98	if (!f) {
99	    _XEatData(dpy, (unsigned long)nbytes);
100	    UnlockDisplay(dpy);
101	    SyncHandle();
102	    return (XFeedbackState *) NULL;
103	}
104	sav = f;
105	_XRead(dpy, (char *)f, nbytes);
106
107	for (i = 0; i < *num_feedbacks; i++) {
108	    switch (f->class) {
109	    case KbdFeedbackClass:
110		size += sizeof(XKbdFeedbackState);
111		break;
112	    case PtrFeedbackClass:
113		size += sizeof(XPtrFeedbackState);
114		break;
115	    case IntegerFeedbackClass:
116		size += sizeof(XIntegerFeedbackState);
117		break;
118	    case StringFeedbackClass:
119	    {
120		xStringFeedbackState *strf = (xStringFeedbackState *) f;
121
122		size += sizeof(XStringFeedbackState) +
123		    (strf->num_syms_supported * sizeof(KeySym));
124	    }
125		break;
126	    case LedFeedbackClass:
127		size += sizeof(XLedFeedbackState);
128		break;
129	    case BellFeedbackClass:
130		size += sizeof(XBellFeedbackState);
131		break;
132	    default:
133		size += f->length;
134		break;
135	    }
136	    f = (xFeedbackState *) ((char *)f + f->length);
137	}
138
139	Feedback = (XFeedbackState *) Xmalloc((unsigned)size);
140	if (!Feedback) {
141	    UnlockDisplay(dpy);
142	    SyncHandle();
143	    return (XFeedbackState *) NULL;
144	}
145	Sav = Feedback;
146
147	f = sav;
148	for (i = 0; i < *num_feedbacks; i++) {
149	    switch (f->class) {
150	    case KbdFeedbackClass:
151	    {
152		xKbdFeedbackState *k;
153		XKbdFeedbackState *K;
154
155		k = (xKbdFeedbackState *) f;
156		K = (XKbdFeedbackState *) Feedback;
157
158		K->class = k->class;
159		K->length = sizeof(XKbdFeedbackState);
160		K->id = k->id;
161		K->click = k->click;
162		K->percent = k->percent;
163		K->pitch = k->pitch;
164		K->duration = k->duration;
165		K->led_mask = k->led_mask;
166		K->global_auto_repeat = k->global_auto_repeat;
167		memcpy((char *)&K->auto_repeats[0],
168		       (char *)&k->auto_repeats[0], 32);
169		break;
170	    }
171	    case PtrFeedbackClass:
172	    {
173		xPtrFeedbackState *p;
174		XPtrFeedbackState *P;
175
176		p = (xPtrFeedbackState *) f;
177		P = (XPtrFeedbackState *) Feedback;
178
179		P->class = p->class;
180		P->length = sizeof(XPtrFeedbackState);
181		P->id = p->id;
182		P->accelNum = p->accelNum;
183		P->accelDenom = p->accelDenom;
184		P->threshold = p->threshold;
185		break;
186	    }
187	    case IntegerFeedbackClass:
188	    {
189		xIntegerFeedbackState *i;
190		XIntegerFeedbackState *I;
191
192		i = (xIntegerFeedbackState *) f;
193		I = (XIntegerFeedbackState *) Feedback;
194
195		I->class = i->class;
196		I->length = sizeof(XIntegerFeedbackState);
197		I->id = i->id;
198		I->resolution = i->resolution;
199		I->minVal = i->min_value;
200		I->maxVal = i->max_value;
201		break;
202	    }
203	    case StringFeedbackClass:
204	    {
205		xStringFeedbackState *s;
206		XStringFeedbackState *S;
207
208		s = (xStringFeedbackState *) f;
209		S = (XStringFeedbackState *) Feedback;
210
211		S->class = s->class;
212		S->length = sizeof(XStringFeedbackState) +
213		    (s->num_syms_supported * sizeof(KeySym));
214		S->id = s->id;
215		S->max_symbols = s->max_symbols;
216		S->num_syms_supported = s->num_syms_supported;
217		S->syms_supported = (KeySym *) (S + 1);
218		memcpy((char *)S->syms_supported, (char *)(s + 1),
219		       (S->num_syms_supported * sizeof(KeySym)));
220		break;
221	    }
222	    case LedFeedbackClass:
223	    {
224		xLedFeedbackState *l;
225		XLedFeedbackState *L;
226
227		l = (xLedFeedbackState *) f;
228		L = (XLedFeedbackState *) Feedback;
229
230		L->class = l->class;
231		L->length = sizeof(XLedFeedbackState);
232		L->id = l->id;
233		L->led_values = l->led_values;
234		L->led_mask = l->led_mask;
235		break;
236	    }
237	    case BellFeedbackClass:
238	    {
239		xBellFeedbackState *b;
240		XBellFeedbackState *B;
241
242		b = (xBellFeedbackState *) f;
243		B = (XBellFeedbackState *) Feedback;
244
245		B->class = b->class;
246		B->length = sizeof(XBellFeedbackState);
247		B->id = b->id;
248		B->percent = b->percent;
249		B->pitch = b->pitch;
250		B->duration = b->duration;
251		break;
252	    }
253	    default:
254		break;
255	    }
256	    f = (xFeedbackState *) ((char *)f + f->length);
257	    Feedback = (XFeedbackState *) ((char *)Feedback + Feedback->length);
258	}
259	XFree((char *)sav);
260    }
261
262    UnlockDisplay(dpy);
263    SyncHandle();
264    return (Sav);
265}
266
267void
268XFreeFeedbackList(list)
269    XFeedbackState *list;
270{
271    XFree((char *)list);
272}
273