--- linuxsampler/trunk/src/network/lscpserver.cpp 2007/05/16 14:22:26 1187 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2007/10/11 18:53:29 1399 @@ -36,6 +36,42 @@ #include "../drivers/audio/AudioOutputDeviceFactory.h" #include "../drivers/midi/MidiInputDeviceFactory.h" + +/** + * Returns a copy of the given string where all special characters are + * replaced by LSCP escape sequences ("\xHH"). This function shall be used + * to escape LSCP response fields in case the respective response field is + * actually defined as using escape sequences in the LSCP specs. + * + * @e Caution: DO NOT use this function for escaping path based responses, + * use the Path class (src/common/Path.h) for this instead! + */ +static String _escapeLscpResponse(String txt) { + for (int i = 0; i < txt.length(); i++) { + const char c = txt.c_str()[i]; + if ( + !(c >= '0' && c <= '9') && + !(c >= 'a' && c <= 'z') && + !(c >= 'A' && c <= 'Z') && + !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') && + !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') && + !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') && + !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') && + !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') && + !(c == '@') && !(c == '[') && !(c == '\\') && !(c == ']') && + !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') && + !(c == '|') && !(c == '}') && !(c == '~') + ) { + // convert the "special" character into a "\xHH" LSCP escape sequence + char buf[5]; + snprintf(buf, sizeof(buf), "\\x%02x", static_cast(c)); + txt.replace(i, 1, buf); + i += 3; + } + } + return txt; +} + /** * Below are a few static members of the LSCPServer class. * The big assumption here is that LSCPServer is going to remain a singleton. @@ -52,6 +88,7 @@ fd_set LSCPServer::fdSet; int LSCPServer::currentSocket = -1; std::vector LSCPServer::Sessions = std::vector(); +std::vector::iterator itCurrentSession = std::vector::iterator(); std::map LSCPServer::bufferedNotifies = std::map(); std::map LSCPServer::bufferedCommands = std::map(); std::map< LSCPEvent::event_t, std::list > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list >(); @@ -84,6 +121,7 @@ LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO"); LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT"); LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); @@ -144,31 +182,36 @@ #if HAVE_SQLITE3 void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, Dir)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); } void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) { - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, Dir)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir))); } void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { - Dir = "'" + Dir + "'"; - NewName = "'" + NewName + "'"; + Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'"; + NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); } void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) { - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, Dir)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir))); } void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) { - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, Instr)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr))); } + void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { - Instr = "'" + Instr + "'"; - NewName = "'" + NewName + "'"; + Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'"; + NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); } + +void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId)); +} #endif // HAVE_SQLITE3 @@ -212,7 +255,7 @@ listen(hSocket, 1); Initialized.Set(true); - + // Registering event listeners pSampler->AddChannelCountListener(&eventHandler); pSampler->AddAudioDeviceCountListener(&eventHandler); @@ -321,12 +364,14 @@ int dummy; // just a temporary hack to fulfill the restart() function prototype restart(NULL, dummy); // restart the 'scanner' currentSocket = (*iter).hSession; //a hack + itCurrentSession = iter; // another hack dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str())); if ((*iter).bVerbose) { // if echo mode enabled AnswerClient(bufferedCommands[currentSocket]); } int result = yyparse(&(*iter)); currentSocket = -1; //continuation of a hack + itCurrentSession = Sessions.end(); // hack as well dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); if (result == LSCP_QUIT) { //Was it a quit command by any chance? CloseConnection(iter); @@ -419,6 +464,10 @@ return command.size(); } +extern yyparse_param_t* GetCurrentYaccSession() { + return &(*itCurrentSession); +} + /** * Will be called to try to read the command from the socket * If command is read, it will return true. Otherwise false is returned. @@ -606,7 +655,7 @@ EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet"); - return pEngineChannel; + return pEngineChannel; } /** @@ -755,7 +804,7 @@ LockRTNotify(); try { Engine* pEngine = EngineFactory::Create(EngineName); - result.Add("DESCRIPTION", pEngine->Description()); + result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description())); result.Add("VERSION", pEngine->Version()); EngineFactory::Destroy(pEngine); } @@ -828,9 +877,13 @@ if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); - result.Add("INSTRUMENT_FILE", InstrumentFileName); + result.Add("INSTRUMENT_FILE", + (InstrumentFileName != "NONE" && InstrumentFileName != "") ? + Path::fromPosix(InstrumentFileName).toLscp() : // TODO: assuming POSIX + InstrumentFileName + ); result.Add("INSTRUMENT_NR", InstrumentIndex); - result.Add("INSTRUMENT_NAME", InstrumentName); + result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName)); result.Add("INSTRUMENT_STATUS", InstrumentStatus); result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); result.Add("SOLO", Solo); @@ -1772,9 +1825,9 @@ std::map::iterator iter = mappings.find(idx); if (iter == mappings.end()) result.Error("there is no map entry with that index"); else { // found - result.Add("NAME", iter->second.Name); + result.Add("NAME", _escapeLscpResponse(iter->second.Name)); result.Add("ENGINE_NAME", iter->second.EngineName); - result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile); + result.Add("INSTRUMENT_FILE", Path::fromPosix(iter->second.InstrumentFile).toLscp()); //TODO: assuming POSIX result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); String instrumentName; Engine* pEngine = EngineFactory::Create(iter->second.EngineName); @@ -1787,7 +1840,7 @@ } EngineFactory::Destroy(pEngine); } - result.Add("INSTRUMENT_NAME", instrumentName); + result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName)); switch (iter->second.LoadMode) { case MidiInstrumentMapper::ON_DEMAND: result.Add("LOAD_MODE", "ON_DEMAND"); @@ -1942,7 +1995,7 @@ dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); LSCPResultSet result; try { - result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID)); + result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID))); result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); } catch (Exception e) { result.Error(e); @@ -1993,7 +2046,7 @@ LSCPResultSet result; try { EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); - + FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name); if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)"); @@ -2077,7 +2130,7 @@ try { EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); - + // gather audio routing informations String AudioRouting; for (int chan = 0; chan < pEngineChannel->Channels(); chan++) { @@ -2086,7 +2139,7 @@ } // success - result.Add("NAME", pFxSend->Name()); + result.Add("NAME", _escapeLscpResponse(pFxSend->Name())); result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); result.Add("LEVEL", ToString(pFxSend->Level())); result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); @@ -2152,6 +2205,28 @@ return result.Produce(); } +String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) { + dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel"); + Engine* pEngine = pEngineChannel->GetEngine(); + InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager(); + if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager"); + InstrumentManager::instrument_id_t instrumentID; + instrumentID.FileName = pEngineChannel->InstrumentFileName(); + instrumentID.Index = pEngineChannel->InstrumentIndex(); + pInstrumentManager->LaunchInstrumentEditor(instrumentID); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + /** * Will be called by the parser to reset a particular sampler channel. */ @@ -2187,8 +2262,10 @@ */ String LSCPServer::GetServerInfo() { dmsg(2,("LSCPServer: GetServerInfo()\n")); + const std::string description = + _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler"); LSCPResultSet result; - result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); + result.Add("DESCRIPTION", description); result.Add("VERSION", VERSION); result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); #if HAVE_SQLITE3 @@ -2196,7 +2273,7 @@ #else result.Add("INSTRUMENTS_DB_SUPPORT", "no"); #endif - + return result.Produce(); } @@ -2319,7 +2396,7 @@ for (int i = 0; i < dirs->size(); i++) { if (list != "") list += ","; - list += "'" + dirs->at(i) + "'"; + list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'"; } result.Add(list); @@ -2339,7 +2416,7 @@ try { DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); - result.Add("DESCRIPTION", info.Description); + result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description)); result.Add("CREATED", info.Created); result.Add("MODIFIED", info.Modified); } catch (Exception e) { @@ -2411,12 +2488,15 @@ return result.Produce(); } -String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index) { - dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index)); +String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) { + dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); LSCPResultSet result; #if HAVE_SQLITE3 try { - InstrumentsDb::GetInstrumentsDb()->AddInstruments(DbDir, FilePath, Index); + int id; + InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); + id = db->AddInstruments(DbDir, FilePath, Index, bBackground); + if (bBackground) result = id; } catch (Exception e) { result.Error(e); } @@ -2426,27 +2506,24 @@ return result.Produce(); } -String LSCPServer::AddDbInstrumentsFlat(String DbDir, String FsDir) { - dmsg(2,("LSCPServer: AddDbInstrumentsFlat(DbDir=%s,FilePath=%s)\n", DbDir.c_str(), FsDir.c_str())); +String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) { + dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground)); LSCPResultSet result; #if HAVE_SQLITE3 try { - InstrumentsDb::GetInstrumentsDb()->AddInstrumentsRecursive(DbDir, FsDir, true); - } catch (Exception e) { - result.Error(e); - } -#else - result.Error(String(DOESNT_HAVE_SQLITE3), 0); -#endif - return result.Produce(); -} + int id; + InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); + if (ScanMode.compare("RECURSIVE") == 0) { + id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground); + } else if (ScanMode.compare("NON_RECURSIVE") == 0) { + id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground); + } else if (ScanMode.compare("FLAT") == 0) { + id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground); + } else { + throw Exception("Unknown scan mode: " + ScanMode); + } -String LSCPServer::AddDbInstrumentsNonrecursive(String DbDir, String FsDir) { - dmsg(2,("LSCPServer: AddDbInstrumentsNonrecursive(DbDir=%s,FilePath=%s)\n", DbDir.c_str(), FsDir.c_str())); - LSCPResultSet result; -#if HAVE_SQLITE3 - try { - InstrumentsDb::GetInstrumentsDb()->AddInstrumentsNonrecursive(DbDir, FsDir); + if (bBackground) result = id; } catch (Exception e) { result.Error(e); } @@ -2496,7 +2573,7 @@ for (int i = 0; i < instrs->size(); i++) { if (list != "") list += ","; - list += "'" + instrs->at(i) + "'"; + list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'"; } result.Add(list); @@ -2523,11 +2600,31 @@ result.Add("SIZE", (int)info.Size); result.Add("CREATED", info.Created); result.Add("MODIFIED", info.Modified); - result.Add("DESCRIPTION", FilterEndlines(info.Description)); + result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description)); result.Add("IS_DRUM", info.IsDrum); - result.Add("PRODUCT", FilterEndlines(info.Product)); - result.Add("ARTISTS", FilterEndlines(info.Artists)); - result.Add("KEYWORDS", FilterEndlines(info.Keywords)); + result.Add("PRODUCT", InstrumentsDb::toEscapedText(info.Product)); + result.Add("ARTISTS", InstrumentsDb::toEscapedText(info.Artists)); + result.Add("KEYWORDS", InstrumentsDb::toEscapedText(info.Keywords)); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::GetDbInstrumentsJobInfo(int JobId) { + dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId); + + result.Add("FILES_TOTAL", job.FilesTotal); + result.Add("FILES_SCANNED", job.FilesScanned); + result.Add("SCANNING", job.Scanning); + result.Add("STATUS", job.Status); } catch (Exception e) { result.Error(e); } @@ -2624,7 +2721,7 @@ for (int i = 0; i < pDirectories->size(); i++) { if (list != "") list += ","; - list += "'" + pDirectories->at(i) + "'"; + list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'"; } result.Add(list); @@ -2680,7 +2777,7 @@ for (int i = 0; i < pInstruments->size(); i++) { if (list != "") list += ","; - list += "'" + pInstruments->at(i) + "'"; + list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'"; } result.Add(list); @@ -2693,6 +2790,21 @@ return result.Produce(); } +String LSCPServer::FormatInstrumentsDb() { + dmsg(2,("LSCPServer: FormatInstrumentsDb()\n")); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->Format(); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + /** * Will be called by the parser to enable or disable echo mode; if echo @@ -2712,13 +2824,3 @@ } return result.Produce(); } - -String LSCPServer::FilterEndlines(String s) { - String s2 = s; - for (int i = 0; i < s2.length(); i++) { - if (s2.at(i) == '\r') s2.at(i) = ' '; - else if (s2.at(i) == '\n') s2.at(i) = ' '; - } - - return s2; -}