/[svn]/liblscp/trunk/src/client.c
ViewVC logotype

Diff of /liblscp/trunk/src/client.c

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

revision 948 by capela, Tue Nov 28 15:31:20 2006 UTC revision 977 by capela, Sun Dec 17 15:08:35 2006 UTC
# Line 39  static lscp_status_t    _lscp_client_evt Line 39  static lscp_status_t    _lscp_client_evt
39    
40  static void _lscp_client_evt_proc ( void *pvClient )  static void _lscp_client_evt_proc ( void *pvClient )
41  {  {
42      lscp_client_t *pClient = (lscp_client_t *) pvClient;          lscp_client_t *pClient = (lscp_client_t *) pvClient;
43    
44      fd_set fds;                         // File descriptor list for select().          fd_set fds;                         // File descriptor list for select().
45      int    fd, fdmax;                   // Maximum file descriptor number.          int    fd, fdmax;                   // Maximum file descriptor number.
46      struct timeval tv;                  // For specifying a timeout value.          struct timeval tv;                  // For specifying a timeout value.
47      int    iSelect;                     // Holds select return status.          int    iSelect;                     // Holds select return status.
48      int    iTimeout;          int    iTimeout;
49    
50      char   achBuffer[LSCP_BUFSIZ];          char   achBuffer[LSCP_BUFSIZ];
51      int    cchBuffer;          int    cchBuffer;
52      const char *pszSeps = ":\r\n";          const char *pszSeps = ":\r\n";
53      char * pszToken;          char * pszToken;
54      char * pch;          char * pch;
55      int     cchToken;          int     cchToken;
56      lscp_event_t event;          lscp_event_t event;
57    
58  #ifdef DEBUG  #ifdef DEBUG
59      fprintf(stderr, "_lscp_client_evt_proc: Client waiting for events.\n");          fprintf(stderr, "_lscp_client_evt_proc: Client waiting for events.\n");
60  #endif  #endif
61    
62      while (pClient->evt.iState) {          while (pClient->evt.iState) {
63    
64          // Prepare for waiting on select...                  // Prepare for waiting on select...
65          fd = (int) pClient->evt.sock;                  fd = (int) pClient->evt.sock;
66          FD_ZERO(&fds);                  FD_ZERO(&fds);
67          FD_SET((unsigned int) fd, &fds);                  FD_SET((unsigned int) fd, &fds);
68          fdmax = fd;                  fdmax = fd;
69    
70          // Use the timeout (x10) select feature ...                  // Use the timeout (x10) select feature ...
71          iTimeout = 10 * pClient->iTimeout;                  iTimeout = 10 * pClient->iTimeout;
72          if (iTimeout >= 1000) {                  if (iTimeout >= 1000) {
73              tv.tv_sec = iTimeout / 1000;                          tv.tv_sec = iTimeout / 1000;
74              iTimeout -= tv.tv_sec * 1000;                          iTimeout -= tv.tv_sec * 1000;
75          }                  }
76          else tv.tv_sec = 0;                  else tv.tv_sec = 0;
77          tv.tv_usec = iTimeout * 1000;                  tv.tv_usec = iTimeout * 1000;
78    
79          // Wait for event...                  // Wait for event...
80          iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv);                  iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv);
81          if (iSelect > 0 && FD_ISSET(fd, &fds)) {                  if (iSelect > 0 && FD_ISSET(fd, &fds)) {
82              // May recv now...                          // May recv now...
83              cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0);                          cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0);
84              if (cchBuffer > 0) {                          if (cchBuffer > 0) {
85                  // Make sure received buffer it's null terminated.                                  // Make sure received buffer it's null terminated.
86                  achBuffer[cchBuffer] = (char) 0;                                  achBuffer[cchBuffer] = (char) 0;
87                  // Parse for the notification event message...                                  // Parse for the notification event message...
88                  pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".                                  pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".
89                  if (strcasecmp(pszToken, "NOTIFY") == 0) {                                  if (strcasecmp(pszToken, "NOTIFY") == 0) {
90                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));
91                      event    = lscp_event_from_text(pszToken);                                          event    = lscp_event_from_text(pszToken);
92                      // And pick the rest of data...                                          // And pick the rest of data...
93                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));
94                      cchToken = (pszToken == NULL ? 0 : strlen(pszToken));                                          cchToken = (pszToken == NULL ? 0 : strlen(pszToken));
95                      // Double-check if we're really up to it...                                          // Double-check if we're really up to it...
96                      if (pClient->events & event) {                                          if (pClient->events & event) {
97                          // Invoke the client event callback...                                                  // Invoke the client event callback...
98                          if ((*pClient->pfnCallback)(                                                  if ((*pClient->pfnCallback)(
99                                  pClient,                                                                  pClient,
100                                  event,                                                                  event,
101                                  pszToken,                                                                  pszToken,
102                                  cchToken,                                                                  cchToken,
103                                  pClient->pvData) != LSCP_OK) {                                                                  pClient->pvData) != LSCP_OK) {
104                              pClient->evt.iState = 0;                                                          pClient->evt.iState = 0;
105                          }                                                  }
106                      }                                          }
107                  }                                  }
108              } else {                          } else {
109                  lscp_socket_perror("_lscp_client_evt_proc: recv");                                  lscp_socket_perror("_lscp_client_evt_proc: recv");
110                  pClient->evt.iState = 0;                                  pClient->evt.iState = 0;
111              }                          }
112          }   // Check if select has in error.                  }   // Check if select has in error.
113          else if (iSelect < 0) {                  else if (iSelect < 0) {
114              lscp_socket_perror("_lscp_client_evt_proc: select");                          lscp_socket_perror("_lscp_client_evt_proc: select");
115              pClient->evt.iState = 0;                          pClient->evt.iState = 0;
116          }                  }
117    
118          // Finally, always signal the event.                  // Finally, always signal the event.
119          lscp_cond_signal(pClient->cond);                  lscp_cond_signal(pClient->cond);
120      }          }
121    
122  #ifdef DEBUG  #ifdef DEBUG
123      fprintf(stderr, "_lscp_client_evt_proc: Client closing.\n");          fprintf(stderr, "_lscp_client_evt_proc: Client closing.\n");
124  #endif  #endif
125  }  }
126    
# Line 131  static void _lscp_client_evt_proc ( void Line 131  static void _lscp_client_evt_proc ( void
131  // Open the event service socket connection.  // Open the event service socket connection.
132  static lscp_status_t _lscp_client_evt_connect ( lscp_client_t *pClient )  static lscp_status_t _lscp_client_evt_connect ( lscp_client_t *pClient )
133  {  {
134      lscp_socket_t sock;          lscp_socket_t sock;
135      struct sockaddr_in addr;          struct sockaddr_in addr;
136      int cAddr;          int cAddr;
137  #if defined(WIN32)  #if defined(WIN32)
138      int iSockOpt = (-1);          int iSockOpt = (-1);
139  #endif  #endif
140    
141      // Prepare the event connection socket...          // Prepare the event connection socket...
142      sock = socket(AF_INET, SOCK_STREAM, 0);          sock = socket(AF_INET, SOCK_STREAM, 0);
143      if (sock == INVALID_SOCKET) {          if (sock == INVALID_SOCKET) {
144          lscp_socket_perror("_lscp_client_evt_connect: socket");                  lscp_socket_perror("_lscp_client_evt_connect: socket");
145          return LSCP_FAILED;                  return LSCP_FAILED;
146      }          }
147    
148  #if defined(WIN32)  #if defined(WIN32)
149      if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR)          if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR)
150          lscp_socket_perror("lscp_client_evt_connect: setsockopt(SO_DONTLINGER)");                  lscp_socket_perror("lscp_client_evt_connect: setsockopt(SO_DONTLINGER)");
151  #endif  #endif
152    
153  #ifdef DEBUG  #ifdef DEBUG
154      lscp_socket_getopts("_lscp_client_evt_connect:", sock);          lscp_socket_getopts("_lscp_client_evt_connect:", sock);
155  #endif  #endif
156    
157      // Use same address of the command connection.          // Use same address of the command connection.
158      cAddr = sizeof(struct sockaddr_in);          cAddr = sizeof(struct sockaddr_in);
159      memmove((char *) &addr, &(pClient->cmd.addr), cAddr);          memmove((char *) &addr, &(pClient->cmd.addr), cAddr);
160    
161      // Start the connection...          // Start the connection...
162      if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {          if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {
163          lscp_socket_perror("_lscp_client_evt_connect: connect");                  lscp_socket_perror("_lscp_client_evt_connect: connect");
164          closesocket(sock);                  closesocket(sock);
165          return LSCP_FAILED;                  return LSCP_FAILED;
166      }          }
167    
168      // Set our socket agent struct...          // Set our socket agent struct...
169      lscp_socket_agent_init(&(pClient->evt), sock, &addr, cAddr);          lscp_socket_agent_init(&(pClient->evt), sock, &addr, cAddr);
170    
171      // And finally the service thread...          // And finally the service thread...
172      return lscp_socket_agent_start(&(pClient->evt), _lscp_client_evt_proc, pClient, 0);          return lscp_socket_agent_start(&(pClient->evt), _lscp_client_evt_proc, pClient, 0);
173  }  }
174    
175    
176  // Subscribe to a single event.  // Subscribe to a single event.
177  static lscp_status_t _lscp_client_evt_request ( lscp_client_t *pClient, int iSubscribe, lscp_event_t event )  static lscp_status_t _lscp_client_evt_request ( lscp_client_t *pClient, int iSubscribe, lscp_event_t event )
178  {  {
179      const char *pszEvent;          const char *pszEvent;
180      char  szQuery[LSCP_BUFSIZ];          char  szQuery[LSCP_BUFSIZ];
181      int   cchQuery;          int   cchQuery;
182    
183      if (pClient == NULL)          if (pClient == NULL)
184          return LSCP_FAILED;                  return LSCP_FAILED;
185    
186      // Which (single) event?          // Which (single) event?
187      pszEvent = lscp_event_to_text(event);          pszEvent = lscp_event_to_text(event);
188      if (pszEvent == NULL)          if (pszEvent == NULL)
189          return LSCP_FAILED;                  return LSCP_FAILED;
190    
191      // Build the query string...          // Build the query string...
192      cchQuery = sprintf(szQuery, "%sSUBSCRIBE %s\n\n", (iSubscribe == 0 ? "UN" : ""), pszEvent);          cchQuery = sprintf(szQuery, "%sSUBSCRIBE %s\n\n", (iSubscribe == 0 ? "UN" : ""), pszEvent);
193      // Just send data, forget result...          // Just send data, forget result...
194      if (send(pClient->evt.sock, szQuery, cchQuery, 0) < cchQuery) {          if (send(pClient->evt.sock, szQuery, cchQuery, 0) < cchQuery) {
195          lscp_socket_perror("_lscp_client_evt_request: send");                  lscp_socket_perror("_lscp_client_evt_request: send");
196          return LSCP_FAILED;                  return LSCP_FAILED;
197      }          }
198    
199      // Wait on response.          // Wait on response.
200      lscp_cond_wait(pClient->cond, pClient->mutex);          lscp_cond_wait(pClient->cond, pClient->mutex);
201    
202      // Update as naively as we can...          // Update as naively as we can...
203      if (iSubscribe)          if (iSubscribe)
204          pClient->events |=  event;                  pClient->events |=  event;
205      else          else
206          pClient->events &= ~event;                  pClient->events &= ~event;
207    
208      return LSCP_OK;          return LSCP_OK;
209  }  }
210    
211    
# Line 242  const char* lscp_client_build   (void) { Line 242  const char* lscp_client_build   (void) {
242   */   */
243  lscp_client_t* lscp_client_create ( const char *pszHost, int iPort, lscp_client_proc_t pfnCallback, void *pvData )  lscp_client_t* lscp_client_create ( const char *pszHost, int iPort, lscp_client_proc_t pfnCallback, void *pvData )
244  {  {
245      lscp_client_t  *pClient;          lscp_client_t  *pClient;
246      struct hostent *pHost;          struct hostent *pHost;
247      lscp_socket_t sock;          lscp_socket_t sock;
248      struct sockaddr_in addr;          struct sockaddr_in addr;
249      int cAddr;          int cAddr;
250  #if defined(WIN32)  #if defined(WIN32)
251      int iSockOpt = (-1);          int iSockOpt = (-1);
252  #endif  #endif
253    
254      if (pfnCallback == NULL) {          if (pfnCallback == NULL) {
255          fprintf(stderr, "lscp_client_create: Invalid client callback function.\n");                  fprintf(stderr, "lscp_client_create: Invalid client callback function.\n");
256          return NULL;                  return NULL;
257      }          }
258    
259      pHost = gethostbyname(pszHost);          pHost = gethostbyname(pszHost);
260      if (pHost == NULL) {          if (pHost == NULL) {
261          lscp_socket_herror("lscp_client_create: gethostbyname");                  lscp_socket_herror("lscp_client_create: gethostbyname");
262          return NULL;                  return NULL;
263      }          }
264    
265      // Allocate client descriptor...          // Allocate client descriptor...
266    
267      pClient = (lscp_client_t *) malloc(sizeof(lscp_client_t));          pClient = (lscp_client_t *) malloc(sizeof(lscp_client_t));
268      if (pClient == NULL) {          if (pClient == NULL) {
269          fprintf(stderr, "lscp_client_create: Out of memory.\n");                  fprintf(stderr, "lscp_client_create: Out of memory.\n");
270          return NULL;                  return NULL;
271      }          }
272      memset(pClient, 0, sizeof(lscp_client_t));          memset(pClient, 0, sizeof(lscp_client_t));
273    
274      pClient->pfnCallback = pfnCallback;          pClient->pfnCallback = pfnCallback;
275      pClient->pvData = pvData;          pClient->pvData = pvData;
276    
277  #ifdef DEBUG  #ifdef DEBUG
278      fprintf(stderr, "lscp_client_create: pClient=%p: pszHost=%s iPort=%d.\n", pClient, pszHost, iPort);          fprintf(stderr, "lscp_client_create: pClient=%p: pszHost=%s iPort=%d.\n", pClient, pszHost, iPort);
279  #endif  #endif
280    
281      // Prepare the command connection socket...          // Prepare the command connection socket...
282    
283      sock = socket(AF_INET, SOCK_STREAM, 0);          sock = socket(AF_INET, SOCK_STREAM, 0);
284      if (sock == INVALID_SOCKET) {          if (sock == INVALID_SOCKET) {
285          lscp_socket_perror("lscp_client_create: cmd: socket");                  lscp_socket_perror("lscp_client_create: cmd: socket");
286          free(pClient);                  free(pClient);
287          return NULL;                  return NULL;
288      }          }
289    
290  #if defined(WIN32)  #if defined(WIN32)
291      if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR)          if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR)
292          lscp_socket_perror("lscp_client_create: cmd: setsockopt(SO_DONTLINGER)");                  lscp_socket_perror("lscp_client_create: cmd: setsockopt(SO_DONTLINGER)");
293  #endif  #endif
294    
295  #ifdef DEBUG  #ifdef DEBUG
296      lscp_socket_getopts("lscp_client_create: cmd", sock);          lscp_socket_getopts("lscp_client_create: cmd", sock);
297  #endif  #endif
298    
299      cAddr = sizeof(struct sockaddr_in);          cAddr = sizeof(struct sockaddr_in);
300      memset((char *) &addr, 0, cAddr);          memset((char *) &addr, 0, cAddr);
301      addr.sin_family = pHost->h_addrtype;          addr.sin_family = pHost->h_addrtype;
302      memmove((char *) &(addr.sin_addr), pHost->h_addr, pHost->h_length);          memmove((char *) &(addr.sin_addr), pHost->h_addr, pHost->h_length);
303      addr.sin_port = htons((short) iPort);          addr.sin_port = htons((short) iPort);
304    
305      if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {          if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {
306          lscp_socket_perror("lscp_client_create: cmd: connect");                  lscp_socket_perror("lscp_client_create: cmd: connect");
307          closesocket(sock);                  closesocket(sock);
308          free(pClient);                  free(pClient);
309          return NULL;                  return NULL;
310      }          }
311    
312      // Initialize the command socket agent struct...          // Initialize the command socket agent struct...
313      lscp_socket_agent_init(&(pClient->cmd), sock, &addr, cAddr);          lscp_socket_agent_init(&(pClient->cmd), sock, &addr, cAddr);
314    
315  #ifdef DEBUG  #ifdef DEBUG
316      fprintf(stderr, "lscp_client_create: cmd: pClient=%p: sock=%d addr=%s port=%d.\n", pClient, pClient->cmd.sock, inet_ntoa(pClient->cmd.addr.sin_addr), ntohs(pClient->cmd.addr.sin_port));          fprintf(stderr, "lscp_client_create: cmd: pClient=%p: sock=%d addr=%s port=%d.\n", pClient, pClient->cmd.sock, inet_ntoa(pClient->cmd.addr.sin_addr), ntohs(pClient->cmd.addr.sin_port));
317  #endif  #endif
318    
319      // Initialize the event service socket struct...          // Initialize the event service socket struct...
320      lscp_socket_agent_init(&(pClient->evt), INVALID_SOCKET, NULL, 0);          lscp_socket_agent_init(&(pClient->evt), INVALID_SOCKET, NULL, 0);
321      // No events subscribed, yet.          // No events subscribed, yet.
322      pClient->events = LSCP_EVENT_NONE;          pClient->events = LSCP_EVENT_NONE;
323      // Initialize cached members.          // Initialize cached members.
324      pClient->audio_drivers = NULL;          pClient->audio_drivers = NULL;
325      pClient->midi_drivers = NULL;          pClient->midi_drivers = NULL;
326      pClient->audio_devices = NULL;          pClient->audio_devices = NULL;
327      pClient->midi_devices = NULL;          pClient->midi_devices = NULL;
328      pClient->engines = NULL;          pClient->engines = NULL;
329      pClient->channels = NULL;          pClient->channels = NULL;
330      pClient->midi_instruments = NULL;          pClient->midi_instruments = NULL;
331      lscp_driver_info_init(&(pClient->audio_driver_info));          pClient->midi_maps = NULL;
332      lscp_driver_info_init(&(pClient->midi_driver_info));          pClient->midi_map_name = NULL;
333      lscp_device_info_init(&(pClient->audio_device_info));          lscp_driver_info_init(&(pClient->audio_driver_info));
334      lscp_device_info_init(&(pClient->midi_device_info));          lscp_driver_info_init(&(pClient->midi_driver_info));
335      lscp_param_info_init(&(pClient->audio_param_info));          lscp_device_info_init(&(pClient->audio_device_info));
336      lscp_param_info_init(&(pClient->midi_param_info));          lscp_device_info_init(&(pClient->midi_device_info));
337      lscp_device_port_info_init(&(pClient->audio_channel_info));          lscp_param_info_init(&(pClient->audio_param_info));
338      lscp_device_port_info_init(&(pClient->midi_port_info));          lscp_param_info_init(&(pClient->midi_param_info));
339      lscp_param_info_init(&(pClient->audio_channel_param_info));          lscp_device_port_info_init(&(pClient->audio_channel_info));
340      lscp_param_info_init(&(pClient->midi_port_param_info));          lscp_device_port_info_init(&(pClient->midi_port_info));
341      lscp_server_info_init(&(pClient->server_info));          lscp_param_info_init(&(pClient->audio_channel_param_info));
342      lscp_engine_info_init(&(pClient->engine_info));          lscp_param_info_init(&(pClient->midi_port_param_info));
343      lscp_channel_info_init(&(pClient->channel_info));          lscp_server_info_init(&(pClient->server_info));
344      lscp_midi_instrument_info_init(&(pClient->midi_instrument_info));          lscp_engine_info_init(&(pClient->engine_info));
345      // Initialize error stuff.          lscp_channel_info_init(&(pClient->channel_info));
346      pClient->pszResult = NULL;          lscp_midi_instrument_info_init(&(pClient->midi_instrument_info));
347      pClient->iErrno = -1;          // Initialize error stuff.
348      // Stream usage stuff.          pClient->pszResult = NULL;
349      pClient->buffer_fill = NULL;          pClient->iErrno = -1;
350      pClient->iStreamCount = 0;          // Stream usage stuff.
351      // Default timeout value.          pClient->buffer_fill = NULL;
352      pClient->iTimeout = LSCP_TIMEOUT_MSECS;          pClient->iStreamCount = 0;
353            // Default timeout value.
354            pClient->iTimeout = LSCP_TIMEOUT_MSECS;
355          pClient->iTimeoutCount = 0;          pClient->iTimeoutCount = 0;
356    
357      // Initialize the transaction mutex.          // Initialize the transaction mutex.
358      lscp_mutex_init(pClient->mutex);          lscp_mutex_init(pClient->mutex);
359      lscp_cond_init(pClient->cond);          lscp_cond_init(pClient->cond);
360    
361      // Finally we've some success...          // Finally we've some success...
362      return pClient;          return pClient;
363  }  }
364    
365    
# Line 368  lscp_client_t* lscp_client_create ( cons Line 370  lscp_client_t* lscp_client_create ( cons
370   */   */
371  lscp_status_t lscp_client_join ( lscp_client_t *pClient )  lscp_status_t lscp_client_join ( lscp_client_t *pClient )
372  {  {
373      if (pClient == NULL)          if (pClient == NULL)
374          return LSCP_FAILED;                  return LSCP_FAILED;
375    
376  #ifdef DEBUG  #ifdef DEBUG
377      fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient);          fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient);
378  #endif  #endif
379    
380  //  lscp_socket_agent_join(&(pClient->evt));  //  lscp_socket_agent_join(&(pClient->evt));
381      lscp_socket_agent_join(&(pClient->cmd));          lscp_socket_agent_join(&(pClient->cmd));
382    
383      return LSCP_OK;          return LSCP_OK;
384  }  }
385    
386    
# Line 391  lscp_status_t lscp_client_join ( lscp_cl Line 393  lscp_status_t lscp_client_join ( lscp_cl
393   */   */
394  lscp_status_t lscp_client_destroy ( lscp_client_t *pClient )  lscp_status_t lscp_client_destroy ( lscp_client_t *pClient )
395  {  {
396      if (pClient == NULL)          if (pClient == NULL)
397          return LSCP_FAILED;                  return LSCP_FAILED;
398    
399  #ifdef DEBUG  #ifdef DEBUG
400      fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient);          fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient);
401  #endif  #endif
402    
403      // Lock this section up.          // Lock this section up.
404      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
405    
406      // Free up all cached members.          // Free up all cached members.
407      lscp_midi_instrument_info_free(&(pClient->midi_instrument_info));          lscp_midi_instrument_info_free(&(pClient->midi_instrument_info));
408      lscp_channel_info_free(&(pClient->channel_info));          lscp_channel_info_free(&(pClient->channel_info));
409      lscp_engine_info_free(&(pClient->engine_info));          lscp_engine_info_free(&(pClient->engine_info));
410      lscp_server_info_free(&(pClient->server_info));          lscp_server_info_free(&(pClient->server_info));
411      lscp_param_info_free(&(pClient->midi_port_param_info));          lscp_param_info_free(&(pClient->midi_port_param_info));
412      lscp_param_info_free(&(pClient->audio_channel_param_info));          lscp_param_info_free(&(pClient->audio_channel_param_info));
413      lscp_device_port_info_free(&(pClient->midi_port_info));          lscp_device_port_info_free(&(pClient->midi_port_info));
414      lscp_device_port_info_free(&(pClient->audio_channel_info));          lscp_device_port_info_free(&(pClient->audio_channel_info));
415      lscp_param_info_free(&(pClient->midi_param_info));          lscp_param_info_free(&(pClient->midi_param_info));
416      lscp_param_info_free(&(pClient->audio_param_info));          lscp_param_info_free(&(pClient->audio_param_info));
417      lscp_device_info_free(&(pClient->midi_device_info));          lscp_device_info_free(&(pClient->midi_device_info));
418      lscp_device_info_free(&(pClient->audio_device_info));          lscp_device_info_free(&(pClient->audio_device_info));
419      lscp_driver_info_free(&(pClient->midi_driver_info));          lscp_driver_info_free(&(pClient->midi_driver_info));
420      lscp_driver_info_free(&(pClient->audio_driver_info));          lscp_driver_info_free(&(pClient->audio_driver_info));
421      // Free available engine table.          // Free available engine table.
422      lscp_szsplit_destroy(pClient->audio_drivers);          lscp_szsplit_destroy(pClient->audio_drivers);
423      lscp_szsplit_destroy(pClient->midi_drivers);          lscp_szsplit_destroy(pClient->midi_drivers);
424      lscp_isplit_destroy(pClient->audio_devices);          lscp_isplit_destroy(pClient->audio_devices);
425      lscp_isplit_destroy(pClient->midi_devices);          lscp_isplit_destroy(pClient->midi_devices);
426      lscp_szsplit_destroy(pClient->engines);          lscp_szsplit_destroy(pClient->engines);
427      lscp_isplit_destroy(pClient->channels);          lscp_isplit_destroy(pClient->channels);
428      lscp_midi_instruments_destroy(pClient->midi_instruments);          lscp_midi_instruments_destroy(pClient->midi_instruments);
429      // Make them null.          lscp_isplit_destroy(pClient->midi_maps);
430      pClient->audio_drivers = NULL;          if (pClient->midi_map_name)
431      pClient->midi_drivers = NULL;                  free(pClient->midi_map_name);
432      pClient->audio_devices = NULL;          // Make them null.
433      pClient->midi_devices = NULL;          pClient->audio_drivers = NULL;
434      pClient->engines = NULL;          pClient->midi_drivers = NULL;
435      pClient->channels = NULL;          pClient->audio_devices = NULL;
436      pClient->midi_instruments = NULL;          pClient->midi_devices = NULL;
437      // Free result error stuff.          pClient->engines = NULL;
438      lscp_client_set_result(pClient, NULL, 0);          pClient->channels = NULL;
439      // Free stream usage stuff.          pClient->midi_instruments = NULL;
440      if (pClient->buffer_fill)          pClient->midi_maps = NULL;
441          free(pClient->buffer_fill);          pClient->midi_map_name = NULL;
442      pClient->buffer_fill = NULL;          // Free result error stuff.
443      pClient->iStreamCount = 0;          lscp_client_set_result(pClient, NULL, 0);
444      pClient->iTimeout = 0;          // Free stream usage stuff.
445            if (pClient->buffer_fill)
446      // Free socket agents.                  free(pClient->buffer_fill);
447      lscp_socket_agent_free(&(pClient->evt));          pClient->buffer_fill = NULL;
448      lscp_socket_agent_free(&(pClient->cmd));          pClient->iStreamCount = 0;
449            pClient->iTimeout = 0;
450      // Last but not least, free good ol'transaction mutex.  
451      lscp_mutex_unlock(pClient->mutex);          // Free socket agents.
452      lscp_mutex_destroy(pClient->mutex);          lscp_socket_agent_free(&(pClient->evt));
453      lscp_cond_destroy(pClient->cond);          lscp_socket_agent_free(&(pClient->cmd));
454    
455            // Last but not least, free good ol'transaction mutex.
456            lscp_mutex_unlock(pClient->mutex);
457            lscp_mutex_destroy(pClient->mutex);
458            lscp_cond_destroy(pClient->cond);
459    
460      free(pClient);          free(pClient);
461    
462      return LSCP_OK;          return LSCP_OK;
463  }  }
464    
465    
# Line 466  lscp_status_t lscp_client_destroy ( lscp Line 473  lscp_status_t lscp_client_destroy ( lscp
473   */   */
474  lscp_status_t lscp_client_set_timeout ( lscp_client_t *pClient, int iTimeout )  lscp_status_t lscp_client_set_timeout ( lscp_client_t *pClient, int iTimeout )
475  {  {
476      if (pClient == NULL)          if (pClient == NULL)
477          return LSCP_FAILED;                  return LSCP_FAILED;
478      if (iTimeout < 0)          if (iTimeout < 0)
479          return LSCP_FAILED;                  return LSCP_FAILED;
480    
481      pClient->iTimeout = iTimeout;          pClient->iTimeout = iTimeout;
482      return LSCP_OK;          return LSCP_OK;
483  }  }
484    
485    
# Line 485  lscp_status_t lscp_client_set_timeout ( Line 492  lscp_status_t lscp_client_set_timeout (
492   */   */
493  int lscp_client_get_timeout ( lscp_client_t *pClient )  int lscp_client_get_timeout ( lscp_client_t *pClient )
494  {  {
495      if (pClient == NULL)          if (pClient == NULL)
496          return -1;                  return -1;
497    
498      return pClient->iTimeout;          return pClient->iTimeout;
499  }  }
500    
501    
# Line 510  int lscp_client_get_timeout ( lscp_clien Line 517  int lscp_client_get_timeout ( lscp_clien
517   */   */
518  lscp_status_t lscp_client_query ( lscp_client_t *pClient, const char *pszQuery )  lscp_status_t lscp_client_query ( lscp_client_t *pClient, const char *pszQuery )
519  {  {
520      lscp_status_t ret;          lscp_status_t ret;
521    
522      // Lock this section up.          if (pClient == NULL)
523      lscp_mutex_lock(pClient->mutex);                  return LSCP_FAILED;
524    
525      // Just make the now guarded call.          // Lock this section up.
526      ret = lscp_client_call(pClient, pszQuery, 0);          lscp_mutex_lock(pClient->mutex);
527    
528      // Unlock this section down.          // Just make the now guarded call.
529      lscp_mutex_unlock(pClient->mutex);          ret = lscp_client_call(pClient, pszQuery, 0);
530    
531      return ret;          // Unlock this section down.
532            lscp_mutex_unlock(pClient->mutex);
533    
534            return ret;
535  }  }
536    
537  /**  /**
# Line 535  lscp_status_t lscp_client_query ( lscp_c Line 545  lscp_status_t lscp_client_query ( lscp_c
545   */   */
546  const char *lscp_client_get_result ( lscp_client_t *pClient )  const char *lscp_client_get_result ( lscp_client_t *pClient )
547  {  {
548      if (pClient == NULL)          if (pClient == NULL)
549          return NULL;                  return NULL;
550    
551      return pClient->pszResult;          return pClient->pszResult;
552  }  }
553    
554    
# Line 552  const char *lscp_client_get_result ( lsc Line 562  const char *lscp_client_get_result ( lsc
562   */   */
563  int lscp_client_get_errno ( lscp_client_t *pClient )  int lscp_client_get_errno ( lscp_client_t *pClient )
564  {  {
565      if (pClient == NULL)          if (pClient == NULL)
566          return -1;                  return -1;
567    
568      return pClient->iErrno;          return pClient->iErrno;
569  }  }
570    
571    
# Line 574  int lscp_client_get_errno ( lscp_client_ Line 584  int lscp_client_get_errno ( lscp_client_
584   */   */
585  lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient, lscp_event_t events )  lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient, lscp_event_t events )
586  {  {
587      lscp_status_t ret = LSCP_FAILED;          lscp_status_t ret = LSCP_FAILED;
588    
589      if (pClient == NULL)          if (pClient == NULL)
590          return LSCP_FAILED;                  return LSCP_FAILED;
591    
592      // Lock this section up.          // Lock this section up.
593      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
594    
595      // If applicable, start the alternate connection...          // If applicable, start the alternate connection...
596      if (pClient->events == LSCP_EVENT_NONE)          if (pClient->events == LSCP_EVENT_NONE)
597          ret = _lscp_client_evt_connect(pClient);                  ret = _lscp_client_evt_connect(pClient);
598    
599      // Send the subscription commands.          // Send the subscription commands.
600      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT))          if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT))
601          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_COUNT);                  ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_COUNT);
602      if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))          if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
603          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT);                  ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT);
604      if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))          if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
605          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_STREAM_COUNT);                  ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_STREAM_COUNT);
606      if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))          if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))
607          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_BUFFER_FILL);                  ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_BUFFER_FILL);
608      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))          if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))
609          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_INFO);                  ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_INFO);
610      if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))          if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))
611          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_MISCELLANEOUS);                  ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_MISCELLANEOUS);
612    
613      // Unlock this section down.          // Unlock this section down.
614      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
615    
616      return ret;          return ret;
617  }  }
618    
619    
# Line 619  lscp_status_t lscp_client_subscribe ( ls Line 629  lscp_status_t lscp_client_subscribe ( ls
629   */   */
630  lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient, lscp_event_t events )  lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient, lscp_event_t events )
631  {  {
632      lscp_status_t ret = LSCP_OK;          lscp_status_t ret = LSCP_OK;
633    
634      if (pClient == NULL)          if (pClient == NULL)
635          return LSCP_FAILED;                  return LSCP_FAILED;
636    
637      // Lock this section up.          // Lock this section up.
638      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
639    
640      // Send the unsubscription commands.          // Send the unsubscription commands.
641      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT))          if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT))
642          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_COUNT);                  ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_COUNT);
643      if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))          if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
644          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT);                  ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT);
645      if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))          if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
646          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_STREAM_COUNT);                  ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_STREAM_COUNT);
647      if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))          if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))
648          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_BUFFER_FILL);                  ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_BUFFER_FILL);
649      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))          if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))
650          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_INFO);                  ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_INFO);
651      if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))          if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))
652          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_MISCELLANEOUS);                  ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_MISCELLANEOUS);
653    
654      // If necessary, close the alternate connection...          // If necessary, close the alternate connection...
655      if (pClient->events == LSCP_EVENT_NONE)          if (pClient->events == LSCP_EVENT_NONE)
656          lscp_socket_agent_free(&(pClient->evt));                  lscp_socket_agent_free(&(pClient->evt));
657    
658      // Unlock this section down.          // Unlock this section down.
659      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
660    
661      return ret;          return ret;
662  }  }
663    
664    
# Line 661  lscp_status_t lscp_client_unsubscribe ( Line 671  lscp_status_t lscp_client_unsubscribe (
671   */   */
672  lscp_event_t lscp_client_get_events ( lscp_client_t *pClient )  lscp_event_t lscp_client_get_events ( lscp_client_t *pClient )
673  {  {
674      if (pClient == NULL)          if (pClient == NULL)
675          return LSCP_EVENT_NONE;                  return LSCP_EVENT_NONE;
676    
677      return pClient->events;          return pClient->events;
678  }  }
679    
680    
# Line 684  lscp_event_t lscp_client_get_events ( ls Line 694  lscp_event_t lscp_client_get_events ( ls
694   */   */
695  lscp_status_t lscp_load_instrument ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )  lscp_status_t lscp_load_instrument ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )
696  {  {
697      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
698    
699      if (pszFileName == NULL || iSamplerChannel < 0)          if (pszFileName == NULL || iSamplerChannel < 0)
700          return LSCP_FAILED;                  return LSCP_FAILED;
701    
702      sprintf(szQuery, "LOAD INSTRUMENT '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);          sprintf(szQuery, "LOAD INSTRUMENT '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);
703      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
704  }  }
705    
706    
# Line 707  lscp_status_t lscp_load_instrument ( lsc Line 717  lscp_status_t lscp_load_instrument ( lsc
717   */   */
718  lscp_status_t lscp_load_instrument_non_modal ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )  lscp_status_t lscp_load_instrument_non_modal ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )
719  {  {
720      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
721    
722      if (pszFileName == NULL || iSamplerChannel < 0)          if (pszFileName == NULL || iSamplerChannel < 0)
723          return LSCP_FAILED;                  return LSCP_FAILED;
724    
725      sprintf(szQuery, "LOAD INSTRUMENT NON_MODAL '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);          sprintf(szQuery, "LOAD INSTRUMENT NON_MODAL '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);
726      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
727  }  }
728    
729    
# Line 729  lscp_status_t lscp_load_instrument_non_m Line 739  lscp_status_t lscp_load_instrument_non_m
739   */   */
740  lscp_status_t lscp_load_engine ( lscp_client_t *pClient, const char *pszEngineName, int iSamplerChannel )  lscp_status_t lscp_load_engine ( lscp_client_t *pClient, const char *pszEngineName, int iSamplerChannel )
741  {  {
742      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
743    
744      if (pszEngineName == NULL || iSamplerChannel < 0)          if (pszEngineName == NULL || iSamplerChannel < 0)
745          return LSCP_FAILED;                  return LSCP_FAILED;
746    
747      sprintf(szQuery, "LOAD ENGINE %s %d\r\n", pszEngineName, iSamplerChannel);          sprintf(szQuery, "LOAD ENGINE %s %d\r\n", pszEngineName, iSamplerChannel);
748      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
749  }  }
750    
751    
# Line 750  lscp_status_t lscp_load_engine ( lscp_cl Line 760  lscp_status_t lscp_load_engine ( lscp_cl
760   */   */
761  int lscp_get_channels ( lscp_client_t *pClient )  int lscp_get_channels ( lscp_client_t *pClient )
762  {  {
763      int iChannels = -1;          int iChannels = -1;
764    
765            if (pClient == NULL)
766                    return -1;
767    
768      // Lock this section up.          // Lock this section up.
769      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
770    
771      if (lscp_client_call(pClient, "GET CHANNELS\r\n", 0) == LSCP_OK)          if (lscp_client_call(pClient, "GET CHANNELS\r\n", 0) == LSCP_OK)
772          iChannels = atoi(lscp_client_get_result(pClient));                  iChannels = atoi(lscp_client_get_result(pClient));
773    
774      // Unlock this section doen.          // Unlock this section doen.
775      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
776    
777      return iChannels;          return iChannels;
778  }  }
779    
780    
# Line 776  int lscp_get_channels ( lscp_client_t *p Line 789  int lscp_get_channels ( lscp_client_t *p
789   */   */
790  int *lscp_list_channels ( lscp_client_t *pClient )  int *lscp_list_channels ( lscp_client_t *pClient )
791  {  {
792      const char *pszSeps = ",";          const char *pszSeps = ",";
793    
794      if (pClient == NULL)          if (pClient == NULL)
795          return NULL;                  return NULL;
796    
797      // Lock this section up.          // Lock this section up.
798      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
799    
800      if (pClient->channels) {          if (pClient->channels) {
801          lscp_isplit_destroy(pClient->channels);                  lscp_isplit_destroy(pClient->channels);
802          pClient->channels = NULL;                  pClient->channels = NULL;
803      }          }
804    
805      if (lscp_client_call(pClient, "LIST CHANNELS\r\n", 0) == LSCP_OK)          if (lscp_client_call(pClient, "LIST CHANNELS\r\n", 0) == LSCP_OK)
806          pClient->channels = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps);                  pClient->channels = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps);
807    
808      // Unlock this section down.          // Unlock this section down.
809      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
810    
811      return pClient->channels;          return pClient->channels;
812  }  }
813    
814    
# Line 810  int *lscp_list_channels ( lscp_client_t Line 823  int *lscp_list_channels ( lscp_client_t
823   */   */
824  int lscp_add_channel ( lscp_client_t *pClient )  int lscp_add_channel ( lscp_client_t *pClient )
825  {  {
826      int iSamplerChannel = -1;          int iSamplerChannel = -1;
827    
828      // Lock this section up.          if (pClient == NULL)
829      lscp_mutex_lock(pClient->mutex);                  return -1;
830    
831      if (lscp_client_call(pClient, "ADD CHANNEL\r\n", 0) == LSCP_OK)          // Lock this section up.
832          iSamplerChannel = atoi(lscp_client_get_result(pClient));          lscp_mutex_lock(pClient->mutex);
833    
834      // Unlock this section down.          if (lscp_client_call(pClient, "ADD CHANNEL\r\n", 0) == LSCP_OK)
835      lscp_mutex_unlock(pClient->mutex);                  iSamplerChannel = atoi(lscp_client_get_result(pClient));
836    
837      return iSamplerChannel;          // Unlock this section down.
838            lscp_mutex_unlock(pClient->mutex);
839    
840            return iSamplerChannel;
841  }  }
842    
843    
# Line 836  int lscp_add_channel ( lscp_client_t *pC Line 852  int lscp_add_channel ( lscp_client_t *pC
852   */   */
853  lscp_status_t lscp_remove_channel ( lscp_client_t *pClient, int iSamplerChannel )  lscp_status_t lscp_remove_channel ( lscp_client_t *pClient, int iSamplerChannel )
854  {  {
855      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
856    
857      if (iSamplerChannel < 0)          if (iSamplerChannel < 0)
858          return LSCP_FAILED;                  return LSCP_FAILED;
859    
860      sprintf(szQuery, "REMOVE CHANNEL %d\r\n", iSamplerChannel);          sprintf(szQuery, "REMOVE CHANNEL %d\r\n", iSamplerChannel);
861      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
862  }  }
863    
864    
# Line 857  lscp_status_t lscp_remove_channel ( lscp Line 873  lscp_status_t lscp_remove_channel ( lscp
873   */   */
874  int lscp_get_available_engines ( lscp_client_t *pClient )  int lscp_get_available_engines ( lscp_client_t *pClient )
875  {  {
876      int iAvailableEngines = -1;          int iAvailableEngines = -1;
877    
878            if (pClient == NULL)
879                    return -1;
880    
881      // Lock this section up.          // Lock this section up.
882      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
883    
884      if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n", 0) == LSCP_OK)          if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n", 0) == LSCP_OK)
885          iAvailableEngines = atoi(lscp_client_get_result(pClient));                  iAvailableEngines = atoi(lscp_client_get_result(pClient));
886    
887      // Unlock this section down.          // Unlock this section down.
888      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
889    
890      return iAvailableEngines;          return iAvailableEngines;
891  }  }
892    
893    
# Line 883  int lscp_get_available_engines ( lscp_cl Line 902  int lscp_get_available_engines ( lscp_cl
902   */   */
903  const char **lscp_list_available_engines ( lscp_client_t *pClient )  const char **lscp_list_available_engines ( lscp_client_t *pClient )
904  {  {
905      const char *pszSeps = ",";          const char *pszSeps = ",";
906    
907      // Lock this section up.          if (pClient == NULL)
908      lscp_mutex_lock(pClient->mutex);                  return NULL;
909    
910      if (pClient->engines) {          // Lock this section up.
911          lscp_szsplit_destroy(pClient->engines);          lscp_mutex_lock(pClient->mutex);
         pClient->engines = NULL;  
     }  
912    
913      if (lscp_client_call(pClient, "LIST AVAILABLE_ENGINES\r\n", 0) == LSCP_OK)          if (pClient->engines) {
914          pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps);                  lscp_szsplit_destroy(pClient->engines);
915                    pClient->engines = NULL;
916            }
917    
918      // Unlock this section down.          if (lscp_client_call(pClient, "LIST AVAILABLE_ENGINES\r\n", 0) == LSCP_OK)
919      lscp_mutex_unlock(pClient->mutex);                  pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps);
920    
921      return (const char **) pClient->engines;          // Unlock this section down.
922            lscp_mutex_unlock(pClient->mutex);
923    
924            return (const char **) pClient->engines;
925  }  }
926    
927    
# Line 915  const char **lscp_list_available_engines Line 937  const char **lscp_list_available_engines
937   */   */
938  lscp_engine_info_t *lscp_get_engine_info ( lscp_client_t *pClient, const char *pszEngineName )  lscp_engine_info_t *lscp_get_engine_info ( lscp_client_t *pClient, const char *pszEngineName )
939  {  {
940      lscp_engine_info_t *pEngineInfo;          lscp_engine_info_t *pEngineInfo;
941      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
942      const char *pszResult;          const char *pszResult;
943      const char *pszSeps = ":";          const char *pszSeps = ":";
944      const char *pszCrlf = "\r\n";          const char *pszCrlf = "\r\n";
945      char *pszToken;          char *pszToken;
946      char *pch;          char *pch;
947    
948      if (pszEngineName == NULL)          if (pClient == NULL)
949          return NULL;                  return NULL;
950            if (pszEngineName == NULL)
951      // Lock this section up.                  return NULL;
952      lscp_mutex_lock(pClient->mutex);  
953            // Lock this section up.
954      pEngineInfo = &(pClient->engine_info);          lscp_mutex_lock(pClient->mutex);
955      lscp_engine_info_reset(pEngineInfo);  
956            pEngineInfo = &(pClient->engine_info);
957      sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName);          lscp_engine_info_reset(pEngineInfo);
958      if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {  
959          pszResult = lscp_client_get_result(pClient);          sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName);
960          pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));          if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {
961          while (pszToken) {                  pszResult = lscp_client_get_result(pClient);
962              if (strcasecmp(pszToken, "DESCRIPTION") == 0) {                  pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
963                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  while (pszToken) {
964                  if (pszToken)                          if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
965                      lscp_unquote_dup(&(pEngineInfo->description), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
966              }                                  if (pszToken)
967              else if (strcasecmp(pszToken, "VERSION") == 0) {                                          lscp_unquote_dup(&(pEngineInfo->description), &pszToken);
968                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
969                  if (pszToken)                          else if (strcasecmp(pszToken, "VERSION") == 0) {
970                      lscp_unquote_dup(&(pEngineInfo->version), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
971              }                                  if (pszToken)
972              pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                          lscp_unquote_dup(&(pEngineInfo->version), &pszToken);
973          }                          }
974      }                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));
975      else pEngineInfo = NULL;                  }
976            }
977            else pEngineInfo = NULL;
978    
979      // Unlock this section down.          // Unlock this section down.
980      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
981    
982      return pEngineInfo;          return pEngineInfo;
983  }  }
984    
985    
# Line 971  lscp_engine_info_t *lscp_get_engine_info Line 995  lscp_engine_info_t *lscp_get_engine_info
995   */   */
996  lscp_channel_info_t *lscp_get_channel_info ( lscp_client_t *pClient, int iSamplerChannel )  lscp_channel_info_t *lscp_get_channel_info ( lscp_client_t *pClient, int iSamplerChannel )
997  {  {
998      lscp_channel_info_t *pChannelInfo;          lscp_channel_info_t *pChannelInfo;
999      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1000      const char *pszResult;          const char *pszResult;
1001      const char *pszSeps = ":";          const char *pszSeps = ":";
1002      const char *pszCrlf = "\r\n";          const char *pszCrlf = "\r\n";
1003      char *pszToken;          char *pszToken;
1004      char *pch;          char *pch;
1005    
1006      if (iSamplerChannel < 0)          if (pClient == NULL)
1007          return NULL;                  return NULL;
1008            if (iSamplerChannel < 0)
1009      // Lock this section up.                  return NULL;
1010      lscp_mutex_lock(pClient->mutex);  
1011            // Lock this section up.
1012      pChannelInfo = &(pClient->channel_info);          lscp_mutex_lock(pClient->mutex);
1013      lscp_channel_info_reset(pChannelInfo);  
1014            pChannelInfo = &(pClient->channel_info);
1015      sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel);          lscp_channel_info_reset(pChannelInfo);
1016      if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {  
1017          pszResult = lscp_client_get_result(pClient);          sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel);
1018          pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));          if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {
1019          while (pszToken) {                  pszResult = lscp_client_get_result(pClient);
1020              if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {                  pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1021                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  while (pszToken) {
1022                  if (pszToken)                          if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {
1023                      lscp_unquote_dup(&(pChannelInfo->engine_name), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1024              }                                  if (pszToken)
1025              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {                                          lscp_unquote_dup(&(pChannelInfo->engine_name), &pszToken);
1026                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1027                  if (pszToken)                          else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {
1028                      pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1029              }                                  if (pszToken)
1030              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) {                                          pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken));
1031                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1032                  if (pszToken)                          else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) {
1033                      pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1034              }                                  if (pszToken)
1035              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {                                          pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken));
1036                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1037                  if (pszToken) {                          else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {
1038                      if (pChannelInfo->audio_routing)                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1039                          lscp_szsplit_destroy(pChannelInfo->audio_routing);                                  if (pszToken) {
1040                      pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");                                          if (pChannelInfo->audio_routing)
1041                  }                                                  lscp_szsplit_destroy(pChannelInfo->audio_routing);
1042              }                                          pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");
1043              else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {                                  }
1044                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1045                  if (pszToken)                          else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {
1046                      lscp_unquote_dup(&(pChannelInfo->instrument_file), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1047              }                                  if (pszToken)
1048              else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {                                          lscp_unquote_dup(&(pChannelInfo->instrument_file), &pszToken);
1049                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1050                  if (pszToken)                          else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {
1051                      pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1052              }                                  if (pszToken)
1053              else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) {                                          pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken));
1054                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1055                  if (pszToken)                          else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) {
1056                      lscp_unquote_dup(&(pChannelInfo->instrument_name), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1057              }                                  if (pszToken)
1058              else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) {                                          lscp_unquote_dup(&(pChannelInfo->instrument_name), &pszToken);
1059                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1060                  if (pszToken)                          else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) {
1061                      pChannelInfo->instrument_status = atoi(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1062              }                                  if (pszToken)
1063              else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) {                                          pChannelInfo->instrument_status = atoi(lscp_ltrim(pszToken));
1064                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1065                  if (pszToken)                          else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) {
1066                      pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1067              }                                  if (pszToken)
1068              else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) {                                          pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken));
1069                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1070                  if (pszToken)                          else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) {
1071                      pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1072              }                                  if (pszToken)
1073              else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {                                          pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken));
1074                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1075                  if (pszToken) {                          else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {
1076                      pszToken = lscp_ltrim(pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1077                      if (strcasecmp(pszToken, "ALL") == 0)                                  if (pszToken) {
1078                          pChannelInfo->midi_channel = LSCP_MIDI_CHANNEL_ALL;                                          pszToken = lscp_ltrim(pszToken);
1079                      else                                          if (strcasecmp(pszToken, "ALL") == 0)
1080                          pChannelInfo->midi_channel = atoi(pszToken);                                                  pChannelInfo->midi_channel = LSCP_MIDI_CHANNEL_ALL;
1081                  }                                          else
1082              }                                                  pChannelInfo->midi_channel = atoi(pszToken);
1083              else if (strcasecmp(pszToken, "VOLUME") == 0) {                                  }
1084                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
1085                  if (pszToken)                          else if (strcasecmp(pszToken, "MIDI_INSTRUMENT_MAP") == 0) {
1086                      pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1087              }                                  if (pszToken) {
1088              else if (strcasecmp(pszToken, "MUTE") == 0) {                                          pszToken = lscp_ltrim(pszToken);
1089                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                                          if (strcasecmp(pszToken, "NONE") == 0)
1090                  if (pszToken)                                                  pChannelInfo->midi_map = LSCP_MIDI_MAP_NONE;
1091                      pChannelInfo->mute = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0);                                          else
1092              }                                          if (strcasecmp(pszToken, "DEFAULT") == 0)
1093              else if (strcasecmp(pszToken, "SOLO") == 0) {                                                  pChannelInfo->midi_map = LSCP_MIDI_MAP_DEFAULT;
1094                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                                          else
1095                  if (pszToken)                                                  pChannelInfo->midi_map = atoi(pszToken);
1096                      pChannelInfo->solo = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0);                                  }
1097              }                          }
1098              pszToken = lscp_strtok(NULL, pszSeps, &(pch));                          else if (strcasecmp(pszToken, "VOLUME") == 0) {
1099          }                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1100      }                                  if (pszToken)
1101      else pChannelInfo = NULL;                                          pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken));
1102                            }
1103                            else if (strcasecmp(pszToken, "MUTE") == 0) {
1104                                    pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1105                                    if (pszToken)
1106                                            pChannelInfo->mute = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0);
1107                            }
1108                            else if (strcasecmp(pszToken, "SOLO") == 0) {
1109                                    pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1110                                    if (pszToken)
1111                                            pChannelInfo->solo = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0);
1112                            }
1113                            pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1114                    }
1115            }
1116            else pChannelInfo = NULL;
1117    
1118      // Unlock this section up.          // Unlock this section up.
1119      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
1120    
1121      return pChannelInfo;          return pChannelInfo;
1122  }  }
1123    
1124    
# Line 1094  lscp_channel_info_t *lscp_get_channel_in Line 1133  lscp_channel_info_t *lscp_get_channel_in
1133   */   */
1134  int lscp_get_channel_voice_count ( lscp_client_t *pClient, int iSamplerChannel )  int lscp_get_channel_voice_count ( lscp_client_t *pClient, int iSamplerChannel )
1135  {  {
1136      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1137      int iVoiceCount = -1;          int iVoiceCount = -1;
1138    
1139      if (iSamplerChannel < 0)          if (pClient == NULL)
1140          return iVoiceCount;                  return -1;
1141            if (iSamplerChannel < 0)
1142                    return -1;
1143    
1144      // Lock this section up.          // Lock this section up.
1145      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
1146    
1147      sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel);          sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel);
1148      if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)          if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
1149          iVoiceCount = atoi(lscp_client_get_result(pClient));                  iVoiceCount = atoi(lscp_client_get_result(pClient));
1150    
1151      // Unlock this section down.          // Unlock this section down.
1152      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
1153    
1154      return iVoiceCount;          return iVoiceCount;
1155  }  }
1156    
1157    
# Line 1125  int lscp_get_channel_voice_count ( lscp_ Line 1166  int lscp_get_channel_voice_count ( lscp_
1166   */   */
1167  int lscp_get_channel_stream_count ( lscp_client_t *pClient, int iSamplerChannel )  int lscp_get_channel_stream_count ( lscp_client_t *pClient, int iSamplerChannel )
1168  {  {
1169      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1170      int iStreamCount = -1;          int iStreamCount = -1;
1171    
1172      if (iSamplerChannel < 0)          if (pClient == NULL)
1173          return iStreamCount;                  return -1;
1174            if (iSamplerChannel < 0)
1175                    return -1;
1176    
1177      // Lock this section up.          // Lock this section up.
1178      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
1179    
1180      sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel);          sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel);
1181      if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)          if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
1182          iStreamCount = atoi(lscp_client_get_result(pClient));                  iStreamCount = atoi(lscp_client_get_result(pClient));
1183    
1184      // Unlock this section down.          // Unlock this section down.
1185      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
1186    
1187      return iStreamCount;          return iStreamCount;
1188  }  }
1189    
1190    
# Line 1156  int lscp_get_channel_stream_count ( lscp Line 1199  int lscp_get_channel_stream_count ( lscp
1199   */   */
1200  int lscp_get_channel_stream_usage ( lscp_client_t *pClient, int iSamplerChannel )  int lscp_get_channel_stream_usage ( lscp_client_t *pClient, int iSamplerChannel )
1201  {  {
1202      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1203      int  iStreamUsage = -1;          int  iStreamUsage = -1;
1204      const char *pszResult;          const char *pszResult;
1205      const char *pszSeps = "[]%,";          const char *pszSeps = "[]%,";
1206      char *pszToken;          char *pszToken;
1207      char *pch;          char *pch;
1208      int   iStream;          int   iStream;
1209      int   iPercent;          int   iPercent;
1210    
1211      if (iSamplerChannel < 0)          if (pClient == NULL)
1212          return iStreamUsage;                  return -1;
1213            if (iSamplerChannel < 0)
1214      // Lock this section up.                  return -1;
1215      lscp_mutex_lock(pClient->mutex);  
1216            // Lock this section up.
1217      iStream = 0;          lscp_mutex_lock(pClient->mutex);
1218      sprintf(szQuery, "GET CHANNEL BUFFER_FILL PERCENTAGE %d\r\n", iSamplerChannel);  
1219      if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) {          iStream = 0;
1220          pszResult = lscp_client_get_result(pClient);          sprintf(szQuery, "GET CHANNEL BUFFER_FILL PERCENTAGE %d\r\n", iSamplerChannel);
1221          pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));          if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) {
1222          while (pszToken) {                  pszResult = lscp_client_get_result(pClient);
1223              if (*pszToken) {                  pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1224                  // Skip stream id.                  while (pszToken) {
1225                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));                          if (*pszToken) {
1226                  if (pszToken == NULL)                                  // Skip stream id.
1227                      break;                                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1228                  // Get least buffer fill percentage.                                  if (pszToken == NULL)
1229                  iPercent = atol(pszToken);                                          break;
1230                  if (iStreamUsage > iPercent || iStream == 0)                                  // Get least buffer fill percentage.
1231                      iStreamUsage = iPercent;                                  iPercent = atol(pszToken);
1232                  iStream++;                                  if (iStreamUsage > iPercent || iStream == 0)
1233              }                                          iStreamUsage = iPercent;
1234              pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                  iStream++;
1235          }                          }
1236      }                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1237                    }
1238            }
1239    
1240      // Unlock this section down.          // Unlock this section down.
1241      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
1242    
1243      return iStreamUsage;          return iStreamUsage;
1244  }  }
1245    
1246    
# Line 1215  int lscp_get_channel_stream_usage ( lscp Line 1260  int lscp_get_channel_stream_usage ( lscp
1260   */   */
1261  lscp_buffer_fill_t *lscp_get_channel_buffer_fill ( lscp_client_t *pClient, lscp_usage_t usage_type, int iSamplerChannel )  lscp_buffer_fill_t *lscp_get_channel_buffer_fill ( lscp_client_t *pClient, lscp_usage_t usage_type, int iSamplerChannel )
1262  {  {
1263      lscp_buffer_fill_t *pBufferFill;          lscp_buffer_fill_t *pBufferFill;
1264      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1265      int iStreamCount;          int iStreamCount;
1266      const char *pszUsageType = (usage_type == LSCP_USAGE_BYTES ? "BYTES" : "PERCENTAGE");          const char *pszUsageType = (usage_type == LSCP_USAGE_BYTES ? "BYTES" : "PERCENTAGE");
1267      const char *pszResult;          const char *pszResult;
1268      const char *pszSeps = "[]%,";          const char *pszSeps = "[]%,";
1269      char *pszToken;          char *pszToken;
1270      char *pch;          char *pch;
1271      int   iStream;          int   iStream;
1272    
1273      // Retrieve a channel stream estimation.          // Retrieve a channel stream estimation.
1274      iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel);          iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel);
1275      if (pClient->iStreamCount < 0)          if (iStreamCount < 0)
1276          return NULL;                  return NULL;
1277    
1278      // Lock this section up.          // Lock this section up.
1279      lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
1280    
1281      // Check if we need to reallocate the stream usage array.          // Check if we need to reallocate the stream usage array.
1282      if (pClient->iStreamCount != iStreamCount) {          if (pClient->iStreamCount != iStreamCount) {
1283          if (pClient->buffer_fill)                  if (pClient->buffer_fill)
1284              free(pClient->buffer_fill);                          free(pClient->buffer_fill);
1285          if (iStreamCount > 0)                  if (iStreamCount > 0)
1286              pClient->buffer_fill = (lscp_buffer_fill_t *) malloc(iStreamCount * sizeof(lscp_buffer_fill_t));                          pClient->buffer_fill = (lscp_buffer_fill_t *) malloc(iStreamCount * sizeof(lscp_buffer_fill_t));
1287          else                  else
1288              pClient->buffer_fill = NULL;                          pClient->buffer_fill = NULL;
1289          pClient->iStreamCount = iStreamCount;                  pClient->iStreamCount = iStreamCount;
1290      }          }
1291    
1292      // Get buffer fill usage...          // Get buffer fill usage...
1293      pBufferFill = pClient->buffer_fill;          pBufferFill = pClient->buffer_fill;
1294      if (pBufferFill && iStreamCount > 0) {          if (pBufferFill && iStreamCount > 0) {
1295          iStream = 0;                  iStream = 0;
1296          pBufferFill = pClient->buffer_fill;                  pBufferFill = pClient->buffer_fill;
1297          sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel);                  sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel);
1298          if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) {                  if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) {
1299              pszResult = lscp_client_get_result(pClient);                          pszResult = lscp_client_get_result(pClient);
1300              pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));                          pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1301              while (pszToken && iStream < pClient->iStreamCount) {                          while (pszToken && iStream < pClient->iStreamCount) {
1302                  if (*pszToken) {                                  if (*pszToken) {
1303                      pBufferFill[iStream].stream_id = atol(pszToken);                                          pBufferFill[iStream].stream_id = atol(pszToken);
1304                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1305                      if (pszToken == NULL)                                          if (pszToken == NULL)
1306                          break;                                                  break;
1307                      pBufferFill[iStream].stream_usage = atol(pszToken);                                          pBufferFill[iStream].stream_usage = atol(pszToken);
1308                      iStream++;                                          iStream++;
1309                  }                                  }
1310                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1311              }                          }
1312          }   // Reset the usage, whatever it was before.                  }   // Reset the usage, whatever it was before.
1313          else while (iStream < pClient->iStreamCount)                  else while (iStream < pClient->iStreamCount)
1314              pBufferFill[iStream++].stream_usage = 0;                          pBufferFill[iStream++].stream_usage = 0;
1315      }          }
1316    
1317      // Unlock this section down.          // Unlock this section down.
1318      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
1319    
1320      return pBufferFill;          return pBufferFill;
1321  }  }
1322    
1323    
# Line 1288  lscp_buffer_fill_t *lscp_get_channel_buf Line 1333  lscp_buffer_fill_t *lscp_get_channel_buf
1333   */   */
1334  lscp_status_t lscp_set_channel_audio_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszAudioDriver )  lscp_status_t lscp_set_channel_audio_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszAudioDriver )
1335  {  {
1336      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1337    
1338      if (iSamplerChannel < 0 || pszAudioDriver == NULL)          if (iSamplerChannel < 0 || pszAudioDriver == NULL)
1339          return LSCP_FAILED;                  return LSCP_FAILED;
1340    
1341      sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_TYPE %d %s\r\n", iSamplerChannel, pszAudioDriver);          sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_TYPE %d %s\r\n", iSamplerChannel, pszAudioDriver);
1342      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1343  }  }
1344    
1345    
# Line 1310  lscp_status_t lscp_set_channel_audio_typ Line 1355  lscp_status_t lscp_set_channel_audio_typ
1355   */   */
1356  lscp_status_t lscp_set_channel_audio_device ( lscp_client_t *pClient, int iSamplerChannel, int iAudioDevice )  lscp_status_t lscp_set_channel_audio_device ( lscp_client_t *pClient, int iSamplerChannel, int iAudioDevice )
1357  {  {
1358      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1359    
1360      if (iSamplerChannel < 0 || iAudioDevice < 0)          if (iSamplerChannel < 0 || iAudioDevice < 0)
1361          return LSCP_FAILED;                  return LSCP_FAILED;
1362    
1363      sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_DEVICE %d %d\r\n", iSamplerChannel, iAudioDevice);          sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_DEVICE %d %d\r\n", iSamplerChannel, iAudioDevice);
1364      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1365  }  }
1366    
1367    
# Line 1333  lscp_status_t lscp_set_channel_audio_dev Line 1378  lscp_status_t lscp_set_channel_audio_dev
1378   */   */
1379  lscp_status_t lscp_set_channel_audio_channel ( lscp_client_t *pClient, int iSamplerChannel, int iAudioOut, int iAudioIn )  lscp_status_t lscp_set_channel_audio_channel ( lscp_client_t *pClient, int iSamplerChannel, int iAudioOut, int iAudioIn )
1380  {  {
1381      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1382    
1383      if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0)          if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0)
1384          return LSCP_FAILED;                  return LSCP_FAILED;
1385    
1386      sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_CHANNEL %d %d %d\r\n", iSamplerChannel, iAudioOut, iAudioIn);          sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_CHANNEL %d %d %d\r\n", iSamplerChannel, iAudioOut, iAudioIn);
1387      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1388  }  }
1389    
1390    
# Line 1355  lscp_status_t lscp_set_channel_audio_cha Line 1400  lscp_status_t lscp_set_channel_audio_cha
1400   */   */
1401  lscp_status_t lscp_set_channel_midi_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszMidiDriver )  lscp_status_t lscp_set_channel_midi_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszMidiDriver )
1402  {  {
1403      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1404    
1405      if (iSamplerChannel < 0 || pszMidiDriver == NULL)          if (iSamplerChannel < 0 || pszMidiDriver == NULL)
1406          return LSCP_FAILED;                  return LSCP_FAILED;
1407    
1408      sprintf(szQuery, "SET CHANNEL MIDI_INPUT_TYPE %d %s\r\n", iSamplerChannel, pszMidiDriver);          sprintf(szQuery, "SET CHANNEL MIDI_INPUT_TYPE %d %s\r\n", iSamplerChannel, pszMidiDriver);
1409      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1410  }  }
1411    
1412    
# Line 1377  lscp_status_t lscp_set_channel_midi_type Line 1422  lscp_status_t lscp_set_channel_midi_type
1422   */   */
1423  lscp_status_t lscp_set_channel_midi_device ( lscp_client_t *pClient, int iSamplerChannel, int iMidiDevice )  lscp_status_t lscp_set_channel_midi_device ( lscp_client_t *pClient, int iSamplerChannel, int iMidiDevice )
1424  {  {
1425      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1426    
1427      if (iSamplerChannel < 0 || iMidiDevice < 0)          if (iSamplerChannel < 0 || iMidiDevice < 0)
1428          return LSCP_FAILED;                  return LSCP_FAILED;
1429    
1430      sprintf(szQuery, "SET CHANNEL MIDI_INPUT_DEVICE %d %d\r\n", iSamplerChannel, iMidiDevice);          sprintf(szQuery, "SET CHANNEL MIDI_INPUT_DEVICE %d %d\r\n", iSamplerChannel, iMidiDevice);
1431      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1432  }  }
1433    
1434    
# Line 1399  lscp_status_t lscp_set_channel_midi_devi Line 1444  lscp_status_t lscp_set_channel_midi_devi
1444   */   */
1445  lscp_status_t lscp_set_channel_midi_port ( lscp_client_t *pClient, int iSamplerChannel, int iMidiPort )  lscp_status_t lscp_set_channel_midi_port ( lscp_client_t *pClient, int iSamplerChannel, int iMidiPort )
1446  {  {
1447      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1448    
1449      if (iSamplerChannel < 0 || iMidiPort < 0)          if (iSamplerChannel < 0 || iMidiPort < 0)
1450          return LSCP_FAILED;                  return LSCP_FAILED;
1451    
1452      sprintf(szQuery, "SET CHANNEL MIDI_INPUT_PORT %d %d\r\n", iSamplerChannel, iMidiPort);          sprintf(szQuery, "SET CHANNEL MIDI_INPUT_PORT %d %d\r\n", iSamplerChannel, iMidiPort);
1453      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1454  }  }
1455    
1456    
# Line 1416  lscp_status_t lscp_set_channel_midi_port Line 1461  lscp_status_t lscp_set_channel_midi_port
1461   *  @param pClient          Pointer to client instance structure.   *  @param pClient          Pointer to client instance structure.
1462   *  @param iSamplerChannel  Sampler channel number.   *  @param iSamplerChannel  Sampler channel number.
1463   *  @param iMidiChannel     MIDI channel address number to listen (0-15) or   *  @param iMidiChannel     MIDI channel address number to listen (0-15) or
1464   *                          LSCP_MIDI_CHANNEL_ALL (16) to listen on all channels.   *                          @ref LSCP_MIDI_CHANNEL_ALL (16) to listen on all channels.
1465   *   *
1466   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1467   */   */
1468  lscp_status_t lscp_set_channel_midi_channel ( lscp_client_t *pClient, int iSamplerChannel, int iMidiChannel )  lscp_status_t lscp_set_channel_midi_channel ( lscp_client_t *pClient, int iSamplerChannel, int iMidiChannel )
1469  {  {
1470      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1471    
1472            if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16)
1473                    return LSCP_FAILED;
1474    
1475            if (iMidiChannel == LSCP_MIDI_CHANNEL_ALL)
1476                    sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel);
1477            else
1478                    sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel);
1479            return lscp_client_query(pClient, szQuery);
1480    }
1481    
1482    
1483    /**
1484     *  Setting MIDI instrument map:
1485     *  SET CHANNEL MIDI_INSTRUMENT_MAP <sampler-channel> <midi-map>
1486     *
1487     *  @param pClient          Pointer to client instance structure.
1488     *  @param iSamplerChannel  Sampler channel number.
1489     *  @param iMidiMap         MIDI instrument map number, or either
1490     *                          @ref LSCP_MIDI_MAP_NONE or
1491     *                          @ref LSCP_MIDI_MAP_DEFAULT .
1492     *
1493     *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1494     */
1495    lscp_status_t lscp_set_channel_midi_map ( lscp_client_t *pClient, int iSamplerChannel, int iMidiMap )
1496    {
1497            char szQuery[LSCP_BUFSIZ];
1498    
1499            if (iSamplerChannel < 0)
1500                    return LSCP_FAILED;
1501    
1502            sprintf(szQuery, "SET CHANNEL MIDI_INSTRUMENT_MAP %d ", iSamplerChannel);
1503            if (iMidiMap == LSCP_MIDI_MAP_NONE)
1504                    strcat(szQuery , "NONE");
1505            else
1506            if (iMidiMap == LSCP_MIDI_MAP_DEFAULT)
1507                    strcat(szQuery , "DEFAULT");
1508            else
1509                    sprintf(szQuery + strlen(szQuery), "%d", iMidiMap);
1510    
1511      if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16)          strcat(szQuery, "\r\n");
         return LSCP_FAILED;  
