-
Notifications
You must be signed in to change notification settings - Fork 21
/
devicesettings.cpp
338 lines (319 loc) · 12 KB
/
devicesettings.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "devicesettings.h"
#include "debug.h"
#include "liboxide.h"
#include "signalhandler.h"
#include <private/qguiapplication_p.h>
#include <private/qinputdevicemanager_p.h>
#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
#define OFF(x) ((x)%BITS_PER_LONG)
#define LONG(x) ((x)/BITS_PER_LONG)
#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
using namespace Oxide;
namespace Oxide {
DeviceSettings& DeviceSettings::instance() {
static DeviceSettings INSTANCE;
return INSTANCE;
}
DeviceSettings::DeviceSettings(): _deviceType(DeviceType::RM1) {
readDeviceType();
O_DEBUG("Looking for input devices...");
QDir dir("/dev/input");
for(auto path : dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::System)){
if(!wacomPath.empty() && !touchPath.empty() && !buttonsPath.empty()){
break;
}
O_DEBUG((" Checking " + path + "...").toStdString().c_str());
QString fullPath(dir.path() + "/" + path);
QFile device(fullPath);
device.open(QIODevice::ReadOnly);
int fd = device.handle();
int version;
if(ioctl(fd, EVIOCGVERSION, &version)){
O_DEBUG(" Invalid");
continue;
}
unsigned long bit[EV_MAX];
ioctl(fd, EVIOCGBIT(0, EV_MAX), bit);
if(test_bit(EV_KEY, bit)){
if(checkBitSet(fd, EV_KEY, BTN_STYLUS) && test_bit(EV_ABS, bit)){
O_DEBUG(" Wacom input device detected");
if(wacomPath.empty()){
wacomPath = fullPath.toStdString();
}
continue;
}
if(checkBitSet(fd, EV_KEY, KEY_POWER)){
O_DEBUG(" Buttons input device detected");
if(buttonsPath.empty()){
buttonsPath = fullPath.toStdString();
}
continue;
}
}
if(checkBitSet(fd, EV_ABS, ABS_MT_SLOT)){
O_DEBUG(" Touch input device detected");
if(touchPath.empty()){
touchPath = fullPath.toStdString();
}
continue;
}
O_DEBUG(" Invalid");
}
if(wacomPath.empty()){
O_WARNING("Wacom input device not found");
}else{
O_DEBUG(("Wacom input device: " + wacomPath).c_str());
}
if(touchPath.empty()){
O_WARNING("Touch input device not found");
}else{
O_DEBUG(("Touch input device: " + touchPath).c_str());
}
if(buttonsPath.empty()){
O_WARNING("Buttons input device not found");
}else{
O_DEBUG(("Buttons input device: " + buttonsPath).c_str());
}
}
DeviceSettings::~DeviceSettings(){}
bool DeviceSettings::checkBitSet(int fd, int type, int i) {
unsigned long bit[NBITS(KEY_MAX)];
ioctl(fd, EVIOCGBIT(type, KEY_MAX), bit);
return test_bit(i, bit);
}
void DeviceSettings::readDeviceType() {
QFile file("/sys/devices/soc0/machine");
if(!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)){
O_DEBUG("Couldn't open " << file.fileName());
_deviceType = DeviceType::Unknown;
return;
}
QTextStream in(&file);
QString modelName = in.readLine();
if (modelName.startsWith("reMarkable 2")) {
O_DEBUG("RM2 detected...");
_deviceType = DeviceType::RM2;
return;
}
O_DEBUG("RM1 detected...");
_deviceType = DeviceType::RM1;
}
DeviceSettings::DeviceType DeviceSettings::getDeviceType() const { return _deviceType; }
const char* DeviceSettings::getButtonsDevicePath() const { return buttonsPath.c_str(); }
const char* DeviceSettings::getWacomDevicePath() const { return wacomPath.c_str(); }
const char* DeviceSettings::getTouchDevicePath() const { return touchPath.c_str(); }
const char* DeviceSettings::getDeviceName() const {
switch(getDeviceType()){
case DeviceType::RM1:
return "reMarkable 1";
case DeviceType::RM2:
return "reMarkable 2";
default:
return "Unknown";
}
}
const char* DeviceSettings::getTouchEnvSetting() const {
switch(getDeviceType()) {
case DeviceType::RM1:
return "rotate=180";
case DeviceType::RM2:
return "rotate=180:invertx";
default:
return "";
}
}
int DeviceSettings::getTouchWidth() const {
switch(getDeviceType()) {
case DeviceType::RM1:
return 767;
case DeviceType::RM2:
return 1403;
default:
return 0;
}
}
int DeviceSettings::getTouchHeight() const {
switch(getDeviceType()) {
case DeviceType::RM1:
return 1023;
case DeviceType::RM2:
return 1871;
default:
return 0;
}
}
const QStringList DeviceSettings::getLocales() {
return execute("localectl", QStringList() << "list-locales" << "--no-pager").split("\n");
}
QString DeviceSettings::getLocale() {
QFile file("/etc/locale.conf");
if(file.open(QFile::ReadOnly)){
while(!file.atEnd()){
QString line = file.readLine();
QStringList fields = line.split("=");
if(fields.first().trimmed() != "LANG"){
continue;
}
return fields.at(1).trimmed();
}
}
return qEnvironmentVariable("LANG", "C");
}
void DeviceSettings::setLocale(const QString& locale){
O_DEBUG("Setting locale:" << locale);
qputenv("LANG", locale.toUtf8());
QProcess::execute("localectl", QStringList() << "set-locale" << locale);
}
const QStringList DeviceSettings::getTimezones() {
return execute("timedatectl", QStringList() << "list-timezones" << "--no-pager").split("\n");
}
QString DeviceSettings::getTimezone() {
auto lines = execute("timedatectl", QStringList() << "show").split("\n");
for(auto line : lines){
QStringList fields = line.split("=");
if(fields.first().trimmed() != "Timezone"){
continue;
}
return fields.at(1).trimmed();
}
return "UTC";
}
void DeviceSettings::setTimezone(const QString& timezone) {
O_DEBUG("Setting timezone:" << timezone);
QProcess::execute("timedatectl", QStringList() << "set-timezone" << timezone);
}
void DeviceSettings::setupQtEnvironment(bool touch){
auto qt_version = qVersion();
if (strcmp(qt_version, QT_VERSION_STR) != 0){
qDebug() << "Version mismatch, Runtime: " << qt_version << ", Build: " << QT_VERSION_STR;
}
QCoreApplication::addLibraryPath("/opt/usr/lib/plugins");
qputenv("QMLSCENE_DEVICE", "software");
qputenv("QT_QUICK_BACKEND","software");
QString platform("oxide:enable_fonts:freetype:freetype");
if(touch){
qputenv(
"QT_QPA_PLATFORM",
QString("%1:%2")
.arg(platform)
.arg(deviceSettings.getTouchEnvSetting())
.toUtf8()
);
}else{
qputenv("QT_QPA_PLATFORM", platform.toUtf8());
}
}
bool DeviceSettings::keyboardAttached(){ return !physicalKeyboards().empty(); }
void DeviceSettings::onKeyboardAttachedChanged(std::function<void()> callback){
bool initialValue = keyboardAttached();
onInputDevicesChanged([this, callback, initialValue]{
static bool attached = initialValue;
bool nowAttached = keyboardAttached();
if(attached != nowAttached){
callback();
}
attached = nowAttached;
});
}
QList<event_device> DeviceSettings::inputDevices(){
QList<event_device> devices;
QDir dir("/dev/input");
for(auto path : dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::System)){
QString fullPath(dir.path() + "/" + path);
QFile device(fullPath);
device.open(QIODevice::ReadOnly);
int fd = device.handle();
int version;
if(ioctl(fd, EVIOCGVERSION, &version)){
continue;
}
devices.append(event_device(fullPath.toStdString(), O_RDWR | O_NONBLOCK));
}
return devices;
}
void DeviceSettings::onInputDevicesChanged(std::function<void()> callback){
callbacks.push_back(callback);
auto manager = QGuiApplicationPrivate::inputDeviceManager();
QObject::connect(manager, &QInputDeviceManager::deviceListChanged, qApp, [callback](QInputDeviceManager::DeviceType type){
Q_UNUSED(type);
callback();
});
}
QList<event_device> DeviceSettings::keyboards(){
QList<event_device> keyboards;
for(auto device : inputDevices()){
if(
device.device == buttonsPath
|| device.device == wacomPath
|| device.device == touchPath
){
continue;
}
int fd = device.fd;
unsigned long bit[EV_MAX];
ioctl(fd, EVIOCGBIT(0, EV_MAX), bit);
if(!test_bit(EV_KEY, bit)){
continue;
}
if(checkBitSet(fd, EV_KEY, BTN_STYLUS) && test_bit(EV_ABS, bit)){
continue;
}
auto name = QFileInfo(device.device.c_str()).baseName();
SysObject sys("/sys/class/input/" + name + "/device");
auto vendor = sys.strProperty("id/vendor");
if(vendor == "0000"){
continue;
}
auto product = sys.strProperty("id/product");
if(product == "0000"){
continue;
}
keyboards.append(device);
}
return keyboards;
}
static QStringList VIRTUAL_KEYBOARD_IDS(
QStringList() << "0fac:0ade" << "0fac:1ade" << "0000:ffff"
);
QList<event_device> DeviceSettings::physicalKeyboards(){
QList<event_device> physicalKeyboards;
for(auto device : keyboards()){
auto name = QFileInfo(device.device.c_str()).baseName();
SysObject sys("/sys/class/input/" + name + "/device/id");
auto id = QString("%1:%2")
.arg(sys.strProperty("vendor").c_str())
.arg(sys.strProperty("product").c_str());
if(!VIRTUAL_KEYBOARD_IDS.contains(id)){
physicalKeyboards.append(device);
}
}
return physicalKeyboards;
}
QList<event_device> DeviceSettings::virtualKeyboards(){
QList<event_device> physicalKeyboards;
for(auto device : keyboards()){
auto name = QFileInfo(device.device.c_str()).baseName();
SysObject sys("/sys/class/input/" + name + "/device/id");
auto id = QString("%1:%2")
.arg(sys.strProperty("vendor").c_str())
.arg(sys.strProperty("product").c_str());
if(VIRTUAL_KEYBOARD_IDS.contains(id)){
physicalKeyboards.append(device);
}
}
return physicalKeyboards;
}
const QString& DeviceSettings::version(){
static QString version;
static std::once_flag flag;
std::call_once(flag, [] {
QSettings settings("/usr/share/remarkable/update.conf", QSettings::IniFormat);
version = settings.value("REMARKABLE_RELEASE_VERSION").toString();
if (version.isEmpty()) {
qWarning() << "Failed to read version from update.conf";
}
});
return version;
}
}