/[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 144 by capela, Thu Jun 24 18:25:11 2004 UTC revision 623 by capela, Thu Jun 9 10:37:19 2005 UTC
# Line 2  Line 2 
2  //  //
3  /****************************************************************************  /****************************************************************************
4     liblscp - LinuxSampler Control Protocol API     liblscp - LinuxSampler Control Protocol API
5     Copyright (C) 2004, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2004-2005, rncbc aka Rui Nuno Capela. All rights reserved.
6    
7     This library is free software; you can redistribute it and/or     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Lesser General Public     modify it under the terms of the GNU Lesser General Public
# Line 14  Line 14 
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15     Lesser General Public License for more details.     Lesser General Public License for more details.
16    
17     You should have received a copy of the GNU Lesser General Public     You should 14have received a copy of the GNU Lesser General Public
18     License along with this library; if not, write to the Free Software     License along with this library; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20    
# Line 40  static lscp_status_t    _lscp_client_evt Line 40  static lscp_status_t    _lscp_client_evt
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      char  achBuffer[LSCP_BUFSIZ];  
44      int   cchBuffer;      fd_set fds;                         // File descriptor list for select().
45        int    fd, fdmax;                   // Maximum file descriptor number.
46        struct timeval tv;                  // For specifying a timeout value.
47        int    iSelect;                     // Holds select return status.
48        int    iTimeout;
49    
50        char   achBuffer[LSCP_BUFSIZ];
51        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_NONE;      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...
65            fd = (int) pClient->evt.sock;
66            FD_ZERO(&fds);
67            FD_SET((unsigned int) fd, &fds);
68            fdmax = fd;
69    
70            // Use the timeout (x10) select feature ...
71            iTimeout = 10 * pClient->iTimeout;
72            if (iTimeout > 1000) {
73                tv.tv_sec = iTimeout / 1000;
74                iTimeout -= tv.tv_sec * 1000;
75            }
76            else tv.tv_sec = 0;
77            tv.tv_usec = iTimeout * 1000;
78    
79          // Wait for event...          // Wait for event...
80          cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0);          iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv);
81          if (cchBuffer > 0) {          if (iSelect > 0 && FD_ISSET(fd, &fds)) {
82              // Make sure received buffer it's null terminated.              // May recv now...
83              achBuffer[cchBuffer] = (char) 0;              cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0);
84              // Parse for the notification event message...              if (cchBuffer > 0) {
85              pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".                  // Make sure received buffer it's null terminated.
86              if (strcasecmp(pszToken, "NOTIFY") == 0) {                  achBuffer[cchBuffer] = (char) 0;
87                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));                  // Parse for the notification event message...
88                  if (strcasecmp(pszToken, "CHANNELS") == 0)                  pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".
89                      event = LSCP_EVENT_CHANNELS;                  if (strcasecmp(pszToken, "NOTIFY") == 0) {
90                  else if (strcasecmp(pszToken, "VOICE_COUNT") == 0)                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));
91                      event = LSCP_EVENT_VOICE_COUNT;                      event    = lscp_event_from_text(pszToken);
92                  else if (strcasecmp(pszToken, "STREAM_COUNT") == 0)                      // And pick the rest of data...
93                      event = LSCP_EVENT_STREAM_COUNT;                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));
94                  else if (strcasecmp(pszToken, "BUFFER_FILL") == 0)                      cchToken = (pszToken == NULL ? 0 : strlen(pszToken));
95                      event = LSCP_EVENT_BUFFER_FILL;                      // Double-check if we're really up to it...
96                  else if (strcasecmp(pszToken, "CHANNEL_INFO") == 0)                      if (pClient->events & event) {
97                      event = LSCP_EVENT_CHANNEL_INFO;                          // Invoke the client event callback...
98                  else if (strcasecmp(pszToken, "MISCELLANEOUS") == 0)                          if ((*pClient->pfnCallback)(
99                      event = LSCP_EVENT_MISCELLANEOUS;                                  pClient,
100                  // And pick the rest of data...                                  event,
101                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));                                  pszToken,
102                  cchToken = (pszToken == NULL ? 0 : strlen(pszToken));                                  cchToken,
103                  // Double-check if we're really up to it...                                  pClient->pvData) != LSCP_OK) {
104                  if (pClient->events & event) {                              pClient->evt.iState = 0;
105                      // Invoke the client event callback...                          }
                     if ((*pClient->pfnCallback)(  
                             pClient,  
                             event,  
                             pszToken,  
                             cchToken,  
                             pClient->pvData) != LSCP_OK) {  
                         pClient->evt.iState = 0;  
106                      }                      }
107                  }                  }
108                } else {
109                    lscp_socket_perror("_lscp_client_evt_proc: recv");
110                    pClient->evt.iState = 0;
111              }              }
112          } else {          }   // Check if select has in error.
113              lscp_socket_perror("_lscp_client_evt_proc: recv");          else if (iSelect < 0) {
114                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.
119            lscp_cond_signal(pClient->cond);
120      }      }
121    
122  #ifdef DEBUG  #ifdef DEBUG
# Line 141  static lscp_status_t _lscp_client_evt_co Line 164  static lscp_status_t _lscp_client_evt_co
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  }  }
# Line 153  static lscp_status_t _lscp_client_evt_co Line 176  static lscp_status_t _lscp_client_evt_co
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      char *pszEvent;      const char *pszEvent;
180      char  szQuery[LSCP_BUFSIZ];      char  szQuery[LSCP_BUFSIZ];
181      int   cchQuery;      int   cchQuery;
182    
# Line 161  static lscp_status_t _lscp_client_evt_re Line 184  static lscp_status_t _lscp_client_evt_re
184          return LSCP_FAILED;          return LSCP_FAILED;
185    
186      // Which (single) event?      // Which (single) event?
187      switch (event) {      pszEvent = lscp_event_to_text(event);
188        case LSCP_EVENT_CHANNELS:      if (pszEvent == NULL)
         pszEvent = "CHANNELS";  
         break;  
       case LSCP_EVENT_VOICE_COUNT:  
         pszEvent = "VOICE_COUNT";  
         break;  
       case LSCP_EVENT_STREAM_COUNT:  
         pszEvent = "STREAM_COUNT";  
         break;  
       case LSCP_EVENT_BUFFER_FILL:  
         pszEvent = "BUFFER_FILL";  
         break;  
       case LSCP_EVENT_CHANNEL_INFO:  
         pszEvent = "CHANNEL_INFO";  
         break;  
       case LSCP_EVENT_MISCELLANEOUS:  
         pszEvent = "CHANNEL_INFO";  
         break;  
       default:  
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...
# Line 192  static lscp_status_t _lscp_client_evt_re Line 196  static lscp_status_t _lscp_client_evt_re
196          return LSCP_FAILED;          return LSCP_FAILED;
197      }      }
198    
199        // Wait on response.
200        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;
# Line 320  lscp_client_t* lscp_client_create ( cons Line 327  lscp_client_t* lscp_client_create ( cons
327      pClient->midi_devices = NULL;      pClient->midi_devices = NULL;
328      pClient->engines = NULL;      pClient->engines = NULL;
329      pClient->channels = NULL;      pClient->channels = NULL;
330      lscp_driver_info_init(&(pClient->audio_info));      lscp_driver_info_init(&(pClient->audio_driver_info));
331      lscp_driver_info_init(&(pClient->midi_info));      lscp_driver_info_init(&(pClient->midi_driver_info));
332        lscp_device_info_init(&(pClient->audio_device_info));
333        lscp_device_info_init(&(pClient->midi_device_info));
334        lscp_param_info_init(&(pClient->audio_param_info));
335        lscp_param_info_init(&(pClient->midi_param_info));
336        lscp_device_port_info_init(&(pClient->audio_channel_info));
337        lscp_device_port_info_init(&(pClient->midi_port_info));
338        lscp_param_info_init(&(pClient->audio_channel_param_info));
339        lscp_param_info_init(&(pClient->midi_port_param_info));
340        lscp_server_info_init(&(pClient->server_info));
341      lscp_engine_info_init(&(pClient->engine_info));      lscp_engine_info_init(&(pClient->engine_info));
342      lscp_channel_info_init(&(pClient->channel_info));      lscp_channel_info_init(&(pClient->channel_info));
343      // Initialize error stuff.      // Initialize error stuff.
# Line 332  lscp_client_t* lscp_client_create ( cons Line 348  lscp_client_t* lscp_client_create ( cons
348      pClient->iStreamCount = 0;      pClient->iStreamCount = 0;
349      // Default timeout value.      // Default timeout value.
350      pClient->iTimeout = LSCP_TIMEOUT_MSECS;      pClient->iTimeout = LSCP_TIMEOUT_MSECS;
351        pClient->iTimeoutCount = 0;
352    
353      // Initialize the transaction mutex.      // Initialize the transaction mutex.
354      lscp_mutex_init(pClient->mutex);      lscp_mutex_init(pClient->mutex);
355        lscp_cond_init(pClient->cond);
356    
357      // Finally we've some success...      // Finally we've some success...
358      return pClient;      return pClient;
# Line 380  lscp_status_t lscp_client_destroy ( lscp Line 398  lscp_status_t lscp_client_destroy ( lscp
398    
399      // Lock this section up.      // Lock this section up.
400      lscp_mutex_lock(pClient->mutex);      lscp_mutex_lock(pClient->mutex);
401        
402      // Free up all cached members.      // Free up all cached members.
403      lscp_channel_info_reset(&(pClient->channel_info));      lscp_channel_info_free(&(pClient->channel_info));
404      lscp_engine_info_reset(&(pClient->engine_info));      lscp_engine_info_free(&(pClient->engine_info));
405      lscp_driver_info_reset(&(pClient->midi_info));      lscp_server_info_free(&(pClient->server_info));
406      lscp_driver_info_reset(&(pClient->audio_info));      lscp_param_info_free(&(pClient->midi_port_param_info));
407        lscp_param_info_free(&(pClient->audio_channel_param_info));
408        lscp_device_port_info_free(&(pClient->midi_port_info));
409        lscp_device_port_info_free(&(pClient->audio_channel_info));
410        lscp_param_info_free(&(pClient->midi_param_info));
411        lscp_param_info_free(&(pClient->audio_param_info));
412        lscp_device_info_free(&(pClient->midi_device_info));
413        lscp_device_info_free(&(pClient->audio_device_info));
414        lscp_driver_info_free(&(pClient->midi_driver_info));
415        lscp_driver_info_free(&(pClient->audio_driver_info));
416      // Free available engine table.      // Free available engine table.
417      lscp_szsplit_destroy(pClient->audio_drivers);      lscp_szsplit_destroy(pClient->audio_drivers);
418      lscp_szsplit_destroy(pClient->midi_drivers);      lscp_szsplit_destroy(pClient->midi_drivers);
# Line 399  lscp_status_t lscp_client_destroy ( lscp Line 426  lscp_status_t lscp_client_destroy ( lscp
426      pClient->engines = NULL;      pClient->engines = NULL;
427      // Free result error stuff.      // Free result error stuff.
428      lscp_client_set_result(pClient, NULL, 0);      lscp_client_set_result(pClient, NULL, 0);
429      // Frre stream usage stuff.      // Free stream usage stuff.
430      if (pClient->buffer_fill)      if (pClient->buffer_fill)
431          free(pClient->buffer_fill);          free(pClient->buffer_fill);
432      pClient->buffer_fill = NULL;      pClient->buffer_fill = NULL;
# Line 413  lscp_status_t lscp_client_destroy ( lscp Line 440  lscp_status_t lscp_client_destroy ( lscp
440      // Last but not least, free good ol'transaction mutex.      // Last but not least, free good ol'transaction mutex.
441      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
442      lscp_mutex_destroy(pClient->mutex);      lscp_mutex_destroy(pClient->mutex);
443        lscp_cond_destroy(pClient->cond);
444    
445      free(pClient);      free(pClient);
446    
# Line 475  int lscp_client_get_timeout ( lscp_clien Line 503  int lscp_client_get_timeout ( lscp_clien
503  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 )
504  {  {
505      lscp_status_t ret;      lscp_status_t ret;
506        
507      // Lock this section up.      // Lock this section up.
508      lscp_mutex_lock(pClient->mutex);      lscp_mutex_lock(pClient->mutex);
509    
510      // Just make the now guarded call.      // Just make the now guarded call.
511      ret = lscp_client_call(pClient, pszQuery);      ret = lscp_client_call(pClient, pszQuery);
512        
513      // Unlock this section down.      // Unlock this section down.
514      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
515        
516      return ret;      return ret;
517  }  }
518    
# Line 528  int lscp_client_get_errno ( lscp_client_ Line 556  int lscp_client_get_errno ( lscp_client_
556    
557  /**  /**
558   *  Register frontend for receiving event messages:   *  Register frontend for receiving event messages:
559   *  SUBSCRIBE CHANNELS | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL   *  SUBSCRIBE CHANNEL_COUNT | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL
560   *      | CHANNEL_INFO | MISCELLANEOUS   *      | CHANNEL_INFO | MISCELLANEOUS
561   *   *
562   *  @param pClient  Pointer to client instance structure.   *  @param pClient  Pointer to client instance structure.
# Line 549  lscp_status_t lscp_client_subscribe ( ls Line 577  lscp_status_t lscp_client_subscribe ( ls
577      // If applicable, start the alternate connection...      // If applicable, start the alternate connection...
578      if (pClient->events == LSCP_EVENT_NONE)      if (pClient->events == LSCP_EVENT_NONE)
579          ret = _lscp_client_evt_connect(pClient);          ret = _lscp_client_evt_connect(pClient);
580        
581      // Send the subscription commands.      // Send the subscription commands.
582      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNELS))      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT))
583          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNELS);          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_COUNT);
584      if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))      if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
585          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT);          ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT);
586      if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))      if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
# Line 573  lscp_status_t lscp_client_subscribe ( ls Line 601  lscp_status_t lscp_client_subscribe ( ls
601    
602  /**  /**
603   *  Deregister frontend from receiving UDP event messages anymore:   *  Deregister frontend from receiving UDP event messages anymore:
604   *  SUBSCRIBE CHANNELS | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL   *  SUBSCRIBE CHANNEL_COUNT | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL
605   *      | CHANNEL_INFO | MISCELLANEOUS   *      | CHANNEL_INFO | MISCELLANEOUS
606   *   *
607   *  @param pClient  Pointer to client instance structure.   *  @param pClient  Pointer to client instance structure.
# Line 592  lscp_status_t lscp_client_unsubscribe ( Line 620  lscp_status_t lscp_client_unsubscribe (
620      lscp_mutex_lock(pClient->mutex);      lscp_mutex_lock(pClient->mutex);
621    
622      // Send the unsubscription commands.      // Send the unsubscription commands.
623      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNELS))      if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT))
624          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNELS);          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_COUNT);
625      if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))      if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
626          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT);          ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT);
627      if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))      if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
# Line 616  lscp_status_t lscp_client_unsubscribe ( Line 644  lscp_status_t lscp_client_unsubscribe (
644  }  }
645    
646    
647    /**
648     *  Getting current subscribed events.
649     *
650     *  @param pClient  Pointer to client instance structure.
651     *
652     *  @returns The current subscrived bit-wise OR'ed event flags.
653     */
654    lscp_event_t lscp_client_get_events ( lscp_client_t *pClient )
655    {
656        if (pClient == NULL)
657            return LSCP_EVENT_NONE;
658    
659        return pClient->events;
660    }
661    
662    
663  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
664  // Client command protocol functions.  // Client command protocol functions.
665    
# Line 728  int *lscp_list_channels ( lscp_client_t Line 772  int *lscp_list_channels ( lscp_client_t
772    
773      if (pClient == NULL)      if (pClient == NULL)
774          return NULL;          return NULL;
775            
776      // Lock this section up.      // Lock this section up.
777      lscp_mutex_lock(pClient->mutex);      lscp_mutex_lock(pClient->mutex);
778    
# Line 765  int lscp_add_channel ( lscp_client_t *pC Line 809  int lscp_add_channel ( lscp_client_t *pC
809    
810      if (lscp_client_call(pClient, "ADD CHANNEL\r\n") == LSCP_OK)      if (lscp_client_call(pClient, "ADD CHANNEL\r\n") == LSCP_OK)
811          iSamplerChannel = atoi(lscp_client_get_result(pClient));          iSamplerChannel = atoi(lscp_client_get_result(pClient));
812            
813      // Unlock this section down.      // Unlock this section down.
814      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
815    
# Line 795  lscp_status_t lscp_remove_channel ( lscp Line 839  lscp_status_t lscp_remove_channel ( lscp
839    
840    
841  /**  /**
842   *  Getting all available engines:   *  Getting all available engines count:
843   *  GET AVAILABLE_ENGINES   *  GET AVAILABLE_ENGINES
844   *   *
845   *  @param pClient  Pointer to client instance structure.   *  @param pClient  Pointer to client instance structure.
846   *   *
847     *  @returns The current total number of sampler engines on success,
848     *  -1 otherwise.
849     */
850    int lscp_get_available_engines ( lscp_client_t *pClient )
851    {
852        int iAvailableEngines = -1;
853    
854        // Lock this section up.
855        lscp_mutex_lock(pClient->mutex);
856    
857        if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n") == LSCP_OK)
858            iAvailableEngines = atoi(lscp_client_get_result(pClient));
859    
860        // Unlock this section down.
861        lscp_mutex_unlock(pClient->mutex);
862    
863        return iAvailableEngines;
864    }
865    
866    
867    /**
868     *  Getting all available engines:
869     *  LIST AVAILABLE_ENGINES
870     *
871     *  @param pClient  Pointer to client instance structure.
872     *
873   *  @returns A NULL terminated array of engine name strings,   *  @returns A NULL terminated array of engine name strings,
874   *  or NULL in case of failure.   *  or NULL in case of failure.
875   */   */
876  const char **lscp_get_available_engines ( lscp_client_t *pClient )  const char **lscp_list_available_engines ( lscp_client_t *pClient )
877  {  {
878      const char *pszSeps = ",";      const char *pszSeps = ",";
879    
# Line 815  const char **lscp_get_available_engines Line 885  const char **lscp_get_available_engines
885          pClient->engines = NULL;          pClient->engines = NULL;
886      }      }
887    
888      if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n") == LSCP_OK)      if (lscp_client_call(pClient, "LIST AVAILABLE_ENGINES\r\n") == LSCP_OK)
889          pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps);          pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps);
890    
891      // Unlock this section down.      // Unlock this section down.
# Line 862  lscp_engine_info_t *lscp_get_engine_info Line 932  lscp_engine_info_t *lscp_get_engine_info
932              if (strcasecmp(pszToken, "DESCRIPTION") == 0) {              if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
933                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
934                  if (pszToken)                  if (pszToken)
935                      pEngineInfo->description = lscp_unquote(&pszToken, 1);                      lscp_unquote_dup(&(pEngineInfo->description), &pszToken);
936              }              }
937              else if (strcasecmp(pszToken, "VERSION") == 0) {              else if (strcasecmp(pszToken, "VERSION") == 0) {
938                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
939                  if (pszToken)                  if (pszToken)
940                      pEngineInfo->version = lscp_unquote(&pszToken, 1);                      lscp_unquote_dup(&(pEngineInfo->version), &pszToken);
941              }              }
942              pszToken = lscp_strtok(NULL, pszSeps, &(pch));              pszToken = lscp_strtok(NULL, pszSeps, &(pch));
943          }          }
944      }      }
945      else pEngineInfo = NULL;      else pEngineInfo = NULL;
946        
947      // Unlock this section down.      // Unlock this section down.
948      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
949    
# Line 906  lscp_channel_info_t *lscp_get_channel_in Line 976  lscp_channel_info_t *lscp_get_channel_in
976    
977      // Lock this section up.      // Lock this section up.
978      lscp_mutex_lock(pClient->mutex);      lscp_mutex_lock(pClient->mutex);
979        
980      pChannelInfo = &(pClient->channel_info);      pChannelInfo = &(pClient->channel_info);
981      lscp_channel_info_reset(pChannelInfo);      lscp_channel_info_reset(pChannelInfo);
982    
# Line 918  lscp_channel_info_t *lscp_get_channel_in Line 988  lscp_channel_info_t *lscp_get_channel_in
988              if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {              if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {
989                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
990                  if (pszToken)                  if (pszToken)
991                      pChannelInfo->engine_name = lscp_unquote(&pszToken, 1);                      lscp_unquote_dup(&(pChannelInfo->engine_name), &pszToken);
992              }              }
993              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {
994                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
# Line 932  lscp_channel_info_t *lscp_get_channel_in Line 1002  lscp_channel_info_t *lscp_get_channel_in
1002              }              }
1003              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {
1004                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1005                  if (pszToken)                  if (pszToken) {
1006                        if (pChannelInfo->audio_routing)
1007                            lscp_szsplit_destroy(pChannelInfo->audio_routing);
1008                      pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");                      pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");
1009                    }
1010              }              }
1011              else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {              else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {
1012                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1013                  if (pszToken)                  if (pszToken)
1014                      pChannelInfo->instrument_file = lscp_unquote(&pszToken, 1);                      lscp_unquote_dup(&(pChannelInfo->instrument_file), &pszToken);
1015              }              }
1016              else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {              else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {
1017                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1018                  if (pszToken)                  if (pszToken)
1019                      pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken));                      pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken));
1020              }              }
1021                else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) {
1022                    pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1023                    if (pszToken)
1024                        lscp_unquote_dup(&(pChannelInfo->instrument_name), &pszToken);
1025                }
1026              else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) {              else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) {
1027                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1028                  if (pszToken)                  if (pszToken)
# Line 962  lscp_channel_info_t *lscp_get_channel_in Line 1040  lscp_channel_info_t *lscp_get_channel_in
1040              }              }
1041              else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {              else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {
1042                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1043                  if (pszToken)                  if (pszToken) {
1044                      pChannelInfo->midi_channel = atoi(lscp_ltrim(pszToken));                      pszToken = lscp_ltrim(pszToken);
1045                        if (strcasecmp(pszToken, "ALL") == 0)
1046                            pChannelInfo->midi_channel = LSCP_MIDI_CHANNEL_ALL;
1047                        else
1048                            pChannelInfo->midi_channel = atoi(pszToken);
1049                    }
1050              }              }
1051              else if (strcasecmp(pszToken, "VOLUME") == 0) {              else if (strcasecmp(pszToken, "VOLUME") == 0) {
1052                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
# Line 974  lscp_channel_info_t *lscp_get_channel_in Line 1057  lscp_channel_info_t *lscp_get_channel_in
1057          }          }
1058      }      }
1059      else pChannelInfo = NULL;      else pChannelInfo = NULL;
1060        
1061      // Unlock this section up.      // Unlock this section up.
1062      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
1063    
# Line 1017  int lscp_get_channel_voice_count ( lscp_ Line 1100  int lscp_get_channel_voice_count ( lscp_
1100   *  Current number of active disk streams:   *  Current number of active disk streams:
1101   *  GET CHANNEL STREAM_COUNT <sampler-channel>   *  GET CHANNEL STREAM_COUNT <sampler-channel>
1102   *   *
1103     *  @param pClient          Pointer to client instance structure.
1104     *  @param iSamplerChannel  Sampler channel number.
1105     *
1106   *  @returns The number of active disk streams on success, -1 otherwise.   *  @returns The number of active disk streams on success, -1 otherwise.
1107   */   */
1108  int lscp_get_channel_stream_count ( lscp_client_t *pClient, int iSamplerChannel )  int lscp_get_channel_stream_count ( lscp_client_t *pClient, int iSamplerChannel )
# Line 1042  int lscp_get_channel_stream_count ( lscp Line 1128  int lscp_get_channel_stream_count ( lscp
1128    
1129    
1130  /**  /**
1131     *  Current least usage of active disk streams.
1132     *
1133     *  @param pClient          Pointer to client instance structure.
1134     *  @param iSamplerChannel  Sampler channel number.
1135     *
1136     *  @returns The usage percentage of the least filled active disk stream
1137     *  on success, -1 otherwise.
1138     */
1139    int lscp_get_channel_stream_usage ( lscp_client_t *pClient, int iSamplerChannel )
1140    {
1141        char szQuery[LSCP_BUFSIZ];
1142        int  iStreamUsage = -1;
1143        const char *pszResult;
1144        const char *pszSeps = "[]%,";
1145        char *pszToken;
1146        char *pch;
1147        int   iStream;
1148        int   iPercent;
1149    
1150        if (iSamplerChannel < 0)
1151            return iStreamUsage;
1152    
1153        // Lock this section up.
1154        lscp_mutex_lock(pClient->mutex);
1155    
1156        iStream = 0;
1157        sprintf(szQuery, "GET CHANNEL BUFFER_FILL PERCENTAGE %d\r\n", iSamplerChannel);
1158        if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
1159            pszResult = lscp_client_get_result(pClient);
1160            pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1161            while (pszToken) {
1162                if (*pszToken) {
1163                    // Skip stream id.
1164                    pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1165                    if (pszToken == NULL)
1166                        break;
1167                    // Get least buffer fill percentage.
1168                    iPercent = atol(pszToken);
1169                    if (iStreamUsage > iPercent || iStream == 0)
1170                        iStreamUsage = iPercent;
1171                    iStream++;
1172                }
1173                pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1174            }
1175        }
1176    
1177        // Unlock this section down.
1178        lscp_mutex_unlock(pClient->mutex);
1179    
1180        return iStreamUsage;
1181    }
1182    
1183    
1184    /**
1185   *  Current fill state of disk stream buffers:   *  Current fill state of disk stream buffers:
1186   *  GET CHANNEL BUFFER_FILL {BYTES|PERCENTAGE} <sampler-channel>   *  GET CHANNEL BUFFER_FILL {BYTES|PERCENTAGE} <sampler-channel>
1187   *   *
# Line 1110  lscp_buffer_fill_t *lscp_get_channel_buf Line 1250  lscp_buffer_fill_t *lscp_get_channel_buf
1250          else while (iStream < pClient->iStreamCount)          else while (iStream < pClient->iStreamCount)
1251              pBufferFill[iStream++].stream_usage = 0;              pBufferFill[iStream++].stream_usage = 0;
1252      }      }
1253        
1254      // Unlock this section down.      // Unlock this section down.
1255      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
1256    
# Line 1176  lscp_status_t lscp_set_channel_audio_cha Line 1316  lscp_status_t lscp_set_channel_audio_cha
1316      if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0)      if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0)
1317          return LSCP_FAILED;          return LSCP_FAILED;
1318    
1319      sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_CHANNELS %d %d %d\r\n", iSamplerChannel, iAudioOut, iAudioIn);      sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_CHANNEL %d %d %d\r\n", iSamplerChannel, iAudioOut, iAudioIn);
1320      return lscp_client_query(pClient, szQuery);      return lscp_client_query(pClient, szQuery);
1321  }  }
1322    
# Line 1251  lscp_status_t lscp_set_channel_midi_port Line 1391  lscp_status_t lscp_set_channel_midi_port
1391   *   *
1392   *  @param pClient          Pointer to client instance structure.   *  @param pClient          Pointer to client instance structure.
1393   *  @param iSamplerChannel  Sampler channel number.   *  @param iSamplerChannel  Sampler channel number.
1394   *  @param iMidiChannel     MIDI channel number to listen (1-16) or   *  @param iMidiChannel     MIDI channel address number to listen (0-15) or
1395   *                          zero (0) to listen on all channels.   *                          LSCP_MIDI_CHANNEL_ALL (16) to listen on all channels.
1396   *   *
1397   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1398   */   */
# Line 1263  lscp_status_t lscp_set_channel_midi_chan Line 1403  lscp_status_t lscp_set_channel_midi_chan
1403      if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16)      if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16)
1404          return LSCP_FAILED;          return LSCP_FAILED;
1405    
1406      if (iMidiChannel > 0)      if (iMidiChannel == LSCP_MIDI_CHANNEL_ALL)
         sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel);  
     else  