1512    
1513      if (iMidiChannel == LSCP_MIDI_CHANNEL_ALL)          return lscp_client_query(pClient, szQuery);
         sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel);  
     else  
         sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel);  
     return lscp_client_query(pClient, szQuery);  
1514  }  }
1515    
1516    
# Line 1449  lscp_status_t lscp_set_channel_midi_chan Line 1528  lscp_status_t lscp_set_channel_midi_chan
1528   */   */
1529  lscp_status_t lscp_set_channel_volume ( lscp_client_t *pClient, int iSamplerChannel, float fVolume )  lscp_status_t lscp_set_channel_volume ( lscp_client_t *pClient, int iSamplerChannel, float fVolume )
1530  {  {
1531      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1532    
1533      if (iSamplerChannel < 0 || fVolume < 0.0)          if (iSamplerChannel < 0 || fVolume < 0.0)
1534          return LSCP_FAILED;                  return LSCP_FAILED;
1535    
1536      sprintf(szQuery, "SET CHANNEL VOLUME %d %g\r\n", iSamplerChannel, fVolume);          sprintf(szQuery, "SET CHANNEL VOLUME %d %g\r\n", iSamplerChannel, fVolume);
1537      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1538  }  }
1539    
1540    
# Line 1473  lscp_status_t lscp_set_channel_volume ( Line 1552  lscp_status_t lscp_set_channel_volume (
1552   */   */
1553  lscp_status_t lscp_set_channel_mute ( lscp_client_t *pClient, int iSamplerChannel, int iMute )  lscp_status_t lscp_set_channel_mute ( lscp_client_t *pClient, int iSamplerChannel, int iMute )
1554  {  {
1555      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1556    
1557      if (iSamplerChannel < 0 || iMute < 0 || iMute > 1)          if (iSamplerChannel < 0 || iMute < 0 || iMute > 1)
1558          return LSCP_FAILED;                  return LSCP_FAILED;
1559    
1560      sprintf(szQuery, "SET CHANNEL MUTE %d %d\r\n", iSamplerChannel, iMute);          sprintf(szQuery, "SET CHANNEL MUTE %d %d\r\n", iSamplerChannel, iMute);
1561      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1562  }  }
1563    
1564    
# Line 1497  lscp_status_t lscp_set_channel_mute ( ls Line 1576  lscp_status_t lscp_set_channel_mute ( ls
1576   */   */
1577  lscp_status_t lscp_set_channel_solo ( lscp_client_t *pClient, int iSamplerChannel, int iSolo )  lscp_status_t lscp_set_channel_solo ( lscp_client_t *pClient, int iSamplerChannel, int iSolo )
1578  {  {
1579      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1580    
1581      if (iSamplerChannel < 0 || iSolo < 0 || iSolo > 1)          if (iSamplerChannel < 0 || iSolo < 0 || iSolo > 1)
1582          return LSCP_FAILED;                  return LSCP_FAILED;
1583    
1584      sprintf(szQuery, "SET CHANNEL SOLO %d %d\r\n", iSamplerChannel, iSolo);          sprintf(szQuery, "SET CHANNEL SOLO %d %d\r\n", iSamplerChannel, iSolo);
1585      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1586  }  }
1587    
1588    
# Line 1518  lscp_status_t lscp_set_channel_solo ( ls Line 1597  lscp_status_t lscp_set_channel_solo ( ls
1597   */   */
1598  lscp_status_t lscp_reset_channel ( lscp_client_t *pClient, int iSamplerChannel )  lscp_status_t lscp_reset_channel ( lscp_client_t *pClient, int iSamplerChannel )
1599  {  {
1600      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1601    
1602      if (iSamplerChannel < 0)          if (iSamplerChannel < 0)
1603          return LSCP_FAILED;                  return LSCP_FAILED;
1604    
1605      sprintf(szQuery, "RESET CHANNEL %d\r\n", iSamplerChannel);          sprintf(szQuery, "RESET CHANNEL %d\r\n", iSamplerChannel);
1606      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1607  }  }
1608    
1609    
# Line 1538  lscp_status_t lscp_reset_channel ( lscp_ Line 1617  lscp_status_t lscp_reset_channel ( lscp_
1617   */   */
1618  lscp_status_t lscp_reset_sampler ( lscp_client_t *pClient )  lscp_status_t lscp_reset_sampler ( lscp_client_t *pClient )
1619  {  {
1620      return lscp_client_query(pClient, "RESET\r\n");          return lscp_client_query(pClient, "RESET\r\n");
1621  }  }
1622    
1623    
# Line 1553  lscp_status_t lscp_reset_sampler ( lscp_ Line 1632  lscp_status_t lscp_reset_sampler ( lscp_
1632   */   */
1633  lscp_server_info_t *lscp_get_server_info ( lscp_client_t *pClient )  lscp_server_info_t *lscp_get_server_info ( lscp_client_t *pClient )
1634  {  {
1635      lscp_server_info_t *pServerInfo;          lscp_server_info_t *pServerInfo;
1636      const char *pszResult;          const char *pszResult;
1637      const char *pszSeps = ":";          const char *pszSeps = ":";
1638      const char *pszCrlf = "\r\n";          const char *pszCrlf = "\r\n";
1639      char *pszToken;          char *pszToken;
1640      char *pch;          char *pch;
1641    
1642      // Lock this section up.          if (pClient == NULL)
1643      lscp_mutex_lock(pClient->mutex);                  return NULL;
1644    
1645      pServerInfo = &(pClient->server_info);          // Lock this section up.
1646      lscp_server_info_reset(pServerInfo);          lscp_mutex_lock(pClient->mutex);
1647    
1648      if (lscp_client_call(pClient, "GET SERVER INFO\r\n", 1) == LSCP_OK) {          pServerInfo = &(pClient->server_info);
1649          pszResult = lscp_client_get_result(pClient);          lscp_server_info_reset(pServerInfo);
1650          pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));  
1651          while (pszToken) {          if (lscp_client_call(pClient, "GET SERVER INFO\r\n", 1) == LSCP_OK) {
1652              if (strcasecmp(pszToken, "DESCRIPTION") == 0) {                  pszResult = lscp_client_get_result(pClient);
1653                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1654                  if (pszToken)                  while (pszToken) {
1655                      lscp_unquote_dup(&(pServerInfo->description), &pszToken);                          if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
1656              }                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1657              else if (strcasecmp(pszToken, "VERSION") == 0) {                                  if (pszToken)
1658                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                                          lscp_unquote_dup(&(pServerInfo->description), &pszToken);
1659                  if (pszToken)                          }
1660                      lscp_unquote_dup(&(pServerInfo->version), &pszToken);                          else if (strcasecmp(pszToken, "VERSION") == 0) {
1661              }                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1662              pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                  if (pszToken)
1663          }                                          lscp_unquote_dup(&(pServerInfo->version), &pszToken);
1664      }                          }
1665      else pServerInfo = NULL;                          else if (strcasecmp(pszToken, "PROTOCOL_VERSION") == 0) {
1666                                    pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1667                                    if (pszToken)
1668                                            lscp_unquote_dup(&(pServerInfo->protocol_version), &pszToken);
1669                            }
1670                            pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1671                    }
1672            }
1673            else pServerInfo = NULL;
1674    
1675      // Unlock this section down.          // Unlock this section down.
1676      lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
1677    
1678      return pServerInfo;          return pServerInfo;
1679  }  }
1680    
1681    
# Line 1603  lscp_server_info_t *lscp_get_server_info Line 1690  lscp_server_info_t *lscp_get_server_info
1690   */   */
1691  int lscp_get_total_voice_count ( lscp_client_t *pClient )  int lscp_get_total_voice_count ( lscp_client_t *pClient )
1692  {  {
1693      int iVoiceCount = -1;          int iVoiceCount = -1;
1694    
1695      // Lock this section up.          if (pClient == NULL)
1696      lscp_mutex_lock(pClient->mutex);                  return -1;
1697    
1698      if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT\r\n", 0) == LSCP_OK)          // Lock this section up.
1699          iVoiceCount = atoi(lscp_client_get_result(pClient));          lscp_mutex_lock(pClient->mutex);
1700    
1701      // Unlock this section down.          if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT\r\n", 0) == LSCP_OK)
1702      lscp_mutex_unlock(pClient->mutex);                  iVoiceCount = atoi(lscp_client_get_result(pClient));
1703    
1704      return iVoiceCount;          // Unlock this section down.
1705            lscp_mutex_unlock(pClient->mutex);
1706    
1707            return iVoiceCount;
1708  }  }
1709    
1710    
# Line 1629  int lscp_get_total_voice_count ( lscp_cl Line 1719  int lscp_get_total_voice_count ( lscp_cl
1719   */   */
1720  int lscp_get_total_voice_count_max ( lscp_client_t *pClient )  int lscp_get_total_voice_count_max ( lscp_client_t *pClient )
1721  {  {
1722      int iVoiceCount = -1;          int iVoiceCount = -1;
1723    
1724            if (pClient == NULL)
1725                    return -1;
1726    
1727            // Lock this section up.
1728            lscp_mutex_lock(pClient->mutex);
1729    
1730            if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT_MAX\r\n", 0) == LSCP_OK)
1731                    iVoiceCount = atoi(lscp_client_get_result(pClient));
1732    
1733            // Unlock this section down.
1734            lscp_mutex_unlock(pClient->mutex);
1735    
1736            return iVoiceCount;
1737    }
1738    
1739    
1740    /**
1741     *  Create a new MIDI instrument map:
1742     *  ADD MIDI_INSTRUMENT_MAP [<name>]
1743     *
1744     *  @param pClient      Pointer to client instance structure.
1745     *  @param pszMapName   MIDI instrument map name (optional)
1746     *
1747     *  @returns The new MIDI instrument map number identifier,
1748     *  or -1 in case of failure.
1749     */
1750    int lscp_add_midi_instrument_map ( lscp_client_t *pClient, const char *pszMapName )
1751    {
1752            int iMidiMap = -1;
1753            char szQuery[LSCP_BUFSIZ];
1754    
1755            if (pClient == NULL)
1756                    return -1;
1757    
1758            // Lock this section up.
1759            lscp_mutex_lock(pClient->mutex);
1760    
1761            strcpy(szQuery, "ADD MIDI_INSTRUMENT_MAP");
1762            
1763            if (pszMapName)
1764                    sprintf(szQuery + strlen(szQuery), " '%s'", pszMapName);
1765    
1766            strcat(szQuery, "\r\n");
1767    
1768            if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
1769                    iMidiMap = atoi(lscp_client_get_result(pClient));
1770    
1771            // Unlock this section down.
1772            lscp_mutex_unlock(pClient->mutex);
1773    
1774            return iMidiMap;
1775    }
1776    
1777    
1778    /**
1779     *  Delete one particular or all MIDI instrument maps:
1780     *  REMOVE MIDI_INSTRUMENT_MAP <midi-map>
1781     *
1782     *  @param pClient  Pointer to client instance structure.
1783     *  @param iMidiMap MIDI instrument map number.
1784     *
1785     *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1786     */
1787    lscp_status_t lscp_remove_midi_instrument_map ( lscp_client_t *pClient, int iMidiMap )
1788    {
1789            char szQuery[LSCP_BUFSIZ];
1790    
1791            if (iMidiMap < 0)
1792                    return LSCP_FAILED;
1793    
1794            sprintf(szQuery, "REMOVE MIDI_INSTRUMENT_MAP %d\r\n", iMidiMap);
1795    
1796            return lscp_client_query(pClient, szQuery);
1797    }
1798    
1799    
1800    /**
1801     *  Get amount of existing MIDI instrument maps:
1802     *  GET MIDI_INSTRUMENT_MAPS
1803     *
1804     *  @param pClient  Pointer to client instance structure.
1805     *
1806     *  @returns The current total number of MIDI instrument maps
1807     *  on success, -1 otherwise.
1808     */
1809    int lscp_get_midi_instrument_maps ( lscp_client_t *pClient )
1810    {
1811            int iMidiMaps = -1;
1812    
1813            if (pClient == NULL)
1814                    return -1;
1815    
1816            // Lock this section up.
1817            lscp_mutex_lock(pClient->mutex);
1818    
1819            if (lscp_client_call(pClient, "GET MIDI_INSTRUMENT_MAPS\r\n", 0) == LSCP_OK)
1820                    iMidiMaps = atoi(lscp_client_get_result(pClient));
1821    
1822            // Unlock this section doen.
1823            lscp_mutex_unlock(pClient->mutex);
1824    
1825            return iMidiMaps;
1826    }
1827    
1828    
1829    /**
1830     *  Getting all created MIDI instrument maps:
1831     *  LIST MIDI_INSTRUMENT_MAPS
1832     *
1833     *  @param pClient  Pointer to client instance structure.
1834     *
1835     *  @returns An array of the MIDI instrument map identifiers as positive
1836     *  integers, terminated with -1 on success, NULL otherwise.
1837     */
1838    int *lscp_list_midi_instrument_maps ( lscp_client_t *pClient )
1839    {
1840            const char *pszSeps = ",";
1841    
1842            if (pClient == NULL)
1843                    return NULL;
1844    
1845            // Lock this section up.
1846            lscp_mutex_lock(pClient->mutex);
1847    
1848            if (pClient->midi_maps) {
1849                    lscp_isplit_destroy(pClient->midi_maps);
1850                    pClient->midi_maps = NULL;
1851            }
1852    
1853      // Lock this section up.          if (lscp_client_call(pClient, "LIST MIDI_INSTRUMENT_MAPS\r\n", 0) == LSCP_OK)
1854      lscp_mutex_lock(pClient->mutex);                  pClient->midi_maps = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps);
1855    
1856      if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT_MAX\r\n", 0) == LSCP_OK)          // Unlock this section down.
1857          iVoiceCount = atoi(lscp_client_get_result(pClient));          lscp_mutex_unlock(pClient->mutex);
1858    
1859      // Unlock this section down.          return pClient->midi_maps;
1860      lscp_mutex_unlock(pClient->mutex);  }
1861    
1862    
1863    /**
1864     *  Getting a MIDI instrument map name:
1865     *  GET MIDI_INSTRUMENT_MAP INFO <midi-map>
1866     *
1867     *  @param pClient  Pointer to client instance structure.
1868     *  @param iMidiMap MIDI instrument map number.
1869     *
1870     *  @returns The MIDI instrument map name on success, NULL on failure.
1871     */
1872    const char *lscp_get_midi_instrument_map_name ( lscp_client_t *pClient, int iMidiMap )
1873    {
1874            char szQuery[LSCP_BUFSIZ];
1875            const char *pszResult;
1876            const char *pszSeps = ":";
1877            const char *pszCrlf = "\r\n";
1878            char *pszToken;
1879            char *pch;
1880    
1881            if (pClient == NULL)
1882                    return NULL;
1883            if (iMidiMap < 0)
1884                    return NULL;
1885    
1886            // Lock this section up.
1887            lscp_mutex_lock(pClient->mutex);
1888            
1889            if (pClient->midi_map_name) {
1890                    free(pClient->midi_map_name);
1891                    pClient->midi_map_name = NULL;
1892            }
1893    
1894            sprintf(szQuery, "GET MIDI_INSTRUMENT_MAP INFO %d\r\n", iMidiMap);
1895            if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {
1896                    pszResult = lscp_client_get_result(pClient);
1897                    pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1898                    while (pszToken) {
1899                            if (strcasecmp(pszToken, "NAME") == 0) {
1900                                    pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1901                                    if (pszToken)
1902                                            lscp_unquote_dup(&(pClient->midi_map_name), &pszToken);
1903                            }
1904                            pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1905                    }
1906            }
1907    
1908            // Unlock this section down.
1909            lscp_mutex_unlock(pClient->mutex);
1910    
1911      return iVoiceCount;          return pClient->midi_map_name;
1912    }
1913    
1914    
1915    /**
1916     *  Renaming a MIDI instrument map:
1917     *  SET MIDI_INSTRUMENT_MAP NAME <midi-map> <map-name>
1918     *
1919     *  @param pClient      Pointer to client instance structure.
1920     *  @param iMidiMap     MIDI instrument map number.
1921     *  @param pszMapName   MIDI instrument map name.
1922     *
1923     *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1924     */
1925    lscp_status_t lscp_set_midi_instrument_map_name ( lscp_client_t *pClient, int iMidiMap, const char *pszMapName )
1926    {
1927            char szQuery[LSCP_BUFSIZ];
1928    
1929            if (iMidiMap < 0)
1930                    return LSCP_FAILED;
1931            if (pszMapName == NULL)
1932                    return LSCP_FAILED;
1933    
1934            sprintf(szQuery, "SET MIDI_INSTRUMENT_MAP NAME %d '%s'\r\n",
1935                    iMidiMap, pszMapName);
1936    
1937            return lscp_client_query(pClient, szQuery);
1938  }  }
1939    
1940    
1941  /**  /**
1942   *  Create or replace a MIDI instrumnet map entry:   *  Create or replace a MIDI instrumnet map entry:
1943   *  MAP MIDI_INSTRUMENT <midi-bank-msb> <midi-bank-lsb> <midi-prog>   *  MAP MIDI_INSTRUMENT <midi-map> <midi-bank> <midi-prog>
1944   *      <engine-name> <filename> <instr-index> <volume> <load-mode> [<name>]   *      <engine-name> <filename> <instr-index> <volume> [<load-mode> [<name>]}
1945   *   *
1946   *  @param pClient          Pointer to client instance structure.   *  @param pClient          Pointer to client instance structure.
1947   *  @param pMidiInstr       MIDI instrument bank and program parameter key.   *  @param pMidiInstr       MIDI instrument bank and program parameter key.
# Line 1663  int lscp_get_total_voice_count_max ( lsc Line 1957  int lscp_get_total_voice_count_max ( lsc
1957   *                          @ref LSCP_LOAD_ON_DEMAND, or   *                          @ref LSCP_LOAD_ON_DEMAND, or
1958   *                          @ref LSCP_LOAD_ON_DEMAND_HOLD, or   *                          @ref LSCP_LOAD_ON_DEMAND_HOLD, or
1959   *                          @ref LSCP_LOAD_PERSISTENT.   *                          @ref LSCP_LOAD_PERSISTENT.
1960   *  @param pszName          Instrument custom name for the map entry.   *  @param pszName         Instrument custom name for the map entry (optional).
1961   *   *
1962   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1963   */   */
1964  lscp_status_t lscp_map_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr, const char *pszEngineName, const char *pszFileName, int iInstrIndex, float fVolume, lscp_load_mode_t load_mode, const char *pszName )  lscp_status_t lscp_map_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr, const char *pszEngineName, const char *pszFileName, int iInstrIndex, float fVolume, lscp_load_mode_t load_mode, const char *pszName )
1965  {  {
1966      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1967        
1968      if (pMidiInstr->bank_msb < 0 || pMidiInstr->bank_msb > 127)          if (pMidiInstr->map < 0)
1969          return LSCP_FAILED;                  return LSCP_FAILED;
1970      if (pMidiInstr->bank_lsb < 0 || pMidiInstr->bank_lsb > 127)          if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383)
1971          return LSCP_FAILED;                  return LSCP_FAILED;
1972      if (pMidiInstr->program < 0 || pMidiInstr->program > 127)          if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127)
1973          return LSCP_FAILED;                  return LSCP_FAILED;
1974      if (pszEngineName == NULL || pszFileName == NULL)          if (pszEngineName == NULL || pszFileName == NULL)
1975          return LSCP_FAILED;                  return LSCP_FAILED;
1976        
1977      if (fVolume < 0.0f)          if (fVolume < 0.0f)
1978          fVolume = 1.0f;                  fVolume = 1.0f;
1979        
1980      sprintf(szQuery, "MAP MIDI_INSTRUMENT %d %d %d %s '%s' %d %g",          sprintf(szQuery, "MAP MIDI_INSTRUMENT %d %d %d %s '%s' %d %g",
1981          pMidiInstr->bank_msb, pMidiInstr->bank_lsb, pMidiInstr->program,                  pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog,
1982          pszEngineName, pszFileName, iInstrIndex, fVolume);                  pszEngineName, pszFileName, iInstrIndex, fVolume);
1983        
1984      switch (load_mode) {          switch (load_mode) {
1985      case LSCP_LOAD_PERSISTENT:          case LSCP_LOAD_PERSISTENT:
1986          strcat(szQuery, " PERSISTENT");                  strcat(szQuery, " PERSISTENT");
1987          break;                  break;
1988      case LSCP_LOAD_ON_DEMAND_HOLD:          case LSCP_LOAD_ON_DEMAND_HOLD:
1989          strcat(szQuery, " ON_DEMAND_HOLD");                  strcat(szQuery, " ON_DEMAND_HOLD");
1990          break;                  break;
1991      case LSCP_LOAD_ON_DEMAND:          case LSCP_LOAD_ON_DEMAND:
1992          strcat(szQuery, " ON_DEMAND_HOLD");                  strcat(szQuery, " ON_DEMAND");
1993          break;                  break;
1994      case LSCP_LOAD_DEFAULT:          case LSCP_LOAD_DEFAULT:
1995      default:          default:
1996          break;                  break;
1997      }          }
1998        
1999      if (pszName)          if (pszName)
2000          sprintf(szQuery + strlen(szQuery), " '%s'", pszName);                  sprintf(szQuery + strlen(szQuery), " '%s'", pszName);
2001        
2002      strcat(szQuery, "\r\n");          strcat(szQuery, "\r\n");
2003        
2004      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
2005  }  }
2006    
2007    
2008  /**  /**
2009   *  Remove an entry from the MIDI instrument map:   *  Remove an entry from the MIDI instrument map:
2010   *  UNMAP MIDI_INSTRUMENT <midi-bank-msb> <midi-bank-lsb> <midi-prog>   *  UNMAP MIDI_INSTRUMENT <midi-map> <midi-bank> <midi-prog>
2011   *   *
2012   *  @param pClient      Pointer to client instance structure.   *  @param pClient      Pointer to client instance structure.
2013   *  @param pMidiInstr   MIDI instrument bank and program parameter key.   *  @param pMidiInstr   MIDI instrument bank and program parameter key.
# Line 1722  lscp_status_t lscp_map_midi_instrument ( Line 2016  lscp_status_t lscp_map_midi_instrument (
2016   */   */
2017  lscp_status_t lscp_unmap_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr )  lscp_status_t lscp_unmap_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr )
2018  {  {
2019      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
2020    
2021      if (pMidiInstr->bank_msb < 0 || pMidiInstr->bank_msb > 127)          if (pMidiInstr->map < 0)
2022          return LSCP_FAILED;                  return LSCP_FAILED;
2023      if (pMidiInstr->bank_lsb < 0 || pMidiInstr->bank_lsb > 127)          if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383)
2024          return LSCP_FAILED;                  return LSCP_FAILED;
2025      if (pMidiInstr->program < 0 || pMidiInstr->program > 127)          if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127)
2026          return LSCP_FAILED;                  return LSCP_FAILED;
2027    
2028      sprintf(szQuery, "UNMAP MIDI_INSTRUMENT %d %d %d\r\n",          sprintf(szQuery, "UNMAP MIDI_INSTRUMENT %d %d %d\r\n",
2029                  pMidiInstr->bank_msb, pMidiInstr->bank_lsb, pMidiInstr->program);                  pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog);
2030    
2031      return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
2032  }  }
2033    
2034    
2035  /**  /**
2036   *  Get the total count of MIDI instrument map entries:   *  Get the total count of MIDI instrument map entries:
2037   *  GET MIDI_INSTRUMENTS   *  GET MIDI_INSTRUMENTS ALL|<midi-map>
2038   *   *
2039   *  @param pClient  Pointer to client instance structure.   *  @param pClient  Pointer to client instance structure.
2040     *  @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL .
2041   *   *
2042   *  @returns The current total number of MIDI instrument map entries   *  @returns The current total number of MIDI instrument map entries
2043   *  on success, -1 otherwise.   *  on success, -1 otherwise.
2044   */   */
2045  int lscp_get_midi_instruments ( lscp_client_t *pClient )  int lscp_get_midi_instruments ( lscp_client_t *pClient, int iMidiMap )
2046  {  {
2047      int iInstruments = -1;          int iInstruments = -1;
2048            char szQuery[LSCP_BUFSIZ];
2049    
2050            if (pClient == NULL)
2051                    return -1;
2052    
2053            // Lock this section up.
2054            lscp_mutex_lock(pClient->mutex);
2055    
2056      // Lock this section up.          strcpy(szQuery, "GET MIDI_INSTRUMENTS ");
     lscp_mutex_lock(pClient->mutex);  
2057    
2058      if (lscp_client_call(pClient, "GET MIDI_INSTRUMENTS\r\n", 0) == LSCP_OK)          if (iMidiMap < 0)
2059          iInstruments = atoi(lscp_client_get_result(pClient));                  strcat(szQuery, "ALL");
2060            else
2061                    sprintf(szQuery + strlen(szQuery), "%d", iMidiMap);
2062    
2063      // Unlock this section down.          strcat(szQuery, "\r\n");
     lscp_mutex_unlock(pClient->mutex);  
2064    
2065      return iInstruments;          if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
2066                    iInstruments = atoi(lscp_client_get_result(pClient));
2067    
2068            // Unlock this section down.
2069            lscp_mutex_unlock(pClient->mutex);
2070    
2071            return iInstruments;
2072  }  }
2073    
2074    
2075  /**  /**
2076   *  Getting indeces of all MIDI instrument map entries:   *  Getting indeces of all MIDI instrument map entries:
2077   *  LIST MIDI_INSTRUMENTS   *  LIST MIDI_INSTRUMENTS ALL|<midi-map>
2078   *   *
2079   *  @param pClient  Pointer to client instance structure.   *  @param pClient  Pointer to client instance structure.
2080     *  @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL .
2081   *   *
2082   *  @returns An array of @ref lscp_midi_instrument_t, terminated with the   *  @returns An array of @ref lscp_midi_instrument_t, terminated with the
2083   *  {-1,-1,-1} triplet, NULL otherwise.   *  {-1,-1,-1} triplet, NULL otherwise.
2084   */   */
2085  lscp_midi_instrument_t *lscp_list_midi_instruments ( lscp_client_t *pClient )  lscp_midi_instrument_t *lscp_list_midi_instruments ( lscp_client_t *pClient, int iMidiMap )
2086  {  {
2087      if (pClient == NULL)          char szQuery[LSCP_BUFSIZ];
2088          return NULL;  
2089            if (pClient == NULL)
2090                    return NULL;
2091    
2092            // Lock this section up.
2093            lscp_mutex_lock(pClient->mutex);
2094    
2095      // Lock this section up.          if (pClient->midi_instruments) {
2096      lscp_mutex_lock(pClient->mutex);                  lscp_midi_instruments_destroy(pClient->midi_instruments);
2097                    pClient->midi_instruments = NULL;
2098            }
2099    
2100      if (pClient->midi_instruments) {          strcpy(szQuery, "LIST MIDI_INSTRUMENTS ");
         lscp_midi_instruments_destroy(pClient->midi_instruments);  
         pClient->midi_instruments = NULL;  
     }  
2101    
2102      if (lscp_client_call(pClient, "LIST MIDI_INSTRUMENTS\r\n", 0) == LSCP_OK)          if (iMidiMap < 0)
2103          pClient->midi_instruments = lscp_midi_instruments_create(lscp_client_get_result(pClient));                  strcat(szQuery, "ALL");
2104            else
2105                    sprintf(szQuery + strlen(szQuery), "%d", iMidiMap);
2106    
2107      // Unlock this section down.          strcat(szQuery, "\r\n");
     lscp_mutex_unlock(pClient->mutex);  
2108    
2109      return pClient->midi_instruments;          if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
2110                    pClient->midi_instruments = lscp_midi_instruments_create(lscp_client_get_result(pClient));
2111    
2112            // Unlock this section down.
2113            lscp_mutex_unlock(pClient->mutex);
2114    
2115            return pClient->midi_instruments;
2116  }  }
2117    
2118    
2119  /**  /**
2120   *  Getting information about a MIDI instrument map entry:   *  Getting information about a MIDI instrument map entry:
2121   *  GET MIDI_INSTRUMENT INFO <midi-bank-msb> <midi-bank-lsb> <midi-prog>   *  GET MIDI_INSTRUMENT INFO <midi-map> <midi-bank> <midi-prog>
2122   *   *
2123   *  @param pClient      Pointer to client instance structure.   *  @param pClient      Pointer to client instance structure.
2124   *  @param pMidiInstr   MIDI instrument bank and program parameter key.   *  @param pMidiInstr   MIDI instrument bank and program parameter key.
# Line 1809  lscp_midi_instrument_t *lscp_list_midi_i Line 2129  lscp_midi_instrument_t *lscp_list_midi_i
2129   */   */
2130  lscp_midi_instrument_info_t *lscp_get_midi_instrument_info ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr )  lscp_midi_instrument_info_t *lscp_get_midi_instrument_info ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr )
2131  {  {
2132      lscp_midi_instrument_info_t *pInstrInfo;          lscp_midi_instrument_info_t *pInstrInfo;
2133      char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
2134      const char *pszResult;          const char *pszResult;
2135      const char *pszSeps = ":";          const char *pszSeps = ":";
2136      const char *pszCrlf = "\r\n";          const char *pszCrlf = "\r\n";
2137      char *pszToken;          char *pszToken;
2138      char *pch;          char *pch;
2139        
2140      if (pMidiInstr->bank_msb < 0 || pMidiInstr->bank_msb > 127)          if (pClient == NULL)
2141          return NULL;                  return NULL;
2142      if (pMidiInstr->bank_lsb < 0 || pMidiInstr->bank_lsb > 127)          if (pMidiInstr->map < 0)
2143          return NULL;                  return NULL;
2144      if (pMidiInstr->program < 0 || pMidiInstr->program > 127)          if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383)
2145          return NULL;                  return NULL;
2146                if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127)
2147      // Lock this section up.                  return NULL;
2148      lscp_mutex_lock(pClient->mutex);  
2149                // Lock this section up.
2150      pInstrInfo = &(pClient->midi_instrument_info);          lscp_mutex_lock(pClient->mutex);
2151      lscp_midi_instrument_info_reset(pInstrInfo);          
2152                pInstrInfo = &(pClient->midi_instrument_info);
2153      sprintf(szQuery, "GET MIDI_INSTRUMENT INFO %d %d %d\r\n",          lscp_midi_instrument_info_reset(pInstrInfo);
2154          pMidiInstr->bank_msb, pMidiInstr->bank_lsb, pMidiInstr->program);  
2155      if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {          sprintf(szQuery, "GET MIDI_INSTRUMENT INFO %d %d %d\r\n",
2156          pszResult = lscp_client_get_result(pClient);                  pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog);
2157          pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));          if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {
2158          while (pszToken) {                  pszResult = lscp_client_get_result(pClient);
2159              if (strcasecmp(pszToken, "NAME") == 0) {                  pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
2160                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  while (pszToken) {
2161                  if (pszToken)                          if (strcasecmp(pszToken, "NAME") == 0) {
2162                      lscp_unquote_dup(&(pInstrInfo->name), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2163              }                                  if (pszToken)
2164              else if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {                                          lscp_unquote_dup(&(pInstrInfo->name), &pszToken);
2165                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
2166                  if (pszToken)                          else if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {
2167                      lscp_unquote_dup(&(pInstrInfo->engine_name), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2168              }                                  if (pszToken)
2169              else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {                                          lscp_unquote_dup(&(pInstrInfo->engine_name), &pszToken);
2170                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
2171                  if (pszToken)                          else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {
2172                      lscp_unquote_dup(&(pInstrInfo->instrument_file), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2173              }                                  if (pszToken)
2174              else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {                                          lscp_unquote_dup(&(pInstrInfo->instrument_file), &pszToken);
2175                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
2176                  if (pszToken)                          else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {
2177                      pInstrInfo->instrument_nr = atoi(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2178              }                                  if (pszToken)
2179              else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) {                                          pInstrInfo->instrument_nr = atoi(lscp_ltrim(pszToken));
2180                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
2181                  if (pszToken)                          else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) {
2182                      lscp_unquote_dup(&(pInstrInfo->instrument_name), &pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2183              }                                  if (pszToken)
2184              else if (strcasecmp(pszToken, "LOAD_MODE") == 0) {                                          lscp_unquote_dup(&(pInstrInfo->instrument_name), &pszToken);
2185                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
2186                  if (pszToken) {                          else if (strcasecmp(pszToken, "LOAD_MODE") == 0) {
2187                      pszToken = lscp_ltrim(pszToken);                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2188                      if (strcasecmp(pszToken, "ON_DEMAND") == 0)                                  if (pszToken) {
2189                          pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND;                                          pszToken = lscp_ltrim(pszToken);
2190                      else                                          if (strcasecmp(pszToken, "ON_DEMAND") == 0)
2191                      if (strcasecmp(pszToken, "ON_DEMAND_HOLD") == 0)                                                  pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND;
2192                          pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND_HOLD;                                          else
2193                      else                                          if (strcasecmp(pszToken, "ON_DEMAND_HOLD") == 0)
2194                      if (strcasecmp(pszToken, "PERSISTENT") == 0)                                                  pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND_HOLD;
2195                          pInstrInfo->load_mode = LSCP_LOAD_PERSISTENT;                                          else
2196                      else                                          if (strcasecmp(pszToken, "PERSISTENT") == 0)
2197                          pInstrInfo->load_mode = LSCP_LOAD_DEFAULT;                                                  pInstrInfo->load_mode = LSCP_LOAD_PERSISTENT;
2198                  }                                          else
2199              }                                                  pInstrInfo->load_mode = LSCP_LOAD_DEFAULT;
2200              else if (strcasecmp(pszToken, "VOLUME") == 0) {                                  }
2201                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                          }
2202                  if (pszToken)                          else if (strcasecmp(pszToken, "VOLUME") == 0) {
2203                      pInstrInfo->volume = (float) atof(lscp_ltrim(pszToken));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2204              }                                  if (pszToken)
2205              pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                          pInstrInfo->volume = (float) atof(lscp_ltrim(pszToken));
2206          }                          }
2207      }                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));
2208      else pInstrInfo = NULL;                  }
2209                }
2210      // Unlock this section down.          else pInstrInfo = NULL;
2211      lscp_mutex_unlock(pClient->mutex);  
2212                // Unlock this section down.
2213      return pInstrInfo;          lscp_mutex_unlock(pClient->mutex);
2214    
2215            return pInstrInfo;
2216  }  }
2217    
2218    
2219  /**  /**
2220   *  Clear the MIDI instrumnet map:   *  Clear the MIDI instrumnet map:
2221   *  CLEAR MIDI_INSTRUMENTS   *  CLEAR MIDI_INSTRUMENTS ALL|<midi-map>
2222   *   *
2223   *  @param pClient          Pointer to client instance structure.   *  @param pClient  Pointer to client instance structure.
2224   *  @param iSamplerChannel  Sampler channel number.   *  @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL .
2225   *   *
2226   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
2227   */   */
2228  lscp_status_t lscp_clear_midi_instruments  ( lscp_client_t *pClient )  lscp_status_t lscp_clear_midi_instruments  ( lscp_client_t *pClient, int iMidiMap )
2229  {  {
2230      return lscp_client_query(pClient, "CLEAR MIDI_INSTRUMENTS\r\n");          char szQuery[LSCP_BUFSIZ];
2231    
2232            strcpy(szQuery, "CLEAR MIDI_INSTRUMENTS ");
2233    
2234            if (iMidiMap < 0)
2235                    strcat(szQuery, "ALL");
2236            else
2237                    sprintf(szQuery + strlen(szQuery), "%d", iMidiMap);
2238    
2239            strcat(szQuery, "\r\n");
2240    
2241            return lscp_client_query(pClient, szQuery);
2242  }  }
2243    
2244    

Legend:
Removed from v.948  
changed lines
  Added in v.977

  ViewVC Help
Powered by ViewVC