/[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 163 by capela, Wed Jun 30 15:16:03 2004 UTC revision 370 by capela, Fri Feb 11 13:32: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 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_t event;
57    
58  #ifdef DEBUG  #ifdef DEBUG
# Line 53  static void _lscp_client_evt_proc ( void Line 60  static void _lscp_client_evt_proc ( void
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                  event    = lscp_event_from_text(pszToken);                  pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".
89                  // And pick the rest of data...                  if (strcasecmp(pszToken, "NOTIFY") == 0) {
90                  pszToken = lscp_strtok(NULL, pszSeps, &(pch));                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));
91                  cchToken = (pszToken == NULL ? 0 : strlen(pszToken));                      event    = lscp_event_from_text(pszToken);
92                  // Double-check if we're really up to it...                      // And pick the rest of data...
93                  if (pClient->events & event) {                      pszToken = lscp_strtok(NULL, pszSeps, &(pch));
94                      // Invoke the client event callback...                      cchToken = (pszToken == NULL ? 0 : strlen(pszToken));
95                      if ((*pClient->pfnCallback)(                      // Double-check if we're really up to it...
96                              pClient,                      if (pClient->events & event) {
97                              event,                          // Invoke the client event callback...
98                              pszToken,                          if ((*pClient->pfnCallback)(
99                              cchToken,                                  pClient,
100                              pClient->pvData) != LSCP_OK) {                                  event,
101                          pClient->evt.iState = 0;                                  pszToken,
102                                    cchToken,
103                                    pClient->pvData) != LSCP_OK) {
104                                pClient->evt.iState = 0;
105                            }
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 162  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 290  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));      lscp_param_info_init(&(pClient->audio_param_info));
335      lscp_param_info_init(&(pClient->midi_param_info));      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_engine_info_init(&(pClient->engine_info));      lscp_engine_info_init(&(pClient->engine_info));
341      lscp_channel_info_init(&(pClient->channel_info));      lscp_channel_info_init(&(pClient->channel_info));
342      // Initialize error stuff.      // Initialize error stuff.
# Line 307  lscp_client_t* lscp_client_create ( cons Line 350  lscp_client_t* lscp_client_create ( cons
350    
351      // Initialize the transaction mutex.      // Initialize the transaction mutex.
352      lscp_mutex_init(pClient->mutex);      lscp_mutex_init(pClient->mutex);
353        lscp_cond_init(pClient->cond);
354    
355      // Finally we've some success...      // Finally we've some success...
356      return pClient;      return pClient;
# Line 354  lscp_status_t lscp_client_destroy ( lscp Line 398  lscp_status_t lscp_client_destroy ( lscp
398      lscp_mutex_lock(pClient->mutex);      lscp_mutex_lock(pClient->mutex);
399            
400      // Free up all cached members.      // Free up all cached members.
401      lscp_channel_info_reset(&(pClient->channel_info));      lscp_channel_info_free(&(pClient->channel_info));
402      lscp_engine_info_reset(&(pClient->engine_info));      lscp_engine_info_free(&(pClient->engine_info));
403      lscp_param_info_reset(&(pClient->midi_param_info));      lscp_param_info_free(&(pClient->midi_port_param_info));
404      lscp_param_info_reset(&(pClient->audio_param_info));      lscp_param_info_free(&(pClient->audio_channel_param_info));
405      lscp_driver_info_reset(&(pClient->midi_info));      lscp_device_port_info_free(&(pClient->midi_port_info));
406      lscp_driver_info_reset(&(pClient->audio_info));      lscp_device_port_info_free(&(pClient->audio_channel_info));
407        lscp_param_info_free(&(pClient->midi_param_info));
408        lscp_param_info_free(&(pClient->audio_param_info));
409        lscp_device_info_free(&(pClient->midi_device_info));
410        lscp_device_info_free(&(pClient->audio_device_info));
411        lscp_driver_info_free(&(pClient->midi_driver_info));
412        lscp_driver_info_free(&(pClient->audio_driver_info));
413      // Free available engine table.      // Free available engine table.
414      lscp_szsplit_destroy(pClient->audio_drivers);      lscp_szsplit_destroy(pClient->audio_drivers);
415      lscp_szsplit_destroy(pClient->midi_drivers);      lscp_szsplit_destroy(pClient->midi_drivers);
# Line 373  lscp_status_t lscp_client_destroy ( lscp Line 423  lscp_status_t lscp_client_destroy ( lscp
423      pClient->engines = NULL;      pClient->engines = NULL;
424      // Free result error stuff.      // Free result error stuff.
425      lscp_client_set_result(pClient, NULL, 0);      lscp_client_set_result(pClient, NULL, 0);
426      // Frre stream usage stuff.      // Free stream usage stuff.
427      if (pClient->buffer_fill)      if (pClient->buffer_fill)
428          free(pClient->buffer_fill);          free(pClient->buffer_fill);
429      pClient->buffer_fill = NULL;      pClient->buffer_fill = NULL;
# Line 387  lscp_status_t lscp_client_destroy ( lscp Line 437  lscp_status_t lscp_client_destroy ( lscp
437      // Last but not least, free good ol'transaction mutex.      // Last but not least, free good ol'transaction mutex.
438      lscp_mutex_unlock(pClient->mutex);      lscp_mutex_unlock(pClient->mutex);
439      lscp_mutex_destroy(pClient->mutex);      lscp_mutex_destroy(pClient->mutex);
440        lscp_cond_destroy(pClient->cond);
441    
442      free(pClient);      free(pClient);
443    
# Line 590  lscp_status_t lscp_client_unsubscribe ( Line 641  lscp_status_t lscp_client_unsubscribe (
641  }  }
642    
643    
644    /**
645     *  Getting current subscribed events.
646     *
647     *  @param pClient  Pointer to client instance structure.
648     *
649     *  @returns The current subscrived bit-wise OR'ed event flags.
650     */
651    lscp_event_t lscp_client_get_events ( lscp_client_t *pClient )
652    {
653        if (pClient == NULL)
654            return LSCP_EVENT_NONE;
655    
656        return pClient->events;
657    }
658    
659    
660  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
661  // Client command protocol functions.  // Client command protocol functions.
662    
# Line 836  lscp_engine_info_t *lscp_get_engine_info Line 903  lscp_engine_info_t *lscp_get_engine_info
903              if (strcasecmp(pszToken, "DESCRIPTION") == 0) {              if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
904                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
905                  if (pszToken)                  if (pszToken)
906                      pEngineInfo->description = lscp_unquote(&pszToken, 1);                      lscp_unquote_dup(&(pEngineInfo->description), &pszToken);
907              }              }
908              else if (strcasecmp(pszToken, "VERSION") == 0) {              else if (strcasecmp(pszToken, "VERSION") == 0) {
909                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
910                  if (pszToken)                  if (pszToken)
911                      pEngineInfo->version = lscp_unquote(&pszToken, 1);                      lscp_unquote_dup(&(pEngineInfo->version), &pszToken);
912              }              }
913              pszToken = lscp_strtok(NULL, pszSeps, &(pch));              pszToken = lscp_strtok(NULL, pszSeps, &(pch));
914          }          }
# Line 892  lscp_channel_info_t *lscp_get_channel_in Line 959  lscp_channel_info_t *lscp_get_channel_in
959              if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {              if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {
960                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
961                  if (pszToken)                  if (pszToken)
962                      pChannelInfo->engine_name = lscp_unquote(&pszToken, 1);                      lscp_unquote_dup(&(pChannelInfo->engine_name), &pszToken);
963              }              }
964              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {
965                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
# Line 906  lscp_channel_info_t *lscp_get_channel_in Line 973  lscp_channel_info_t *lscp_get_channel_in
973              }              }
974              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {              else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {
975                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
976                  if (pszToken)                  if (pszToken) {
977                        if (pChannelInfo->audio_routing)
978                            lscp_szsplit_destroy(pChannelInfo->audio_routing);
979                      pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");                      pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");
980                    }
981              }              }
982              else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {              else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {
983                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
984                  if (pszToken)                  if (pszToken)
985                      pChannelInfo->instrument_file = lscp_unquote(&pszToken, 1);                      lscp_unquote_dup(&(pChannelInfo->instrument_file), &pszToken);
986              }              }
987              else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {              else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {
988                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
# Line 936  lscp_channel_info_t *lscp_get_channel_in Line 1006  lscp_channel_info_t *lscp_get_channel_in
1006              }              }
1007              else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {              else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {
1008                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1009                  if (pszToken)                  if (pszToken) {
1010                      pChannelInfo->midi_channel = atoi(lscp_ltrim(pszToken));                      pszToken = lscp_ltrim(pszToken);
1011                        if (strcasecmp(pszToken, "ALL") == 0)
1012                            pChannelInfo->midi_channel = LSCP_MIDI_CHANNEL_ALL;
1013                        else
1014                            pChannelInfo->midi_channel = atoi(pszToken);
1015                    }
1016              }              }
1017              else if (strcasecmp(pszToken, "VOLUME") == 0) {              else if (strcasecmp(pszToken, "VOLUME") == 0) {
1018                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
# Line 991  int lscp_get_channel_voice_count ( lscp_ Line 1066  int lscp_get_channel_voice_count ( lscp_
1066   *  Current number of active disk streams:   *  Current number of active disk streams:
1067   *  GET CHANNEL STREAM_COUNT <sampler-channel>   *  GET CHANNEL STREAM_COUNT <sampler-channel>
1068   *   *
1069     *  @param pClient          Pointer to client instance structure.
1070     *  @param iSamplerChannel  Sampler channel number.
1071     *
1072   *  @returns The number of active disk streams on success, -1 otherwise.   *  @returns The number of active disk streams on success, -1 otherwise.
1073   */   */
1074  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 1016  int lscp_get_channel_stream_count ( lscp Line 1094  int lscp_get_channel_stream_count ( lscp
1094    
1095    
1096  /**  /**
1097     *  Current least usage of active disk streams.
1098     *
1099     *  @param pClient          Pointer to client instance structure.
1100     *  @param iSamplerChannel  Sampler channel number.
1101     *
1102     *  @returns The usage percentage of the least filled active disk stream
1103     *  on success, -1 otherwise.
1104     */
1105    int lscp_get_channel_stream_usage ( lscp_client_t *pClient, int iSamplerChannel )
1106    {
1107        char szQuery[LSCP_BUFSIZ];
1108        int  iStreamUsage = -1;
1109        const char *pszResult;
1110        const char *pszSeps = "[]%,";
1111        char *pszToken;
1112        char *pch;
1113        int   iStream;
1114        int   iPercent;
1115    
1116        if (iSamplerChannel < 0)
1117            return iStreamUsage;
1118    
1119        // Lock this section up.
1120        lscp_mutex_lock(pClient->mutex);
1121    
1122        iStream = 0;
1123        sprintf(szQuery, "GET CHANNEL BUFFER_FILL PERCENTAGE %d\r\n", iSamplerChannel);
1124        if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
1125            pszResult = lscp_client_get_result(pClient);
1126            pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1127            while (pszToken) {
1128                if (*pszToken) {
1129                    // Skip stream id.
1130                    pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1131                    if (pszToken == NULL)
1132                        break;
1133                    // Get least buffer fill percentage.
1134                    iPercent = atol(pszToken);
1135                    if (iStreamUsage > iPercent || iStream == 0)
1136                        iStreamUsage = iPercent;
1137                    iStream++;
1138                }
1139                pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1140            }
1141        }
1142    
1143        // Unlock this section down.
1144        lscp_mutex_unlock(pClient->mutex);
1145    
1146        return iStreamUsage;
1147    }
1148    
1149    
1150    /**
1151   *  Current fill state of disk stream buffers:   *  Current fill state of disk stream buffers:
1152   *  GET CHANNEL BUFFER_FILL {BYTES|PERCENTAGE} <sampler-channel>   *  GET CHANNEL BUFFER_FILL {BYTES|PERCENTAGE} <sampler-channel>
1153   *   *
# Line 1150  lscp_status_t lscp_set_channel_audio_cha Line 1282  lscp_status_t lscp_set_channel_audio_cha
1282      if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0)      if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0)
1283          return LSCP_FAILED;          return LSCP_FAILED;
1284    
1285      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);
1286      return lscp_client_query(pClient, szQuery);      return lscp_client_query(pClient, szQuery);
1287  }  }
1288    
# Line 1225  lscp_status_t lscp_set_channel_midi_port Line 1357  lscp_status_t lscp_set_channel_midi_port
1357   *   *
1358   *  @param pClient          Pointer to client instance structure.   *  @param pClient          Pointer to client instance structure.
1359   *  @param iSamplerChannel  Sampler channel number.   *  @param iSamplerChannel  Sampler channel number.
1360   *  @param iMidiChannel     MIDI channel number to listen (1-16) or   *  @param iMidiChannel     MIDI channel address number to listen (0-15) or
1361   *                          zero (0) to listen on all channels.   *                          LSCP_MIDI_CHANNEL_ALL (16) to listen on all channels.
1362   *   *
1363   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.   *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1364   */   */
# Line 1237  lscp_status_t lscp_set_channel_midi_chan Line 1369  lscp_status_t lscp_set_channel_midi_chan
1369      if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16)      if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16)
1370          return LSCP_FAILED;          return LSCP_FAILED;
1371    
1372      if (iMidiChannel > 0)      if (iMidiChannel == LSCP_MIDI_CHANNEL_ALL)
         sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel);  
     else  
1373          sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel);          sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel);
1374        else
1375            sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel);
1376      return lscp_client_query(pClient, szQuery);      return lscp_client_query(pClient, szQuery);
1377  }  }
1378    
# Line 1290  lscp_status_t lscp_reset_channel ( lscp_ Line 1422  lscp_status_t lscp_reset_channel ( lscp_
1422  }  }
1423    
1424    
1425    /**
1426     *  Resetting the sampler:
1427     *  RESET
1428     *
1429     *  @param pClient  Pointer to client instance structure.
1430     *
1431     *  @returns LSCP_OK on success, LSCP_FAILED otherwise.
1432     */
1433    lscp_status_t lscp_reset_sampler ( lscp_client_t *pClient )
1434    {
1435        return lscp_client_query(pClient, "RESET\r\n");
1436    }
1437    
1438    
1439  // end of client.c  // end of client.c

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

  ViewVC Help
Powered by ViewVC