summaryrefslogtreecommitdiff
path: root/synth/in_synth_cli.cpp
blob: a21e1f778275d979914a555edcd263be826d823d (plain)
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
#include <iostream>
#include <string>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

#include "in_synth_cli.h"
#include "util.h"
#include "communication.h"
#include "globals.h"
#include "load.h"

using namespace std;

#define PROMPT "> "

void signal_handler(int sig)
{
	cout << endl << PROMPT << flush;
}

void do_request(int prg_no, bool susp)
{
	pthread_mutex_lock(&suspend_request_mutex);
	
	suspend_request.prog=prg_no;
	suspend_request.suspend=susp;
	suspend_request.done=false;
	
	pthread_mutex_unlock(&suspend_request_mutex);
	
	
	
	while (true)
	{
		usleep(100000);

		pthread_mutex_lock(&suspend_request_mutex);
		if (suspend_request.done)
		{
			pthread_mutex_unlock(&suspend_request_mutex);
			break;
		}
		else
			pthread_mutex_unlock(&suspend_request_mutex);
	}
}

void lock_and_load_program(int prg_no, string file)
{
	do_request(prg_no, true);
	
	if (load_program(file,program_settings[prg_no]))
	{
		cout << "success" << endl;
		programfile[prg_no]=file;
	}
	else
		cout << "failed" << endl;
	
	for (int i=0;i<N_CHANNELS;i++)
		channel[i]->maybe_reload_program(prg_no);
	
	do_request(prg_no, false);
}

void do_in_synth_cli()
{
	string input;
	string command;
	string params;
	int num;
	
	if (signal(2,signal_handler)==SIG_ERR)
		output_warning("WARNING: failed to set signal handler in the in-synth-cli. pressing enter will\n"
		               "         kill the synth, so be careful. this is not fatal");


	while (true)
	{
		cout << PROMPT << flush;
		getline(cin,input);
		input=trim_spaces(input);
		
		command=trim_spaces(str_before(input,' ',input));
		params=trim_spaces(str_after(input,' ',""));
		
		if ((command=="exit") || (command=="quit"))
			break;
		else if (command=="reload")
		{
			if ((!isnum(params)) || (params==""))
				cout << "error: expected program-number, found '"<<params<<"'"<<endl;
			else
			{
				num=atoi(params.c_str());
				lock_and_load_program(num, programfile[num]);
			}
		}
		else if (command=="load")
		{
			string prgstr, file;
			prgstr=trim_spaces(str_before(params,' ',params));
			file=trim_spaces(str_after(params,' ',""));
			
			if ((!isnum(prgstr)) || (prgstr==""))
				cout << "error: expected program-number, found '"<<prgstr<<"'"<<endl;
			else if (file=="")
				cout << "error: expected program-file to load, found nothing"<<endl;
			else
			{
				num=atoi(params.c_str());
				lock_and_load_program(num, file);
				
				programfile[num]=file;
			}
		}
		else if (command!="")
		{
			cout << "error: unrecognized command '"<<command<<"'"<<endl;
		}
	}
}