foxygit / doom Log in
commit d832c5765cbd83ecc6a0035d8d8e277215bd4822
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Sat Jul 13 23:52:11 2019 -0400
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Sat Jul 13 23:52:11 2019 -0400

    midiproc: Add command for song unregistration.

    This mirrors the RegisterSong command and makes the midiproc API match
    the internal music module API. This should help to avoid bugs caused by
    a difference in song unregistration when using the midiproc.

    Part of a fix for #963.
---
 midiproc/main.c  | 10 ++++++++--
 midiproc/proto.h |  3 ++-
 src/i_midipipe.c | 26 ++++++++++++++++++++++++++
 src/i_midipipe.h |  1 +
 src/i_sdlmusic.c | 15 +++++++++++----
 5 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/midiproc/main.c b/midiproc/main.c
index 19d2c047..c21428d3 100644
--- a/midiproc/main.c
+++ b/midiproc/main.c
@@ -119,7 +119,6 @@ static void ShutdownSDL(void)

 static boolean RegisterSong(const char *filename)
 {
-    UnregisterSong();
     music = Mix_LoadMUS(filename);

     // Remove the temporary MIDI file
@@ -191,6 +190,12 @@ static boolean MidiPipe_RegisterSong(buffer_reader_t *reader)
     return true;
 }

+static boolean MidiPipe_UnregisterSong(buffer_reader_t *reader)
+{
+    UnregisterSong();
+    return true;
+}
+
 boolean MidiPipe_SetVolume(buffer_reader_t *reader)
 {
     int vol;
@@ -222,7 +227,6 @@ boolean MidiPipe_PlaySong(buffer_reader_t *reader)
 boolean MidiPipe_StopSong()
 {
     StopSong();
-    UnregisterSong();

     return true;
 }
@@ -246,6 +250,8 @@ boolean ParseCommand(buffer_reader_t *reader, uint16_t command)
     {
     case MIDIPIPE_PACKET_TYPE_REGISTER_SONG:
         return MidiPipe_RegisterSong(reader);
+    case MIDIPIPE_PACKET_TYPE_UNREGISTER_SONG:
+        return MidiPipe_UnregisterSong(reader);
     case MIDIPIPE_PACKET_TYPE_SET_VOLUME:
         return MidiPipe_SetVolume(reader);
     case MIDIPIPE_PACKET_TYPE_PLAY_SONG:
diff --git a/midiproc/proto.h b/midiproc/proto.h
index c3e9287a..35319fc8 100755
--- a/midiproc/proto.h
+++ b/midiproc/proto.h
@@ -24,7 +24,8 @@ typedef enum {
     MIDIPIPE_PACKET_TYPE_SET_VOLUME,
     MIDIPIPE_PACKET_TYPE_PLAY_SONG,
     MIDIPIPE_PACKET_TYPE_STOP_SONG,
-    MIDIPIPE_PACKET_TYPE_SHUTDOWN
+    MIDIPIPE_PACKET_TYPE_SHUTDOWN,
+    MIDIPIPE_PACKET_TYPE_UNREGISTER_SONG,
 } net_midipipe_packet_type_t;

 #endif
diff --git a/src/i_midipipe.c b/src/i_midipipe.c
index 330377d9..46b9b312 100644
--- a/src/i_midipipe.c
+++ b/src/i_midipipe.c
@@ -262,6 +262,32 @@ boolean I_MidiPipe_RegisterSong(char *filename)
     return true;
 }

+//
+// I_MidiPipe_UnregisterSong
+//
+// Tells the MIDI subprocess to unload the current song.
+//
+void I_MidiPipe_UnregisterSong(void)
+{
+    boolean ok;
+    net_packet_t *packet;
+
+    packet = NET_NewPacket(64);
+    NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_UNREGISTER_SONG);
+    ok = WritePipe(packet);
+    NET_FreePacket(packet);
+
+    if (!ok)
+    {
+        DEBUGOUT("I_MidiPipe_UnregisterSong failed");
+        return;
+    }
+
+    midi_server_registered = false;
+
+    DEBUGOUT("I_MidiPipe_UnregisterSong succeeded");
+}
+
 //
 // I_MidiPipe_SetVolume
 //
diff --git a/src/i_midipipe.h b/src/i_midipipe.h
index 72f26381..c71b9d4d 100644
--- a/src/i_midipipe.h
+++ b/src/i_midipipe.h
@@ -29,6 +29,7 @@ extern boolean midi_server_initialized;
 extern boolean midi_server_registered;

 boolean I_MidiPipe_RegisterSong(char *filename);
+void I_MidiPipe_UnregisterSong(void);
 void I_MidiPipe_SetVolume(int vol);
 void I_MidiPipe_PlaySong(int loops);
 void I_MidiPipe_StopSong();
diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c
index 48c18187..40ac69dc 100644
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -345,12 +345,19 @@ static void I_SDL_UnRegisterSong(void *handle)
         return;
     }

-    if (handle == NULL)
+#if defined(_WIN32)
+    if (midi_server_registered)
     {
-        return;
+        I_MidiPipe_StopSong();
+    }
+    else
+#endif
+    {
+        if (handle != NULL)
+        {
+            Mix_FreeMusic(music);
+        }
     }
-
-    Mix_FreeMusic(music);
 }

 // Determine whether memory block is a .mid file