@@ -12,12 +12,13 @@
#ifndef __MORPHOS__
#include "../stdafx.h"
#include "../debug.h"
#include "../string_func.h"
#include "../sound/sound_driver.hpp"
#include "../video/video_driver.hpp"
#include "../gfx_func.h"
#include "extmidi.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
@@ -105,10 +106,30 @@ void MusicDriver_ExtMidi::DoPlay()
break;
}
void MusicDriver_ExtMidi::DoStop()
{
if (this->pid != -1) kill(this->pid, SIGTERM);
if (this->pid <= 0) return;
/* First try to gracefully stop for about five seconds;
* 5 seconds = 5000 milliseconds, 10 ms per cycle => 500 cycles. */
for (int i = 0; i < 500; i++) {
kill(this->pid, SIGTERM);
if (waitpid(this->pid, NULL, WNOHANG) == this->pid) {
/* It has shut down, so we are done */
this->pid = -1;
return;
/* Wait 10 milliseconds. */
CSleep(10);
DEBUG(driver, 0, "extmidi: gracefully stopping failed, trying the hard way");
/* Gracefully stopping failed. Do it the hard way
* and wait till the process finally died. */
kill(this->pid, SIGKILL);
waitpid(this->pid, NULL, 0);
#endif /* __MORPHOS__ */