void LLViewerJoystick::scanJoystick()
{
	if (mDriverState != JDS_INITIALIZED || !gSavedSettings.getBOOL("JoystickEnabled"))
	{
		return;
	}

#if LL_WINDOWS
	// On windows, the flycam is updated syncronously with a timer, so there is
	// no need to update the status of the joystick here.
	if (!mOverrideCamera)
#endif
	updateStatus();

	static long toggle_flycam = 0;

	if (mBtn[0] == 1)
    {
		if (mBtn[0] != toggle_flycam)
		{
			toggle_flycam = toggleFlycam() ? 1 : 0;
		}
	}
	else
	{
		toggle_flycam = 0;
	}
	
	if (!mOverrideCamera && !(LLToolMgr::getInstance()->inBuildMode() && gSavedSettings.getBOOL("JoystickBuildEnabled")))
	{
		moveAvatar();
	}
}
void LLViewerJoystick::scanJoystick()
{
	if (mDriverState != JDS_INITIALIZED || !gSavedSettings.getBOOL("JoystickEnabled"))
	{
		return;
	}

#if LL_WINDOWS
	// On windows, the flycam is updated syncronously with a timer, so there is
	// no need to update the status of the joystick here.
	if (!mOverrideCamera)
#endif
	updateStatus();

	static long toggle_flycam = 0;
	static bool toggle_cursor = false;

	// <Singu> Xbox 360 support
	if (sType == XBOX || sType == DS3)
	{
		bool ds3 = sType == DS3;
		// Special command keys ...
		// - Back = toggle flycam
		U8 key = ds3 ? (U8)DS3_SELECT_KEY : (U8)XBOX_BACK_KEY;
		if (mBtn[key] == 1)
		{
			if (!toggle_flycam) toggle_flycam = toggleFlycam();
		}
		else
		{
			toggle_flycam = false;
		}

		// - Start = toggle cursor/camera control
		key = ds3 ? (U8)DS3_START_KEY : (U8)XBOX_START_KEY;
		if (mBtn[key] == 1)
		{
			if (!toggle_cursor) toggle_cursor = toggleCursor();
		}
		else
		{
			toggle_cursor = false;
		}

		// Toggle mouselook ...
		static bool right_stick_click_down = false;
		key = ds3 ? (U8)DS3_R_STICK_CLICK : (U8)XBOX_R_STICK_CLICK;
		if (!!mBtn[key] != right_stick_click_down)
		{
			if (right_stick_click_down = mBtn[key]) // Note: Setting, not comparing.
				gAgentCamera.cameraMouselook() ? gAgentCamera.changeCameraToDefault() : gAgentCamera.changeCameraToMouselook();
		}

		MASK mask = gKeyboard->currentMask(TRUE);
		// Esc
		static bool esc_down = false;
		key = ds3 ? (U8)DS3_TRIANGLE_KEY : (U8)XBOX_Y_KEY;
		if (!!mBtn[key] != esc_down)
		{
			esc_down = mBtn[key];
			(gKeyboard->*(esc_down ? &LLKeyboard::handleTranslatedKeyDown : &LLKeyboard::handleTranslatedKeyDown))(KEY_ESCAPE, mask);
		}

		// Alt
		static bool alt_down = false;
		key = ds3 ? (U8)DS3_X_KEY : (U8)XBOX_A_KEY;
		if (!!mBtn[key] != alt_down)
		{
			gKeyboard->setControllerKey(KEY_ALT, alt_down = mBtn[key]);
		}

		// Ctrl
		static bool ctrl_down = false;
		key = ds3 ? (U8)DS3_SQUARE_KEY : (U8)XBOX_X_KEY;
		if (!!mBtn[key] != ctrl_down)
		{
			gKeyboard->setControllerKey(KEY_CONTROL, ctrl_down = mBtn[key]);
		}

		// Shift
		static bool shift_down = false;
		key = ds3 ? (U8)DS3_CIRCLE_KEY : (U8)XBOX_B_KEY;
		if (!!mBtn[key] != shift_down)
		{
			gKeyboard->setControllerKey(KEY_SHIFT, shift_down = mBtn[key]);
		}

		// Mouse clicks ...
		LLCoordGL coord;
		LLUI::getMousePositionScreen(&coord.mX, &coord.mY);
		static bool m1_down = false;
		static F32 last_m1 = 0;
		key = ds3 ? (U8)DS3_L1_KEY : (U8)XBOX_L_BUMP_KEY;
		if (!!mBtn[key] != m1_down)
		{
			m1_down = mBtn[key];
			(gViewerWindow->*(m1_down ? &LLViewerWindow::handleMouseDown : &LLViewerWindow::handleMouseUp))(gViewerWindow->getWindow(), coord, mask);
			if (m1_down && gFrameTimeSeconds-last_m1 == 0.5f)
				gViewerWindow->handleDoubleClick(gViewerWindow->getWindow(), coord, mask);
			last_m1 = gFrameTimeSeconds;
		}
		static bool m2_down = false;
		key = ds3 ? (U8)DS3_R1_KEY : (U8)XBOX_R_BUMP_KEY;
		if (!!mBtn[key] != m2_down)
		{
			m2_down = mBtn[key];
			(gViewerWindow->*(m2_down ? &LLViewerWindow::handleRightMouseDown : &LLViewerWindow::handleRightMouseUp))(gViewerWindow->getWindow(), coord, mask);
		}

		if (ds3) // Yay bonus keys~
		{
			static bool sit_down = false;
			if (!!mBtn[DS3_LOGO_KEY] != sit_down)
			{
				if (sit_down = mBtn[DS3_LOGO_KEY])
					(gAgentAvatarp && gAgentAvatarp->isSitting()) ? gAgent.standUp() : gAgent.sitDown();
			}
			/* Singu TODO: What should these be?
			DS3_L2_KEY
			DS3_R2_KEY
			*/
		}
	}
	else
	// </Singu>
	{
		if (mBtn[0] == 1)
		{
			if (mBtn[0] != toggle_flycam)
			{
				toggle_flycam = toggleFlycam() ? 1 : 0;
			}
		}
		else
		{
			toggle_flycam = 0;
		}
	}
	
	if (sControlCursor || (!mOverrideCamera && !(LLToolMgr::getInstance()->inBuildMode() && gSavedSettings.getBOOL("JoystickBuildEnabled"))))
	{
		moveAvatar();
	}
}