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