/[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 107 by capela, Fri Jun 4 21:06:59 2004 UTC revision 163 by capela, Wed Jun 30 15:16:03 2004 UTC
# Line 28  Line 28 
28    
29  // Local prototypes.  // Local prototypes.
30    
31  static void _lscp_client_set_result (lscp_client_t *pClient, char *pszResult, int iErrno);  static void             _lscp_client_evt_proc       (void *pvClient);
 static void _lscp_client_udp_proc   (void *pvClient);  
32    
33    static lscp_status_t    _lscp_client_evt_connect    (lscp_client_t *pClient);
34  //-------------------------------------------------------------------------  static lscp_status_t    _lscp_client_evt_request    (lscp_client_t *pClient, int iSubscribe, lscp_event_t event);
 // Helper functions.  
   
 // Result buffer internal settler.  
 static void _lscp_client_set_result ( lscp_client_t *pClient, char *pszResult, int iErrno )  
 {  
     if (pClient->pszResult)  
         free(pClient->pszResult);  
     pClient->pszResult = NULL;  
   
     pClient->iErrno = iErrno;  
   
     if (pszResult)  
         pClient->pszResult = strdup(lscp_ltrim(pszResult));  
 }  
35    
36    
37  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
38  // UDP service (datagram oriented).  // Event service (datagram oriented).
39    
40  static void _lscp_client_udp_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;
     struct sockaddr_in addr;  
     int   cAddr;  
43      char  achBuffer[LSCP_BUFSIZ];      char  achBuffer[LSCP_BUFSIZ];
44      int   cchBuffer;      int   cchBuffer;
45      const char *pszSeps = " \r\n";      const char *pszSeps = ":\r\n";
46      char *pszToken;      char *pszToken;
47      char *pch;      char *pch;
48        int   cchToken;
49        lscp_event_t event;
50    
51  #ifdef DEBUG  #ifdef DEBUG
52      fprintf(stderr, "_lscp_client_udp_proc: Client waiting for events.\n");      fprintf(stderr, "_lscp_client_evt_proc: Client waiting for events.\n");
53  #endif  #endif
54    
55      while (pClient->udp.iState) {      while (pClient->evt.iState) {
56          cAddr = sizeof(struct sockaddr_in);          // Wait for event...
57          cchBuffer = recvfrom(pClient->udp.sock, achBuffer, sizeof(achBuffer), 0, (struct sockaddr *) &addr, &cAddr);          cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0);
58          if (cchBuffer > 0) {          if (cchBuffer > 0) {
59  #ifdef DEBUG              // Make sure received buffer it's null terminated.
60              lscp_socket_trace("_lscp_client_udp_proc: recvfrom", &addr, achBuffer, cchBuffer);              achBuffer[cchBuffer] = (char) 0;
61  #endif              // Parse for the notification event message...
62              if (strncasecmp(achBuffer, "PING ", 5) == 0) {              pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".
63                  // Make sure received buffer it's null terminated.              if (strcasecmp(pszToken, "NOTIFY") == 0) {
64                  achBuffer[cchBuffer] = (char) 0;                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));
65                  lscp_strtok(achBuffer, pszSeps, &(pch));       // Skip "PING"                  event    = lscp_event_from_text(pszToken);
66                  lscp_strtok(NULL, pszSeps, &(pch));            // Skip port (must be the same as in addr)                  // And pick the rest of data...
67                  pszToken = lscp_strtok(NULL, pszSeps, &(pch)); // Have session-id.                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));
68                  if (pszToken) {                  cchToken = (pszToken == NULL ? 0 : strlen(pszToken));
69                      // Set now client's session-id, if not already                  // Double-check if we're really up to it...
70                      if (pClient->sessid == NULL)                  if (pClient->events & event) {
71                          pClient->sessid = strdup(pszToken);                      // Invoke the client event callback...
72                      if (pClient->sessid && strcmp(pszToken, pClient->sessid) == 0) {                      if ((*pClient->pfnCallback)(
73                          sprintf(achBuffer, "PONG %s\r\n", pClient->sessid);                              pClient,
74                          cchBuffer = strlen(achBuffer);                              event,
75                          if (sendto(pClient->udp.sock, achBuffer, cchBuffer, 0, (struct sockaddr *) &addr, cAddr) < cchBuffer)                              pszToken,
76                              lscp_socket_perror("_lscp_client_udp_proc: sendto");                              cchToken,
77  #ifdef DEBUG                              pClient->pvData) != LSCP_OK) {
78                          fprintf(stderr, "> %s", achBuffer);                          pClient->evt.iState = 0;
 #endif  
79                      }                      }
80                  }                  }
                 // Done with life proof.  
             } else {  
                 //  
                 if ((*pClient->pfnCallback)(  
                         pClient,  
                         achBuffer,  
                         cchBuffer,  
                         pClient->pvData) != LSCP_OK) {  
                     pClient->udp.iState = 0;  
                 }  
81              }              }
82          } else {          } else {
83              lscp_socket_perror("_lscp_client_udp_proc: recvfrom");              lscp_socket_perror("_lscp_client_evt_proc: recv");
84              pClient->udp.iState = 0;              pClient->evt.iState = 0;
85          }          }
86      }      }
87    
88  #ifdef DEBUG  #ifdef DEBUG
89      fprintf(stderr, "_lscp_client_udp_proc: Client closing.\n");      fprintf(stderr, "_lscp_client_evt_proc: Client closing.\n");
90    #endif
91    }
92    
93    
94    //-------------------------------------------------------------------------
95    // Event subscription helpers.
96    
97    // Open the event service socket connection.
98    static lscp_status_t _lscp_client_evt_connect ( lscp_client_t *pClient )
99    {
100        lscp_socket_t sock;
101        struct sockaddr_in addr;
102        int cAddr;
103    #if defined(WIN32)
104        int iSockOpt = (-1);
105    #endif
106    
107        // Prepare the event connection socket...
108        sock = socket(AF_INET, SOCK_STREAM, 0);
109        if (sock == INVALID_SOCKET) {
110            lscp_socket_perror("_lscp_client_evt_connect: socket");
111            return LSCP_FAILED;
112        }
113    
114    #if defined(WIN32)
115        if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR)
116            lscp_socket_perror("lscp_client_evt_connect: setsockopt(SO_DONTLINGER)");
117    #endif
118    
119    #ifdef DEBUG
120        lscp_socket_getopts("_lscp_client_evt_connect:", sock);
121  #endif  #endif
122    
123        // Use same address of the command connection.
124        cAddr = sizeof(struct sockaddr_in);
125        memmove((char *) &addr, &(pClient->cmd.addr), cAddr);
126    
127        // Start the connection...
128        if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {
129            lscp_socket_perror("_lscp_client_evt_connect: connect");
130            closesocket(sock);
131            return LSCP_FAILED;
132        }
133        
134        // Set our socket agent struct...
135        lscp_socket_agent_init(&(pClient->evt), sock, &addr, cAddr);
136        
137        // And finally the service thread...
138        return lscp_socket_agent_start(&(pClient->evt), _lscp_client_evt_proc, pClient, 0);
139    }
140    
141    
142    // Subscribe to a single event.
143    static lscp_status_t _lscp_client_evt_request ( lscp_client_t *pClient, int iSubscribe, lscp_event_t event )
144    {
145        const char *pszEvent;
146        char  szQuery[LSCP_BUFSIZ];
147        int   cchQuery;
148    
149        if (pClient == NULL)
150            return LSCP_FAILED;
151    
152        // Which (single) event?
153        pszEvent = lscp_event_to_text(event);
154        if (pszEvent == NULL)
155            return LSCP_FAILED;
156    
157        // Build the query string...
158        cchQuery = sprintf(szQuery, "%sSUBSCRIBE %s\n\n", (iSubscribe == 0 ? "UN" : ""), pszEvent);
159        // Just send data, forget result...
160        if (send(pClient->evt.sock, szQuery, cchQuery, 0) < cchQuery) {
161            lscp_socket_perror("_lscp_client_evt_request: send");
162            return LSCP_FAILED;
163        }
164    
165        // Update as naively as we can...
166        if (iSubscribe)
167            pClient->events |=  event;
168        else
169            pClient->events &= ~event;
170    
171        return LSCP_OK;
172  }  }
173    
174    
# Line 155  lscp_client_t* lscp_client_create ( cons Line 210  lscp_client_t* lscp_client_create ( cons
210      lscp_socket_t sock;      lscp_socket_t sock;
211      struct sockaddr_in addr;      struct sockaddr_in addr;
212      int cAddr;      int cAddr;
213    #if defined(WIN32)
214      int iSockOpt = (-1);      int iSockOpt = (-1);
215    #endif
216    
217      if (pfnCallback == NULL) {      if (pfnCallback == NULL) {
218          fprintf(stderr, "lscp_client_create: Invalid client callback function.\n");          fprintf(stderr, "lscp_client_create: Invalid client callback function.\n");
# Line 164  lscp_client_t* lscp_client_create ( cons Line 221  lscp_client_t* lscp_client_create ( cons
221    
222      pHost = gethostbyname(pszHost);      pHost = gethostbyname(pszHost);
223      if (pHost == NULL) {      if (pHost == NULL) {
224          lscp_socket_perror("lscp_client_create: gethostbyname");          lscp_socket_herror("lscp_client_create: gethostbyname");
225          return NULL;          return NULL;
226      }      }
227    
# Line 184  lscp_client_t* lscp_client_create ( cons Line 241  lscp_client_t* lscp_client_create ( cons
241      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);
242  #endif  #endif
243    
244      // Prepare the TCP connection socket...      // Prepare the command connection socket...
245    
246      sock = socket(AF_INET, SOCK_STREAM, 0);      sock = socket(AF_INET, SOCK_STREAM, 0);
247      if (sock == INVALID_SOCKET) {      if (sock == INVALID_SOCKET) {
248          lscp_socket_perror("lscp_client_create: tcp: socket");          lscp_socket_perror("lscp_client_create: cmd: socket");
249          free(pClient);          free(pClient);
250          return NULL;          return NULL;
251      }      }
252    
253  #if defined(WIN32)  #if defined(WIN32)
254      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)
255          lscp_socket_perror("lscp_client_create: tcp: setsockopt(SO_DONTLINGER)");          lscp_socket_perror("lscp_client_create: cmd: setsockopt(SO_DONTLINGER)");
256  #endif  #endif
257    
258  #ifdef DEBUG  #ifdef DEBUG
259      lscp_socket_getopts("lscp_client_create: tcp", sock);      lscp_socket_getopts("lscp_client_create: cmd", sock);
260  #endif  #endif
261    
262      cAddr = sizeof(struct sockaddr_in);      cAddr = sizeof(struct sockaddr_in);
# Line 209  lscp_client_t* lscp_client_create ( cons Line 266  lscp_client_t* lscp_client_create ( cons
266      addr.sin_port = htons((short) iPort);      addr.sin_port = htons((short) iPort);
267    
268      if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {      if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {
269          lscp_socket_perror("lscp_client_create: tcp: connect");          lscp_socket_perror("lscp_client_create: cmd: connect");
270          closesocket(sock);          closesocket(sock);
271          free(pClient);          free(pClient);
272          return NULL;          return NULL;
273      }      }
274    
275      lscp_socket_agent_init(&(pClient->tcp), sock, &addr, cAddr);      // Initialize the command socket agent struct...
276        lscp_socket_agent_init(&(pClient->cmd), sock, &addr, cAddr);
277    
278  #ifdef DEBUG  #ifdef DEBUG
279      fprintf(stderr, "lscp_client_create: tcp: pClient=%p: sock=%d addr=%s port=%d.\n", pClient, pClient->tcp.sock, inet_ntoa(pClient->tcp.addr.sin_addr), ntohs(pClient->tcp.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));
280  #endif  #endif
281    
282      // Prepare the UDP datagram service socket...      // Initialize the event service socket struct...
283        lscp_socket_agent_init(&(pClient->evt), INVALID_SOCKET, NULL, 0);
284      sock = socket(AF_INET, SOCK_DGRAM, 0);      // No events subscribed, yet.
285      if (sock == INVALID_SOCKET) {      pClient->events = LSCP_EVENT_NONE;
         lscp_socket_perror("lscp_client_create: udp: socket");  
         lscp_socket_agent_free(&(pClient->tcp));  
         free(pClient);  
         return NULL;  
     }  
   
     if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR)  
         lscp_socket_perror("lscp_client_create: udp: setsockopt(SO_REUSEADDR)");  
   
 #ifdef DEBUG  
     lscp_socket_getopts("lscp_client_create: udp", sock);  
 #endif  
   
     cAddr = sizeof(struct sockaddr_in);  
     memset((char *) &addr, 0, cAddr);  
     addr.sin_family = AF_INET;  
     addr.sin_addr.s_addr = htonl(INADDR_ANY);  
     addr.sin_port = htons(0);  
   
     if (bind(sock, (const struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {  
         lscp_socket_perror("lscp_client_create: udp: bind");  
         lscp_socket_agent_free(&(pClient->tcp));  
         closesocket(sock);  
         free(pClient);  
         return NULL;  
     }  
   
     if (getsockname(sock, (struct sockaddr *) &addr, &cAddr) == SOCKET_ERROR) {  
         lscp_socket_perror("lscp_client_create: udp: getsockname");  
         lscp_socket_agent_free(&(pClient->tcp));  
         closesocket(sock);  
         free(pClient);  
         return NULL;  
     }  
   
     lscp_socket_agent_init(&(pClient->udp), sock, &addr, cAddr);  
   
 #ifdef DEBUG  
     fprintf(stderr, "lscp_client_create: udp: pClient=%p: sock=%d addr=%s port=%d.\n", pClient, pClient->udp.sock, inet_ntoa(pClient->udp.addr.sin_addr), ntohs(pClient->udp.addr.sin_port));  
 #endif  
   
     // No session id, yet.  
     pClient->sessid = NULL;  
286      // Initialize cached members.      // Initialize cached members.
287      pClient->audio_drivers = NULL;      pClient->audio_drivers = NULL;
288      pClient->midi_drivers = NULL;      pClient->midi_drivers = NULL;
289        pClient->audio_devices = NULL;
290        pClient->midi_devices = NULL;
291      pClient->engines = NULL;      pClient->engines = NULL;
292        pClient->channels = NULL;
293      lscp_driver_info_init(&(pClient->audio_info));      lscp_driver_info_init(&(pClient->audio_info));
294      lscp_driver_info_init(&(pClient->midi_info));      lscp_driver_info_init(&(pClient->midi_info));
295        lscp_param_info_init(&(pClient->audio_param_info));
296        lscp_param_info_init(&(pClient->midi_param_info));
297      lscp_engine_info_init(&(pClient->engine_info));      lscp_engine_info_init(&(pClient->engine_info));
298      lscp_channel_info_init(&(pClient->channel_info));      lscp_channel_info_init(&(pClient->channel_info));
299      // Initialize error stuff.      // Initialize error stuff.
# Line 288  lscp_client_t* lscp_client_create ( cons Line 308  lscp_client_t* lscp_client_create ( cons
308      // Initialize the transaction mutex.      // Initialize the transaction mutex.
309      lscp_mutex_init(pClient->mutex);      lscp_mutex_init(pClient->mutex);
310    
     // Now's finally time to startup threads...  
     // UDP service thread...  
     if (lscp_socket_agent_start(&(pClient->udp), _lscp_client_udp_proc, pClient, 0) != LSCP_OK) {  
         lscp_socket_agent_free(&(pClient->tcp));  
         lscp_socket_agent_free(&(pClient->udp));  
         lscp_mutex_destroy(pClient->mutex);  
         free(pClient);  
         return NULL;  
     }  
   
311      // Finally we've some success...      // Finally we've some success...
312      return pClient;      return pClient;
313  }  }
# Line 317  lscp_status_t lscp_client_join ( lscp_cl Line 327  lscp_status_t lscp_client_join ( lscp_cl
327      fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient);      fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient);
328  #endif  #endif
329    
330  //  lscp_socket_agent_join(&(pClient->udp));  //  lscp_socket_agent_join(&(pClient->evt));
331      lscp_socket_agent_join(&(pClient->tcp));      lscp_socket_agent_join(&(pClient->cmd));
332    
333      return LSCP_OK;      return LSCP_OK;
334  }  }
# Line 340  lscp_status_t lscp_client_destroy ( lscp Line 350  lscp_status_t lscp_client_destroy ( lscp
350      fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient);      fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient);
351  #endif  #endif
352    
353      // Free session-id, if any.      // Lock this section up.
354      if (pClient->sessid)      lscp_mutex_lock(pClient->mutex);
355          free(pClient->sessid);      
     pClient->sessid = NULL;  
