PageRenderTime 53ms CodeModel.GetById 28ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llpanelvoicedevicesettings.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 329 lines | 235 code | 49 blank | 45 comment | 32 complexity | 6f841034e13a3799388bde345688ae9e MD5 | raw file
  1/** 
  2 * @file llpanelvoicedevicesettings.cpp
  3 * @author Richard Nelson
  4 * @brief Voice communication set-up 
  5 *
  6 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27 
 28#include "llviewerprecompiledheaders.h"
 29
 30#include "llpanelvoicedevicesettings.h"
 31
 32// Viewer includes
 33#include "llcombobox.h"
 34#include "llsliderctrl.h"
 35#include "llviewercontrol.h"
 36#include "llvoiceclient.h"
 37#include "llvoicechannel.h"
 38
 39// Library includes (after viewer)
 40#include "lluictrlfactory.h"
 41
 42
 43static LLRegisterPanelClassWrapper<LLPanelVoiceDeviceSettings> t_panel_group_general("panel_voice_device_settings");
 44static const std::string DEFAULT_DEVICE("Default");
 45
 46
 47LLPanelVoiceDeviceSettings::LLPanelVoiceDeviceSettings()
 48	: LLPanel()
 49{
 50	mCtrlInputDevices = NULL;
 51	mCtrlOutputDevices = NULL;
 52	mInputDevice = gSavedSettings.getString("VoiceInputAudioDevice");
 53	mOutputDevice = gSavedSettings.getString("VoiceOutputAudioDevice");
 54	mDevicesUpdated = FALSE;
 55	mUseTuningMode = true;
 56
 57	// grab "live" mic volume level
 58	mMicVolume = gSavedSettings.getF32("AudioLevelMic");
 59
 60}
 61
 62LLPanelVoiceDeviceSettings::~LLPanelVoiceDeviceSettings()
 63{
 64}
 65
 66BOOL LLPanelVoiceDeviceSettings::postBuild()
 67{
 68	LLSlider* volume_slider = getChild<LLSlider>("mic_volume_slider");
 69	// set mic volume tuning slider based on last mic volume setting
 70	volume_slider->setValue(mMicVolume);
 71
 72	mCtrlInputDevices = getChild<LLComboBox>("voice_input_device");
 73	mCtrlOutputDevices = getChild<LLComboBox>("voice_output_device");
 74
 75	mCtrlInputDevices->setCommitCallback(
 76		boost::bind(&LLPanelVoiceDeviceSettings::onCommitInputDevice, this));
 77	mCtrlOutputDevices->setCommitCallback(
 78		boost::bind(&LLPanelVoiceDeviceSettings::onCommitOutputDevice, this));
 79
 80	mLocalizedDeviceNames[DEFAULT_DEVICE]				= getString("default_text");
 81	mLocalizedDeviceNames["No Device"]					= getString("name_no_device");
 82	mLocalizedDeviceNames["Default System Device"]		= getString("name_default_system_device");
 83	
 84	return TRUE;
 85}
 86
 87// virtual
 88void LLPanelVoiceDeviceSettings::handleVisibilityChange ( BOOL new_visibility )
 89{
 90	if (new_visibility)
 91	{
 92		initialize();	
 93	}
 94	else
 95	{
 96		cleanup();
 97		// when closing this window, turn of visiblity control so that 
 98		// next time preferences is opened we don't suspend voice
 99		gSavedSettings.setBOOL("ShowDeviceSettings", FALSE);
100	}
101}
102void LLPanelVoiceDeviceSettings::draw()
103{
104	refresh();
105
106	// let user know that volume indicator is not yet available
107	bool is_in_tuning_mode = LLVoiceClient::getInstance()->inTuningMode();
108	getChildView("wait_text")->setVisible( !is_in_tuning_mode && mUseTuningMode);
109
110	LLPanel::draw();
111
112	if (is_in_tuning_mode)
113	{
114		const S32 num_bars = 5;
115		F32 voice_power = LLVoiceClient::getInstance()->tuningGetEnergy() / LLVoiceClient::OVERDRIVEN_POWER_LEVEL;
116		S32 discrete_power = llmin(num_bars, llfloor(voice_power * (F32)num_bars + 0.1f));
117
118		for(S32 power_bar_idx = 0; power_bar_idx < num_bars; power_bar_idx++)
119		{
120			std::string view_name = llformat("%s%d", "bar", power_bar_idx);
121			LLView* bar_view = getChild<LLView>(view_name);
122			if (bar_view)
123			{
124				gl_rect_2d(bar_view->getRect(), LLColor4::grey, TRUE);
125
126				LLColor4 color;
127				if (power_bar_idx < discrete_power)
128				{
129					color = (power_bar_idx >= 3) ? LLUIColorTable::instance().getColor("OverdrivenColor") : LLUIColorTable::instance().getColor("SpeakingColor");
130				}
131				else
132				{
133					color = LLUIColorTable::instance().getColor("PanelFocusBackgroundColor");
134				}
135
136				LLRect color_rect = bar_view->getRect();
137				color_rect.stretch(-1);
138				gl_rect_2d(color_rect, color, TRUE);
139			}
140		}
141	}
142}
143
144void LLPanelVoiceDeviceSettings::apply()
145{
146	std::string s;
147	if(mCtrlInputDevices)
148	{
149		s = mCtrlInputDevices->getValue().asString();
150		gSavedSettings.setString("VoiceInputAudioDevice", s);
151		mInputDevice = s;
152	}
153
154	if(mCtrlOutputDevices)
155	{
156		s = mCtrlOutputDevices->getValue().asString();
157		gSavedSettings.setString("VoiceOutputAudioDevice", s);
158		mOutputDevice = s;
159	}
160
161	// assume we are being destroyed by closing our embedding window
162	LLSlider* volume_slider = getChild<LLSlider>("mic_volume_slider");
163	if(volume_slider)
164	{
165		F32 slider_value = (F32)volume_slider->getValue().asReal();
166		gSavedSettings.setF32("AudioLevelMic", slider_value);
167		mMicVolume = slider_value;
168	}
169}
170
171void LLPanelVoiceDeviceSettings::cancel()
172{
173	gSavedSettings.setString("VoiceInputAudioDevice", mInputDevice);
174	gSavedSettings.setString("VoiceOutputAudioDevice", mOutputDevice);
175
176	if(mCtrlInputDevices)
177		mCtrlInputDevices->setValue(mInputDevice);
178
179	if(mCtrlOutputDevices)
180		mCtrlOutputDevices->setValue(mOutputDevice);
181
182	gSavedSettings.setF32("AudioLevelMic", mMicVolume);
183	LLSlider* volume_slider = getChild<LLSlider>("mic_volume_slider");
184	if(volume_slider)
185	{
186		volume_slider->setValue(mMicVolume);
187	}
188}
189
190void LLPanelVoiceDeviceSettings::refresh()
191{
192	//grab current volume
193	LLSlider* volume_slider = getChild<LLSlider>("mic_volume_slider");
194	// set mic volume tuning slider based on last mic volume setting
195	F32 current_volume = (F32)volume_slider->getValue().asReal();
196	LLVoiceClient::getInstance()->tuningSetMicVolume(current_volume);
197
198	// Fill in popup menus
199	bool device_settings_available = LLVoiceClient::getInstance()->deviceSettingsAvailable();
200
201	if (mCtrlInputDevices)
202	{
203		mCtrlInputDevices->setEnabled(device_settings_available);
204	}
205
206	if (mCtrlOutputDevices)
207	{
208		mCtrlOutputDevices->setEnabled(device_settings_available);
209	}
210
211	getChild<LLSlider>("mic_volume_slider")->setEnabled(device_settings_available);
212
213	if(!device_settings_available)
214	{
215		// The combo boxes are disabled, since we can't get the device settings from the daemon just now.
216		// Put the currently set default (ONLY) in the box, and select it.
217		if(mCtrlInputDevices)
218		{
219			mCtrlInputDevices->removeall();
220			mCtrlInputDevices->add(getLocalizedDeviceName(mInputDevice), mInputDevice, ADD_BOTTOM);
221			mCtrlInputDevices->setValue(mInputDevice);
222		}
223		if(mCtrlOutputDevices)
224		{
225			mCtrlOutputDevices->removeall();
226			mCtrlOutputDevices->add(getLocalizedDeviceName(mOutputDevice), mOutputDevice, ADD_BOTTOM);
227			mCtrlOutputDevices->setValue(mOutputDevice);
228		}
229		mDevicesUpdated = FALSE;
230	}
231	else if (!mDevicesUpdated)
232	{
233		LLVoiceDeviceList::const_iterator iter;
234		
235		if(mCtrlInputDevices)
236		{
237			mCtrlInputDevices->removeall();
238			mCtrlInputDevices->add(getLocalizedDeviceName(DEFAULT_DEVICE), DEFAULT_DEVICE, ADD_BOTTOM);
239
240			for(iter=LLVoiceClient::getInstance()->getCaptureDevices().begin(); 
241				iter != LLVoiceClient::getInstance()->getCaptureDevices().end();
242				iter++)
243			{
244				mCtrlInputDevices->add(getLocalizedDeviceName(*iter), *iter, ADD_BOTTOM);
245			}
246
247			// Fix invalid input audio device preference.
248			if (!mCtrlInputDevices->setSelectedByValue(mInputDevice, TRUE))
249			{
250				mCtrlInputDevices->setValue(DEFAULT_DEVICE);
251				gSavedSettings.setString("VoiceInputAudioDevice", DEFAULT_DEVICE);
252				mInputDevice = DEFAULT_DEVICE;
253			}
254		}
255		
256		if(mCtrlOutputDevices)
257		{
258			mCtrlOutputDevices->removeall();
259			mCtrlOutputDevices->add(getLocalizedDeviceName(DEFAULT_DEVICE), DEFAULT_DEVICE, ADD_BOTTOM);
260
261			for(iter= LLVoiceClient::getInstance()->getRenderDevices().begin(); 
262				iter !=  LLVoiceClient::getInstance()->getRenderDevices().end(); iter++)
263			{
264				mCtrlOutputDevices->add(getLocalizedDeviceName(*iter), *iter, ADD_BOTTOM);
265			}
266
267			// Fix invalid output audio device preference.
268			if (!mCtrlOutputDevices->setSelectedByValue(mOutputDevice, TRUE))
269			{
270				mCtrlOutputDevices->setValue(DEFAULT_DEVICE);
271				gSavedSettings.setString("VoiceOutputAudioDevice", DEFAULT_DEVICE);
272				mOutputDevice = DEFAULT_DEVICE;
273			}
274		}
275		mDevicesUpdated = TRUE;
276	}	
277}
278
279void LLPanelVoiceDeviceSettings::initialize()
280{
281	mInputDevice = gSavedSettings.getString("VoiceInputAudioDevice");
282	mOutputDevice = gSavedSettings.getString("VoiceOutputAudioDevice");
283	mMicVolume = gSavedSettings.getF32("AudioLevelMic");
284	mDevicesUpdated = FALSE;
285
286	// ask for new device enumeration
287	LLVoiceClient::getInstance()->refreshDeviceLists();
288
289	// put voice client in "tuning" mode
290	if (mUseTuningMode)
291	{
292		LLVoiceClient::getInstance()->tuningStart();
293		LLVoiceChannel::suspend();
294	}
295}
296
297void LLPanelVoiceDeviceSettings::cleanup()
298{
299	if (mUseTuningMode)
300	{
301		LLVoiceClient::getInstance()->tuningStop();
302		LLVoiceChannel::resume();
303	}
304}
305
306// returns English name if no translation found
307std::string LLPanelVoiceDeviceSettings::getLocalizedDeviceName(const std::string& en_dev_name)
308{
309	std::map<std::string, std::string>::const_iterator it = mLocalizedDeviceNames.find(en_dev_name);
310	return it != mLocalizedDeviceNames.end() ? it->second : en_dev_name;
311}
312
313void LLPanelVoiceDeviceSettings::onCommitInputDevice()
314{
315	if(LLVoiceClient::getInstance())
316	{
317		LLVoiceClient::getInstance()->setCaptureDevice(
318			mCtrlInputDevices->getValue().asString());
319	}
320}
321
322void LLPanelVoiceDeviceSettings::onCommitOutputDevice()
323{
324	if(LLVoiceClient::getInstance())
325	{
326		LLVoiceClient::getInstance()->setRenderDevice(
327			mCtrlInputDevices->getValue().asString());
328	}
329}