1c43cc173Smrg/************************************************************
2c43cc173Smrg
3c43cc173SmrgCopyright 1989, 1998  The Open Group
4c43cc173Smrg
5c43cc173SmrgPermission to use, copy, modify, distribute, and sell this software and its
6c43cc173Smrgdocumentation for any purpose is hereby granted without fee, provided that
7c43cc173Smrgthe above copyright notice appear in all copies and that both that
8c43cc173Smrgcopyright notice and this permission notice appear in supporting
9c43cc173Smrgdocumentation.
10c43cc173Smrg
11c43cc173SmrgThe above copyright notice and this permission notice shall be included in
12c43cc173Smrgall copies or substantial portions of the Software.
13c43cc173Smrg
14c43cc173SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15c43cc173SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16c43cc173SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17c43cc173SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18c43cc173SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19c43cc173SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20c43cc173Smrg
21c43cc173SmrgExcept as contained in this notice, the name of The Open Group shall not be
22c43cc173Smrgused in advertising or otherwise to promote the sale, use or other dealings
23c43cc173Smrgin this Software without prior written authorization from The Open Group.
24c43cc173Smrg
25c43cc173SmrgCopyright 1989 by Hewlett-Packard Company, Palo Alto, California.
26c43cc173Smrg
27c43cc173Smrg			All Rights Reserved
28c43cc173Smrg
29c43cc173SmrgPermission to use, copy, modify, and distribute this software and its
30c43cc173Smrgdocumentation for any purpose and without fee is hereby granted,
31c43cc173Smrgprovided that the above copyright notice appear in all copies and that
32c43cc173Smrgboth that copyright notice and this permission notice appear in
33c43cc173Smrgsupporting documentation, and that the name of Hewlett-Packard not be
34c43cc173Smrgused in advertising or publicity pertaining to distribution of the
35c43cc173Smrgsoftware without specific, written prior permission.
36c43cc173Smrg
37c43cc173SmrgHEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38c43cc173SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39c43cc173SmrgHEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40c43cc173SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41c43cc173SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42c43cc173SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43c43cc173SmrgSOFTWARE.
44c43cc173Smrg
45c43cc173Smrg********************************************************/
46c43cc173Smrg
47c43cc173Smrg/***********************************************************************
48c43cc173Smrg *
49c43cc173Smrg * XGetFeedbackControl - get the feedback attributes of an extension device.
50c43cc173Smrg *
51c43cc173Smrg */
52c43cc173Smrg
53f1ee322dSmrg#ifdef HAVE_CONFIG_H
54f1ee322dSmrg#include <config.h>
55f1ee322dSmrg#endif
56f1ee322dSmrg
57c43cc173Smrg#include <X11/extensions/XI.h>
58c43cc173Smrg#include <X11/extensions/XIproto.h>
59c43cc173Smrg#include <X11/Xlibint.h>
60c43cc173Smrg#include <X11/Xlib.h>
61c43cc173Smrg#include <X11/extensions/XInput.h>
62c43cc173Smrg#include <X11/extensions/extutil.h>
63c43cc173Smrg#include "XIint.h"
64190694daSmrg#include <limits.h>
65c43cc173Smrg
66c43cc173SmrgXFeedbackState *
67c27c18e8SmrgXGetFeedbackControl(
68c27c18e8Smrg    register Display	*dpy,
69c27c18e8Smrg    XDevice		*dev,
705bc19eebSmrg    int			*num_feedbacks_out)
71c43cc173Smrg{
72c43cc173Smrg    XFeedbackState *Feedback = NULL;
73c43cc173Smrg    XFeedbackState *Sav = NULL;
74c43cc173Smrg    xFeedbackState *f = NULL;
75c43cc173Smrg    xFeedbackState *sav = NULL;
76f1c62215Smrg    char *end = NULL;
77c43cc173Smrg    xGetFeedbackControlReq *req;
78c43cc173Smrg    xGetFeedbackControlReply rep;
79c43cc173Smrg    XExtDisplayInfo *info = XInput_find_display(dpy);
805bc19eebSmrg    int num_feedbacks = 0;
815bc19eebSmrg    int known_feedbacks = 0;
82c43cc173Smrg
83c43cc173Smrg    LockDisplay(dpy);
84c43cc173Smrg    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
8564276682Smrg        return NULL;
86c43cc173Smrg
87c43cc173Smrg    GetReq(GetFeedbackControl, req);
88c43cc173Smrg    req->reqType = info->codes->major_opcode;
89c43cc173Smrg    req->ReqType = X_GetFeedbackControl;
90c43cc173Smrg    req->deviceid = dev->device_id;
91c43cc173Smrg
9237eb1ca1Smrg    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse))
9337eb1ca1Smrg	goto out;
9437eb1ca1Smrg
95c43cc173Smrg    if (rep.length > 0) {
96190694daSmrg	unsigned long nbytes;
97190694daSmrg	size_t size = 0;
98190694daSmrg	int i;
99190694daSmrg
1005bc19eebSmrg	num_feedbacks = rep.num_feedbacks;
101190694daSmrg
102190694daSmrg	if (rep.length < (INT_MAX >> 2)) {
103190694daSmrg	    nbytes = rep.length << 2;
104190694daSmrg	    f = Xmalloc(nbytes);
105190694daSmrg	}
106c43cc173Smrg	if (!f) {
10710baa824Smrg	    _XEatDataWords(dpy, rep.length);
10837eb1ca1Smrg	    goto out;
109c43cc173Smrg	}
110c43cc173Smrg	sav = f;
111f1c62215Smrg	end = (char *)f + nbytes;
112c43cc173Smrg	_XRead(dpy, (char *)f, nbytes);
113c43cc173Smrg
1145bc19eebSmrg	for (i = 0; i < num_feedbacks; i++) {
115f1c62215Smrg	    if ((char *)f + sizeof(*f) > end ||
116f1c62215Smrg	        f->length == 0 || f->length > nbytes)
117190694daSmrg		goto out;
118190694daSmrg	    nbytes -= f->length;
119190694daSmrg
120c43cc173Smrg	    switch (f->class) {
121c43cc173Smrg	    case KbdFeedbackClass:
122c43cc173Smrg		size += sizeof(XKbdFeedbackState);
123c43cc173Smrg		break;
124c43cc173Smrg	    case PtrFeedbackClass:
125c43cc173Smrg		size += sizeof(XPtrFeedbackState);
126c43cc173Smrg		break;
127c43cc173Smrg	    case IntegerFeedbackClass:
128c43cc173Smrg		size += sizeof(XIntegerFeedbackState);
129c43cc173Smrg		break;
130c43cc173Smrg	    case StringFeedbackClass:
131c43cc173Smrg	    {
132c43cc173Smrg		xStringFeedbackState *strf = (xStringFeedbackState *) f;
133f1c62215Smrg		if ((char *)f + sizeof(*strf) > end)
134f1c62215Smrg		    goto out;
135c43cc173Smrg		size += sizeof(XStringFeedbackState) +
136c43cc173Smrg		    (strf->num_syms_supported * sizeof(KeySym));
137c43cc173Smrg	    }
138c43cc173Smrg		break;
139c43cc173Smrg	    case LedFeedbackClass:
140c43cc173Smrg		size += sizeof(XLedFeedbackState);
141c43cc173Smrg		break;
142c43cc173Smrg	    case BellFeedbackClass:
143c43cc173Smrg		size += sizeof(XBellFeedbackState);
144c43cc173Smrg		break;
145c43cc173Smrg	    default:
1465bc19eebSmrg		/* unknown classes are skipped */
147c43cc173Smrg		break;
148c43cc173Smrg	    }
149190694daSmrg	    if (size > INT_MAX)
150190694daSmrg		goto out;
151c43cc173Smrg	    f = (xFeedbackState *) ((char *)f + f->length);
152c43cc173Smrg	}
153c43cc173Smrg
154190694daSmrg	Feedback = Xmalloc(size);
15537eb1ca1Smrg	if (!Feedback)
15637eb1ca1Smrg	    goto out;
15737eb1ca1Smrg
158c43cc173Smrg	Sav = Feedback;
1595bc19eebSmrg	known_feedbacks = num_feedbacks;
160c43cc173Smrg
161c43cc173Smrg	f = sav;
1625bc19eebSmrg	for (i = 0; i < num_feedbacks; i++) {
1635bc19eebSmrg	    Bool skip = False;
164c43cc173Smrg	    switch (f->class) {
165c43cc173Smrg	    case KbdFeedbackClass:
166c43cc173Smrg	    {
167c43cc173Smrg		xKbdFeedbackState *k;
168c43cc173Smrg		XKbdFeedbackState *K;
169c43cc173Smrg
170c43cc173Smrg		k = (xKbdFeedbackState *) f;
171c43cc173Smrg		K = (XKbdFeedbackState *) Feedback;
172c43cc173Smrg
173c43cc173Smrg		K->class = k->class;
174c43cc173Smrg		K->length = sizeof(XKbdFeedbackState);
175c43cc173Smrg		K->id = k->id;
176c43cc173Smrg		K->click = k->click;
177c43cc173Smrg		K->percent = k->percent;
178c43cc173Smrg		K->pitch = k->pitch;
179c43cc173Smrg		K->duration = k->duration;
180c43cc173Smrg		K->led_mask = k->led_mask;
181c43cc173Smrg		K->global_auto_repeat = k->global_auto_repeat;
182c43cc173Smrg		memcpy((char *)&K->auto_repeats[0],
183c43cc173Smrg		       (char *)&k->auto_repeats[0], 32);
184c43cc173Smrg		break;
185c43cc173Smrg	    }
186c43cc173Smrg	    case PtrFeedbackClass:
187c43cc173Smrg	    {
188c43cc173Smrg		xPtrFeedbackState *p;
189c43cc173Smrg		XPtrFeedbackState *P;
190c43cc173Smrg
191c43cc173Smrg		p = (xPtrFeedbackState *) f;
192c43cc173Smrg		P = (XPtrFeedbackState *) Feedback;
193c43cc173Smrg
194c43cc173Smrg		P->class = p->class;
195c43cc173Smrg		P->length = sizeof(XPtrFeedbackState);
196c43cc173Smrg		P->id = p->id;
197c43cc173Smrg		P->accelNum = p->accelNum;
198c43cc173Smrg		P->accelDenom = p->accelDenom;
199c43cc173Smrg		P->threshold = p->threshold;
200c43cc173Smrg		break;
201c43cc173Smrg	    }
202c43cc173Smrg	    case IntegerFeedbackClass:
203c43cc173Smrg	    {
204f1ee322dSmrg		xIntegerFeedbackState *ifs;
205c43cc173Smrg		XIntegerFeedbackState *I;
206c43cc173Smrg
207f1ee322dSmrg		ifs = (xIntegerFeedbackState *) f;
208c43cc173Smrg		I = (XIntegerFeedbackState *) Feedback;
209c43cc173Smrg
210f1ee322dSmrg		I->class = ifs->class;
211c43cc173Smrg		I->length = sizeof(XIntegerFeedbackState);
212f1ee322dSmrg		I->id = ifs->id;
213f1ee322dSmrg		I->resolution = ifs->resolution;
214f1ee322dSmrg		I->minVal = ifs->min_value;
215f1ee322dSmrg		I->maxVal = ifs->max_value;
216c43cc173Smrg		break;
217c43cc173Smrg	    }
218c43cc173Smrg	    case StringFeedbackClass:
219c43cc173Smrg	    {
220c43cc173Smrg		xStringFeedbackState *s;
221c43cc173Smrg		XStringFeedbackState *S;
222c43cc173Smrg
223c43cc173Smrg		s = (xStringFeedbackState *) f;
224c43cc173Smrg		S = (XStringFeedbackState *) Feedback;
225c43cc173Smrg
226c43cc173Smrg		S->class = s->class;
227c43cc173Smrg		S->length = sizeof(XStringFeedbackState) +
228c43cc173Smrg		    (s->num_syms_supported * sizeof(KeySym));
229c43cc173Smrg		S->id = s->id;
230c43cc173Smrg		S->max_symbols = s->max_symbols;
231c43cc173Smrg		S->num_syms_supported = s->num_syms_supported;
232c43cc173Smrg		S->syms_supported = (KeySym *) (S + 1);
233c43cc173Smrg		memcpy((char *)S->syms_supported, (char *)(s + 1),
234c43cc173Smrg		       (S->num_syms_supported * sizeof(KeySym)));
235c43cc173Smrg		break;
236c43cc173Smrg	    }
237c43cc173Smrg	    case LedFeedbackClass:
238c43cc173Smrg	    {
239c43cc173Smrg		xLedFeedbackState *l;
240c43cc173Smrg		XLedFeedbackState *L;
241c43cc173Smrg
242c43cc173Smrg		l = (xLedFeedbackState *) f;
243c43cc173Smrg		L = (XLedFeedbackState *) Feedback;
244c43cc173Smrg
245c43cc173Smrg		L->class = l->class;
246c43cc173Smrg		L->length = sizeof(XLedFeedbackState);
247c43cc173Smrg		L->id = l->id;
248c43cc173Smrg		L->led_values = l->led_values;
249c43cc173Smrg		L->led_mask = l->led_mask;
250c43cc173Smrg		break;
251c43cc173Smrg	    }
252c43cc173Smrg	    case BellFeedbackClass:
253c43cc173Smrg	    {
254c43cc173Smrg		xBellFeedbackState *b;
255c43cc173Smrg		XBellFeedbackState *B;
256c43cc173Smrg
257c43cc173Smrg		b = (xBellFeedbackState *) f;
258c43cc173Smrg		B = (XBellFeedbackState *) Feedback;
259c43cc173Smrg
260c43cc173Smrg		B->class = b->class;
261c43cc173Smrg		B->length = sizeof(XBellFeedbackState);
262c43cc173Smrg		B->id = b->id;
263c43cc173Smrg		B->percent = b->percent;
264c43cc173Smrg		B->pitch = b->pitch;
265c43cc173Smrg		B->duration = b->duration;
266c43cc173Smrg		break;
267c43cc173Smrg	    }
268c43cc173Smrg	    default:
2695bc19eebSmrg		--known_feedbacks;
2705bc19eebSmrg		skip = True;
271c43cc173Smrg		break;
272c43cc173Smrg	    }
273c43cc173Smrg	    f = (xFeedbackState *) ((char *)f + f->length);
2745bc19eebSmrg	    if (!skip)
2755bc19eebSmrg		Feedback = (XFeedbackState *) ((char *)Feedback + Feedback->length);
276c43cc173Smrg	}
277c43cc173Smrg    }
27837eb1ca1Smrgout:
27937eb1ca1Smrg    XFree((char *)sav);
2805bc19eebSmrg    *num_feedbacks_out = known_feedbacks;
281c43cc173Smrg
282c43cc173Smrg    UnlockDisplay(dpy);
283c43cc173Smrg    SyncHandle();
284c43cc173Smrg    return (Sav);
285c43cc173Smrg}
286c43cc173Smrg
287c43cc173Smrgvoid
288c27c18e8SmrgXFreeFeedbackList(XFeedbackState *list)
289c43cc173Smrg{
290c43cc173Smrg    XFree((char *)list);
291c43cc173Smrg}
292