-
Notifications
You must be signed in to change notification settings - Fork 1
/
anja.cpp
225 lines (205 loc) · 6.34 KB
/
anja.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
//@ {
//@ "targets":
//@ [{"name":"anja","type":"application","include_targets":["projectinfo.hpp"]}]
//@ }
#include "ui/application.hpp"
#include "cmdtypes.hpp"
#include "common/filein.hpp"
#include "common/fileout.hpp"
#include <cstdio>
ALICE_OPTION_DESCRIPTOR(OptionDescriptor
,{"Program information","about","prints an about message to *filename out*. "
"Without argument, the data is written to standard outout.","filename out"
,Alice::Option::Multiplicity::ZERO_OR_ONE}
,{"Program information","help","prints all availible command line options to "
"*filename out*. Without argument, the data is written to standard outout."
,"filename out",Alice::Option::Multiplicity::ZERO_OR_ONE}
,{"Program information","version","prints version information to *filename out*. "
"Without argument, the data is written to standard outout.","filename out"
,Alice::Option::Multiplicity::ZERO_OR_ONE}
,{"Appearance","theme","selects the UI theme. The default theme is `dark`. "
"Notice that theme modes does not work on all GTK+3 themes. In this case, "
"this option has no effect.","theme",Alice::Option::Multiplicity::ONE}
,{"Appearance","window-mode","selects the window mode. The default mode is "
"`windowed`.","window mode",Alice::Option::Multiplicity::ONE}
,{"Session loading or control","session","loads a saved session from "
"*filename in*. Notice that this option is ignored if `script` is given."
,"filename in",Alice::Option::Multiplicity::ONE}
,{"Session loading or control","script","reads and executes commands from "
"*filename in*. Without argument, the session is read from standard input. "
"The command stream works independently of the UI. Notice that this option "
"overrides the `session` option.","filename in",Alice::Option::Multiplicity::ZERO_OR_ONE}
,{"Session loading or control","offline","determines whether or not Anja "
"should be started in offline mode. By default, Anja tries to connect to "
"the JACK server determined by the JACK_DEFAULT_SERVER environment variable. "
"This option can be used to override that behaviour.","bool",Alice::Option::Multiplicity::ONE});
static void commands_read(Anja::FileIn& src,Anja::Application& dest)
{
Anja::String buffer;
Anja::ArrayDynamicShort<decltype(buffer)> cmd;
enum class State:int{NORMAL,ESCAPE};
auto state_current=State::NORMAL;
auto fptr=src.get();
while(1)
{
auto ch_in=getc(fptr);
switch(state_current)
{
case State::NORMAL:
switch(ch_in)
{
case EOF:
cmd.append(buffer);
dest.invoke(std::move(cmd));
return;
case '\n':
{
cmd.append(buffer);
buffer.clear();
auto do_exit=0;
if(cmd[0]=="exit")
{do_exit=1;}
dest.invoke(std::move(cmd));
if(do_exit)
{return;}
cmd.clear();
}
break;
case '\\':
state_current=State::ESCAPE;
break;
case ',':
cmd.append(buffer);
buffer.clear();
break;
default:
buffer.append(ch_in);
break;
}
break;
case State::ESCAPE:
if(ch_in==EOF)
{
cmd.append(buffer);
dest.invoke(std::move(cmd));
return;
}
buffer.append(ch_in);
state_current=State::NORMAL;
break;
}
}
}
namespace
{
class CmdReader
{
public:
CmdReader(const char* filename,Anja::Application& dest):
m_src(filename==nullptr?"":filename),r_dest(dest)
,m_read_thread(*this,std::integral_constant<int,0>{})
{}
template<int id>
void run()
{
try
{
Anja::FileIn src(m_src.length()?m_src.begin():nullptr);
commands_read(src,r_dest);
}
catch(const Anja::Error& err)
{
fprintf(stderr,"Error: %s\n",err.message());
return;
}
}
private:
Anja::String m_src;
Anja::Application& r_dest;
Anja::Thread m_read_thread;
};
}
static CmdReader* cmdreader_create(const std::vector<std::string>& filename
,Anja::Application& app)
{
if(filename.size())
{return new CmdReader(filename[0].c_str(),app);}
return new CmdReader(nullptr,app);
}
int main(int argc, char **argv)
{
try
{
Alice::CommandLine<OptionDescriptor> cmdline(argc,argv);
if(cmdline.get<Alice::Stringkey("help")>())
{
auto& val=cmdline.get<Alice::Stringkey("help")>().valueGet();
Anja::FileOut output(val.size()?val[0].c_str():nullptr);
cmdline.help(1,output.get());
return 0;
}
if(cmdline.get<Alice::Stringkey("version")>())
{
auto& val=cmdline.get<Alice::Stringkey("version")>().valueGet();
Anja::FileOut output(val.size()?val[0].c_str():nullptr);
fprintf(output.get(),"%s version %s\n"
"This anja was compiled on %s"
,Anja::ProjectInfo::name()
,Anja::ProjectInfo::revision()
,Anja::ProjectInfo::compilationDate());
return 0;
}
if(cmdline.get<Alice::Stringkey("about")>())
{
auto& val=cmdline.get<Alice::Stringkey("about")>().valueGet();
Anja::FileOut output(val.size()?val[0].c_str():nullptr);
fprintf(output.get(),"%s version %s\n"
"This anja was compiled on %s\n\n"
"%s\n\n"
"%s\n"
,Anja::ProjectInfo::name()
,Anja::ProjectInfo::revision()
,Anja::ProjectInfo::compilationDate()
,Anja::ProjectInfo::copyright()
,Anja::ProjectInfo::legalBrief());
return 0;
}
auto offline=false;
if(cmdline.get<Alice::Stringkey("offline")>())
{offline=cmdline.get<Alice::Stringkey("offline")>().valueGet();}
Anja::Application anja;
if(!offline)
{anja.engineStart();}
if(cmdline.get<Alice::Stringkey("theme")>())
{
auto& val=cmdline.get<Alice::Stringkey("theme")>();
anja.dark(val.valueGet()==Alice::Theme::DARK);
}
else
{anja.dark(1);}
if(cmdline.get<Alice::Stringkey("window-mode")>())
{
auto& val=cmdline.get<Alice::Stringkey("window-mode")>();
anja.fullscreen(val.valueGet()==Alice::WindowMode::FULLSCREEN);
}
else
{anja.fullscreen(0);}
if(cmdline.get<Alice::Stringkey("session")>() && !cmdline.get<Alice::Stringkey("script")>())
{anja.sessionLoad(cmdline.get<Alice::Stringkey("session")>().valueGet().c_str());}
std::unique_ptr<CmdReader> cmdreader(cmdline.get<Alice::Stringkey("script")>()?
cmdreader_create(cmdline.get<Alice::Stringkey("script")>().valueGet(),anja)
:nullptr);
anja.run();
}
catch(const Anja::Error& err)
{
fprintf(stderr,"Error: %s\n",err.message());
return -1;
}
catch(const Alice::ErrorMessage& msg)
{
fprintf(stderr,"Error: %s\n",msg.data);
return -1;
}
return 0;
}