Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2003:
[Freeciv-Dev] audio hotswap v3 (PR#2319)
Home

[Freeciv-Dev] audio hotswap v3 (PR#2319)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] audio hotswap v3 (PR#2319)
From: "Per I. Mathisen via RT" <rt@xxxxxxxxxxxxxx>
Date: Mon, 20 Jan 2003 07:24:40 -0800
Reply-to: rt@xxxxxxxxxxxxxx

This implements hot swap of audio plugins from within the local options
dialog. It now also works :-)

I haven't tested it with the SDL client, and I made some changes to SDL
initialization, so Rafal please look at this.

Note that I did not follow Jason's in providing two different options for
Default and Current, as he did for tilesets. I think is fundamentally
stupid and confusing, and I would very much like Jason to recant and mend
the errors of his way.

Anyway, please test and comment.

  - Per

Index: client/audio.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/audio.c,v
retrieving revision 1.16
diff -u -r1.16 audio.c
--- client/audio.c      2002/11/14 09:22:09     1.16
+++ client/audio.c      2003/01/20 15:20:34
@@ -50,6 +50,8 @@
 #define MAX_NUM_PLUGINS                3
 #define SNDSPEC_SUFFIX         ".soundspec"
 
+static bool audio_select_plugin(const char *const name, bool fatal);
+
 /* keep it open throughout */
 static struct section_file tagstruct, *tagfile = &tagstruct;
 
@@ -57,6 +59,9 @@
 static int num_plugins_used = 0;
 static int selected_plugin = -1;
 
+/* for options.c */
+char current_plugin_name[MAX_AUDIO_NAME_LEN];
+
 /**********************************************************************
   Returns a static, NULL-terminated list of all sound plugins
   available on the system.  This function is unfortunately similar to
@@ -71,6 +76,10 @@
     plugin_list[i] = plugins[i].name;
   }
   assert(i <= MAX_NUM_PLUGINS);
+  if (!tagfile) {
+    /* Audio disabled on client start - only display "None" */
+    plugin_list[1] = NULL; 
+  }
   plugin_list[i] = NULL;
 
   return plugin_list;
@@ -105,9 +114,19 @@
 }
 
 /**************************************************************************
+  This is merely a wrapper for the below function for use in options.c
+  and the client local options dialog.
+**************************************************************************/
+void audio_select_plugin_callback(struct client_option *option)
+{
+  assert(option->p_string_value && *option->p_string_value);
+  audio_select_plugin(option->p_string_value, FALSE);
+}
+
+/**************************************************************************
   Choose plugin. Returns TRUE on success, FALSE if not
 **************************************************************************/
-bool audio_select_plugin(const char *const name)
+static bool audio_select_plugin(const char *const name, bool fatal)
 {
   int i;
   bool found = FALSE;
@@ -124,22 +143,33 @@
     plugins[selected_plugin].stop();
     plugins[selected_plugin].wait();
     plugins[selected_plugin].shutdown();
+  } else if (i == selected_plugin) {
+    return TRUE;
   }
 
   if (!found) {
     freelog(LOG_FATAL,
            _("Plugin '%s' isn't available. Available are %s"), name,
            audio_get_all_plugin_names());
-    exit(EXIT_FAILURE);
+    if (fatal) {
+      exit(EXIT_FAILURE);
+    } else {
+      return FALSE;
+    }
   }
 
   if (!plugins[i].init()) {
     freelog(LOG_ERROR, _("Plugin %s found but can't be initialized."), name);
+    if (selected_plugin != -1) {
+      /* Fall back to original plugin */
+      plugins[selected_plugin].init();
+    }
     return FALSE;
   }
 
   selected_plugin = i;
-  freelog(LOG_VERBOSE, _("Plugin '%s' is now selected"),
+  sz_strlcpy(current_plugin_name, plugins[i].name);
+  freelog(LOG_NORMAL, _("Plugin '%s' is now selected"),
          plugins[selected_plugin].name);
   return TRUE;
 }
@@ -209,7 +239,8 @@
 
   if (strcmp(prefered_plugin_name, "none") == 0) {
     /* We explicitly choose none plugin, silently skip the code below */
-    freelog(LOG_VERBOSE, _("Proceeding with sound support disabled"));
+    freelog(LOG_NORMAL, _("Proceeding with sound support disabled"));
+    sz_strlcpy(current_plugin_name, prefered_plugin_name);
     tagfile = NULL;
     return;
   }
@@ -268,22 +299,22 @@
   atexit(audio_shutdown);
 
   if (prefered_plugin_name[0] != '\0') {
-    if (!audio_select_plugin(prefered_plugin_name))
+    if (!audio_select_plugin(prefered_plugin_name, TRUE))
       freelog(LOG_NORMAL, _("Proceeding with sound support disabled"));
     return;
   }
 
 #ifdef ESD
-  if (audio_select_plugin("esd")) return; 
+  if (audio_select_plugin("esd", TRUE)) return; 
 #endif
 #ifdef SDL
-  if (audio_select_plugin("sdl")) return; 
+  if (audio_select_plugin("sdl", TRUE)) return; 
 #endif
 #ifdef WINMM
-  if (audio_select_plugin("winmm")) return;
+  if (audio_select_plugin("winmm", TRUE)) return;
 #endif
 #ifdef AMIGA
-  if (audio_select_plugin("amiga")) return;
+  if (audio_select_plugin("amiga", TRUE)) return;
 #endif
   freelog(LOG_ERROR,
     _("No real audio subsystem managed to initialize!"));
Index: client/audio.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/audio.h,v
retrieving revision 1.4
diff -u -r1.4 audio.h
--- client/audio.h      2002/11/01 18:11:42     1.4
+++ client/audio.h      2003/01/20 15:20:34
@@ -14,6 +14,7 @@
 #define FC__AUDIO_H
 
 #include "shared.h"            /* bool type */
