Repository navigation
Expand file tree
/
Copy pathinput.cpp
More file actions
181 lines (148 loc) · 4.78 KB
/
Copy pathinput.cpp
File metadata and controls
181 lines (148 loc) · 4.78 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
#include "input.h"
#include <unistd.h>
#include <sys/select.h>
#include <cstring>
#include <cerrno>
#include <unistd.h>
namespace PlexTUI {
Input::Input() = default;
Input::~Input() = default;
bool Input::has_input() {
if (!terminal_valid) return false;
// Check if terminal is still valid
if (!isatty(STDIN_FILENO)) {
terminal_valid = false;
return false;
}
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
struct timeval timeout = {0, 0};
int result = select(STDIN_FILENO + 1, &readfds, nullptr, nullptr, &timeout);
if (result < 0) {
// Error - terminal likely closed
terminal_valid = false;
return false;
}
return result > 0;
}
InputEvent Input::poll() {
InputEvent event;
if (!has_input()) {
return event;
}
char buf[32];
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf) - 1);
if (n <= 0) {
// EOF or error - terminal likely closed
if (n == 0 || (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
terminal_valid = false;
}
return event;
}
buf[n] = '\0';
// Check for escape sequences
if (buf[0] == '\033') {
if (n == 1) {
event.key = Key::Escape;
return event;
}
std::string seq(buf + 1, n - 1);
// Mouse events
if (seq[0] == '[' && seq[1] == '<') {
event.key = Key::Mouse;
event.mouse = parse_mouse_event(seq);
return event;
}
// Arrow keys and special keys
if (seq == "[A") event.key = Key::Up;
else if (seq == "[B") event.key = Key::Down;
else if (seq == "[C") event.key = Key::Right;
else if (seq == "[D") event.key = Key::Left;
else if (seq == "[5~") event.key = Key::PageUp;
else if (seq == "[6~") event.key = Key::PageDown;
else if (seq == "[H" || seq == "[1~") event.key = Key::Home;
else if (seq == "[F" || seq == "[4~") event.key = Key::End;
return event;
}
// Regular character
char c = buf[0];
// Map to keys
event.key = map_char_to_key(c);
if (event.key == Key::Char) {
event.character = c;
}
return event;
}
Key Input::map_char_to_key(char c) {
switch (c) {
case '\r':
case '\n': return Key::Enter;
case ' ': return Key::Pause;
case '\t': return Key::Tab;
case 127:
case '\b': return Key::Backspace;
// Playback
case 'p': return Key::Play;
case 's': return Key::Stop;
case 'n': return Key::Next;
case 'N': return Key::Previous;
// Volume
case '+':
case '=': return Key::VolumeUp;
case '-':
case '_': return Key::VolumeDown;
case 'm': return Key::Mute;
// Navigation
case '/': return Key::Search;
case 'q': return Key::Quit;
case '?': return Key::Help;
default: return Key::Char;
}
}
MouseEvent Input::parse_mouse_event(const std::string& seq) {
MouseEvent event;
// Parse SGR mouse format: [<B;X;YM or [<B;X;Ym
size_t pos = 2; // Skip "[<"
int button = 0;
int x = 0, y = 0;
char action = 'M';
sscanf(seq.c_str() + pos, "%d;%d;%d%c", &button, &x, &y, &action);
event.x = x - 1; // Convert to 0-based
event.y = y - 1;
// Parse button
if (button == 0) event.button = MouseEvent::Button::Left;
else if (button == 1) event.button = MouseEvent::Button::Middle;
else if (button == 2) event.button = MouseEvent::Button::Right;
else if (button == 64) event.button = MouseEvent::Button::ScrollUp;
else if (button == 65) event.button = MouseEvent::Button::ScrollDown;
// Parse action
if (action == 'M') {
event.type = MouseEvent::Type::Press;
} else if (action == 'm') {
event.type = MouseEvent::Type::Release;
} else if (button >= 32) {
event.type = MouseEvent::Type::Move;
}
if (button == 64 || button == 65) {
event.type = MouseEvent::Type::Scroll;
}
return event;
}
std::string Input::key_name(Key key) {
switch (key) {
case Key::Play: return "Play (p)";
case Key::Pause: return "Pause (space)";
case Key::Stop: return "Stop (s)";
case Key::Next: return "Next (n)";
case Key::Previous: return "Previous (N)";
case Key::VolumeUp: return "Vol+ (+)";
case Key::VolumeDown: return "Vol- (-)";
case Key::Mute: return "Mute (m)";
case Key::Search: return "Search (/)";
case Key::Quit: return "Quit (q)";
case Key::Help: return "Help (?)";
default: return "";
}
}
} // namespace PlexTUI