From f86a8bce2266bbc26bf7f8477777a37b45a88216 Mon Sep 17 00:00:00 2001 From: Duilio Protti Date: Fri, 1 Jul 2016 16:37:17 -0300 Subject: [PATCH] Interactive mode saves effects file in home dir instead of system dir --- configure.ac | 2 +- src/display.c | 34 +++++---- src/effects.c | 27 ++++--- src/effects.h | 35 +++++---- src/infinity.c | 194 ++++++++++++++++++++++++------------------------- 5 files changed, 151 insertions(+), 141 deletions(-) diff --git a/configure.ac b/configure.ac index 2d7776f..d2853a1 100644 --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,7 @@ ## Process this file with autoconf to produce a configure script. AC_PREREQ(2.65) -AC_INIT([Infinity plugin],[0.9.0alpha2],[https://github.com/dprotti/infinity-plugin/issues],[infinity-plugin],[https://dprotti.github.io/infinity-plugin]) +AC_INIT([Infinity plugin],[0.9.0-dev],[https://github.com/dprotti/infinity-plugin/issues],[infinity-plugin],[https://dprotti.github.io/infinity-plugin]) AC_CANONICAL_HOST AC_CANONICAL_TARGET diff --git a/src/display.c b/src/display.c index e44883f..d6d10c0 100644 --- a/src/display.c +++ b/src/display.c @@ -222,26 +222,35 @@ static void line(gint32 x1, gint32 y1, gint32 x2, gint32 y2, gint32 c) } } +static void sdl_quit() { + if (screen != NULL) + SDL_FreeSurface(screen); + screen = NULL; + if (window != NULL) + SDL_DestroyWindow(window); + window = NULL; +} + gboolean display_init(gint32 _width, gint32 _height, gint32 _scale, Player *_player) { - gboolean sdl_ok; - width = _width; height = _height; scale = _scale; player = _player; + if (! effects_load_effects(player)) { + return FALSE; + } + if (! sdl_init()) { + sdl_quit(); + return FALSE; + } compute_init(width, height, scale); - sdl_ok = sdl_init(); generate_colors(); - effects_load_effects(); vector_field = compute_vector_field_new(width, height); compute_generate_vector_field(vector_field); - if (!sdl_ok) { - display_quit(); - } initialized = TRUE; - return sdl_ok; + return TRUE; } void display_quit(void) @@ -250,12 +259,7 @@ void display_quit(void) return; compute_vector_field_destroy(vector_field); compute_quit(); - if (screen != NULL) - SDL_FreeSurface(screen); - screen = NULL; - if (window != NULL) - SDL_DestroyWindow(window); - window = NULL; + sdl_quit(); // do not call SDL_Quit() since there is another plugin that use // SDL inside Audacious. // Do not call neither SDL_QuitSubsystem() just in case @@ -480,7 +484,7 @@ void display_save_screen(void) inline void display_save_effect(t_effect *effect) { - effects_save_effect(effect); + effects_append_effect(effect); } diff --git a/src/effects.c b/src/effects.c index 72e6e54..721531f 100644 --- a/src/effects.c +++ b/src/effects.c @@ -10,35 +10,43 @@ static t_effect effects[100]; static gint32 nb_effects = 0; static gboolean initialized = FALSE; +static gchar error_msg[256]; -void effects_save_effect(t_effect *effect) +void effects_append_effect(t_effect *effect) { FILE *f; gint32 i; + gchar *personal_states = g_strconcat(g_get_home_dir(), "/infinite_states", NULL); - g_assert(effect); - f = fopen (EFFECTS_FILE,"a"); + g_return_if_fail(effect != NULL); + + f = fopen (personal_states, "a"); if (f == NULL) { - g_warning ("Cannot open file %s for saving effects\n", - EFFECTS_FILE); + g_critical("Cannot open file '%s' for saving effects", personal_states); + g_free(personal_states); return; } + g_message("Infinity appended effect to '%s'", personal_states); + g_free(personal_states); for (i = 0; i < sizeof(t_effect); i++) fputc(*((byte *)effect + i), f); fclose(f); } -void effects_load_effects(void) +gboolean effects_load_effects(Player *player) { FILE *f; gint32 finished = 0; gint32 i, b, c, d, e; - f = fopen (EFFECTS_FILE,"r"); + g_return_val_if_fail(player != NULL, FALSE); + + f = fopen (EFFECTS_FILE, "r"); if (f == NULL) { - g_warning ("Cannot open file %s for loading effects\n", + g_snprintf(error_msg, 256, "Cannot open file '%s' for loading effects", EFFECTS_FILE); - return; + player->notify_critical_error(error_msg); + return FALSE; } while (!finished) { byte *ptr_effect = (byte *)&effects[nb_effects]; @@ -70,6 +78,7 @@ void effects_load_effects(void) } nb_effects--; fclose(f); + return TRUE; } void effects_load_random_effect(t_effect *effect) diff --git a/src/effects.h b/src/effects.h index b4f0019..21252d3 100644 --- a/src/effects.h +++ b/src/effects.h @@ -17,6 +17,7 @@ #define __INFINITY_EFFECTS__ #include +#include "music-player.h" #include "types.h" /* @@ -26,29 +27,31 @@ * would be enough for some of them. */ typedef struct { - gint32 num_effect; /* The number of the effect */ - gint32 x_curve; - gint32 curve_color; - gint32 curve_amplitude; - gint32 spectral_amplitude; - gint32 spectral_color; - gint32 mode_spectre; - gint32 spectral_shift; + gint32 num_effect; /* The number of the effect */ + gint32 x_curve; + gint32 curve_color; + gint32 curve_amplitude; + gint32 spectral_amplitude; + gint32 spectral_color; + gint32 mode_spectre; + gint32 spectral_shift; } t_effect; /* - * Saves the given effect pointed by \a effect to disk. - * - * The effect are saved to the file - * {prefix}/share/xmms/infinity_states, where {prefix} is - * usually /usr or /usr/local. + * Appends effect to file ~/infinite_states * * @param effect Must be a non NULL reference to a ::t_effect * object. */ -void effects_save_effect(t_effect *effect); +void effects_append_effect (t_effect *effect); -void effects_load_effects(void); -void effects_load_random_effect(t_effect *effect); +/* + * Loads effects from file {DATADIR}/infinite_states + * + * Returns TRUE on success or FALSE otherwise. + */ +gboolean effects_load_effects (Player *player); + +void effects_load_random_effect (t_effect *effect); #endif /* __INFINITY_EFFECTS__ */ diff --git a/src/infinity.c b/src/infinity.c index 7ea24ab..b1e8fef 100644 --- a/src/infinity.c +++ b/src/infinity.c @@ -53,7 +53,7 @@ G_LOCK_DEFINE_STATIC(resizing); static gboolean initializing = FALSE; static gboolean visible; static gboolean quiting; -static gboolean mode_interactif; +static gboolean interactive_mode; static gboolean first_xevent; static gchar *current_title; static GTimer *title_timer; @@ -99,7 +99,7 @@ void infinity_init(InfParameters * _params, Player * _player) finished = FALSE; must_resize = FALSE; resizing = FALSE; - mode_interactif = FALSE; + interactive_mode = FALSE; visible = TRUE; quiting = FALSE; first_xevent = TRUE; @@ -156,56 +156,90 @@ void infinity_render_multi_pcm(const float *data, int channels) display_set_pcm_data(data, channels); } +#ifdef INFINITY_DEBUG +static void handle_interactive_mode() { + gint32 i; + gint32 sx, sy; + const byte *keystate = SDL_GetKeyboardState(NULL); + + SDL_GetMouseState(&sx, &sy); + current_effect.spectral_shift = sx; + if (keystate[SDL_SCANCODE_A]) + current_effect.curve_color = wrap(current_effect.curve_color - 32); + if (keystate[SDL_SCANCODE_Z]) + current_effect.curve_color = wrap(current_effect.curve_color + 32); + if (keystate[SDL_SCANCODE_Q]) + current_effect.spectral_color = wrap(current_effect.spectral_color - 32); + if (keystate[SDL_SCANCODE_S]) + current_effect.spectral_color = wrap(current_effect.spectral_color + 32); + for (i = 0; i < 10; i++) + if (keystate[SDL_SCANCODE_F1 + i]) + current_effect.num_effect = i % NB_FCT; + if (keystate[SDL_SCANCODE_D]) + current_effect.spectral_amplitude = (current_effect.spectral_amplitude - 1); + if (keystate[SDL_SCANCODE_F]) + current_effect.spectral_amplitude = (current_effect.spectral_amplitude + 1); + if (keystate[SDL_SCANCODE_E]) + current_effect.curve_amplitude = (current_effect.curve_amplitude - 1); + if (keystate[SDL_SCANCODE_R]) + current_effect.curve_amplitude = (current_effect.curve_amplitude + 1); + if (keystate[SDL_SCANCODE_M]) + display_save_effect(¤t_effect); + if (keystate[SDL_SCANCODE_W]) + current_effect.mode_spectre = (current_effect.mode_spectre + 1) % 5; +} +#endif /* INFINITY_DEBUG */ + static void handle_window_event(SDL_Event *event) { switch (event->window.event) { case SDL_WINDOWEVENT_SHOWN: - //SDL_Log("Window %d shown", event->window.windowID); - visible = TRUE; - break; - case SDL_WINDOWEVENT_EXPOSED: - //SDL_Log("Window %d exposed", event->window.windowID); - visible = TRUE; - break; - case SDL_WINDOWEVENT_HIDDEN: - //SDL_Log("Window %d hidden", event->window.windowID); - visible = FALSE; - break; - /*case SDL_WINDOWEVENT_MOVED: - SDL_Log("Window %d moved to %d,%d", - event->window.windowID, event->window.data1, - event->window.data2); - break;*/ - case SDL_WINDOWEVENT_RESIZED: - G_LOCK(resizing); + //SDL_Log("Window %d shown", event->window.windowID); + visible = TRUE; + break; + case SDL_WINDOWEVENT_EXPOSED: + //SDL_Log("Window %d exposed", event->window.windowID); + visible = TRUE; + break; + case SDL_WINDOWEVENT_HIDDEN: + //SDL_Log("Window %d hidden", event->window.windowID); + visible = FALSE; + break; + /*case SDL_WINDOWEVENT_MOVED: + SDL_Log("Window %d moved to %d,%d", + event->window.windowID, event->window.data1, + event->window.data2); + break;*/ + case SDL_WINDOWEVENT_RESIZED: + G_LOCK(resizing); resizing = TRUE; G_UNLOCK(resizing); width = event->window.data1; height = event->window.data2; g_message("Infinity: Screen resized to %dx%d pixels^2", width, height); must_resize = TRUE; - break; - /*case SDL_WINDOWEVENT_SIZE_CHANGED: - SDL_Log("Window %d size changed to %dx%d", - event->window.windowID, event->window.data1, - event->window.data2); - break;*/ - case SDL_WINDOWEVENT_MINIMIZED: - //SDL_Log("Window %d minimized", event->window.windowID); - visible = FALSE; - break; - /*case SDL_WINDOWEVENT_MAXIMIZED: - SDL_Log("Window %d maximized", event->window.windowID); - break; - case SDL_WINDOWEVENT_RESTORED: - SDL_Log("Window %d restored", event->window.windowID); - break;*/ - case SDL_WINDOWEVENT_CLOSE: - SDL_Log("Window %d closed", event->window.windowID); - player->disable_plugin(); - break; - default: - break; - } + break; + /*case SDL_WINDOWEVENT_SIZE_CHANGED: + SDL_Log("Window %d size changed to %dx%d", + event->window.windowID, event->window.data1, + event->window.data2); + break;*/ + case SDL_WINDOWEVENT_MINIMIZED: + //SDL_Log("Window %d minimized", event->window.windowID); + visible = FALSE; + break; + /*case SDL_WINDOWEVENT_MAXIMIZED: + SDL_Log("Window %d maximized", event->window.windowID); + break; + case SDL_WINDOWEVENT_RESTORED: + SDL_Log("Window %d restored", event->window.windowID); + break;*/ + case SDL_WINDOWEVENT_CLOSE: + SDL_Log("Window %d closed", event->window.windowID); + player->disable_plugin(); + break; + default: + break; + } } static void check_events() @@ -231,11 +265,9 @@ static void check_events() while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: - player->disable_plugin(); - break; + player->disable_plugin(); break; case SDL_WINDOWEVENT: - handle_window_event(&event); - break; + handle_window_event(&event); break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_RIGHT: @@ -247,32 +279,23 @@ static void check_events() player->seek(-5000); break; case SDLK_UP: - player->adjust_volume(5); - break; + player->adjust_volume(5); break; case SDLK_DOWN: - player->adjust_volume(-5); - break; + player->adjust_volume(-5); break; case SDLK_TAB: - display_toggle_fullscreen(); - break; + display_toggle_fullscreen(); break; case SDLK_z: - player->previous(); - break; + player->previous(); break; case SDLK_x: - player->play(); - break; + player->play(); break; case SDLK_c: - player->pause(); - break; + player->pause(); break; case SDLK_v: - player->stop(); - break; + player->stop(); break; case SDLK_b: - player->next(); - break; + player->next(); break; case SDLK_F11: - display_save_screen(); - break; + display_save_screen(); break; case SDLK_F12: if (t_last_color > 32) { t_last_color = 0; @@ -286,7 +309,8 @@ static void check_events() break; #ifdef INFINITY_DEBUG case SDLK_RETURN: - mode_interactif = !mode_interactif; + interactive_mode = !interactive_mode; + g_message("Infinity %s interactive mode", interactive_mode ? "entered" : "leaved"); break; #endif default: @@ -298,38 +322,8 @@ static void check_events() } } #ifdef INFINITY_DEBUG - if (mode_interactif) { - gint32 i; - gint32 sx, sy; - byte *keystate; - - keystate = SDL_GetKeyState(NULL); - SDL_GetMouseState(&sx, &sy); - current_effect.spectral_shift = sx; - if (keystate[SDLK_a]) - current_effect.curve_color = wrap(current_effect.curve_color - 32); - if (keystate[SDLK_z]) - current_effect.curve_color = wrap(current_effect.curve_color + 32); - if (keystate[SDLK_q]) - current_effect.spectral_color = wrap(current_effect.spectral_color - 32); - if (keystate[SDLK_s]) - current_effect.spectral_color = wrap(current_effect.spectral_color + 32); - for (i = 0; i < 10; i++) - if (keystate[SDLK_F1 + i]) - current_effect.num_effect = i % NB_FCT; - if (keystate[SDLK_d]) - current_effect.spectral_amplitude = (current_effect.spectral_amplitude - 1); - if (keystate[SDLK_f]) - current_effect.spectral_amplitude = (current_effect.spectral_amplitude + 1); - if (keystate[SDLK_e]) - current_effect.curve_amplitude = (current_effect.curve_amplitude - 1); - if (keystate[SDLK_r]) - current_effect.curve_amplitude = (current_effect.curve_amplitude + 1); - if (keystate[SDLK_m]) - display_save_effect(¤t_effect); - if (keystate[SDLK_w]) - current_effect.mode_spectre = (current_effect.mode_spectre + 1) % 5; - } + if (interactive_mode) + handle_interactive_mode(); #endif /* INFINITY_DEBUG */ } @@ -392,7 +386,7 @@ static int renderer(void *arg) next_effect(); if (t_last_effect % t_between_effects == 0) { #ifdef INFINITY_DEBUG - if (!mode_interactif) { + if (!interactive_mode) { display_load_random_effect(¤t_effect); t_last_effect = 0; t_between_effects = params->get_effect_interval(); @@ -405,7 +399,7 @@ static int renderer(void *arg) } if (t_last_color % t_between_colors == 0) { #ifdef INFINITY_DEBUG - if (!mode_interactif) { + if (!interactive_mode) { old_color = color; color = rand() % NB_PALETTES; t_last_color = 0;