1407          sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel);          sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel);
1408        else
1409            sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel);
1410      return lscp_client_query(pClient, szQuery);      return lscp_client_query(pClient, szQuery);
1411  }  }
1412    
# Line 1316  lscp_status_t lscp_reset_channel ( lscp_ Line 1456  lscp_status_t lscp_reset_channel ( lscp_
1456  }  }
1457    
1458    
1459    /**
1460     *  Resetting the sampler:
1461     *  RESET
1462     *
1463     *  @param pClient  Pointer to client instance structure.
1464     *
1465     *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1466     */
1467    lscp_status_t lscp_reset_sampler ( lscp_client_t *pClient )
1468    {
1469        return lscp_client_query(pClient, "RESET\r\n");
1470    }
1471    
1472    
1473    /**
1474     *  Getting information about the server.
1475     *  GET SERVER INFO
1476     *
1477     *  @param pClient  Pointer to client instance structure.
1478     *
1479     *  @returns A pointer to a @ref lscp_server_info_t structure, with all the
1480     *  information of the current connected server, or NULL in case of failure.
1481     */
1482    lscp_server_info_t *lscp_get_server_info ( lscp_client_t *pClient )
1483    {
1484        lscp_server_info_t *pServerInfo;
1485        const char *pszResult;
1486        const char *pszSeps = ":";
1487        const char *pszCrlf = "\r\n";
1488        char *pszToken;
1489        char *pch;
1490    
1491        // Lock this section up.
1492        lscp_mutex_lock(pClient->mutex);
1493    
1494        pServerInfo = &(pClient->server_info);
1495        lscp_server_info_reset(pServerInfo);
1496    
1497        if (lscp_client_call(pClient, "GET SERVER INFO\r\n") == LSCP_OK) {
1498            pszResult = lscp_client_get_result(pClient);
1499            pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1500            while (pszToken) {
1501                if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
1502                    pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1503                    if (pszToken)
1504                        lscp_unquote_dup(&(pServerInfo->description), &pszToken);
1505                }
1506                else if (strcasecmp(pszToken, "VERSION") == 0) {
1507                    pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1508                    if (pszToken)
1509                        lscp_unquote_dup(&(pServerInfo->version), &pszToken);
1510                }
1511                pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1512            }
1513        }
1514        else pServerInfo = NULL;
1515    
1516        // Unlock this section down.
1517        lscp_mutex_unlock(pClient->mutex);
1518    
1519        return pServerInfo;
1520    }
1521    
1522    
1523  // end of client.c  // end of client.c

Legend:
Removed from v.144  
changed lines
  Added in v.623

  ViewVC Help
Powered by ViewVC