/[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 1802 by schoenebeck, Sun Dec 7 13:50:08 2008 UTC revision 1806 by schoenebeck, Thu Dec 11 01:28:42 2008 UTC
# Line 20  Line 20 
20    
21  *****************************************************************************/  *****************************************************************************/
22    
23    #include <locale.h>
24  #include "common.h"  #include "common.h"
25    
26  // Default timeout value (in milliseconds).  // Default timeout value (in milliseconds).
# Line 35  static lscp_status_t    _lscp_client_evt Line 36  static lscp_status_t    _lscp_client_evt
36    
37    
38  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
39    // General helper functions.
40    
41    struct _locale_t {
42            char numeric[32];
43            char ctype[32];
44    };
45    
46    // we need to ensure a constant locale setting e.g. for parsing
47    // floating point numbers with atof(), as the floating point separator
48    // character varies by the invidual locale settings
49    static void _save_and_set_c_locale(struct _locale_t* locale)
50    {
51            strncpy(locale->numeric, setlocale(LC_NUMERIC, NULL), 32);
52            strncpy(locale->ctype, setlocale(LC_CTYPE, NULL), 32);
53            setlocale(LC_NUMERIC, "C");
54            setlocale(LC_CTYPE, "C");
55    }
56    
57    // restore the original locale setting as nothing happened
58    static void _restore_locale(struct _locale_t* locale)
59    {
60            setlocale(LC_NUMERIC, locale->numeric);
61            setlocale(LC_CTYPE, locale->ctype);
62    }
63    
64    // seems the standard atof() function doesnt care much about locale
65    // runtime modifications, so we use this workaround
66    static float _atof(const char* txt) {
67            float f;
68            sscanf(txt, "%f", &f); // yeah, you're a good boy sscanf()
69            return f;
70    }
71    
72    
73    //-------------------------------------------------------------------------
74  // Event service (datagram oriented).  // Event service (datagram oriented).
75    
76  static void _lscp_client_evt_proc ( void *pvClient )  static void _lscp_client_evt_proc ( void *pvClient )
# Line 1111  lscp_channel_info_t *lscp_get_channel_in Line 1147  lscp_channel_info_t *lscp_get_channel_in
1147          const char *pszCrlf = "\r\n";          const char *pszCrlf = "\r\n";
1148          char *pszToken;          char *pszToken;
1149          char *pch;          char *pch;
1150            struct _locale_t locale;
1151    
1152          if (pClient == NULL)          if (pClient == NULL)
1153                  return NULL;                  return NULL;
# Line 1123  lscp_channel_info_t *lscp_get_channel_in Line 1160  lscp_channel_info_t *lscp_get_channel_in
1160          pChannelInfo = &(pClient->channel_info);          pChannelInfo = &(pClient->channel_info);
1161          lscp_channel_info_reset(pChannelInfo);          lscp_channel_info_reset(pChannelInfo);
1162    
1163            _save_and_set_c_locale(&locale);
1164    
1165          sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel);          sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel);
1166          if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {          if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {
1167                  pszResult = lscp_client_get_result(pClient);                  pszResult = lscp_client_get_result(pClient);
# Line 1207  lscp_channel_info_t *lscp_get_channel_in Line 1246  lscp_channel_info_t *lscp_get_channel_in
1246                          else if (strcasecmp(pszToken, "VOLUME") == 0) {                          else if (strcasecmp(pszToken, "VOLUME") == 0) {
1247                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1248                                  if (pszToken)                                  if (pszToken)
1249                                          pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken));                                          pChannelInfo->volume = _atof(lscp_ltrim(pszToken));
1250                          }                          }
1251                          else if (strcasecmp(pszToken, "MUTE") == 0) {                          else if (strcasecmp(pszToken, "MUTE") == 0) {
1252                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
# Line 1224  lscp_channel_info_t *lscp_get_channel_in Line 1263  lscp_channel_info_t *lscp_get_channel_in
1263          }          }
1264          else pChannelInfo = NULL;          else pChannelInfo = NULL;
1265    
1266            _restore_locale(&locale);
1267    
1268          // Unlock this section up.          // Unlock this section up.
1269          lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
1270    
# Line 1638  lscp_status_t lscp_set_channel_midi_map Line 1679  lscp_status_t lscp_set_channel_midi_map
1679  lscp_status_t lscp_set_channel_volume ( lscp_client_t *pClient, int iSamplerChannel, float fVolume )  lscp_status_t lscp_set_channel_volume ( lscp_client_t *pClient, int iSamplerChannel, float fVolume )
1680  {  {
1681          char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1682            struct _locale_t locale;
1683    
1684          if (iSamplerChannel < 0 || fVolume < 0.0f)          if (iSamplerChannel < 0 || fVolume < 0.0f)
1685                  return LSCP_FAILED;                  return LSCP_FAILED;
1686    
1687            _save_and_set_c_locale(&locale);
1688          sprintf(szQuery, "SET CHANNEL VOLUME %d %g\r\n", iSamplerChannel, fVolume);          sprintf(szQuery, "SET CHANNEL VOLUME %d %g\r\n", iSamplerChannel, fVolume);
1689            _restore_locale(&locale);
1690    
1691          return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1692  }  }
1693    
# Line 1859  int lscp_get_total_voice_count_max ( lsc Line 1904  int lscp_get_total_voice_count_max ( lsc
1904  float lscp_get_volume ( lscp_client_t *pClient )  float lscp_get_volume ( lscp_client_t *pClient )
1905  {  {
1906          float fVolume = 0.0f;          float fVolume = 0.0f;
1907            struct _locale_t locale;
1908    
1909          if (pClient == NULL)          if (pClient == NULL)
1910                  return 0.0f;                  return 0.0f;
# Line 1866  float lscp_get_volume ( lscp_client_t *p Line 1912  float lscp_get_volume ( lscp_client_t *p
1912          // Lock this section up.          // Lock this section up.
1913          lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
1914    
1915            _save_and_set_c_locale(&locale);
1916    
1917          if (lscp_client_call(pClient, "GET VOLUME\r\n", 0) == LSCP_OK)          if (lscp_client_call(pClient, "GET VOLUME\r\n", 0) == LSCP_OK)
1918                  fVolume = (float) atof(lscp_client_get_result(pClient));                  fVolume = _atof(lscp_client_get_result(pClient));
1919    
1920            _restore_locale(&locale);
1921    
1922          // Unlock this section down.          // Unlock this section down.
1923          lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
# Line 1890  float lscp_get_volume ( lscp_client_t *p Line 1940  float lscp_get_volume ( lscp_client_t *p
1940  lscp_status_t lscp_set_volume ( lscp_client_t *pClient, float fVolume )  lscp_status_t lscp_set_volume ( lscp_client_t *pClient, float fVolume )
1941  {  {
1942          char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
1943            struct _locale_t locale;
1944    
1945          if (fVolume < 0.0f)          if (fVolume < 0.0f)
1946                  return LSCP_FAILED;                  return LSCP_FAILED;
1947    
1948            _save_and_set_c_locale(&locale);
1949          sprintf(szQuery, "SET VOLUME %g\r\n", fVolume);          sprintf(szQuery, "SET VOLUME %g\r\n", fVolume);
1950            _restore_locale(&locale);
1951    
1952          return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
1953  }  }
1954    
# Line 2181  lscp_fxsend_info_t *lscp_get_fxsend_info Line 2235  lscp_fxsend_info_t *lscp_get_fxsend_info
2235          const char *pszCrlf = "\r\n";          const char *pszCrlf = "\r\n";
2236          char *pszToken;          char *pszToken;
2237          char *pch;          char *pch;
2238            struct _locale_t locale;
2239    
2240          if (pClient == NULL)          if (pClient == NULL)
2241                  return NULL;                  return NULL;
# Line 2190  lscp_fxsend_info_t *lscp_get_fxsend_info Line 2245  lscp_fxsend_info_t *lscp_get_fxsend_info
2245          // Lock this section up.          // Lock this section up.
2246          lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
2247    
2248            _save_and_set_c_locale(&locale);
2249    
2250          pFxSendInfo = &(pClient->fxsend_info);          pFxSendInfo = &(pClient->fxsend_info);
2251          lscp_fxsend_info_reset(pFxSendInfo);          lscp_fxsend_info_reset(pFxSendInfo);
2252    
# Line 2219  lscp_fxsend_info_t *lscp_get_fxsend_info Line 2276  lscp_fxsend_info_t *lscp_get_fxsend_info
2276                          else if (strcasecmp(pszToken, "LEVEL") == 0) {                          else if (strcasecmp(pszToken, "LEVEL") == 0) {
2277                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2278                                  if (pszToken)                                  if (pszToken)
2279                                          pFxSendInfo->level = (float) atof(lscp_ltrim(pszToken));                                          pFxSendInfo->level = _atof(lscp_ltrim(pszToken));
2280                          }                          }
2281                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));
2282                  }                  }
2283          }          }
2284          else pFxSendInfo = NULL;          else pFxSendInfo = NULL;
2285    
2286            _restore_locale(&locale);
2287    
2288          // Unlock this section up.          // Unlock this section up.
2289          lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
2290    
# Line 2319  lscp_status_t lscp_set_fxsend_midi_contr Line 2378  lscp_status_t lscp_set_fxsend_midi_contr
2378  lscp_status_t lscp_set_fxsend_level ( lscp_client_t *pClient, int iSamplerChannel, int iFxSend, float fLevel )  lscp_status_t lscp_set_fxsend_level ( lscp_client_t *pClient, int iSamplerChannel, int iFxSend, float fLevel )
2379  {  {
2380          char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
2381            struct _locale_t locale;
2382    
2383          if (iSamplerChannel < 0 || iFxSend < 0 || fLevel < 0.0f)          if (iSamplerChannel < 0 || iFxSend < 0 || fLevel < 0.0f)
2384                  return LSCP_FAILED;                  return LSCP_FAILED;
2385    
2386            _save_and_set_c_locale(&locale);
2387          sprintf(szQuery, "SET FX_SEND LEVEL %d %d %f\r\n", iSamplerChannel, iFxSend, fLevel);          sprintf(szQuery, "SET FX_SEND LEVEL %d %d %f\r\n", iSamplerChannel, iFxSend, fLevel);
2388            _restore_locale(&locale);
2389    
2390          return lscp_client_query(pClient, szQuery);          return lscp_client_query(pClient, szQuery);
2391  }  }
2392    
# Line 2555  lscp_status_t lscp_set_midi_instrument_m Line 2618  lscp_status_t lscp_set_midi_instrument_m
2618  lscp_status_t lscp_map_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr, const char *pszEngineName, const char *pszFileName, int iInstrIndex, float fVolume, lscp_load_mode_t load_mode, const char *pszName )  lscp_status_t lscp_map_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr, const char *pszEngineName, const char *pszFileName, int iInstrIndex, float fVolume, lscp_load_mode_t load_mode, const char *pszName )
2619  {  {
2620          char szQuery[LSCP_BUFSIZ];          char szQuery[LSCP_BUFSIZ];
2621            struct _locale_t locale;
2622    
2623          if (pMidiInstr->map < 0)          if (pMidiInstr->map < 0)
2624                  return LSCP_FAILED;                  return LSCP_FAILED;
# Line 2568  lscp_status_t lscp_map_midi_instrument ( Line 2632  lscp_status_t lscp_map_midi_instrument (
2632          if (fVolume < 0.0f)          if (fVolume < 0.0f)
2633                  fVolume = 1.0f;                  fVolume = 1.0f;
2634    
2635            _save_and_set_c_locale(&locale);
2636          sprintf(szQuery, "MAP MIDI_INSTRUMENT %d %d %d %s '%s' %d %g",          sprintf(szQuery, "MAP MIDI_INSTRUMENT %d %d %d %s '%s' %d %g",
2637                  pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog,                  pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog,
2638                  pszEngineName, pszFileName, iInstrIndex, fVolume);                  pszEngineName, pszFileName, iInstrIndex, fVolume);
2639            _restore_locale(&locale);
2640    
2641          switch (load_mode) {          switch (load_mode) {
2642          case LSCP_LOAD_PERSISTENT:          case LSCP_LOAD_PERSISTENT:
# Line 2727  lscp_midi_instrument_info_t *lscp_get_mi Line 2793  lscp_midi_instrument_info_t *lscp_get_mi
2793          const char *pszCrlf = "\r\n";          const char *pszCrlf = "\r\n";
2794          char *pszToken;          char *pszToken;
2795          char *pch;          char *pch;
2796            struct _locale_t locale;
2797    
2798          if (pClient == NULL)          if (pClient == NULL)
2799                  return NULL;                  return NULL;
# Line 2740  lscp_midi_instrument_info_t *lscp_get_mi Line 2807  lscp_midi_instrument_info_t *lscp_get_mi
2807          // Lock this section up.          // Lock this section up.
2808          lscp_mutex_lock(pClient->mutex);          lscp_mutex_lock(pClient->mutex);
2809    
2810            _save_and_set_c_locale(&locale);
2811    
2812          pInstrInfo = &(pClient->midi_instrument_info);          pInstrInfo = &(pClient->midi_instrument_info);
2813          lscp_midi_instrument_info_reset(pInstrInfo);          lscp_midi_instrument_info_reset(pInstrInfo);
2814    
# Line 2793  lscp_midi_instrument_info_t *lscp_get_mi Line 2862  lscp_midi_instrument_info_t *lscp_get_mi
2862                          else if (strcasecmp(pszToken, "VOLUME") == 0) {                          else if (strcasecmp(pszToken, "VOLUME") == 0) {
2863                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));                                  pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2864                                  if (pszToken)                                  if (pszToken)
2865                                          pInstrInfo->volume = (float) atof(lscp_ltrim(pszToken));                                          pInstrInfo->volume = _atof(lscp_ltrim(pszToken));
2866                          }                          }
2867                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));                          pszToken = lscp_strtok(NULL, pszSeps, &(pch));
2868                  }                  }
2869          }          }
2870          else pInstrInfo = NULL;          else pInstrInfo = NULL;
2871    
2872            _restore_locale(&locale);
2873    
2874          // Unlock this section down.          // Unlock this section down.
2875          lscp_mutex_unlock(pClient->mutex);          lscp_mutex_unlock(pClient->mutex);
2876    

Legend:
Removed from v.1802  
changed lines
  Added in v.1806

  ViewVC Help
Powered by ViewVC