summaryrefslogtreecommitdiff
path: root/tests/common/xit-property.h
blob: b0d28185c75817c6bc741bcd2f812fad1830f619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright © 2012-2013 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */

#ifndef _ATOM_HELPERS_H_
#define _ATOM_HELPERS_H_

#include <stdint.h>
#include <xorg/gtest/xorg-gtest.h>
#include <X11/Xatom.h>
#include <X11/extensions/XInput2.h>

#define ASSERT_PROPERTY(_type, _name, _dpy, _deviceid, _propname) \
    ASSERT_TRUE(DevicePropertyExists(_dpy, _deviceid, _propname)); \
    XITProperty<_type> _name(_dpy, _deviceid, _propname);


template <typename DataType>
class XITProperty {
private:
    /* I'm too lazy for that right now. */
    XITProperty(const XITProperty&);
    XITProperty& operator=(const XITProperty &);

    ::Display *dpy;
    int deviceid;

public:
    Atom type;
    int format;
    unsigned long nitems;

    DataType *data;

    Atom prop;
    std::string propname;

    XITProperty(::Display *dpy, int deviceid, const std::string &propname);
    XITProperty(::Display *dpy, int deviceid, const std::string &propname,
                Atom type, int format, unsigned long nitems, const DataType *data);
    ~XITProperty();

    /**
     * Resize the data member to hold nitems.
     */
    void Resize(size_t nitems);
    void Update(void);

    /**
     * Get value at index, throw an exception if we're OOB.
     */
    DataType At(size_t index) const;

    /**
     * Set value at index, resizing data as-needed.
     */
    void Set(size_t index, DataType value);
};


bool DevicePropertyExists(::Display *dpy, int deviceid, const std::string &propname);

template <typename DataType>
XITProperty<DataType>::XITProperty(::Display *dpy, int deviceid, const std::string &propname)
{
    this->dpy = dpy;
    this->deviceid = deviceid;
    this->format = -1;
    this->data = NULL;

    if (!DevicePropertyExists(dpy, deviceid, propname)) {
        ADD_FAILURE() << "Property " << propname << " does not exist on device";
        return;
    }

    prop = XInternAtom(dpy, propname.c_str(), True);

    unsigned char *d = nullptr;

    unsigned long bytes_after;
    XIGetProperty(dpy, deviceid, prop, 0, 1000, False,
                  AnyPropertyType, &type, &format, &nitems, &bytes_after, &d);

    if (!d)
        return;

    /* XI2 32-bit properties are actually 32 bit, Atom is > 4, so copy over
     * where needed */
    data = new DataType[nitems];
    if (sizeof(DataType) > 4) {
        for (unsigned int i = 0; i < nitems; i++)
            data[i] = reinterpret_cast<int32_t*>(d)[i];
    } else
        memcpy(data, d, nitems * sizeof(DataType));
    XFree(d);
}

template <typename DataType>
XITProperty<DataType>::XITProperty(::Display *dpy, int deviceid, const std::string &propname,
                                   Atom type, int format, unsigned long nitems, const DataType *data)
{
    if (format != 8 && format != 16 && format != 32) {
        ADD_FAILURE()  << "Invalid format " << format;
        return;
    }
    this->format = format;
    this->nitems = nitems;
    this->data = new DataType[nitems];

    memcpy(this->data, data, nitems * sizeof(data));

    union  {
        unsigned char *c;
        unsigned short *s;
        uint32_t *i;
    } d;

    d.c = new unsigned char[format/8 * nitems];

    for (unsigned int i = 0; i < nitems; i++) {
        switch(format) {
            case  8: d.c[i] = data[i]; break;
            case 16: d.s[i] = data[i]; break;
            case 32: d.i[i] = data[i]; break;
        }
    }

    prop = XInternAtom(dpy, propname.c_str(), False);
    XIChangeProperty(dpy, deviceid, prop, type, format, PropModeReplace, d.c, nitems);
    XSync(dpy, False);

    delete[] d.c;
}


template <typename DataType>
void XITProperty<DataType>::Update(void)
{
    unsigned char *d;

    /* XI2 32-bit properties are actually 32 bit, Atom is > 4, so copy over
     * where needed */
    if (sizeof(DataType) > 4) {
        int32_t tmp[nitems];
        for (unsigned int i = 0; i < nitems; i++)
            tmp[i] = data[i];
        d = reinterpret_cast<unsigned char*>(tmp);
    } else
        d = reinterpret_cast<unsigned char*>(data);

    XIChangeProperty(dpy, deviceid, prop, type, format, PropModeReplace, d, nitems);
    XSync(dpy, False);
}

template <typename DataType>
void XITProperty<DataType>::Resize(size_t nitems)
{
    size_t sz = (sizeof(DataType) >= 4) ? 4 : sizeof(DataType);

    data = reinterpret_cast<DataType*>(realloc(data, nitems * sz));

    if (nitems > this->nitems)
        memset(&data[this->nitems], 0, (nitems - this->nitems) * sz);

    this->nitems = nitems;
}

template <typename DataType>
XITProperty<DataType>::~XITProperty()
{
    delete[] data;
}

template <typename DataType>
DataType XITProperty<DataType>::At(size_t index) const
{
    if (index > nitems)
        throw new std::runtime_error("Array index out of bounds");
    return data[index];
}

template <typename DataType>
void XITProperty<DataType>::Set(size_t index, DataType value)
{
    if (index >= nitems)
        Resize(index + 1);
    data[index] = value;
}

#endif /* _ATOM_HELPERS_H_ */