-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.c
105 lines (89 loc) · 1.98 KB
/
ui.c
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
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <signal.h>
#include "ui.h"
#include "rvcon-log.h"
static void ui_cleanup();
static void ui_handle_signal(int sig);
static ui_config_t *ui_config;
static WINDOW *scr = NULL;
static int nrows=0, ncols=0;
int ui_init(ui_config_t *config) {
ui_config = config;
signal(SIGINT, ui_handle_signal); /* arrange interrupts to terminate */
scr = initscr(); /* initialize the curses library */
keypad(stdscr, TRUE); /* enable keyboard mapping */
nonl(); /* tell curses not to do NL->CR/NL on output */
cbreak(); /* take input chars one at a time, no wait for \n */
noecho();
getmaxyx(scr, nrows, ncols);
return 0;
}
static int ui_send_event(ui_event_type_t type) {
ui_event_t event;
event.type = type;
if (ui_config->event_cb)
return (ui_config->event_cb(&event));
return 0;
}
static int ui_handle_key_up()
{
mvprintw(nrows/2, ncols/2, "%6s", "UP");
ui_send_event(UI_EVENT_FWD);
return 0;
}
static int ui_handle_key_down()
{
mvprintw(nrows/2, ncols/2, "%6s", "DOWN");
ui_send_event(UI_EVENT_BACK);
return 0;
}
static int ui_handle_key_left()
{
mvprintw(nrows/2, ncols/2, "%6s", "LEFT");
ui_send_event(UI_EVENT_LEFT);
return 0;
}
static int ui_handle_key_right()
{
mvprintw(nrows/2, ncols/2, "%6s", "RIGHT");
ui_send_event(UI_EVENT_RIGHT);
return 0;
}
/* UI Loop */
void ui_start() {
int c;
while (1) {
c = getch(); /* refresh, accept single keystroke of input */
mvprintw(0, 0, "c=%03d", c, c);
refresh();
switch (c) {
case 'q':
ui_cleanup();
break;
case KEY_UP:
ui_handle_key_up();
break;
case KEY_DOWN:
ui_handle_key_down();
break;
case KEY_LEFT:
ui_handle_key_left();
break;
case KEY_RIGHT:
ui_handle_key_right();
break;
}
}
}
/* Clean-up function */
static void ui_cleanup() {
endwin();
if (ui_config->exit_cb) {
ui_config->exit_cb();
}
}
static void ui_handle_signal(int sig) {
ui_cleanup();
}