From c6cd6522a6bdf0f6ad6753e106f5fb53756eeb94 Mon Sep 17 00:00:00 2001 From: Duilio Protti Date: Thu, 30 Jun 2016 15:00:15 -0300 Subject: [PATCH] Show SDL errors on player UI --- src/Makefile.am | 2 +- src/audacious.cc | 6 ++++++ src/compute.h | 2 +- src/display.c | 49 +++++++++++++++++++++++++++++++--------------- src/display.h | 16 ++++++++------- src/effects.h | 6 ++---- src/infinity.c | 26 ++++++++++++++++++------ src/infinity.h | 18 +---------------- src/music-player.h | 40 +++++++++++++++++++++++++++++++++++++ 9 files changed, 113 insertions(+), 52 deletions(-) create mode 100644 src/music-player.h diff --git a/src/Makefile.am b/src/Makefile.am index ec4e020..e4ec6d9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,6 +21,6 @@ libinfinite_la_SOURCES = \ display.c display.h \ effects.c effects.h\ cputest.c cputest.h\ - mmx.h types.h + mmx.h music-player.h types.h EXTRA_DIST = infinite_states diff --git a/src/audacious.cc b/src/audacious.cc index c24a2c5..6fca3a0 100644 --- a/src/audacious.cc +++ b/src/audacious.cc @@ -15,6 +15,7 @@ */ #include #include +#include #include #include #include @@ -166,6 +167,10 @@ static void adjust_volume(gint delta) { aud_drct_set_volume_main(volume + delta); } +static void notify_critical_error (const gchar *message) { + aud_ui_show_error(message); +} + static void disable_plugin() { PluginHandle * plugin = aud_plugin_lookup_basename("libinfinite"); aud_plugin_enable(plugin, false); @@ -181,6 +186,7 @@ static Player player = { .next = next, .seek = seek, .adjust_volume = adjust_volume, + .notify_critical_error = notify_critical_error, .disable_plugin = disable_plugin }; diff --git a/src/compute.h b/src/compute.h index 35298df..1b7af0f 100644 --- a/src/compute.h +++ b/src/compute.h @@ -16,6 +16,7 @@ #ifndef __INFINITY_COMPUTE__ #define __INFINITY_COMPUTE__ +#include #include "types.h" #define NB_FCT 7 @@ -74,5 +75,4 @@ void compute_generate_vector_field(vector_field_t *vector_field); byte *compute_surface(t_interpol *vector, gint32 width, gint32 height); byte *compute_surface_mmx(t_interpol *vector, gint32 width, gint32 height); - #endif /* __INFINITY_COMPUTE__ */ diff --git a/src/display.c b/src/display.c index 75aab85..866a2d2 100644 --- a/src/display.c +++ b/src/display.c @@ -49,17 +49,26 @@ static SDL_Color color_table[NB_PALETTES][256]; static gint16 current_colors[256]; static byte *surface1; +static Player *player; -static void init_sdl(gint32 _width, gint32 _height, gint32 _scale) +static gchar error_msg[256]; + +static gboolean sdl_init(gint32 _width, gint32 _height, gint32 _scale) { - if (SDL_Init((Uint32)(SDL_INIT_VIDEO | SDL_INIT_TIMER)) < 0) - g_error("Infinity: Couldn't initialize SDL: %s\n", SDL_GetError()); + if (SDL_Init((Uint32)(SDL_INIT_VIDEO | SDL_INIT_TIMER)) < 0) { + g_snprintf(error_msg, 256, "Infinity cannot initialize SDL: %s", SDL_GetError()); + player->notify_critical_error(error_msg); + return FALSE; + } screen = SDL_SetVideoMode(_width * _scale, _height * _scale, 16, VIDEO_FLAGS); - if (screen == NULL) - g_error("Infinity: could not init video mode: %s\n", SDL_GetError()); - g_message("Infinity: SDL SetVideoMode() Ok"); + if (screen == NULL) { + g_snprintf(error_msg, 256, "Infinity cannot create display: %s", SDL_GetError()); + player->notify_critical_error(error_msg); + return FALSE; + } (void)SDL_ShowCursor(0); (void)SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); + return TRUE; } static void generate_colors() @@ -208,19 +217,26 @@ static void line(gint32 x1, gint32 y1, gint32 x2, gint32 y2, gint32 c) } } -void display_init(gint32 _width, gint32 _height, gint32 _scale) +gboolean display_init(gint32 _width, gint32 _height, gint32 _scale, Player *_player) { + gboolean sdl_ok; + width = _width; height = _height; scale = _scale; + player = _player; pcm_data_mutex = SDL_CreateMutex(); compute_init(width, height, scale); - init_sdl(width, height, scale); + sdl_ok = sdl_init(width, height, scale); generate_colors(); effects_load_effects(); vector_field = compute_vector_field_new(width, height); compute_generate_vector_field(vector_field); + if (!sdl_ok) { + display_quit(); + } + return sdl_ok; } void display_quit(void) @@ -234,21 +250,22 @@ void display_quit(void) SDL_Quit(); } -void display_resize(gint32 _width, gint32 _height) +gboolean display_resize(gint32 _width, gint32 _height) { width = _width; height = _height; - screen = SDL_SetVideoMode(width * scale, - height * scale, - 16, VIDEO_FLAGS); - if (screen == NULL) - g_error("Infinity: Couldn't set %dx%d video mode: %s\n", - width * scale, height * scale, - SDL_GetError()); + screen = SDL_SetVideoMode(width * scale, height * scale, 16, VIDEO_FLAGS); + if (screen == NULL) { + g_snprintf(error_msg, 256, "Infinity cannot resize display to %dx%d pixels: %s", + width * scale, height * scale, SDL_GetError()); + player->notify_critical_error(error_msg); + return FALSE; + } compute_vector_field_destroy(vector_field); vector_field = compute_vector_field_new(width, height); compute_resize(width, height); compute_generate_vector_field(vector_field); + return TRUE; } inline void display_set_pcm_data(const float *data, int channels) diff --git a/src/display.h b/src/display.h index f32a80f..a839af5 100644 --- a/src/display.h +++ b/src/display.h @@ -21,19 +21,19 @@ #include "compute.h" #include "effects.h" +#include "music-player.h" #define NB_PALETTES 5 /* - * Initializes the display related structures. + * Initializes the display related structures, including the SDL library. * - * It initializes the module and the SDL library. + * Warning: must be called before any SDL operation and must not be + * called when SDL was already started. * - * Warning: because this function initializes the SDL - * library, must be called before any SDL operation and - * must not be called when SDL was already started. + * Returns true on success; and false otherwise. */ -void display_init(gint32 width, gint32 height, gint32 scale); +gboolean display_init(gint32 _width, gint32 _height, gint32 _scale, Player *player); /* * Closes the display module. @@ -53,8 +53,10 @@ void display_quit(void); /* * Change the size of the display to the new dimension * width x height. + * + * Returns true on success; and false otherwise. */ -void display_resize(gint32 width, gint32 height); +gboolean display_resize(gint32 width, gint32 height); /* * Set data as the data PCM data of this module. diff --git a/src/effects.h b/src/effects.h index 48dd900..b4f0019 100644 --- a/src/effects.h +++ b/src/effects.h @@ -16,9 +16,8 @@ #ifndef __INFINITY_EFFECTS__ #define __INFINITY_EFFECTS__ - -#include - +#include +#include "types.h" /* * Represents effect related information. @@ -52,5 +51,4 @@ void effects_save_effect(t_effect *effect); void effects_load_effects(void); void effects_load_random_effect(t_effect *effect); - #endif /* __INFINITY_EFFECTS__ */ diff --git a/src/infinity.c b/src/infinity.c index a06c2ad..052482c 100644 --- a/src/infinity.c +++ b/src/infinity.c @@ -72,7 +72,7 @@ void infinity_init(InfParameters * _params, Player * _player) gint32 _try; if (initializing) { - g_warning("We are already initializing"); + g_warning("Infinity: is already initializing..."); _try = 0; while (initializing) { g_usleep(1000000); @@ -88,6 +88,14 @@ void infinity_init(InfParameters * _params, Player * _player) height = params->get_height(); scale = params->get_scale(); + if (! display_init(width, height, scale, player)) { + g_critical("Infinity: cannot initialize display"); + initializing = FALSE; + finished = TRUE; + player->disable_plugin(); + return; + } + old_color = 0; color = 0; @@ -100,7 +108,6 @@ void infinity_init(InfParameters * _params, Player * _player) quiting = FALSE; first_xevent = TRUE; - display_init(width, height, scale); current_title = g_strdup("Infinity"); set_title(); title_timer = g_timer_new(); @@ -214,6 +221,12 @@ static gint disable_func(gpointer data) return FALSE; } +static void schedule_exit() { + GDK_THREADS_ENTER(); + (void)gtk_idle_add(disable_func, NULL); + GDK_THREADS_LEAVE(); +} + static void check_events() { SDL_Event event; @@ -268,9 +281,7 @@ static void check_events() * } * break;*/ case SDL_QUIT: - GDK_THREADS_ENTER(); - (void)gtk_idle_add(disable_func, NULL); - GDK_THREADS_LEAVE(); + schedule_exit(); break; case SDL_VIDEORESIZE: g_return_if_fail(SDL_LockMutex(resizing_mutex) >= 0); @@ -413,7 +424,10 @@ static int renderer(void *arg) if (finished) break; if (must_resize) { - display_resize(width, height); + if (! display_resize(width, height)) { + schedule_exit(); + break; + } params->set_width(width); params->set_height(height); must_resize = FALSE; diff --git a/src/infinity.h b/src/infinity.h index 0f8ac18..8f6d9c4 100644 --- a/src/infinity.h +++ b/src/infinity.h @@ -17,6 +17,7 @@ #define __INFINITY_INFINITY__ #include +#include "music-player.h" typedef struct _InfParameters { gint32 (*get_x_origin) (void); @@ -33,23 +34,6 @@ typedef struct _InfParameters { gint32 (*get_max_fps) (void); } InfParameters; -typedef struct _Player { - - gboolean (*is_playing) (void); - gchar* (*get_title) (void); - - void (*play) (void); - void (*pause) (void); - void (*stop) (void); - void (*previous) (void); - void (*next) (void); - void (*seek) (gint32 usecs); - void (*adjust_volume) (gint delta); - - void (*disable_plugin) (void); /* tell the player that this plugin has stopped */ - -} Player; - /* * Initializes rendering process. * diff --git a/src/music-player.h b/src/music-player.h new file mode 100644 index 0000000..06a8d95 --- /dev/null +++ b/src/music-player.h @@ -0,0 +1,40 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +#ifndef __INFINITY_PLAYER__ +#define __INFINITY_PLAYER__ + +/* + * Interface that Infinity uses to talk with the hosting music player. + */ +typedef struct _Player { + + gboolean (*is_playing) (void); + gchar* (*get_title) (void); + + void (*play) (void); + void (*pause) (void); + void (*stop) (void); + void (*previous) (void); + void (*next) (void); + void (*seek) (gint32 usecs); + void (*adjust_volume) (gint delta); + + void (*notify_critical_error) (const gchar *message); + void (*disable_plugin) (void); /* tell the player that this plugin has stopped */ + +} Player; + +#endif /* __INFINITY_PLAYER__ */