356      // Free up all cached members.      // Free up all cached members.
357      lscp_channel_info_reset(&(pClient->channel_info));      lscp_channel_info_reset(&(pClient->channel_info));
358      lscp_engine_info_reset(&(pClient->engine_info));      lscp_engine_info_reset(&(pClient->engine_info));
359        lscp_param_info_reset(&(pClient->midi_param_info));
360        lscp_param_info_reset(&(pClient->audio_param_info));
361      lscp_driver_info_reset(&(pClient->midi_info));      lscp_driver_info_reset(&(pClient->midi_info));
362      lscp_driver_info_reset(&(pClient->audio_info));      lscp_driver_info_reset(&(pClient->audio_info));
363      // Free available engine table.      // Free available engine table.
364      lscp_szsplit_destroy(pClient->audio_drivers);      lscp_szsplit_destroy(pClient->audio_drivers);
365      lscp_szsplit_destroy(pClient->midi_drivers);      lscp_szsplit_destroy(pClient->midi_drivers);
366        lscp_isplit_destroy(pClient->audio_devices);
367        lscp_isplit_destroy(pClient->midi_devices);
368      lscp_szsplit_destroy(pClient->engines);      lscp_szsplit_destroy(pClient->engines);
369        lscp_isplit_destroy(pClient->channels);
370      // Make them null.      // Make them null.
371      pClient->audio_drivers = NULL;      pClient->audio_drivers = NULL;
372      pClient->midi_drivers = NULL;      pClient->midi_drivers = NULL;
373      pClient->engines = NULL;      pClient->engines = NULL;
374      // Free result error stuff.      // Free result error stuff.
375      _lscp_client_set_result(pClient, NULL, 0);      lscp_client_set_result(pClient, NULL, 0);
376      // Frre stream usage stuff.      // Frre stream usage stuff.
377      if (pClient->buffer_fill)      if (pClient->buffer_fill)
378          free(pClient->buffer_fill);          free(pClient->buffer_fill);
# Line 367  lscp_status_t lscp_client_destroy ( lscp Line 381  lscp_status_t lscp_client_destroy ( lscp
381      pClient->iTimeout = 0;      pClient->iTimeout = 0;
382    
383      // Free socket agents.      // Free socket agents.
384      lscp_socket_agent_free(&(pClient->udp));      lscp_socket_agent_free(&(pClient->evt));
385      lscp_socket_agent_free(&(pClient->tcp));      lscp_socket_agent_free(&(pClient->cmd));
386    
387      // Last but not least, free good ol'transaction mutex.      // Last but not least, free good ol'transaction mutex.
388        lscp_mutex_unlock(pClient->mutex);
389      lscp_mutex_destroy(pClient->mutex);      lscp_mutex_destroy(pClient->mutex);
390    
391      free(pClient);      free(pClient);
# Line 418  int lscp_client_get_timeout ( lscp_clien Line 433  int lscp_client_get_timeout ( lscp_clien
433  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
434  // Client common protocol functions.  // Client common protocol functions.
435    
   
