Skip to content

Commit

Permalink
MIDI init/close refactor (+ disable on WinXP, buggy)
Browse files Browse the repository at this point in the history
  • Loading branch information
8bitbubsy committed Apr 9, 2024
1 parent 7d116f5 commit 459e139
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 108 deletions.
43 changes: 35 additions & 8 deletions src/ft2_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ bool loadConfig(bool showErrorFlag)
audio.currInputDevice = getAudioInputDeviceFromConfig();

#ifdef HAS_MIDI
if (midi.initThreadDone)
if (midi.supported && midi.initThreadDone)
{
setMidiInputDeviceFromConfig();
if (ui.configScreenShown && editor.currConfigScreen == CONFIG_SCREEN_MIDI_INPUT)
Expand Down Expand Up @@ -390,7 +390,8 @@ bool saveConfig(bool showErrorFlag)

saveAudioDevicesToConfig(audio.currOutputDevice, audio.currInputDevice);
#ifdef HAS_MIDI
saveMidiInputDeviceToConfig();
if (midi.supported)
saveMidiInputDeviceToConfig();
#endif

FILE *f = UNICHAR_FOPEN(editor.configFileLocationU, "wb");
Expand Down Expand Up @@ -673,7 +674,9 @@ static void setConfigFileLocation(void) // kinda hackish
strcat(editor.configFileLocationU, "/FT2.CFG");
#endif

editor.midiConfigFileLocationU = getFullMidiDevConfigPathU();
if (midi.supported)
editor.midiConfigFileLocationU = getFullMidiDevConfigPathU();

editor.audioDevConfigFileLocationU = getFullAudDevConfigPathU();
}

Expand Down Expand Up @@ -781,6 +784,8 @@ static void setConfigRadioButtonStates(void)
{
uint16_t tmpID;



uncheckRadioButtonGroup(RB_GROUP_CONFIG_SELECT);
switch (editor.currConfigScreen)
{
Expand All @@ -795,6 +800,14 @@ static void setConfigRadioButtonStates(void)
radioButtons[tmpID].state = RADIOBUTTON_CHECKED;

showRadioButtonGroup(RB_GROUP_CONFIG_SELECT);

// hide MIDI radio button if MIDI is not supported (hackish)
if (!midi.supported)
{
radioButton_t *t = &radioButtons[RB_CONFIG_MIDI_INPUT];
hideRadioButton(RB_CONFIG_MIDI_INPUT);
fillRect(t->x, t->y, RADIOBUTTON_W, RADIOBUTTON_H, PAL_DESKTOP);
}
}

void setConfigAudioRadioButtonStates(void) // accessed by other .c files
Expand Down Expand Up @@ -1018,7 +1031,10 @@ static void setConfigMiscCheckButtonStates(void)
checkBoxes[CB_CONF_CHANGE_PATTLEN_INS_DEL].checked = config.recTrueInsert;
checkBoxes[CB_CONF_MIDI_ALLOW_PC].checked = config.recMIDIAllowPC;
#ifdef HAS_MIDI
checkBoxes[CB_CONF_MIDI_ENABLE].checked = midi.enable;
if (midi.supported)
checkBoxes[CB_CONF_MIDI_ENABLE].checked = midi.enable;
else
checkBoxes[CB_CONF_MIDI_ENABLE].checked = false;
#else
checkBoxes[CB_CONF_MIDI_ENABLE].checked = false;
#endif
Expand Down Expand Up @@ -1109,7 +1125,8 @@ void showConfigScreen(void)
textOutShadow(21, 35, PAL_FORGRND, PAL_DSKTOP2, "Layout");
textOutShadow(21, 51, PAL_FORGRND, PAL_DSKTOP2, "Miscellaneous");
#ifdef HAS_MIDI
textOutShadow(21, 67, PAL_FORGRND, PAL_DSKTOP2, "MIDI input");
if (midi.supported)
textOutShadow(21, 67, PAL_FORGRND, PAL_DSKTOP2, "MIDI input");
#endif
textOutShadow(20, 93, PAL_FORGRND, PAL_DSKTOP2, "Auto save");

Expand Down Expand Up @@ -1377,6 +1394,7 @@ void showConfigScreen(void)
}
break;

