Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
main.cc
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#include "main.hh"
3#include "api.hh"
4#include "path.hh"
5#include "utils.hh"
6#include "jsonapi.hh"
7#include "driver.hh"
8#include "project.hh"
9#include "loft.hh"
10#include "compress.hh"
11#include "webui.hh"
12#include "server.hh"
13#include "internal.hh"
14#include "testing.hh"
15
16#include <limits.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <signal.h>
20#include <malloc.h>
21#include <unistd.h>
22#include <fcntl.h>
23#ifdef ASE_WITH_CPPTRACE
24#include <cpptrace/from_current.hpp>
25#endif
26
27#include "trkn.hh"
28
29#undef B0 // undo pollution from termios.h
30
31#define MDEBUG(...) Ase::debug ("memory", __VA_ARGS__)
32
33namespace Ase {
34
36 MainAppImpl ();
37};
38MainAppImpl main_app;
39const MainApp &App = main_app;
40
41MainAppImpl::MainAppImpl()
42{}
43
44LoopP main_loop = Loop::current();
45static String arg_ui_mode;
46static int arg_unauth_port = 0;
47
48// == JobQueue ==
49static void
50call_main_loop (const std::function<void()> &fun)
51{
52 main_loop->add (fun);
53}
54JobQueue main_jobs (call_main_loop);
55
56// == MainConfig and arguments ==
57static void
58print_usage (bool help)
59{
60 if (!help)
61 {
62 printout ("%s %s\n", executable_name(), ase_version());
63 printout ("Build: %s\n", ase_build_id());
64 return;
65 }
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 (" --ui-js=script Run JavaScript code after UI startup\n");
80 printout (" --play-autostart Automatically start playback of `project.anklang`\n");
81 printout (" --rand64 Produce 64bit random numbers on stdout\n");
82 printout (" --test[=test] Run specific test(s) (comma-separated)\n");
83 printout (" --unauth-dev=NUM Open an unauthenticated websocket port for testing\n");
84 printout (" --headless[=bool] Run browser in headless mode (default for --ui-test)\n");
85 printout (" --ui <none|chromium|google-chrome|htmlgui>\n");
86 printout (" Open GUI in web browser [htmlgui]\n");
87 printout (" --version Print program version\n");
88 printout (" -M mididriver Force use of <mididriver>\n");
89 printout (" -P pcmdriver Force use of <pcmdriver>\n");
90 printout (" -o wavfile Capture output to OPUS/FLAC/WAV file\n");
91 printout (" -t <time> Automatically play and stop after <time> has passed\n"); // -t <time>[{,|;}tailtime]
92 printout ("Options set via $ASE_DEBUG:\n");
93 printout (" :no-logfile: Disable logging to ~/.cache/anklang/ instead of stderr\n");
94}
95
97static bool
98parse_option_arg (const char *option, char **argv, unsigned *ith, const char **argp)
99{
100 const size_t l = strlen (option);
101 if (strncmp (option, argv[*ith], l) == 0) {
102 *argp = argv[*ith] + l;
103 argv[*ith] = nullptr;
104 if ((*argp)[0] == '=')
105 *argp += 1;
106 else if ((*argp)[0] == 0) {
107 *ith += 1;
108 *argp = argv[*ith] ? argv[*ith] : "";
109 argv[*ith] = nullptr;
110 }
111 return true;
112 }
113 return false;
114}
115
116// 1:ERROR 2:FAILED+REJECT 4:IO 8:MESSAGE 16:GET 256:BINARY
117static constexpr int jsipc_logflags = 1 | 2 | 4 | 8 | 16;
118static constexpr int jsbin_logflags = 1 | 256;
119
120static StringS check_test_names;
121static StringS ui_test_names;
122static String ui_js_script;
123
124static void
125parse_args (int *argcp, char **argv, MainAppImpl &config)
126{
127 if (0) // allow jsipc logging via ASE_DEBUG ?
128 {
129 config.jsonapi_logflags |= debug_key_enabled ("jsbin") ? jsbin_logflags : 0;
130 config.jsonapi_logflags |= debug_key_enabled ("jsipc") ? jsipc_logflags : 0;
131 }
132
133 config.norc = false;
134 bool sep = false; // -- separator
135 std::string default_ui_mode = "htmlgui";
136 const uint argc = *argcp;
137 for (uint i = 1; i < argc; i++)
138 {
139 const char *optarg = nullptr;
140 if (sep)
141 config.args.push_back (argv[i]);
142 else if (strcmp (argv[i], "--fatal-warnings") == 0 || strcmp (argv[i], "--g-fatal-warnings") == 0)
144 else if (strcmp ("--disable-randomization", argv[i]) == 0)
145 config.allow_randomization = false;
146 else if (strcmp ("--norc", argv[i]) == 0)
147 config.norc = true;
148 else if (strcmp ("--rand64", argv[i]) == 0)
149 {
150 FastRng prng;
151 constexpr int N = 8192;
152 uint64_t buffer[N];
153 while (1)
154 {
155 for (size_t i = 0; i < N; i++)
156 buffer[i] = prng.next();
157 fwrite (buffer, sizeof (buffer[0]), N, stdout);
158 }
159 exit (0);
160 }
161 else if (strcmp ("--check", argv[i]) == 0)
162 {
163 config.mode = MainApp::CHECK_INTEGRITY_TESTS;
165 printerr ("CHECK_INTEGRITY_TESTS…\n");
166 default_ui_mode = "none";
167 }
168 else if (strcmp ("--list-tests", argv[i]) == 0)
169 {
171 for (const auto &t : Test::list_tests())
172 ids.push_back (t.ident);
173 std::sort (ids.begin(), ids.end());
174 for (const auto &t : ids)
175 printout ("%s\n", t);
176 exit (0);
177 }
178 else if (strcmp ("--list-ui-tests", argv[i]) == 0)
179 {
180 const String testfile = anklang_runpath (RPath::INSTALLDIR, "/ui/assets/testcalls-list.txt");
181 if (!Path::check (testfile, "e"))
182 fatal_error ("missing UI test list: %s", testfile);
183 const String content = Path::stringread (testfile);
184 StringS lines = string_split (content, "\n");
185 for (const String &line : lines)
186 if (!line.empty () || string_startswith (line, "#"))
187 printout ("%s\n", line);
188 exit (0);
189 }
190 else if (strcmp ("--ui-test", argv[i]) == 0 || strncmp ("--ui-test=", argv[i], 9) == 0)
191 {
192 const char *eq = strchr (argv[i], '=');
193 const char *arg = eq ? eq + 1 : i+1 < argc ? argv[++i] : nullptr;
194 if (arg) {
195 const auto tests = string_split (arg, ",");
196 for (const auto &t : tests)
197 ui_test_names.push_back (t);
198 } else
199 ui_test_names.push_back ("all");
200 config.headless = true;
201 }
202 else if (strcmp ("--ui-js", argv[i]) == 0 || strncmp ("--ui-js=", argv[i], 8) == 0)
203 {
204 const char *eq = strchr (argv[i], '=');
205 ui_js_script = eq ? eq + 1 : i+1 < argc ? argv[++i] : "";
206 }
207 else if (strcmp ("--headless", argv[i]) == 0 || strncmp ("--headless=", argv[i], 10) == 0)
208 {
209 const char *eq = strchr (argv[i], '=');
210 config.headless = eq ? string_to_bool (eq + 1) : true;
211 }
212 else if (strcmp ("--test", argv[i]) == 0 || strncmp ("--test=", argv[i], 7) == 0)
213 {
214 const char *eq = strchr (argv[i], '=');
215 const char *arg = eq ? eq + 1 : i+1 < argc ? argv[++i] : nullptr;
216 config.mode = MainApp::CHECK_INTEGRITY_TESTS;
218 if (arg) {
219 const auto tests = string_split (arg, ",");
220 for (const auto &t : tests)
221 check_test_names.push_back (t);
222 }
223 default_ui_mode = "none";
224 }
225 else if (argv[i] == String ("--blake3") && i + 1 < size_t (argc))
226 {
227 argv[i++] = nullptr;
228 String hash = blake3_hash_file (argv[i]);
229 if (hash.empty())
230 printerr ("%s: failed to read: %s\n", argv[i], strerror (errno));
231 else
232 printout ("%s\n", string_to_hex (hash));
233 exit (hash == "");
234 }
235 else if (strcmp ("--jsonts", argv[i]) == 0) {
236 if (getenv ("ASE_JSONTS") == nullptr)
237 fatal_error ("%s: environment must contain ASE_JSONTS for --jsonts", argv[0]);
238 printout ("%s\n", Jsonipc::g_binding_printer->finish());
239 exit (0);
240 } else if (strcmp ("--jsipc", argv[i]) == 0)
241 config.jsonapi_logflags |= jsipc_logflags;
242 else if (strcmp ("--jsbin", argv[i]) == 0)
243 config.jsonapi_logflags |= jsbin_logflags;
244 else if (strcmp ("--list-drivers", argv[i]) == 0)
245 config.list_drivers = true;
246 else if (strcmp ("-M", argv[i]) == 0 && i + 1 < size_t (argc))
247 {
248 argv[i++] = nullptr;
249 config.midi_override = argv[i];
250 }
251 else if (strcmp ("-P", argv[i]) == 0 && i + 1 < size_t (argc))
252 {
253 argv[i++] = nullptr;
254 config.pcm_override = argv[i];
255 }
256 else if (strcmp ("--no-devices", argv[i]) == 0)
257 {
258 config.no_devices = true;
259 }
260 else if (strcmp ("-h", argv[i]) == 0 ||
261 strcmp ("--help", argv[i]) == 0)
262 {
263 print_usage (true);
264 exit (0);
265 }
266 else if (strcmp ("--version", argv[i]) == 0)
267 {
268 print_usage (false);
269 exit (0);
270 }
271 else if (argv[i] == String ("-o") && i + 1 < size_t (argc))
272 {
273 argv[i++] = nullptr;
274 config.outputfile = argv[i];
275 }
276 else if (argv[i] == String ("--play-autostart"))
277 {
278 config.play_autostart = true;
279 default_ui_mode = "none";
280 }
281 else if (parse_option_arg ("--unauth-dev", argv, &i, &optarg))
282 {
283 arg_unauth_port = string_to_int (optarg);
284 default_ui_mode = "wait";
285 }
286 else if (argv[i] == String ("-t") && i + 1 < size_t (argc))
287 {
288 config.play_autostart = true;
289 argv[i++] = nullptr;
290 config.play_autostop = string_to_seconds (argv[i]);
291 default_ui_mode = "none";
292 }
293 else if (parse_option_arg ("--ui", argv, &i, &optarg))
294 {
295 arg_ui_mode = optarg;
296 }
297 else if (argv[i] == String ("--") && !sep)
298 sep = true;
299 else if (argv[i][0] == '-' && !sep)
300 fatal_error ("invalid command line argument: %s", argv[i]);
301 else
302 config.args.push_back (argv[i]);
303 argv[i] = nullptr;
304 }
305 if (arg_ui_mode.empty())
306 arg_ui_mode = default_ui_mode;
307 if (*argcp > 1)
308 {
309 uint e = 1;
310 for (uint i = 1; i < argc; i++)
311 if (argv[i])
312 {
313 argv[e++] = argv[i];
314 if (i >= e)
315 argv[i] = nullptr;
316 }
317 *argcp = e;
318 }
319}
320
321static String
322make_auth_string()
323{
324 const char *const c52 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
325 /* We use WebScoket subprotocol randomization as authentication, so:
326 * a) Authentication happens *before* message interpretation, so an
327 * unauthenticated sender cannot cause crahses via e.g. rapidjson exceptions.
328 * b) To serve as working authentication measure, the subprotocol random string
329 * must be cryptographically-secure.
330 */
331 KeccakCryptoRng csprng;
332 String auth = "sessC";
333 for (size_t i = 0; i < 23; ++i)
334 auth += c52[csprng.random() % 52]; // each step adds 5.7 bits
335 return auth;
336}
337
338static void
339run_tests_and_quit ()
340{
341 if (check_test_names.empty())
342 Test::run();
343 else
344 Test::run (check_test_names);
345 main_loop->quit (0);
346}
347
348void
350{
351 LoopP loop = main_loop;
352 if (loop)
353 loop->wakeup();
354}
355
356static std::atomic<bool> seen_autostop = false;
357
358// Lock and obstruction-free autostop trigger.
359void
361{
362 if (!seen_autostop)
363 {
364 seen_autostop = true;
366 }
367}
368
369static bool
370handle_autostop (const LoopState &state)
371{
372 switch (state.phase)
373 {
374 case LoopState::PREPARE: return seen_autostop;
375 case LoopState::CHECK: return seen_autostop;
376 case LoopState::DISPATCH:
377 info ("Main: stopping playback (auto)");
378 main_loop->quit (0);
379 return true; // keep alive
380 default: ;
381 }
382 return false;
383}
384
385static void
386init_sigpipe()
387{
388 // don't die if we write() data to a process and that process dies (i.e. jackd)
389 sigset_t signal_mask;
390 sigemptyset (&signal_mask);
391 sigaddset (&signal_mask, SIGPIPE);
392
393 int rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);
394 if (rc != 0)
395 Ase::warning ("Ase: pthread_sigmask for SIGPIPE failed: %s\n", strerror (errno));
396}
397
398static std::atomic<bool> loft_needs_preallocation_mt = false;
399
400// handle watermark underrun notifications
401static void
402notify_loft_lowmem ()
403{
404 if (!loft_needs_preallocation_mt)
405 {
406 loft_needs_preallocation_mt = true;
408 }
409}
410
411static size_t last_loft_preallocation = 0;
412
413static void
414preallocate_loft (size_t preallocation)
415{
416 using namespace Ase;
417 last_loft_preallocation = preallocation;
418 LoftConfig loftcfg = {
419 .preallocate = last_loft_preallocation,
420 .watermark = last_loft_preallocation / 2,
421 .flags = Loft::PREFAULT_PAGES,
422 };
423 loft_set_config (loftcfg);
424 loft_set_notifier (notify_loft_lowmem);
425 loft_grow_preallocate();
426}
427
428static bool
429dispatch_loft_lowmem (const Ase::LoopState &lstate)
430{
431 using namespace Ase;
432 const bool keep_alive = lstate.phase == LoopState::DISPATCH;
433 // generally, dispatch logic may only run in LoopState::DISPATCH, but this handler
434 // makes a rare exception, because we try to get ahead of concurrently runnint RT-threads...
435 return_unless (loft_needs_preallocation_mt, keep_alive);
436 loft_needs_preallocation_mt = false;
437 last_loft_preallocation *= 2;
438 const size_t newalloc = loft_grow_preallocate (last_loft_preallocation);
439 LoftConfig config;
440 loft_get_config (config);
441 config.watermark = last_loft_preallocation / 2;
442 loft_set_config (config);
443 if (newalloc > 0)
444 MDEBUG ("Loft preallocation in main thread: %f MB", newalloc / (1024. * 1024));
445 return keep_alive;
446}
447
448static void
449prefault_pages (size_t stacksize, size_t heapsize)
450{
451 const size_t pagesize = sysconf (_SC_PAGESIZE);
452 char *heap = (char*) malloc (heapsize);
453 if (heap)
454 for (size_t i = 0; i < heapsize; i += pagesize)
455 heap[i] = 1;
456 free (heap);
457 char *stack = (char*) alloca (stacksize);
458 if (stack)
459 for (size_t i = 0; i < stacksize; i += pagesize)
460 stack[i] = 1;
461}
462
463static int
464main (int argc, char *argv[])
465{
466 using namespace Ase;
467 using namespace AnsiColors;
468
469 // setup thread identifier
470 TaskRegistry::setup_ase ("AnklangMainProc");
471 // use malloc to serve allocations via sbrk only (avoid mmap)
472 mallopt (M_MMAP_MAX, 0);
473 // avoid releasing sbrk memory back to the system (reduce page faults)
474 mallopt (M_TRIM_THRESHOLD, -1);
475 // reserve large sbrk area and reduce page faults for heap and stack
476 prefault_pages ((1024 + 768) * 1024, 64 * 1024 * 1024);
477 // preallocate memory for lock-free allocator
478 preallocate_loft (64 * 1024 * 1024);
479 // warn if preallocation is not sufficient
480 loft_set_growth_notifier ([] (size_t total, size_t needed)
481 {
482 warning ("Loft.BumpAllocator: growing beyond preallocation: totalmem=%u needed=%d\n", total, needed);
483 });
484
485 // print stack trace for uncaught exceptions
486 logging_handle_terminate();
487
488 // SIGPIPE init: needs to be done before any child thread is created
489 init_sigpipe();
490 if (setpgid (0, 0) < 0)
491 diag ("Main: setpgid failed: %s", ::strerror (errno));
492
493 // apply user locale
494 if (!setlocale (LC_ALL, ""))
495 fatal_error ("setlocale: locale not supported by libc: %s", ::strerror (errno));
496
497 // parse args and config
498 parse_args (&argc, argv, main_app);
499 main_app.ui_tests = ui_test_names;
500 main_app.ui_js = ui_js_script;
501 const int socket_port = arg_unauth_port > 0 ? arg_unauth_port : 0;
502 const char *socket_host = "127.0.0.1";
503 const auto socket_info = WebSocketServer::bind_port (socket_host, socket_port);
504 logging_configure (arg_ui_mode != "none" ? string_format ("%u", socket_info.port) : "");
505
506 // handle loft preallocation needs
507 main_loop->exec_dispatcher (dispatch_loft_lowmem, LoopPriority::SYSALLOC);
508
509 // load preferences unless --norc was given
510 if (!App.norc)
511 Preference::load_preferences (true);
512
513 // Ensure Ase server exists
514 ServerImpl::instancep();
515
516 // tracktion initialisation
517 if (!trkn_init (argc, argv, App.no_devices))
518 fatal_error ("Main: failed to initialize tracktion engine");
519
520 const auto B1 = color (BOLD);
521 const auto B0 = color (BOLD_OFF);
522
523 // load drivers and dump device list
525 if (App.list_drivers)
526 {
527 Ase::Driver::EntryVec entries;
528 printout ("%s", _("Available PCM drivers:\n"));
529 entries = Ase::PcmDriver::list_drivers();
530 std::sort (entries.begin(), entries.end(), [] (auto &a, auto &b) { return a.priority < b.priority; });
531 for (const auto &entry : entries)
532 {
533 printout (" %-30s (%s, %08x)\n\t%s\n%s%s%s%s", entry.devid + ":",
534 entry.readonly ? "Input" : entry.writeonly ? "Output" : "Duplex",
535 entry.priority, entry.device_name,
536 entry.capabilities.empty() ? "" : "\t" + entry.capabilities + "\n",
537 entry.device_info.empty() ? "" : "\t" + entry.device_info + "\n",
538 entry.hints.empty() ? "" : "\t(" + entry.hints + ")\n",
539 entry.notice.empty() ? "" : "\t" + entry.notice + "\n");
540 if (debug_key_enabled ("driver"))
541 printerr (" %08x: %s\n", entry.priority, Driver::priority_string (entry.priority));
542 }
543 printout ("%s", _("Available MIDI drivers:\n"));
544 entries = Ase::MidiDriver::list_drivers();
545 std::sort (entries.begin(), entries.end(), [] (auto &a, auto &b) { return a.priority < b.priority; });
546 for (const auto &entry : entries)
547 {
548 printout (" %-30s (%s, %08x)\n\t%s\n%s%s%s%s", entry.devid + ":",
549 entry.readonly ? "Input" : entry.writeonly ? "Output" : "Duplex",
550 entry.priority, entry.device_name,
551 entry.capabilities.empty() ? "" : "\t" + entry.capabilities + "\n",
552 entry.device_info.empty() ? "" : "\t" + entry.device_info + "\n",
553 entry.hints.empty() ? "" : "\t(" + entry.hints + ")\n",
554 entry.notice.empty() ? "" : "\t" + entry.notice + "\n");
555 if (debug_key_enabled ("driver"))
556 printerr (" %08x: %s\n", entry.priority, Driver::priority_string (entry.priority));
557 }
558 return 0;
559 }
560
561 // load projects
562 ProjectImplP preload_project;
563 for (const auto &filename : App.args)
564 {
565 preload_project = ProjectImpl::create (Path::basename (filename));
566 Error error = Error::NO_MEMORY;
567 if (preload_project)
568 error = preload_project->load_project (filename);
569 diag ("Main: load project: %s: %s", filename, ase_error_blurb (error));
570 if (!!error)
571 warning ("%s: failed to load project: %s", filename, ase_error_blurb (error));
572 }
573
574 // open Jsonapi socket
575 const String auth_token = arg_unauth_port > 0 ? "" : make_auth_string();
576 auto wss = WebSocketServer::create (jsonapi_make_connection, App.jsonapi_logflags, auth_token);
577 main_app.web_socket_server = &*wss;
578 wss->http_dir (anklang_runpath (RPath::INSTALLDIR, "/ui/"));
579 // wss->http_alias ("/User/Controller", anklang_home_dir ("/Controller"));
580 wss->http_alias ("/Builtin/Controller", anklang_runpath (RPath::INSTALLDIR, "/Controller"));
581 // wss->http_alias ("/User/Scripts", anklang_home_dir ("/Scripts"));
582 wss->http_alias ("/Builtin/Scripts", anklang_runpath (RPath::INSTALLDIR,"/Scripts"));
583 const String subprotocol = ""; // make_auth_string()
584 jsonapi_set_subprotocol (subprotocol);
585 if (App.mode == MainApp::SYNTHENGINE && arg_ui_mode != "none") {
586 wss->listen (socket_info, [] () { main_loop->quit (-1); });
587 std::string webui_url = wss->url();
588 if (!socket_port) {
589 String redirecthtml = webui_create_auth_redirect ("anklang", wss->listen_port(), auth_token, arg_ui_mode);
590 if (errno)
591 fatal_error ("%s: failed to create html redirect file in $HOME", redirecthtml);
592 webui_url = "file://" + redirecthtml;
593 wss->see_other (webui_url);
594 }
595 info ("Main: WebUI address: %s", webui_url);
596
597 WebuiFlags webui_flags = main_app.headless ? WebuiFlags::HEADLESS : WebuiFlags::NONE;
598 if (main_app.ui_tests.size())
599 webui_flags = webui_flags | WebuiFlags::CONSOLE_LOGS; // WebuiFlags::STDIO_REDIRECT
600 auto ereason = webui_start_browser (arg_ui_mode, main_loop, webui_url,
601 [] (int exit_code)
602 {
603 main_loop->quit (exit_code);
604 }, webui_flags);
605
606 if (ereason.error)
607 fatal_error ("Main: failed to run WebUI: %s: %s", ereason.what, ::strerror (ereason.error));
608 }
609
610 // run atquit handler on SIGHUP SIGINT
611 for (int sigid : { SIGHUP, SIGINT, SIGQUIT, SIGABRT, SIGTERM, SIGSYS }) {
612 main_loop->exec_usignal (sigid, [] (int8 sig) {
613 info ("Main: got signal %d: terminate", sig);
614 const pid_t pgid = getpgrp();
615 atquit_terminate (-1, pgid);
616 return false;
617 });
618 USignalSource::install_sigaction (sigid);
619 }
620
621 // catch SIGUSR2 to close sockets
622 main_loop->exec_usignal (SIGUSR2, [wss] (int8 sig) {
623 info ("Main: got signal %d: reset WebSocket", sig);
624 wss->reset();
625 return true;
626 });
627 USignalSource::install_sigaction (SIGUSR2);
628
629 // start output capturing
630 if (App.outputfile)
631 ; // TODO: implement capturing
632
633 // start auto play
634 if (App.play_autostart && preload_project)
635 main_loop->add ([preload_project] ()
636 {
637 info ("Main: starting playback (auto)");
638 preload_project->start_playback (App.play_autostop);
640 // handle automatic shutdown
641 main_loop->exec_dispatcher (handle_autostop);
642
643 // prune old log files after some time
644 main_loop->add ([] ()
645 {
646 logging_prune_old_logs (3.0 * 24.0 * 60.0 * 60.0);
648
649 // run test suite
650 if (App.mode == MainApp::CHECK_INTEGRITY_TESTS)
651 main_loop->add (run_tests_and_quit);
652
653 // run main event loop and catch SIGUSR2
654 const int exitcode = main_loop->run();
655 assert_return (main_loop, -1); // ptr must be kept around
656 diag ("Main: event loop quit: code=%d", exitcode);
657
658 // cleanup
659 wss->shutdown(); // close socket, allow no more calls
660 main_app.web_socket_server = nullptr;
661 wss = nullptr;
662
663 // deactivate any projects, releases Audio resources
664 ProjectImpl::force_shutdown_all();
665
666 // halt audio engine, join its threads, dispatch cleanups
667 main_loop->iterate_pending();
668
669 // shutdown tracktion *after* main loop stopped
670 trkn_shutdown ();
671
672 diag ("Main: exiting: %d", exitcode);
673 return exitcode;
674}
675
676} // Ase
677
678int
679main (int argc, char *argv[])
680{
681 int r = -128;
682#ifdef ASE_WITH_CPPTRACE
683 CPPTRACE_TRY { r = Ase::main (argc, argv); }
684 CPPTRACE_CATCH (const std::exception& e) {
685 std::string msg = "Exception: ";
686 msg += e.what();
687 msg += "\n";
688 fflush (stdout);
689 fputs (msg.c_str(), stderr);
690 fflush (stderr);
691 cpptrace::from_current_exception().print();
692 }
693#else
694 r = Ase::main (argc, argv);
695#endif
696 return r;
697}
T c_str(T... args)
static String priority_string(uint priority)
Return string which represents the given priority mask.
Definition driver.cc:31
static LoopP current()
Return the thread-local singleton loop, created on first call.
Definition loop.cc:306
T empty(T... args)
exit
fflush
fputs
free
fwrite
optarg
getpgrp
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
Definition internal.hh:28
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
Definition internal.hh:72
#define _(...)
Retrieve the translation of a C or C++ string.
Definition internal.hh:17
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().
Definition platform.cc:191
String basename(const String &path)
Strips all directory components from path and returns the resulting file name.
Definition path.cc:68
bool check(const String &file, const String &mode)
Definition path.cc:625
int run(void)
Run all registered tests.
Definition testing.cc:264
The Anklang C++ API namespace.
Definition api.hh:8
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.
Definition trkn.cc:99
StringS string_split(const String &string, const String &splitter, size_t maxn)
Split a string, using splitter as delimiter.
Definition strings.cc:343
String string_to_hex(const String &input)
Convert bytes in string input to hexadecimal numbers.
Definition strings.cc:1171
void logging_prune_old_logs(double age_seconds)
Delete log files older than age seconds.
Definition logging.cc:233
int8_t int8
An 8-bit signed integer.
Definition cxxaux.hh:25
std::vector< String > StringS
Convenience alias for a std::vector<std::string>.
Definition cxxaux.hh:35
JobQueue main_jobs(call_main_loop)
Execute a job callback in the event loop.
Definition main.hh:44
Error
Enum representing Error states.
Definition api.hh:21
const char * ase_error_blurb(Error error)
Describe Error condition.
Definition server.cc:276
std::string anklang_runpath(RPath rpath, const String &segment)
Retrieve various resource paths at runtime.
Definition platform.cc:58
double string_to_seconds(const String &string, double fallback)
Parse string into seconds.
Definition strings.cc:773
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.
Definition strings.cc:578
void main_loop_wakeup()
Wake up the event loop.
Definition main.cc:349
bool logging_fatal_warnings
Global flag to cause the program to abort on warnings.
Definition logging.cc:304
@ IDLE
Mildly important, used for background tasks.
@ SYSALLOC
Internal maintenance, don't use.
const char * ase_version()
Provide a string containing the package version.
Definition platform.cc:803
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:34
std::string executable_name()
Retrieve the name part of executable_path().
Definition platform.cc:742
bool string_to_bool(const String &string, bool fallback)
Interpret a string as boolean value.
Definition strings.cc:467
void main_loop_autostop_mt()
Stop the event loop after a timeout.
Definition main.cc:360
bool debug_key_enabled(const char *conditional) noexcept
Check if conditional is enabled by $ASE_DEBUG.
Definition logging.cc:320
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:17
void load_registered_drivers()
Load all registered drivers.
Definition driver.cc:76
size_t preallocate
Amount of preallocated available memory.
Definition loft.hh:44
size_t watermark
Watermark to trigger async preallocation.
Definition loft.hh:45
bool string_startswith(const String &string, const String &fragment)
Returns whether string starts with fragment.
Definition strings.cc:846
const char * ase_build_id()
Provide a string containing the ASE library build id.
Definition platform.cc:809
Configuration for Loft allocations.
Definition loft.hh:43
size_t hash(size_t seed, const T &v)
pthread_sigmask
T push_back(T... args)
sigaddset
sigemptyset
T size(T... args)
T sort(T... args)
typedef uint64_t
strchr
strlen
typedef pid_t
sysconf
T what(T... args)