summaryrefslogtreecommitdiff
path: root/freeonebook.c
blob: 3e2a0088954bde7832fd11b072ba55c014a640ac (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "gpio.h"
#include "fb.h"

#define SDPATH "/run/media/mmcblk0p1"

void halt(void)
{
	fflush(NULL);
	sync();

	char *halt[] = { "shutdown", "-h", "now", NULL };
	execvp(halt[0], halt);
}

void poweroff(int reason)
{
	static int halting = 0;
	if (halting) {
		return;
	}
	halting = 1;

	printf("shutting down (%d)\n", reason);

	if (reason == GPIO_LOWBATTERY) {
		printf("low battery\n");
	}

	exit(0);
}

void buttonpress(int button)
{
	switch (button) {
	case BUTTON_SPECIAL:
		printf("special\n");
		break;

	case BUTTON_NEXTCHAPTER:
		printf("next chapter\n");
		break;

	case BUTTON_PREVBOOK:
		printf("previous book\n");
		break;

	case BUTTON_NEXTBOOK:
		printf("next book\n");
		break;

	case BUTTON_PREVPAGE:
		printf("previous page\n");
		break;

	case BUTTON_NEXTPAGE:
		printf("next page\n");
		break;

	default:
		printf("button %d pressed\n", button);
	}

	fflush(NULL);
	sync();
}

int main(int argc, char *argv[])
{
	printf("starting freeonebook\n");
	(void)argc; (void)argv;
	printf("registering atexit() handler\n");
	atexit(halt);

	printf("adding watchers\n");
	gpio_watch(GPIO_LOWBATTERY, poweroff);
	gpio_watch(GPIO_SHUTDOWN, poweroff);
	gpio_watch(BUTTON_SPECIAL, buttonpress);
	gpio_watch(BUTTON_PREVBOOK, buttonpress);
	gpio_watch(BUTTON_PREVPAGE, buttonpress);
	gpio_watch(BUTTON_NEXTPAGE, buttonpress);
	gpio_watch(BUTTON_NEXTCHAPTER, buttonpress);
	gpio_watch(BUTTON_NEXTBOOK, buttonpress);

	printf("initializing framebuffer\n");
	fb_init();
	fb_loadimage(RIGHT_SCREEN, SDPATH "/right.gray");
	fb_loadimage(LEFT_SCREEN, SDPATH "/left.gray");
	
	for (;;) {
		sleep(INT_MAX);
	}

	return 0;
}