Debian Codegraph @

src/XGetFCtl.c

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