436  /**  /**
437   *  Submit a command query line string to the server. The query string   *  Submit a command query line string to the server. The query string
438   *  must be cr/lf and null terminated. Besides the return code, the   *  must be cr/lf and null terminated. Besides the return code, the
# Line 434  int lscp_client_get_timeout ( lscp_clien Line 448  int lscp_client_get_timeout ( lscp_clien
448   */   */
449  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 )
450  {  {
451      fd_set fds;                         // File descriptor list for select().      lscp_status_t ret;
452      int    fd, fdmax;                   // Maximum file descriptor number.      
     struct timeval tv;                  // For specifying a timeout value.  
     int    iSelect;                     // Holds select return status.  
     int    iTimeout;  
     int    cchQuery;  
     char   achResult[LSCP_BUFSIZ];  
     int    cchResult;  
     const  char *pszSeps = ":[]";  
     char  *pszResult;  
     char  *pszToken;  
     char  *pch;  
     int    iErrno;  
   
     lscp_status_t ret = LSCP_FAILED;  
   
     if (pClient == NULL)  
         return ret;  
   
453      // Lock this section up.      // Lock this section up.
454      lscp_mutex_lock(pClient->mutex);      lscp_mutex_lock(pClient->mutex);
455    
456      pszResult = NULL;      // Just make the now guarded call.
457      iErrno = -1;      ret = lscp_client_call(pClient, pszQuery);
458        
459      // Send data, and then, wait for the result...      // Unlock this section down.
     cchQuery = strlen(pszQuery);  
     if (send(pClient->tcp.sock, pszQuery, cchQuery, 0) < cchQuery) {  
         lscp_socket_perror("lscp_client_query: send");  
         lscp_mutex_unlock(pClient->mutex);  
         return ret;  
     }  
   
     // Prepare for waiting on select...  
     fd = (int) pClient->tcp.sock;  
     FD_ZERO(&fds);  
     FD_SET((unsigned int) fd, &fds);  
     fdmax = fd;  
   
     // Use the timeout select feature...  
     iTimeout = pClient->iTimeout;  
     if (iTimeout > 1000) {  
         tv.tv_sec = iTimeout / 1000;  
         iTimeout -= tv.tv_sec * 1000;  
     }  
     else tv.tv_sec = 0;  
     tv.tv_usec = iTimeout * 1000;  
   
     // Wait for event...  
     iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv);  
     if (iSelect > 0 && FD_ISSET(fd, &fds)) {  
         // May recv now...  
         cchResult = recv(pClient->tcp.sock, achResult, sizeof(achResult), 0);  
         if (cchResult > 0) {  
             // Assume early success.  
             ret = LSCP_OK;  
             // Always force the result to be null terminated (and trim trailing CRLFs)!  
             while (cchResult > 0 && (achResult[cchResult - 1] == '\n' || achResult[cchResult- 1] == '\r'))  
                 cchResult--;  
             achResult[cchResult] = (char) 0;  
             // Check if the response it's an error or warning message.  
             if (strncasecmp(achResult, "WRN:", 4) == 0)  
                 ret = LSCP_WARNING;  
             else if (strncasecmp(achResult, "ERR:", 4) == 0)  
                 ret = LSCP_ERROR;  
             // So we got a result...  
             if (ret == LSCP_OK) {  
                 // Reset errno in case of success.  
                 iErrno = 0;  
                 // Is it a special successful response?  
                 if (strncasecmp(achResult, "OK[", 3) == 0) {  
                     // Parse the OK message, get the return string under brackets...  
                     pszToken = lscp_strtok(achResult, pszSeps, &(pch));  
                     if (pszToken)  
                         pszResult = lscp_strtok(NULL, pszSeps, &(pch));  
                 }  
                 else pszResult = achResult;  
                 // The result string is now set to the command response, if any.  
             } else {  
                 // Parse the error/warning message, skip first colon...  
                 pszToken = lscp_strtok(achResult, pszSeps, &(pch));  
                 if (pszToken) {  
                     // Get the error number...  
                     pszToken = lscp_strtok(NULL, pszSeps, &(pch));  
                     if (pszToken) {  
                         iErrno = atoi(pszToken);  
                         // And make the message text our final result.  
                         pszResult = lscp_strtok(NULL, pszSeps, &(pch));  
                     }  
                 }  
                 // The result string is set to the error/warning message text.  
             }  
         }  
         else if (cchResult == 0) {  
             // Fake a result message.  
             pszResult = "Server terminated the connection";  
             ret = LSCP_QUIT;  
         }  
         else lscp_socket_perror("lscp_client_query: recv");  
     }   // Check if select has timed out.  
     else if (iSelect == 0) {  
         // Fake a result message.  
         pszResult = "Timeout during receive operation";  
         ret = LSCP_TIMEOUT;  
     }  
     else lscp_socket_perror("lscp_client_query: select");  
   
     // Make the result official...  
     _lscp_client_set_result(pClient, pszResult, iErrno);  
   
     // Can go on with it...  
460      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
461        
462      return ret;      return ret;
463  }  }
464    
   