#ifdef HAS_MIDI
case CONFIG_SCREEN_MIDI_INPUT:
{
drawFramework(110, 0, 394, 173, FRAMEWORK_TYPE1);
Expand All @@ -1387,15 +1405,14 @@ void showConfigScreen(void)

blitFast(517, 51, bmp.midiLogo, 103, 55);

#ifdef HAS_MIDI
showPushButton(PB_CONFIG_MIDI_INPUT_DOWN);
showPushButton(PB_CONFIG_MIDI_INPUT_UP);
rescanMidiInputDevices();
drawMidiInputList();
showScrollBar(SB_MIDI_INPUT_SCROLL);
#endif
}
break;
#endif
}
}

Expand Down Expand Up @@ -2072,7 +2089,17 @@ void cbMIDIAllowPC(void)
void cbMIDIEnable(void)
{
#ifdef HAS_MIDI
midi.enable ^= 1;
if (midi.supported)
{
midi.enable ^= 1;
}
else
{
checkBoxes[CB_CONF_MIDI_ENABLE].checked = false;
drawCheckBox(CB_CONF_MIDI_ENABLE);

okBox(0, "System message", "MIDI support is disabled for Windows XP as it is buggy!", NULL);
}
#else
checkBoxes[CB_CONF_MIDI_ENABLE].checked = false;
drawCheckBox(CB_CONF_MIDI_ENABLE);
Expand Down
2 changes: 2 additions & 0 deletions src/ft2_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ enum
CONFIG_SCREEN_AUDIO,
CONFIG_SCREEN_LAYOUT,
CONFIG_SCREEN_MISCELLANEOUS,
#ifdef HAS_MIDI
CONFIG_SCREEN_MIDI_INPUT,
#endif

CONFIG_HIDE_ERRORS = 0,
CONFIG_SHOW_ERRORS = 1,
Expand Down
15 changes: 9 additions & 6 deletions src/ft2_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "ft2_audio.h"
#include "ft2_trim.h"
#include "ft2_sample_ed_features.h"
#include "ft2_midi.h"
#include "ft2_structs.h"

keyb_t keyb; // globalized
Expand Down Expand Up @@ -1280,16 +1281,18 @@ static bool checkModifiedKeys(SDL_Keycode keycode)

return true;
}
#ifdef HAS_MIDI
else if (keyb.leftCtrlPressed)
{
editor.currConfigScreen = 3;
showConfigScreen();
checkRadioButton(RB_CONFIG_MIDI_INPUT);

#ifdef HAS_MIDI
if (midi.supported)
{
editor.currConfigScreen = 3;
showConfigScreen();
checkRadioButton(RB_CONFIG_MIDI_INPUT);
}
#endif
return true;
}
#endif
}
break;

Expand Down
70 changes: 41 additions & 29 deletions src/ft2_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifdef _WIN32
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include <versionhelpers.h>
#include <SDL2/SDL_syswm.h>
#else
#include <unistd.h> // chdir()
Expand All @@ -35,10 +36,6 @@
#include "ft2_structs.h"
#include "ft2_hpc.h"

#ifdef HAS_MIDI
static SDL_Thread *initMidiThread;
#endif

static void initializeVars(void);
static void cleanUpAndExit(void); // never call this inside the main loop
#ifdef __APPLE__
Expand Down Expand Up @@ -99,6 +96,10 @@ int main(int argc, char *argv[])
#endif

#ifdef _WIN32
// disable MIDI support if using Windows XP, as it is unstable
if (!IsWindowsVistaOrGreater())
midi.supported = false;

#ifndef _MSC_VER
SetProcessDPIAware();
#endif
Expand Down Expand Up @@ -231,14 +232,16 @@ int main(int argc, char *argv[])

