aboutsummaryrefslogtreecommitdiff
path: root/clak.c
blob: 3c484bce28ea12b9768df7d8175dad4a25468292 (plain) (blame)
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "platform/platform.h"
#include "board/board.h"

#define VOLUME 0.15

struct board_state state = {0};

void do_exit(int code)
{
	keyboard_unhook();
	fprintf(stderr, "Goodbye.\n");
	exit(code);
}
void on_clean_exit(void) { do_exit(0); }

void keyboard_on_down(void)
{
	assert(state.on_down != NULL);
	state.on_down(&state);
}

void keyboard_on_up(void)
{
	assert(state.on_up != NULL);
	state.on_up(&state);
}

int main(int argc, char **argv)
{
	fprintf(stderr, "Clak: This is Clak, Nicholas Tay, version: 0.1.0.\n");

	if (argc < 2) {
		printf("ERROR: Please provide a board name.\n");
		// TODO: List valid boards.
		return 1;
	}

	/* Initialise board state */
	state.sound_play = &sound_play;  /* Inject platform-specific impl */

	fprintf(stderr, "Loading board...\n");
	if (!load_board(&state, argv[1])) {
		printf("Failed to load board, exiting.\n");
		return 1;
	}
	state.board_init(&state);
	fprintf(stderr, "Loaded board: %s\n", state.name);

	fprintf(stderr, "Initialisating sound system...\n");
	if (!sound_init(VOLUME)) {
		printf("ERROR: Could not initialise sound system.\n");
		return 1;
	}
	if (!keyboard_hook()) {
		printf("ERROR: Could not set up keyboard hooks.\n");
		return 1;
	}
	atexit(on_clean_exit);
	fprintf(stderr, "Hooks set up, enjoy your stay!\n");

	enter_idle();

	return 0;
}