/[svn]/linuxsampler/trunk/src/network/lscpserver.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/network/lscpserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1345 by iliev, Thu Sep 13 21:46:25 2007 UTC revision 1471 by schoenebeck, Mon Nov 5 13:56:26 2007 UTC
# Line 36  Line 36 
36  #include "../drivers/audio/AudioOutputDeviceFactory.h"  #include "../drivers/audio/AudioOutputDeviceFactory.h"
37  #include "../drivers/midi/MidiInputDeviceFactory.h"  #include "../drivers/midi/MidiInputDeviceFactory.h"
38    
39    
40    /**
41     * Returns a copy of the given string where all special characters are
42     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
43     * to escape LSCP response fields in case the respective response field is
44     * actually defined as using escape sequences in the LSCP specs.
45     *
46     * @e Caution: DO NOT use this function for escaping path based responses,
47     * use the Path class (src/common/Path.h) for this instead!
48     */
49    static String _escapeLscpResponse(String txt) {
50        for (int i = 0; i < txt.length(); i++) {
51            const char c = txt.c_str()[i];
52            if (
53                !(c >= '0' && c <= '9') &&
54                !(c >= 'a' && c <= 'z') &&
55                !(c >= 'A' && c <= 'Z') &&
56                !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
57                !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
58                !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
59                !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
60                !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
61                !(c == '@') && !(c == '[') && !(c == ']') &&
62                !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
63                !(c == '|') && !(c == '}') && !(c == '~')
64            ) {
65                // convert the "special" character into a "\xHH" LSCP escape sequence
66                char buf[5];
67                snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
68                txt.replace(i, 1, buf);
69                i += 3;
70            }
71        }
72        return txt;
73    }
74    
75  /**  /**
76   * Below are a few static members of the LSCPServer class.   * Below are a few static members of the LSCPServer class.
77   * 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.
# Line 155  void LSCPServer::DbInstrumentsEventHandl Line 191  void LSCPServer::DbInstrumentsEventHandl
191    
192  void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {  void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
193      Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";      Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
194      NewName = "'" + InstrumentsDb::toEscapedName(NewName) + "'";      NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
195      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
196  }  }
197    
# Line 169  void LSCPServer::DbInstrumentsEventHandl Line 205  void LSCPServer::DbInstrumentsEventHandl
205    
206  void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {  void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
207      Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";      Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
208      NewName = "'" + InstrumentsDb::toEscapedName(NewName) + "'";      NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
209      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
210  }  }
211    
# Line 768  String LSCPServer::GetEngineInfo(String Line 804  String LSCPServer::GetEngineInfo(String
804      LockRTNotify();      LockRTNotify();
805      try {      try {
806          Engine* pEngine = EngineFactory::Create(EngineName);          Engine* pEngine = EngineFactory::Create(EngineName);
807          result.Add("DESCRIPTION", pEngine->Description());          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
808          result.Add("VERSION",     pEngine->Version());          result.Add("VERSION",     pEngine->Version());
809          EngineFactory::Destroy(pEngine);          EngineFactory::Destroy(pEngine);
810      }      }
# Line 841  String LSCPServer::GetChannelInfo(uint u Line 877  String LSCPServer::GetChannelInfo(uint u
877          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
878          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
879    
880            // convert the filename into the correct encoding as defined for LSCP
881            // (especially in terms of special characters -> escape sequences)
882            if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
883    #if WIN32
884                InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
885    #else
886                // assuming POSIX
887                InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
888    #endif
889            }
890    
891          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
892          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
893          result.Add("INSTRUMENT_NAME", InstrumentName);          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
894          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
895          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
896          result.Add("SOLO", Solo);          result.Add("SOLO", Solo);
# Line 1785  String LSCPServer::GetMidiInstrumentMapp Line 1832  String LSCPServer::GetMidiInstrumentMapp
1832          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);
1833          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");
1834          else { // found          else { // found
1835              result.Add("NAME", iter->second.Name);  
1836                // convert the filename into the correct encoding as defined for LSCP
1837                // (especially in terms of special characters -> escape sequences)
1838    #if WIN32
1839                const String instrumentFileName = Path::fromWindows(iter->second.InstrumentFile).toLscp();
1840    #else
1841                // assuming POSIX
1842                const String instrumentFileName = Path::fromPosix(iter->second.InstrumentFile).toLscp();
1843    #endif
1844    
1845                result.Add("NAME", _escapeLscpResponse(iter->second.Name));
1846              result.Add("ENGINE_NAME", iter->second.EngineName);              result.Add("ENGINE_NAME", iter->second.EngineName);
1847              result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile);              result.Add("INSTRUMENT_FILE", instrumentFileName);
1848              result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);              result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);
1849              String instrumentName;              String instrumentName;
1850              Engine* pEngine = EngineFactory::Create(iter->second.EngineName);              Engine* pEngine = EngineFactory::Create(iter->second.EngineName);
# Line 1800  String LSCPServer::GetMidiInstrumentMapp Line 1857  String LSCPServer::GetMidiInstrumentMapp
1857                  }                  }
1858                  EngineFactory::Destroy(pEngine);                  EngineFactory::Destroy(pEngine);
1859              }              }
1860              result.Add("INSTRUMENT_NAME", instrumentName);              result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
1861              switch (iter->second.LoadMode) {              switch (iter->second.LoadMode) {
1862                  case MidiInstrumentMapper::ON_DEMAND:                  case MidiInstrumentMapper::ON_DEMAND:
1863                      result.Add("LOAD_MODE", "ON_DEMAND");                      result.Add("LOAD_MODE", "ON_DEMAND");
# Line 1955  String LSCPServer::GetMidiInstrumentMap( Line 2012  String LSCPServer::GetMidiInstrumentMap(
2012      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2013      LSCPResultSet result;      LSCPResultSet result;
2014      try {      try {
2015          result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID));          result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2016          result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);          result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2017      } catch (Exception e) {      } catch (Exception e) {
2018          result.Error(e);          result.Error(e);
# Line 2099  String LSCPServer::GetFxSendInfo(uint ui Line 2156  String LSCPServer::GetFxSendInfo(uint ui
2156          }          }
2157    
2158          // success          // success
2159          result.Add("NAME", pFxSend->Name());          result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2160          result.Add("MIDI_CONTROLLER", pFxSend->MidiController());          result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2161          result.Add("LEVEL", ToString(pFxSend->Level()));          result.Add("LEVEL", ToString(pFxSend->Level()));
2162          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
# Line 2222  String LSCPServer::ResetSampler() { Line 2279  String LSCPServer::ResetSampler() {
2279   */   */
2280  String LSCPServer::GetServerInfo() {  String LSCPServer::GetServerInfo() {
2281      dmsg(2,("LSCPServer: GetServerInfo()\n"));      dmsg(2,("LSCPServer: GetServerInfo()\n"));
2282        const std::string description =
2283            _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2284      LSCPResultSet result;      LSCPResultSet result;
2285      result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");      result.Add("DESCRIPTION", description);
2286      result.Add("VERSION", VERSION);      result.Add("VERSION", VERSION);
2287      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2288  #if HAVE_SQLITE3  #if HAVE_SQLITE3
# Line 2354  String LSCPServer::GetDbInstrumentDirect Line 2413  String LSCPServer::GetDbInstrumentDirect
2413    
2414          for (int i = 0; i < dirs->size(); i++) {          for (int i = 0; i < dirs->size(); i++) {
2415              if (list != "") list += ",";              if (list != "") list += ",";
2416                list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
             if (Recursive) list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";  
             else list += "'" + InstrumentsDb::toEscapedName(dirs->at(i)) + "'";  
2417          }          }
2418    
2419          result.Add(list);          result.Add(list);
# Line 2376  String LSCPServer::GetDbInstrumentDirect Line 2433  String LSCPServer::GetDbInstrumentDirect
2433      try {      try {
2434          DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);          DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
2435    
2436          result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description));          result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2437          result.Add("CREATED", info.Created);          result.Add("CREATED", info.Created);
2438          result.Add("MODIFIED", info.Modified);          result.Add("MODIFIED", info.Modified);
2439      } catch (Exception e) {      } catch (Exception e) {
# Line 2533  String LSCPServer::GetDbInstruments(Stri Line 2590  String LSCPServer::GetDbInstruments(Stri
2590    
2591          for (int i = 0; i < instrs->size(); i++) {          for (int i = 0; i < instrs->size(); i++) {
2592              if (list != "") list += ",";              if (list != "") list += ",";
2593                list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
             if (Recursive) list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";  
             else list += "'" + InstrumentsDb::toEscapedName(instrs->at(i)) + "'";  
2594          }          }
2595    
2596          result.Add(list);          result.Add(list);
# Line 2562  String LSCPServer::GetDbInstrumentInfo(S Line 2617  String LSCPServer::GetDbInstrumentInfo(S
2617          result.Add("SIZE", (int)info.Size);          result.Add("SIZE", (int)info.Size);
2618          result.Add("CREATED", info.Created);          result.Add("CREATED", info.Created);
2619          result.Add("MODIFIED", info.Modified);          result.Add("MODIFIED", info.Modified);
2620          result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description));          result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2621          result.Add("IS_DRUM", info.IsDrum);          result.Add("IS_DRUM", info.IsDrum);
2622          result.Add("PRODUCT", InstrumentsDb::toEscapedText(info.Product));          result.Add("PRODUCT", _escapeLscpResponse(info.Product));
2623          result.Add("ARTISTS", InstrumentsDb::toEscapedText(info.Artists));          result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
2624          result.Add("KEYWORDS", InstrumentsDb::toEscapedText(info.Keywords));          result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
2625      } catch (Exception e) {      } catch (Exception e) {
2626           result.Error(e);           result.Error(e);
2627      }      }
# Line 2746  String LSCPServer::FindDbInstruments(Str Line 2801  String LSCPServer::FindDbInstruments(Str
2801      } catch (Exception e) {      } catch (Exception e) {
2802           result.Error(e);           result.Error(e);
2803      }      }
2804    #else
2805        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2806    #endif
2807        return result.Produce();
2808    }
2809    
2810    String LSCPServer::FormatInstrumentsDb() {
2811        dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
2812        LSCPResultSet result;
2813    #if HAVE_SQLITE3
2814        try {
2815            InstrumentsDb::GetInstrumentsDb()->Format();
2816        } catch (Exception e) {
2817             result.Error(e);
2818        }
2819  #else  #else
2820      result.Error(String(DOESNT_HAVE_SQLITE3), 0);      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2821  #endif  #endif

Legend:
Removed from v.1345  
changed lines
  Added in v.1471

  ViewVC Help
Powered by ViewVC