Browse Source

Fltk adds MIDI playback option; CLI midiplayer can be specified in cfg; default cli player(timidity) path can be detected if in path

Pal Lockheart 7 years ago
parent
commit
6ad53c0d15
5 changed files with 44 additions and 15 deletions
  1. 1 0
      .gitignore
  2. 5 0
      palcfg.c
  3. 2 0
      palcfg.h
  4. 27 12
      unix/native_midi.cpp
  5. 9 3
      unix/unix.cpp

+ 1 - 0
.gitignore

@@ -242,6 +242,7 @@ android/so
 resources
 
 unix/sdlpal
+sdlpal.cfg
 data
 
 *.xcscmblueprint

+ 5 - 0
palcfg.c

@@ -63,6 +63,7 @@ static const ConfigItem gConfigItems[PALCFG_ALL_MAX] = {
 	{ PALCFG_MUSIC,             PALCFG_STRING,   "Music",              5, MAKE_VALUE("RIX",    NULL, NULL) },
 	{ PALCFG_OPL,               PALCFG_STRING,   "OPL",                3, MAKE_VALUE("DOSBOX", NULL, NULL) },
 	{ PALCFG_RIXEXTRAINIT,      PALCFG_STRING,   "RIXExtraInit",      12, MAKE_VALUE(NULL,     NULL, NULL) },
+	{ PALCFG_CLIMIDIPLAYER,     PALCFG_STRING,   "CLIMIDIPlayer",     13, MAKE_VALUE(NULL,     NULL, NULL) },
 };
 
 
@@ -365,6 +366,9 @@ PAL_LoadConfig(
 #endif
 					break;
 				}
+				case PALCFG_CLIMIDIPLAYER:
+					gConfig.pszCLIMIDIPlayerPath = ParseStringValue(value.sValue, gConfig.pszCLIMIDIPlayerPath);
+					break;
 				default:
 					values[item->Item] = value;
 					break;
@@ -458,6 +462,7 @@ PAL_SaveConfig(
 		if (gConfig.pszSavePath) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_SAVEPATH), gConfig.pszSavePath); fputs(buf, fp); }
 		if (gConfig.pszMsgFile) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_MESSAGEFILE), gConfig.pszMsgFile); fputs(buf, fp); }
 		if (gConfig.pszBdfFile) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_BDFFILE), gConfig.pszBdfFile); fputs(buf, fp); }
+		if (gConfig.pszCLIMIDIPlayerPath) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_CLIMIDIPLAYER), gConfig.pszCLIMIDIPlayerPath); fputs(buf, fp); }
 
 		fclose(fp);
 

+ 2 - 0
palcfg.h

@@ -78,6 +78,7 @@ typedef enum tagPALCFG_ITEM
 	PALCFG_MUSIC,
 	PALCFG_OPL,
 	PALCFG_RIXEXTRAINIT,
+	PALCFG_CLIMIDIPLAYER,
 	/* Strings */
 	PALCFG_STRING_MAX,
 
@@ -164,6 +165,7 @@ typedef struct tagCONFIGURATION
 	char            *pszSavePath;
 	char            *pszMsgFile;
 	char            *pszBdfFile;
+	char            *pszCLIMIDIPlayerPath;
 	CODEPAGE         uCodePage;
 	DWORD            dwWordLength;
 	DWORD            dwScreenWidth;

+ 27 - 12
unix/native_midi.cpp

@@ -13,8 +13,10 @@
 #include <vector>
 
 #include "native_midi/native_midi.h"
+#include "util.h"
+#include "palcfg.h"
 
-// Warning: this is a unverified implementation and this comment should be removed if verified
+#define CLIPLAYER_EXECUTABLE (which("timidity"))
 
 struct _NativeMidiSong
 {
@@ -28,20 +30,34 @@ struct _NativeMidiSong
 };
 
 const char* midi_file = "/tmp/sdlpal.temp.mid";
