aboutsummaryrefslogblamecommitdiff
path: root/clak.c
blob: eace40d12b0a824f1bf2c056bf9e532509429fac (plain) (tree)
1
2
3
4
5
6
7
8
9
                  
                   
                   

                    
 
                   
 
                         

                                                    

                              








                                                        
 
                    
 
                                  
 
                                            





                                                            







                                        












                               

                               






                                                                                          
         
 







                                                                      


                                                            
                     
 

                 
#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;
}