#ifdef HAS_MIDI
// set up MIDI input (in a thread because it can take quite a while on f.ex. macOS)
initMidiThread = SDL_CreateThread(initMidiFunc, NULL, NULL);
if (initMidiThread == NULL)
if (midi.supported)
{
showErrorMsgBox("Couldn't create MIDI initialization thread!");
cleanUpAndExit();
return 1;
midi.initMidiThread = SDL_CreateThread(initMidiFunc, NULL, NULL);
if (midi.initMidiThread == NULL)
{
showErrorMsgBox("Couldn't create MIDI initialization thread!");
cleanUpAndExit();
return 1;
}
}
SDL_DetachThread(initMidiThread); // don't wait for this thread, let it clean up when done
#endif

hpc_ResetCounters(&video.vblankHpc); // quirk: this is needed for potential okBox() calls in handleModuleLoadFromArg()
Expand Down Expand Up @@ -271,6 +274,10 @@ static void initializeVars(void)
cpu.hasSSE2 = SDL_HasSSE2();

// clear common structs
#ifdef HAS_MIDI
memset(&midi, 0, sizeof (midi));
midi.supported = true;
#endif
memset(&video, 0, sizeof (video));
memset(&keyb, 0, sizeof (keyb));
memset(&mouse, 0, sizeof (mouse));
Expand All @@ -289,7 +296,7 @@ static void initializeVars(void)

// now set data that must be initialized to non-zero values...

audio.locked = true;
audio.locked = true; // XXX: Why..?
audio.rescanAudioDevicesSupported = true;

// set non-zero values
Expand All @@ -305,7 +312,7 @@ static void initializeVars(void)
editor.srcInstr = 1;
editor.curInstr = 1;
editor.curOctave = 4;
editor.smpEd_NoteNr = 48 + 1; // middle-C
editor.smpEd_NoteNr = 1+NOTE_C4;

editor.ptnJumpPos[0] = 0x00;
editor.ptnJumpPos[1] = 0x10;
Expand All @@ -316,25 +323,41 @@ static void initializeVars(void)
memset(editor.copyMask, 1, sizeof (editor.copyMask));
memset(editor.pasteMask, 1, sizeof (editor.pasteMask));

#ifdef HAS_MIDI
midi.enable = true;
#endif

editor.diskOpReadOnOpen = true;

audio.linearPeriodsFlag = true;
calcReplayerLogTab();

#ifdef HAS_MIDI
midi.enable = true;
#endif

editor.programRunning = true;
}

static void cleanUpAndExit(void) // never call this inside the main loop!
{
#ifdef HAS_MIDI
if (midi.closeMidiOnExit)
if (midi.supported)
{
if (midi.initMidiThread != NULL)
{
SDL_WaitThread(midi.initMidiThread, NULL);
midi.initMidiThread = NULL;
}

midi.enable = false; // stop MIDI callback from doing things
while (midi.callbackBusy) SDL_Delay(1); // wait for MIDI callback to finish

closeMidiInDevice();
freeMidiIn();
freeMidiInputDeviceList();

if (midi.inputDeviceName != NULL)
{
free(midi.inputDeviceName);
midi.inputDeviceName = NULL;
}
}
#endif

Expand All @@ -345,22 +368,11 @@ static void cleanUpAndExit(void) // never call this inside the main loop!
freeDiskOp();
clearCopyBuffer();
freeAudioDeviceSelectorBuffers();
#ifdef HAS_MIDI
freeMidiInputDeviceList();
#endif
windUpFTHelp();
freeTextBoxes();
freeMouseCursors();
freeBMPs();

#ifdef HAS_MIDI
if (midi.inputDeviceName != NULL)
{
free(midi.inputDeviceName);
midi.inputDeviceName = NULL;
}
#endif

if (editor.audioDevConfigFileLocationU != NULL)
{
free(editor.audioDevConfigFileLocationU);
Expand Down Expand Up @@ -396,7 +408,7 @@ static void cleanUpAndExit(void) // never call this inside the main loop!
static void osxSetDirToProgramDirFromArgs(char **argv)
{
/* OS X/macOS: hackish way of setting the current working directory to the place where we double clicked
** on the icon (for protracker.ini loading)
** on the icon (for FT2.CFG loading)
*/

// if we launched from the terminal, argv[0][0] would be '.'
Expand Down
Loading

0 comments on commit 459e139

Please sign in to comment.