aboutsummaryrefslogtreecommitdiff
path: root/clak.c
blob: eace40d12b0a824f1bf2c056bf9e532509429fac (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
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

#define VOLUME 0.15

#include "board/boards.h"
#define BOARD(str, buf) { .name = str, .wav = buf },

#include "platform/platform.h"

typedef struct {
	char *name;
	unsigned char* wav;
} Board;

Board boards[] = {
	BOARDS
};
int const boards_n = sizeof(boards) / sizeof(boards[0]);

Board *board = NULL;

Board *get_board(char *board_name)
{
	for (int i = 0; i < boards_n; ++i) {
		if (strcmp(boards[i].name, board_name) == 0)
			return &boards[i];
	}
	return NULL;
}

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)
{
	if (board == NULL)
		return;
	sound_play(board->wav);
}

void keyboard_on_up(void)
{
	// if (board == NULL)
	// 	return;
}

int main(int argc, char **argv)
{
	if (argc < 2 || (board = get_board(argv[1])) == NULL) {
		printf("Please provide a valid board name.\n");
		printf("Valid boards: ");
		for (int i = 0; i < boards_n; ++i) {
			printf("%s%s", boards[i].name, (i == boards_n - 1) ? "\n" : ", ");
		}
		return 1;
	}

	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, welcome to Clak!\n");

	enter_idle();

	return 0;
}