+#include "options.h"
 
 #define MAX_AUDIO_NAME_LEN             20
 #define MAX_AUDIO_DESCR_LEN            200
@@ -28,6 +29,8 @@
   bool (*play) (const char *const tag, const char *const path, bool repeat);
 };
 
+extern char current_plugin_name[MAX_AUDIO_NAME_LEN];
+
 const char **get_soundplugin_list(void);
 const char **get_soundset_list(void);
 
@@ -41,7 +44,7 @@
 void audio_play_sound(const char *const tag, char *const alt_tag);
 void audio_play_music(const char *const tag, char *const alt_tag);
 
-bool audio_select_plugin(const char *const name);
+void audio_select_plugin_callback(struct client_option *option);
 const char *audio_get_all_plugin_names(void);
 
 #endif                         /* FC__AUDIO_H */
Index: client/audio_sdl.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/audio_sdl.c,v
retrieving revision 1.4
diff -u -r1.4 audio_sdl.c
--- client/audio_sdl.c  2003/01/03 11:55:45     1.4
+++ client/audio_sdl.c  2003/01/20 15:20:34
@@ -60,7 +60,7 @@
     }
 
     Mix_PlayMusic(mus, -1);    /* -1 means loop forever */
-    freelog(LOG_VERBOSE, "Playing file %s on music channel", fullpath);
+    freelog(LOG_DEBUG, "Playing file %s on music channel", fullpath);
     /* in case we did a my_stop() recently; add volume controls later */
     Mix_VolumeMusic(MIX_MAX_VOLUME);
 
@@ -90,7 +90,7 @@
       Mix_FreeChunk(wave);
       return FALSE;
     }
-    freelog(LOG_VERBOSE, "Playing file %s on channel %d", fullpath, i);
+    freelog(LOG_DEBUG, "Playing file %s on channel %d", fullpath, i);
     /* free previous sample on this channel. it will by definition no
        longer be playing by the time we get here */
     if (samples[i].wave) {
@@ -127,21 +127,6 @@
 }
 
 /**************************************************************************
-  Quit SDL.  If the video is still in use (by gui-sdl), just quit the
-  subsystem.
-
-  This will need to be changed if SDL is used elsewhere.
-**************************************************************************/
-static void quit_sdl_audio(void)
-{
-  if (SDL_WasInit(SDL_INIT_VIDEO)) {
-    SDL_QuitSubSystem(SDL_INIT_AUDIO);
-  } else {
-    SDL_Quit();
-  }
-}
-
-/**************************************************************************
   Init SDL.  If the video is already in use (by gui-sdl), just init the
   subsystem.
 
@@ -152,6 +137,7 @@
   if (SDL_WasInit(SDL_INIT_VIDEO)) {
     return SDL_InitSubSystem(SDL_INIT_AUDIO);
   } else {
+    atexit(SDL_Quit);
     return SDL_Init(SDL_INIT_AUDIO);
   }
 }
@@ -171,11 +157,13 @@
     if (samples[i].wave) {
       Mix_FreeChunk(samples[i].wave);
     }
+    samples[i].wave = NULL;
+    samples[i].tag = NULL;
   }
   Mix_FreeMusic(mus);
 
   Mix_CloseAudio();
-  quit_sdl_audio();
+  SDL_QuitSubSystem(SDL_INIT_AUDIO);
 }
 
 /**************************************************************************
@@ -196,7 +184,7 @@
   if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
     freelog(LOG_ERROR, "Error calling Mix_OpenAudio");
     /* try something else */
-    quit_sdl_audio();
+    SDL_QuitSubSystem(SDL_INIT_AUDIO);
     return FALSE;
   }
 
Index: client/civclient.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/civclient.c,v
retrieving revision 1.160
diff -u -r1.160 civclient.c
--- client/civclient.c  2003/01/05 20:51:36     1.160
+++ client/civclient.c  2003/01/20 15:20:34
@@ -216,7 +216,7 @@
   if (sound_set_name[0] == '\0') 
     sz_strlcpy(sound_set_name, default_sound_set_name); 
   if (sound_plugin_name[0] == '\0')
-    sz_strlcpy(sound_plugin_name, default_sound_plugin_name); 
+    sz_strlcpy(sound_plugin_name, current_plugin_name); 
   if (server_host[0] == '\0')
     sz_strlcpy(server_host, default_server_host); 
   if (player_name[0] == '\0')
Index: client/options.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/options.c,v
retrieving revision 1.77
diff -u -r1.77 options.c
--- client/options.c    2002/12/21 14:19:05     1.77
+++ client/options.c    2003/01/20 15:20:35
@@ -49,7 +49,6 @@
 char default_metaserver[512] = METALIST_ADDR;
 char default_tile_set_name[512] = "\0";
 char default_sound_set_name[512] = "stdsounds";
-char default_sound_plugin_name[512] = "\0";
 
 /** Local Options: **/
 
@@ -99,8 +98,8 @@
                  get_tileset_list, tilespec_reread_callback),
   GEN_STR_OPTION(default_sound_set_name,    N_("Default name of sound set"),
                 get_soundset_list, NULL),
-  GEN_STR_OPTION(default_sound_plugin_name, N_("Default sound plugin"),
-                get_soundplugin_list, NULL),
+  GEN_STR_OPTION(current_plugin_name, N_("Current sound plugin"),
+                get_soundplugin_list, audio_select_plugin_callback),
 
   GEN_BOOL_OPTION(solid_color_behind_units, N_("Solid unit background color")),
   GEN_BOOL_OPTION(sound_bell_at_new_turn,   N_("Sound bell at new turn")),

[Prev in Thread] Current Thread [Next in Thread]