diff options
-rw-r--r-- | main-sdl.c | 197 |
1 files changed, 102 insertions, 95 deletions
@@ -17,6 +17,7 @@ | |||
17 | #include "config.h" | 17 | #include "config.h" |
18 | #include "engine.h" | 18 | #include "engine.h" |
19 | #include "calib.h" | 19 | #include "calib.h" |
20 | #include "menu.h" | ||
20 | 21 | ||
21 | /*** | 22 | /*** |
22 | Global config and status values | 23 | Global config and status values |
@@ -32,9 +33,6 @@ enum { | |||
32 | 33 | ||
33 | int g_harfe_connected = 0; | 34 | int g_harfe_connected = 0; |
34 | int g_harfe_fd = -1; | 35 | int g_harfe_fd = -1; |
35 | int g_importing_config = 0; | ||
36 | ConfigSource g_config_source = source_none; | ||
37 | int g_calibration_running = 0; | ||
38 | 36 | ||
39 | static char * | 37 | static char * |
40 | find_harfe() | 38 | find_harfe() |
@@ -259,33 +257,74 @@ config_parse(char *config_file) | |||
259 | g_config_source = source_file; | 257 | g_config_source = source_file; |
260 | } | 258 | } |
261 | 259 | ||
262 | static void | 260 | void config_save() { |
263 | calib_fetch() { | 261 | const char *homeDir = getenv("HOME"); |
264 | int default_notes[] = { 60, 62, 64, 65, 67, 69, 71, 72, 74, 76, 77, 79, 81, 83 }; | 262 | char savefile[512], date[32], confdump[512]; |
265 | int i, result_count; | 263 | time_t t = time(NULL); |
266 | LLine *result = calib_get_results(&result_count); | 264 | struct tm *tmp = localtime(&t); |
265 | int i, fd; | ||
266 | size_t len; | ||
267 | |||
268 | if (!homeDir) { | ||
269 | struct passwd* pwd = getpwuid(getuid()); | ||
270 | if (pwd) | ||
271 | homeDir = (const char*)pwd->pw_dir; | ||
272 | } | ||
273 | strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", tmp); | ||
274 | snprintf( savefile, sizeof(savefile), "%s/Laserharfe-%s.cfg", homeDir, date); | ||
275 | fd = open(savefile, O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
276 | |||
277 | len = config_dumpglobals( confdump, sizeof(confdump)); | ||
278 | write(fd, confdump, len ); | ||
279 | for (i=0; i<g_string_count; ++i) { | ||
280 | len = config_dumpstring(i, confdump, sizeof(confdump)); | ||
281 | write(fd, confdump, len ); | ||
282 | } | ||
283 | close(fd); | ||
267 | 284 | ||
268 | config_reset(); | 285 | snprintf(savefile, sizeof(savefile), "Config saved to your home directory at %s/Laserharfe-%s.cfg", homeDir, date); |
269 | g_string_count = result_count; | 286 | display_messagebox("Config saved", savefile); |
270 | 287 | ||
271 | g_min_y = 0; g_max_y = 1024; | 288 | } |
272 | 289 | ||
273 | for (i=0; i<g_string_count; ++i) { | 290 | void config_import() { |
274 | LLine *l = result + i; | 291 | fprintf( stderr, "import config\n" ); |
292 | if (g_harfe_connected) { | ||
293 | config_reset(); | ||
294 | g_importing_config = 1; | ||
295 | write(g_harfe_fd, "I\n", 2); | ||
296 | } else | ||
297 | display_messagebox( "Not connected", "Can't import config if Harfe is not connected."); | ||
298 | } | ||
275 | 299 | ||
276 | if (l->p0.y > g_min_y) | 300 | void config_export() { |
277 | g_min_y = l->p0.y; | 301 | size_t len; |
278 | if (l->p1.y < g_max_y) | 302 | char confdump[512]; |
279 | g_max_y = l->p1.y; | 303 | int i; |
304 | if (!g_harfe_connected) { | ||
305 | display_messagebox( "Not connected", "Can't write config if Harfe is not connected."); | ||
306 | return; | ||
307 | } | ||
308 | if ((int)g_config_source < 2) { | ||
309 | display_messagebox( "No changes", "Config is unchanged. Won't write config to Harfe."); | ||
310 | return; | ||
311 | } | ||
312 | write(g_harfe_fd, "E\n", 2); | ||
313 | len = config_dumpglobals( confdump, sizeof(confdump)); | ||
314 | write(g_harfe_fd, confdump, len ); | ||
315 | for (i=0; i<g_string_count; ++i) { | ||
316 | len = config_dumpstring(i, confdump, sizeof(confdump)); | ||
317 | write(g_harfe_fd, confdump, len ); | ||
318 | usleep(1000); | ||
319 | } | ||
320 | write(g_harfe_fd, "-- DONE\n", 8); /* End dump marker */ | ||
321 | write(g_harfe_fd, "W\n", 2); /* Remote write to SD */ | ||
322 | g_config_source = source_harfe; | ||
323 | } | ||
280 | 324 | ||
281 | g_string_conf[i].line = *l; | 325 | void |
282 | g_string_conf[i].mode = midi_three_octaves; | 326 | handle_keydown(SDL_Event *ev) { |
283 | g_string_conf[i].channel = i; | ||
284 | g_string_conf[i].note = default_notes[i]; | ||
285 | g_string_conf[i].modifier = pitch_bend_up; | ||
286 | } | ||
287 | 327 | ||
288 | g_config_source = source_edit; | ||
289 | } | 328 | } |
290 | 329 | ||
291 | int | 330 | int |
@@ -351,54 +390,21 @@ main(int argc, char **argv) | |||
351 | if ( ev.key.repeat || ! g_down_pressed++) | 390 | if ( ev.key.repeat || ! g_down_pressed++) |
352 | engine_change_selected(-1); | 391 | engine_change_selected(-1); |
353 | 392 | ||
354 | if ( ev.key.keysym.scancode == SDL_SCANCODE_S) { /* export locally */ | 393 | if ( ev.key.keysym.scancode == SDL_SCANCODE_S) |
355 | const char *homeDir = getenv("HOME"); | 394 | config_save(); |
356 | char savefile[512], date[32], confdump[512]; | ||
357 | time_t t = time(NULL); | ||
358 | struct tm *tmp = localtime(&t); | ||
359 | int fd; | ||
360 | size_t len; | ||
361 | |||
362 | if (!homeDir) { | ||
363 | struct passwd* pwd = getpwuid(getuid()); | ||
364 | if (pwd) | ||
365 | homeDir = (const char*)pwd->pw_dir; | ||
366 | } | ||
367 | strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", tmp); | ||
368 | snprintf( savefile, sizeof(savefile), "%s/Laserharfe-%s.cfg", homeDir, date); | ||
369 | fd = open(savefile, O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
370 | |||
371 | len = config_dumpglobals( confdump, sizeof(confdump)); | ||
372 | write(fd, confdump, len ); | ||
373 | for (i=0; i<g_string_count; ++i) { | ||
374 | len = config_dumpstring(i, confdump, sizeof(confdump)); | ||
375 | write(fd, confdump, len ); | ||
376 | } | ||
377 | close(fd); | ||
378 | } | ||
379 | if (ev.key.keysym.scancode == SDL_SCANCODE_W) { | 395 | if (ev.key.keysym.scancode == SDL_SCANCODE_W) { |
380 | fprintf( stderr, "write remote config\n" ); | 396 | fprintf( stderr, "write remote config\n" ); |
381 | write(g_harfe_fd, "W\n", 2); | 397 | write(g_harfe_fd, "W\n", 2); |
382 | } | 398 | } |
383 | if (ev.key.keysym.scancode == SDL_SCANCODE_I) { | 399 | if (ev.key.keysym.scancode == SDL_SCANCODE_I) |
384 | fprintf( stderr, "dump config\n" ); | 400 | config_import(); |
385 | config_reset(); | ||
386 | g_importing_config = 1; | ||
387 | write(g_harfe_fd, "I\n", 2); | ||
388 | } | ||
389 | if (ev.key.keysym.scancode == SDL_SCANCODE_Q) | 401 | if (ev.key.keysym.scancode == SDL_SCANCODE_Q) |
390 | config_reset(); | 402 | config_reset(); |
391 | if (ev.key.keysym.scancode == SDL_SCANCODE_L) | 403 | if (ev.key.keysym.scancode == SDL_SCANCODE_L) |
392 | config_parse("config_midi"); | 404 | config_parse("config_midi"); |
405 | if (ev.key.keysym.scancode == SDL_SCANCODE_X) | ||
406 | config_reverse_strings(); | ||
393 | if (ev.key.keysym.scancode == SDL_SCANCODE_Y) { | 407 | if (ev.key.keysym.scancode == SDL_SCANCODE_Y) { |
394 | for (i=0; i<g_string_count/2; ++i) { | ||
395 | LLine temp = g_string_conf[i].line; | ||
396 | g_string_conf[i].line = g_string_conf[g_string_count-i-1].line; | ||
397 | g_string_conf[g_string_count-i-1].line = temp; | ||
398 | } | ||
399 | g_config_source = source_edit; | ||
400 | } | ||
401 | if (ev.key.keysym.scancode == SDL_SCANCODE_X) { | ||
402 | g_midi_three_octave_split_inverse ^= 1; | 408 | g_midi_three_octave_split_inverse ^= 1; |
403 | g_config_source = source_edit; | 409 | g_config_source = source_edit; |
404 | } | 410 | } |
@@ -407,34 +413,34 @@ main(int argc, char **argv) | |||
407 | if (g_calibration_running) | 413 | if (g_calibration_running) |
408 | calib_next(10); | 414 | calib_next(10); |
409 | #endif | 415 | #endif |
410 | if ( ev.key.keysym.scancode == SDL_SCANCODE_C) { | 416 | if ( ev.key.keysym.scancode == SDL_SCANCODE_RETURN) |
417 | menu_setmode(1, 1); | ||
418 | if ( ev.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { | ||
419 | if (g_importing_config) { | ||
420 | g_importing_config = 0; | ||
421 | config_reset(); | ||
422 | } | ||
423 | calib_stop(); | ||
424 | menu_setmode(6, 0); | ||
425 | } | ||
426 | if ( ev.key.keysym.sym == SDLK_GREATER || ev.key.keysym.scancode == SDL_SCANCODE_NONUSBACKSLASH) { | ||
427 | menu_setmode(2,2); | ||
428 | } | ||
429 | if ( ev.key.keysym.scancode == SDL_SCANCODE_R) { | ||
411 | if (g_calibration_running) | 430 | if (g_calibration_running) |
412 | calib_fetch(); | ||
413 | else | ||
414 | calib_init(); | 431 | calib_init(); |
415 | g_calibration_running ^= 1; | ||
416 | } | 432 | } |
417 | if ( ev.key.keysym.scancode == SDL_SCANCODE_E) { | 433 | if ( ev.key.keysym.scancode == SDL_SCANCODE_C) { |
418 | char confdump[512]; | 434 | if (g_calibration_running) { |
419 | if (!g_harfe_connected) { | 435 | calib_fetch(); |
420 | display_messagebox( "Not connected", "Can't write config if Harfe is not connected."); | 436 | menu_setmode(1<<3, 0); |
421 | break; | 437 | } else { |
422 | } | 438 | calib_init(); |
423 | if ((int)g_config_source < 2) { | 439 | menu_setmode(0, 3); |
424 | display_messagebox( "No changes", "Config is unchanged. Won't write config to Harfe."); | ||
425 | break; | ||
426 | } | ||
427 | write(g_harfe_fd, "E\n", 2); | ||
428 | size_t len = config_dumpglobals( confdump, sizeof(confdump)); | ||
429 | write(g_harfe_fd, confdump, len ); | ||
430 | for (i=0; i<g_string_count; ++i) { | ||
431 | len = config_dumpstring(i, confdump, sizeof(confdump)); | ||
432 | write(g_harfe_fd, confdump, len ); | ||
433 | } | 440 | } |
434 | write(g_harfe_fd, "-- DONE\n", 8); /* End dump marker */ | ||
435 | write(g_harfe_fd, "W\n", 2); /* Remote write to SD */ | ||
436 | g_config_source = source_harfe; | ||
437 | } | 441 | } |
442 | if ( ev.key.keysym.scancode == SDL_SCANCODE_E) | ||
443 | config_export(); | ||
438 | if ( ev.key.keysym.scancode == SDL_SCANCODE_M) { | 444 | if ( ev.key.keysym.scancode == SDL_SCANCODE_M) { |
439 | fprintf( stderr, "MIDIing on %d\n", g_harfe_fd ); | 445 | fprintf( stderr, "MIDIing on %d\n", g_harfe_fd ); |
440 | if (g_harfe_connected && (g_harfe_fd != -1)) | 446 | if (g_harfe_connected && (g_harfe_fd != -1)) |
@@ -449,18 +455,19 @@ main(int argc, char **argv) | |||
449 | } | 455 | } |
450 | break; | 456 | break; |
451 | case SDL_MOUSEBUTTONDOWN: | 457 | case SDL_MOUSEBUTTONDOWN: |
452 | /* | ||
453 | if ( ( g_last_mouse_event / 1000 ) != ( engine_now( ) / 1000 ) || ev.button.x != last_click_x || ev.button.y != last_click_y ) | ||
454 | engine_process_mouse( ev.button.x, ev.button.y ); | ||
455 | last_click_x = ev.button.x; | ||
456 | last_click_y = ev.button.y; | ||
457 | last_mouse_event = engine_now( ); | ||
458 | */ | ||
459 | { | 458 | { |
460 | LPoint p = { display_scale_screen_to_harfe(ev.button.x), 768 - display_scale_screen_to_harfe(ev.button.y) }; | 459 | LPoint p = { display_scale_screen_to_harfe(ev.button.x), 768 - display_scale_screen_to_harfe(ev.button.y) }; |
461 | engine_handle_point(&p, now()); | 460 | if (menu_test_mouse_down(ev.button.x, ev.button.y)) |
461 | engine_handle_point(&p, now()); | ||
462 | } | 462 | } |
463 | break; | 463 | break; |
464 | case SDL_MOUSEBUTTONUP: | ||
465 | { | ||
466 | LPoint p = { display_scale_screen_to_harfe(ev.button.x), 768 - display_scale_screen_to_harfe(ev.button.y) }; | ||
467 | menu_handle_button_up(ev.button.x, ev.button.y); | ||
468 | } | ||
469 | |||
470 | break; | ||
464 | case SDL_DROPFILE: { | 471 | case SDL_DROPFILE: { |
465 | char t[512]; | 472 | char t[512]; |
466 | int ret; | 473 | int ret; |
@@ -476,7 +483,7 @@ main(int argc, char **argv) | |||
476 | engine_checksilence(now()); | 483 | engine_checksilence(now()); |
477 | 484 | ||
478 | runtime = now(); | 485 | runtime = now(); |
479 | if (runtime - g_lastredraw > 30 && !g_importing_config) { | 486 | if (runtime - g_lastredraw > 30) { |
480 | g_lastredraw = runtime; | 487 | g_lastredraw = runtime; |
481 | 488 | ||
482 | if (!g_calibration_running) | 489 | if (!g_calibration_running) |