native_midi.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/wait.h>
  5. #include <signal.h>
  6. #include <unistd.h>
  7. #include <future>
  8. #include <thread>
  9. #include <condition_variable>
  10. #include <mutex>
  11. #include <atomic>
  12. #include <vector>
  13. #include "native_midi/native_midi.h"
  14. #include "util.h"
  15. #include "palcfg.h"
  16. #define CLIPLAYER_EXECUTABLE (which("timidity"))
  17. struct _NativeMidiSong
  18. {
  19. std::thread Thread;
  20. std::mutex Mutex;
  21. char *file;
  22. int pid;
  23. volatile bool playing;
  24. bool looping;
  25. };
  26. const char* midi_file = "/tmp/sdlpal.temp.mid";
  27. char* cliplayer = nullptr;
  28. static char *which(const char *cmd)
  29. {
  30. static char path[PATH_MAX] = { '\0' };
  31. FILE *fp = popen(PAL_va(0, "which %s", cmd), "r");
  32. if (fp == NULL) {
  33. return NULL;
  34. }else{
  35. if (fgets(path, sizeof(path)-1, fp) != NULL)
  36. {
  37. if (path[strlen(path) - 1] == '\n') path[strlen(path) - 1] = '\0';
  38. if (path[strlen(path) - 1] == '\r') path[strlen(path) - 1] = '\0';
  39. }
  40. pclose(fp);
  41. return path;
  42. }
  43. }
  44. extern "C" int native_midi_detect()
  45. {
  46. if (cliplayer)
  47. {
  48. free(cliplayer);
  49. cliplayer = nullptr;
  50. }
  51. char *path = (gConfig.pszMIDIClient ? gConfig.pszMIDIClient : CLIPLAYER_EXECUTABLE);
  52. if (path && access(path,F_OK) == 0)
  53. {
  54. cliplayer = strdup(path);
  55. return 1;
  56. }
  57. else
  58. {
  59. return 0;
  60. }
  61. }
  62. extern "C" NativeMidiSong *native_midi_loadsong(const char *midifile)
  63. {
  64. struct stat st;
  65. if (0 != stat(midifile, &st)) return NULL;
  66. auto song = new NativeMidiSong;
  67. if (NULL == song) return NULL;
  68. if (NULL == (song->file = new char[strlen(midifile) + 1])) return NULL;
  69. song->pid = -1;
  70. song->playing = false;
  71. song->looping = false;
  72. strcpy(song->file, midifile);
  73. return song;
  74. }
  75. extern "C" NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
  76. {
  77. FILE *fp = fopen(midi_file, "wb+");
  78. if (fp)
  79. {
  80. char buf[4096];
  81. size_t bytes;
  82. while((bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf))) > 0)
  83. fwrite(buf, sizeof(char), bytes, fp);
  84. fclose(fp);
  85. return native_midi_loadsong(midi_file);
  86. }
  87. return NULL;
  88. }
  89. extern "C" void native_midi_freesong(NativeMidiSong *song)
  90. {
  91. if (song)
  92. {
  93. if (native_midi_active(song))
  94. native_midi_stop(song);
  95. if (song->file) delete []song->file;
  96. delete song;
  97. }
  98. }
  99. extern "C" void native_midi_start(NativeMidiSong *song, int looping)
  100. {
  101. if (!song) return;
  102. native_midi_stop(song);
  103. song->playing = true;
  104. song->looping = looping ? true : false;
  105. song->Thread = std::move(std::thread([](NativeMidiSong *song)->void {
  106. while(song->looping)
  107. {
  108. auto pid = fork();
  109. int status;
  110. if (0 == pid)
  111. {
  112. char* args[] = { cliplayer, song->file, NULL };
  113. if (-1 == execv(cliplayer, args)) exit(-1);
  114. }
  115. else if (-1 == pid)
  116. {
  117. return;
  118. }
  119. song->Mutex.lock();
  120. song->pid = pid;
  121. song->Mutex.unlock();
  122. waitpid(pid, &status, 0);
  123. }
  124. song->playing = false;
  125. }, song));
  126. }
  127. extern "C" void native_midi_stop(NativeMidiSong *song)
  128. {
  129. if (song)
  130. {
  131. song->looping = false;
  132. song->playing = false;
  133. if (-1 != song->pid)
  134. {
  135. song->Mutex.lock();
  136. kill(song->pid, SIGTERM);
  137. song->Mutex.unlock();
  138. }
  139. if (song->Thread.joinable())
  140. song->Thread.join();
  141. song->Thread = std::move(std::thread());
  142. }
  143. }
  144. extern "C" int native_midi_active(NativeMidiSong *song)
  145. {
  146. return (song && song->playing) ? 1 : 0;
  147. }
  148. extern "C" void native_midi_setvolume(NativeMidiSong *song, int volume)
  149. {
  150. // TODO
  151. }
  152. extern "C" const char *native_midi_error(NativeMidiSong *song)
  153. {
  154. return "";
  155. }