Remember sound devices

This commit is contained in:
Ilari Liusvaara 2014-10-19 02:37:55 +03:00
parent 25d4f27a9b
commit a00d357bef
3 changed files with 54 additions and 22 deletions

View file

@ -133,6 +133,14 @@ struct platform
* Set sound device. * Set sound device.
*/ */
static void set_sound_device(const std::string& pdev, const std::string& rdev) throw(); static void set_sound_device(const std::string& pdev, const std::string& rdev) throw();
/**
* Set sound device by description.
*/
static void set_sound_device_by_description(const std::string& pdev, const std::string& rdev) throw();
/**
* Get sound device description.
*/
static std::string get_sound_device_description(bool rec) throw(std::bad_alloc);
/** /**
* Show error message dialog after UI thread becomes free. * Show error message dialog after UI thread becomes free.
* *

View file

@ -138,33 +138,16 @@ namespace
"Syntax: reset-audio\nResets the audio driver.\n", "Syntax: reset-audio\nResets the audio driver.\n",
[]() throw(std::bad_alloc, std::runtime_error) { []() throw(std::bad_alloc, std::runtime_error) {
//Save the old devices. We save descriptions if possible, since handles change. //Save the old devices. We save descriptions if possible, since handles change.
auto pdevs = audioapi_driver_get_devices(false); auto cpdev = platform::get_sound_device_description(false);
auto rdevs = audioapi_driver_get_devices(true); auto crdev = platform::get_sound_device_description(true);
auto cpdev = audioapi_driver_get_device(false);
auto crdev = audioapi_driver_get_device(true);
cpdev = pdevs.count(cpdev) ? pdevs[cpdev] : std::string("");
crdev = rdevs.count(crdev) ? rdevs[crdev] : std::string("");
//Restart the system. //Restart the system.
audioapi_driver_quit(); audioapi_driver_quit();
audioapi_driver_init(); audioapi_driver_init();
//Try to find the devices again.
pdevs = audioapi_driver_get_devices(false);
rdevs = audioapi_driver_get_devices(true);
for(auto i : pdevs)
if(i.second == cpdev) {
cpdev = i.first;
break;
}
for(auto i : rdevs)
if(i.second == crdev) {
crdev = i.first;
break;
}
//Defaults. //Defaults.
if(cpdev == "") cpdev = audioapi_driver_get_device(false); if(cpdev == "") cpdev = platform::get_sound_device_description(false);
if(crdev == "") crdev = audioapi_driver_get_device(true); if(crdev == "") crdev = platform::get_sound_device_description(true);
//Try to change device back. //Try to change device back.
platform::set_sound_device(cpdev, crdev); platform::set_sound_device_by_description(cpdev, crdev);
}); });
keyboard::invbind_info ienable_sound(lsnes_invbinds, "enable-sound on", "Sound‣Enable"); keyboard::invbind_info ienable_sound(lsnes_invbinds, "enable-sound on", "Sound‣Enable");
@ -250,6 +233,36 @@ void platform::set_sound_device(const std::string& pdev, const std::string& rdev
audioapi_driver_get_device(false))); audioapi_driver_get_device(false)));
} }
void platform::set_sound_device_by_description(const std::string& pdev, const std::string& rdev) throw()
{
std::string old_play = audioapi_driver_get_device(false);
std::string old_rec = audioapi_driver_get_device(true);
auto pdevs = audioapi_driver_get_devices(false);
auto rdevs = audioapi_driver_get_devices(true);
for(auto i : pdevs)
if(i.second == pdev) {
old_play = i.first;
break;
}
for(auto i : rdevs)
if(i.second == rdev) {
old_rec = i.first;
break;
}
set_sound_device(old_play, old_rec);
}
std::string platform::get_sound_device_description(bool rec) throw(std::bad_alloc)
{
auto dev = audioapi_driver_get_device(rec);
auto devs = audioapi_driver_get_devices(rec);
if(devs.count(dev))
return devs[dev];
else
return "";
}
bool platform::is_sound_enabled() throw() bool platform::is_sound_enabled() throw()
{ {
return sounds_enabled; return sounds_enabled;

View file

@ -162,6 +162,9 @@ end:
wxPostEvent(ui_services, uic); wxPostEvent(ui_services, uic);
} }
std::string loaded_pdev;
std::string loaded_rdev;
void handle_config_line(std::string line) void handle_config_line(std::string line)
{ {
regex_results r; regex_results r;
@ -198,6 +201,10 @@ end:
core_selections[r[1]] = r[2]; core_selections[r[1]] = r[2];
messages << "Prefer " << r[2] << " for " << r[1] << std::endl; messages << "Prefer " << r[2] << " for " << r[1] << std::endl;
} }
} else if(r = regex("AUDIO_PDEV[ \t]+(.*)", line)) {
loaded_pdev = r[1];
} else if(r = regex("AUDIO_RDEV[ \t]+(.*)", line)) {
loaded_rdev = r[1];
} else } else
(stringfmt() << "Unrecognized directive: " << line).throwex(); (stringfmt() << "Unrecognized directive: " << line).throwex();
} }
@ -216,6 +223,7 @@ end:
} }
lineno++; lineno++;
} }
platform::set_sound_device_by_description(loaded_pdev, loaded_rdev);
(*lsnes_instance.abindmanager)(); (*lsnes_instance.abindmanager)();
lsnes_uri_rewrite.load(get_config_path() + "/lsnesurirewrite.cfg"); lsnes_uri_rewrite.load(get_config_path() + "/lsnesurirewrite.cfg");
} }
@ -259,6 +267,9 @@ end:
for(auto i : core_selections) for(auto i : core_selections)
if(i.second != "") if(i.second != "")
cfgfile << "PREFER " << i.first << " " << i.second << std::endl; cfgfile << "PREFER " << i.first << " " << i.second << std::endl;
//Sound config.
cfgfile << "AUDIO_PDEV " << platform::get_sound_device_description(false) << std::endl;
cfgfile << "AUDIO_RDEV " << platform::get_sound_device_description(true) << std::endl;
if(!cfgfile) { if(!cfgfile) {
show_message_ok(NULL, "Error Saving configuration", "Error saving configuration", show_message_ok(NULL, "Error Saving configuration", "Error saving configuration",
wxICON_EXCLAMATION); wxICON_EXCLAMATION);