25 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
26 |
#include "lscpevent.h" |
#include "lscpevent.h" |
27 |
|
|
28 |
|
#if defined(WIN32) |
29 |
|
#else |
30 |
#include <fcntl.h> |
#include <fcntl.h> |
31 |
|
#endif |
32 |
|
|
33 |
#if ! HAVE_SQLITE3 |
#if ! HAVE_SQLITE3 |
34 |
#define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built." |
#define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built." |
39 |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
40 |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
41 |
|
|
42 |
|
|
43 |
|
/** |
44 |
|
* Returns a copy of the given string where all special characters are |
45 |
|
* replaced by LSCP escape sequences ("\xHH"). This function shall be used |
46 |
|
* to escape LSCP response fields in case the respective response field is |
47 |
|
* actually defined as using escape sequences in the LSCP specs. |
48 |
|
* |
49 |
|
* @e Caution: DO NOT use this function for escaping path based responses, |
50 |
|
* use the Path class (src/common/Path.h) for this instead! |
51 |
|
*/ |
52 |
|
static String _escapeLscpResponse(String txt) { |
53 |
|
for (int i = 0; i < txt.length(); i++) { |
54 |
|
const char c = txt.c_str()[i]; |
55 |
|
if ( |
56 |
|
!(c >= '0' && c <= '9') && |
57 |
|
!(c >= 'a' && c <= 'z') && |
58 |
|
!(c >= 'A' && c <= 'Z') && |
59 |
|
!(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') && |
60 |
|
!(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') && |
61 |
|
!(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') && |
62 |
|
!(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') && |
63 |
|
!(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') && |
64 |
|
!(c == '@') && !(c == '[') && !(c == ']') && |
65 |
|
!(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') && |
66 |
|
!(c == '|') && !(c == '}') && !(c == '~') |
67 |
|
) { |
68 |
|
// convert the "special" character into a "\xHH" LSCP escape sequence |
69 |
|
char buf[5]; |
70 |
|
snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c)); |
71 |
|
txt.replace(i, 1, buf); |
72 |
|
i += 3; |
73 |
|
} |
74 |
|
} |
75 |
|
return txt; |
76 |
|
} |
77 |
|
|
78 |
/** |
/** |
79 |
* Below are a few static members of the LSCPServer class. |
* Below are a few static members of the LSCPServer class. |
80 |
* The big assumption here is that LSCPServer is going to remain a singleton. |
* The big assumption here is that LSCPServer is going to remain a singleton. |
132 |
} |
} |
133 |
|
|
134 |
LSCPServer::~LSCPServer() { |
LSCPServer::~LSCPServer() { |
135 |
|
#if defined(WIN32) |
136 |
|
if (hSocket >= 0) closesocket(hSocket); |
137 |
|
#else |
138 |
if (hSocket >= 0) close(hSocket); |
if (hSocket >= 0) close(hSocket); |
139 |
|
#endif |
140 |
} |
} |
141 |
|
|
142 |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
198 |
|
|
199 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { |
200 |
Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'"; |
Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'"; |
201 |
NewName = "'" + InstrumentsDb::toEscapedName(NewName) + "'"; |
NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; |
202 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); |
203 |
} |
} |
204 |
|
|
212 |
|
|
213 |
void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { |
void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { |
214 |
Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'"; |
Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'"; |
215 |
NewName = "'" + InstrumentsDb::toEscapedName(NewName) + "'"; |
NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; |
216 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); |
217 |
} |
} |
218 |
|
|
237 |
} |
} |
238 |
|
|
239 |
int LSCPServer::Main() { |
int LSCPServer::Main() { |
240 |
|
#if defined(WIN32) |
241 |
|
WSADATA wsaData; |
242 |
|
int iResult; |
243 |
|
iResult = WSAStartup(MAKEWORD(2,2), &wsaData); |
244 |
|
if (iResult != 0) { |
245 |
|
std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n"; |
246 |
|
exit(EXIT_FAILURE); |
247 |
|
} |
248 |
|
#endif |
249 |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
250 |
if (hSocket < 0) { |
if (hSocket < 0) { |
251 |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
259 |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
260 |
if (trial > LSCP_SERVER_BIND_TIMEOUT) { |
if (trial > LSCP_SERVER_BIND_TIMEOUT) { |
261 |
std::cerr << "gave up!" << std::endl; |
std::cerr << "gave up!" << std::endl; |
262 |
|
#if defined(WIN32) |
263 |
|
closesocket(hSocket); |
264 |
|
#else |
265 |
close(hSocket); |
close(hSocket); |
266 |
|
#endif |
267 |
//return -1; |
//return -1; |
268 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
269 |
} |
} |
345 |
continue; //Nothing try again |
continue; //Nothing try again |
346 |
if (retval == -1) { |
if (retval == -1) { |
347 |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
348 |
|
#if defined(WIN32) |
349 |
|
closesocket(hSocket); |
350 |
|
#else |
351 |
close(hSocket); |
close(hSocket); |
352 |
|
#endif |
353 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
354 |
} |
} |
355 |
|
|
361 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
362 |
} |
} |
363 |
|
|
364 |
|
#if defined(WIN32) |
365 |
|
u_long nonblock_io = 1; |
366 |
|
if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) { |
367 |
|
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
368 |
|
exit(EXIT_FAILURE); |
369 |
|
} |
370 |
|
#else |
371 |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
372 |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
373 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
374 |
} |
} |
375 |
|
#endif |
376 |
|
|
377 |
// Parser initialization |
// Parser initialization |
378 |
yyparse_param_t yyparse_param; |
yyparse_param_t yyparse_param; |
431 |
NotifyMutex.Lock(); |
NotifyMutex.Lock(); |
432 |
bufferedCommands.erase(socket); |
bufferedCommands.erase(socket); |
433 |
bufferedNotifies.erase(socket); |
bufferedNotifies.erase(socket); |
434 |
|
#if defined(WIN32) |
435 |
|
closesocket(socket); |
436 |
|
#else |
437 |
close(socket); |
close(socket); |
438 |
|
#endif |
439 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
440 |
} |
} |
441 |
|
|
514 |
char c; |
char c; |
515 |
int i = 0; |
int i = 0; |
516 |
while (true) { |
while (true) { |
517 |
|
#if defined(WIN32) |
518 |
|
int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now |
519 |
|
#else |
520 |
int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now |
int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now |
521 |
|
#endif |
522 |
if (result == 0) { //socket was selected, so 0 here means client has closed the connection |
if (result == 0) { //socket was selected, so 0 here means client has closed the connection |
523 |
CloseConnection(iter); |
CloseConnection(iter); |
524 |
break; |
break; |
533 |
} |
} |
534 |
bufferedCommands[socket] += c; |
bufferedCommands[socket] += c; |
535 |
} |
} |
536 |
|
#if defined(WIN32) |
537 |
|
if (result == SOCKET_ERROR) { |
538 |
|
int wsa_lasterror = WSAGetLastError(); |
539 |
|
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
540 |
|
return false; |
541 |
|
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
542 |
|
CloseConnection(iter); |
543 |
|
break; |
544 |
|
} |
545 |
|
#else |
546 |
if (result == -1) { |
if (result == -1) { |
547 |
if (errno == EAGAIN) //Would block, try again later. |
if (errno == EAGAIN) //Would block, try again later. |
548 |
return false; |
return false; |
581 |
CloseConnection(iter); |
CloseConnection(iter); |
582 |
break; |
break; |
583 |
} |
} |
584 |
|
#endif |
585 |
} |
} |
586 |
return false; |
return false; |
587 |
} |
} |
855 |
LockRTNotify(); |
LockRTNotify(); |
856 |
try { |
try { |
857 |
Engine* pEngine = EngineFactory::Create(EngineName); |
Engine* pEngine = EngineFactory::Create(EngineName); |
858 |
result.Add("DESCRIPTION", pEngine->Description()); |
result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description())); |
859 |
result.Add("VERSION", pEngine->Version()); |
result.Add("VERSION", pEngine->Version()); |
860 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
861 |
} |
} |
928 |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
929 |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
930 |
|
|
931 |
|
// convert the filename into the correct encoding as defined for LSCP |
932 |
|
// (especially in terms of special characters -> escape sequences) |
933 |
|
if (InstrumentFileName != "NONE" && InstrumentFileName != "") { |
934 |
|
#if WIN32 |
935 |
|
InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp(); |
936 |
|
#else |
937 |
|
// assuming POSIX |
938 |
|
InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp(); |
939 |
|
#endif |
940 |
|
} |
941 |
|
|
942 |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
943 |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
944 |
result.Add("INSTRUMENT_NAME", InstrumentName); |
result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName)); |
945 |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
946 |
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
947 |
result.Add("SOLO", Solo); |
result.Add("SOLO", Solo); |
1883 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx); |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx); |
1884 |
if (iter == mappings.end()) result.Error("there is no map entry with that index"); |
if (iter == mappings.end()) result.Error("there is no map entry with that index"); |
1885 |
else { // found |
else { // found |
1886 |
result.Add("NAME", iter->second.Name); |
|
1887 |
|
// convert the filename into the correct encoding as defined for LSCP |
1888 |
|
// (especially in terms of special characters -> escape sequences) |
1889 |
|
#if WIN32 |
1890 |
|
const String instrumentFileName = Path::fromWindows(iter->second.InstrumentFile).toLscp(); |
1891 |
|
#else |
1892 |
|
// assuming POSIX |
1893 |
|
const String instrumentFileName = Path::fromPosix(iter->second.InstrumentFile).toLscp(); |
1894 |
|
#endif |
1895 |
|
|
1896 |
|
result.Add("NAME", _escapeLscpResponse(iter->second.Name)); |
1897 |
result.Add("ENGINE_NAME", iter->second.EngineName); |
result.Add("ENGINE_NAME", iter->second.EngineName); |
1898 |
result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile); |
result.Add("INSTRUMENT_FILE", instrumentFileName); |
1899 |
result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); |
result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); |
1900 |
String instrumentName; |
String instrumentName; |
1901 |
Engine* pEngine = EngineFactory::Create(iter->second.EngineName); |
Engine* pEngine = EngineFactory::Create(iter->second.EngineName); |
1908 |
} |
} |
1909 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
1910 |
} |
} |
1911 |
result.Add("INSTRUMENT_NAME", instrumentName); |
result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName)); |
1912 |
switch (iter->second.LoadMode) { |
switch (iter->second.LoadMode) { |
1913 |
case MidiInstrumentMapper::ON_DEMAND: |
case MidiInstrumentMapper::ON_DEMAND: |
1914 |
result.Add("LOAD_MODE", "ON_DEMAND"); |
result.Add("LOAD_MODE", "ON_DEMAND"); |
2063 |
dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); |
dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); |
2064 |
LSCPResultSet result; |
LSCPResultSet result; |
2065 |
try { |
try { |
2066 |
result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID)); |
result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID))); |
2067 |
result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); |
result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); |
2068 |
} catch (Exception e) { |
} catch (Exception e) { |
2069 |
result.Error(e); |
result.Error(e); |
2207 |
} |
} |
2208 |
|
|
2209 |
// success |
// success |
2210 |
result.Add("NAME", pFxSend->Name()); |
result.Add("NAME", _escapeLscpResponse(pFxSend->Name())); |
2211 |
result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); |
result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); |
2212 |
result.Add("LEVEL", ToString(pFxSend->Level())); |
result.Add("LEVEL", ToString(pFxSend->Level())); |
2213 |
result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); |
result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); |
2330 |
*/ |
*/ |
2331 |
String LSCPServer::GetServerInfo() { |
String LSCPServer::GetServerInfo() { |
2332 |
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
2333 |
|
const std::string description = |
2334 |
|
_escapeLscpResponse("LinuxSampler - modular, streaming capable sampler"); |
2335 |
LSCPResultSet result; |
LSCPResultSet result; |
2336 |
result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); |
result.Add("DESCRIPTION", description); |
2337 |
result.Add("VERSION", VERSION); |
result.Add("VERSION", VERSION); |
2338 |
result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); |
result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); |
2339 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
2464 |
|
|
2465 |
for (int i = 0; i < dirs->size(); i++) { |
for (int i = 0; i < dirs->size(); i++) { |
2466 |
if (list != "") list += ","; |
if (list != "") list += ","; |
2467 |
|
list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'"; |
|
if (Recursive) list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'"; |
|
|
else list += "'" + InstrumentsDb::toEscapedName(dirs->at(i)) + "'"; |
|
2468 |
} |
} |
2469 |
|
|
2470 |
result.Add(list); |
result.Add(list); |
2484 |
try { |
try { |
2485 |
DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); |
DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); |
2486 |
|
|
2487 |
result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description)); |
result.Add("DESCRIPTION", _escapeLscpResponse(info.Description)); |
2488 |
result.Add("CREATED", info.Created); |
result.Add("CREATED", info.Created); |
2489 |
result.Add("MODIFIED", info.Modified); |
result.Add("MODIFIED", info.Modified); |
2490 |
} catch (Exception e) { |
} catch (Exception e) { |
2641 |
|
|
2642 |
for (int i = 0; i < instrs->size(); i++) { |
for (int i = 0; i < instrs->size(); i++) { |
2643 |
if (list != "") list += ","; |
if (list != "") list += ","; |
2644 |
|
list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'"; |
|
if (Recursive) list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'"; |
|
|
else list += "'" + InstrumentsDb::toEscapedName(instrs->at(i)) + "'"; |
|
2645 |
} |
} |
2646 |
|
|
2647 |
result.Add(list); |
result.Add(list); |
2668 |
result.Add("SIZE", (int)info.Size); |
result.Add("SIZE", (int)info.Size); |
2669 |
result.Add("CREATED", info.Created); |
result.Add("CREATED", info.Created); |
2670 |
result.Add("MODIFIED", info.Modified); |
result.Add("MODIFIED", info.Modified); |
2671 |
result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description)); |
result.Add("DESCRIPTION", _escapeLscpResponse(info.Description)); |
2672 |
result.Add("IS_DRUM", info.IsDrum); |
result.Add("IS_DRUM", info.IsDrum); |
2673 |
result.Add("PRODUCT", InstrumentsDb::toEscapedText(info.Product)); |
result.Add("PRODUCT", _escapeLscpResponse(info.Product)); |
2674 |
result.Add("ARTISTS", InstrumentsDb::toEscapedText(info.Artists)); |
result.Add("ARTISTS", _escapeLscpResponse(info.Artists)); |
2675 |
result.Add("KEYWORDS", InstrumentsDb::toEscapedText(info.Keywords)); |
result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords)); |
2676 |
} catch (Exception e) { |
} catch (Exception e) { |
2677 |
result.Error(e); |
result.Error(e); |
2678 |
} |
} |
2852 |
} catch (Exception e) { |
} catch (Exception e) { |
2853 |
result.Error(e); |
result.Error(e); |
2854 |
} |
} |
2855 |
|
#else |
2856 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
2857 |
|
#endif |
2858 |
|
return result.Produce(); |
2859 |
|
} |
2860 |
|
|
2861 |
|
String LSCPServer::FormatInstrumentsDb() { |
2862 |
|
dmsg(2,("LSCPServer: FormatInstrumentsDb()\n")); |
2863 |
|
LSCPResultSet result; |
2864 |
|
#if HAVE_SQLITE3 |
2865 |
|
try { |
2866 |
|
InstrumentsDb::GetInstrumentsDb()->Format(); |
2867 |
|
} catch (Exception e) { |
2868 |
|
result.Error(e); |
2869 |
|
} |
2870 |
#else |
#else |
2871 |
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
2872 |
#endif |
#endif |