|
1 | 1 | #include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <fstream> |
2 | 4 | #include <string> |
3 | 5 | #include <string_view> |
4 | 6 | #include <chrono> |
5 | 7 | #include <thread> |
| 8 | +#include <filesystem> |
6 | 9 |
|
7 | 10 | #include <report.hpp> |
8 | 11 | #include <view.hpp> |
9 | 12 | #include <lib.hpp> |
10 | 13 |
|
| 14 | +#include <conflict/conflict.hpp> |
| 15 | + |
11 | 16 | extern "C" { |
12 | 17 | #include <jack/jack.h> |
13 | 18 | #include <jack/midiport.h> |
14 | 19 | #include <jack/ringbuffer.h> |
15 | 20 | } |
16 | 21 |
|
17 | | -int main(int, const char*[]) { |
18 | | - std::string device = "j2a"; |
| 22 | +enum { |
| 23 | + OPT_HELP = 0b01, |
| 24 | + OPT_LIST = 0b10, |
| 25 | +}; |
19 | 26 |
|
| 27 | +inline std::string read_file(std::filesystem::path path) { |
20 | 28 | try { |
21 | | - std::istreambuf_iterator<char> begin { std::cin }, end; |
22 | | - std::string in { begin, end }; |
| 29 | + std::filesystem::path cur = path; |
23 | 30 |
|
24 | | - cane::View src { &*in.begin(), &*in.end() }; |
25 | | - cane::Lexer lx { src }; |
| 31 | + while (std::filesystem::is_symlink(cur)) { |
| 32 | + std::filesystem::path tmp = std::filesystem::read_symlink(cur); |
26 | 33 |
|
27 | | - if (not cane::utf_validate(src)) |
28 | | - lx.error(cane::Phases::ENCODING, src, cane::STR_ENCODING); |
| 34 | + if (tmp == cur) |
| 35 | + cane::general_error(cane::STR_SYMLINK_ERROR, path); |
29 | 36 |
|
30 | | - namespace time = std::chrono; |
| 37 | + cur = tmp; |
| 38 | + } |
31 | 39 |
|
32 | | - using clock = time::steady_clock; |
33 | | - using unit = time::microseconds; |
| 40 | + if (std::filesystem::is_directory(cur) or std::filesystem::is_other(cur)) |
| 41 | + cane::general_error(cane::STR_NOT_FILE_ERROR, path); |
34 | 42 |
|
35 | | - auto t1 = clock::now(); |
36 | | - cane::Context ctx = cane::compile(lx); |
37 | | - auto t2 = clock::now(); |
| 43 | + if (not std::filesystem::exists(cur)) |
| 44 | + cane::general_error(cane::STR_FILE_NOT_FOUND_ERROR, path); |
38 | 45 |
|
39 | | - time::duration<double, std::micro> t = t2 - t1; |
40 | | - CANE_LOG(cane::LOG_SUCC, CANE_ANSI_FG_YELLOW "took: {}µs" CANE_ANSI_RESET, t.count()); |
| 46 | + std::ifstream is(cur, std::ios::binary); |
41 | 47 |
|
42 | | - if (ctx.timeline.empty()) |
| 48 | + if (not is.is_open()) |
| 49 | + cane::general_error(cane::STR_FILE_READ_ERROR, path); |
| 50 | + |
| 51 | + std::stringstream ss; |
| 52 | + ss << is.rdbuf(); |
| 53 | + |
| 54 | + return ss.str(); |
| 55 | + } |
| 56 | + |
| 57 | + catch (const std::filesystem::filesystem_error&) { |
| 58 | + cane::general_error(cane::STR_FILE_READ_ERROR, path); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +int main(int argc, const char* argv[]) { |
| 63 | + std::string_view device; |
| 64 | + std::string_view filename; |
| 65 | + uint64_t flags; |
| 66 | + |
| 67 | + auto parser = conflict::parser { |
| 68 | + conflict::option { { 'h', "help", "show help" }, flags, OPT_HELP }, |
| 69 | + conflict::option { { 'l', "list", "list available midi devices" }, flags, OPT_LIST }, |
| 70 | + |
| 71 | + conflict::string_option { { 'f', "file", "input file" }, "filename", filename }, |
| 72 | + conflict::string_option { { 'm', "midi", "midi device to connect to" }, "device", device }, |
| 73 | + }; |
| 74 | + |
| 75 | + parser.apply_defaults(); |
| 76 | + auto st = parser.parse(argc - 1, argv + 1); |
| 77 | + |
| 78 | + try { |
| 79 | + conflict::default_report(st); |
| 80 | + |
| 81 | + if (flags & OPT_HELP) { |
| 82 | + parser.print_help(); |
43 | 83 | return 0; |
| 84 | + } |
44 | 85 |
|
45 | 86 |
|
46 | 87 | // Setup JACK |
@@ -92,21 +133,67 @@ int main(int, const char*[]) { |
92 | 133 | cane::general_error(cane::STR_MIDI_ACTIVATE_ERROR); |
93 | 134 |
|
94 | 135 |
|
| 136 | + // If no device is specified _and_ `-l` is not passed, |
| 137 | + // throw an error. It's perfectly valid to give an empty |
| 138 | + // string to JACK here and it will give us back a list |
| 139 | + // of ports regardless. |
| 140 | + if (device.empty() and (flags & OPT_LIST) != OPT_LIST) |
| 141 | + cane::general_error(cane::STR_MIDI_NO_DEVICE); |
| 142 | + |
| 143 | + |
95 | 144 | // Get an array of all MIDI input ports that we could potentially connect to. |
96 | 145 | const char** ports = nullptr; |
97 | 146 |
|
98 | | - if (not (ports = jack_get_ports(midi.client, device.c_str(), JACK_DEFAULT_MIDI_TYPE, JackPortIsInput))) |
| 147 | + if (not (ports = jack_get_ports(midi.client, std::string{ device }.c_str(), JACK_DEFAULT_MIDI_TYPE, JackPortIsInput))) |
99 | 148 | cane::general_error(cane::STR_MIDI_GET_PORTS_ERROR); |
100 | 149 |
|
101 | 150 | if (*ports == nullptr) // No MIDI input ports. |
102 | 151 | cane::general_error(cane::STR_MIDI_NOT_FOUND, device); |
103 | 152 |
|
| 153 | + if (flags & OPT_LIST) { |
| 154 | + for (; *ports != nullptr; ++ports) |
| 155 | + cane::general_notice(cane::STR_MIDI_DEVICE, *ports); |
| 156 | + |
| 157 | + return 0; |
| 158 | + } |
| 159 | + |
104 | 160 | cane::general_notice(cane::STR_MIDI_FOUND, *ports); |
105 | 161 |
|
106 | 162 | if (jack_connect(midi.client, jack_port_name(midi.port), *ports)) |
107 | 163 | cane::general_error(cane::STR_MIDI_PATCH_ERROR); |
108 | 164 |
|
109 | | - jack_free(ports); |
| 165 | + jack_free(ports); // TODO: free this on error. |
| 166 | + |
| 167 | + |
| 168 | + // Compiler |
| 169 | + if (filename.empty()) |
| 170 | + cane::general_error(cane::STR_NO_FILE); |
| 171 | + |
| 172 | + auto path = std::filesystem::current_path() / std::filesystem::path { filename }; |
| 173 | + std::filesystem::current_path(path.parent_path()); |
| 174 | + |
| 175 | + std::string in = read_file(path); |
| 176 | + |
| 177 | + cane::View src { &*in.begin(), &*in.end() }; |
| 178 | + cane::Lexer lx { src }; |
| 179 | + |
| 180 | + if (not cane::utf_validate(src)) |
| 181 | + lx.error(cane::Phases::ENCODING, src, cane::STR_ENCODING); |
| 182 | + |
| 183 | + namespace time = std::chrono; |
| 184 | + |
| 185 | + using clock = time::steady_clock; |
| 186 | + using unit = time::microseconds; |
| 187 | + |
| 188 | + auto t1 = clock::now(); |
| 189 | + cane::Context ctx = cane::compile(lx); |
| 190 | + auto t2 = clock::now(); |
| 191 | + |
| 192 | + time::duration<double, std::micro> t = t2 - t1; |
| 193 | + CANE_LOG(cane::LOG_SUCC, CANE_ANSI_FG_YELLOW "took: {}µs" CANE_ANSI_RESET, t.count()); |
| 194 | + |
| 195 | + if (ctx.timeline.empty()) |
| 196 | + return 0; |
110 | 197 |
|
111 | 198 |
|
112 | 199 | // Sequencer |
|
0 commit comments