-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIThread.cpp
More file actions
executable file
·287 lines (254 loc) · 7.06 KB
/
Copy pathUIThread.cpp
File metadata and controls
executable file
·287 lines (254 loc) · 7.06 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
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
#include <map>
#include "UIThread.h"
#include <sys/ioctl.h>
#include <csignal>
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <termios.h>
#define say(str) write(1,str,sizeof(str))
// #define move(x,y) say("\x1b[" y ";" x "H")
// #include "ncurses.h"
void sayWithBackground(char* string, bool blackNotWhite)
{
}
UIThread::UIThread(const char* threadName, soul::patch::SOULPatchAudioProcessor* patch)
: Thread(threadName),
patch(patch)
{
startThread(5);
}
UIThread::~UIThread()
{
stopThread(-1);
}
struct termios initial;
void cleanup(void) {
//clean up the alternate buffer
say("\x1b[2J");
//switch back to the normal buffer
say("\x1b[?1049l");
//show the cursor again
// say("\x1b[?25h");
tcsetattr(1, TCSANOW, &initial);
}
void cleanup_die(int i) {
std::exit(1);
}
void resize(int i) {
// spurious argument needed so that the
// function signature matches what signal(3) expects
struct winsize ws;
ioctl(1, TIOCGWINSZ, &ws);
// from here, call a function to repaint the screen
// (probably starting with "\x1b[2J")
int halfWidth = ws.ws_col / 2;
int halfHeight = ws.ws_row / 2;
int left = halfWidth - 4;
int top = halfHeight - 2;
//clean up the buffer
say("\x1b[2J");
say("\x1b[0;0H"); // move to 0,0
while (left-- > 0)
{
say("\x1b[1C");
}
while (top-- > 0)
{
say("\x1b[1B");
}
// paint keyboard
say("\u001b[37m"); // white text
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("2");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("3");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("5");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("6");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("7");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("9");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("0");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("=");
say("\u001b[47m"); // white background
say(" ");
for (int col = 20; col > 0; col--)
{
say("\x1b[1D"); // left
}
say("\x1b[1B"); // down
say("\u001b[30m"); // black text
say("\u001b[47m"); // white background
say("q");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("w");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("er");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("t");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("y");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("ui");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("o");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("p[");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("]");
for (int col = 32; col > 0; col--)
{
say("\x1b[1D"); // left
}
say("\x1b[2B"); // down
say("\u001b[37m"); // white text
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("s");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("d");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("g");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("h");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("j");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say("l");
say("\u001b[47m"); // white background
say(" ");
say("\u001b[40m"); // black background
say(";");
say("\u001b[47m"); // white background
say(" ");
for (int col = 17; col > 0; col--)
{
say("\x1b[1D"); // left
}
say("\x1b[1B"); // down
say("\u001b[30m"); // black text
say("\u001b[47m"); // white background
say("z");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("x");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("cv");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("b");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("n");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("m,");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say(".");
say("\u001b[40m"); // black background
say(" ");
say("\u001b[47m"); // white background
say("/");
say("\u001b[0m"); // reset colors
say("\x1b[0;0H"); // move to 0,0
}
void terminit(void) {
struct termios t;
tcgetattr(1, &t);
initial = t;
t.c_lflag &= (~ECHO & ~ICANON);
tcsetattr(1, TCSANOW, &t);
//enter the alternate buffer
say("\x1b[?1049h");
std::signal(SIGWINCH, resize);
resize(0);
//hide the cursor
say("\x1b[?25l");
//register our cleanup function
std::atexit(cleanup);
std::signal(SIGTERM, cleanup_die);
std::signal(SIGINT, cleanup_die);
}
void UIThread::run() {
terminit();
const int EGOCOLS = 16;
const int EGOLINES = 16;
// say("Press any key to play a sound, then press ESC to quit.");
std::map<char, uint8_t> keymap;
const char firstRow[] = "zsxdcvgbhnjm,l.;/";
for (uint8_t i=0; i<strlen(firstRow); ++i) if (firstRow[i] != ' ') keymap[firstRow[i]] = i+48;
const char secondRow[] = "q2w3er5t6y7ui9o0p[=]";
for (uint8_t i=0; i<strlen(secondRow); ++i) if (secondRow[i] != ' ') keymap[secondRow[i]] = i+60;
char next='\0', prev;
while (next != '\x1b') {
prev = next;
read(1, &next, 1);
if (keymap.count(prev) > 0)
{
auto noteOff = juce::MidiMessage::noteOff(1, keymap[prev], 0.f).getRawData();
patch->injectMIDIMessage(noteOff[0], noteOff[1], noteOff[2]);
}
if (keymap.count(next) > 0)
{
auto noteOn = juce::MidiMessage::noteOn(1, keymap[next], 1.0f).getRawData();
patch->injectMIDIMessage(noteOn[0], noteOn[1], noteOn[2]);
}
}
juce::JUCEApplication::getInstance()->systemRequestedQuit();
}