summaryrefslogtreecommitdiff
path: root/main-sdl.c
diff options
context:
space:
mode:
authorerdgeist <erdgeist@bauklotz.fritz.box>2016-08-12 14:46:51 +0200
committererdgeist <erdgeist@bauklotz.fritz.box>2016-08-12 14:46:51 +0200
commita8be0d3d20f07d4561826b01f566ca307eb23526 (patch)
treeb2c5c6d513ae3a84aba8e4eea94ec32e46d352fa /main-sdl.c
commit as a backup
Diffstat (limited to 'main-sdl.c')
-rw-r--r--main-sdl.c268
1 files changed, 268 insertions, 0 deletions
diff --git a/main-sdl.c b/main-sdl.c
new file mode 100644
index 0000000..1f6d92d
--- /dev/null
+++ b/main-sdl.c
@@ -0,0 +1,268 @@
1#include <stdarg.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <signal.h>
5#include <sys/time.h>
6#include <pthread.h>
7#include <errno.h>
8#include <fcntl.h>
9#include <string.h>
10#include <termios.h>
11#include <unistd.h>
12#include <dirent.h>
13
14#include <SDL2/SDL.h>
15#include "display.h"
16#include "config.h"
17#include "engine.h"
18
19/***
20 Global config and status values
21 ***/
22
23/* Window width and height */
24// const int g_width = 1024, g_height = 768;
25const int g_width = 800, g_height = 600;
26
27StringConfig
28g_string_conf[MAX_LINECOUNT];
29int g_string_count;
30int g_harfe_connected = 0;
31int g_harfe_fd = -1;
32
33static char *
34find_harfe()
35{
36 // Device name should be a cu.device
37 // followed by the letter HAR somewhere
38 // e.g. /dev /cu.usbmodemHAR1
39 DIR * dev = opendir("/dev/");
40 struct dirent *dp;
41 char *harfe = 0;
42
43 if (!dev)
44 return 0;
45
46 while ((dp = readdir(dev)) != NULL) {
47 size_t len = dp->d_namlen;
48 char *name = dp->d_name, *H, *A, *R;
49 int i;
50
51 if (len < 6 || name[0] != 'c' || name[1] != 'u' || name[2] != '.')
52 continue;
53
54 for (i = 0; i < len - 3; ++i)
55 if (name[i] == 'H' && name[i + 1] == 'A' && name[i + 2] == 'R') {
56 if ((harfe = calloc(1, 5 + len + 1))) {
57 sprintf(harfe, "/dev/");
58 memcpy(harfe + 5, name, len);
59 }
60 break;
61 }
62 }
63 closedir(dev);
64 return harfe;
65}
66
67int
68set_interface_attribs(int fd, int speed, int parity)
69{
70 struct termios tty;
71
72 memset(&tty, 0, sizeof tty);
73 if (tcgetattr(fd, &tty) != 0)
74 return -1;
75 cfsetospeed(&tty, speed);
76 cfsetispeed(&tty, speed);
77
78 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
79 tty.c_iflag &= ~IGNBRK;
80 tty.c_lflag = 0;
81 tty.c_oflag = 0;
82 tty.c_cc[VMIN] = 0;
83 tty.c_cc[VTIME] = 5;
84
85 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
86
87 tty.c_cflag |= (CLOCAL | CREAD);
88
89 tty.c_cflag &= ~(PARENB | PARODD);
90 tty.c_cflag |= parity;
91 tty.c_cflag &= ~CSTOPB;
92 tty.c_cflag &= ~CRTSCTS;
93
94 if (tcsetattr(fd, TCSANOW, &tty) != 0)
95 return -1;
96 return 0;
97}
98
99void
100set_blocking(int fd, int should_block)
101{
102 struct termios tty;
103
104 memset(&tty, 0, sizeof tty);
105 if (tcgetattr(fd, &tty) != 0)
106 return;
107
108 tty.c_cc[VMIN] = should_block ? 1 : 0;
109 tty.c_cc[VTIME] = 5;
110
111 if (tcsetattr(fd, TCSANOW, &tty) != 0)
112 return;
113}
114
115
116uint32_t
117now()
118{
119 struct timeval now;
120
121 gettimeofday(&now, (struct timezone *)NULL);
122 return now.tv_sec * 1000 + now.tv_usec / 1000; /* in ms */
123}
124
125static uint32_t g_last_avg;
126static uint32_t g_starttime, g_last_mouse_event, g_lastredraw;
127static int g_events;
128static void
129harfe_worker(void)
130{
131 LPoint p[4];
132 char text[256], *lineend;
133 int fd, i, text_fill = 0, consumed, running = 1;
134 uint32_t ptime;
135
136 char *portname = find_harfe();
137
138 if (!portname) {
139 printf("Can't find harfe serial device.\n");
140 return;
141 }
142 g_harfe_fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
143 if (g_harfe_fd < 0) {
144 free(portname);
145 printf("Can't connect to harfe...");
146 return;
147 }
148 set_interface_attribs(g_harfe_fd, B115200, 0);
149 set_blocking(g_harfe_fd, 0);
150
151 printf("Connection to harfe established at %s.\n", portname);
152 free(portname);
153 g_harfe_connected = 1;
154
155 while (running) {
156 while (text_fill < sizeof(text) && !memchr(text, '\n', text_fill)) {
157 ssize_t b = read(g_harfe_fd, text + text_fill, sizeof(text) - text_fill);
158
159 if ((b < 0) && (errno != EAGAIN)) {
160 printf("Connection to harfe lost...");
161 running = 0;
162 close(g_harfe_fd);
163 g_harfe_fd = -1;
164 break;
165 }
166 text_fill += b;
167 }
168
169 //Overlong line, drop it and try to resync
170 if (text_fill == sizeof(text)) {
171 text_fill = 0;
172 continue;
173 }
174
175 // find and remove newline and cf
176 if (!(lineend = memchr(text, '\n', text_fill)))
177 continue;
178
179 *lineend = 0;
180 if (text_fill && lineend[-1] == '\r')
181 lineend[-1] = 0;
182
183 int num_points = sscanf(text, "%04d:%04d %04d:%04d %04d:%04d %04d:%04d", &p[0].x, &p[0].y, &p[1].x, &p[1].y, &p[2].x, &p[2].y, &p[3].x, &p[3].y);
184
185 ptime = now();
186 for (i = 0; i < num_points / 2; ++i) {
187 // printf("%04d:%04d\n", p[i].x, p[i].y);
188 engine_handle_point(p + i, ptime);
189 }
190 if (num_points > 1 || *text == '-')
191 ++g_events;
192
193 consumed = lineend - text + 1;
194 memmove(text, lineend + 1, text_fill - consumed);
195 text_fill -= consumed;
196 }
197 return;
198}
199static void *
200worker(void *args)
201{
202 while (1) {
203 harfe_worker();
204 g_harfe_connected = 0;
205 printf("retrying in 5 seconds.\n");
206 sleep(5);
207 }
208}
209
210int
211main(int argc, char **argv)
212{
213 SDL_Event ev;
214 int i, counter = 0;
215 pthread_t thread_id;
216 uint32_t runtime;
217 static int last_click_x, last_click_y, last_mouse_event;
218
219 display_init(g_width, g_height);
220 engine_init();
221 config_parse("config_midi");
222
223 pthread_create(&thread_id, NULL, worker, NULL);
224
225 /* Spin and let call back do all the work */
226 while (1) {
227 SDL_WaitEventTimeout(&ev, 10);
228 switch (ev.type) {
229 case SDL_QUIT:
230 exit(0);
231 case SDL_KEYDOWN:
232/*
233 if( ev.key.keysym.sym >= SDLK_1 && ev.key.keysym.sym <= SDLK_9 )
234 engine_select_string( ev.key.keysym.sym - SDLK_1 );
235 if( ev.key.keysym.sym == SDLK_BACKSPACE || ev.key.keysym.sym == SDLK_DELETE )
236 engine_delete_selected_string( );
237 if( ev.key.keysym.sym == SDLK_d ) {
238 g_stringsdescending = 1 - g_stringsdescending;
239 printf( "String order (left to right) is now %sscending.\n", g_stringsdescending ? "de" : "a" );
240 }
241*/
242 break;
243 case SDL_MOUSEBUTTONDOWN:
244/*
245 if( ( g_last_mouse_event / 1000 ) != ( engine_now( ) / 1000 ) || ev.button.x != last_click_x || ev.button.y != last_click_y )
246 engine_process_mouse( ev.button.x, ev.button.y );
247 last_click_x = ev.button.x;
248 last_click_y = ev.button.y;
249 last_mouse_event = engine_now( );
250*/
251 break;
252 }
253
254 engine_checksilence(now());
255 runtime = now();
256 if (runtime - g_lastredraw > 30) {
257 g_lastredraw = runtime;
258 engine_redraw();
259 }
260 if (runtime / 1000 - g_last_avg > 10) {
261 printf("avg: %i\n", g_events / 10);
262 g_events = 0;
263 g_last_avg = runtime / 1000;
264 }
265 }
266
267 return 0;
268}