author | František Kučera <franta-hg@frantovo.cz> |
Tue, 30 Mar 2021 20:26:08 +0200 | |
branch | v_0 |
changeset 4 | e5baa07d6e60 |
parent 3 | 2c4e533e9e33 |
child 5 | dbf093b8b9ac |
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 <memory> |
|
20 |
#include <string> |
|
21 |
#include <vector> |
|
22 |
#include <iostream> |
|
23 |
#include <sstream> |
|
24 |
#include <locale> |
|
25 |
#include <codecvt> |
|
26 |
#include <stdexcept> |
|
27 |
||
2 | 28 |
#include <X11/extensions/XTest.h> |
29 |
||
1 | 30 |
#include <relpipe/common/type/typedefs.h> |
0 | 31 |
#include <relpipe/reader/TypeId.h> |
32 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
33 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
34 |
||
35 |
#include "Configuration.h" |
|
36 |
||
37 |
namespace relpipe { |
|
38 |
namespace out { |
|
39 |
namespace x11 { |
|
40 |
||
1 | 41 |
class X11Handler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
0 | 42 |
private: |
2 | 43 |
|
44 |
class Display { |
|
45 |
public: |
|
46 |
::Display* display = nullptr; |
|
47 |
// TODO: more OOP |
|
48 |
||
49 |
virtual ~Display() { |
|
50 |
if (display) XCloseDisplay(display); |
|
51 |
} |
|
52 |
}; |
|
53 |
||
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
54 |
class Event { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
public: |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
56 |
|
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
enum class Type { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
58 |
KEY, |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
59 |
BUTTON, |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
60 |
MOTION, |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
61 |
UNKNOWN |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
}; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
63 |
|
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
enum class State { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
65 |
PRESSED, |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
66 |
RELEASED, |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
67 |
UNKNOWN |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
}; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
69 |
|
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
Type type = Type::UNKNOWN; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
71 |
State state = State::UNKNOWN; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
72 |
int key = -1; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
73 |
int button = -1; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
74 |
unsigned long screen = 0; |
4
e5baa07d6e60
use CurrentTime (0) as default delay
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
75 |
unsigned long delay = CurrentTime; |
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
76 |
int x = -1; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
77 |
int y = -1; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
78 |
} currentEvent; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
79 |
|
2 | 80 |
Display display; |
0 | 81 |
Configuration& configuration; |
82 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: probably not needed |
|
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
83 |
std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
84 |
relpipe::common::type::Integer attributeIndex = 0; |
0 | 85 |
public: |
86 |
||
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
87 |
X11Handler(Configuration& configuration) : configuration(configuration) { |
2 | 88 |
display.display = XOpenDisplay(nullptr); |
0 | 89 |
} |
90 |
||
1 | 91 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
2 | 92 |
if (display.display) { |
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
93 |
if (this->attributes.empty()) { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
94 |
this->attributes = attributes; |
2 | 95 |
} else { |
96 |
throw std::logic_error("Only a single relation can be converted to the X11 format."); |
|
97 |
} |
|
0 | 98 |
} else { |
2 | 99 |
throw std::invalid_argument("Unable to open display. Please check the $DISPLAY variable."); |
0 | 100 |
} |
2 | 101 |
|
0 | 102 |
} |
103 |
||
1 | 104 |
void attribute(const relpipe::common::type::StringX& value) override { |
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
105 |
if (attributes[attributeIndex].getAttributeName() == L"device") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
106 |
// TODO: maybe add some filtering based on device ID |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
} else if (attributes[attributeIndex].getAttributeName() == L"type") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
108 |
if (value == L"key") currentEvent.type = Event::Type::KEY; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
109 |
else if (value == L"button") currentEvent.type = Event::Type::BUTTON; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
110 |
else if (value == L"motion") currentEvent.type = Event::Type::MOTION; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
111 |
else currentEvent.type == Event::Type::UNKNOWN; // TODO: throw exception or print warning? |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
112 |
} else if (attributes[attributeIndex].getAttributeName() == L"state") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
113 |
if (value == L"pressed") currentEvent.state = Event::State::PRESSED; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
114 |
else if (value == L"released") currentEvent.state = Event::State::RELEASED; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
115 |
else currentEvent.state == Event::State::UNKNOWN; // TODO: throw exception or print warning? |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
116 |
} else if (attributes[attributeIndex].getAttributeName() == L"button") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
currentEvent.button = stol(value); |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
} else if (attributes[attributeIndex].getAttributeName() == L"key") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
119 |
currentEvent.key = stol(value); |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
120 |
} else if (attributes[attributeIndex].getAttributeName() == L"delay") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
121 |
currentEvent.delay = stoul(value); |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
122 |
} else if (attributes[attributeIndex].getAttributeName() == L"screen") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
123 |
currentEvent.screen = stol(value); |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
124 |
} else if (attributes[attributeIndex].getAttributeName() == L"x") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
125 |
currentEvent.x = stol(value); |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
126 |
} else if (attributes[attributeIndex].getAttributeName() == L"y") { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
127 |
currentEvent.y = stol(value); |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
128 |
} else { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
129 |
// ignore other attributes |
0 | 130 |
} |
131 |
||
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
132 |
attributeIndex++; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
133 |
|
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
134 |
if (attributeIndex % attributes.size() == 0) { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
135 |
if (currentEvent.type == Event::Type::KEY) { |
4
e5baa07d6e60
use CurrentTime (0) as default delay
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
136 |
XTestFakeKeyEvent(display.display, currentEvent.key, currentEvent.state == Event::State::PRESSED, currentEvent.delay); |
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
137 |
} else if (currentEvent.type == Event::Type::BUTTON) { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
138 |
XTestFakeButtonEvent(display.display, currentEvent.button, currentEvent.state == Event::State::PRESSED, currentEvent.delay); |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
139 |
} else if (currentEvent.type == Event::Type::MOTION) { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
140 |
XTestFakeMotionEvent(display.display, currentEvent.screen, currentEvent.x, currentEvent.y, currentEvent.delay); |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
141 |
} else { |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
142 |
std::wcerr << L"Unsupported event" << std::endl; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
143 |
} |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
144 |
|
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
145 |
attributeIndex = 0; |
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
146 |
currentEvent = Event(); |
0 | 147 |
} |
148 |
} |
|
149 |
||
150 |
void endOfPipe() { |
|
3
2c4e533e9e33
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
151 |
// XTestDiscard(display.display); // TODO: wait before discard? |
0 | 152 |
} |
153 |
||
154 |
}; |
|
155 |
||
156 |
} |
|
157 |
} |
|
158 |
} |