From d9ace12fee24965baf6cf065691ed99c0d229ab1 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 17 Jan 2025 20:19:51 +0100 Subject: Detect MIDI port --- midiplay.cpp | 81 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 38 deletions(-) (limited to 'midiplay.cpp') diff --git a/midiplay.cpp b/midiplay.cpp index 34879ee..bc1ad08 100644 --- a/midiplay.cpp +++ b/midiplay.cpp @@ -2,6 +2,7 @@ #include #include +#include #include @@ -79,53 +80,57 @@ std::string to_xml(const std::vector& filelist, const std::string& } // namespace int main(int argc, char* argv[]) { - MIDIPlayer player; + try { + MIDIPlayer player; - std::string ok_data{"okOK"}; - std::string error_data{"errorGeneral Error"}; + std::string ok_data{"okOK"}; + std::string error_data{"errorGeneral Error"}; - int result = FCGX_Init(); - if (result != 0) { - return 1; // error on init - } - - FCGX_Request request; - - if (FCGX_InitRequest(&request, 0, 0) != 0) { - return 1; // error on init - } + int result = FCGX_Init(); + if (result != 0) { + return 1; // error on init + } - while (FCGX_Accept_r(&request) == 0) { - std::string method = FCGX_GetParam("REQUEST_METHOD", request.envp); + FCGX_Request request; - try { - if (method == "POST") { - FCGX_PutS("Content-Type: text/xml\r\n\r\n", request.out); + if (FCGX_InitRequest(&request, 0, 0) != 0) { + return 1; // error on init + } - PostData data{request}; - std::string command {getCommand(request)}; - if (command == "start") { - player.start(); - FCGX_PutS(ok_data.c_str(), request.out); - } else if (command == "stop") { - player.stop(); - FCGX_PutS(ok_data.c_str(), request.out); - } else if (command == "getlist") { - FCGX_PutS(to_xml(player.get_filelist(), player.get_file()).c_str(), request.out); - } else if (command == "setfile") { - std::string filename = data.getXMLElement("data.value"); - player.set_file(filename); - FCGX_PutS(ok_data.c_str(), request.out); + while (FCGX_Accept_r(&request) == 0) { + std::string method = FCGX_GetParam("REQUEST_METHOD", request.envp); + + try { + if (method == "POST") { + FCGX_PutS("Content-Type: text/xml\r\n\r\n", request.out); + + PostData data{request}; + std::string command {getCommand(request)}; + if (command == "start") { + player.start(); + FCGX_PutS(ok_data.c_str(), request.out); + } else if (command == "stop") { + player.stop(); + FCGX_PutS(ok_data.c_str(), request.out); + } else if (command == "getlist") { + FCGX_PutS(to_xml(player.get_filelist(), player.get_file()).c_str(), request.out); + } else if (command == "setfile") { + std::string filename = data.getXMLElement("data.value"); + player.set_file(filename); + FCGX_PutS(ok_data.c_str(), request.out); + } else { + FCGX_PutS(error_data.c_str(), request.out); + } } else { - FCGX_PutS(error_data.c_str(), request.out); + throw std::runtime_error(fmt::format("Bad request method: POST expected, got {}", method).c_str()); } - } else { - throw std::runtime_error(fmt::format("Bad request method: POST expected, got {}", method).c_str()); + } catch (const std::exception& ex) { + FCGX_PutS("Content-Type: text/xml\r\n\r\n", request.out); + FCGX_PutS(("error"s + ex.what() + "").c_str(), request.out); } - } catch (const std::exception& ex) { - FCGX_PutS("Content-Type: text/xml\r\n\r\n", request.out); - FCGX_PutS(("error"s + ex.what() + "").c_str(), request.out); } + } catch (const std::exception& ex) { + std::cerr << "Error: " << ex.what() << std::endl; } return 0; -- cgit v1.2.3