Imported Upstream version 1.6.1
[platform/upstream/libXi.git] / src / XChDProp.c
1 /************************************************************
2
3 Copyright 2008 Peter Hutterer
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 AUTHOR 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 author 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 author.
24
25 */
26
27 /***********************************************************************
28  * XChangeDeviceProperties - change an input device's properties.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <X11/Xlibint.h>
36 #include <X11/extensions/XI.h>
37 #include <X11/extensions/XIproto.h>
38 #include <X11/extensions/XInput.h>
39 #include <X11/extensions/extutil.h>
40 #include "XIint.h"
41
42 void
43 XChangeDeviceProperty(Display* dpy, XDevice* dev,
44                       Atom property, Atom type,
45                       int format, int mode,
46                       _Xconst unsigned char *data, int nelements)
47 {
48     xChangeDevicePropertyReq   *req;
49     int                         len;
50
51     XExtDisplayInfo *info = XInput_find_display(dpy);
52
53     LockDisplay(dpy);
54     if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
55         return;
56
57     GetReq(ChangeDeviceProperty, req);
58     req->reqType    = info->codes->major_opcode;
59     req->ReqType    = X_ChangeDeviceProperty;
60     req->deviceid   = dev->device_id;
61     req->property   = property;
62     req->type       = type;
63     req->mode       = mode;
64     if (nelements < 0) {
65         req->nUnits = 0;
66         req->format = 0; /* ask for garbage, get garbage */
67     } else {
68         req->nUnits = nelements;
69         req->format = format;
70     }
71
72     switch (req->format) {
73     case 8:
74         len = ((long)nelements + 3) >> 2;
75         if (dpy->bigreq_size || req->length + len <= (unsigned) 65535) {
76             SetReqLen(req, len, len);
77             Data (dpy, (char *)data, nelements);
78         } /* else force BadLength */
79         break;
80
81     case 16:
82         len = ((long)nelements + 1) >> 1;
83         if (dpy->bigreq_size || req->length + len <= (unsigned) 65535) {
84             SetReqLen(req, len, len);
85             len = (long)nelements << 1;
86             Data16 (dpy, (short *) data, len);
87         } /* else force BadLength */
88         break;
89
90     case 32:
91         len = nelements;
92         if (dpy->bigreq_size || req->length + len <= (unsigned) 65535) {
93             SetReqLen(req, len, len);
94             len = (long)nelements << 2;
95             Data32 (dpy, (long *) data, len);
96         } /* else force BadLength */
97         break;
98
99     default:
100         /* BadValue will be generated */ ;
101     }
102
103     UnlockDisplay(dpy);
104     SyncHandle();
105 }
106