This repository was archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrotateinput.cpp
More file actions
181 lines (162 loc) · 5.37 KB
/
Copy pathrotateinput.cpp
File metadata and controls
181 lines (162 loc) · 5.37 KB
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
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2018 Marco Gulino <marco@gulinux.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include "rotateinput.h"
#include <QtX11Extras/QX11Info>
#include <QList>
#include <X11/extensions/XInput2.h>
#include <X11/Xatom.h>
using namespace std;
struct InputProperty {
InputProperty(int deviceId, Display *display, Atom atom);
int deviceId;
Display *display;
Atom atom;
Atom type;
int format;
QString name;
bool isRotationMatrix() const;
void setRotationMatrix(const vector<float> &matrix);
};
struct InputDevice {
InputDevice(XIDeviceInfo *info, Display *display);
Display *display;
QString name;
int id;
bool isTouchScreen = false;
bool hasRotationMatrix = false;
QList<InputProperty> properties();
};
InputDevice::InputDevice(XIDeviceInfo* info, Display *display) : display{display}
{
name = QString{info->name};
id = info->deviceid;
for(int j=0; j<info->num_classes; j++) {
if(info->classes[j]->type == XITouchClass) {
qDebug() << "Device" << info->name << "seems to be a touchscreen device";
isTouchScreen = true;
}
}
for(auto property: properties()) {
if(property.isRotationMatrix())
this->hasRotationMatrix = true;
}
}
QList<InputProperty> InputDevice::properties()
{
QList<InputProperty> properties;
int properties_size;
Atom *atoms = XIListProperties(display, id, &properties_size);
for(int i=0; i<properties_size; i++)
properties.push_back(InputProperty{id, display, atoms[i]});
XFree(atoms);
return properties;
}
InputProperty::InputProperty(int deviceId, Display* display, Atom atom) : deviceId{deviceId}, display{display}, atom{atom}
{
char *name_c = XGetAtomName(display, atom);
name = QString{name_c};
XFree(name_c);
Atom property_type;
int property_format;
uint64_t items_count, bytes_after;
uint8_t *data;
if(XIGetProperty(display, deviceId, atom, 0, 0, False, AnyPropertyType, &property_type, &property_format, &items_count, &bytes_after, &data) == Success) {
type = property_type;
format = property_format;
qDebug() << "Property name=" << name << ", atom=" << atom << ", format=" << format << ", type=" << type;
XFree(data);
} else {
qDebug() << "Unable to get data for property " << name;
}
}
bool InputProperty::isRotationMatrix() const
{
return name == "Coordinate Transformation Matrix";
}
void InputProperty::setRotationMatrix(const vector<float>& matrix)
{
union {
unsigned char *c;
float *f;
} data;
data.f = new float[matrix.size()];
int data_size = 0;
for(auto item: matrix) {
data.f[data_size++] = item;
}
XIChangeProperty(display, deviceId, atom, type, format, PropModeReplace, data.c, matrix.size());
delete data.f;
}
class RotateInput::Private {
public:
Display *display;
QList<InputDevice> devices;
};
RotateInput::RotateInput(QObject* parent) : QObject{parent}, d{new Private}
{
d->display = QX11Info::display();
int devices;
XIDeviceInfo *deviceInfo = XIQueryDevice(d->display, XIAllDevices, &devices);
for(int i=0; i<devices; i++) {
InputDevice device{&deviceInfo[i], d->display};
if(device.hasRotationMatrix)
d->devices.push_back(device);
}
XIFreeDeviceInfo(deviceInfo);
for(auto device: d->devices) {
for(auto property: device.properties()) {
qDebug() << "Device" << device.name << ", property:" << property.name << ", atom=" << property.atom << ", type: " << property.type;
}
}
}
RotateInput::~RotateInput()
{
}
void RotateInput::rotate(Orientation orientation)
{
#ifdef USE_XINPUT
static QHash<Orientation, QStringList> orientation_matrix_map {
{TopUp, {"1", "0", "0", "0", "1", "0", "0", "0", "1"}},
{TopDown, {"-1", "0", "1", "0", "-1", "1", "0", "0", "1"}},
{LeftUp, {"0", "-1", "1", "1", "0", "0", "0", "0", "1"}},
{RightUp, {"0", "1", "0", "-1", "0", "1", "0", "0", "1"}},
};
auto orientation_matrix = orientation_matrix_map[orientation];
for(auto device: d->devices) {
do_set_prop_xi2(d->display, device.id, None, 0, "Coordinate Transformation Matrix", orientation_matrix);
}
#else
static QHash<Orientation, vector<float>> orientation_matrix_map {
{TopUp, {1, 0, 0, 0, 1, 0, 0, 0, 1}},
{TopDown, {-1, 0, 1, 0, -1, 1, 0, 0, 1}},
{LeftUp, {0, -1, 1, 1, 0, 0, 0, 0, 1}},
{RightUp, {0, 1, 0, -1, 0, 1, 0, 0, 1}},
};
auto orientation_matrix = orientation_matrix_map[orientation];
qDebug() << "Setting rotation matrix: " << orientation_matrix;
for(auto device: d->devices) {
for(auto property: device.properties()) {
if(property.isRotationMatrix()) {
qDebug() << "Changing orientation matrix for device " << device.name << ", property " << property.name;
property.setRotationMatrix(orientation_matrix);
}
}
}
#endif
}