SDL_mixer: Update Mix_Init() return value to match documentation

From 2ec6b1a41391046035335c86b606f3c4af411fc4 Mon Sep 17 00:00:00 2001
From: Nathan Houghton <[EMAIL REDACTED]>
Date: Fri, 31 Dec 2021 14:33:21 -0800
Subject: [PATCH] Update Mix_Init() return value to match documentation

Include the already initialized MIX_INIT_* flags in the return value.
---
 CHANGES.txt |  3 +++
 src/mixer.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/CHANGES.txt b/CHANGES.txt
index 5b458bfa..c8dc51de 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,7 @@
 2.0.5:
+Nate Houghton - Fri Dec 31 22:33:21 2021
+ * Update Mix_Init() return value to match documentation, including
+   MIXER_INIT_* flags for already-initialized modules
 Ozkan Sezer - Sun Feb 07 03:10:10 2021
  * fixed distorted MIDI playback with FluidSynth if the sample rate
    is out of library's limits
diff --git a/src/mixer.c b/src/mixer.c
index 628205b4..c9ce4da1 100644
--- a/src/mixer.c
+++ b/src/mixer.c
@@ -164,9 +164,53 @@ const SDL_version *Mix_Linked_Version(void)
     return(&linked_version);
 }
 
+/*
+ * Returns a bitmap of already loaded modules (MIX_INIT_* flags).
+ *
+ * Note that functions other than Mix_Init() may cause a module to get loaded
+ * (hence the looping over the interfaces instead of maintaining a set of flags
+ * just in Mix_Init() and Mix_Quit()).
+ */
+static int get_loaded_mix_init_flags(void)
+{
+    int i;
+    int loaded_init_flags = 0;
+
+    for (i = 0; i < get_num_music_interfaces(); ++i) {
+        Mix_MusicInterface *interface;
+
+        interface = get_music_interface(i);
+        if (interface->loaded) {
+            switch (interface->type) {
+            case MUS_FLAC:
+                loaded_init_flags |= MIX_INIT_FLAC;
+                break;
+            case MUS_MOD:
+                loaded_init_flags |= MIX_INIT_MOD;
+                break;
+            case MUS_MP3:
+                loaded_init_flags |= MIX_INIT_MP3;
+                break;
+            case MUS_OGG:
+                loaded_init_flags |= MIX_INIT_OGG;
+                break;
+            case MUS_MID:
+                loaded_init_flags |= MIX_INIT_MID;
+                break;
+            case MUS_OPUS:
+                loaded_init_flags |= MIX_INIT_OPUS;
+                break;
+            }
+        }
+    }
+
+    return loaded_init_flags;
+}
+
 int Mix_Init(int flags)
 {
     int result = 0;
+    int already_loaded = get_loaded_mix_init_flags();
 
     if (flags & MIX_INIT_FLAC) {
         if (load_music_type(MUS_FLAC)) {
@@ -216,6 +260,8 @@ int Mix_Init(int flags)
             Mix_SetError("MIDI support not available");
         }
     }
+    result |= already_loaded;
+
     return result;
 }