XGetFCtl.c revision f1c62215
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    char *end = NULL;
77    xGetFeedbackControlReq *req;
78    xGetFeedbackControlReply rep;
79    XExtDisplayInfo *info = XInput_find_display(dpy);
80
81    LockDisplay(dpy);
82    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
83        return NULL;
84
85    GetReq(GetFeedbackControl, req);
86    req->reqType = info->codes->major_opcode;
87    req->ReqType = X_GetFeedbackControl;
88    req->deviceid = dev->device_id;
89
90    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse))
91	goto out;
92
93    if (rep.length > 0) {
94	unsigned long nbytes;
95	size_t size = 0;
96	int i;
97
98	*num_feedbacks = rep.num_feedbacks;
99
100	if (rep.length < (INT_MAX >> 2)) {
101	    nbytes = rep.length << 2;
102	    f = Xmalloc(nbytes);
103	}
104	if (!f) {
105	    _XEatDataWords(dpy, rep.length);
106	    goto out;
107	}
108	sav = f;
109	end = (char *)f + nbytes;
110	_XRead(dpy, (char *)f, nbytes);
111
112	for (i = 0; i < *num_feedbacks; i++) {
113	    if ((char *)f + sizeof(*f) > end ||
114	        f->length == 0 || f->length > nbytes)
115		goto out;
116	    nbytes -= f->length;
117
118	    switch (f->class) {
119	    case KbdFeedbackClass:
120		size += sizeof(XKbdFeedbackState);
121		break;
122	    case PtrFeedbackClass:
123		size += sizeof(XPtrFeedbackState);
124		break;
125	    case IntegerFeedbackClass:
126		size += sizeof(XIntegerFeedbackState);
127		break;
128	    case StringFeedbackClass:
129	    {
130		xStringFeedbackState *strf = (xStringFeedbackState *) f;
131		if ((char *)f + sizeof(*strf) > end)
132		    goto out;
133		size += sizeof(XStringFeedbackState) +
134		    (strf->num_syms_supported * sizeof(KeySym));
135	    }
136		break;
137	    case LedFeedbackClass:
138		size += sizeof(XLedFeedbackState);
139		break;
140	    case BellFeedbackClass:
141		size += sizeof(XBellFeedbackState);
142		break;
143	    default:
144		size += f->length;
145		break;
146	    }
147	    if (size > INT_MAX)
148		goto out;
149	    f = (xFeedbackState *) ((char *)f + f->length);
150	}
151
152	Feedback = Xmalloc(size);
153	if (!Feedback)
154	    goto out;
155
156	Sav = Feedback;
157
158	f = sav;
159	for (i = 0; i < *num_feedbacks; i++) {
160	    switch (f->class) {
161	    case KbdFeedbackClass:
162	    {
163		xKbdFeedbackState *k;
164		XKbdFeedbackState *K;
165
166		k = (xKbdFeedbackState *) f;
167		K = (XKbdFeedbackState *) Feedback;
168
169		K->class = k->class;
170		K->length = sizeof(XKbdFeedbackState);
171		K->id = k->id;
172		K->click = k->click;
173		K->percent = k->percent;
174		K->pitch = k->pitch;
175		K->duration = k->duration;
176		K->led_mask = k->led_mask;
177		K->global_auto_repeat = k->global_auto_repeat;
178		memcpy((char *)&K->auto_repeats[0],
179		       (char *)&k->auto_repeats[0], 32);
180		break;
181	    }
182	    case PtrFeedbackClass:
183	    {
184		xPtrFeedbackState *p;
185		XPtrFeedbackState *P;
186
187		p = (xPtrFeedbackState *) f;
188		P = (XPtrFeedbackState *) Feedback;
189
190		P->class = p->class;
191		P->length = sizeof(XPtrFeedbackState);
192		P->id = p->id;
193		P->accelNum = p->accelNum;
194		P->accelDenom = p->accelDenom;
195		P->threshold = p->threshold;
196		break;
197	    }
198	    case IntegerFeedbackClass:
199	    {
200		xIntegerFeedbackState *ifs;
201		XIntegerFeedbackState *I;
202
203		ifs = (xIntegerFeedbackState *) f;
204		I = (XIntegerFeedbackState *) Feedback;
205
206		I->class = ifs->class;
207		I->length = sizeof(XIntegerFeedbackState);
208		I->id = ifs->id;
209		I->resolution = ifs->resolution;
210		I->minVal = ifs->min_value;
211		I->maxVal = ifs->max_value;
212		break;
213	    }
214	    case StringFeedbackClass:
215	    {
216		xStringFeedbackState *s;
217		XStringFeedbackState *S;
218
219		s = (xStringFeedbackState *) f;
220		S = (XStringFeedbackState *) Feedback;
221
222		S->class = s->class;
223		S->length = sizeof(XStringFeedbackState) +
224		    (s->num_syms_supported * sizeof(KeySym));
225		S->id = s->id;
226		S->max_symbols = s->max_symbols;
227		S->num_syms_supported = s->num_syms_supported;
228		S->syms_supported = (KeySym *) (S + 1);
229		memcpy((char *)S->syms_supported, (char *)(s + 1),
230		       (S->num_syms_supported * sizeof(KeySym)));
231		break;
232	    }
233	    case LedFeedbackClass:
234	    {
235		xLedFeedbackState *l;
236		XLedFeedbackState *L;
237
238		l = (xLedFeedbackState *) f;
239		L = (XLedFeedbackState *) Feedback;
240
241		L->class = l->class;
242		L->length = sizeof(XLedFeedbackState);
243		L->id = l->id;
244		L->led_values = l->led_values;
245		L->led_mask = l->led_mask;
246		break;
247	    }
248	    case BellFeedbackClass:
249	    {
250		xBellFeedbackState *b;
251		XBellFeedbackState *B;
252
253		b = (xBellFeedbackState *) f;
254		B = (XBellFeedbackState *) Feedback;
255
256		B->class = b->class;
257		B->length = sizeof(XBellFeedbackState);
258		B->id = b->id;
259		B->percent = b->percent;
260		B->pitch = b->pitch;
261		B->duration = b->duration;
262		break;
263	    }
264	    default:
265		break;
266	    }
267	    f = (xFeedbackState *) ((char *)f + f->length);
268	    Feedback = (XFeedbackState *) ((char *)Feedback + Feedback->length);
269	}
270    }
271out:
272    XFree((char *)sav);
273
274    UnlockDisplay(dpy);
275    SyncHandle();
276    return (Sav);
277}
278
279void
280XFreeFeedbackList(XFeedbackState *list)
281{
282    XFree((char *)list);
283}
284