author | František Kučera <franta-hg@frantovo.cz> |
Fri, 26 Mar 2021 22:29:46 +0100 | |
branch | v_0 |
changeset 1 | 3bbf848b3565 |
parent 0 | 3493d6e7016e |
child 2 | 8d44cba0a3d1 |
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> |
|
21 |
#include <iostream> |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
22 |
#include <stdexcept> |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
23 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
24 |
#include <X11/extensions/XInput.h> |
0 | 25 |
|
26 |
#include "Configuration.h" |
|
27 |
||
28 |
namespace relpipe { |
|
29 |
namespace in { |
|
30 |
namespace x11 { |
|
31 |
||
32 |
class X11Command { |
|
33 |
private: |
|
34 |
||
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
35 |
class Display { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
36 |
public: |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
37 |
::Display* display = nullptr; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
// TODO: more OOP |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
40 |
virtual ~Display() { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
if (display) XCloseDisplay(display); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
42 |
} |
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 |
class DeviceList { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
public: |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
XDeviceInfo* items = nullptr; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
int count = 0; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
// TODO: more OOP |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
virtual ~DeviceList() { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
if (items) XFreeDeviceList(items); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
} |
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 |
|
0 | 59 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default |
60 |
||
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
{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
|
64 |
{L"name", relpipe::writer::TypeId::STRING}, |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
// TODO: device type name and ID |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
}, true); |
0 | 67 |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
DeviceList devices; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
devices.items = XListInputDevices(display.display, &devices.count); |
0 | 70 |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
relpipe::common::type::StringX name = convertor.from_bytes((devices.items + i)->name); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
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
|
76 |
writer->writeAttribute(&name, typeid (name)); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
|
0 | 78 |
} |
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
} |
0 | 80 |
|
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
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
|
82 |
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
|
83 |
{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
|
84 |
}, true); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
// TODO: list events |
0 | 87 |
} |
88 |
||
89 |
public: |
|
90 |
||
91 |
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
|
92 |
Display display; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
93 |
display.display = XOpenDisplay(nullptr); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
|
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
95 |
if (display.display) { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
96 |
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
|
97 |
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
|
98 |
} else { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
99 |
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
|
100 |
} |
0 | 101 |
} |
102 |
}; |
|
103 |
||
104 |
} |
|
105 |
} |
|
106 |
} |