Changeset 679

Show
Ignore:
Timestamp:
12/20/09 13:19:33 (5 years ago)
Author:
erikd
Message:

Allow chosing between output drivers at runtime.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sweep/trunk/src/driver.c

    r677 r679  
    4242 
    4343#include "driver.h" 
     44#include "preferences.h" 
     45#include "pcmio.h" 
     46 
     47#define ARRAY_LEN(x) ((int) (sizeof (x)) / (sizeof (x [0]))) 
    4448 
    4549extern sw_driver * driver_alsa; 
     
    4852extern sw_driver * driver_solaris; 
    4953 
     54static sw_driver * driver_table [10]; 
     55 
    5056/* preferred driver */ 
    5157static sw_driver _driver_null = { 
    52   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
     58  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
    5359}; 
    5460 
    55 static sw_driver * pref = &_driver_null; 
    56  
    57 #include "preferences.h" 
    58 #include "pcmio.h" 
     61/* 
     62** Always keep two pointers to the output diver. current_driver is the 
     63** one that is currently being used and dialog_driver is the one that 
     64** has been chosen on the dialog box. 
     65** This allows dialog_driver to be changed when current_driver is being 
     66** used, with the change from dialog_driver to current_driver occurring 
     67** at the next time the device_open() is called. 
     68*/ 
     69static sw_driver * current_driver = &_driver_null; 
     70static sw_driver * dialog_driver = &_driver_null; 
     71 
     72static char * prefs_driver_key = "driver"; 
    5973 
    6074char * 
     
    6377  GList * names = NULL, * gl; 
    6478 
    65   if (pref->get_names) 
    66     names = pref->get_names (); 
     79  if (dialog_driver->get_names) 
     80    names = dialog_driver->get_names (); 
    6781 
    6882  if ((gl = names) != NULL) { 
     
    7084  } 
    7185 
    72   return NULL
     86  return "Default"
    7387} 
    7488 
     
    7892  GList * names = NULL, * gl; 
    7993 
    80   if (pref->get_names) 
    81     names = pref->get_names (); 
     94  if (dialog_driver->get_names) 
     95    names = dialog_driver->get_names (); 
    8296 
    8397  if ((gl = names) != NULL) { 
     
    87101  } 
    88102 
    89   return NULL
     103  return "Default"
    90104} 
    91105 
     
    95109  char * main_dev; 
    96110 
    97   main_dev = prefs_get_string (pref->primary_device_key); 
     111  main_dev = prefs_get_string (dialog_driver->primary_device_key); 
    98112 
    99113  if (main_dev == NULL) return pcmio_get_default_main_dev(); 
     
    107121  char * monitor_dev; 
    108122 
    109   monitor_dev = prefs_get_string (pref->monitor_device_key); 
     123  monitor_dev = prefs_get_string (dialog_driver->monitor_device_key); 
    110124 
    111125  if (monitor_dev == NULL) return pcmio_get_default_monitor_dev (); 
     
    130144  int * log_frags; 
    131145 
    132   log_frags = prefs_get_int (pref->log_frags_key); 
     146  log_frags = prefs_get_int (dialog_driver->log_frags_key); 
    133147  if (log_frags == NULL) return DEFAULT_LOG_FRAGS; 
    134148  else return (*log_frags); 
     
    137151extern GtkStyle * style_bw; 
    138152static GtkWidget * dialog = NULL; 
     153static GtkWidget * driver_combo; 
    139154static GtkWidget * main_combo; 
    140155static GtkWidget * monitor_combo; 
     
    159174  GtkAdjustment * adj; 
    160175  G_CONST_RETURN gchar * main_dev, * monitor_dev; 
     176  int driver_index; 
     177 
     178  driver_index = gtk_combo_box_get_active (GTK_COMBO_BOX(driver_combo)); 
     179  prefs_set_string (prefs_driver_key, (gchar *)driver_table [driver_index]->name); 
    161180 
    162181  adj = g_object_get_data (G_OBJECT(dialog), "buff_adj"); 
    163182 
    164   prefs_set_int (pref->log_frags_key, adj->value); 
     183  prefs_set_int (dialog_driver->log_frags_key, adj->value); 
    165184 
    166185  main_dev = 
    167186    gtk_entry_get_text (GTK_ENTRY(GTK_COMBO(main_combo)->entry)); 
    168187 
    169   prefs_set_string (pref->primary_device_key, (gchar *)main_dev); 
     188  prefs_set_string (dialog_driver->primary_device_key, (gchar *)main_dev); 
    170189 
    171190  if (monitor_checked (dialog)) { 
    172191    monitor_dev = 
    173       gtk_entry_get_text (GTK_ENTRY(GTK_COMBO(monitor_combo)->entry));  
    174     prefs_set_string (pref->monitor_device_key, (gchar *)monitor_dev); 
     192      gtk_entry_get_text (GTK_ENTRY(GTK_COMBO(monitor_combo)->entry)); 
     193    prefs_set_string (dialog_driver->monitor_device_key, (gchar *)monitor_dev); 
    175194 
    176195    prefs_set_int (USE_MONITOR_KEY, 1); 
     
    183202 
    184203static void 
     204update_pcmio_settings (void) 
     205{ 
     206  gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(main_combo)->entry), 
     207                      pcmio_get_main_dev ()); 
     208 
     209  gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(monitor_combo)->entry), 
     210                      pcmio_get_monitor_dev ()); 
     211 
     212  gtk_adjustment_set_value (GTK_ADJUSTMENT(adj), pcmio_get_log_frags ()); 
     213} 
     214 
     215static void 
    185216config_dev_dsp_dialog_cancel_cb (GtkWidget * widget, gpointer data) 
    186217{ 
    187218  GtkWidget * dialog; 
     219  int k; 
    188220 
    189221  dialog = gtk_widget_get_toplevel (widget); 
    190222  gtk_widget_hide (dialog); 
     223 
     224  dialog_driver = current_driver; 
     225 
     226  for (k = 0 ; driver_table [k] != NULL ; k++) 
     227    if (strcmp (current_driver->name, driver_table [k]->name) == 0) { 
     228          gtk_combo_box_set_active (GTK_COMBO_BOX (driver_combo), k); 
     229      break ; 
     230        } 
     231 
     232  update_pcmio_settings (); 
     233} 
     234 
     235static void 
     236choose_driver_cb (GtkWidget * widget, gpointer data) 
     237{ 
     238  int driver_index; 
     239 
     240  driver_index = gtk_combo_box_get_active (GTK_COMBO_BOX(driver_combo)); 
     241 
     242  /* Change the driver dialog, not the current_dialog. The change in 
     243  ** dialog_driver gets picked up and transfered to current_driver 
     244  ** at the next call to driver_open(). 
     245  */ 
     246  dialog_driver = driver_table [driver_index]; 
     247 
     248  update_pcmio_settings (); 
    191249} 
    192250 
     
    198256  gchar * main_devname, * monitor_devname; 
    199257  gboolean ok = FALSE; 
     258  int driver_index; 
    200259 
    201260  ok_button = 
    202261    GTK_WIDGET(g_object_get_data (G_OBJECT(dialog), "ok_button")); 
     262 
     263  driver_index = gtk_combo_box_get_active (GTK_COMBO_BOX(driver_combo)); 
    203264 
    204265  if (monitor_checked (dialog)) { 
     
    248309  gchar * main_dev, * monitor_dev; 
    249310 
    250   main_dev = 
    251     g_strdup (gtk_entry_get_text (GTK_ENTRY(GTK_COMBO(main_combo)->entry))); 
     311  main_dev = (gchar *)gtk_entry_get_text (GTK_ENTRY(GTK_COMBO(main_combo)->entry)); 
    252312  monitor_dev = (gchar *)gtk_entry_get_text (GTK_ENTRY(GTK_COMBO(monitor_combo)->entry)); 
    253313 
    254314  gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(main_combo)->entry), monitor_dev); 
    255315  gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(monitor_combo)->entry), main_dev); 
    256  
    257   g_free (main_dev); 
    258316 
    259317  set_monitor_widgets (dialog, pcmio_get_use_monitor()); 
     
    326384 
    327385static GtkWidget * 
     386create_drivers_combo (void) 
     387{ 
     388  GtkWidget * combo; 
     389  int k, current=0; 
     390   
     391  combo = gtk_combo_box_new_text (); 
     392 
     393  for (k = 0 ; k < ARRAY_LEN (driver_table) && driver_table [k] ; k++) { 
     394    gtk_combo_box_append_text (GTK_COMBO_BOX (combo), driver_table [k]->name) ; 
     395    if (strcmp (driver_table [k]->name,  dialog_driver->name) == 0) 
     396      current = k; 
     397  } 
     398 
     399  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), current); 
     400 
     401  return combo; 
     402} 
     403 
     404static GtkWidget * 
    328405create_devices_combo (void) 
    329406{ 
     
    331408  GList * cbitems = NULL; 
    332409 
    333   if (pref->get_names) 
    334     cbitems = pref->get_names(); 
     410  if (dialog_driver->get_names) 
     411    cbitems = dialog_driver->get_names(); 
    335412   
    336413  combo = gtk_combo_new (); 
     
    412489    gtk_widget_show (label); 
    413490 
     491    /* Choose driver */ 
     492    hbox = gtk_hbox_new (FALSE, 8); 
     493    gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, TRUE, 8); 
     494    gtk_container_set_border_width (GTK_CONTAINER(hbox), 12); 
     495    gtk_widget_show (hbox); 
     496 
     497    label = gtk_label_new (_("Driver:")); 
     498    gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0); 
     499    gtk_widget_show (label); 
     500 
     501    driver_combo = create_drivers_combo (); 
     502    gtk_box_pack_start (GTK_BOX(hbox), driver_combo, TRUE, TRUE, 0); 
     503    gtk_widget_show (driver_combo); 
     504 
     505    g_signal_connect (GTK_COMBO_BOX(driver_combo), "changed", 
     506                        G_CALLBACK(choose_driver_cb), dialog); 
     507 
     508    g_object_set_data (G_OBJECT (dialog), "driver_combo", driver_combo); 
    414509 
    415510    /* Notebook */ 
    416  
    417511    notebook = gtk_notebook_new (); 
    418512    gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), notebook, 
     
    645739  } 
    646740 
    647   gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(main_combo)->entry), 
    648                       pcmio_get_main_dev ()); 
    649  
    650   gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(monitor_combo)->entry), 
    651                       pcmio_get_monitor_dev ()); 
    652  
    653   gtk_adjustment_set_value (GTK_ADJUSTMENT(adj), pcmio_get_log_frags ()); 
     741  update_pcmio_settings (); 
    654742 
    655743  if (!GTK_WIDGET_VISIBLE(dialog)) { 
     
    663751device_open (int cueing, int flags) 
    664752{ 
    665   if (pref->open) 
    666     return pref->open (cueing, flags); 
     753  current_driver = dialog_driver; 
     754 
     755  if (current_driver->open) 
     756    return current_driver->open (cueing, flags); 
    667757  else 
    668758    return NULL; 
     
    672762device_setup (sw_handle * handle, sw_format * format) 
    673763{ 
    674   if (pref->setup) 
    675     pref->setup (handle, format); 
     764  if (current_driver->setup) 
     765    current_driver->setup (handle, format); 
    676766} 
    677767 
     
    679769device_wait (sw_handle * handle) 
    680770{ 
    681   if (pref->wait) 
    682     return pref->wait (handle); 
     771  if (current_driver->wait) 
     772    return current_driver->wait (handle); 
    683773  else 
    684774    return 0; 
     
    688778device_read (sw_handle * handle, sw_audio_t * buf, size_t count) 
    689779{ 
    690   if (pref->read) 
    691     return pref->read (handle, buf, count); 
     780  if (current_driver->read) 
     781    return current_driver->read (handle, buf, count); 
    692782  else 
    693783    return -1; 
     
    702792#endif 
    703793 
    704   if (pref->write) 
    705     return pref->write (handle, buf, count, offset); 
     794  if (current_driver->write) 
     795    return current_driver->write (handle, buf, count, offset); 
    706796  else 
    707797    return -1; 
     
    711801device_offset (sw_handle * handle) 
    712802{ 
    713   if (pref->offset) 
    714     return pref->offset (handle); 
     803  if (current_driver->offset) 
     804    return current_driver->offset (handle); 
    715805  else 
    716806    return -1; 
     
    720810device_reset (sw_handle * handle) 
    721811{ 
    722   if (pref->reset) 
    723     pref->reset (handle); 
     812  if (current_driver->reset) 
     813    current_driver->reset (handle); 
    724814} 
    725815 
     
    727817device_flush (sw_handle * handle) 
    728818{ 
    729   if (pref->flush) 
    730     pref->flush (handle); 
     819  if (current_driver->flush) 
     820    current_driver->flush (handle); 
    731821} 
    732822 
     
    734824device_drain (sw_handle * handle) 
    735825{ 
    736   if (pref->drain) 
    737     pref->drain (handle); 
     826  if (current_driver->drain) 
     827    current_driver->drain (handle); 
    738828} 
    739829 
     
    741831device_close (sw_handle * handle) 
    742832{ 
    743   if (pref->close) 
    744     pref->close (handle); 
     833  if (current_driver->close) 
     834    current_driver->close (handle); 
    745835 
    746836  handle->driver_fd = -1; 
     
    750840init_devices (void) 
    751841{ 
    752 #if defined(DRIVER_ALSA) 
    753   pref = driver_alsa; 
    754 #elif defined(DRIVER_OSS) 
    755   pref = driver_oss; 
    756 #elif defined(DRIVER_PULSEAUDIO) 
    757   pref = driver_pulseaudio; 
    758 #elif defined(DRIVER_SOLARIS_AUDIO) 
    759   pref = driver_solaris; 
    760 #endif 
    761 
     842  const char * driver; 
     843  int k = 0; 
     844 
     845  memset (driver_table, 0, sizeof (driver_table)); 
     846 
     847  /* Populate the driver table, with all valid drivers (name field is non-NULL). */ 
     848  if (driver_alsa->name != NULL) 
     849    driver_table [k++] = driver_alsa; 
     850  if (driver_oss->name != NULL) 
     851    driver_table [k++] = driver_oss; 
     852  if (driver_pulseaudio->name != NULL) 
     853    driver_table [k++] = driver_pulseaudio; 
     854  if (driver_solaris->name != NULL) 
     855    driver_table [k++] = driver_solaris; 
     856 
     857  driver = prefs_get_string (prefs_driver_key); 
     858 
     859  /* Switch to driver from preferences if possible. */ 
     860  if (driver != NULL) 
     861    for (k = 0 ; driver_table [k] != NULL ; k++) 
     862      if (strcmp (driver, driver_table [k]->name) == 0) { 
     863        current_driver = driver_table [k]; 
     864                dialog_driver = current_driver; 
     865      } 
     866