Skip to content

Commit

Permalink
Fix Frontend MIDI when selected prior to game booting
Browse files Browse the repository at this point in the history
Fixes #70
  • Loading branch information
schellingb committed Jan 11, 2021
1 parent b7d6d47 commit 0edcc34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
20 changes: 5 additions & 15 deletions dosbox_pure_libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ bool MSCDEX_HasDrive(char driveLetter);
int MSCDEX_AddDrive(char driveLetter, const char* physicalPath, Bit8u& subUnit);
int MSCDEX_RemoveDrive(char driveLetter);
bool MIDI_TSF_SwitchSF2(const char*);
bool MIDI_Retro_IsActiveHandler();
bool MIDI_Retro_HasOutputIssue();

void DBP_Crash(const char* msg)
{
Expand Down Expand Up @@ -626,9 +626,9 @@ bool DBP_IsShuttingDown()
return (!first_shell || first_shell->exit);
}

void DBP_GetRetroMidiInterface(retro_midi_interface* res)
bool DBP_GetRetroMidiInterface(retro_midi_interface* res)
{
if(environ_cb) environ_cb(RETRO_ENVIRONMENT_GET_MIDI_INTERFACE, res);
return (environ_cb && environ_cb(RETRO_ENVIRONMENT_GET_MIDI_INTERFACE, res));
}

Bitu GFX_GetBestMode(Bitu flags)
Expand Down Expand Up @@ -2847,18 +2847,8 @@ void retro_run(void)
dbp_calculate_min_sleep();
}

retro_variable var = { "dosbox_pure_midi", NULL };
if (environ_cb && environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && !strcasecmp(var.value, "frontend") && !MIDI_Retro_IsActiveHandler())
{
retro_midi_interface midi = {0};
if (environ_cb(RETRO_ENVIRONMENT_GET_MIDI_INTERFACE, &midi) && midi.output_enabled && midi.output_enabled())
{
// Reset MIDI section to restart the midi_retro handler now that the frontend MIDI driver has fully started up
std::string str = "midiconfig"; str += '='; str += "frontend";
DBP_QueueEvent(DBPET_SET_VARIABLE, str, control->GetSection("midi"));
}
else retro_notify(0, RETRO_LOG_WARN, "The frontend MIDI output is not set up correctly");
}
if (MIDI_Retro_HasOutputIssue())
retro_notify(0, RETRO_LOG_WARN, "The frontend MIDI output is not set up correctly");

// first frame
DBP_ASSERT(dbp_state != DBPSTATE_BOOT);
Expand Down
18 changes: 8 additions & 10 deletions src/gui/midi_retro.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ struct MidiHandler_retro : public MidiHandler

bool Open(const char * conf)
{
midi_interface.write = NULL;
if (conf && !strcasecmp(conf, "frontend"))
{
midi_interface.output_enabled = NULL;
extern void DBP_GetRetroMidiInterface(retro_midi_interface* res);
DBP_GetRetroMidiInterface(&midi_interface);
if (midi_interface.output_enabled && midi_interface.output_enabled())
extern bool DBP_GetRetroMidiInterface(retro_midi_interface* res);
if (DBP_GetRetroMidiInterface(&midi_interface) && midi_interface.write)
return true;
}
midi_interface.write = NULL;
return false;
};

Expand All @@ -49,7 +47,7 @@ struct MidiHandler_retro : public MidiHandler
for (Bit8u b : resetchan) midi_interface.write(b, 0);
}
midi_interface.write(0xFF, 0); // system reset
midi_interface.flush();
if (midi_interface.flush) midi_interface.flush();
midi_interface.write = NULL;
};

Expand All @@ -60,20 +58,20 @@ struct MidiHandler_retro : public MidiHandler
static const Bit8u ctrl_lengths[] = { 0, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
Bit8u b = *msg, len = (b < 0x80 ? 0 : (b >= 0xF0 ? ctrl_lengths[b-0xF0] : msg_lengths[(b>>4)&7]));
while (len--) midi_interface.write(*(msg++), 0);
midi_interface.flush();
if (midi_interface.flush) midi_interface.flush();
};

void PlaySysex(Bit8u * sysex,Bitu len)
{
if (!midi_interface.write) return;
while (len--) midi_interface.write(*(sysex++), 0);
midi_interface.flush();
if (midi_interface.flush) midi_interface.flush();
}
};

static MidiHandler_retro Midi_retro;

bool MIDI_Retro_IsActiveHandler()
bool MIDI_Retro_HasOutputIssue()
{
return (midi.handler == &Midi_retro);
return (midi.handler == &Midi_retro && (!Midi_retro.midi_interface.output_enabled || !Midi_retro.midi_interface.output_enabled()));
}

0 comments on commit 0edcc34

Please sign in to comment.