Changeset 5929:e3e67f000dce in roaraudio for roarclients


Ignore:
Timestamp:
09/11/13 14:56:42 (11 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

improve inner structure

File:
1 edited

Legend:

Unmodified
Added
Removed
  • roarclients/roarlight.c

    r5823 r5929  
    2929#define CONVAR struct roar_connection * con 
    3030 
     31static struct roar_connection * g_connection; 
     32static struct roar_vio_calls  * g_stream; 
     33 
     34static int __run_argv(int argc, char * argv[]); 
     35 
    3136void usage (void) { 
    3237 printf("roarlight [OPTIONS]... command [command...]\n"); 
     
    4752} 
    4853 
    49 int cmd_set (CONVAR, char * arg, const int mixer) { 
     54int cmd_set (char * arg) { 
    5055 char * next = arg; 
    5156 char * k, * v; 
    5257 int32_t chan, val; 
    5358 struct roar_roardmx_message mes; 
    54  struct roar_vio_calls vio; 
    5559 
    5660 roar_roardmx_message_new_sset(&mes); 
     
    7983 } 
    8084 
    81  if ( roar_vio_simple_new_stream_obj(&vio, con, NULL, 
    82                                      ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT, 
    83                                      ROAR_CODEC_ROARDMX, ROAR_DIR_LIGHT_IN, mixer) == -1 ) 
    84   return -1; 
    85  
    86  if ( roar_roardmx_message_send(&mes, &vio) == -1 ) { 
    87   roar_vio_close(&vio); 
     85 if ( roar_roardmx_message_send(&mes, g_stream) == -1 ) { 
    8886  return -1; 
    8987 } 
    9088 
    91  roar_vio_close(&vio); 
     89 return 0; 
     90} 
     91 
     92static int __run_argv(int argc, char * argv[]) { 
     93 char * k; 
     94 int i; 
     95 
     96 for (i = 0; i < argc; i++) { 
     97  k = argv[i]; 
     98  // cmd is in k 
     99 
     100  printf("--- [ %s ] ---\n", k); 
     101 
     102  if ( !strcmp(k, "help") ) { 
     103   usage(); 
     104 
     105  } else if ( !strcmp(k, "sleep") ) { 
     106   roar_sleep(atoi(argv[++i])); 
     107 
     108  } else if ( !strcmp(k, "set") ) { 
     109   i++; 
     110   if ( cmd_set(argv[i]) == -1 ) { 
     111    fprintf(stderr, "Error: can not set channels\n"); 
     112   } else { 
     113    printf("channels changed\n"); 
     114   } 
     115 
     116  } else { 
     117   fprintf(stderr, "Error: invalid command: %s\n", k); 
     118   return 1; 
     119  } 
     120 } 
    92121 
    93122 return 0; 
     
    96125int main (int argc, char * argv[]) { 
    97126 struct roar_connection con; 
     127 struct roar_vio_calls vio; 
    98128 int    mixer = -1; // -1 = Default 
    99129 const char * server   = NULL; 
    100130 const char * k; 
    101131 int    i; 
     132 int    ret; 
    102133 
    103134 for (i = 1; i < argc; i++) { 
     
    122153 } 
    123154 
     155 if ( i == argc ) { 
     156  fprintf(stderr, "Error: No Commands given\n"); 
     157  return 0; // this is not a fatal error... 
     158 } 
     159 
    124160 if ( roar_simple_connect(&con, server, "roarlight") == -1 ) { 
    125161  fprintf(stderr, "Error: Can not connect to server\n"); 
     
    127163 } 
    128164 
    129  if ( i == argc ) { 
    130   fprintf(stderr, "Error: No Commands given\n"); 
    131   return 0; // this is not a fatal error... 
     165 if ( roar_vio_simple_new_stream_obj(&vio, &con, NULL, 
     166                                     ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT, 
     167                                     ROAR_CODEC_ROARDMX, ROAR_DIR_LIGHT_IN, mixer) == -1 ) { 
    132168 } 
    133169 
    134  for (; i < argc; i++) { 
    135   k = argv[i]; 
    136   // cmd is in k 
     170 g_connection = &con; 
     171 g_stream = &vio; 
    137172 
    138   printf("--- [ %s ] ---\n", k); 
     173 ret = __run_argv(argc - i, argv + i); 
    139174 
    140   if ( !strcmp(k, "help") ) { 
    141    usage(); 
     175 // try to flush all data: 
    142176 
    143   } else if ( !strcmp(k, "sleep") ) { 
    144    roar_sleep(atoi(argv[++i])); 
    145  
    146   } else if ( !strcmp(k, "set") ) { 
    147    i++; 
    148    if ( cmd_set(&con, argv[i], mixer) == -1 ) { 
    149     fprintf(stderr, "Error: can not set channels\n"); 
    150    } else { 
    151     printf("channels changed\n"); 
    152    } 
    153  
    154   } else { 
    155    fprintf(stderr, "Error: invalid command: %s\n", k); 
    156   } 
    157  } 
     177 roar_vio_sync(&vio); 
     178 roar_vio_close(&vio); 
     179 roar_usleep(100); 
    158180 
    159181 roar_disconnect(&con); 
    160182 
    161  return 0; 
     183 return ret; 
    162184} 
    163185 
Note: See TracChangeset for help on using the changeset viewer.