1#include <stdlib.h>
2#include <stdio.h>
3#include <locale.h>
4#include <unistd.h>
5
6#include "interface.h"
7#include "mutex.h"
8
9#define TB_IMPL
10#include "termbox2.h"
11
12static int block_width = 14;
13static int block_height = 4;
14
15void draw_block(int x, int y, const char *label, int empty) {
16
17 tb_set_cell(x, y, 0x250C, TB_WHITE, TB_DEFAULT);
18 tb_set_cell(x+block_width-1, y, 0x2510, TB_WHITE, TB_DEFAULT);
19 tb_set_cell(x, y+block_height-1, 0x2514, TB_WHITE, TB_DEFAULT);
20 tb_set_cell(x+block_width-1, y+block_height-1, 0x2518, TB_WHITE, TB_DEFAULT);
21
22 tb_printf(x+(block_width/2) - (strlen(label)/2), y+(block_height/2)-1 ,TB_YELLOW, 0, label);
23 tb_printf(x+(block_width/2) - (strlen("empty")/2), y+(block_height/2) ,TB_DIM, 0, "empty");
24
25 tb_present();
26}
27
28void draw_blocks() {
29 int offset_x = 0;
30 int offset_y = 6;
31 char col_names[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
32
33 for (int r = 0; r < 4; r++) {
34 for (int c = 0; c < 6; c++) {
35 char label[3];
36 label[0] = col_names[c];
37 label[1] = '1' + r;
38 label[2] = '\0';
39 draw_block(offset_x+(block_width*c), offset_y+(block_height*r), label, 0);
40 }
41 }
42}
43
44void draw_help_tooltip(const char* tooltip_text) {
45 tb_printf(tb_width() - strlen(tooltip_text) - 1, tb_height()-1, TB_DIM, 0, tooltip_text);
46 tb_present();
47}
48
49void *interface(void *arg) {
50 InterfaceArgs* args = (InterfaceArgs*)arg;
51
52 int ret;
53 setlocale(LC_ALL, "");
54
55 ret = tb_init();
56 if (ret) {
57 fprintf(stderr, "tb_init() failed with error code %d\n", ret);
58 exit(1);
59 }
60
61 tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE);
62 struct tb_event ev;
63
64 tb_clear();
65 tb_present();
66
67 tb_set_cursor(0, tb_height()-1);
68 tb_hide_cursor();
69
70 // Show currently selected soundfont.
71 tb_printf(0, 0, TB_GREEN, 0, "Soundfont: %s", args->soundfont_file);
72 tb_printf(0, 1, TB_GREEN, 0, "Preset: %d", args->soundfont_preset);
73 tb_present();
74
75 // Draw interface.
76 draw_help_tooltip("Ctrl+q - Quit");
77 draw_blocks();
78 /* draw_block(10, 10, "A1"); */
79
80 while(1) {
81 ret = tb_poll_event(&ev);
82
83 if (ret != TB_OK) {
84 if (ret == TB_ERR_POLL && tb_last_errno() == EINTR) {
85 /* Poll was interrupted, maybe by a SIGWINCH; try again */
86 continue;
87 }
88 /* Some other error occurred; bail */
89 break;
90 }
91
92 switch (ev.type) {
93 case TB_EVENT_KEY:
94 if (ev.key == TB_KEY_CTRL_Q) {
95 tb_shutdown();
96 exit(0);
97 }
98
99 if (ev.key == TB_KEY_ARROW_UP) {
100
101 }
102
103 if (ev.key == TB_KEY_ARROW_DOWN) {
104
105 }
106
107 break;
108 }
109 }
110}
111