diff options
-rw-r--r-- | Makefile | 15 | ||||
-rw-r--r-- | html/index.html | 63 | ||||
-rw-r--r-- | midiplay.cpp | 60 |
3 files changed, 138 insertions, 0 deletions
@@ -1,4 +1,19 @@ TARGET=midiplay +all: $(TARGET) + play: aplaymidi -p24 locked_out_of_heaven.midi + +run-fcgi: + spawn-fcgi -a 127.0.0.1 -p 9090 -n -- ./midiplay + +midiplay.o: midiplay.cpp + g++ -Wall -g -O2 -fPIC -o $@ -c $^ + +$(TARGET): midiplay.o + g++ -Wall -g -O2 -fPIC -o $@ $^ -lfcgi -lreichwein -lfmt + + +clean: + -rm -rf $(TARGET) diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..a27b654 --- /dev/null +++ b/html/index.html @@ -0,0 +1,63 @@ +<!DOCTYPE html> +<html> + <head> + <title>MIDIPLAY</title> + +<style> +.button{ + width: 200px; + height: 150px; + font-size: 20px; +} +</style> +<script type="text/javascript"> + function playbutton_clicked() + { + var element = document.getElementById("playbutton"); + if (element.innerHTML == "Play") { + element.innerHTML = "Stop"; + } else { + element.innerHTML = "Play"; + } + + var xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function() { + if (this.readyState != 4) { + return; + } + if (this.status != 200) { + document.getElementById("status").innerHTML = "HTTP error"; + } else { + var xml = xhr.responseXML; + var value = xml.getElementsByTagName("value1")[0].childNodes[0].nodeValue; + document.getElementById("songlist").innerHTML = value; + } + } + + xhr.open("POST", "midiplay.fcgi" + "?command=list", true); + xhr.setRequestHeader("Content-type", "text/xml"); + xhr.send("<data><value1>3</value1></data>"); + } + + function startup() { + document.getElementById("playbutton").onclick = playbutton_clicked; + + document.getElementById("songlist").innerHTML = "01 Locked Out Of Heaven"; + } + +</script> + </head> + <body onload="startup();"> +MIDIPLAY + +<br/> +<br/> +<div id="songlist"> +</div> +<br/> +<br/> +<button id="playbutton" class="button">Play</button> + + </body> +</html> diff --git a/midiplay.cpp b/midiplay.cpp new file mode 100644 index 0000000..75f33da --- /dev/null +++ b/midiplay.cpp @@ -0,0 +1,60 @@ +#include <string> + +#include <fcgiapp.h> + +#include <fmt/format.h> + +std::string getPostData(FCGX_Request& request) +{ + std::string result; + std::string contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp)); + int contentLength = std::stoul(contentLengthString); + + if (contentLength < 1) { + return "Bad content length"; + } else { + result.resize(contentLength); + + unsigned int status = FCGX_GetStr(result.data(), result.size(), request.in); + if (status != result.size()) { + return fmt::format("Read error: {}/{}", status, result); + } + + return result; + } +} + +int main(int argc, char* argv[]) { + + 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 + } + + while (FCGX_Accept_r(&request) == 0) { + std::string query = FCGX_GetParam("QUERY_STRING", request.envp); + + std::string method = FCGX_GetParam("REQUEST_METHOD", request.envp); + + if (method == "POST") { + FCGX_PutS("Content-Type: text/xml\r\n\r\n", request.out); + + std::string data = getPostData(request); + if (data == "<data><value1>3</value1></data>") + FCGX_PutS("<data><value1>4</value1></data>", request.out); + } else { + FCGX_PutS("Content-Type: text/text\r\n\r\n", request.out); + FCGX_PutS("Bad request method: POST expected", request.out); + } + + } + + return 0; +} + |