-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
577 lines (506 loc) · 16.2 KB
/
main.cpp
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* This file is part of oscprompt - a curses OSC frontend
* Copyright Mark McCurry 2014
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rtosc/rtosc.h>
#include <rtosc/ports.h>
#include <rtosc/thread-link.h>
#include <lo/lo.h>
#include <ncurses.h>
#include <string.h>
#include <unistd.h>
#include <cstdlib>
#include <cctype>
#include <string>
#include <sstream>
#include "render.h"
using namespace rtosc;
using std::string;
//History
char history_buffer[32][1024];
int history_pos;
const int history_size = 32;
//Tab completion recommendation
string tab_recommendation;
//Error detected in user promppt
int error = 0;
//Liblo destination
lo_address lo_addr;
//Base path which can be used via 'cd' command
string root_path;
//Ignore one message from client
int dummy_update;
/**
* Parses simple messages from strings into something that librtosc can accept
*
* This function assumes that message_buffer contains a valid string and that
* message_arguments is populated as a side effect of the pretty printer
*/
void send_message(void)
{
string tmp = root_path + message_buffer;
const char *str = tmp.c_str();
char path[1024];
//Load message path
{
char *p = path;
while(*str && *str != ' ')
*p++ = *str++;
*p = 0;
}
//Load arguments
rtosc_arg_t *args = new rtosc_arg_t[message_narguments];
//String buffer
char buf[1024];
char *b = buf;
char* endptr;
locale_t locale = newlocale(LC_ALL, "en_US", NULL);
for(int i=0; i<message_narguments; ++i) {
switch(message_arguments[i]) {
case 'i':
case 'f':
while(!isdigit(*str) && *str!='-' && *str!='.') ++str;
if(message_arguments[i] == 'i')
args[i].i = atoi(str);
else
args[i].f = strtod_l(str, &endptr, locale);
while(isdigit(*str) || *str=='-' || *str=='.') ++str;
break;
case 'c':
while(*str++ != 'c');
args[i].i = atoi(str);
while(*str && *str != ' ') ++str;
break;
case 's':
while(*str!='"') ++str;
args[i].s = b;
++str;
while(*str!='"') *b++ = *str++;
str++;
*b++ = 0;
break;
case 'T':
case 'F':
while(*str != 'T' && *str != 'F') ++str;
++str;
}
}
char buffer[2048];
size_t len = rtosc_amessage(buffer, 2048, path, message_arguments, args);
lo_message msg = lo_message_deserialise(buffer, len, NULL);
if(lo_addr)
lo_send_message(lo_addr, buffer, msg);
delete [] args;
}
int do_exit = 0;
void die_nicely(msg_t, void*)
{
do_exit = 1;
}
void update_paths(msg_t m, void*)
{
unsigned fields = rtosc_narguments(m)/2;
werase(status);
if(fields)
tab_recommendation = rtosc_argument(m,0).s;
else {
tab_recommendation = "";
wprintw(status, "No matching ports...\n");
}
//Gather information for the long format of the status field
char *tmp = rindex(message_buffer, '/');
if(fields == 1 && tmp) {
status_name = rtosc_argument(m, 0).s;
*tmp = 0;
string new_status = root_path + string(message_buffer) + "/" + status_name;
if(new_status != status_url)
dummy_update = 1; //permit one ignored message
status_url = new_status;
*tmp = '/';
auto trim = status_url.find(':');
if(trim != string::npos)
status_url.erase(trim);
assert(rtosc_type(m, 1) == 'b');
auto blob = rtosc_argument(m, 1).b;
delete[] status_metadata;
status_metadata = new char[blob.len];
memcpy(status_metadata, blob.data, blob.len);
//Request the value of the field when possible
auto meta = rtosc::Port::MetaContainer(status_metadata);
if(meta.find("parameter") != meta.end())
lo_send(lo_addr, status_url.c_str(), "");
} else {
status_name = "";
status_url = "";
status_value = "";
}
for(unsigned i=0; i<fields; ++i)
emit_status_field(rtosc_argument(m,2*i).s,
(const char*)rtosc_argument(m,2*i+1).b.data,
fields==1 ? LONG : SHORT);
wrefresh(status);
}
void rebuild_status(void)
{
//Base port level from synth code
string tmpstr = (root_path.empty() ? root_path : root_path.substr(1));
if(tmpstr.empty()) {
if(strstr(message_buffer, "cd ") == message_buffer)
tmpstr = string(message_buffer+3);
else
tmpstr = strlen(message_buffer) ? message_buffer+1 : "";
} else {
if(strstr(message_buffer, "cd ") == message_buffer)
tmpstr += '/' + string(message_buffer+3);
else
tmpstr += message_buffer;
}
if(tmpstr[0] == '/')
tmpstr = tmpstr.substr(1);
const char *src = tmpstr.c_str();
if(index(src, ' ')) return; //avoid complete strings
//Trim the string to its last subpath.
//This allows manual filtering of the results
char *s1 = strdup(src);
char *s2 = strdup(src);
const char *path = NULL;
const char *needle = NULL;
char *tmp = rindex(s2, '/');
if(!tmp) {
needle = s2;
path = "";
tmp = s2;
while(*tmp++)
if(isdigit(*tmp))
*tmp=0;
} else {
needle = tmp + 1;
//eliminate digits
while(*tmp++)
if(isdigit(*tmp))
*tmp=0;
//terminate string
rindex(s1, '/')[1] = 0;
path = s1;
}
char buffer[2048];
size_t len = rtosc_message(buffer, 2048, "/path-search", "ss", path, needle);
lo_message msg = lo_message_deserialise(buffer, len, NULL);
if(lo_addr)
lo_send_message(lo_addr, buffer, msg);
free(s1);
free(s2);
}
void tab_complete(void)
{
if(tab_recommendation.empty())
return;
const char *src = tab_recommendation.c_str();
char *w_ptr = rindex(message_buffer,'/');
if(w_ptr)
++w_ptr;
else {
//change dir messages
if(strstr(message_buffer, "cd ")) {
if(root_path.empty()) {
w_ptr = message_buffer+4;
message_buffer[3] = '/';
} else {
w_ptr = message_buffer+3;
}
} else {
w_ptr = message_buffer+1;
message_buffer[0] = '/';
}
}
while(*src && *src != '#' && *src != ':')
*w_ptr++ = *src++;
if(*w_ptr != '\0' && *src == '#')
strcat(message_buffer, "/");
else
*w_ptr = '\0';
message_pos = strlen(message_buffer);
}
void error_cb(int i, const char *m, const char *loc)
{
wprintw(log, "liblo :-( %d-%s@%s\n",i,m,loc);
}
template<class T>
string toString(const T &t)
{
std::stringstream converter;
converter << t;
return converter.str();
}
int all_strings(const char *args)
{
while(args && *args) {
if(*args != 's')
break;
args++;
}
return *args==0;
}
void update_status_info(const char *buffer)
{
const char *args = rtosc_argument_string(buffer);
if(!strcmp(args , "f")) {
status_value = toString(rtosc_argument(buffer,0).f);
status_type = 'f';
} else if(!strcmp(args, "i")) {
status_value = toString(rtosc_argument(buffer,0).i);
status_type = 'i';
} else if(!strcmp(args, "c")) {
status_value = toString((int)rtosc_argument(buffer,0).i);
status_type = 'c';
} else if(!strcmp(args, "T")) {
status_value = "T";
status_type = 'T';
} else if(!strcmp(args, "F")) {
status_value = "F";
status_type = 'T';
} else if(all_strings(args)) {
status_type = 's';
status_value = rtosc_argument(buffer,0).s;
for(int i=1; i<(int)rtosc_narguments(buffer); ++i) {
status_value += "\n";
status_value += rtosc_argument(buffer,i).s;
}
} else
status_type = 0;
werase(status);
emit_status_field(status_name.c_str(), status_metadata, LONG);
wrefresh(status);
}
int handler_function(const char *path, const char *, lo_arg **, int, lo_message msg, void *)
{
static char buffer[1024*20];
memset(buffer, 0, sizeof(buffer));
size_t size = sizeof(buffer);
lo_message_serialise(msg, path, buffer, &size);
if(!strcmp("/paths", buffer)) // /paths:sbsbsbsbsb...
update_paths(buffer, NULL);
else if(!strcmp("/exit", buffer))
die_nicely(buffer, NULL);
else if(status_url == path) {
update_status_info(buffer);
if(!dummy_update)
display(buffer, NULL);
dummy_update = 0;
} else if(!strcmp("undo_change", buffer))
;//ignore undo messages
else
display(buffer, NULL);
return 0;
}
void change_dir(string dir)
{
if(dir.length() != 1 && dir[dir.length()-1] == '/')
dir = dir.substr(0, dir.length()-1);
if(dir[0] == '/')
root_path = dir;
else if(root_path.empty() && dir[0] == '/')
root_path = dir;
else if(root_path.empty())
root_path = '/' + dir;
else if(dir[0] != '.')
root_path += '/' + dir;
else if(dir == "..") {
size_t itr = root_path.rfind('/');
if(itr != root_path.npos)
root_path = root_path.substr(0,itr);
}
}
void process_message(void)
{
//alias
const char *m = message_buffer;
//Check for special cases
if(strstr(m, "disconnect")==m) {
if(lo_addr) {
lo_address_free(lo_addr);
lo_addr = NULL;
}
}
else if(strstr(m, "quit")==m)
do_exit = true;
else if(strstr(m, "exit")==m)
do_exit = true;
else if(strstr(m, "connect")==m) {
while(*m && !isdigit(*m)) ++m;
if(isdigit(*m)) { //lets hope lo is robust :p
if(lo_addr)
lo_address_free(lo_addr);
lo_addr = lo_address_new(NULL, m);
//populate fields
if(lo_addr)
lo_send(lo_addr, "/path-search", "ss", "", "");
}
} else if(strstr(m, "help")==m) {
wprintw(log, "\nWelcome to oscprompt...\n");
wprintw(log, "To start talking to an app please run\nconnect PORT\n");
wprintw(log, "From here you can enter in osc paths with arguments to send messages. ");
wprintw(log, "Assuming that everything has connected properly you should see some metadata ");
wprintw(log, "telling you about the ports...\n");
} else if(strstr(m, "cd")==m && strlen(m) >= 4) {
change_dir(&m[3]);
} else if(*m != '/') {
wprintw(status, "Unrecognized command...\n");
} else { //normal OSC message
if(error)
wprintw(status,"bad message...\n");
else
send_message();
}
//History Buffer
for(unsigned i=sizeof(history_buffer)/sizeof(history_buffer[0])-1; i>0; --i)
memcpy(history_buffer[i], history_buffer[i-1], sizeof(history_buffer[0]));
memcpy(history_buffer[1], message_buffer, sizeof(message_buffer));
history_pos = 0;
//Reset message buffer
memset(message_buffer, 0, 1024);
memset(message_arguments, 0, 32);
message_narguments = 0;
message_pos = 0;
}
template<class T>
T min(T a, T b) { return b<a?b:a; }
template<class T>
T max(T a, T b) { return b<a?a:b; }
void try_param_add(int size)
{
if(status_url.empty() || status_value.empty() || status_type == 0)
return;
if(status_type == 'c') {
int x = atoi(status_value.c_str());
if(x != 127)
lo_send(lo_addr, status_url.c_str(), "c", min(127,x+size));
}
}
void try_param_sub(int size)
{
if(status_url.empty() || status_value.empty() || status_type == 0)
return;
if(status_type == 'c') {
int x = atoi(status_value.c_str());
if(x != 0)
lo_send(lo_addr, status_url.c_str(), "c", max(0,x-size));
}
}
int main()
{
//For misc utf8 chars
setlocale(LC_ALL, "");
memset(message_buffer, 0,sizeof(message_buffer));
memset(history_buffer, 0,sizeof(history_buffer));
memset(message_arguments,0,sizeof(message_arguments));
history_pos = 0;
int ch;
render_setup();
//setup liblo - it can choose its own port
lo_server server = lo_server_new_with_proto(NULL, LO_UDP, error_cb);
lo_server_add_method(server, NULL, NULL, handler_function, NULL);
//lo_addr = lo_address_new_with_proto(LO_UDP, NULL, "8080");
wprintw(log, "lo server running on %d\n", lo_server_get_port(server));
//Loop on prompt until the program should exit
do {
//Redraw prompt
wclrtoeol(prompt);
wprintw(prompt,":%s> ", root_path.c_str());
error = print_colorized_message(prompt);
wprintw(prompt,"\r");
wrefresh(prompt);
//box(helper_box,0,0);
//wrefresh(helper_box);
wrefresh(log);
wrefresh(status);
//Handle events from backend
lo_server_recv_noblock(server, 0);
//FILE *file;
switch(ch = wgetch(prompt)) {
case '\\':
try_param_add(1);
break;
case ']':
try_param_sub(1);
break;
case '|':
try_param_add(10);
break;
case '}':
try_param_sub(10);
break;
case KEY_BACKSPACE:
case '':
case 127: //ascii 'DEL'
if(message_pos)
message_buffer[--message_pos] = 0;
rebuild_status();
break;
case 91: //escape
ch = wgetch(prompt);
if(ch == 'A') {//up
history_pos = (history_pos+1)%history_size;
memcpy(message_buffer, history_buffer[history_pos], sizeof(message_buffer));
message_pos = strlen(message_buffer);
} else if(ch == 'B') {//down
history_pos = (history_pos-1+history_size)%history_size;
memcpy(message_buffer, history_buffer[history_pos], sizeof(message_buffer));
message_pos = strlen(message_buffer);
}
break;
case '\t':
tab_complete();
rebuild_status();
break;
case '\n':
case '\r':
process_message();
break;
case 3: //Control-C
do_exit = 1;
break;
case KEY_RESIZE:
log = newwin(LINES-3, COLS/2-3, 1, 1);
status = newwin(LINES-3, COLS/2-3, 1, COLS/2+1);
prompt = newwin(1, COLS, LINES-1,0);
scrollok(log, TRUE);
idlok(log, TRUE);
wtimeout(prompt, 10);
{
WINDOW *helper_box = newwin(LINES-1,COLS/2-1,0,0);
box(helper_box,0,0);
wrefresh(helper_box);
}
{
WINDOW *helper_box = newwin(LINES-1,COLS/2,0,COLS/2);
box(helper_box,0,0);
wrefresh(helper_box);
}
break;
default:
if(ch > 0 && isprint(ch)) {
message_buffer[message_pos++] = ch;
rebuild_status();
} else {
usleep(100);
}
}
} while(!do_exit);
lo_server_free(server);
delete[] status_metadata;
endwin();
return 0;
}