-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.cc
120 lines (110 loc) · 3.95 KB
/
term.cc
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
#include "term.h"
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include "jackengine.h"
using namespace awb;
using namespace std;
Term::Term(JackEngine &jackEngine) : jackEngine(jackEngine), mode(KEY_CMD) {
// Switch to "raw" mode.
struct termios mode;
tcgetattr(/*file_descriptor*/ 0, &mode);
mode.c_iflag = 0;
mode.c_oflag &= ~OPOST;
mode.c_lflag &= ~(ISIG | ICANON | ECHO);
tcsetattr(/*file_descriptor*/ 0, TCSADRAIN, &mode);
}
Term::~Term() {
// Restore cooked mode.
struct termios mode;
tcgetattr(/*file_descriptor*/ 0, &mode);
mode.c_iflag = BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;;
mode.c_oflag |= OPOST;
mode.c_lflag |= ISIG | ICANON | ECHO;
tcsetattr(/*file_descriptor*/ 0, TCSADRAIN, &mode);
}
void Term::handleRead(spug::Reactor &reactor) {
char buffer[1024];
int amtRead = read(0, buffer, sizeof(buffer));
for (int i = 0; i < amtRead; ++i) {
char ch = buffer[i];
if (mode == KEY_CMD) {
if (ch >= '0' && ch <= '9') {
int curRecordChannel = jackEngine.getRecordChannel();
if (curRecordChannel == -1) {
jackEngine.startRecord(ch - '0');
cerr << "Recording on channel " <<
jackEngine.getRecordChannel() << "\r" << endl;
} else {
cerr << "Finished recording on channel " << curRecordChannel <<
"\r" << endl;
jackEngine.endRecord();
}
} else if (ch == ' ') {
if (jackEngine.isPlaying())
jackEngine.endPlay();
else
jackEngine.startPlay();
} else if (ch == 'K') {
jackEngine.clear();
cerr << "\033[31;43mDeleted\033[0m\r\n\033[K" << flush;
} else if (ch == 'q') {
throw Quit();
} else if (ch == 's') {
if (jackEngine.isPlaying())
jackEngine.endPlay();
cerr << "\nsave file: " << flush;
lastCmd = CMD_STORE;
mode = LINE_READ;
} else if (ch == 'l') {
if (jackEngine.isPlaying())
jackEngine.endPlay();
cerr << "\nload file: " << flush;
lastCmd = CMD_LOAD;
mode = LINE_READ;
} else if (ch == ',') {
jackEngine.startPrevSection();
} else if (ch == '.') {
jackEngine.startNextSection();
} else if (ch == 'n') {
jackEngine.startNewSection();
}
} else {
// backspace.
if (ch == 7 || ch == 127) {
if (lineBuf.size()) {
cerr << "\b \b" << flush;
lineBuf = lineBuf.substr(0, lineBuf.size() - 1);
}
} else if (ch == '\r') {
switch (lastCmd) {
// Processs whatever command we have waiting.
case CMD_LOAD: {
ifstream src(lineBuf);
jackEngine.load(src);
cerr << "\r\nloaded file " << lineBuf << "\r" << endl;
lineBuf = "";
break;
}
case CMD_STORE: {
ofstream dst(lineBuf);
jackEngine.store(dst);
cerr << "\r\nsaved file " << lineBuf << "\r" << endl;
lineBuf = "";
break;
}
default:
cerr << "Internal error: Unkwnown command.\r" << endl;
}
mode = KEY_CMD;
} else {
lineBuf = lineBuf + ch;
cerr << ch << flush;
}
}
}
}
bool Term::isTTY() {
return isatty(0);
}