summaryrefslogtreecommitdiff
path: root/main.c
blob: 3a45592a6df2ca6153c04f102cbde20d774813c2 (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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <getopt.h>
#include <pthread.h>

#include "version.h"
#include "midi.h"

void help(const char *argv0) {
	printf("Usage: %s [options]\n"
			"\nAvailable options:\n"
			"  -l,--list                    list available devices\n"
			"  -p,--port=client:port        device port\n"
			"  -s,--soundfont=file.sf2      soundfont file\n"
			"  -h,--help                    this help\n"
			"  -v,--version                 show version\n",
			argv0);
}

int main(int argc, char *argv[]) {
	const char short_options[] = "lp:s:hv:";
	const struct option long_options[] = {
		{ "list", 0, NULL, 'l' },
		{ "port", 1, NULL, 'p' },
		{ "soundfont", 1, NULL, 's' },
		{ "help", 0, NULL, 'h' },
		{ "version", 0, NULL, 'v' },
		{ 0 },
	};

	char *port_name = NULL;
	char *soundfont_file = NULL;

	int opt;
	while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
		switch (opt) {
			case 'l':
				fprintf(stderr, "List feature is NOT implemented yet.\n");
				return 0;
			case 'p':
				port_name = optarg;
				break;
			case 's':
				soundfont_file = optarg;
				break;
			case 'h':
				help(argv[0]);
				return 0;
			case 'v':
				fprintf(stdout, "ttdaw version %s\n", TTDAW_VERSION);
				fprintf(stdout, "Website: %s.\n", TTDAW_WEBSITE);
				fprintf(stdout, "%s\n", TTDAW_LICENSE);
				fprintf(stdout, "%s\n", TTDAW_WARRANTY);
				fprintf(stdout, "\n%s\n", TTDAW_AUTHOR);
				return 0;
			default:
				fprintf(stdout, "Missing options. Check help.\n");
				return 0;
		}
	}

	if (port_name == NULL || soundfont_file == NULL) {
		fprintf(stdout, "Missing options. Check help.\n\n");
		fprintf(stdout, "Port and soundfile are required fields.\n\n");
		return 1;
	}

	fprintf(stdout, "> Device port:   %s\n", port_name);
	fprintf(stdout, "> Soundfont:     %s\n", soundfont_file);

	// Create and start MIDI thread.
	pthread_t midi_thread;
	MidiArgs midi_args = { port_name };

	if (pthread_create(&midi_thread, NULL, midi, (void*)&midi_args) != 0) {
		fprintf(stderr, "Error creating midi thread\n");
		return 1;
	}

	pthread_join(midi_thread, NULL);

	fprintf(stdout, "Exiting...\n");
	return 0;
}