author | František Kučera <franta-hg@frantovo.cz> |
Sat, 27 Mar 2021 00:29:10 +0100 | |
branch | v_0 |
changeset 2 | 8d44cba0a3d1 |
parent 1 | 3bbf848b3565 |
child 3 | 72384bb5c66e |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <codecvt> |
|
20 |
#include <memory> |
|
2
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
21 |
#include <algorithm> |
0 | 22 |
#include <iostream> |
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
23 |
#include <stdexcept> |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
24 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
25 |
#include <X11/extensions/XInput.h> |
0 | 26 |
|
27 |
#include "Configuration.h" |
|
28 |
||
29 |
namespace relpipe { |
|
30 |
namespace in { |
|
31 |
namespace x11 { |
|
32 |
||
33 |
class X11Command { |
|
34 |
private: |
|
35 |
||
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
36 |
class Display { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
37 |
public: |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
::Display* display = nullptr; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
// TODO: more OOP |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
40 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
virtual ~Display() { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
42 |
if (display) XCloseDisplay(display); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
} |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
}; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
class DeviceList { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
public: |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
XDeviceInfo* items = nullptr; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
int count = 0; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
// TODO: more OOP |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
virtual ~DeviceList() { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
if (items) XFreeDeviceList(items); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
} |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
}; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
|
0 | 60 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default |
61 |
||
2
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
62 |
relpipe::common::type::StringX getDeviceType(Display& display, XDeviceInfo* device) { |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
63 |
if (device && device->type) { |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
64 |
char* raw = XGetAtomName(display.display, device->type); |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
65 |
if (raw) { |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
66 |
relpipe::common::type::StringX type = convertor.from_bytes(raw); |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
67 |
XFree(raw); |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
68 |
transform(type.begin(), type.end(), type.begin(), ::tolower); |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
69 |
return type; |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
70 |
} |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
71 |
} |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
72 |
return L""; |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
73 |
} |
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
74 |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
void listInputDevices(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
writer->startRelation(L"x11_input_device",{ |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
{L"id", relpipe::writer::TypeId::INTEGER}, |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
{L"name", relpipe::writer::TypeId::STRING}, |
2
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
79 |
{L"type", relpipe::writer::TypeId::STRING}, |
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
}, true); |
0 | 81 |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
DeviceList devices; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
devices.items = XListInputDevices(display.display, &devices.count); |
0 | 84 |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
for (int i = 0; i < devices.count; i++) { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
relpipe::common::type::Integer id = (devices.items + i)->id; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
relpipe::common::type::StringX name = convertor.from_bytes((devices.items + i)->name); |
2
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
88 |
relpipe::common::type::StringX type = getDeviceType(display, devices.items + i); |
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
writer->writeAttribute(&id, typeid (id)); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
writer->writeAttribute(&name, typeid (name)); |
2
8d44cba0a3d1
list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
92 |
writer->writeAttribute(&type, typeid (type)); |
0 | 93 |
} |
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
} |
0 | 95 |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
96 |
void listInputEvents(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
97 |
writer->startRelation(L"x11_input_event",{ |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
98 |
{L"device_id", relpipe::writer::TypeId::INTEGER}, |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
99 |
}, true); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
100 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
101 |
// TODO: list events |
0 | 102 |
} |
103 |
||
104 |
public: |
|
105 |
||
106 |
void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) { |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
107 |
Display display; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
108 |
display.display = XOpenDisplay(nullptr); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
109 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
110 |
if (display.display) { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
111 |
if (configuration.listInputDevices) listInputDevices(display, configuration, writer); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
112 |
if (configuration.listInputEvents) listInputEvents(display, configuration, writer); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
113 |
} else { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
114 |
throw std::invalid_argument("Unable to open display. Please check the $DISPLAY variable."); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
115 |
} |
0 | 116 |
} |
117 |
}; |
|
118 |
||
119 |
} |
|
120 |
} |
|
121 |
} |