-char* timidity = nullptr;
+char* cliplayer = nullptr;
+
+static char *which(const char *cmd)
+{
+    char path[PATH_MAX];
+    FILE *fp = popen(va("which %s", cmd), "r");
+    if (fp == NULL) {
+        return NULL;
+    }else{
+        fgets(path, sizeof(path)-1, fp);
+        pclose(fp);
+        if( path[strlen(path)-1] == '\n' ) path[strlen(path)-1] = '\0';
+        if( path[strlen(path)-1] == '\r' ) path[strlen(path)-1] = '\0';
+        return path;
+    }
+}
 
 extern "C" int native_midi_detect()
 {
-    // FIXME!!!
-    if (timidity)
+    if (cliplayer)
     {
-        free(timidity);
-        timidity = nullptr;
+        free(cliplayer);
+        cliplayer = nullptr;
     }
-    //system `timidity -v` will cause CLI blocked by the version info...
-    if (access("/usr/bin/timidity",F_OK) == 0)
+    char *path = (gConfig.pszCLIMIDIPlayerPath ? gConfig.pszCLIMIDIPlayerPath : CLIPLAYER_EXECUTABLE);
+    if (path && access(path,F_OK) == 0)
     {
-        timidity = strdup("/usr/bin/timidity");
+        cliplayer = strdup(path);
         return 1;
     }
     else
@@ -87,7 +103,6 @@ extern "C" void native_midi_freesong(NativeMidiSong *song)
 {
     if (song)
     {
-	//stop it first to prevent app terminated by destructing joinable thread destruction 
 	if (native_midi_active(song)) 
 		native_midi_stop(song);
         if (song->file) delete []song->file;
@@ -111,8 +126,8 @@ extern "C" void native_midi_start(NativeMidiSong *song, int looping)
 
             if (0 == pid)
             {
-                char* args[] = { timidity, song->file, NULL };
-                if (-1 == execv(timidity, args)) exit(-1);
+                char* args[] = { cliplayer, song->file, NULL };
+                if (-1 == execv(cliplayer, args)) exit(-1);
             }
             else if (-1 == pid)
             {

+ 9 - 3
unix/unix.cpp

@@ -19,6 +19,12 @@
 #include <FL/Fl_Int_Input.H>
 #include <FL/Fl_Choice.H>
 
+#ifdef PAL_HAS_NATIVEMIDI
+    #define MUSIC_MIN MUSIC_MIDI
+#else
+    #define MUSIC_MIN MUSIC_RIX
+#endif
+
 struct {
    Fl_Input* gamepath;
    Fl_Radio_Round_Button* cht;
@@ -100,7 +106,7 @@ void InitControls()
 #endif
    gWidgets.fullscreen->value(gConfig.fFullScreen ? 1 : 0);
    gWidgets.cd->value(gConfig.eCDType - MUSIC_MP3);
-   gWidgets.bgm->value(gConfig.eMusicType - MUSIC_RIX);
+   gWidgets.bgm->value(gConfig.eMusicType - MUSIC_MIN);
    gWidgets.stereo->value(gConfig.iAudioChannels == 2 ? 1 : 0);
    sprintf(buffer, "%d", gConfig.iSampleRate); gWidgets.samplerate->value(buffer);
    gWidgets.opl->value(gConfig.eOPLType);
@@ -126,7 +132,7 @@ void SaveControls()
 #endif
    gConfig.fFullScreen = gWidgets.fullscreen->value();
    gConfig.eCDType = (MUSICTYPE)(gWidgets.cd->value() + MUSIC_MP3);
-   gConfig.eMusicType = (MUSICTYPE)(gWidgets.bgm->value() + MUSIC_RIX);
+   gConfig.eMusicType = (MUSICTYPE)(gWidgets.bgm->value() + MUSIC_MIN);
    gConfig.iAudioChannels = gWidgets.stereo->value() ? 2 : 1;
    gConfig.iSampleRate = atoi(gWidgets.samplerate->value());
    gConfig.eOPLType = (OPLTYPE)gWidgets.opl->value();
@@ -174,7 +180,7 @@ Fl_Window* InitWindow()
 
    (new Fl_Box(FL_BORDER_BOX, 5, 210, 630, 130, gLabels[lang].audio))->align(FL_ALIGN_TOP);
    (gWidgets.cd = new Fl_Choice(84, 219, lang ? 100 : 120, 22, gLabels[lang].cd))->add("MP3|OGG");
-   (gWidgets.bgm = new Fl_Choice(285, 219, 60, 22, gLabels[lang].bgm))->add("RIX|MP3|OGG");
+   (gWidgets.bgm = new Fl_Choice(285, 219, 60, 22, gLabels[lang].bgm))->add( va("%s%s",(PAL_HAS_NATIVEMIDI ? "MIDI|" : ""), "RIX|MP3|OGG") );
    gWidgets.stereo = new Fl_Check_Button(365, 220, 60, 20, gLabels[lang].stereo);
    gWidgets.samplerate = new Fl_Int_Input(570, 219, 60, 22, gLabels[lang].samplerate);
    (gWidgets.opl = new Fl_Choice(84, 249, lang ? 100 : 120, 22, gLabels[lang].opl))->add("DOSBOX|MAME|DOSBOXNEW");