-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.cpp
362 lines (310 loc) · 9.22 KB
/
monitor.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
/**
* @file monitor.cpp
* @brief Brief description of file.
*
*/
#include <sstream>
#include <ncurses.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include "ringbuffer.h"
#include "exception.h"
#include "save.h"
#include "process.h"
#include "monitor.h"
#include "screen.h"
#include "version.h"
#include "colours.h"
#include "ctrl.h"
#include "screenctrl.h"
class Screen *curscreen=NULL; // the current screen. LOCK IT.
static InputRequest req;
MonitorThread *MonitorThread::instance =NULL;
InputManager *InputManager::instance = NULL;
/// primary mutex
static pthread_mutex_t mutex;
/// condition used to block the main thread while we get input
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static pthread_t thread;
static void *_threadfunc(void *p){
((MonitorThread *)p)->threadfunc();
return NULL;
}
// yes, these are all the same mutex
void MonitorThread::lock(){
pthread_mutex_lock(&mutex);
}
void MonitorThread::unlock(){
pthread_mutex_unlock(&mutex);
}
void InputManager::lock(){
pthread_mutex_lock(&mutex);
}
void InputManager::unlock(){
pthread_mutex_unlock(&mutex);
}
MonitorThread::MonitorThread(){
if(instance)
throw _("a monitor thread is already running.");
running=true;requestStop=false;
// init the mutex as recursive
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex,&attr);
// start the thread
pthread_create(&thread,NULL,_threadfunc,this);
instance = this;
}
MonitorThread::~MonitorThread(){
lock();
requestStop=true;
unlock();
for(;;){
usleep(10000);
lock();
if(!running)break;
unlock();
}
instance = NULL;
unlock();
}
void MonitorThread::threadfunc(){
// initialise curses
initscr();
keypad(stdscr,TRUE);
noecho();
cbreak();
nl();
curs_set(0);
timeout(0); // nonblocking read
// display initial status line
stringstream ss;
ss << "Jackmix Ready [" << VERSION << "]";
setStatus(ss.str(),10);
// set colour pairs
start_color();
// if(can_change_color())
// setStatus("Color!",10);
init_pair(PAIR_HILIGHT,COLOR_YELLOW,COLOR_BLACK);
init_pair(PAIR_GREEN,COLOR_GREEN,COLOR_GREEN);
init_pair(PAIR_YELLOW,COLOR_YELLOW,COLOR_YELLOW);
init_pair(PAIR_RED,COLOR_RED,COLOR_RED);
init_pair(PAIR_DARK,COLOR_BLUE,COLOR_BLUE);
init_pair(PAIR_REDTEXT,COLOR_RED,COLOR_BLACK);
init_pair(PAIR_BLUETEXT,COLOR_BLUE,COLOR_BLACK);
loop(); // DO THE MAIN LOOP
// shutdown curses
endwin();
printf("Terminated\n");
running=false;
unlock();
}
void MonitorThread::loop(){
// main loop
while(1){
// send commands from the UI thread to the process thread
Process::sendCmds();
Screen *sc;
getmaxyx(stdscr,h,w);
lock();
sc = curscreen;
if(requestStop)break;
// handle input requests
if(!req.running && req.t!=InputReqIdle){
// start a new one
switch(req.t){
case InputReqLineEdit:
case InputReqEditVal:
lineEdit.begin(req.prompt);
break;
case InputReqStringList:
stringList.begin(req.prompt,req.list);
break;
default:break;
}
req.running=true;
}
unlock();
// handle actual input
int k = getch();
if(req.running && k!=ERR){
EditState newst;
switch(req.t){
case InputReqKey:
if(req.validKeys.size()>0 && req.validKeys.find((char)k)==string::npos)
break; // abort if not a valid key
lock();
req.intout=k;
req.setDone();
// signal the other thread
unlock();
pthread_cond_signal(&cond);
break;
case InputReqLineEdit:
newst=lineEdit.handleKey(k);
if(newst!=Running){
lock();
req.aborted = newst==Aborted;
req.strout = lineEdit.consume();
req.setDone();
// signal the other thread
unlock();
pthread_cond_signal(&cond);
}
break;
case InputReqEditVal:
newst=lineEdit.handleKey(k);
if(newst!=Running){
lock();
req.aborted = newst==Aborted;
req.strout = lineEdit.consume();
req.value->setTarget(atof(req.strout.c_str()));
req.setDone();
// signal the other thread
unlock();
pthread_cond_signal(&cond);
}
break;
case InputReqStringList:
newst=stringList.handleKey(k);
if(newst!=Running){
lock();
req.aborted = newst==Aborted;
req.strout = stringList.consume();
req.setDone();
// signal the other thread
unlock();
pthread_cond_signal(&cond);
}
break;
default:break;
}
}
// fetch any data and display it using the current screen
static MonitorData mdat;
static unsigned int monpackct=0;
if(Process::pollMonRing(&mdat)){
// we only erase sometimes, because it's only sometimes that data appears here
erase();
sc->display(&mdat);
monpackct++;
}
// copy into a buffer for IM to look at (it should lock too)
lock();
lastReceived=mdat;
unlock();
// DEBUGGING - shows a running counter so we can check we're not stalled
static unsigned int ct=0;
mvprintw(h-1,w-17,"%08u %08u",ct++,monpackct);
extern unsigned long diamondMsgCt;
mvprintw(h-2,w-17,"%08u",diamondMsgCt);
// do the display, first the screen
// display line edit if any, then string list if any, then keyprompt if any,
// otherwise status
if(lineEdit.getState() == Running)
lineEdit.display(h-1,0);
else if(stringList.getState()==Running)
stringList.display();
else if(req.running && req.prompt.size()!=0){
attrset(COLOR_PAIR(0)|A_BOLD);
mvaddstr(h-1,0,req.prompt.c_str());
attrset(COLOR_PAIR(0));
} else
// else the status msg.
displayStatus();
refresh();
usleep(10000);
// and poll things - MIDI, diamond etc.
extern void poll();
poll();
}
}
void MonitorThread::setStatus(string s,double t){
statusTimeToEnd=Time()+Time(t);
statusMsg = s;
statusShowing=true;
}
void MonitorThread::displayStatus(){
if(statusShowing){
Time now;
if(now>statusTimeToEnd){
statusShowing=false;
} else {
mvaddstr(h-1,0,statusMsg.c_str());
}
}
}
void InputManager::flow(){
go(&scrMain);
for(;;){
try{
curscreen->flow(this);
if(!curscreen)return; // the screen did go(NULL)
usleep(12000);
}catch(std::string& s){
endwin();
cout << "Fatal error: " << s << endl;
exit(1);
}
}
}
void InputManager::setStatus(string s,double t){
// this is called from the main thread, so we need
// to lock.
lock();
MonitorThread::get()->setStatus(s,t);
unlock();
}
string InputManager::getString(string p,bool *aborted){
lock();
// fill in the request
req.startRequest(InputReqLineEdit);
req.prompt=p;
// unlock the mutex and wait for the condition
pthread_cond_wait(&cond,&mutex);
///... some time later... the mutex will be locked
*aborted = req.aborted;
string rv = req.strout;
unlock();
return rv;
}
int InputManager::getKey(const char *p,const char *k){
lock();
req.startRequest(InputReqKey);
req.prompt = p?p:"";
if(k){
stringstream ss;
ss << req.prompt << " [" << k << "]";
req.prompt = ss.str();
}
req.validKeys = k?k:"";
pthread_cond_wait(&cond,&mutex);
int rv = req.intout;
unlock();
return rv;
}
string InputManager::getFromList(string p,vector<string>& l,bool *aborted){
lock();
req.startRequest(InputReqStringList);
req.prompt=p;
req.list=l;
pthread_cond_wait(&cond,&mutex);
*aborted = req.aborted;
string rv = req.strout;
unlock();
return rv;
}
void InputManager::editVal(string name,Value *v){
lock();
req.startRequest(InputReqEditVal);
stringstream ss;
ss << name << " [" << (v->mn) << ":" << (v->mx) << "]";
req.prompt=ss.str();
req.value = v;
pthread_cond_wait(&cond,&mutex);
///... some time later... the mutex will be locked
string rv = req.strout;
unlock();
}