465  /**  /**
466   *  Get the last received result string. In case of error or warning,   *  Get the last received result string. In case of error or warning,
467   *  this is the text of the error or warning message issued.   *  this is the text of the error or warning message issued.
# Line 590  int lscp_client_get_errno ( lscp_client_ Line 501  int lscp_client_get_errno ( lscp_client_
501  // Client registration protocol functions.  // Client registration protocol functions.
502    
503  /**  /**
504   *  Register frontend for receiving UDP event messages:   *  Register frontend for receiving event messages:
505   *  SUBSCRIBE NOTIFICATION <udp-port>   *  SUBSCRIBE CHANNELS | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL
506     *      | CHANNEL_INFO | MISCELLANEOUS
507   *   *
508   *  @param pClient  Pointer to client instance structure.   *  @param pClient  Pointer to client instance structure.
509     *  @param events   Bit-wise OR'ed event flags to subscribe.
510   *   *
511   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
512   */   */
513  lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient )  lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient, lscp_event_t events )
514  {  {
515      lscp_status_t ret;      lscp_status_t ret = LSCP_FAILED;
     char szQuery[LSCP_BUFSIZ];  
     const char *pszResult;  
     const char *pszSeps = "[]";  
     char *pszToken;  
     char *pch;  
516    
517      if (pClient == NULL || pClient->sessid)      if (pClient == NULL)
518          return LSCP_FAILED;          return LSCP_FAILED;
519    
520      sprintf(szQuery, "SUBSCRIBE NOTIFICATION %d\r\n", ntohs(pClient->udp.addr.sin_port));      // Lock this section up.
521      ret = lscp_client_query(pClient, szQuery);      lscp_mutex_lock(pClient->mutex);
522      if (ret == LSCP_OK) {  
523          pszResult = lscp_client_get_result(pClient);      // If applicable, start the alternate connection...
524  #ifdef DEBUG      if (pClient->events == LSCP_EVENT_NONE)
525          fprintf(stderr, "lscp_client_subscribe: %s\n", pszResult);          ret = _lscp_client_evt_connect(pClient);
526  #endif      
527          // Check for the session-id on "OK[sessid]" response.      // Send the subscription commands.
528          pszToken = lscp_strtok(pszResult, pszSeps, &(pch));      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNELS))
529          if (pszToken && strcasecmp(pszToken, "OK") == 0) {          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNELS);
530              pszToken = lscp_strtok(NULL, pszSeps, &(pch));      if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
531              if (pszToken)          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT);
532                  pClient->sessid = strdup(pszToken);      if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
533          }          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_STREAM_COUNT);
534      }      if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))
535            ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_BUFFER_FILL);
536        if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))
537            ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_INFO);
538        if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))
539            ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_MISCELLANEOUS);
540    
541        // Unlock this section down.
542        lscp_mutex_unlock(pClient->mutex);
543    
544      return ret;      return ret;
545  }  }
546    
547    
548  /**  /**
549   *  Deregister frontend for not receiving UDP event messages anymore:   *  Deregister frontend from receiving UDP event messages anymore:
550   *  UNSUBSCRIBE NOTIFICATION <session-id>   *  SUBSCRIBE CHANNELS | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL
551     *      | CHANNEL_INFO | MISCELLANEOUS
552   *   *
553   *  @param pClient  Pointer to client instance structure.   *  @param pClient  Pointer to client instance structure.
554     *  @param events   Bit-wise OR'ed event flags to unsubscribe.
555   *   *
556   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
557   */   */
558  lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient )  lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient, lscp_event_t events )
559  {  {
560      lscp_status_t ret;      lscp_status_t ret = LSCP_OK;
     char szQuery[LSCP_BUFSIZ];  
561    
562      if (pClient == NULL)      if (pClient == NULL)
563          return LSCP_FAILED;          return LSCP_FAILED;
     if (pClient->sessid == NULL)  
         return LSCP_FAILED;  
564    
565      sprintf(szQuery, "UNSUBSCRIBE NOTIFICATION %s\n\n", pClient->sessid);      // Lock this section up.
566      ret = lscp_client_query(pClient, szQuery);      lscp_mutex_lock(pClient->mutex);
567      if (ret == LSCP_OK) {  
568  #ifdef DEBUG      // Send the unsubscription commands.
569          fprintf(stderr, "lscp_client_unsubscribe: %s\n", lscp_client_get_result(pClient));      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNELS))
570  #endif          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNELS);
571          // Bail out session-id string.      if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
572          free(pClient->sessid);          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT);
573          pClient->sessid = NULL;      if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
574      }          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_STREAM_COUNT);
575        if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))
576            ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_BUFFER_FILL);
577        if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))
578            ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_INFO);
579        if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))
580            ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_MISCELLANEOUS);
581    
582        // If necessary, close the alternate connection...
583        if (pClient->events == LSCP_EVENT_NONE)
584            lscp_socket_agent_free(&(pClient->evt));
585    
586        // Unlock this section down.
587        lscp_mutex_unlock(pClient->mutex);
588    
589      return ret;      return ret;
590  }  }
# Line 683  lscp_status_t lscp_load_instrument ( lsc Line 611  lscp_status_t lscp_load_instrument ( lsc
611      if (pszFileName == NULL || iSamplerChannel < 0)      if (pszFileName == NULL || iSamplerChannel < 0)
612          return LSCP_FAILED;          return LSCP_FAILED;
613    
614      sprintf(szQuery, "LOAD INSTRUMENT %s %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);      sprintf(szQuery, "LOAD INSTRUMENT '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);
615        return lscp_client_query(pClient, szQuery);
616    }
617    
618    
619    /**
620     *  Loading an instrument in the background (non modal):
621     *  LOAD INSTRUMENT NON_MODAL <filename> <instr-index> <sampler-channel>
622     *
623     *  @param pClient          Pointer to client instance structure.
624     *  @param pszFileName      Instrument file name.
625     *  @param iInstrIndex      Instrument index number.
626     *  @param iSamplerChannel  Sampler Channel.
627     *
628     *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
629     */
630    lscp_status_t lscp_load_instrument_non_modal ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )
631    {
632        char szQuery[LSCP_BUFSIZ];
633    
634        if (pszFileName == NULL || iSamplerChannel < 0)
635            return LSCP_FAILED;
636    
637        sprintf(szQuery, "LOAD INSTRUMENT NON_MODAL '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);
638      return lscp_client_query(pClient, szQuery);      return lscp_client_query(pClient, szQuery);
639  }  }
640    
# Line 722  lscp_status_t lscp_load_engine ( lscp_cl Line 673  lscp_status_t lscp_load_engine ( lscp_cl
673  int lscp_get_channels ( lscp_client_t *pClient )  int lscp_get_channels ( lscp_client_t *pClient )
674  {  {
675      int iChannels = -1;      int iChannels = -1;
676      if (lscp_client_query(pClient, "GET CHANNELS\r\n") == LSCP_OK)  
677        // Lock this section up.
678        lscp_mutex_lock(pClient->mutex);
679    
680        if (lscp_client_call(pClient, "GET CHANNELS\r\n") == LSCP_OK)
681          iChannels = atoi(lscp_client_get_result(pClient));          iChannels = atoi(lscp_client_get_result(pClient));
682    
683        // Unlock this section doen.
684        lscp_mutex_unlock(pClient->mutex);
685    
686      return iChannels;      return iChannels;
687  }  }
688    
689    
690  /**  /**
691     *  List current sampler channels number identifiers:
692     *  LIST CHANNELS
693     *
694     *  @param pClient  Pointer to client instance structure.
695     *
696     *  @returns An array of the sampler channels identifiers as positive integers,
697     *  terminated with -1 on success, NULL otherwise.
698     */
699    int *lscp_list_channels ( lscp_client_t *pClient )
700    {
701        const char *pszSeps = ",";
702    
703        if (pClient == NULL)
704            return NULL;
705            
706        // Lock this section up.
707        lscp_mutex_lock(pClient->mutex);
708    
709        if (pClient->channels) {
710            lscp_isplit_destroy(pClient->channels);
711            pClient->channels = NULL;
712        }
713    
714        if (lscp_client_call(pClient, "LIST CHANNELS\r\n") == LSCP_OK)
715            pClient->channels = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps);
716    
717        // Unlock this section down.
718        lscp_mutex_unlock(pClient->mutex);
719    
720        return pClient->channels;
721    }
722    
723    
724    /**
725   *  Adding a new sampler channel:   *  Adding a new sampler channel:
726   *  ADD CHANNEL   *  ADD CHANNEL
727   *   *
# Line 740  int lscp_get_channels ( lscp_client_t *p Line 733  int lscp_get_channels ( lscp_client_t *p
733  int lscp_add_channel ( lscp_client_t *pClient )  int lscp_add_channel ( lscp_client_t *pClient )
734  {  {
735      int iSamplerChannel = -1;      int iSamplerChannel = -1;
736      if (lscp_client_query(pClient, "ADD CHANNEL\r\n") == LSCP_OK)  
737        // Lock this section up.
738        lscp_mutex_lock(pClient->mutex);
739    
740        if (lscp_client_call(pClient, "ADD CHANNEL\r\n") == LSCP_OK)
741          iSamplerChannel = atoi(lscp_client_get_result(pClient));          iSamplerChannel = atoi(lscp_client_get_result(pClient));
742            
743        // Unlock this section down.
744        lscp_mutex_unlock(pClient->mutex);
745    
746      return iSamplerChannel;      return iSamplerChannel;
747  }  }
748    
# Line 780  const char **lscp_get_available_engines Line 781  const char **lscp_get_available_engines
781  {  {
782      const char *pszSeps = ",";      const char *pszSeps = ",";
783    
784        // Lock this section up.
785        lscp_mutex_lock(pClient->mutex);
786    
787      if (pClient->engines) {      if (pClient->engines) {
788          lscp_szsplit_destroy(pClient->engines);          lscp_szsplit_destroy(pClient->engines);
789          pClient->engines = NULL;          pClient->engines = NULL;
790      }      }
791    
792      if (lscp_client_query(pClient, "GET AVAILABLE_ENGINES\r\n") == LSCP_OK)      if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n") == LSCP_OK)
793          pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps);          pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps);
794    
795        // Unlock this section down.
796        lscp_mutex_unlock(pClient->mutex);
797    
798      return (const char **) pClient->engines;      return (const char **) pClient->engines;
799  }  }
800    
# Line 815  lscp_engine_info_t *lscp_get_engine_info Line 822  lscp_engine_info_t *lscp_get_engine_info
822      if (pszEngineName == NULL)      if (pszEngineName == NULL)
823          return NULL;          return NULL;
824    
825        // Lock this section up.
826        lscp_mutex_lock(pClient->mutex);
827    
828      pEngineInfo = &(pClient->engine_info);      pEngineInfo = &(pClient->engine_info);
829      lscp_engine_info_reset(pEngineInfo);      lscp_engine_info_reset(pEngineInfo);
830    
831      sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName);      sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName);
832      if (lscp_client_query(pClient, szQuery) != LSCP_OK)      if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
833          return NULL;          pszResult = lscp_client_get_result(pClient);
834            pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
835      pszResult = lscp_client_get_result(pClient);          while (pszToken) {
836      pszToken = lscp_strtok(pszResult, pszSeps, &(pch));              if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
837      while (pszToken) {                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
838          if (strcasecmp(pszToken, "DESCRIPTION") == 0) {                  if (pszToken)
839              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pEngineInfo->description = lscp_unquote(&pszToken, 1);
840              if (pszToken)              }
841                  pEngineInfo->description = lscp_unquote(&pszToken, 1);              else if (strcasecmp(pszToken, "VERSION") == 0) {
842          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
843          else if (strcasecmp(pszToken, "VERSION") == 0) {                  if (pszToken)
844              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pEngineInfo->version = lscp_unquote(&pszToken, 1);
845              if (pszToken)              }
846                  pEngineInfo->version = lscp_unquote(&pszToken, 1);              pszToken = lscp_strtok(NULL, pszSeps, &(pch));
847          }          }
         pszToken = lscp_strtok(NULL, pszSeps, &(pch));  
848      }      }
849        else pEngineInfo = NULL;
850        
851        // Unlock this section down.
852        lscp_mutex_unlock(pClient->mutex);
853    
854      return pEngineInfo;      return pEngineInfo;
855  }  }
# Line 865  lscp_channel_info_t *lscp_get_channel_in Line 878  lscp_channel_info_t *lscp_get_channel_in
878      if (iSamplerChannel < 0)      if (iSamplerChannel < 0)
879          return NULL;          return NULL;
880    
881        // Lock this section up.
882        lscp_mutex_lock(pClient->mutex);
883        
884      pChannelInfo = &(pClient->channel_info);      pChannelInfo = &(pClient->channel_info);
885      lscp_channel_info_reset(pChannelInfo);      lscp_channel_info_reset(pChannelInfo);
886    
887      sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel);      sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel);
888      if (lscp_client_query(pClient, szQuery) != LSCP_OK)      if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
889          return NULL;          pszResult = lscp_client_get_result(pClient);
890            pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
891      pszResult = lscp_client_get_result(pClient);          while (pszToken) {
892      pszToken = lscp_strtok(pszResult, pszSeps, &(pch));              if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {
893      while (pszToken) {                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
894          if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {                  if (pszToken)
895              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->engine_name = lscp_unquote(&pszToken, 1);
896              if (pszToken)              }
897                  pChannelInfo->engine_name = lscp_unquote(&pszToken, 1);              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {
898          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
899          else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {                  if (pszToken)
900              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken));
901              if (pszToken)              }
902                  pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken));              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) {
903          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
904          else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) {                  if (pszToken)
905              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken));
906              if (pszToken)              }
907                  pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken));              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {
908          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
909          else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {                  if (pszToken)
910              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");
911              if (pszToken)              }
912                  pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");              else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {
913          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
914          else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {                  if (pszToken)
915              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->instrument_file = lscp_unquote(&pszToken, 1);
916              if (pszToken)              }
917                  pChannelInfo->instrument_file = lscp_unquote(&pszToken, 1);              else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {
918          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
919          else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {                  if (pszToken)
920              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken));
921              if (pszToken)              }
922                  pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken));              else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) {
923          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
924          else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) {                  if (pszToken)
925              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->instrument_status = atoi(lscp_ltrim(pszToken));
926              if (pszToken)              }
927                  pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken));              else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) {
928          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
929          else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) {                  if (pszToken)
930              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken));
931              if (pszToken)              }
932                  pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken));              else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) {
933          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
934          else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {                  if (pszToken)
935              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken));
936              if (pszToken)              }
937                  pChannelInfo->midi_channel = atoi(lscp_ltrim(pszToken));              else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {
938          }                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
939          else if (strcasecmp(pszToken, "VOLUME") == 0) {                  if (pszToken)
940              pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                      pChannelInfo->midi_channel = atoi(lscp_ltrim(pszToken));
941              if (pszToken)              }
942                  pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken));              else if (strcasecmp(pszToken, "VOLUME") == 0) {
943                    pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
944                    if (pszToken)
945                        pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken));
946                }
947                pszToken = lscp_strtok(NULL, pszSeps, &(pch));
948          }          }
         pszToken = lscp_strtok(NULL, pszSeps, &(pch));  
949      }      }
950        else pChannelInfo = NULL;
951        
952        // Unlock this section up.
953        lscp_mutex_unlock(pClient->mutex);
954    
955      return pChannelInfo;      return pChannelInfo;
956  }  }
# Line 949  int lscp_get_channel_voice_count ( lscp_ Line 973  int lscp_get_channel_voice_count ( lscp_
973      if (iSamplerChannel < 0)      if (iSamplerChannel < 0)
974          return iVoiceCount;          return iVoiceCount;
975    
976        // Lock this section up.
977        lscp_mutex_lock(pClient->mutex);
978    
979      sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel);      sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel);
980      if (lscp_client_query(pClient, szQuery) == LSCP_OK)      if (lscp_client_call(pClient, szQuery) == LSCP_OK)
981          iVoiceCount = atoi(lscp_client_get_result(pClient));          iVoiceCount = atoi(lscp_client_get_result(pClient));
982    
983        // Unlock this section down.
984        lscp_mutex_unlock(pClient->mutex);
985    
986      return iVoiceCount;      return iVoiceCount;
987  }  }
988    
# Line 971  int lscp_get_channel_stream_count ( lscp Line 1001  int lscp_get_channel_stream_count ( lscp
1001      if (iSamplerChannel < 0)      if (iSamplerChannel < 0)
1002          return iStreamCount;          return iStreamCount;
1003    
1004        // Lock this section up.
1005        lscp_mutex_lock(pClient->mutex);
1006    
1007      sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel);      sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel);
1008      if (lscp_client_query(pClient, szQuery) == LSCP_OK)      if (lscp_client_call(pClient, szQuery) == LSCP_OK)
1009          iStreamCount = atoi(lscp_client_get_result(pClient));          iStreamCount = atoi(lscp_client_get_result(pClient));
1010    
1011        // Unlock this section down.
1012        lscp_mutex_unlock(pClient->mutex);
1013    
1014      return iStreamCount;      return iStreamCount;
1015  }  }
1016    
# Line 1005  lscp_buffer_fill_t *lscp_get_channel_buf Line 1041  lscp_buffer_fill_t *lscp_get_channel_buf
1041      char *pch;      char *pch;
1042      int   iStream;      int   iStream;
1043    
1044        // Retrieve a channel stream estimation.
1045      iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel);      iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel);
1046        if (pClient->iStreamCount < 0)
1047            return NULL;
1048    
1049        // Lock this section up.
1050        lscp_mutex_lock(pClient->mutex);
1051    
1052        // Check if we need to reallocate the stream usage array.
1053      if (pClient->iStreamCount != iStreamCount) {      if (pClient->iStreamCount != iStreamCount) {
1054          if (pClient->buffer_fill)          if (pClient->buffer_fill)
1055              free(pClient->buffer_fill);              free(pClient->buffer_fill);
# Line 1016  lscp_buffer_fill_t *lscp_get_channel_buf Line 1060  lscp_buffer_fill_t *lscp_get_channel_buf
1060          pClient->iStreamCount = iStreamCount;          pClient->iStreamCount = iStreamCount;
1061      }      }
1062    
     if (pClient->iStreamCount < 1)  
         return NULL;  
   
     iStream = 0;  
     pBufferFill = pClient->buffer_fill;  
   
1063      // Get buffer fill usage...      // Get buffer fill usage...
1064      sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel);      pBufferFill = pClient->buffer_fill;
1065      if (lscp_client_query(pClient, szQuery) == LSCP_OK) {      if (pBufferFill && iStreamCount > 0) {
1066          pszResult = lscp_client_get_result(pClient);          iStream = 0;
1067          pszToken = lscp_strtok(pszResult, pszSeps, &(pch));          pBufferFill = pClient->buffer_fill;
1068          while (pszToken && iStream < pClient->iStreamCount) {          sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel);
1069              if (*pszToken) {          if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
1070                  pBufferFill[iStream].stream_id = atol(pszToken);              pszResult = lscp_client_get_result(pClient);
1071                pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1072                while (pszToken && iStream < pClient->iStreamCount) {
1073                    if (*pszToken) {
1074                        pBufferFill[iStream].stream_id = atol(pszToken);
1075                        pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1076                        if (pszToken == NULL)
1077                            break;
1078                        pBufferFill[iStream].stream_usage = atol(pszToken);
1079                        iStream++;
1080                    }
1081                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));
                 if (pszToken == NULL)  
                     break;  
                 pBufferFill[iStream].stream_usage = atol(pszToken);  
                 iStream++;  
1082              }              }
1083              pszToken = lscp_strtok(NULL, pszSeps, &(pch));          }   // Reset the usage, whatever it was before.
1084          }          else while (iStream < pClient->iStreamCount)
1085      }   // Reset the usage, whatever it was before.              pBufferFill[iStream++].stream_usage = 0;
1086      else while (iStream < pClient->iStreamCount)      }
1087          pBufferFill[iStream++].stream_usage = 0;      
1088        // Unlock this section down.
1089        lscp_mutex_unlock(pClient->mutex);
1090    
1091      return pBufferFill;      return pBufferFill;
1092  }  }
# Line 1067  lscp_status_t lscp_set_channel_audio_typ Line 1113  lscp_status_t lscp_set_channel_audio_typ
1113    
1114    
1115  /**  /**
1116     *  Setting audio output device:
1117     *  SET CHANNEL AUDIO_OUTPUT_DEVICE <sampler-channel> <device-id>
1118     *
1119     *  @param pClient          Pointer to client instance structure.
1120     *  @param iSamplerChannel  Sampler channel number.
1121     *  @param iAudioDevice     Audio output device number identifier.
1122     */
1123    lscp_status_t lscp_set_channel_audio_device ( lscp_client_t *pClient, int iSamplerChannel, int iAudioDevice )
1124    {
1125        char szQuery[LSCP_BUFSIZ];
1126    
1127        if (iSamplerChannel < 0 || iAudioDevice < 0)
1128            return LSCP_FAILED;
1129    
1130        sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_DEVICE %d %d\r\n", iSamplerChannel, iAudioDevice);
1131        return lscp_client_query(pClient, szQuery);
1132    }
1133    
1134    
1135    /**
1136   *  Setting audio output channel:   *  Setting audio output channel:
1137   *  SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-output-chan> <audio-input-chan>   *  SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-output-chan> <audio-input-chan>
1138   *   *
# Line 1110  lscp_status_t lscp_set_channel_midi_type Line 1176  lscp_status_t lscp_set_channel_midi_type
1176      return lscp_client_query(pClient, szQuery);      return lscp_client_query(pClient, szQuery);
1177  }  }
1178    
1179    
1180    /**
1181     *  Setting MIDI input device:
1182     *  SET CHANNEL MIDI_INPUT_DEVICE <sampler-channel> <device-id>
1183     *
1184     *  @param pClient          Pointer to client instance structure.
1185     *  @param iSamplerChannel  Sampler channel number.
1186     *  @param iMidiDevice      MIDI input device number identifier.
1187     */
1188    lscp_status_t lscp_set_channel_midi_device ( lscp_client_t *pClient, int iSamplerChannel, int iMidiDevice )
1189    {
1190        char szQuery[LSCP_BUFSIZ];
1191    
1192        if (iSamplerChannel < 0 || iMidiDevice < 0)
1193            return LSCP_FAILED;
1194    
1195        sprintf(szQuery, "SET CHANNEL MIDI_INPUT_DEVICE %d %d\r\n", iSamplerChannel, iMidiDevice);
1196        return lscp_client_query(pClient, szQuery);
1197    }
1198    
1199    
1200  /**  /**
1201   *  Setting MIDI input port:   *  Setting MIDI input port:

Legend:
Removed from v.107  
changed lines
  Added in v.163

  ViewVC Help
Powered by ViewVC