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