23#ifdef ASE_WITH_CPPTRACE
24#include <cpptrace/from_current.hpp>
31#define MDEBUG(...) Ase::debug ("memory", __VA_ARGS__)
41MainAppImpl::MainAppImpl()
46static int arg_unauth_port = 0;
58print_usage (
bool help)
66 printout (
"Usage: %s [OPTIONS] [project.anklang]\n",
executable_name());
67 printout (
" --check Run integrity tests\n");
68 printout (
" --disable-randomization Test mode for deterministic tests\n");
69 printout (
" --fatal-warnings Abort on warnings and failing assertions\n");
70 printout (
" --help Print program usage and options\n");
71 printout (
" --jsbin Print Javascript IPC & binary messages\n");
72 printout (
" --jsipc Print Javascript IPC messages\n");
73 printout (
" --jsonts Print TypeScript bindings\n");
74 printout (
" --list-drivers Print PCM and MIDI drivers\n");
75 printout (
" --list-tests List all test names\n");
76 printout (
" --list-ui-tests List all TypeScript UI test function names\n");
77 printout (
" --norc Prevent loading of any rc files\n");
78 printout (
" --ui-test=test Specify TypeScript UI test(s) to run (comma-separated)\n");
79 printout (
" --play-autostart Automatically start playback of `project.anklang`\n");
80 printout (
" --rand64 Produce 64bit random numbers on stdout\n");
81 printout (
" --test[=test] Run specific test(s) (comma-separated)\n");
82 printout (
" --unauth-dev=NUM Open an unauthenticated websocket port for testing\n");
83 printout (
" --headless[=bool] Run browser in headless mode (default for --ui-test)\n");
84 printout (
" --ui <none|chromium|google-chrome|htmlgui>\n");
85 printout (
" Open GUI in web browser [htmlgui]\n");
86 printout (
" --version Print program version\n");
87 printout (
" -M mididriver Force use of <mididriver>\n");
88 printout (
" -P pcmdriver Force use of <pcmdriver>\n");
89 printout (
" -o wavfile Capture output to OPUS/FLAC/WAV file\n");
90 printout (
" -t <time> Automatically play and stop after <time> has passed\n");
91 printout (
"Options set via $ASE_DEBUG:\n");
92 printout (
" :no-logfile: Disable logging to ~/.cache/anklang/ instead of stderr\n");
97parse_option_arg (
const char *option,
char **argv,
unsigned *ith,
const char **argp)
99 const size_t l =
strlen (option);
100 if (strncmp (option, argv[*ith], l) == 0) {
101 *argp = argv[*ith] + l;
102 argv[*ith] =
nullptr;
103 if ((*argp)[0] ==
'=')
105 else if ((*argp)[0] == 0) {
107 *argp = argv[*ith] ? argv[*ith] :
"";
108 argv[*ith] =
nullptr;
116static constexpr int jsipc_logflags = 1 | 2 | 4 | 8 | 16;
117static constexpr int jsbin_logflags = 1 | 256;
119static StringS check_test_names;
123parse_args (
int *argcp,
char **argv, MainAppImpl &config)
134 const uint argc = *argcp;
135 for (
uint i = 1; i < argc; i++)
137 const char *
optarg =
nullptr;
139 config.args.push_back (argv[i]);
140 else if (strcmp (argv[i],
"--fatal-warnings") == 0 || strcmp (argv[i],
"--g-fatal-warnings") == 0)
142 else if (strcmp (
"--disable-randomization", argv[i]) == 0)
143 config.allow_randomization =
false;
144 else if (strcmp (
"--norc", argv[i]) == 0)
146 else if (strcmp (
"--rand64", argv[i]) == 0)
149 constexpr int N = 8192;
153 for (
size_t i = 0; i < N; i++)
154 buffer[i] = prng.next();
155 fwrite (buffer,
sizeof (buffer[0]), N, stdout);
159 else if (strcmp (
"--check", argv[i]) == 0)
161 config.mode = MainApp::CHECK_INTEGRITY_TESTS;
163 printerr (
"CHECK_INTEGRITY_TESTS…\n");
164 default_ui_mode =
"none";
166 else if (strcmp (
"--list-tests", argv[i]) == 0)
169 for (
const auto &t : Test::list_tests())
170 ids.push_back (t.ident);
172 for (
const auto &t : ids)
173 printout (
"%s\n", t);
176 else if (strcmp (
"--list-ui-tests", argv[i]) == 0)
180 fatal_error (
"missing UI test list: %s", testfile);
181 const String content = Path::stringread (testfile);
183 for (
const String &line : lines)
185 printout (
"%s\n", line);
188 else if (strcmp (
"--ui-test", argv[i]) == 0 || strncmp (
"--ui-test=", argv[i], 9) == 0)
190 const char *eq =
strchr (argv[i],
'=');
191 const char *arg = eq ? eq + 1 : i+1 < argc ? argv[++i] :
nullptr;
194 for (
const auto &t : tests)
195 ui_test_names.push_back (t);
198 config.headless =
true;
200 else if (strcmp (
"--headless", argv[i]) == 0 || strncmp (
"--headless=", argv[i], 10) == 0)
202 const char *eq =
strchr (argv[i],
'=');
205 else if (strcmp (
"--test", argv[i]) == 0 || strncmp (
"--test=", argv[i], 7) == 0)
207 const char *eq =
strchr (argv[i],
'=');
208 const char *arg = eq ? eq + 1 : i+1 < argc ? argv[++i] :
nullptr;
209 config.mode = MainApp::CHECK_INTEGRITY_TESTS;
213 for (
const auto &t : tests)
214 check_test_names.push_back (t);
216 default_ui_mode =
"none";
218 else if (argv[i] ==
String (
"--blake3") && i + 1 <
size_t (argc))
223 printerr (
"%s: failed to read: %s\n", argv[i], strerror (errno));
228 else if (strcmp (
"--jsonts", argv[i]) == 0) {
229 if (getenv (
"ASE_JSONTS") ==
nullptr)
230 fatal_error (
"%s: environment must contain ASE_JSONTS for --jsonts", argv[0]);
231 printout (
"%s\n", Jsonipc::g_binding_printer->finish());
233 }
else if (strcmp (
"--jsipc", argv[i]) == 0)
234 config.jsonapi_logflags |= jsipc_logflags;
235 else if (strcmp (
"--jsbin", argv[i]) == 0)
236 config.jsonapi_logflags |= jsbin_logflags;
237 else if (strcmp (
"--list-drivers", argv[i]) == 0)
238 config.list_drivers =
true;
239 else if (strcmp (
"-M", argv[i]) == 0 && i + 1 <
size_t (argc))
242 config.midi_override = argv[i];
244 else if (strcmp (
"-P", argv[i]) == 0 && i + 1 <
size_t (argc))
247 config.pcm_override = argv[i];
249 else if (strcmp (
"--no-devices", argv[i]) == 0)
251 config.no_devices =
true;
253 else if (strcmp (
"-h", argv[i]) == 0 ||
254 strcmp (
"--help", argv[i]) == 0)
259 else if (strcmp (
"--version", argv[i]) == 0)
264 else if (argv[i] ==
String (
"-o") && i + 1 <
size_t (argc))
267 config.outputfile = argv[i];
269 else if (argv[i] ==
String (
"--play-autostart"))
271 config.play_autostart =
true;
272 default_ui_mode =
"none";
274 else if (parse_option_arg (
"--unauth-dev", argv, &i, &optarg))
277 default_ui_mode =
"wait";
279 else if (argv[i] ==
String (
"-t") && i + 1 <
size_t (argc))
281 config.play_autostart =
true;
284 default_ui_mode =
"none";
286 else if (parse_option_arg (
"--ui", argv, &i, &optarg))
290 else if (argv[i] ==
String (
"--") && !sep)
292 else if (argv[i][0] ==
'-' && !sep)
293 fatal_error (
"invalid command line argument: %s", argv[i]);
295 config.args.push_back (argv[i]);
298 if (arg_ui_mode.
empty())
299 arg_ui_mode = default_ui_mode;
303 for (
uint i = 1; i < argc; i++)
317 const char *
const c52 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
324 KeccakCryptoRng csprng;
326 for (
size_t i = 0; i < 23; ++i)
327 auth += c52[csprng.random() % 52];
334 if (check_test_names.
empty())
344 LoopP loop = main_loop;
357 seen_autostop =
true;
363handle_autostop (
const LoopState &state)
367 case LoopState::PREPARE:
return seen_autostop;
368 case LoopState::CHECK:
return seen_autostop;
369 case LoopState::DISPATCH:
370 info (
"Main: stopping playback (auto)");
382 sigset_t signal_mask;
388 Ase::warning (
"Ase: pthread_sigmask for SIGPIPE failed: %s\n", strerror (errno));
397 if (!loft_needs_preallocation_mt)
399 loft_needs_preallocation_mt =
true;
404static size_t last_loft_preallocation = 0;
407preallocate_loft (
size_t preallocation)
410 last_loft_preallocation = preallocation;
413 .watermark = last_loft_preallocation / 2,
414 .flags = Loft::PREFAULT_PAGES,
416 loft_set_config (loftcfg);
417 loft_set_notifier (notify_loft_lowmem);
418 loft_grow_preallocate();
425 const bool keep_alive = lstate.phase == LoopState::DISPATCH;
429 loft_needs_preallocation_mt =
false;
430 last_loft_preallocation *= 2;
431 const size_t newalloc = loft_grow_preallocate (last_loft_preallocation);
433 loft_get_config (config);
434 config.
watermark = last_loft_preallocation / 2;
435 loft_set_config (config);
437 MDEBUG (
"Loft preallocation in main thread: %f MB", newalloc / (1024. * 1024));
442prefault_pages (
size_t stacksize,
size_t heapsize)
444 const size_t pagesize =
sysconf (_SC_PAGESIZE);
445 char *heap = (
char*) malloc (heapsize);
447 for (
size_t i = 0; i < heapsize; i += pagesize)
450 char *stack = (
char*) alloca (stacksize);
452 for (
size_t i = 0; i < stacksize; i += pagesize)
457main (
int argc,
char *argv[])
460 using namespace AnsiColors;
463 TaskRegistry::setup_ase (
"AnklangMainProc");
465 mallopt (M_MMAP_MAX, 0);
467 mallopt (M_TRIM_THRESHOLD, -1);
469 prefault_pages ((1024 + 768) * 1024, 64 * 1024 * 1024);
471 preallocate_loft (64 * 1024 * 1024);
473 loft_set_growth_notifier ([] (
size_t total,
size_t needed)
475 warning (
"Loft.BumpAllocator: growing beyond preallocation: totalmem=%u needed=%d\n", total, needed);
479 logging_handle_terminate();
483 if (setpgid (0, 0) < 0)
484 diag (
"Main: setpgid failed: %s", ::strerror (errno));
487 if (!setlocale (LC_ALL,
""))
488 fatal_error (
"setlocale: locale not supported by libc: %s", ::strerror (errno));
491 parse_args (&argc, argv, main_app);
492 main_app.ui_tests = ui_test_names;
493 const int socket_port = arg_unauth_port > 0 ? arg_unauth_port : 0;
494 const char *socket_host =
"127.0.0.1";
495 const auto socket_info = WebSocketServer::bind_port (socket_host, socket_port);
496 logging_configure (arg_ui_mode !=
"none" ?
string_format (
"%u", socket_info.port) :
"");
503 Preference::load_preferences (
true);
506 ServerImpl::instancep();
509 if (!
trkn_init (argc, argv, App.no_devices))
510 fatal_error (
"Main: failed to initialize tracktion engine");
512 const auto B1 =
color (BOLD);
513 const auto B0 =
color (BOLD_OFF);
517 if (App.list_drivers)
519 Ase::Driver::EntryVec entries;
520 printout (
"%s",
_(
"Available PCM drivers:\n"));
521 entries = Ase::PcmDriver::list_drivers();
522 std::sort (entries.begin(), entries.end(), [] (
auto &a,
auto &b) { return a.priority < b.priority; });
523 for (
const auto &entry : entries)
525 printout (
" %-30s (%s, %08x)\n\t%s\n%s%s%s%s", entry.devid +
":",
526 entry.readonly ?
"Input" : entry.writeonly ?
"Output" :
"Duplex",
527 entry.priority, entry.device_name,
528 entry.capabilities.
empty() ?
"" :
"\t" + entry.capabilities +
"\n",
529 entry.device_info.
empty() ?
"" :
"\t" + entry.device_info +
"\n",
530 entry.hints.
empty() ?
"" :
"\t(" + entry.hints +
")\n",
531 entry.notice.
empty() ?
"" :
"\t" + entry.notice +
"\n");
535 printout (
"%s",
_(
"Available MIDI drivers:\n"));
536 entries = Ase::MidiDriver::list_drivers();
537 std::sort (entries.begin(), entries.end(), [] (
auto &a,
auto &b) { return a.priority < b.priority; });
538 for (
const auto &entry : entries)
540 printout (
" %-30s (%s, %08x)\n\t%s\n%s%s%s%s", entry.devid +
":",
541 entry.readonly ?
"Input" : entry.writeonly ?
"Output" :
"Duplex",
542 entry.priority, entry.device_name,
543 entry.capabilities.
empty() ?
"" :
"\t" + entry.capabilities +
"\n",
544 entry.device_info.
empty() ?
"" :
"\t" + entry.device_info +
"\n",
545 entry.hints.
empty() ?
"" :
"\t(" + entry.hints +
")\n",
546 entry.notice.
empty() ?
"" :
"\t" + entry.notice +
"\n");
555 for (
const auto &filename : App.args)
557 preload_project = ProjectImpl::create (
Path::basename (filename));
558 Error error = Error::NO_MEMORY;
560 error = preload_project->load_project (filename);
561 diag (
"Main: load project: %s: %s", filename,
ase_error_blurb (error));
563 warning (
"%s: failed to load project: %s", filename,
ase_error_blurb (error));
567 const String auth_token = arg_unauth_port > 0 ?
"" : make_auth_string();
568 auto wss = WebSocketServer::create (jsonapi_make_connection, App.jsonapi_logflags, auth_token);
569 main_app.web_socket_server = &*wss;
572 wss->http_alias (
"/Builtin/Controller",
anklang_runpath (RPath::INSTALLDIR,
"/Controller"));
574 wss->http_alias (
"/Builtin/Scripts",
anklang_runpath (RPath::INSTALLDIR,
"/Scripts"));
575 const String subprotocol =
"";
576 jsonapi_set_subprotocol (subprotocol);
577 if (App.mode == MainApp::SYNTHENGINE && arg_ui_mode !=
"none") {
578 wss->listen (socket_info, [] () { main_loop->quit (-1); });
581 String redirecthtml = webui_create_auth_redirect (
"anklang", wss->listen_port(), auth_token, arg_ui_mode);
583 fatal_error (
"%s: failed to create html redirect file in $HOME", redirecthtml);
584 webui_url =
"file://" + redirecthtml;
585 wss->see_other (webui_url);
587 info (
"Main: WebUI address: %s", webui_url);
589 WebuiFlags webui_flags = main_app.headless ? WebuiFlags::HEADLESS : WebuiFlags::NONE;
590 if (main_app.ui_tests.
size())
591 webui_flags = webui_flags | WebuiFlags::STDIO_REDIRECT | WebuiFlags::CONSOLE_LOGS;
592 auto ereason = webui_start_browser (arg_ui_mode, main_loop, webui_url, [] () { main_loop->quit (0); }, webui_flags);
595 fatal_error (
"Main: failed to run WebUI: %s: %s", ereason.what, ::strerror (ereason.error));
599 for (
int sigid : { SIGHUP, SIGINT, SIGQUIT, SIGABRT, SIGTERM, SIGSYS }) {
600 main_loop->exec_usignal (sigid, [] (
int8 sig) {
601 info (
"Main: got signal %d: terminate", sig);
603 atquit_terminate (-1, pgid);
606 USignalSource::install_sigaction (sigid);
610 main_loop->exec_usignal (SIGUSR2, [wss] (
int8 sig) {
611 info (
"Main: got signal %d: reset WebSocket", sig);
615 USignalSource::install_sigaction (SIGUSR2);
622 if (App.play_autostart && preload_project)
623 main_loop->add ([preload_project] ()
625 info (
"Main: starting playback (auto)");
626 preload_project->start_playback (App.play_autostop);
629 main_loop->exec_dispatcher (handle_autostop);
632 main_loop->add ([] ()
638 if (App.mode == MainApp::CHECK_INTEGRITY_TESTS)
639 main_loop->add (run_tests_and_quit);
642 const int exitcode = main_loop->run();
644 diag (
"Main: event loop quit: code=%d", exitcode);
648 main_app.web_socket_server =
nullptr;
652 ProjectImpl::force_shutdown_all();
655 main_loop->iterate_pending();
660 diag (
"Main: exiting: %d", exitcode);
667main (
int argc,
char *argv[])
670#ifdef ASE_WITH_CPPTRACE
671 CPPTRACE_TRY { r = Ase::main (argc, argv); }
679 cpptrace::from_current_exception().print();
682 r = Ase::main (argc, argv);
static String priority_string(uint priority)
Return string which represents the given priority mask.
static LoopP current()
Return the thread-local singleton loop, created on first call.
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
#define _(...)
Retrieve the translation of a C or C++ string.
std::string color(Colors acolor, Colors c1, Colors c2, Colors c3, Colors c4, Colors c5, Colors c6)
Return ANSI code for the specified color if stdout & stderr should be colorized, see colorize_tty().
String basename(const String &path)
Strips all directory components from path and returns the resulting file name.
bool check(const String &file, const String &mode)
int run(void)
Run all registered tests.
The Anklang C++ API namespace.
std::string string_format(const char *format, const Args &...args) __attribute__((__format__(__printf__
Format a string similar to sprintf(3) with support for std::string and std::ostringstream convertible...
bool trkn_init(int argc, char *argv[], bool nodevs)
Setup tracktion and tracktion::engine.
StringS string_split(const String &string, const String &splitter, size_t maxn)
Split a string, using splitter as delimiter.
String string_to_hex(const String &input)
Convert bytes in string input to hexadecimal numbers.
void logging_prune_old_logs(double age_seconds)
Delete log files older than age seconds.
int8_t int8
An 8-bit signed integer.
std::vector< String > StringS
Convenience alias for a std::vector<std::string>.
JobQueue main_jobs(call_main_loop)
Execute a job callback in the event loop.
Error
Enum representing Error states.
const char * ase_error_blurb(Error error)
Describe Error condition.
std::string anklang_runpath(RPath rpath, const String &segment)
Retrieve various resource paths at runtime.
double string_to_seconds(const String &string, double fallback)
Parse string into seconds.
int64 string_to_int(const String &string, size_t *consumed, uint base)
Parse a string into a 64bit integer, optionally specifying the expected number base.
void main_loop_wakeup()
Wake up the event loop.
bool logging_fatal_warnings
Global flag to cause the program to abort on warnings.
@ IDLE
Mildly important, used for background tasks.
@ SYSALLOC
Internal maintenance, don't use.
const char * ase_version()
Provide a string containing the package version.
std::string String
Convenience alias for std::string.
std::string executable_name()
Retrieve the name part of executable_path().
bool string_to_bool(const String &string, bool fallback)
Interpret a string as boolean value.
void main_loop_autostop_mt()
Stop the event loop after a timeout.
bool debug_key_enabled(const char *conditional) noexcept
Check if conditional is enabled by $ASE_DEBUG.
uint32_t uint
Provide 'uint' as convenience type.
void load_registered_drivers()
Load all registered drivers.
size_t preallocate
Amount of preallocated available memory.
size_t watermark
Watermark to trigger async preallocation.
bool string_startswith(const String &string, const String &fragment)
Returns whether string starts with fragment.
const char * ase_build_id()
Provide a string containing the ASE library build id.
Configuration for Loft allocations.
size_t hash(size_t seed, const T &v)