diff options
Diffstat (limited to '')
168 files changed, 11380 insertions, 1484 deletions
@@ -6,8 +6,5 @@ extraction: packages: - "libsdl2-dev" - "qtmultimedia5-dev" - - "clang-format-10" - "libtbb-dev" - "libjack-jackd2-dev" - - "doxygen" - - "graphviz" diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f3c59c5d..45bd03a65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,7 +210,7 @@ if(ENABLE_QT) set(QT_PREFIX_HINT) if(YUZU_USE_BUNDLED_QT) if ((MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS 1930) AND ARCHITECTURE_x86_64) - set(QT_VER qt-5.12.0-msvc2017_64) + set(QT_VER qt-5.12.8-msvc2017_64) else() message(FATAL_ERROR "No bundled Qt binaries for your toolchain. Disable YUZU_USE_BUNDLED_QT and provide your own.") endif() diff --git a/dist/icons/controller/applet_dual_joycon.png b/dist/icons/controller/applet_dual_joycon.png Binary files differnew file mode 100644 index 000000000..32e0a04ae --- /dev/null +++ b/dist/icons/controller/applet_dual_joycon.png diff --git a/dist/icons/controller/applet_dual_joycon_dark.png b/dist/icons/controller/applet_dual_joycon_dark.png Binary files differnew file mode 100644 index 000000000..6adc66356 --- /dev/null +++ b/dist/icons/controller/applet_dual_joycon_dark.png diff --git a/dist/icons/controller/applet_dual_joycon_dark_disabled.png b/dist/icons/controller/applet_dual_joycon_dark_disabled.png Binary files differnew file mode 100644 index 000000000..208603ee7 --- /dev/null +++ b/dist/icons/controller/applet_dual_joycon_dark_disabled.png diff --git a/dist/icons/controller/applet_dual_joycon_disabled.png b/dist/icons/controller/applet_dual_joycon_disabled.png Binary files differnew file mode 100644 index 000000000..43950618d --- /dev/null +++ b/dist/icons/controller/applet_dual_joycon_disabled.png diff --git a/dist/icons/controller/applet_dual_joycon_midnight.png b/dist/icons/controller/applet_dual_joycon_midnight.png Binary files differnew file mode 100644 index 000000000..c7edea8a3 --- /dev/null +++ b/dist/icons/controller/applet_dual_joycon_midnight.png diff --git a/dist/icons/controller/applet_dual_joycon_midnight_disabled.png b/dist/icons/controller/applet_dual_joycon_midnight_disabled.png Binary files differnew file mode 100644 index 000000000..ee1aafc85 --- /dev/null +++ b/dist/icons/controller/applet_dual_joycon_midnight_disabled.png diff --git a/dist/icons/controller/applet_handheld.png b/dist/icons/controller/applet_handheld.png Binary files differnew file mode 100644 index 000000000..7f8dd2227 --- /dev/null +++ b/dist/icons/controller/applet_handheld.png diff --git a/dist/icons/controller/applet_handheld_dark.png b/dist/icons/controller/applet_handheld_dark.png Binary files differnew file mode 100644 index 000000000..41f6d7aea --- /dev/null +++ b/dist/icons/controller/applet_handheld_dark.png diff --git a/dist/icons/controller/applet_handheld_dark_disabled.png b/dist/icons/controller/applet_handheld_dark_disabled.png Binary files differnew file mode 100644 index 000000000..5a136ddd1 --- /dev/null +++ b/dist/icons/controller/applet_handheld_dark_disabled.png diff --git a/dist/icons/controller/applet_handheld_disabled.png b/dist/icons/controller/applet_handheld_disabled.png Binary files differnew file mode 100644 index 000000000..53f8ce7ad --- /dev/null +++ b/dist/icons/controller/applet_handheld_disabled.png diff --git a/dist/icons/controller/applet_handheld_midnight.png b/dist/icons/controller/applet_handheld_midnight.png Binary files differnew file mode 100644 index 000000000..7188b9154 --- /dev/null +++ b/dist/icons/controller/applet_handheld_midnight.png diff --git a/dist/icons/controller/applet_handheld_midnight_disabled.png b/dist/icons/controller/applet_handheld_midnight_disabled.png Binary files differnew file mode 100644 index 000000000..6ccfc3253 --- /dev/null +++ b/dist/icons/controller/applet_handheld_midnight_disabled.png diff --git a/dist/icons/controller/applet_pro_controller.png b/dist/icons/controller/applet_pro_controller.png Binary files differnew file mode 100644 index 000000000..e32258855 --- /dev/null +++ b/dist/icons/controller/applet_pro_controller.png diff --git a/dist/icons/controller/applet_pro_controller_dark.png b/dist/icons/controller/applet_pro_controller_dark.png Binary files differnew file mode 100644 index 000000000..c122b7b11 --- /dev/null +++ b/dist/icons/controller/applet_pro_controller_dark.png diff --git a/dist/icons/controller/applet_pro_controller_dark_disabled.png b/dist/icons/controller/applet_pro_controller_dark_disabled.png Binary files differnew file mode 100644 index 000000000..416e1e2fb --- /dev/null +++ b/dist/icons/controller/applet_pro_controller_dark_disabled.png diff --git a/dist/icons/controller/applet_pro_controller_disabled.png b/dist/icons/controller/applet_pro_controller_disabled.png Binary files differnew file mode 100644 index 000000000..72a549ea9 --- /dev/null +++ b/dist/icons/controller/applet_pro_controller_disabled.png diff --git a/dist/icons/controller/applet_pro_controller_midnight.png b/dist/icons/controller/applet_pro_controller_midnight.png Binary files differnew file mode 100644 index 000000000..622e57fa1 --- /dev/null +++ b/dist/icons/controller/applet_pro_controller_midnight.png diff --git a/dist/icons/controller/applet_pro_controller_midnight_disabled.png b/dist/icons/controller/applet_pro_controller_midnight_disabled.png Binary files differnew file mode 100644 index 000000000..2907f3be4 --- /dev/null +++ b/dist/icons/controller/applet_pro_controller_midnight_disabled.png diff --git a/dist/icons/controller/applet_single_joycon_left.png b/dist/icons/controller/applet_single_joycon_left.png Binary files differnew file mode 100644 index 000000000..47c44d43a --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_left.png diff --git a/dist/icons/controller/applet_single_joycon_left_dark.png b/dist/icons/controller/applet_single_joycon_left_dark.png Binary files differnew file mode 100644 index 000000000..69530b69c --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_left_dark.png diff --git a/dist/icons/controller/applet_single_joycon_left_dark_disabled.png b/dist/icons/controller/applet_single_joycon_left_dark_disabled.png Binary files differnew file mode 100644 index 000000000..cfe4b1475 --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_left_dark_disabled.png diff --git a/dist/icons/controller/applet_single_joycon_left_disabled.png b/dist/icons/controller/applet_single_joycon_left_disabled.png Binary files differnew file mode 100644 index 000000000..c0102dc20 --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_left_disabled.png diff --git a/dist/icons/controller/applet_single_joycon_left_midnight.png b/dist/icons/controller/applet_single_joycon_left_midnight.png Binary files differnew file mode 100644 index 000000000..56522bccb --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_left_midnight.png diff --git a/dist/icons/controller/applet_single_joycon_left_midnight_disabled.png b/dist/icons/controller/applet_single_joycon_left_midnight_disabled.png Binary files differnew file mode 100644 index 000000000..62434c188 --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_left_midnight_disabled.png diff --git a/dist/icons/controller/applet_single_joycon_right.png b/dist/icons/controller/applet_single_joycon_right.png Binary files differnew file mode 100644 index 000000000..b0d4fba90 --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_right.png diff --git a/dist/icons/controller/applet_single_joycon_right_dark.png b/dist/icons/controller/applet_single_joycon_right_dark.png Binary files differnew file mode 100644 index 000000000..af26cce4b --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_right_dark.png diff --git a/dist/icons/controller/applet_single_joycon_right_dark_disabled.png b/dist/icons/controller/applet_single_joycon_right_dark_disabled.png Binary files differnew file mode 100644 index 000000000..b33efab42 --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_right_dark_disabled.png diff --git a/dist/icons/controller/applet_single_joycon_right_disabled.png b/dist/icons/controller/applet_single_joycon_right_disabled.png Binary files differnew file mode 100644 index 000000000..01ca38322 --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_right_disabled.png diff --git a/dist/icons/controller/applet_single_joycon_right_midnight.png b/dist/icons/controller/applet_single_joycon_right_midnight.png Binary files differnew file mode 100644 index 000000000..5bf70e21e --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_right_midnight.png diff --git a/dist/icons/controller/applet_single_joycon_right_midnight_disabled.png b/dist/icons/controller/applet_single_joycon_right_midnight_disabled.png Binary files differnew file mode 100644 index 000000000..e6693b027 --- /dev/null +++ b/dist/icons/controller/applet_single_joycon_right_midnight_disabled.png diff --git a/dist/icons/controller/controller.qrc b/dist/icons/controller/controller.qrc index f44725d8f..1c4e960c0 100644 --- a/dist/icons/controller/controller.qrc +++ b/dist/icons/controller/controller.qrc @@ -21,5 +21,35 @@ <file alias="single_joycon_right_vertical">single_joycon_right_vertical.png</file> <file alias="single_joycon_right_vertical_dark">single_joycon_right_vertical_dark.png</file> <file alias="single_joycon_right_vertical_midnight">single_joycon_right_vertical_midnight.png</file> + <file alias="applet_dual_joycon">applet_dual_joycon.png</file> + <file alias="applet_dual_joycon_dark">applet_dual_joycon_dark.png</file> + <file alias="applet_dual_joycon_midnight">applet_dual_joycon_midnight.png</file> + <file alias="applet_handheld">applet_handheld.png</file> + <file alias="applet_handheld_dark">applet_handheld_dark.png</file> + <file alias="applet_handheld_midnight">applet_handheld_midnight.png</file> + <file alias="applet_pro_controller">applet_pro_controller.png</file> + <file alias="applet_pro_controller_dark">applet_pro_controller_dark.png</file> + <file alias="applet_pro_controller_midnight">applet_pro_controller_midnight.png</file> + <file alias="applet_joycon_left">applet_single_joycon_left.png</file> + <file alias="applet_joycon_left_dark">applet_single_joycon_left_dark.png</file> + <file alias="applet_joycon_left_midnight">applet_single_joycon_left_midnight.png</file> + <file alias="applet_joycon_right">applet_single_joycon_right.png</file> + <file alias="applet_joycon_right_dark">applet_single_joycon_right_dark.png</file> + <file alias="applet_joycon_right_midnight">applet_single_joycon_right_midnight.png</file> + <file alias="applet_dual_joycon_disabled">applet_dual_joycon_disabled.png</file> + <file alias="applet_dual_joycon_dark_disabled">applet_dual_joycon_dark_disabled.png</file> + <file alias="applet_dual_joycon_midnight_disabled">applet_dual_joycon_midnight_disabled.png</file> + <file alias="applet_handheld_disabled">applet_handheld_disabled.png</file> + <file alias="applet_handheld_dark_disabled">applet_handheld_dark_disabled.png</file> + <file alias="applet_handheld_midnight_disabled">applet_handheld_midnight_disabled.png</file> + <file alias="applet_pro_controller_disabled">applet_pro_controller_disabled.png</file> + <file alias="applet_pro_controller_dark_disabled">applet_pro_controller_dark_disabled.png</file> + <file alias="applet_pro_controller_midnight_disabled">applet_pro_controller_midnight_disabled.png</file> + <file alias="applet_joycon_left_disabled">applet_single_joycon_left_disabled.png</file> + <file alias="applet_joycon_left_dark_disabled">applet_single_joycon_left_dark_disabled.png</file> + <file alias="applet_joycon_left_midnight_disabled">applet_single_joycon_left_midnight_disabled.png</file> + <file alias="applet_joycon_right_disabled">applet_single_joycon_right_disabled.png</file> + <file alias="applet_joycon_right_dark_disabled">applet_single_joycon_right_dark_disabled.png</file> + <file alias="applet_joycon_right_midnight_disabled">applet_single_joycon_right_midnight_disabled.png</file> </qresource> </RCC> diff --git a/dist/qt_themes/default/style.qss b/dist/qt_themes/default/style.qss index 5da56520b..b6dd2063d 100644 --- a/dist/qt_themes/default/style.qss +++ b/dist/qt_themes/default/style.qss @@ -41,6 +41,99 @@ QPushButton#buttonRefreshDevices { max-height: 20px; } +QWidget#bottomPerGameInput, +QWidget#topControllerApplet, +QWidget#bottomControllerApplet, +QGroupBox#groupPlayer1Connected:checked, +QGroupBox#groupPlayer2Connected:checked, +QGroupBox#groupPlayer3Connected:checked, +QGroupBox#groupPlayer4Connected:checked, +QGroupBox#groupPlayer5Connected:checked, +QGroupBox#groupPlayer6Connected:checked, +QGroupBox#groupPlayer7Connected:checked, +QGroupBox#groupPlayer8Connected:checked { + background-color: #f5f5f5; +} + +QWidget#topControllerApplet { + border-bottom: 1px solid #828790 +} + +QWidget#bottomPerGameInput, +QWidget#bottomControllerApplet { + border-top: 1px solid #828790 +} + +QWidget#topPerGameInput, +QWidget#middleControllerApplet { + background-color: #fff; +} + +QWidget#topPerGameInput QComboBox, +QWidget#middleControllerApplet QComboBox { + width: 123px; +} + +QWidget#connectedControllers { + background: transparent; +} + +QWidget#playersSupported, +QWidget#controllersSupported, +QWidget#controllerSupported1, +QWidget#controllerSupported2, +QWidget#controllerSupported3, +QWidget#controllerSupported4, +QWidget#controllerSupported5, +QWidget#controllerSupported6 { + border: none; + background: transparent; +} + +QGroupBox#groupPlayer1Connected, +QGroupBox#groupPlayer2Connected, +QGroupBox#groupPlayer3Connected, +QGroupBox#groupPlayer4Connected, +QGroupBox#groupPlayer5Connected, +QGroupBox#groupPlayer6Connected, +QGroupBox#groupPlayer7Connected, +QGroupBox#groupPlayer8Connected { + border: 1px solid #828790; + border-radius: 3px; + padding: 0px; + min-height: 98px; + max-height: 98px; +} + +QGroupBox#groupPlayer1Connected:unchecked, +QGroupBox#groupPlayer2Connected:unchecked, +QGroupBox#groupPlayer3Connected:unchecked, +QGroupBox#groupPlayer4Connected:unchecked, +QGroupBox#groupPlayer5Connected:unchecked, +QGroupBox#groupPlayer6Connected:unchecked, +QGroupBox#groupPlayer7Connected:unchecked, +QGroupBox#groupPlayer8Connected:unchecked { + border: 1px solid #d9d9d9; +} + +QGroupBox#groupPlayer1Connected::title, +QGroupBox#groupPlayer2Connected::title, +QGroupBox#groupPlayer3Connected::title, +QGroupBox#groupPlayer4Connected::title, +QGroupBox#groupPlayer5Connected::title, +QGroupBox#groupPlayer6Connected::title, +QGroupBox#groupPlayer7Connected::title, +QGroupBox#groupPlayer8Connected::title { + subcontrol-origin: margin; + subcontrol-position: top left; + padding-left: 0px; + padding-right: 0px; + padding-top: 1px; + margin-left: 0px; + margin-right: -4px; + margin-bottom: 4px; +} + QCheckBox#checkboxPlayer1Connected, QCheckBox#checkboxPlayer2Connected, QCheckBox#checkboxPlayer3Connected, @@ -52,6 +145,42 @@ QCheckBox#checkboxPlayer8Connected { spacing: 0px; } +QWidget#Player1LEDs QCheckBox, +QWidget#Player2LEDs QCheckBox, +QWidget#Player3LEDs QCheckBox, +QWidget#Player4LEDs QCheckBox, +QWidget#Player5LEDs QCheckBox, +QWidget#Player6LEDs QCheckBox, +QWidget#Player7LEDs QCheckBox, +QWidget#Player8LEDs QCheckBox { + spacing: 0px; +} + +QWidget#Player1LEDs QCheckBox::indicator, +QWidget#Player2LEDs QCheckBox::indicator, +QWidget#Player3LEDs QCheckBox::indicator, +QWidget#Player4LEDs QCheckBox::indicator, +QWidget#Player5LEDs QCheckBox::indicator, +QWidget#Player6LEDs QCheckBox::indicator, +QWidget#Player7LEDs QCheckBox::indicator, +QWidget#Player8LEDs QCheckBox::indicator { + width: 6px; + height: 6px; + margin-left: 0px; +} + +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer1Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer2Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer3Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer4Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer5Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer6Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer7Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer8Connected::indicator { + width: 12px; + height: 12px; +} + QCheckBox#checkboxPlayer1Connected::indicator, QCheckBox#checkboxPlayer2Connected::indicator, QCheckBox#checkboxPlayer3Connected::indicator, @@ -64,6 +193,34 @@ QCheckBox#checkboxPlayer8Connected::indicator { height: 14px; } +QGroupBox#groupPlayer1Connected::indicator, +QGroupBox#groupPlayer2Connected::indicator, +QGroupBox#groupPlayer3Connected::indicator, +QGroupBox#groupPlayer4Connected::indicator, +QGroupBox#groupPlayer5Connected::indicator, +QGroupBox#groupPlayer6Connected::indicator, +QGroupBox#groupPlayer7Connected::indicator, +QGroupBox#groupPlayer8Connected::indicator { + width: 16px; + height: 16px; +} + +QWidget#Player1LEDs QCheckBox::indicator:checked, +QWidget#Player2LEDs QCheckBox::indicator:checked, +QWidget#Player3LEDs QCheckBox::indicator:checked, +QWidget#Player4LEDs QCheckBox::indicator:checked, +QWidget#Player5LEDs QCheckBox::indicator:checked, +QWidget#Player6LEDs QCheckBox::indicator:checked, +QWidget#Player7LEDs QCheckBox::indicator:checked, +QWidget#Player8LEDs QCheckBox::indicator:checked, +QGroupBox#groupPlayer1Connected::indicator:checked, +QGroupBox#groupPlayer2Connected::indicator:checked, +QGroupBox#groupPlayer3Connected::indicator:checked, +QGroupBox#groupPlayer4Connected::indicator:checked, +QGroupBox#groupPlayer5Connected::indicator:checked, +QGroupBox#groupPlayer6Connected::indicator:checked, +QGroupBox#groupPlayer7Connected::indicator:checked, +QGroupBox#groupPlayer8Connected::indicator:checked, QCheckBox#checkboxPlayer1Connected::indicator:checked, QCheckBox#checkboxPlayer2Connected::indicator:checked, QCheckBox#checkboxPlayer3Connected::indicator:checked, @@ -74,11 +231,27 @@ QCheckBox#checkboxPlayer7Connected::indicator:checked, QCheckBox#checkboxPlayer8Connected::indicator:checked, QGroupBox#groupConnectedController::indicator:checked { border-radius: 2px; - border: 1px solid black; + border: 1px solid #929192; background: #39ff14; image: none; } +QWidget#Player1LEDs QCheckBox::indicator:unchecked, +QWidget#Player2LEDs QCheckBox::indicator:unchecked, +QWidget#Player3LEDs QCheckBox::indicator:unchecked, +QWidget#Player4LEDs QCheckBox::indicator:unchecked, +QWidget#Player5LEDs QCheckBox::indicator:unchecked, +QWidget#Player6LEDs QCheckBox::indicator:unchecked, +QWidget#Player7LEDs QCheckBox::indicator:unchecked, +QWidget#Player8LEDs QCheckBox::indicator:unchecked, +QGroupBox#groupPlayer1Connected::indicator:unchecked, +QGroupBox#groupPlayer2Connected::indicator:unchecked, +QGroupBox#groupPlayer3Connected::indicator:unchecked, +QGroupBox#groupPlayer4Connected::indicator:unchecked, +QGroupBox#groupPlayer5Connected::indicator:unchecked, +QGroupBox#groupPlayer6Connected::indicator:unchecked, +QGroupBox#groupPlayer7Connected::indicator:unchecked, +QGroupBox#groupPlayer8Connected::indicator:unchecked, QCheckBox#checkboxPlayer1Connected::indicator:unchecked, QCheckBox#checkboxPlayer2Connected::indicator:unchecked, QCheckBox#checkboxPlayer3Connected::indicator:unchecked, @@ -89,7 +262,18 @@ QCheckBox#checkboxPlayer7Connected::indicator:unchecked, QCheckBox#checkboxPlayer8Connected::indicator:unchecked, QGroupBox#groupConnectedController::indicator:unchecked { border-radius: 2px; - border: 1px solid black; + border: 1px solid #929192; background: transparent; image: none; } + +QWidget#controllerPlayer1, +QWidget#controllerPlayer2, +QWidget#controllerPlayer3, +QWidget#controllerPlayer4, +QWidget#controllerPlayer5, +QWidget#controllerPlayer6, +QWidget#controllerPlayer7, +QWidget#controllerPlayer8 { + background: transparent; +} diff --git a/dist/qt_themes/qdarkstyle/style.qrc b/dist/qt_themes/qdarkstyle/style.qrc index ec07ba160..2b91204f3 100644 --- a/dist/qt_themes/qdarkstyle/style.qrc +++ b/dist/qt_themes/qdarkstyle/style.qrc @@ -52,6 +52,6 @@ <file>rc/radio_unchecked.png</file> </qresource> <qresource prefix="qdarkstyle"> - <file>style.qss</file> + <file>style.qss</file> </qresource> </RCC> diff --git a/dist/qt_themes/qdarkstyle/style.qss b/dist/qt_themes/qdarkstyle/style.qss index 16218f0c2..66026e8be 100644 --- a/dist/qt_themes/qdarkstyle/style.qss +++ b/dist/qt_themes/qdarkstyle/style.qss @@ -1284,6 +1284,135 @@ QPushButton#buttonRefreshDevices { padding: 0px 0px; } +QSpinBox#spinboxLStickRange, +QSpinBox#spinboxRStickRange { + padding: 4px 0px 5px 0px; + min-width: 63px; +} + +QSpinBox#vibrationSpin { + padding: 4px 0px 5px 0px; + min-width: 63px; +} + +QSpinBox#spinboxLStickRange:up-button, +QSpinBox#spinboxRStickRange:up-button, +QSpinBox#vibrationSpin:up-button { + left: -2px; +} + +QSpinBox#spinboxLStickRange:down-button, +QSpinBox#spinboxRStickRange:down-button, +QSpinBox#vibrationSpin:down-button { + right: -1px; +} + +QGroupBox#motionGroup::indicator, +QGroupBox#vibrationGroup::indicator { + margin-left: 0px; +} + +QGroupBox#motionGroup::title, +QGroupBox#vibrationGroup::title { + spacing: 2px; + padding-left: 1px; + padding-right: 1px; +} + +QWidget#bottomPerGameInput, +QWidget#topControllerApplet, +QWidget#bottomControllerApplet, +QGroupBox#groupPlayer1Connected:checked, +QGroupBox#groupPlayer2Connected:checked, +QGroupBox#groupPlayer3Connected:checked, +QGroupBox#groupPlayer4Connected:checked, +QGroupBox#groupPlayer5Connected:checked, +QGroupBox#groupPlayer6Connected:checked, +QGroupBox#groupPlayer7Connected:checked, +QGroupBox#groupPlayer8Connected:checked { + background-color: #232629; +} + +QWidget#topPerGameInput, +QWidget#middleControllerApplet { + background-color: #31363b; +} + +QWidget#topPerGameInput QComboBox, +QWidget#middleControllerApplet QComboBox { + width: 119px; +} + +QRadioButton#radioDocked { + margin-left: -3px; +} + + +QRadioButton#radioUndocked { + margin-right: 5px; +} + +QWidget#connectedControllers { + background: transparent; +} + +QWidget#playersSupported, +QWidget#controllersSupported, +QWidget#controllerSupported1, +QWidget#controllerSupported2, +QWidget#controllerSupported3, +QWidget#controllerSupported4, +QWidget#controllerSupported5, +QWidget#controllerSupported6 { + border: none; + background: transparent; +} + +QGroupBox#groupPlayer1Connected, +QGroupBox#groupPlayer2Connected, +QGroupBox#groupPlayer3Connected, +QGroupBox#groupPlayer4Connected, +QGroupBox#groupPlayer5Connected, +QGroupBox#groupPlayer6Connected, +QGroupBox#groupPlayer7Connected, +QGroupBox#groupPlayer8Connected { + border: 1px solid #76797c; + border-radius: 3px; + padding: 0px; + min-height: 98px; + max-height: 98px; + margin-top: 0px; +} + +QGroupBox#groupPlayer1Connected:unchecked, +QGroupBox#groupPlayer2Connected:unchecked, +QGroupBox#groupPlayer3Connected:unchecked, +QGroupBox#groupPlayer4Connected:unchecked, +QGroupBox#groupPlayer5Connected:unchecked, +QGroupBox#groupPlayer6Connected:unchecked, +QGroupBox#groupPlayer7Connected:unchecked, +QGroupBox#groupPlayer8Connected:unchecked { + border: 1px solid #54575b; +} + +QGroupBox#groupPlayer1Connected::title, +QGroupBox#groupPlayer2Connected::title, +QGroupBox#groupPlayer3Connected::title, +QGroupBox#groupPlayer4Connected::title, +QGroupBox#groupPlayer5Connected::title, +QGroupBox#groupPlayer6Connected::title, +QGroupBox#groupPlayer7Connected::title, +QGroupBox#groupPlayer8Connected::title { + subcontrol-origin: margin; + subcontrol-position: top left; + padding-left: 0px; + padding-right: 0px; + padding-top: 1px; + margin-left: -2px; + margin-right: -4px; + margin-bottom: 6px; +} + QCheckBox#checkboxPlayer1Connected, QCheckBox#checkboxPlayer2Connected, QCheckBox#checkboxPlayer3Connected, @@ -1295,6 +1424,55 @@ QCheckBox#checkboxPlayer8Connected { spacing: 0px; } +QWidget#Player1LEDs, +QWidget#Player2LEDs, +QWidget#Player3LEDs, +QWidget#Player4LEDs, +QWidget#Player5LEDs, +QWidget#Player6LEDs, +QWidget#Player7LEDs, +QWidget#Player8LEDs { + background: transparent; +} + +QWidget#Player1LEDs QCheckBox, +QWidget#Player2LEDs QCheckBox, +QWidget#Player3LEDs QCheckBox, +QWidget#Player4LEDs QCheckBox, +QWidget#Player5LEDs QCheckBox, +QWidget#Player6LEDs QCheckBox, +QWidget#Player7LEDs QCheckBox, +QWidget#Player8LEDs QCheckBox { + spacing: 0px; + margin-bottom: 0px; + margin-right: 0px; +} + +QWidget#Player1LEDs QCheckBox::indicator, +QWidget#Player2LEDs QCheckBox::indicator, +QWidget#Player3LEDs QCheckBox::indicator, +QWidget#Player4LEDs QCheckBox::indicator, +QWidget#Player5LEDs QCheckBox::indicator, +QWidget#Player6LEDs QCheckBox::indicator, +QWidget#Player7LEDs QCheckBox::indicator, +QWidget#Player8LEDs QCheckBox::indicator { + width: 6px; + height: 6px; + margin-left: 0px; +} + +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer1Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer2Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer3Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer4Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer5Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer6Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer7Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer8Connected::indicator { + width: 12px; + height: 12px; +} + QCheckBox#checkboxPlayer1Connected::indicator, QCheckBox#checkboxPlayer2Connected::indicator, QCheckBox#checkboxPlayer3Connected::indicator, @@ -1307,6 +1485,34 @@ QCheckBox#checkboxPlayer8Connected::indicator { height: 14px; } +QGroupBox#groupPlayer1Connected::indicator, +QGroupBox#groupPlayer2Connected::indicator, +QGroupBox#groupPlayer3Connected::indicator, +QGroupBox#groupPlayer4Connected::indicator, +QGroupBox#groupPlayer5Connected::indicator, +QGroupBox#groupPlayer6Connected::indicator, +QGroupBox#groupPlayer7Connected::indicator, +QGroupBox#groupPlayer8Connected::indicator { + width: 16px; + height: 16px; +} + +QWidget#Player1LEDs QCheckBox::indicator:checked, +QWidget#Player2LEDs QCheckBox::indicator:checked, +QWidget#Player3LEDs QCheckBox::indicator:checked, +QWidget#Player4LEDs QCheckBox::indicator:checked, +QWidget#Player5LEDs QCheckBox::indicator:checked, +QWidget#Player6LEDs QCheckBox::indicator:checked, +QWidget#Player7LEDs QCheckBox::indicator:checked, +QWidget#Player8LEDs QCheckBox::indicator:checked, +QGroupBox#groupPlayer1Connected::indicator:checked, +QGroupBox#groupPlayer2Connected::indicator:checked, +QGroupBox#groupPlayer3Connected::indicator:checked, +QGroupBox#groupPlayer4Connected::indicator:checked, +QGroupBox#groupPlayer5Connected::indicator:checked, +QGroupBox#groupPlayer6Connected::indicator:checked, +QGroupBox#groupPlayer7Connected::indicator:checked, +QGroupBox#groupPlayer8Connected::indicator:checked, QCheckBox#checkboxPlayer1Connected::indicator:checked, QCheckBox#checkboxPlayer2Connected::indicator:checked, QCheckBox#checkboxPlayer3Connected::indicator:checked, @@ -1322,6 +1528,22 @@ QGroupBox#groupConnectedController::indicator:checked { image: none; } +QWidget#Player1LEDs QCheckBox::indicator:unchecked, +QWidget#Player2LEDs QCheckBox::indicator:unchecked, +QWidget#Player3LEDs QCheckBox::indicator:unchecked, +QWidget#Player4LEDs QCheckBox::indicator:unchecked, +QWidget#Player5LEDs QCheckBox::indicator:unchecked, +QWidget#Player6LEDs QCheckBox::indicator:unchecked, +QWidget#Player7LEDs QCheckBox::indicator:unchecked, +QWidget#Player8LEDs QCheckBox::indicator:unchecked, +QGroupBox#groupPlayer1Connected::indicator:unchecked, +QGroupBox#groupPlayer2Connected::indicator:unchecked, +QGroupBox#groupPlayer3Connected::indicator:unchecked, +QGroupBox#groupPlayer4Connected::indicator:unchecked, +QGroupBox#groupPlayer5Connected::indicator:unchecked, +QGroupBox#groupPlayer6Connected::indicator:unchecked, +QGroupBox#groupPlayer7Connected::indicator:unchecked, +QGroupBox#groupPlayer8Connected::indicator:unchecked, QCheckBox#checkboxPlayer1Connected::indicator:unchecked, QCheckBox#checkboxPlayer2Connected::indicator:unchecked, QCheckBox#checkboxPlayer3Connected::indicator:unchecked, @@ -1337,39 +1559,15 @@ QGroupBox#groupConnectedController::indicator:unchecked { image: none; } -QSpinBox#spinboxLStickRange, -QSpinBox#spinboxRStickRange { - padding: 4px 0px 5px 0px; - min-width: 63px; -} - -QSpinBox#vibrationSpin { - padding: 4px 0px 5px 0px; - min-width: 63px; -} - -QSpinBox#spinboxLStickRange:up-button, -QSpinBox#spinboxRStickRange:up-button, -QSpinBox#vibrationSpin:up-button { - left: -2px; -} - -QSpinBox#spinboxLStickRange:down-button, -QSpinBox#spinboxRStickRange:down-button, -QSpinBox#vibrationSpin:down-button { - right: -1px; -} - -QGroupBox#motionGroup::indicator, -QGroupBox#vibrationGroup::indicator { - margin-left: 0px; -} - -QGroupBox#motionGroup::title, -QGroupBox#vibrationGroup::title { - spacing: 2px; - padding-left: 1px; - padding-right: 1px; +QWidget#controllerPlayer1, +QWidget#controllerPlayer2, +QWidget#controllerPlayer3, +QWidget#controllerPlayer4, +QWidget#controllerPlayer5, +QWidget#controllerPlayer6, +QWidget#controllerPlayer7, +QWidget#controllerPlayer8 { + background: transparent; } /* touchscreen mapping widget */ diff --git a/dist/qt_themes/qdarkstyle_midnight_blue/style.qrc b/dist/qt_themes/qdarkstyle_midnight_blue/style.qrc index 616aace73..579e73ece 100644 --- a/dist/qt_themes/qdarkstyle_midnight_blue/style.qrc +++ b/dist/qt_themes/qdarkstyle_midnight_blue/style.qrc @@ -221,6 +221,6 @@ <file>rc/window_undock_pressed@2x.png</file> </qresource> <qresource prefix="qdarkstyle_midnight_blue"> - <file>style.qss</file> + <file>style.qss</file> </qresource> </RCC> diff --git a/dist/qt_themes/qdarkstyle_midnight_blue/style.qss b/dist/qt_themes/qdarkstyle_midnight_blue/style.qss index a714e1475..c6318ba4e 100644 --- a/dist/qt_themes/qdarkstyle_midnight_blue/style.qss +++ b/dist/qt_themes/qdarkstyle_midnight_blue/style.qss @@ -235,19 +235,19 @@ https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qgroupbox --------------------------------------------------------------------------- */ QGroupBox { - font-weight: bold; - border: 1px solid #32414B; - border-radius: 4px; - margin-top: 12px; - padding: 4px; + font-weight: bold; + border: 1px solid #32414B; + border-radius: 4px; + margin-top: 12px; + padding: 4px; } QGroupBox::title { - subcontrol-origin: margin; - subcontrol-position: top left; - padding-left: 3px; - padding-right: 5px; - padding-top: 4px; + subcontrol-origin: margin; + subcontrol-position: top left; + padding-left: 3px; + padding-right: 5px; + padding-top: 4px; } QGroupBox::indicator { @@ -2205,6 +2205,144 @@ QPushButton#buttonRefreshDevices { padding: 0px 0px; } +QSpinBox#spinboxLStickRange, +QSpinBox#spinboxRStickRange { + min-width: 38px; +} + +QGroupBox#motionGroup::indicator, +QGroupBox#vibrationGroup::indicator { + margin-left: 0px; +} + +QWidget#bottomPerGameInput QGroupBox#motionGroup, +QWidget#bottomPerGameInput QGroupBox#vibrationGroup, +QWidget#bottomPerGameInput QGroupBox#inputConfigGroup { + padding: 0px; +} + +QGroupBox#motionGroup::title, +QGroupBox#vibrationGroup::title { + spacing: 2px; + padding-left: 1px; + padding-right: 1px; +} + +QListWidget#selectorList { + background-color: #0f1922; +} + +QSpinBox, +QLineEdit, +QTreeView#hotkey_list, +QScrollArea#scrollArea QTreeView { + background-color: #0f1922; +} + +QWidget#bottomPerGameInput, +QWidget#topControllerApplet, +QWidget#bottomControllerApplet, +QGroupBox#groupPlayer1Connected:checked, +QGroupBox#groupPlayer2Connected:checked, +QGroupBox#groupPlayer3Connected:checked, +QGroupBox#groupPlayer4Connected:checked, +QGroupBox#groupPlayer5Connected:checked, +QGroupBox#groupPlayer6Connected:checked, +QGroupBox#groupPlayer7Connected:checked, +QGroupBox#groupPlayer8Connected:checked { + background-color: #0f1922; +} + +QWidget#topPerGameInput, +QWidget#middleControllerApplet { + background-color: #19232d; +} + +QWidget#topPerGameInput QComboBox, +QWidget#middleControllerApplet QComboBox { + padding-right: 2px; + width: 127px; +} + +QGroupBox#handheldGroup { + padding-left: 0px; +} + +QRadioButton#radioDocked { + margin-left: -1px; + padding-left: 0px; +} + +QRadioButton#radioDocked::indicator { + margin-left: 0px; +} + + +QRadioButton#radioUndocked { + margin-right: 2px; +} + +QWidget#connectedControllers { + background: transparent; +} + +QWidget#playersSupported, +QWidget#controllersSupported, +QWidget#controllerSupported1, +QWidget#controllerSupported2, +QWidget#controllerSupported3, +QWidget#controllerSupported4, +QWidget#controllerSupported5, +QWidget#controllerSupported6 { + border: none; + background: transparent; +} + +QGroupBox#groupPlayer1Connected, +QGroupBox#groupPlayer2Connected, +QGroupBox#groupPlayer3Connected, +QGroupBox#groupPlayer4Connected, +QGroupBox#groupPlayer5Connected, +QGroupBox#groupPlayer6Connected, +QGroupBox#groupPlayer7Connected, +QGroupBox#groupPlayer8Connected { + border: 1px solid #76797c; + border-radius: 3px; + padding: 0px; + min-height: 98px; + max-height: 98px; + margin-top: 0px; +} + + +QGroupBox#groupPlayer1Connected:unchecked, +QGroupBox#groupPlayer2Connected:unchecked, +QGroupBox#groupPlayer3Connected:unchecked, +QGroupBox#groupPlayer4Connected:unchecked, +QGroupBox#groupPlayer5Connected:unchecked, +QGroupBox#groupPlayer6Connected:unchecked, +QGroupBox#groupPlayer7Connected:unchecked, +QGroupBox#groupPlayer8Connected:unchecked { + border: 1px solid #32414b; +} + +QGroupBox#groupPlayer1Connected::title, +QGroupBox#groupPlayer2Connected::title, +QGroupBox#groupPlayer3Connected::title, +QGroupBox#groupPlayer4Connected::title, +QGroupBox#groupPlayer5Connected::title, +QGroupBox#groupPlayer6Connected::title, +QGroupBox#groupPlayer7Connected::title, +QGroupBox#groupPlayer8Connected::title { + subcontrol-origin: margin; + subcontrol-position: top left; + padding-left: 0px; + padding-right: 0px; + padding-top: 1px; + margin-left: -2px; + margin-right: -4px; + margin-bottom: 6px; +} QCheckBox#checkboxPlayer1Connected, QCheckBox#checkboxPlayer2Connected, @@ -2214,7 +2352,69 @@ QCheckBox#checkboxPlayer5Connected, QCheckBox#checkboxPlayer6Connected, QCheckBox#checkboxPlayer7Connected, QCheckBox#checkboxPlayer8Connected { + spacing: 0px; +} + +QWidget#connectedControllers QLabel { + padding: 0px; +} + +QWidget#Player1LEDs, +QWidget#Player2LEDs, +QWidget#Player3LEDs, +QWidget#Player4LEDs, +QWidget#Player5LEDs, +QWidget#Player6LEDs, +QWidget#Player7LEDs, +QWidget#Player8LEDs { + background: transparent; +} + +QWidget#Player1LEDs QCheckBox, +QWidget#Player2LEDs QCheckBox, +QWidget#Player3LEDs QCheckBox, +QWidget#Player4LEDs QCheckBox, +QWidget#Player5LEDs QCheckBox, +QWidget#Player6LEDs QCheckBox, +QWidget#Player7LEDs QCheckBox, +QWidget#Player8LEDs QCheckBox, +QCheckBox#checkboxPlayer1Connected, +QCheckBox#checkboxPlayer2Connected, +QCheckBox#checkboxPlayer3Connected, +QCheckBox#checkboxPlayer4Connected, +QCheckBox#checkboxPlayer5Connected, +QCheckBox#checkboxPlayer6Connected, +QCheckBox#checkboxPlayer7Connected, +QCheckBox#checkboxPlayer8Connected { spacing: 0px; + padding-top: 0px; + padding-bottom: 0px; + background: transparent; +} + +QWidget#Player1LEDs QCheckBox::indicator, +QWidget#Player2LEDs QCheckBox::indicator, +QWidget#Player3LEDs QCheckBox::indicator, +QWidget#Player4LEDs QCheckBox::indicator, +QWidget#Player5LEDs QCheckBox::indicator, +QWidget#Player6LEDs QCheckBox::indicator, +QWidget#Player7LEDs QCheckBox::indicator, +QWidget#Player8LEDs QCheckBox::indicator { + width: 6px; + height: 6px; + margin-left: 0px; +} + +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer1Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer2Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer3Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer4Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer5Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer6Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer7Connected::indicator, +QWidget#bottomPerGameInput QCheckBox#checkboxPlayer8Connected::indicator { + width: 12px; + height: 12px; } QCheckBox#checkboxPlayer1Connected::indicator, @@ -2227,8 +2427,25 @@ QCheckBox#checkboxPlayer7Connected::indicator, QCheckBox#checkboxPlayer8Connected::indicator { width: 14px; height: 14px; + margin-left: 2px; } +QWidget#Player1LEDs QCheckBox::indicator:checked, +QWidget#Player2LEDs QCheckBox::indicator:checked, +QWidget#Player3LEDs QCheckBox::indicator:checked, +QWidget#Player4LEDs QCheckBox::indicator:checked, +QWidget#Player5LEDs QCheckBox::indicator:checked, +QWidget#Player6LEDs QCheckBox::indicator:checked, +QWidget#Player7LEDs QCheckBox::indicator:checked, +QWidget#Player8LEDs QCheckBox::indicator:checked, +QGroupBox#groupPlayer1Connected::indicator:checked, +QGroupBox#groupPlayer2Connected::indicator:checked, +QGroupBox#groupPlayer3Connected::indicator:checked, +QGroupBox#groupPlayer4Connected::indicator:checked, +QGroupBox#groupPlayer5Connected::indicator:checked, +QGroupBox#groupPlayer6Connected::indicator:checked, +QGroupBox#groupPlayer7Connected::indicator:checked, +QGroupBox#groupPlayer8Connected::indicator:checked, QCheckBox#checkboxPlayer1Connected::indicator:checked, QCheckBox#checkboxPlayer2Connected::indicator:checked, QCheckBox#checkboxPlayer3Connected::indicator:checked, @@ -2244,6 +2461,22 @@ QGroupBox#groupConnectedController::indicator:checked { image: none; } +QWidget#Player1LEDs QCheckBox::indicator:unchecked, +QWidget#Player2LEDs QCheckBox::indicator:unchecked, +QWidget#Player3LEDs QCheckBox::indicator:unchecked, +QWidget#Player4LEDs QCheckBox::indicator:unchecked, +QWidget#Player5LEDs QCheckBox::indicator:unchecked, +QWidget#Player6LEDs QCheckBox::indicator:unchecked, +QWidget#Player7LEDs QCheckBox::indicator:unchecked, +QWidget#Player8LEDs QCheckBox::indicator:unchecked, +QGroupBox#groupPlayer1Connected::indicator:unchecked, +QGroupBox#groupPlayer2Connected::indicator:unchecked, +QGroupBox#groupPlayer3Connected::indicator:unchecked, +QGroupBox#groupPlayer4Connected::indicator:unchecked, +QGroupBox#groupPlayer5Connected::indicator:unchecked, +QGroupBox#groupPlayer6Connected::indicator:unchecked, +QGroupBox#groupPlayer7Connected::indicator:unchecked, +QGroupBox#groupPlayer8Connected::indicator:unchecked, QCheckBox#checkboxPlayer1Connected::indicator:unchecked, QCheckBox#checkboxPlayer2Connected::indicator:unchecked, QCheckBox#checkboxPlayer3Connected::indicator:unchecked, @@ -2255,34 +2488,17 @@ QCheckBox#checkboxPlayer8Connected::indicator:unchecked, QGroupBox#groupConnectedController::indicator:unchecked { border-radius: 2px; border: 1px solid #929192; - background: transparent; + background: #19232d; image: none; } -QSpinBox#spinboxLStickRange, -QSpinBox#spinboxRStickRange { - min-width: 38px; -} - -QGroupBox#motionGroup::indicator, -QGroupBox#vibrationGroup::indicator { - margin-left: 0px; -} - -QGroupBox#motionGroup::title, -QGroupBox#vibrationGroup::title { -spacing: 2px; - padding-left: 1px; - padding-right: 1px; -} - -QListWidget#selectorList { - background-color: #0f1922; +QWidget#controllerPlayer1, +QWidget#controllerPlayer2, +QWidget#controllerPlayer3, +QWidget#controllerPlayer4, +QWidget#controllerPlayer5, +QWidget#controllerPlayer6, +QWidget#controllerPlayer7, +QWidget#controllerPlayer8 { + background: transparent; } - -QSpinBox, -QLineEdit, -QTreeView#hotkey_list, -QScrollArea#scrollArea QTreeView { - background-color: #0f1922; -}
\ No newline at end of file diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt index 5ef38a337..cb00ef60e 100644 --- a/src/audio_core/CMakeLists.txt +++ b/src/audio_core/CMakeLists.txt @@ -12,16 +12,32 @@ add_library(audio_core STATIC buffer.h codec.cpp codec.h + command_generator.cpp + command_generator.h common.h + effect_context.cpp + effect_context.h + info_updater.cpp + info_updater.h + memory_pool.cpp + memory_pool.h + mix_context.cpp + mix_context.h null_sink.h sink.h + sink_context.cpp + sink_context.h sink_details.cpp sink_details.h sink_stream.h + splitter_context.cpp + splitter_context.h stream.cpp stream.h time_stretch.cpp time_stretch.h + voice_context.cpp + voice_context.h $<$<BOOL:${ENABLE_CUBEB}>:cubeb_sink.cpp cubeb_sink.h> ) diff --git a/src/audio_core/algorithm/interpolate.cpp b/src/audio_core/algorithm/interpolate.cpp index 49ab9d3e1..689a54508 100644 --- a/src/audio_core/algorithm/interpolate.cpp +++ b/src/audio_core/algorithm/interpolate.cpp @@ -197,4 +197,36 @@ std::vector<s16> Interpolate(InterpolationState& state, std::vector<s16> input, return output; } +void Resample(s32* output, const s32* input, s32 pitch, s32& fraction, std::size_t sample_count) { + const std::array<s16, 512>& lut = [pitch] { + if (pitch > 0xaaaa) { + return curve_lut0; + } + if (pitch <= 0x8000) { + return curve_lut1; + } + return curve_lut2; + }(); + + std::size_t index{}; + + for (std::size_t i = 0; i < sample_count; i++) { + const std::size_t lut_index{(static_cast<std::size_t>(fraction) >> 8) * 4}; + const auto l0 = lut[lut_index + 0]; + const auto l1 = lut[lut_index + 1]; + const auto l2 = lut[lut_index + 2]; + const auto l3 = lut[lut_index + 3]; + + const auto s0 = static_cast<s32>(input[index]); + const auto s1 = static_cast<s32>(input[index + 1]); + const auto s2 = static_cast<s32>(input[index + 2]); + const auto s3 = static_cast<s32>(input[index + 3]); + + output[i] = (l0 * s0 + l1 * s1 + l2 * s2 + l3 * s3) >> 15; + fraction += pitch; + index += (fraction >> 15); + fraction &= 0x7fff; + } +} + } // namespace AudioCore diff --git a/src/audio_core/algorithm/interpolate.h b/src/audio_core/algorithm/interpolate.h index ab1a31754..d534077af 100644 --- a/src/audio_core/algorithm/interpolate.h +++ b/src/audio_core/algorithm/interpolate.h @@ -38,4 +38,7 @@ inline std::vector<s16> Interpolate(InterpolationState& state, std::vector<s16> return Interpolate(state, std::move(input), ratio); } +/// Nintendo Switchs DSP resampling algorithm. Based on a single channel +void Resample(s32* output, const s32* input, s32 pitch, s32& fraction, std::size_t sample_count); + } // namespace AudioCore diff --git a/src/audio_core/audio_renderer.cpp b/src/audio_core/audio_renderer.cpp index d64452617..56dc892b1 100644 --- a/src/audio_core/audio_renderer.cpp +++ b/src/audio_core/audio_renderer.cpp @@ -2,95 +2,49 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <vector> #include "audio_core/algorithm/interpolate.h" #include "audio_core/audio_out.h" #include "audio_core/audio_renderer.h" #include "audio_core/codec.h" #include "audio_core/common.h" +#include "audio_core/info_updater.h" +#include "audio_core/voice_context.h" #include "common/assert.h" #include "common/logging/log.h" #include "core/core.h" #include "core/hle/kernel/writable_event.h" #include "core/memory.h" +#include "core/settings.h" namespace AudioCore { - -constexpr u32 STREAM_SAMPLE_RATE{48000}; -constexpr u32 STREAM_NUM_CHANNELS{2}; -using VoiceChannelHolder = std::array<VoiceResourceInformation*, 6>; -class AudioRenderer::VoiceState { -public: - bool IsPlaying() const { - return is_in_use && info.play_state == PlayState::Started; - } - - const VoiceOutStatus& GetOutStatus() const { - return out_status; - } - - const VoiceInfo& GetInfo() const { - return info; - } - - VoiceInfo& GetInfo() { - return info; - } - - void SetWaveIndex(std::size_t index); - std::vector<s16> DequeueSamples(std::size_t sample_count, Core::Memory::Memory& memory, - const VoiceChannelHolder& voice_resources); - void UpdateState(); - void RefreshBuffer(Core::Memory::Memory& memory, const VoiceChannelHolder& voice_resources); - -private: - bool is_in_use{}; - bool is_refresh_pending{}; - std::size_t wave_index{}; - std::size_t offset{}; - Codec::ADPCMState adpcm_state{}; - InterpolationState interp_state{}; - std::vector<s16> samples; - VoiceOutStatus out_status{}; - VoiceInfo info{}; -}; - -class AudioRenderer::EffectState { -public: - const EffectOutStatus& GetOutStatus() const { - return out_status; - } - - const EffectInStatus& GetInfo() const { - return info; - } - - EffectInStatus& GetInfo() { - return info; - } - - void UpdateState(Core::Memory::Memory& memory); - -private: - EffectOutStatus out_status{}; - EffectInStatus info{}; -}; - AudioRenderer::AudioRenderer(Core::Timing::CoreTiming& core_timing, Core::Memory::Memory& memory_, - AudioRendererParameter params, + AudioCommon::AudioRendererParameter params, std::shared_ptr<Kernel::WritableEvent> buffer_event, std::size_t instance_number) - : worker_params{params}, buffer_event{buffer_event}, voices(params.voice_count), - voice_resources(params.voice_count), effects(params.effect_count), memory{memory_} { + : worker_params{params}, buffer_event{buffer_event}, + memory_pool_info(params.effect_count + params.voice_count * 4), + voice_context(params.voice_count), effect_context(params.effect_count), mix_context(), + sink_context(params.sink_count), splitter_context(), + voices(params.voice_count), memory{memory_}, + command_generator(worker_params, voice_context, mix_context, splitter_context, effect_context, + memory), + temp_mix_buffer(AudioCommon::TOTAL_TEMP_MIX_SIZE) { behavior_info.SetUserRevision(params.revision); + splitter_context.Initialize(behavior_info, params.splitter_count, + params.num_splitter_send_channels); + mix_context.Initialize(behavior_info, params.submix_count + 1, params.effect_count); audio_out = std::make_unique<AudioCore::AudioOut>(); - stream = audio_out->OpenStream(core_timing, STREAM_SAMPLE_RATE, STREAM_NUM_CHANNELS, - fmt::format("AudioRenderer-Instance{}", instance_number), - [=]() { buffer_event->Signal(); }); + stream = + audio_out->OpenStream(core_timing, params.sample_rate, AudioCommon::STREAM_NUM_CHANNELS, + fmt::format("AudioRenderer-Instance{}", instance_number), + [=]() { buffer_event->Signal(); }); audio_out->StartStream(stream); QueueMixedBuffer(0); QueueMixedBuffer(1); QueueMixedBuffer(2); + QueueMixedBuffer(3); } AudioRenderer::~AudioRenderer() = default; @@ -111,355 +65,200 @@ Stream::State AudioRenderer::GetStreamState() const { return stream->GetState(); } -ResultVal<std::vector<u8>> AudioRenderer::UpdateAudioRenderer(const std::vector<u8>& input_params) { - // Copy UpdateDataHeader struct - UpdateDataHeader config{}; - std::memcpy(&config, input_params.data(), sizeof(UpdateDataHeader)); - u32 memory_pool_count = worker_params.effect_count + (worker_params.voice_count * 4); - - if (!behavior_info.UpdateInput(input_params, sizeof(UpdateDataHeader))) { - LOG_ERROR(Audio, "Failed to update behavior info input parameters"); - return Audren::ERR_INVALID_PARAMETERS; - } - - // Copy MemoryPoolInfo structs - std::vector<MemoryPoolInfo> mem_pool_info(memory_pool_count); - std::memcpy(mem_pool_info.data(), - input_params.data() + sizeof(UpdateDataHeader) + config.behavior_size, - memory_pool_count * sizeof(MemoryPoolInfo)); - - // Copy voice resources - const std::size_t voice_resource_offset{sizeof(UpdateDataHeader) + config.behavior_size + - config.memory_pools_size}; - std::memcpy(voice_resources.data(), input_params.data() + voice_resource_offset, - sizeof(VoiceResourceInformation) * voice_resources.size()); - - // Copy VoiceInfo structs - std::size_t voice_offset{sizeof(UpdateDataHeader) + config.behavior_size + - config.memory_pools_size + config.voice_resource_size}; - for (auto& voice : voices) { - std::memcpy(&voice.GetInfo(), input_params.data() + voice_offset, sizeof(VoiceInfo)); - voice_offset += sizeof(VoiceInfo); - } - - std::size_t effect_offset{sizeof(UpdateDataHeader) + config.behavior_size + - config.memory_pools_size + config.voice_resource_size + - config.voices_size}; - for (auto& effect : effects) { - std::memcpy(&effect.GetInfo(), input_params.data() + effect_offset, sizeof(EffectInStatus)); - effect_offset += sizeof(EffectInStatus); - } - - // Update memory pool state - std::vector<MemoryPoolEntry> memory_pool(memory_pool_count); - for (std::size_t index = 0; index < memory_pool.size(); ++index) { - if (mem_pool_info[index].pool_state == MemoryPoolStates::RequestAttach) { - memory_pool[index].state = MemoryPoolStates::Attached; - } else if (mem_pool_info[index].pool_state == MemoryPoolStates::RequestDetach) { - memory_pool[index].state = MemoryPoolStates::Detached; - } - } - - // Update voices - for (auto& voice : voices) { - voice.UpdateState(); - if (!voice.GetInfo().is_in_use) { - continue; - } - if (voice.GetInfo().is_new) { - voice.SetWaveIndex(voice.GetInfo().wave_buffer_head); - } - } - - for (auto& effect : effects) { - effect.UpdateState(memory); - } - - // Release previous buffers and queue next ones for playback - ReleaseAndQueueBuffers(); - - // Copy output header - UpdateDataHeader response_data{worker_params}; - if (behavior_info.IsElapsedFrameCountSupported()) { - response_data.render_info = sizeof(RendererInfo); - response_data.total_size += sizeof(RendererInfo); - } - - std::vector<u8> output_params(response_data.total_size); - std::memcpy(output_params.data(), &response_data, sizeof(UpdateDataHeader)); - - // Copy output memory pool entries - std::memcpy(output_params.data() + sizeof(UpdateDataHeader), memory_pool.data(), - response_data.memory_pools_size); - - // Copy output voice status - std::size_t voice_out_status_offset{sizeof(UpdateDataHeader) + response_data.memory_pools_size}; - for (const auto& voice : voices) { - std::memcpy(output_params.data() + voice_out_status_offset, &voice.GetOutStatus(), - sizeof(VoiceOutStatus)); - voice_out_status_offset += sizeof(VoiceOutStatus); - } +static constexpr s16 ClampToS16(s32 value) { + return static_cast<s16>(std::clamp(value, -32768, 32767)); +} - std::size_t effect_out_status_offset{ - sizeof(UpdateDataHeader) + response_data.memory_pools_size + response_data.voices_size + - response_data.voice_resource_size}; - for (const auto& effect : effects) { - std::memcpy(output_params.data() + effect_out_status_offset, &effect.GetOutStatus(), - sizeof(EffectOutStatus)); - effect_out_status_offset += sizeof(EffectOutStatus); - } +ResultCode AudioRenderer::UpdateAudioRenderer(const std::vector<u8>& input_params, + std::vector<u8>& output_params) { - // Update behavior info output - const std::size_t behavior_out_status_offset{ - sizeof(UpdateDataHeader) + response_data.memory_pools_size + response_data.voices_size + - response_data.effects_size + response_data.sinks_size + - response_data.performance_manager_size}; + InfoUpdater info_updater{input_params, output_params, behavior_info}; - if (!behavior_info.UpdateOutput(output_params, behavior_out_status_offset)) { - LOG_ERROR(Audio, "Failed to update behavior info output parameters"); - return Audren::ERR_INVALID_PARAMETERS; + if (!info_updater.UpdateBehaviorInfo(behavior_info)) { + LOG_ERROR(Audio, "Failed to update behavior info input parameters"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - if (behavior_info.IsElapsedFrameCountSupported()) { - const std::size_t renderer_info_offset{ - sizeof(UpdateDataHeader) + response_data.memory_pools_size + response_data.voices_size + - response_data.effects_size + response_data.sinks_size + - response_data.performance_manager_size + response_data.behavior_size}; - RendererInfo renderer_info{}; - renderer_info.elasped_frame_count = elapsed_frame_count; - std::memcpy(output_params.data() + renderer_info_offset, &renderer_info, - sizeof(RendererInfo)); + if (!info_updater.UpdateMemoryPools(memory_pool_info)) { + LOG_ERROR(Audio, "Failed to update memory pool parameters"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - return MakeResult(output_params); -} - -void AudioRenderer::VoiceState::SetWaveIndex(std::size_t index) { - wave_index = index & 3; - is_refresh_pending = true; -} - -std::vector<s16> AudioRenderer::VoiceState::DequeueSamples( - std::size_t sample_count, Core::Memory::Memory& memory, - const VoiceChannelHolder& voice_resources) { - if (!IsPlaying()) { - return {}; + if (!info_updater.UpdateVoiceChannelResources(voice_context)) { + LOG_ERROR(Audio, "Failed to update voice channel resource parameters"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - if (is_refresh_pending) { - RefreshBuffer(memory, voice_resources); + if (!info_updater.UpdateVoices(voice_context, memory_pool_info, 0)) { + LOG_ERROR(Audio, "Failed to update voice parameters"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - const std::size_t max_size{samples.size() - offset}; - const std::size_t dequeue_offset{offset}; - std::size_t size{sample_count * STREAM_NUM_CHANNELS}; - if (size > max_size) { - size = max_size; + // TODO(ogniK): Deal with stopped audio renderer but updates still taking place + if (!info_updater.UpdateEffects(effect_context, true)) { + LOG_ERROR(Audio, "Failed to update effect parameters"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - out_status.played_sample_count += size / STREAM_NUM_CHANNELS; - offset += size; - - const auto& wave_buffer{info.wave_buffer[wave_index]}; - if (offset == samples.size()) { - offset = 0; - - if (!wave_buffer.is_looping && wave_buffer.buffer_sz) { - SetWaveIndex(wave_index + 1); - } - - if (wave_buffer.buffer_sz) { - out_status.wave_buffer_consumed++; - } - - if (wave_buffer.end_of_stream || wave_buffer.buffer_sz == 0) { - info.play_state = PlayState::Paused; + if (behavior_info.IsSplitterSupported()) { + if (!info_updater.UpdateSplitterInfo(splitter_context)) { + LOG_ERROR(Audio, "Failed to update splitter parameters"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } } - return {samples.begin() + dequeue_offset, samples.begin() + dequeue_offset + size}; -} + auto mix_result = info_updater.UpdateMixes(mix_context, worker_params.mix_buffer_count, + splitter_context, effect_context); -void AudioRenderer::VoiceState::UpdateState() { - if (is_in_use && !info.is_in_use) { - // No longer in use, reset state - is_refresh_pending = true; - wave_index = 0; - offset = 0; - out_status = {}; + if (mix_result.IsError()) { + LOG_ERROR(Audio, "Failed to update mix parameters"); + return mix_result; } - is_in_use = info.is_in_use; -} -void AudioRenderer::VoiceState::RefreshBuffer(Core::Memory::Memory& memory, - const VoiceChannelHolder& voice_resources) { - const auto wave_buffer_address = info.wave_buffer[wave_index].buffer_addr; - const auto wave_buffer_size = info.wave_buffer[wave_index].buffer_sz; - std::vector<s16> new_samples(wave_buffer_size / sizeof(s16)); - memory.ReadBlock(wave_buffer_address, new_samples.data(), wave_buffer_size); - - switch (static_cast<Codec::PcmFormat>(info.sample_format)) { - case Codec::PcmFormat::Int16: { - // PCM16 is played as-is - break; - } - case Codec::PcmFormat::Adpcm: { - // Decode ADPCM to PCM16 - Codec::ADPCM_Coeff coeffs; - memory.ReadBlock(info.additional_params_addr, coeffs.data(), sizeof(Codec::ADPCM_Coeff)); - new_samples = Codec::DecodeADPCM(reinterpret_cast<u8*>(new_samples.data()), - new_samples.size() * sizeof(s16), coeffs, adpcm_state); - break; + // TODO(ogniK): Sinks + if (!info_updater.UpdateSinks(sink_context)) { + LOG_ERROR(Audio, "Failed to update sink parameters"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - default: - UNIMPLEMENTED_MSG("Unimplemented sample_format={}", info.sample_format); - break; - } - - switch (info.channel_count) { - case 1: { - // 1 channel is upsampled to 2 channel - samples.resize(new_samples.size() * 2); - for (std::size_t index = 0; index < new_samples.size(); ++index) { - auto sample = static_cast<float>(new_samples[index]); - if (voice_resources[0]->in_use) { - sample *= voice_resources[0]->mix_volumes[0]; - } - - samples[index * 2] = static_cast<s16>(sample * info.volume); - samples[index * 2 + 1] = static_cast<s16>(sample * info.volume); - } - break; + // TODO(ogniK): Performance buffer + if (!info_updater.UpdatePerformanceBuffer()) { + LOG_ERROR(Audio, "Failed to update performance buffer parameters"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - case 2: { - // 2 channel is played as is - samples = std::move(new_samples); - const std::size_t sample_count = (samples.size() / 2); - for (std::size_t index = 0; index < sample_count; ++index) { - const std::size_t index_l = index * 2; - const std::size_t index_r = index * 2 + 1; - - auto sample_l = static_cast<float>(samples[index_l]); - auto sample_r = static_cast<float>(samples[index_r]); - - if (voice_resources[0]->in_use) { - sample_l *= voice_resources[0]->mix_volumes[0]; - } - - if (voice_resources[1]->in_use) { - sample_r *= voice_resources[1]->mix_volumes[1]; - } - samples[index_l] = static_cast<s16>(sample_l * info.volume); - samples[index_r] = static_cast<s16>(sample_r * info.volume); - } - break; + if (!info_updater.UpdateErrorInfo(behavior_info)) { + LOG_ERROR(Audio, "Failed to update error info"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - case 6: { - samples.resize((new_samples.size() / 6) * 2); - const std::size_t sample_count = samples.size() / 2; - - for (std::size_t index = 0; index < sample_count; ++index) { - auto FL = static_cast<float>(new_samples[index * 6]); - auto FR = static_cast<float>(new_samples[index * 6 + 1]); - auto FC = static_cast<float>(new_samples[index * 6 + 2]); - auto BL = static_cast<float>(new_samples[index * 6 + 4]); - auto BR = static_cast<float>(new_samples[index * 6 + 5]); - - if (voice_resources[0]->in_use) { - FL *= voice_resources[0]->mix_volumes[0]; - } - if (voice_resources[1]->in_use) { - FR *= voice_resources[1]->mix_volumes[1]; - } - if (voice_resources[2]->in_use) { - FC *= voice_resources[2]->mix_volumes[2]; - } - if (voice_resources[4]->in_use) { - BL *= voice_resources[4]->mix_volumes[4]; - } - if (voice_resources[5]->in_use) { - BR *= voice_resources[5]->mix_volumes[5]; - } - samples[index * 2] = - static_cast<s16>((0.3694f * FL + 0.2612f * FC + 0.3694f * BL) * info.volume); - samples[index * 2 + 1] = - static_cast<s16>((0.3694f * FR + 0.2612f * FC + 0.3694f * BR) * info.volume); + if (behavior_info.IsElapsedFrameCountSupported()) { + if (!info_updater.UpdateRendererInfo(elapsed_frame_count)) { + LOG_ERROR(Audio, "Failed to update renderer info"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - break; - } - default: - UNIMPLEMENTED_MSG("Unimplemented channel_count={}", info.channel_count); - break; } + // TODO(ogniK): Statistics - // Only interpolate when necessary, expensive. - if (GetInfo().sample_rate != STREAM_SAMPLE_RATE) { - samples = Interpolate(interp_state, std::move(samples), GetInfo().sample_rate, - STREAM_SAMPLE_RATE); + if (!info_updater.WriteOutputHeader()) { + LOG_ERROR(Audio, "Failed to write output header"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } - is_refresh_pending = false; -} + // TODO(ogniK): Check when all sections are implemented -void AudioRenderer::EffectState::UpdateState(Core::Memory::Memory& memory) { - if (info.is_new) { - out_status.state = EffectStatus::New; - } else { - if (info.type == Effect::Aux) { - ASSERT_MSG(memory.Read32(info.aux_info.return_buffer_info) == 0, - "Aux buffers tried to update"); - ASSERT_MSG(memory.Read32(info.aux_info.send_buffer_info) == 0, - "Aux buffers tried to update"); - ASSERT_MSG(memory.Read32(info.aux_info.return_buffer_base) == 0, - "Aux buffers tried to update"); - ASSERT_MSG(memory.Read32(info.aux_info.send_buffer_base) == 0, - "Aux buffers tried to update"); - } + if (!info_updater.CheckConsumedSize()) { + LOG_ERROR(Audio, "Audio buffers were not consumed!"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; } -} -static constexpr s16 ClampToS16(s32 value) { - return static_cast<s16>(std::clamp(value, -32768, 32767)); + ReleaseAndQueueBuffers(); + + return RESULT_SUCCESS; } void AudioRenderer::QueueMixedBuffer(Buffer::Tag tag) { - constexpr std::size_t BUFFER_SIZE{512}; + command_generator.PreCommand(); + // Clear mix buffers before our next operation + command_generator.ClearMixBuffers(); + + // If the splitter is not in use, sort our mixes + if (!splitter_context.UsingSplitter()) { + mix_context.SortInfo(); + } + // Sort our voices + voice_context.SortInfo(); + + // Handle samples + command_generator.GenerateVoiceCommands(); + command_generator.GenerateSubMixCommands(); + command_generator.GenerateFinalMixCommands(); + + command_generator.PostCommand(); + // Base sample size + std::size_t BUFFER_SIZE{worker_params.sample_count}; + // Samples std::vector<s16> buffer(BUFFER_SIZE * stream->GetNumChannels()); - - for (auto& voice : voices) { - if (!voice.IsPlaying()) { - continue; - } - VoiceChannelHolder resources{}; - for (u32 channel = 0; channel < voice.GetInfo().channel_count; channel++) { - const auto channel_resource_id = voice.GetInfo().voice_channel_resource_ids[channel]; - resources[channel] = &voice_resources[channel_resource_id]; + // Make sure to clear our samples + std::memset(buffer.data(), 0, buffer.size() * sizeof(s16)); + + if (sink_context.InUse()) { + const auto stream_channel_count = stream->GetNumChannels(); + const auto buffer_offsets = sink_context.OutputBuffers(); + const auto channel_count = buffer_offsets.size(); + const auto& final_mix = mix_context.GetFinalMixInfo(); + const auto& in_params = final_mix.GetInParams(); + std::vector<s32*> mix_buffers(channel_count); + for (std::size_t i = 0; i < channel_count; i++) { + mix_buffers[i] = + command_generator.GetMixBuffer(in_params.buffer_offset + buffer_offsets[i]); } - std::size_t offset{}; - s64 samples_remaining{BUFFER_SIZE}; - while (samples_remaining > 0) { - const std::vector<s16> samples{ - voice.DequeueSamples(samples_remaining, memory, resources)}; - - if (samples.empty()) { - break; - } - - samples_remaining -= samples.size() / stream->GetNumChannels(); - - for (const auto& sample : samples) { - const s32 buffer_sample{buffer[offset]}; - buffer[offset++] = - ClampToS16(buffer_sample + static_cast<s32>(sample * voice.GetInfo().volume)); + for (std::size_t i = 0; i < BUFFER_SIZE; i++) { + if (channel_count == 1) { + const auto sample = ClampToS16(mix_buffers[0][i]); + buffer[i * stream_channel_count + 0] = sample; + if (stream_channel_count > 1) { + buffer[i * stream_channel_count + 1] = sample; + } + if (stream_channel_count == 6) { + buffer[i * stream_channel_count + 2] = sample; + buffer[i * stream_channel_count + 4] = sample; + buffer[i * stream_channel_count + 5] = sample; + } + } else if (channel_count == 2) { + const auto l_sample = ClampToS16(mix_buffers[0][i]); + const auto r_sample = ClampToS16(mix_buffers[1][i]); + if (stream_channel_count == 1) { + buffer[i * stream_channel_count + 0] = l_sample; + } else if (stream_channel_count == 2) { + buffer[i * stream_channel_count + 0] = l_sample; + buffer[i * stream_channel_count + 1] = r_sample; + } else if (stream_channel_count == 6) { + buffer[i * stream_channel_count + 0] = l_sample; + buffer[i * stream_channel_count + 1] = r_sample; + + buffer[i * stream_channel_count + 2] = + ClampToS16((static_cast<s32>(l_sample) + static_cast<s32>(r_sample)) / 2); + + buffer[i * stream_channel_count + 4] = l_sample; + buffer[i * stream_channel_count + 5] = r_sample; + } + + } else if (channel_count == 6) { + const auto fl_sample = ClampToS16(mix_buffers[0][i]); + const auto fr_sample = ClampToS16(mix_buffers[1][i]); + const auto fc_sample = ClampToS16(mix_buffers[2][i]); + const auto lf_sample = ClampToS16(mix_buffers[3][i]); + const auto bl_sample = ClampToS16(mix_buffers[4][i]); + const auto br_sample = ClampToS16(mix_buffers[5][i]); + + if (stream_channel_count == 1) { + buffer[i * stream_channel_count + 0] = fc_sample; + } else if (stream_channel_count == 2) { + buffer[i * stream_channel_count + 0] = + static_cast<s16>(0.3694f * static_cast<float>(fl_sample) + + 0.2612f * static_cast<float>(fc_sample) + + 0.3694f * static_cast<float>(bl_sample)); + buffer[i * stream_channel_count + 1] = + static_cast<s16>(0.3694f * static_cast<float>(fr_sample) + + 0.2612f * static_cast<float>(fc_sample) + + 0.3694f * static_cast<float>(br_sample)); + } else if (stream_channel_count == 6) { + buffer[i * stream_channel_count + 0] = fl_sample; + buffer[i * stream_channel_count + 1] = fr_sample; + buffer[i * stream_channel_count + 2] = fc_sample; + buffer[i * stream_channel_count + 3] = lf_sample; + buffer[i * stream_channel_count + 4] = bl_sample; + buffer[i * stream_channel_count + 5] = br_sample; + } } } } + audio_out->QueueBuffer(stream, tag, std::move(buffer)); elapsed_frame_count++; + voice_context.UpdateStateByDspShared(); } void AudioRenderer::ReleaseAndQueueBuffers() { diff --git a/src/audio_core/audio_renderer.h b/src/audio_core/audio_renderer.h index f0b691a86..2bca795ba 100644 --- a/src/audio_core/audio_renderer.h +++ b/src/audio_core/audio_renderer.h @@ -9,8 +9,15 @@ #include <vector> #include "audio_core/behavior_info.h" +#include "audio_core/command_generator.h" #include "audio_core/common.h" +#include "audio_core/effect_context.h" +#include "audio_core/memory_pool.h" +#include "audio_core/mix_context.h" +#include "audio_core/sink_context.h" +#include "audio_core/splitter_context.h" #include "audio_core/stream.h" +#include "audio_core/voice_context.h" #include "common/common_funcs.h" #include "common/common_types.h" #include "common/swap.h" @@ -30,220 +37,25 @@ class Memory; } namespace AudioCore { +using DSPStateHolder = std::array<VoiceState*, 6>; class AudioOut; -enum class PlayState : u8 { - Started = 0, - Stopped = 1, - Paused = 2, -}; - -enum class Effect : u8 { - None = 0, - Aux = 2, -}; - -enum class EffectStatus : u8 { - None = 0, - New = 1, -}; - -struct AudioRendererParameter { - u32_le sample_rate; - u32_le sample_count; - u32_le mix_buffer_count; - u32_le submix_count; - u32_le voice_count; - u32_le sink_count; - u32_le effect_count; - u32_le performance_frame_count; - u8 is_voice_drop_enabled; - u8 unknown_21; - u8 unknown_22; - u8 execution_mode; - u32_le splitter_count; - u32_le num_splitter_send_channels; - u32_le unknown_30; - u32_le revision; -}; -static_assert(sizeof(AudioRendererParameter) == 52, "AudioRendererParameter is an invalid size"); - -enum class MemoryPoolStates : u32 { // Should be LE - Invalid = 0x0, - Unknown = 0x1, - RequestDetach = 0x2, - Detached = 0x3, - RequestAttach = 0x4, - Attached = 0x5, - Released = 0x6, -}; - -struct MemoryPoolEntry { - MemoryPoolStates state; - u32_le unknown_4; - u32_le unknown_8; - u32_le unknown_c; -}; -static_assert(sizeof(MemoryPoolEntry) == 0x10, "MemoryPoolEntry has wrong size"); - -struct MemoryPoolInfo { - u64_le pool_address; - u64_le pool_size; - MemoryPoolStates pool_state; - INSERT_PADDING_WORDS(3); // Unknown -}; -static_assert(sizeof(MemoryPoolInfo) == 0x20, "MemoryPoolInfo has wrong size"); -struct BiquadFilter { - u8 enable; - INSERT_PADDING_BYTES(1); - std::array<s16_le, 3> numerator; - std::array<s16_le, 2> denominator; -}; -static_assert(sizeof(BiquadFilter) == 0xc, "BiquadFilter has wrong size"); - -struct WaveBuffer { - u64_le buffer_addr; - u64_le buffer_sz; - s32_le start_sample_offset; - s32_le end_sample_offset; - u8 is_looping; - u8 end_of_stream; - u8 sent_to_server; - INSERT_PADDING_BYTES(5); - u64 context_addr; - u64 context_sz; - INSERT_PADDING_BYTES(8); -}; -static_assert(sizeof(WaveBuffer) == 0x38, "WaveBuffer has wrong size"); - -struct VoiceResourceInformation { - s32_le id{}; - std::array<float_le, MAX_MIX_BUFFERS> mix_volumes{}; - bool in_use{}; - INSERT_PADDING_BYTES(11); -}; -static_assert(sizeof(VoiceResourceInformation) == 0x70, "VoiceResourceInformation has wrong size"); - -struct VoiceInfo { - u32_le id; - u32_le node_id; - u8 is_new; - u8 is_in_use; - PlayState play_state; - u8 sample_format; - u32_le sample_rate; - u32_le priority; - u32_le sorting_order; - u32_le channel_count; - float_le pitch; - float_le volume; - std::array<BiquadFilter, 2> biquad_filter; - u32_le wave_buffer_count; - u32_le wave_buffer_head; - INSERT_PADDING_WORDS(1); - u64_le additional_params_addr; - u64_le additional_params_sz; - u32_le mix_id; - u32_le splitter_info_id; - std::array<WaveBuffer, 4> wave_buffer; - std::array<u32_le, 6> voice_channel_resource_ids; - INSERT_PADDING_BYTES(24); -}; -static_assert(sizeof(VoiceInfo) == 0x170, "VoiceInfo is wrong size"); - -struct VoiceOutStatus { - u64_le played_sample_count; - u32_le wave_buffer_consumed; - u32_le voice_drops_count; -}; -static_assert(sizeof(VoiceOutStatus) == 0x10, "VoiceOutStatus has wrong size"); - -struct AuxInfo { - std::array<u8, 24> input_mix_buffers; - std::array<u8, 24> output_mix_buffers; - u32_le mix_buffer_count; - u32_le sample_rate; // Stored in the aux buffer currently - u32_le sample_count; - u64_le send_buffer_info; - u64_le send_buffer_base; - - u64_le return_buffer_info; - u64_le return_buffer_base; -}; -static_assert(sizeof(AuxInfo) == 0x60, "AuxInfo is an invalid size"); - -struct EffectInStatus { - Effect type; - u8 is_new; - u8 is_enabled; - INSERT_PADDING_BYTES(1); - u32_le mix_id; - u64_le buffer_base; - u64_le buffer_sz; - s32_le priority; - INSERT_PADDING_BYTES(4); - union { - std::array<u8, 0xa0> raw; - AuxInfo aux_info; - }; -}; -static_assert(sizeof(EffectInStatus) == 0xc0, "EffectInStatus is an invalid size"); - -struct EffectOutStatus { - EffectStatus state; - INSERT_PADDING_BYTES(0xf); -}; -static_assert(sizeof(EffectOutStatus) == 0x10, "EffectOutStatus is an invalid size"); - struct RendererInfo { u64_le elasped_frame_count{}; INSERT_PADDING_WORDS(2); }; static_assert(sizeof(RendererInfo) == 0x10, "RendererInfo is an invalid size"); -struct UpdateDataHeader { - UpdateDataHeader() {} - - explicit UpdateDataHeader(const AudioRendererParameter& config) { - revision = Common::MakeMagic('R', 'E', 'V', '8'); // 9.2.0 Revision - behavior_size = 0xb0; - memory_pools_size = (config.effect_count + (config.voice_count * 4)) * 0x10; - voices_size = config.voice_count * 0x10; - voice_resource_size = 0x0; - effects_size = config.effect_count * 0x10; - mixes_size = 0x0; - sinks_size = config.sink_count * 0x20; - performance_manager_size = 0x10; - render_info = 0; - total_size = sizeof(UpdateDataHeader) + behavior_size + memory_pools_size + voices_size + - effects_size + sinks_size + performance_manager_size; - } - - u32_le revision{}; - u32_le behavior_size{}; - u32_le memory_pools_size{}; - u32_le voices_size{}; - u32_le voice_resource_size{}; - u32_le effects_size{}; - u32_le mixes_size{}; - u32_le sinks_size{}; - u32_le performance_manager_size{}; - u32_le splitter_size{}; - u32_le render_info{}; - INSERT_PADDING_WORDS(4); - u32_le total_size{}; -}; -static_assert(sizeof(UpdateDataHeader) == 0x40, "UpdateDataHeader has wrong size"); - class AudioRenderer { public: AudioRenderer(Core::Timing::CoreTiming& core_timing, Core::Memory::Memory& memory_, - AudioRendererParameter params, + AudioCommon::AudioRendererParameter params, std::shared_ptr<Kernel::WritableEvent> buffer_event, std::size_t instance_number); ~AudioRenderer(); - ResultVal<std::vector<u8>> UpdateAudioRenderer(const std::vector<u8>& input_params); + ResultCode UpdateAudioRenderer(const std::vector<u8>& input_params, + std::vector<u8>& output_params); void QueueMixedBuffer(Buffer::Tag tag); void ReleaseAndQueueBuffers(); u32 GetSampleRate() const; @@ -252,19 +64,23 @@ public: Stream::State GetStreamState() const; private: - class EffectState; - class VoiceState; BehaviorInfo behavior_info{}; - AudioRendererParameter worker_params; + AudioCommon::AudioRendererParameter worker_params; std::shared_ptr<Kernel::WritableEvent> buffer_event; + std::vector<ServerMemoryPoolInfo> memory_pool_info; + VoiceContext voice_context; + EffectContext effect_context; + MixContext mix_context; + SinkContext sink_context; + SplitterContext splitter_context; std::vector<VoiceState> voices; - std::vector<VoiceResourceInformation> voice_resources; - std::vector<EffectState> effects; std::unique_ptr<AudioOut> audio_out; StreamPtr stream; Core::Memory::Memory& memory; + CommandGenerator command_generator; std::size_t elapsed_frame_count{}; + std::vector<s32> temp_mix_buffer{}; }; } // namespace AudioCore diff --git a/src/audio_core/behavior_info.cpp b/src/audio_core/behavior_info.cpp index 94b7a3bf1..5d62adb0b 100644 --- a/src/audio_core/behavior_info.cpp +++ b/src/audio_core/behavior_info.cpp @@ -9,39 +9,11 @@ namespace AudioCore { -BehaviorInfo::BehaviorInfo() : process_revision(CURRENT_PROCESS_REVISION) {} +BehaviorInfo::BehaviorInfo() : process_revision(AudioCommon::CURRENT_PROCESS_REVISION) {} BehaviorInfo::~BehaviorInfo() = default; -bool BehaviorInfo::UpdateInput(const std::vector<u8>& buffer, std::size_t offset) { - if (!CanConsumeBuffer(buffer.size(), offset, sizeof(InParams))) { - LOG_ERROR(Audio, "Buffer is an invalid size!"); - return false; - } - InParams params{}; - std::memcpy(¶ms, buffer.data() + offset, sizeof(InParams)); - - if (!IsValidRevision(params.revision)) { - LOG_ERROR(Audio, "Invalid input revision, revision=0x{:08X}", params.revision); - return false; - } - - if (user_revision != params.revision) { - LOG_ERROR(Audio, - "User revision differs from input revision, expecting 0x{:08X} but got 0x{:08X}", - user_revision, params.revision); - return false; - } - - ClearError(); - UpdateFlags(params.flags); - - // TODO(ogniK): Check input params size when InfoUpdater is used - - return true; -} - bool BehaviorInfo::UpdateOutput(std::vector<u8>& buffer, std::size_t offset) { - if (!CanConsumeBuffer(buffer.size(), offset, sizeof(OutParams))) { + if (!AudioCommon::CanConsumeBuffer(buffer.size(), offset, sizeof(OutParams))) { LOG_ERROR(Audio, "Buffer is an invalid size!"); return false; } @@ -65,36 +37,69 @@ void BehaviorInfo::SetUserRevision(u32_le revision) { user_revision = revision; } +u32_le BehaviorInfo::GetUserRevision() const { + return user_revision; +} + +u32_le BehaviorInfo::GetProcessRevision() const { + return process_revision; +} + bool BehaviorInfo::IsAdpcmLoopContextBugFixed() const { - return IsRevisionSupported(2, user_revision); + return AudioCommon::IsRevisionSupported(2, user_revision); } bool BehaviorInfo::IsSplitterSupported() const { - return IsRevisionSupported(2, user_revision); + return AudioCommon::IsRevisionSupported(2, user_revision); } bool BehaviorInfo::IsLongSizePreDelaySupported() const { - return IsRevisionSupported(3, user_revision); + return AudioCommon::IsRevisionSupported(3, user_revision); } bool BehaviorInfo::IsAudioRenererProcessingTimeLimit80PercentSupported() const { - return IsRevisionSupported(5, user_revision); + return AudioCommon::IsRevisionSupported(5, user_revision); } bool BehaviorInfo::IsAudioRenererProcessingTimeLimit75PercentSupported() const { - return IsRevisionSupported(4, user_revision); + return AudioCommon::IsRevisionSupported(4, user_revision); } bool BehaviorInfo::IsAudioRenererProcessingTimeLimit70PercentSupported() const { - return IsRevisionSupported(1, user_revision); + return AudioCommon::IsRevisionSupported(1, user_revision); } bool BehaviorInfo::IsElapsedFrameCountSupported() const { - return IsRevisionSupported(5, user_revision); + return AudioCommon::IsRevisionSupported(5, user_revision); } bool BehaviorInfo::IsMemoryPoolForceMappingEnabled() const { return (flags & 1) != 0; } +bool BehaviorInfo::IsFlushVoiceWaveBuffersSupported() const { + return AudioCommon::IsRevisionSupported(5, user_revision); +} + +bool BehaviorInfo::IsVoicePlayedSampleCountResetAtLoopPointSupported() const { + return AudioCommon::IsRevisionSupported(5, user_revision); +} + +bool BehaviorInfo::IsVoicePitchAndSrcSkippedSupported() const { + return AudioCommon::IsRevisionSupported(5, user_revision); +} + +bool BehaviorInfo::IsMixInParameterDirtyOnlyUpdateSupported() const { + return AudioCommon::IsRevisionSupported(7, user_revision); +} + +bool BehaviorInfo::IsSplitterBugFixed() const { + return AudioCommon::IsRevisionSupported(5, user_revision); +} + +void BehaviorInfo::CopyErrorInfo(BehaviorInfo::OutParams& dst) { + dst.error_count = static_cast<u32>(error_count); + std::copy(errors.begin(), errors.begin() + error_count, dst.errors.begin()); +} + } // namespace AudioCore diff --git a/src/audio_core/behavior_info.h b/src/audio_core/behavior_info.h index c5e91ab39..50948e8df 100644 --- a/src/audio_core/behavior_info.h +++ b/src/audio_core/behavior_info.h @@ -14,15 +14,37 @@ namespace AudioCore { class BehaviorInfo { public: + struct ErrorInfo { + u32_le result{}; + INSERT_PADDING_WORDS(1); + u64_le result_info{}; + }; + static_assert(sizeof(ErrorInfo) == 0x10, "ErrorInfo is an invalid size"); + + struct InParams { + u32_le revision{}; + u32_le padding{}; + u64_le flags{}; + }; + static_assert(sizeof(InParams) == 0x10, "InParams is an invalid size"); + + struct OutParams { + std::array<ErrorInfo, 10> errors{}; + u32_le error_count{}; + INSERT_PADDING_BYTES(12); + }; + static_assert(sizeof(OutParams) == 0xb0, "OutParams is an invalid size"); + explicit BehaviorInfo(); ~BehaviorInfo(); - bool UpdateInput(const std::vector<u8>& buffer, std::size_t offset); bool UpdateOutput(std::vector<u8>& buffer, std::size_t offset); void ClearError(); void UpdateFlags(u64_le dest_flags); void SetUserRevision(u32_le revision); + u32_le GetUserRevision() const; + u32_le GetProcessRevision() const; bool IsAdpcmLoopContextBugFixed() const; bool IsSplitterSupported() const; @@ -32,35 +54,19 @@ public: bool IsAudioRenererProcessingTimeLimit70PercentSupported() const; bool IsElapsedFrameCountSupported() const; bool IsMemoryPoolForceMappingEnabled() const; + bool IsFlushVoiceWaveBuffersSupported() const; + bool IsVoicePlayedSampleCountResetAtLoopPointSupported() const; + bool IsVoicePitchAndSrcSkippedSupported() const; + bool IsMixInParameterDirtyOnlyUpdateSupported() const; + bool IsSplitterBugFixed() const; + void CopyErrorInfo(OutParams& dst); private: u32_le process_revision{}; u32_le user_revision{}; u64_le flags{}; - - struct ErrorInfo { - u32_le result{}; - INSERT_PADDING_WORDS(1); - u64_le result_info{}; - }; - static_assert(sizeof(ErrorInfo) == 0x10, "ErrorInfo is an invalid size"); - std::array<ErrorInfo, 10> errors{}; std::size_t error_count{}; - - struct InParams { - u32_le revision{}; - u32_le padding{}; - u64_le flags{}; - }; - static_assert(sizeof(InParams) == 0x10, "InParams is an invalid size"); - - struct OutParams { - std::array<ErrorInfo, 10> errors{}; - u32_le error_count{}; - INSERT_PADDING_BYTES(12); - }; - static_assert(sizeof(OutParams) == 0xb0, "OutParams is an invalid size"); }; } // namespace AudioCore diff --git a/src/audio_core/command_generator.cpp b/src/audio_core/command_generator.cpp new file mode 100644 index 000000000..8f7da49e6 --- /dev/null +++ b/src/audio_core/command_generator.cpp @@ -0,0 +1,977 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "audio_core/algorithm/interpolate.h" +#include "audio_core/command_generator.h" +#include "audio_core/effect_context.h" +#include "audio_core/mix_context.h" +#include "audio_core/voice_context.h" +#include "core/memory.h" + +namespace AudioCore { +namespace { +constexpr std::size_t MIX_BUFFER_SIZE = 0x3f00; +constexpr std::size_t SCALED_MIX_BUFFER_SIZE = MIX_BUFFER_SIZE << 15ULL; + +template <std::size_t N> +void ApplyMix(s32* output, const s32* input, s32 gain, s32 sample_count) { + for (std::size_t i = 0; i < static_cast<std::size_t>(sample_count); i += N) { + for (std::size_t j = 0; j < N; j++) { + output[i + j] += + static_cast<s32>((static_cast<s64>(input[i + j]) * gain + 0x4000) >> 15); + } + } +} + +s32 ApplyMixRamp(s32* output, const s32* input, float gain, float delta, s32 sample_count) { + s32 x = 0; + for (s32 i = 0; i < sample_count; i++) { + x = static_cast<s32>(static_cast<float>(input[i]) * gain); + output[i] += x; + gain += delta; + } + return x; +} + +void ApplyGain(s32* output, const s32* input, s32 gain, s32 delta, s32 sample_count) { + for (s32 i = 0; i < sample_count; i++) { + output[i] = static_cast<s32>((static_cast<s64>(input[i]) * gain + 0x4000) >> 15); + gain += delta; + } +} + +void ApplyGainWithoutDelta(s32* output, const s32* input, s32 gain, s32 sample_count) { + for (s32 i = 0; i < sample_count; i++) { + output[i] = static_cast<s32>((static_cast<s64>(input[i]) * gain + 0x4000) >> 15); + } +} + +s32 ApplyMixDepop(s32* output, s32 first_sample, s32 delta, s32 sample_count) { + const bool positive = first_sample > 0; + auto final_sample = std::abs(first_sample); + for (s32 i = 0; i < sample_count; i++) { + final_sample = static_cast<s32>((static_cast<s64>(final_sample) * delta) >> 15); + if (positive) { + output[i] += final_sample; + } else { + output[i] -= final_sample; + } + } + if (positive) { + return final_sample; + } else { + return -final_sample; + } +} + +} // namespace + +CommandGenerator::CommandGenerator(AudioCommon::AudioRendererParameter& worker_params, + VoiceContext& voice_context, MixContext& mix_context, + SplitterContext& splitter_context, EffectContext& effect_context, + Core::Memory::Memory& memory) + : worker_params(worker_params), voice_context(voice_context), mix_context(mix_context), + splitter_context(splitter_context), effect_context(effect_context), memory(memory), + mix_buffer((worker_params.mix_buffer_count + AudioCommon::MAX_CHANNEL_COUNT) * + worker_params.sample_count), + sample_buffer(MIX_BUFFER_SIZE), + depop_buffer((worker_params.mix_buffer_count + AudioCommon::MAX_CHANNEL_COUNT) * + worker_params.sample_count) {} +CommandGenerator::~CommandGenerator() = default; + +void CommandGenerator::ClearMixBuffers() { + std::fill(mix_buffer.begin(), mix_buffer.end(), 0); + std::fill(sample_buffer.begin(), sample_buffer.end(), 0); + // std::fill(depop_buffer.begin(), depop_buffer.end(), 0); +} + +void CommandGenerator::GenerateVoiceCommands() { + if (dumping_frame) { + LOG_DEBUG(Audio, "(DSP_TRACE) GenerateVoiceCommands"); + } + // Grab all our voices + const auto voice_count = voice_context.GetVoiceCount(); + for (std::size_t i = 0; i < voice_count; i++) { + auto& voice_info = voice_context.GetSortedInfo(i); + // Update voices and check if we should queue them + if (voice_info.ShouldSkip() || !voice_info.UpdateForCommandGeneration(voice_context)) { + continue; + } + + // Queue our voice + GenerateVoiceCommand(voice_info); + } + // Update our splitters + splitter_context.UpdateInternalState(); +} + +void CommandGenerator::GenerateVoiceCommand(ServerVoiceInfo& voice_info) { + auto& in_params = voice_info.GetInParams(); + const auto channel_count = in_params.channel_count; + + for (s32 channel = 0; channel < channel_count; channel++) { + const auto resource_id = in_params.voice_channel_resource_id[channel]; + auto& dsp_state = voice_context.GetDspSharedState(resource_id); + auto& channel_resource = voice_context.GetChannelResource(resource_id); + + // Decode our samples for our channel + GenerateDataSourceCommand(voice_info, dsp_state, channel); + + if (in_params.should_depop) { + in_params.last_volume = 0.0f; + } else if (in_params.splitter_info_id != AudioCommon::NO_SPLITTER || + in_params.mix_id != AudioCommon::NO_MIX) { + // Apply a biquad filter if needed + GenerateBiquadFilterCommandForVoice(voice_info, dsp_state, + worker_params.mix_buffer_count, channel); + // Base voice volume ramping + GenerateVolumeRampCommand(in_params.last_volume, in_params.volume, channel, + in_params.node_id); + in_params.last_volume = in_params.volume; + + if (in_params.mix_id != AudioCommon::NO_MIX) { + // If we're using a mix id + auto& mix_info = mix_context.GetInfo(in_params.mix_id); + const auto& dest_mix_params = mix_info.GetInParams(); + + // Voice Mixing + GenerateVoiceMixCommand( + channel_resource.GetCurrentMixVolume(), channel_resource.GetLastMixVolume(), + dsp_state, dest_mix_params.buffer_offset, dest_mix_params.buffer_count, + worker_params.mix_buffer_count + channel, in_params.node_id); + + // Update last mix volumes + channel_resource.UpdateLastMixVolumes(); + } else if (in_params.splitter_info_id != AudioCommon::NO_SPLITTER) { + s32 base = channel; + while (auto* destination_data = + GetDestinationData(in_params.splitter_info_id, base)) { + base += channel_count; + + if (!destination_data->IsConfigured()) { + continue; + } + if (destination_data->GetMixId() >= mix_context.GetCount()) { + continue; + } + + const auto& mix_info = mix_context.GetInfo(destination_data->GetMixId()); + const auto& dest_mix_params = mix_info.GetInParams(); + GenerateVoiceMixCommand( + destination_data->CurrentMixVolumes(), destination_data->LastMixVolumes(), + dsp_state, dest_mix_params.buffer_offset, dest_mix_params.buffer_count, + worker_params.mix_buffer_count + channel, in_params.node_id); + destination_data->MarkDirty(); + } + } + // Update biquad filter enabled states + for (std::size_t i = 0; i < AudioCommon::MAX_BIQUAD_FILTERS; i++) { + in_params.was_biquad_filter_enabled[i] = in_params.biquad_filter[i].enabled; + } + } + } +} + +void CommandGenerator::GenerateSubMixCommands() { + const auto mix_count = mix_context.GetCount(); + for (std::size_t i = 0; i < mix_count; i++) { + auto& mix_info = mix_context.GetSortedInfo(i); + const auto& in_params = mix_info.GetInParams(); + if (!in_params.in_use || in_params.mix_id == AudioCommon::FINAL_MIX) { + continue; + } + GenerateSubMixCommand(mix_info); + } +} + +void CommandGenerator::GenerateFinalMixCommands() { + GenerateFinalMixCommand(); +} + +void CommandGenerator::PreCommand() { + if (!dumping_frame) { + return; + } + for (std::size_t i = 0; i < splitter_context.GetInfoCount(); i++) { + const auto& base = splitter_context.GetInfo(i); + std::string graph = fmt::format("b[{}]", i); + const auto* head = base.GetHead(); + while (head != nullptr) { + graph += fmt::format("->{}", head->GetMixId()); + head = head->GetNextDestination(); + } + LOG_DEBUG(Audio, "(DSP_TRACE) SplitterGraph splitter_info={}, {}", i, graph); + } +} + +void CommandGenerator::PostCommand() { + if (!dumping_frame) { + return; + } + dumping_frame = false; +} + +void CommandGenerator::GenerateDataSourceCommand(ServerVoiceInfo& voice_info, VoiceState& dsp_state, + s32 channel) { + const auto& in_params = voice_info.GetInParams(); + const auto depop = in_params.should_depop; + + if (depop) { + if (in_params.mix_id != AudioCommon::NO_MIX) { + auto& mix_info = mix_context.GetInfo(in_params.mix_id); + const auto& mix_in = mix_info.GetInParams(); + GenerateDepopPrepareCommand(dsp_state, mix_in.buffer_count, mix_in.buffer_offset); + } else if (in_params.splitter_info_id != AudioCommon::NO_SPLITTER) { + s32 index{}; + while (const auto* destination = + GetDestinationData(in_params.splitter_info_id, index++)) { + if (!destination->IsConfigured()) { + continue; + } + auto& mix_info = mix_context.GetInfo(destination->GetMixId()); + const auto& mix_in = mix_info.GetInParams(); + GenerateDepopPrepareCommand(dsp_state, mix_in.buffer_count, mix_in.buffer_offset); + } + } + } else { + switch (in_params.sample_format) { + case SampleFormat::Pcm16: + DecodeFromWaveBuffers(voice_info, GetChannelMixBuffer(channel), dsp_state, channel, + worker_params.sample_rate, worker_params.sample_count, + in_params.node_id); + break; + case SampleFormat::Adpcm: + ASSERT(channel == 0 && in_params.channel_count == 1); + DecodeFromWaveBuffers(voice_info, GetChannelMixBuffer(0), dsp_state, 0, + worker_params.sample_rate, worker_params.sample_count, + in_params.node_id); + break; + default: + UNREACHABLE_MSG("Unimplemented sample format={}", in_params.sample_format); + } + } +} + +void CommandGenerator::GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voice_info, + VoiceState& dsp_state, + s32 mix_buffer_count, s32 channel) { + for (std::size_t i = 0; i < AudioCommon::MAX_BIQUAD_FILTERS; i++) { + const auto& in_params = voice_info.GetInParams(); + auto& biquad_filter = in_params.biquad_filter[i]; + // Check if biquad filter is actually used + if (!biquad_filter.enabled) { + continue; + } + + // Reinitialize our biquad filter state if it was enabled previously + if (!in_params.was_biquad_filter_enabled[i]) { + dsp_state.biquad_filter_state.fill(0); + } + + // Generate biquad filter + // GenerateBiquadFilterCommand(mix_buffer_count, biquad_filter, + // dsp_state.biquad_filter_state, + // mix_buffer_count + channel, mix_buffer_count + + // channel, worker_params.sample_count, + // voice_info.GetInParams().node_id); + } +} + +void AudioCore::CommandGenerator::GenerateBiquadFilterCommand( + s32 mix_buffer, const BiquadFilterParameter& params, std::array<s64, 2>& state, + std::size_t input_offset, std::size_t output_offset, s32 sample_count, s32 node_id) { + if (dumping_frame) { + LOG_DEBUG(Audio, + "(DSP_TRACE) GenerateBiquadFilterCommand node_id={}, " + "input_mix_buffer={}, output_mix_buffer={}", + node_id, input_offset, output_offset); + } + const auto* input = GetMixBuffer(input_offset); + auto* output = GetMixBuffer(output_offset); + + // Biquad filter parameters + const auto [n0, n1, n2] = params.numerator; + const auto [d0, d1] = params.denominator; + + // Biquad filter states + auto [s0, s1] = state; + + constexpr s64 int32_min = std::numeric_limits<s32>::min(); + constexpr s64 int32_max = std::numeric_limits<s32>::max(); + + for (int i = 0; i < sample_count; ++i) { + const auto sample = static_cast<s64>(input[i]); + const auto f = (sample * n0 + s0 + 0x4000) >> 15; + const auto y = std::clamp(f, int32_min, int32_max); + s0 = sample * n1 + y * d0 + s1; + s1 = sample * n2 + y * d1; + output[i] = static_cast<s32>(y); + } + + state = {s0, s1}; +} + +void CommandGenerator::GenerateDepopPrepareCommand(VoiceState& dsp_state, + std::size_t mix_buffer_count, + std::size_t mix_buffer_offset) { + for (std::size_t i = 0; i < mix_buffer_count; i++) { + auto& sample = dsp_state.previous_samples[i]; + if (sample != 0) { + depop_buffer[mix_buffer_offset + i] += sample; + sample = 0; + } + } +} + +void CommandGenerator::GenerateDepopForMixBuffersCommand(std::size_t mix_buffer_count, + std::size_t mix_buffer_offset, + s32 sample_rate) { + const std::size_t end_offset = + std::min(mix_buffer_offset + mix_buffer_count, GetTotalMixBufferCount()); + const s32 delta = sample_rate == 48000 ? 0x7B29 : 0x78CB; + for (std::size_t i = mix_buffer_offset; i < end_offset; i++) { + if (depop_buffer[i] == 0) { + continue; + } + + depop_buffer[i] = + ApplyMixDepop(GetMixBuffer(i), depop_buffer[i], delta, worker_params.sample_count); + } +} + +void CommandGenerator::GenerateEffectCommand(ServerMixInfo& mix_info) { + const std::size_t effect_count = effect_context.GetCount(); + const auto buffer_offset = mix_info.GetInParams().buffer_offset; + for (std::size_t i = 0; i < effect_count; i++) { + const auto index = mix_info.GetEffectOrder(i); + if (index == AudioCommon::NO_EFFECT_ORDER) { + break; + } + auto* info = effect_context.GetInfo(index); + const auto type = info->GetType(); + + // TODO(ogniK): Finish remaining effects + switch (type) { + case EffectType::Aux: + GenerateAuxCommand(buffer_offset, info, info->IsEnabled()); + break; + case EffectType::I3dl2Reverb: + GenerateI3dl2ReverbEffectCommand(buffer_offset, info, info->IsEnabled()); + break; + case EffectType::BiquadFilter: + GenerateBiquadFilterEffectCommand(buffer_offset, info, info->IsEnabled()); + break; + default: + break; + } + + info->UpdateForCommandGeneration(); + } +} + +void CommandGenerator::GenerateI3dl2ReverbEffectCommand(s32 mix_buffer_offset, EffectBase* info, + bool enabled) { + if (!enabled) { + return; + } + const auto& params = dynamic_cast<EffectI3dl2Reverb*>(info)->GetParams(); + const auto channel_count = params.channel_count; + for (s32 i = 0; i < channel_count; i++) { + // TODO(ogniK): Actually implement reverb + if (params.input[i] != params.output[i]) { + const auto* input = GetMixBuffer(mix_buffer_offset + params.input[i]); + auto* output = GetMixBuffer(mix_buffer_offset + params.output[i]); + ApplyMix<1>(output, input, 32768, worker_params.sample_count); + } + } +} + +void CommandGenerator::GenerateBiquadFilterEffectCommand(s32 mix_buffer_offset, EffectBase* info, + bool enabled) { + if (!enabled) { + return; + } + const auto& params = dynamic_cast<EffectBiquadFilter*>(info)->GetParams(); + const auto channel_count = params.channel_count; + for (s32 i = 0; i < channel_count; i++) { + // TODO(ogniK): Actually implement biquad filter + if (params.input[i] != params.output[i]) { + const auto* input = GetMixBuffer(mix_buffer_offset + params.input[i]); + auto* output = GetMixBuffer(mix_buffer_offset + params.output[i]); + ApplyMix<1>(output, input, 32768, worker_params.sample_count); + } + } +} + +void CommandGenerator::GenerateAuxCommand(s32 mix_buffer_offset, EffectBase* info, bool enabled) { + auto* aux = dynamic_cast<EffectAuxInfo*>(info); + const auto& params = aux->GetParams(); + if (aux->GetSendBuffer() != 0 && aux->GetRecvBuffer() != 0) { + const auto max_channels = params.count; + u32 offset{}; + for (u32 channel = 0; channel < max_channels; channel++) { + u32 write_count = 0; + if (channel == (max_channels - 1)) { + write_count = offset + worker_params.sample_count; + } + + const auto input_index = params.input_mix_buffers[channel] + mix_buffer_offset; + const auto output_index = params.output_mix_buffers[channel] + mix_buffer_offset; + + if (enabled) { + AuxInfoDSP send_info{}; + AuxInfoDSP recv_info{}; + memory.ReadBlock(aux->GetSendInfo(), &send_info, sizeof(AuxInfoDSP)); + memory.ReadBlock(aux->GetRecvInfo(), &recv_info, sizeof(AuxInfoDSP)); + + WriteAuxBuffer(send_info, aux->GetSendBuffer(), params.sample_count, + GetMixBuffer(input_index), worker_params.sample_count, offset, + write_count); + memory.WriteBlock(aux->GetSendInfo(), &send_info, sizeof(AuxInfoDSP)); + + const auto samples_read = ReadAuxBuffer( + recv_info, aux->GetRecvBuffer(), params.sample_count, + GetMixBuffer(output_index), worker_params.sample_count, offset, write_count); + memory.WriteBlock(aux->GetRecvInfo(), &recv_info, sizeof(AuxInfoDSP)); + + if (samples_read != worker_params.sample_count && + samples_read <= params.sample_count) { + std::memset(GetMixBuffer(output_index), 0, params.sample_count - samples_read); + } + } else { + AuxInfoDSP empty{}; + memory.WriteBlock(aux->GetSendInfo(), &empty, sizeof(AuxInfoDSP)); + memory.WriteBlock(aux->GetRecvInfo(), &empty, sizeof(AuxInfoDSP)); + if (output_index != input_index) { + std::memcpy(GetMixBuffer(output_index), GetMixBuffer(input_index), + worker_params.sample_count * sizeof(s32)); + } + } + + offset += worker_params.sample_count; + } + } +} + +ServerSplitterDestinationData* CommandGenerator::GetDestinationData(s32 splitter_id, s32 index) { + if (splitter_id == AudioCommon::NO_SPLITTER) { + return nullptr; + } + return splitter_context.GetDestinationData(splitter_id, index); +} + +s32 CommandGenerator::WriteAuxBuffer(AuxInfoDSP& dsp_info, VAddr send_buffer, u32 max_samples, + const s32* data, u32 sample_count, u32 write_offset, + u32 write_count) { + if (max_samples == 0) { + return 0; + } + u32 offset = dsp_info.write_offset + write_offset; + if (send_buffer == 0 || offset > max_samples) { + return 0; + } + + std::size_t data_offset{}; + u32 remaining = sample_count; + while (remaining > 0) { + // Get position in buffer + const auto base = send_buffer + (offset * sizeof(u32)); + const auto samples_to_grab = std::min(max_samples - offset, remaining); + // Write to output + memory.WriteBlock(base, (data + data_offset), samples_to_grab * sizeof(u32)); + offset = (offset + samples_to_grab) % max_samples; + remaining -= samples_to_grab; + data_offset += samples_to_grab; + } + + if (write_count != 0) { + dsp_info.write_offset = (dsp_info.write_offset + write_count) % max_samples; + } + return sample_count; +} + +s32 CommandGenerator::ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u32 max_samples, + s32* out_data, u32 sample_count, u32 read_offset, + u32 read_count) { + if (max_samples == 0) { + return 0; + } + + u32 offset = recv_info.read_offset + read_offset; + if (recv_buffer == 0 || offset > max_samples) { + return 0; + } + + u32 remaining = sample_count; + while (remaining > 0) { + const auto base = recv_buffer + (offset * sizeof(u32)); + const auto samples_to_grab = std::min(max_samples - offset, remaining); + std::vector<s32> buffer(samples_to_grab); + memory.ReadBlock(base, buffer.data(), buffer.size() * sizeof(u32)); + std::memcpy(out_data, buffer.data(), buffer.size() * sizeof(u32)); + out_data += samples_to_grab; + offset = (offset + samples_to_grab) % max_samples; + remaining -= samples_to_grab; + } + + if (read_count != 0) { + recv_info.read_offset = (recv_info.read_offset + read_count) % max_samples; + } + return sample_count; +} + +void CommandGenerator::GenerateVolumeRampCommand(float last_volume, float current_volume, + s32 channel, s32 node_id) { + const auto last = static_cast<s32>(last_volume * 32768.0f); + const auto current = static_cast<s32>(current_volume * 32768.0f); + const auto delta = static_cast<s32>((static_cast<float>(current) - static_cast<float>(last)) / + static_cast<float>(worker_params.sample_count)); + + if (dumping_frame) { + LOG_DEBUG(Audio, + "(DSP_TRACE) GenerateVolumeRampCommand node_id={}, input={}, output={}, " + "last_volume={}, current_volume={}", + node_id, GetMixChannelBufferOffset(channel), GetMixChannelBufferOffset(channel), + last_volume, current_volume); + } + // Apply generic gain on samples + ApplyGain(GetChannelMixBuffer(channel), GetChannelMixBuffer(channel), last, delta, + worker_params.sample_count); +} + +void CommandGenerator::GenerateVoiceMixCommand(const MixVolumeBuffer& mix_volumes, + const MixVolumeBuffer& last_mix_volumes, + VoiceState& dsp_state, s32 mix_buffer_offset, + s32 mix_buffer_count, s32 voice_index, s32 node_id) { + // Loop all our mix buffers + for (s32 i = 0; i < mix_buffer_count; i++) { + if (last_mix_volumes[i] != 0.0f || mix_volumes[i] != 0.0f) { + const auto delta = static_cast<float>((mix_volumes[i] - last_mix_volumes[i])) / + static_cast<float>(worker_params.sample_count); + + if (dumping_frame) { + LOG_DEBUG(Audio, + "(DSP_TRACE) GenerateVoiceMixCommand node_id={}, input={}, " + "output={}, last_volume={}, current_volume={}", + node_id, voice_index, mix_buffer_offset + i, last_mix_volumes[i], + mix_volumes[i]); + } + + dsp_state.previous_samples[i] = + ApplyMixRamp(GetMixBuffer(mix_buffer_offset + i), GetMixBuffer(voice_index), + last_mix_volumes[i], delta, worker_params.sample_count); + } else { + dsp_state.previous_samples[i] = 0; + } + } +} + +void CommandGenerator::GenerateSubMixCommand(ServerMixInfo& mix_info) { + if (dumping_frame) { + LOG_DEBUG(Audio, "(DSP_TRACE) GenerateSubMixCommand"); + } + const auto& in_params = mix_info.GetInParams(); + GenerateDepopForMixBuffersCommand(in_params.buffer_count, in_params.buffer_offset, + in_params.sample_rate); + + GenerateEffectCommand(mix_info); + + GenerateMixCommands(mix_info); +} + +void CommandGenerator::GenerateMixCommands(ServerMixInfo& mix_info) { + if (!mix_info.HasAnyConnection()) { + return; + } + const auto& in_params = mix_info.GetInParams(); + if (in_params.dest_mix_id != AudioCommon::NO_MIX) { + const auto& dest_mix = mix_context.GetInfo(in_params.dest_mix_id); + const auto& dest_in_params = dest_mix.GetInParams(); + + const auto buffer_count = in_params.buffer_count; + + for (s32 i = 0; i < buffer_count; i++) { + for (s32 j = 0; j < dest_in_params.buffer_count; j++) { + const auto mixed_volume = in_params.volume * in_params.mix_volume[i][j]; + if (mixed_volume != 0.0f) { + GenerateMixCommand(dest_in_params.buffer_offset + j, + in_params.buffer_offset + i, mixed_volume, + in_params.node_id); + } + } + } + } else if (in_params.splitter_id != AudioCommon::NO_SPLITTER) { + s32 base{}; + while (const auto* destination_data = GetDestinationData(in_params.splitter_id, base++)) { + if (!destination_data->IsConfigured()) { + continue; + } + + const auto& dest_mix = mix_context.GetInfo(destination_data->GetMixId()); + const auto& dest_in_params = dest_mix.GetInParams(); + const auto mix_index = (base - 1) % in_params.buffer_count + in_params.buffer_offset; + for (std::size_t i = 0; i < dest_in_params.buffer_count; i++) { + const auto mixed_volume = in_params.volume * destination_data->GetMixVolume(i); + if (mixed_volume != 0.0f) { + GenerateMixCommand(dest_in_params.buffer_offset + i, mix_index, mixed_volume, + in_params.node_id); + } + } + } + } +} + +void CommandGenerator::GenerateMixCommand(std::size_t output_offset, std::size_t input_offset, + float volume, s32 node_id) { + + if (dumping_frame) { + LOG_DEBUG(Audio, + "(DSP_TRACE) GenerateMixCommand node_id={}, input={}, output={}, volume={}", + node_id, input_offset, output_offset, volume); + } + + auto* output = GetMixBuffer(output_offset); + const auto* input = GetMixBuffer(input_offset); + + const s32 gain = static_cast<s32>(volume * 32768.0f); + // Mix with loop unrolling + if (worker_params.sample_count % 4 == 0) { + ApplyMix<4>(output, input, gain, worker_params.sample_count); + } else if (worker_params.sample_count % 2 == 0) { + ApplyMix<2>(output, input, gain, worker_params.sample_count); + } else { + ApplyMix<1>(output, input, gain, worker_params.sample_count); + } +} + +void CommandGenerator::GenerateFinalMixCommand() { + if (dumping_frame) { + LOG_DEBUG(Audio, "(DSP_TRACE) GenerateFinalMixCommand"); + } + auto& mix_info = mix_context.GetFinalMixInfo(); + const auto& in_params = mix_info.GetInParams(); + + GenerateDepopForMixBuffersCommand(in_params.buffer_count, in_params.buffer_offset, + in_params.sample_rate); + + GenerateEffectCommand(mix_info); + + for (s32 i = 0; i < in_params.buffer_count; i++) { + const s32 gain = static_cast<s32>(in_params.volume * 32768.0f); + if (dumping_frame) { + LOG_DEBUG( + Audio, + "(DSP_TRACE) ApplyGainWithoutDelta node_id={}, input={}, output={}, volume={}", + in_params.node_id, in_params.buffer_offset + i, in_params.buffer_offset + i, + in_params.volume); + } + ApplyGainWithoutDelta(GetMixBuffer(in_params.buffer_offset + i), + GetMixBuffer(in_params.buffer_offset + i), gain, + worker_params.sample_count); + } +} + +s32 CommandGenerator::DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_state, + s32 sample_count, s32 channel, std::size_t mix_offset) { + const auto& in_params = voice_info.GetInParams(); + const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; + if (wave_buffer.buffer_address == 0) { + return 0; + } + if (wave_buffer.buffer_size == 0) { + return 0; + } + if (wave_buffer.end_sample_offset < wave_buffer.start_sample_offset) { + return 0; + } + const auto samples_remaining = + (wave_buffer.end_sample_offset - wave_buffer.start_sample_offset) - dsp_state.offset; + const auto start_offset = + ((wave_buffer.start_sample_offset + dsp_state.offset) * in_params.channel_count) * + sizeof(s16); + const auto buffer_pos = wave_buffer.buffer_address + start_offset; + const auto samples_processed = std::min(sample_count, samples_remaining); + + if (in_params.channel_count == 1) { + std::vector<s16> buffer(samples_processed); + memory.ReadBlock(buffer_pos, buffer.data(), buffer.size() * sizeof(s16)); + for (std::size_t i = 0; i < buffer.size(); i++) { + sample_buffer[mix_offset + i] = buffer[i]; + } + } else { + const auto channel_count = in_params.channel_count; + std::vector<s16> buffer(samples_processed * channel_count); + memory.ReadBlock(buffer_pos, buffer.data(), buffer.size() * sizeof(s16)); + + for (std::size_t i = 0; i < samples_processed; i++) { + sample_buffer[mix_offset + i] = buffer[i * channel_count + channel]; + } + } + + return samples_processed; +} + +s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_state, + s32 sample_count, s32 channel, std::size_t mix_offset) { + const auto& in_params = voice_info.GetInParams(); + const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; + if (wave_buffer.buffer_address == 0) { + return 0; + } + if (wave_buffer.buffer_size == 0) { + return 0; + } + if (wave_buffer.end_sample_offset < wave_buffer.start_sample_offset) { + return 0; + } + + constexpr std::array<int, 16> SIGNED_NIBBLES = { + {0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1}}; + + constexpr std::size_t FRAME_LEN = 8; + constexpr std::size_t NIBBLES_PER_SAMPLE = 16; + constexpr std::size_t SAMPLES_PER_FRAME = 14; + + auto frame_header = dsp_state.context.header; + s32 idx = (frame_header >> 4) & 0xf; + s32 scale = frame_header & 0xf; + s16 yn1 = dsp_state.context.yn1; + s16 yn2 = dsp_state.context.yn2; + + Codec::ADPCM_Coeff coeffs; + memory.ReadBlock(in_params.additional_params_address, coeffs.data(), + sizeof(Codec::ADPCM_Coeff)); + + s32 coef1 = coeffs[idx * 2]; + s32 coef2 = coeffs[idx * 2 + 1]; + + const auto samples_remaining = + (wave_buffer.end_sample_offset - wave_buffer.start_sample_offset) - dsp_state.offset; + const auto samples_processed = std::min(sample_count, samples_remaining); + const auto sample_pos = wave_buffer.start_sample_offset + dsp_state.offset; + + const auto samples_remaining_in_frame = sample_pos % SAMPLES_PER_FRAME; + auto position_in_frame = ((sample_pos / SAMPLES_PER_FRAME) * NIBBLES_PER_SAMPLE) + + samples_remaining_in_frame + (samples_remaining_in_frame != 0 ? 2 : 0); + + const auto decode_sample = [&](const int nibble) -> s16 { + const int xn = nibble * (1 << scale); + // We first transform everything into 11 bit fixed point, perform the second order + // digital filter, then transform back. + // 0x400 == 0.5 in 11 bit fixed point. + // Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2] + int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11; + // Clamp to output range. + val = std::clamp<s32>(val, -32768, 32767); + // Advance output feedback. + yn2 = yn1; + yn1 = static_cast<s16>(val); + return yn1; + }; + + std::size_t buffer_offset{}; + std::vector<u8> buffer( + std::max((samples_processed / FRAME_LEN) * SAMPLES_PER_FRAME, FRAME_LEN)); + memory.ReadBlock(wave_buffer.buffer_address + (position_in_frame / 2), buffer.data(), + buffer.size()); + std::size_t cur_mix_offset = mix_offset; + + auto remaining_samples = samples_processed; + while (remaining_samples > 0) { + if (position_in_frame % NIBBLES_PER_SAMPLE == 0) { + // Read header + frame_header = buffer[buffer_offset++]; + idx = (frame_header >> 4) & 0xf; + scale = frame_header & 0xf; + coef1 = coeffs[idx * 2]; + coef2 = coeffs[idx * 2 + 1]; + position_in_frame += 2; + + // Decode entire frame + if (remaining_samples >= SAMPLES_PER_FRAME) { + for (std::size_t i = 0; i < SAMPLES_PER_FRAME / 2; i++) { + + // Sample 1 + const s32 s0 = SIGNED_NIBBLES[buffer[buffer_offset] >> 4]; + const s32 s1 = SIGNED_NIBBLES[buffer[buffer_offset++] & 0xf]; + const s16 sample_1 = decode_sample(s0); + const s16 sample_2 = decode_sample(s1); + sample_buffer[cur_mix_offset++] = sample_1; + sample_buffer[cur_mix_offset++] = sample_2; + } + remaining_samples -= SAMPLES_PER_FRAME; + position_in_frame += SAMPLES_PER_FRAME; + continue; + } + } + // Decode mid frame + s32 current_nibble = buffer[buffer_offset]; + if (position_in_frame++ & 0x1) { + current_nibble &= 0xf; + buffer_offset++; + } else { + current_nibble >>= 4; + } + const s16 sample = decode_sample(SIGNED_NIBBLES[current_nibble]); + sample_buffer[cur_mix_offset++] = sample; + remaining_samples--; + } + + dsp_state.context.header = frame_header; + dsp_state.context.yn1 = yn1; + dsp_state.context.yn2 = yn2; + + return samples_processed; +} + +s32* CommandGenerator::GetMixBuffer(std::size_t index) { + return mix_buffer.data() + (index * worker_params.sample_count); +} + +const s32* CommandGenerator::GetMixBuffer(std::size_t index) const { + return mix_buffer.data() + (index * worker_params.sample_count); +} + +std::size_t CommandGenerator::GetMixChannelBufferOffset(s32 channel) const { + return worker_params.mix_buffer_count + channel; +} + +std::size_t CommandGenerator::GetTotalMixBufferCount() const { + return worker_params.mix_buffer_count + AudioCommon::MAX_CHANNEL_COUNT; +} + +s32* CommandGenerator::GetChannelMixBuffer(s32 channel) { + return GetMixBuffer(worker_params.mix_buffer_count + channel); +} + +const s32* CommandGenerator::GetChannelMixBuffer(s32 channel) const { + return GetMixBuffer(worker_params.mix_buffer_count + channel); +} + +void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* output, + VoiceState& dsp_state, s32 channel, + s32 target_sample_rate, s32 sample_count, + s32 node_id) { + const auto& in_params = voice_info.GetInParams(); + if (dumping_frame) { + LOG_DEBUG(Audio, + "(DSP_TRACE) DecodeFromWaveBuffers, node_id={}, channel={}, " + "format={}, sample_count={}, sample_rate={}, mix_id={}, splitter_id={}", + node_id, channel, in_params.sample_format, sample_count, in_params.sample_rate, + in_params.mix_id, in_params.splitter_info_id); + } + ASSERT_OR_EXECUTE(output != nullptr, { return; }); + + const auto resample_rate = static_cast<s32>( + static_cast<float>(in_params.sample_rate) / static_cast<float>(target_sample_rate) * + static_cast<float>(static_cast<s32>(in_params.pitch * 32768.0f))); + auto* output_base = output; + if (dsp_state.fraction + sample_count * resample_rate > + static_cast<s32>(SCALED_MIX_BUFFER_SIZE - 4ULL)) { + return; + } + + auto min_required_samples = + std::min(static_cast<s32>(SCALED_MIX_BUFFER_SIZE) - dsp_state.fraction, resample_rate); + if (min_required_samples >= sample_count) { + min_required_samples = sample_count; + } + + std::size_t temp_mix_offset{}; + bool is_buffer_completed{false}; + auto samples_remaining = sample_count; + while (samples_remaining > 0 && !is_buffer_completed) { + const auto samples_to_output = std::min(samples_remaining, min_required_samples); + const auto samples_to_read = (samples_to_output * resample_rate + dsp_state.fraction) >> 15; + + if (!in_params.behavior_flags.is_pitch_and_src_skipped) { + // Append sample histtory for resampler + for (std::size_t i = 0; i < AudioCommon::MAX_SAMPLE_HISTORY; i++) { + sample_buffer[temp_mix_offset + i] = dsp_state.sample_history[i]; + } + temp_mix_offset += 4; + } + + s32 samples_read{}; + while (samples_read < samples_to_read) { + const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; + // No more data can be read + if (!dsp_state.is_wave_buffer_valid[dsp_state.wave_buffer_index]) { + is_buffer_completed = true; + break; + } + + if (in_params.sample_format == SampleFormat::Adpcm && dsp_state.offset == 0 && + wave_buffer.context_address != 0 && wave_buffer.context_size != 0) { + // TODO(ogniK): ADPCM loop context + } + + s32 samples_decoded{0}; + switch (in_params.sample_format) { + case SampleFormat::Pcm16: + samples_decoded = DecodePcm16(voice_info, dsp_state, samples_to_read - samples_read, + channel, temp_mix_offset); + break; + case SampleFormat::Adpcm: + samples_decoded = DecodeAdpcm(voice_info, dsp_state, samples_to_read - samples_read, + channel, temp_mix_offset); + break; + default: + UNREACHABLE_MSG("Unimplemented sample format={}", in_params.sample_format); + } + + temp_mix_offset += samples_decoded; + samples_read += samples_decoded; + dsp_state.offset += samples_decoded; + dsp_state.played_sample_count += samples_decoded; + + if (dsp_state.offset >= + (wave_buffer.end_sample_offset - wave_buffer.start_sample_offset) || + samples_decoded == 0) { + // Reset our sample offset + dsp_state.offset = 0; + if (wave_buffer.is_looping) { + if (samples_decoded == 0) { + // End of our buffer + is_buffer_completed = true; + break; + } + + if (in_params.behavior_flags.is_played_samples_reset_at_loop_point.Value()) { + dsp_state.played_sample_count = 0; + } + } else { + + // Update our wave buffer states + dsp_state.is_wave_buffer_valid[dsp_state.wave_buffer_index] = false; + dsp_state.wave_buffer_consumed++; + dsp_state.wave_buffer_index = + (dsp_state.wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; + if (wave_buffer.end_of_stream) { + dsp_state.played_sample_count = 0; + } + } + } + } + + if (in_params.behavior_flags.is_pitch_and_src_skipped.Value()) { + // No need to resample + std::memcpy(output, sample_buffer.data(), samples_read * sizeof(s32)); + } else { + std::fill(sample_buffer.begin() + temp_mix_offset, + sample_buffer.begin() + temp_mix_offset + (samples_to_read - samples_read), + 0); + AudioCore::Resample(output, sample_buffer.data(), resample_rate, dsp_state.fraction, + samples_to_output); + // Resample + for (std::size_t i = 0; i < AudioCommon::MAX_SAMPLE_HISTORY; i++) { + dsp_state.sample_history[i] = sample_buffer[samples_to_read + i]; + } + } + output += samples_to_output; + samples_remaining -= samples_to_output; + } +} + +} // namespace AudioCore diff --git a/src/audio_core/command_generator.h b/src/audio_core/command_generator.h new file mode 100644 index 000000000..967d24078 --- /dev/null +++ b/src/audio_core/command_generator.h @@ -0,0 +1,103 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <array> +#include "audio_core/common.h" +#include "audio_core/voice_context.h" +#include "common/common_funcs.h" +#include "common/common_types.h" + +namespace Core::Memory { +class Memory; +} + +namespace AudioCore { +class MixContext; +class SplitterContext; +class ServerSplitterDestinationData; +class ServerMixInfo; +class EffectContext; +class EffectBase; +struct AuxInfoDSP; +using MixVolumeBuffer = std::array<float, AudioCommon::MAX_MIX_BUFFERS>; + +class CommandGenerator { +public: + explicit CommandGenerator(AudioCommon::AudioRendererParameter& worker_params, + VoiceContext& voice_context, MixContext& mix_context, + SplitterContext& splitter_context, EffectContext& effect_context, + Core::Memory::Memory& memory); + ~CommandGenerator(); + + void ClearMixBuffers(); + void GenerateVoiceCommands(); + void GenerateVoiceCommand(ServerVoiceInfo& voice_info); + void GenerateSubMixCommands(); + void GenerateFinalMixCommands(); + void PreCommand(); + void PostCommand(); + + s32* GetChannelMixBuffer(s32 channel); + const s32* GetChannelMixBuffer(s32 channel) const; + s32* GetMixBuffer(std::size_t index); + const s32* GetMixBuffer(std::size_t index) const; + std::size_t GetMixChannelBufferOffset(s32 channel) const; + + std::size_t GetTotalMixBufferCount() const; + +private: + void GenerateDataSourceCommand(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 channel); + void GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voice_info, VoiceState& dsp_state, + s32 mix_buffer_count, s32 channel); + void GenerateVolumeRampCommand(float last_volume, float current_volume, s32 channel, + s32 node_id); + void GenerateVoiceMixCommand(const MixVolumeBuffer& mix_volumes, + const MixVolumeBuffer& last_mix_volumes, VoiceState& dsp_state, + s32 mix_buffer_offset, s32 mix_buffer_count, s32 voice_index, + s32 node_id); + void GenerateSubMixCommand(ServerMixInfo& mix_info); + void GenerateMixCommands(ServerMixInfo& mix_info); + void GenerateMixCommand(std::size_t output_offset, std::size_t input_offset, float volume, + s32 node_id); + void GenerateFinalMixCommand(); + void GenerateBiquadFilterCommand(s32 mix_buffer, const BiquadFilterParameter& params, + std::array<s64, 2>& state, std::size_t input_offset, + std::size_t output_offset, s32 sample_count, s32 node_id); + void GenerateDepopPrepareCommand(VoiceState& dsp_state, std::size_t mix_buffer_count, + std::size_t mix_buffer_offset); + void GenerateDepopForMixBuffersCommand(std::size_t mix_buffer_count, + std::size_t mix_buffer_offset, s32 sample_rate); + void GenerateEffectCommand(ServerMixInfo& mix_info); + void GenerateI3dl2ReverbEffectCommand(s32 mix_buffer_offset, EffectBase* info, bool enabled); + void GenerateBiquadFilterEffectCommand(s32 mix_buffer_offset, EffectBase* info, bool enabled); + void GenerateAuxCommand(s32 mix_buffer_offset, EffectBase* info, bool enabled); + ServerSplitterDestinationData* GetDestinationData(s32 splitter_id, s32 index); + + s32 WriteAuxBuffer(AuxInfoDSP& dsp_info, VAddr send_buffer, u32 max_samples, const s32* data, + u32 sample_count, u32 write_offset, u32 write_count); + s32 ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u32 max_samples, s32* out_data, + u32 sample_count, u32 read_offset, u32 read_count); + + // DSP Code + s32 DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 sample_count, + s32 channel, std::size_t mix_offset); + s32 DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 sample_count, + s32 channel, std::size_t mix_offset); + void DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* output, VoiceState& dsp_state, + s32 channel, s32 target_sample_rate, s32 sample_count, s32 node_id); + + AudioCommon::AudioRendererParameter& worker_params; + VoiceContext& voice_context; + MixContext& mix_context; + SplitterContext& splitter_context; + EffectContext& effect_context; + Core::Memory::Memory& memory; + std::vector<s32> mix_buffer{}; + std::vector<s32> sample_buffer{}; + std::vector<s32> depop_buffer{}; + bool dumping_frame{false}; +}; +} // namespace AudioCore diff --git a/src/audio_core/common.h b/src/audio_core/common.h index 7bb145c53..72ebce221 100644 --- a/src/audio_core/common.h +++ b/src/audio_core/common.h @@ -8,13 +8,30 @@ #include "common/swap.h" #include "core/hle/result.h" -namespace AudioCore { +namespace AudioCommon { namespace Audren { constexpr ResultCode ERR_INVALID_PARAMETERS{ErrorModule::Audio, 41}; -} +constexpr ResultCode ERR_SPLITTER_SORT_FAILED{ErrorModule::Audio, 43}; +} // namespace Audren constexpr u32_le CURRENT_PROCESS_REVISION = Common::MakeMagic('R', 'E', 'V', '8'); constexpr std::size_t MAX_MIX_BUFFERS = 24; +constexpr std::size_t MAX_BIQUAD_FILTERS = 2; +constexpr std::size_t MAX_CHANNEL_COUNT = 6; +constexpr std::size_t MAX_WAVE_BUFFERS = 4; +constexpr std::size_t MAX_SAMPLE_HISTORY = 4; +constexpr u32 STREAM_SAMPLE_RATE = 48000; +constexpr u32 STREAM_NUM_CHANNELS = 6; +constexpr s32 NO_SPLITTER = -1; +constexpr s32 NO_MIX = 0x7fffffff; +constexpr s32 NO_FINAL_MIX = std::numeric_limits<s32>::min(); +constexpr s32 FINAL_MIX = 0; +constexpr s32 NO_EFFECT_ORDER = -1; +constexpr std::size_t TEMP_MIX_BASE_SIZE = 0x3f00; // TODO(ogniK): Work out this constant +// Any size checks seem to take the sample history into account +// and our const ends up being 0x3f04, the 4 bytes are most +// likely the sample history +constexpr std::size_t TOTAL_TEMP_MIX_SIZE = TEMP_MIX_BASE_SIZE + AudioCommon::MAX_SAMPLE_HISTORY; static constexpr u32 VersionFromRevision(u32_le rev) { // "REV7" -> 7 @@ -45,4 +62,46 @@ static constexpr bool CanConsumeBuffer(std::size_t size, std::size_t offset, std return true; } -} // namespace AudioCore +struct UpdateDataSizes { + u32_le behavior{}; + u32_le memory_pool{}; + u32_le voice{}; + u32_le voice_channel_resource{}; + u32_le effect{}; + u32_le mixer{}; + u32_le sink{}; + u32_le performance{}; + u32_le splitter{}; + u32_le render_info{}; + INSERT_PADDING_WORDS(4); +}; +static_assert(sizeof(UpdateDataSizes) == 0x38, "UpdateDataSizes is an invalid size"); + +struct UpdateDataHeader { + u32_le revision{}; + UpdateDataSizes size{}; + u32_le total_size{}; +}; +static_assert(sizeof(UpdateDataHeader) == 0x40, "UpdateDataHeader is an invalid size"); + +struct AudioRendererParameter { + u32_le sample_rate; + u32_le sample_count; + u32_le mix_buffer_count; + u32_le submix_count; + u32_le voice_count; + u32_le sink_count; + u32_le effect_count; + u32_le performance_frame_count; + u8 is_voice_drop_enabled; + u8 unknown_21; + u8 unknown_22; + u8 execution_mode; + u32_le splitter_count; + u32_le num_splitter_send_channels; + u32_le unknown_30; + u32_le revision; +}; +static_assert(sizeof(AudioRendererParameter) == 52, "AudioRendererParameter is an invalid size"); + +} // namespace AudioCommon diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp index c27df946c..83c06c0ed 100644 --- a/src/audio_core/cubeb_sink.cpp +++ b/src/audio_core/cubeb_sink.cpp @@ -23,14 +23,24 @@ class CubebSinkStream final : public SinkStream { public: CubebSinkStream(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, const std::string& name) - : ctx{ctx}, num_channels{std::min(num_channels_, 2u)}, time_stretch{sample_rate, + : ctx{ctx}, num_channels{std::min(num_channels_, 6u)}, time_stretch{sample_rate, num_channels} { cubeb_stream_params params{}; params.rate = sample_rate; params.channels = num_channels; params.format = CUBEB_SAMPLE_S16NE; - params.layout = num_channels == 1 ? CUBEB_LAYOUT_MONO : CUBEB_LAYOUT_STEREO; + switch (num_channels) { + case 1: + params.layout = CUBEB_LAYOUT_MONO; + break; + case 2: + params.layout = CUBEB_LAYOUT_STEREO; + break; + case 6: + params.layout = CUBEB_LAYOUT_3F2_LFE; + break; + } u32 minimum_latency{}; if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) { @@ -193,6 +203,7 @@ long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const const std::size_t samples_to_write = num_channels * num_frames; std::size_t samples_written; + /* if (Settings::values.enable_audio_stretching.GetValue()) { const std::vector<s16> in{impl->queue.Pop()}; const std::size_t num_in{in.size() / num_channels}; @@ -207,7 +218,8 @@ long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const } } else { samples_written = impl->queue.Pop(buffer, samples_to_write); - } + }*/ + samples_written = impl->queue.Pop(buffer, samples_to_write); if (samples_written >= num_channels) { std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16), diff --git a/src/audio_core/effect_context.cpp b/src/audio_core/effect_context.cpp new file mode 100644 index 000000000..adfec3df5 --- /dev/null +++ b/src/audio_core/effect_context.cpp @@ -0,0 +1,299 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <algorithm> +#include "audio_core/effect_context.h" + +namespace AudioCore { +namespace { +bool ValidChannelCountForEffect(s32 channel_count) { + return channel_count == 1 || channel_count == 2 || channel_count == 4 || channel_count == 6; +} +} // namespace + +EffectContext::EffectContext(std::size_t effect_count) : effect_count(effect_count) { + effects.reserve(effect_count); + std::generate_n(std::back_inserter(effects), effect_count, + [] { return std::make_unique<EffectStubbed>(); }); +} +EffectContext::~EffectContext() = default; + +std::size_t EffectContext::GetCount() const { + return effect_count; +} + +EffectBase* EffectContext::GetInfo(std::size_t i) { + return effects.at(i).get(); +} + +EffectBase* EffectContext::RetargetEffect(std::size_t i, EffectType effect) { + switch (effect) { + case EffectType::Invalid: + effects[i] = std::make_unique<EffectStubbed>(); + break; + case EffectType::BufferMixer: + effects[i] = std::make_unique<EffectBufferMixer>(); + break; + case EffectType::Aux: + effects[i] = std::make_unique<EffectAuxInfo>(); + break; + case EffectType::Delay: + effects[i] = std::make_unique<EffectDelay>(); + break; + case EffectType::Reverb: + effects[i] = std::make_unique<EffectReverb>(); + break; + case EffectType::I3dl2Reverb: + effects[i] = std::make_unique<EffectI3dl2Reverb>(); + break; + case EffectType::BiquadFilter: + effects[i] = std::make_unique<EffectBiquadFilter>(); + break; + default: + UNREACHABLE_MSG("Unimplemented effect {}", effect); + effects[i] = std::make_unique<EffectStubbed>(); + } + return GetInfo(i); +} + +const EffectBase* EffectContext::GetInfo(std::size_t i) const { + return effects.at(i).get(); +} + +EffectStubbed::EffectStubbed() : EffectBase::EffectBase(EffectType::Invalid) {} +EffectStubbed::~EffectStubbed() = default; + +void EffectStubbed::Update(EffectInfo::InParams& in_params) {} +void EffectStubbed::UpdateForCommandGeneration() {} + +EffectBase::EffectBase(EffectType effect_type) : effect_type(effect_type) {} +EffectBase::~EffectBase() = default; + +UsageState EffectBase::GetUsage() const { + return usage; +} + +EffectType EffectBase::GetType() const { + return effect_type; +} + +bool EffectBase::IsEnabled() const { + return enabled; +} + +s32 EffectBase::GetMixID() const { + return mix_id; +} + +s32 EffectBase::GetProcessingOrder() const { + return processing_order; +} + +EffectI3dl2Reverb::EffectI3dl2Reverb() : EffectGeneric::EffectGeneric(EffectType::I3dl2Reverb) {} +EffectI3dl2Reverb::~EffectI3dl2Reverb() = default; + +void EffectI3dl2Reverb::Update(EffectInfo::InParams& in_params) { + auto& internal_params = GetParams(); + const auto* reverb_params = reinterpret_cast<I3dl2ReverbParams*>(in_params.raw.data()); + if (!ValidChannelCountForEffect(reverb_params->max_channels)) { + UNREACHABLE_MSG("Invalid reverb max channel count {}", reverb_params->max_channels); + return; + } + + const auto last_status = internal_params.status; + mix_id = in_params.mix_id; + processing_order = in_params.processing_order; + internal_params = *reverb_params; + if (!ValidChannelCountForEffect(reverb_params->channel_count)) { + internal_params.channel_count = internal_params.max_channels; + } + enabled = in_params.is_enabled; + if (last_status != ParameterStatus::Updated) { + internal_params.status = last_status; + } + + if (in_params.is_new || skipped) { + usage = UsageState::Initialized; + internal_params.status = ParameterStatus::Initialized; + skipped = in_params.buffer_address == 0 || in_params.buffer_size == 0; + } +} + +void EffectI3dl2Reverb::UpdateForCommandGeneration() { + if (enabled) { + usage = UsageState::Running; + } else { + usage = UsageState::Stopped; + } + GetParams().status = ParameterStatus::Updated; +} + +EffectBiquadFilter::EffectBiquadFilter() : EffectGeneric::EffectGeneric(EffectType::BiquadFilter) {} +EffectBiquadFilter::~EffectBiquadFilter() = default; + +void EffectBiquadFilter::Update(EffectInfo::InParams& in_params) { + auto& internal_params = GetParams(); + const auto* biquad_params = reinterpret_cast<BiquadFilterParams*>(in_params.raw.data()); + mix_id = in_params.mix_id; + processing_order = in_params.processing_order; + internal_params = *biquad_params; + enabled = in_params.is_enabled; +} + +void EffectBiquadFilter::UpdateForCommandGeneration() { + if (enabled) { + usage = UsageState::Running; + } else { + usage = UsageState::Stopped; + } + GetParams().status = ParameterStatus::Updated; +} + +EffectAuxInfo::EffectAuxInfo() : EffectGeneric::EffectGeneric(EffectType::Aux) {} +EffectAuxInfo::~EffectAuxInfo() = default; + +void EffectAuxInfo::Update(EffectInfo::InParams& in_params) { + const auto* aux_params = reinterpret_cast<AuxInfo*>(in_params.raw.data()); + mix_id = in_params.mix_id; + processing_order = in_params.processing_order; + GetParams() = *aux_params; + enabled = in_params.is_enabled; + + if (in_params.is_new || skipped) { + skipped = aux_params->send_buffer_info == 0 || aux_params->return_buffer_info == 0; + if (skipped) { + return; + } + + // There's two AuxInfos which are an identical size, the first one is managed by the cpu, + // the second is managed by the dsp. All we care about is managing the DSP one + send_info = aux_params->send_buffer_info + sizeof(AuxInfoDSP); + send_buffer = aux_params->send_buffer_info + (sizeof(AuxInfoDSP) * 2); + + recv_info = aux_params->return_buffer_info + sizeof(AuxInfoDSP); + recv_buffer = aux_params->return_buffer_info + (sizeof(AuxInfoDSP) * 2); + } +} + +void EffectAuxInfo::UpdateForCommandGeneration() { + if (enabled) { + usage = UsageState::Running; + } else { + usage = UsageState::Stopped; + } +} + +const VAddr EffectAuxInfo::GetSendInfo() const { + return send_info; +} + +const VAddr EffectAuxInfo::GetSendBuffer() const { + return send_buffer; +} + +const VAddr EffectAuxInfo::GetRecvInfo() const { + return recv_info; +} + +const VAddr EffectAuxInfo::GetRecvBuffer() const { + return recv_buffer; +} + +EffectDelay::EffectDelay() : EffectGeneric::EffectGeneric(EffectType::Delay) {} +EffectDelay::~EffectDelay() = default; + +void EffectDelay::Update(EffectInfo::InParams& in_params) { + const auto* delay_params = reinterpret_cast<DelayParams*>(in_params.raw.data()); + auto& internal_params = GetParams(); + if (!ValidChannelCountForEffect(delay_params->max_channels)) { + return; + } + + const auto last_status = internal_params.status; + mix_id = in_params.mix_id; + processing_order = in_params.processing_order; + internal_params = *delay_params; + if (!ValidChannelCountForEffect(delay_params->channels)) { + internal_params.channels = internal_params.max_channels; + } + enabled = in_params.is_enabled; + + if (last_status != ParameterStatus::Updated) { + internal_params.status = last_status; + } + + if (in_params.is_new || skipped) { + usage = UsageState::Initialized; + internal_params.status = ParameterStatus::Initialized; + skipped = in_params.buffer_address == 0 || in_params.buffer_size == 0; + } +} + +void EffectDelay::UpdateForCommandGeneration() { + if (enabled) { + usage = UsageState::Running; + } else { + usage = UsageState::Stopped; + } + GetParams().status = ParameterStatus::Updated; +} + +EffectBufferMixer::EffectBufferMixer() : EffectGeneric::EffectGeneric(EffectType::BufferMixer) {} +EffectBufferMixer::~EffectBufferMixer() = default; + +void EffectBufferMixer::Update(EffectInfo::InParams& in_params) { + mix_id = in_params.mix_id; + processing_order = in_params.processing_order; + GetParams() = *reinterpret_cast<BufferMixerParams*>(in_params.raw.data()); + enabled = in_params.is_enabled; +} + +void EffectBufferMixer::UpdateForCommandGeneration() { + if (enabled) { + usage = UsageState::Running; + } else { + usage = UsageState::Stopped; + } +} + +EffectReverb::EffectReverb() : EffectGeneric::EffectGeneric(EffectType::Reverb) {} +EffectReverb::~EffectReverb() = default; + +void EffectReverb::Update(EffectInfo::InParams& in_params) { + const auto* reverb_params = reinterpret_cast<ReverbParams*>(in_params.raw.data()); + auto& internal_params = GetParams(); + if (!ValidChannelCountForEffect(reverb_params->max_channels)) { + return; + } + + const auto last_status = internal_params.status; + mix_id = in_params.mix_id; + processing_order = in_params.processing_order; + internal_params = *reverb_params; + if (!ValidChannelCountForEffect(reverb_params->channels)) { + internal_params.channels = internal_params.max_channels; + } + enabled = in_params.is_enabled; + + if (last_status != ParameterStatus::Updated) { + internal_params.status = last_status; + } + + if (in_params.is_new || skipped) { + usage = UsageState::Initialized; + internal_params.status = ParameterStatus::Initialized; + skipped = in_params.buffer_address == 0 || in_params.buffer_size == 0; + } +} + +void EffectReverb::UpdateForCommandGeneration() { + if (enabled) { + usage = UsageState::Running; + } else { + usage = UsageState::Stopped; + } + GetParams().status = ParameterStatus::Updated; +} + +} // namespace AudioCore diff --git a/src/audio_core/effect_context.h b/src/audio_core/effect_context.h new file mode 100644 index 000000000..2f2da72dd --- /dev/null +++ b/src/audio_core/effect_context.h @@ -0,0 +1,322 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <array> +#include <memory> +#include <vector> +#include "audio_core/common.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/swap.h" + +namespace AudioCore { +enum class EffectType : u8 { + Invalid = 0, + BufferMixer = 1, + Aux = 2, + Delay = 3, + Reverb = 4, + I3dl2Reverb = 5, + BiquadFilter = 6, +}; + +enum class UsageStatus : u8 { + Invalid = 0, + New = 1, + Initialized = 2, + Used = 3, + Removed = 4, +}; + +enum class UsageState { + Invalid = 0, + Initialized = 1, + Running = 2, + Stopped = 3, +}; + +enum class ParameterStatus : u8 { + Initialized = 0, + Updating = 1, + Updated = 2, +}; + +struct BufferMixerParams { + std::array<s8, AudioCommon::MAX_MIX_BUFFERS> input{}; + std::array<s8, AudioCommon::MAX_MIX_BUFFERS> output{}; + std::array<float_le, AudioCommon::MAX_MIX_BUFFERS> volume{}; + s32_le count{}; +}; +static_assert(sizeof(BufferMixerParams) == 0x94, "BufferMixerParams is an invalid size"); + +struct AuxInfoDSP { + u32_le read_offset{}; + u32_le write_offset{}; + u32_le remaining{}; + INSERT_PADDING_WORDS(13); +}; +static_assert(sizeof(AuxInfoDSP) == 0x40, "AuxInfoDSP is an invalid size"); + +struct AuxInfo { + std::array<s8, AudioCommon::MAX_MIX_BUFFERS> input_mix_buffers{}; + std::array<s8, AudioCommon::MAX_MIX_BUFFERS> output_mix_buffers{}; + u32_le count{}; + s32_le sample_rate{}; + s32_le sample_count{}; + s32_le mix_buffer_count{}; + u64_le send_buffer_info{}; + u64_le send_buffer_base{}; + + u64_le return_buffer_info{}; + u64_le return_buffer_base{}; +}; +static_assert(sizeof(AuxInfo) == 0x60, "AuxInfo is an invalid size"); + +struct I3dl2ReverbParams { + std::array<s8, AudioCommon::MAX_CHANNEL_COUNT> input{}; + std::array<s8, AudioCommon::MAX_CHANNEL_COUNT> output{}; + u16_le max_channels{}; + u16_le channel_count{}; + INSERT_PADDING_BYTES(1); + u32_le sample_rate{}; + f32 room_hf{}; + f32 hf_reference{}; + f32 decay_time{}; + f32 hf_decay_ratio{}; + f32 room{}; + f32 reflection{}; + f32 reverb{}; + f32 diffusion{}; + f32 reflection_delay{}; + f32 reverb_delay{}; + f32 density{}; + f32 dry_gain{}; + ParameterStatus status{}; + INSERT_PADDING_BYTES(3); +}; +static_assert(sizeof(I3dl2ReverbParams) == 0x4c, "I3dl2ReverbParams is an invalid size"); + +struct BiquadFilterParams { + std::array<s8, AudioCommon::MAX_CHANNEL_COUNT> input{}; + std::array<s8, AudioCommon::MAX_CHANNEL_COUNT> output{}; + std::array<s16_le, 3> numerator; + std::array<s16_le, 2> denominator; + s8 channel_count{}; + ParameterStatus status{}; +}; +static_assert(sizeof(BiquadFilterParams) == 0x18, "BiquadFilterParams is an invalid size"); + +struct DelayParams { + std::array<s8, AudioCommon::MAX_CHANNEL_COUNT> input{}; + std::array<s8, AudioCommon::MAX_CHANNEL_COUNT> output{}; + u16_le max_channels{}; + u16_le channels{}; + s32_le max_delay{}; + s32_le delay{}; + s32_le sample_rate{}; + s32_le gain{}; + s32_le feedback_gain{}; + s32_le out_gain{}; + s32_le dry_gain{}; + s32_le channel_spread{}; + s32_le low_pass{}; + ParameterStatus status{}; + INSERT_PADDING_BYTES(3); +}; +static_assert(sizeof(DelayParams) == 0x38, "DelayParams is an invalid size"); + +struct ReverbParams { + std::array<s8, AudioCommon::MAX_CHANNEL_COUNT> input{}; + std::array<s8, AudioCommon::MAX_CHANNEL_COUNT> output{}; + u16_le max_channels{}; + u16_le channels{}; + s32_le sample_rate{}; + s32_le mode0{}; + s32_le mode0_gain{}; + s32_le pre_delay{}; + s32_le mode1{}; + s32_le mode1_gain{}; + s32_le decay{}; + s32_le hf_decay_ratio{}; + s32_le coloration{}; + s32_le reverb_gain{}; + s32_le out_gain{}; + s32_le dry_gain{}; + ParameterStatus status{}; + INSERT_PADDING_BYTES(3); +}; +static_assert(sizeof(ReverbParams) == 0x44, "ReverbParams is an invalid size"); + +class EffectInfo { +public: + struct InParams { + EffectType type{}; + u8 is_new{}; + u8 is_enabled{}; + INSERT_PADDING_BYTES(1); + s32_le mix_id{}; + u64_le buffer_address{}; + u64_le buffer_size{}; + s32_le processing_order{}; + INSERT_PADDING_BYTES(4); + union { + std::array<u8, 0xa0> raw; + }; + }; + static_assert(sizeof(EffectInfo::InParams) == 0xc0, "InParams is an invalid size"); + + struct OutParams { + UsageStatus status{}; + INSERT_PADDING_BYTES(15); + }; + static_assert(sizeof(EffectInfo::OutParams) == 0x10, "OutParams is an invalid size"); +}; + +struct AuxAddress { + VAddr send_dsp_info{}; + VAddr send_buffer_base{}; + VAddr return_dsp_info{}; + VAddr return_buffer_base{}; +}; + +class EffectBase { +public: + EffectBase(EffectType effect_type); + ~EffectBase(); + + virtual void Update(EffectInfo::InParams& in_params) = 0; + virtual void UpdateForCommandGeneration() = 0; + UsageState GetUsage() const; + EffectType GetType() const; + bool IsEnabled() const; + s32 GetMixID() const; + s32 GetProcessingOrder() const; + +protected: + UsageState usage{UsageState::Invalid}; + EffectType effect_type{}; + s32 mix_id{}; + s32 processing_order{}; + bool enabled = false; +}; + +template <typename T> +class EffectGeneric : public EffectBase { +public: + EffectGeneric(EffectType effect_type) : EffectBase::EffectBase(effect_type) {} + ~EffectGeneric() = default; + + T& GetParams() { + return internal_params; + } + + const I3dl2ReverbParams& GetParams() const { + return internal_params; + } + +private: + T internal_params{}; +}; + +class EffectStubbed : public EffectBase { +public: + explicit EffectStubbed(); + ~EffectStubbed(); + + void Update(EffectInfo::InParams& in_params) override; + void UpdateForCommandGeneration() override; +}; + +class EffectI3dl2Reverb : public EffectGeneric<I3dl2ReverbParams> { +public: + explicit EffectI3dl2Reverb(); + ~EffectI3dl2Reverb(); + + void Update(EffectInfo::InParams& in_params) override; + void UpdateForCommandGeneration() override; + +private: + bool skipped = false; +}; + +class EffectBiquadFilter : public EffectGeneric<BiquadFilterParams> { +public: + explicit EffectBiquadFilter(); + ~EffectBiquadFilter(); + + void Update(EffectInfo::InParams& in_params) override; + void UpdateForCommandGeneration() override; +}; + +class EffectAuxInfo : public EffectGeneric<AuxInfo> { +public: + explicit EffectAuxInfo(); + ~EffectAuxInfo(); + + void Update(EffectInfo::InParams& in_params) override; + void UpdateForCommandGeneration() override; + const VAddr GetSendInfo() const; + const VAddr GetSendBuffer() const; + const VAddr GetRecvInfo() const; + const VAddr GetRecvBuffer() const; + +private: + VAddr send_info{}; + VAddr send_buffer{}; + VAddr recv_info{}; + VAddr recv_buffer{}; + bool skipped = false; + AuxAddress addresses{}; +}; + +class EffectDelay : public EffectGeneric<DelayParams> { +public: + explicit EffectDelay(); + ~EffectDelay(); + + void Update(EffectInfo::InParams& in_params) override; + void UpdateForCommandGeneration() override; + +private: + bool skipped = false; +}; + +class EffectBufferMixer : public EffectGeneric<BufferMixerParams> { +public: + explicit EffectBufferMixer(); + ~EffectBufferMixer(); + + void Update(EffectInfo::InParams& in_params) override; + void UpdateForCommandGeneration() override; +}; + +class EffectReverb : public EffectGeneric<ReverbParams> { +public: + explicit EffectReverb(); + ~EffectReverb(); + + void Update(EffectInfo::InParams& in_params) override; + void UpdateForCommandGeneration() override; + +private: + bool skipped = false; +}; + +class EffectContext { +public: + explicit EffectContext(std::size_t effect_count); + ~EffectContext(); + + std::size_t GetCount() const; + EffectBase* GetInfo(std::size_t i); + EffectBase* RetargetEffect(std::size_t i, EffectType effect); + const EffectBase* GetInfo(std::size_t i) const; + +private: + std::size_t effect_count{}; + std::vector<std::unique_ptr<EffectBase>> effects; +}; +} // namespace AudioCore diff --git a/src/audio_core/info_updater.cpp b/src/audio_core/info_updater.cpp new file mode 100644 index 000000000..f53ce21a5 --- /dev/null +++ b/src/audio_core/info_updater.cpp @@ -0,0 +1,517 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "audio_core/behavior_info.h" +#include "audio_core/effect_context.h" +#include "audio_core/info_updater.h" +#include "audio_core/memory_pool.h" +#include "audio_core/mix_context.h" +#include "audio_core/sink_context.h" +#include "audio_core/splitter_context.h" +#include "audio_core/voice_context.h" +#include "common/logging/log.h" + +namespace AudioCore { + +InfoUpdater::InfoUpdater(const std::vector<u8>& in_params, std::vector<u8>& out_params, + BehaviorInfo& behavior_info) + : in_params(in_params), out_params(out_params), behavior_info(behavior_info) { + ASSERT( + AudioCommon::CanConsumeBuffer(in_params.size(), 0, sizeof(AudioCommon::UpdateDataHeader))); + std::memcpy(&input_header, in_params.data(), sizeof(AudioCommon::UpdateDataHeader)); + output_header.total_size = sizeof(AudioCommon::UpdateDataHeader); +} + +InfoUpdater::~InfoUpdater() = default; + +bool InfoUpdater::UpdateBehaviorInfo(BehaviorInfo& in_behavior_info) { + if (input_header.size.behavior != sizeof(BehaviorInfo::InParams)) { + LOG_ERROR(Audio, "Behavior info is an invalid size, expecting 0x{:X} but got 0x{:X}", + sizeof(BehaviorInfo::InParams), input_header.size.behavior); + return false; + } + + if (!AudioCommon::CanConsumeBuffer(in_params.size(), input_offset, + sizeof(BehaviorInfo::InParams))) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + BehaviorInfo::InParams behavior_in{}; + std::memcpy(&behavior_in, in_params.data() + input_offset, sizeof(BehaviorInfo::InParams)); + input_offset += sizeof(BehaviorInfo::InParams); + + // Make sure it's an audio revision we can actually support + if (!AudioCommon::IsValidRevision(behavior_in.revision)) { + LOG_ERROR(Audio, "Invalid input revision, revision=0x{:08X}", behavior_in.revision); + return false; + } + + // Make sure that our behavior info revision matches the input + if (in_behavior_info.GetUserRevision() != behavior_in.revision) { + LOG_ERROR(Audio, + "User revision differs from input revision, expecting 0x{:08X} but got 0x{:08X}", + in_behavior_info.GetUserRevision(), behavior_in.revision); + return false; + } + + // Update behavior info flags + in_behavior_info.ClearError(); + in_behavior_info.UpdateFlags(behavior_in.flags); + + return true; +} + +bool InfoUpdater::UpdateMemoryPools(std::vector<ServerMemoryPoolInfo>& memory_pool_info) { + const auto force_mapping = behavior_info.IsMemoryPoolForceMappingEnabled(); + const auto memory_pool_count = memory_pool_info.size(); + const auto total_memory_pool_in = sizeof(ServerMemoryPoolInfo::InParams) * memory_pool_count; + const auto total_memory_pool_out = sizeof(ServerMemoryPoolInfo::OutParams) * memory_pool_count; + + if (input_header.size.memory_pool != total_memory_pool_in) { + LOG_ERROR(Audio, "Memory pools are an invalid size, expecting 0x{:X} but got 0x{:X}", + total_memory_pool_in, input_header.size.memory_pool); + return false; + } + + if (!AudioCommon::CanConsumeBuffer(in_params.size(), input_offset, total_memory_pool_in)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + std::vector<ServerMemoryPoolInfo::InParams> mempool_in(memory_pool_count); + std::vector<ServerMemoryPoolInfo::OutParams> mempool_out(memory_pool_count); + + std::memcpy(mempool_in.data(), in_params.data() + input_offset, total_memory_pool_in); + input_offset += total_memory_pool_in; + + // Update our memory pools + for (std::size_t i = 0; i < memory_pool_count; i++) { + if (!memory_pool_info[i].Update(mempool_in[i], mempool_out[i])) { + LOG_ERROR(Audio, "Failed to update memory pool {}!", i); + return false; + } + } + + if (!AudioCommon::CanConsumeBuffer(out_params.size(), output_offset, + sizeof(BehaviorInfo::InParams))) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + std::memcpy(out_params.data() + output_offset, mempool_out.data(), total_memory_pool_out); + output_offset += total_memory_pool_out; + output_header.size.memory_pool = static_cast<u32>(total_memory_pool_out); + return true; +} + +bool InfoUpdater::UpdateVoiceChannelResources(VoiceContext& voice_context) { + const auto voice_count = voice_context.GetVoiceCount(); + const auto voice_size = voice_count * sizeof(VoiceChannelResource::InParams); + std::vector<VoiceChannelResource::InParams> resources_in(voice_count); + + if (input_header.size.voice_channel_resource != voice_size) { + LOG_ERROR(Audio, "VoiceChannelResource is an invalid size, expecting 0x{:X} but got 0x{:X}", + voice_size, input_header.size.voice_channel_resource); + return false; + } + + if (!AudioCommon::CanConsumeBuffer(in_params.size(), input_offset, voice_size)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + std::memcpy(resources_in.data(), in_params.data() + input_offset, voice_size); + input_offset += voice_size; + + // Update our channel resources + for (std::size_t i = 0; i < voice_count; i++) { + // Grab our channel resource + auto& resource = voice_context.GetChannelResource(i); + resource.Update(resources_in[i]); + } + + return true; +} + +bool InfoUpdater::UpdateVoices(VoiceContext& voice_context, + std::vector<ServerMemoryPoolInfo>& memory_pool_info, + VAddr audio_codec_dsp_addr) { + const auto voice_count = voice_context.GetVoiceCount(); + std::vector<VoiceInfo::InParams> voice_in(voice_count); + std::vector<VoiceInfo::OutParams> voice_out(voice_count); + + const auto voice_in_size = voice_count * sizeof(VoiceInfo::InParams); + const auto voice_out_size = voice_count * sizeof(VoiceInfo::OutParams); + + if (input_header.size.voice != voice_in_size) { + LOG_ERROR(Audio, "Voices are an invalid size, expecting 0x{:X} but got 0x{:X}", + voice_in_size, input_header.size.voice); + return false; + } + + if (!AudioCommon::CanConsumeBuffer(in_params.size(), input_offset, voice_in_size)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + std::memcpy(voice_in.data(), in_params.data() + input_offset, voice_in_size); + input_offset += voice_in_size; + + // Set all voices to not be in use + for (std::size_t i = 0; i < voice_count; i++) { + voice_context.GetInfo(i).GetInParams().in_use = false; + } + + // Update our voices + for (std::size_t i = 0; i < voice_count; i++) { + auto& in_params = voice_in[i]; + const auto channel_count = static_cast<std::size_t>(in_params.channel_count); + // Skip if it's not currently in use + if (!in_params.is_in_use) { + continue; + } + // Voice states for each channel + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT> voice_states{}; + ASSERT(in_params.id < voice_count); + + // Grab our current voice info + auto& voice_info = voice_context.GetInfo(static_cast<std::size_t>(in_params.id)); + + ASSERT(channel_count <= AudioCommon::MAX_CHANNEL_COUNT); + + // Get all our channel voice states + for (std::size_t channel = 0; channel < channel_count; channel++) { + voice_states[channel] = + &voice_context.GetState(in_params.voice_channel_resource_ids[channel]); + } + + if (in_params.is_new) { + // Default our values for our voice + voice_info.Initialize(); + if (channel_count == 0 || channel_count > AudioCommon::MAX_CHANNEL_COUNT) { + continue; + } + + // Zero out our voice states + for (std::size_t channel = 0; channel < channel_count; channel++) { + std::memset(voice_states[channel], 0, sizeof(VoiceState)); + } + } + + // Update our voice + voice_info.UpdateParameters(in_params, behavior_info); + // TODO(ogniK): Handle mapping errors with behavior info based on in params response + + // Update our wave buffers + voice_info.UpdateWaveBuffers(in_params, voice_states, behavior_info); + voice_info.WriteOutStatus(voice_out[i], in_params, voice_states); + } + + if (!AudioCommon::CanConsumeBuffer(out_params.size(), output_offset, voice_out_size)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + std::memcpy(out_params.data() + output_offset, voice_out.data(), voice_out_size); + output_offset += voice_out_size; + output_header.size.voice = static_cast<u32>(voice_out_size); + return true; +} + +bool InfoUpdater::UpdateEffects(EffectContext& effect_context, bool is_active) { + const auto effect_count = effect_context.GetCount(); + std::vector<EffectInfo::InParams> effect_in(effect_count); + std::vector<EffectInfo::OutParams> effect_out(effect_count); + + const auto total_effect_in = effect_count * sizeof(EffectInfo::InParams); + const auto total_effect_out = effect_count * sizeof(EffectInfo::OutParams); + + if (input_header.size.effect != total_effect_in) { + LOG_ERROR(Audio, "Effects are an invalid size, expecting 0x{:X} but got 0x{:X}", + total_effect_in, input_header.size.effect); + return false; + } + + if (!AudioCommon::CanConsumeBuffer(in_params.size(), input_offset, total_effect_in)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + std::memcpy(effect_in.data(), in_params.data() + input_offset, total_effect_in); + input_offset += total_effect_in; + + // Update effects + for (std::size_t i = 0; i < effect_count; i++) { + auto* info = effect_context.GetInfo(i); + if (effect_in[i].type != info->GetType()) { + info = effect_context.RetargetEffect(i, effect_in[i].type); + } + + info->Update(effect_in[i]); + + if ((!is_active && info->GetUsage() != UsageState::Initialized) || + info->GetUsage() == UsageState::Stopped) { + effect_out[i].status = UsageStatus::Removed; + } else { + effect_out[i].status = UsageStatus::Used; + } + } + + if (!AudioCommon::CanConsumeBuffer(out_params.size(), output_offset, total_effect_out)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + std::memcpy(out_params.data() + output_offset, effect_out.data(), total_effect_out); + output_offset += total_effect_out; + output_header.size.effect = static_cast<u32>(total_effect_out); + + return true; +} + +bool InfoUpdater::UpdateSplitterInfo(SplitterContext& splitter_context) { + std::size_t start_offset = input_offset; + std::size_t bytes_read{}; + // Update splitter context + if (!splitter_context.Update(in_params, input_offset, bytes_read)) { + LOG_ERROR(Audio, "Failed to update splitter context!"); + return false; + } + + const auto consumed = input_offset - start_offset; + + if (input_header.size.splitter != consumed) { + LOG_ERROR(Audio, "Splitters is an invalid size, expecting 0x{:X} but got 0x{:X}", + bytes_read, input_header.size.splitter); + return false; + } + + return true; +} + +ResultCode InfoUpdater::UpdateMixes(MixContext& mix_context, std::size_t mix_buffer_count, + SplitterContext& splitter_context, + EffectContext& effect_context) { + std::vector<MixInfo::InParams> mix_in_params; + + if (!behavior_info.IsMixInParameterDirtyOnlyUpdateSupported()) { + // If we're not dirty, get ALL mix in parameters + const auto context_mix_count = mix_context.GetCount(); + const auto total_mix_in = context_mix_count * sizeof(MixInfo::InParams); + if (input_header.size.mixer != total_mix_in) { + LOG_ERROR(Audio, "Mixer is an invalid size, expecting 0x{:X} but got 0x{:X}", + total_mix_in, input_header.size.mixer); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; + } + + if (!AudioCommon::CanConsumeBuffer(in_params.size(), input_offset, total_mix_in)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; + } + + mix_in_params.resize(context_mix_count); + std::memcpy(mix_in_params.data(), in_params.data() + input_offset, total_mix_in); + + input_offset += total_mix_in; + } else { + // Only update the "dirty" mixes + MixInfo::DirtyHeader dirty_header{}; + if (!AudioCommon::CanConsumeBuffer(in_params.size(), input_offset, + sizeof(MixInfo::DirtyHeader))) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; + } + + std::memcpy(&dirty_header, in_params.data() + input_offset, sizeof(MixInfo::DirtyHeader)); + input_offset += sizeof(MixInfo::DirtyHeader); + + const auto total_mix_in = + dirty_header.mixer_count * sizeof(MixInfo::InParams) + sizeof(MixInfo::DirtyHeader); + + if (input_header.size.mixer != total_mix_in) { + LOG_ERROR(Audio, "Mixer is an invalid size, expecting 0x{:X} but got 0x{:X}", + total_mix_in, input_header.size.mixer); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; + } + + if (dirty_header.mixer_count != 0) { + mix_in_params.resize(dirty_header.mixer_count); + std::memcpy(mix_in_params.data(), in_params.data() + input_offset, + mix_in_params.size() * sizeof(MixInfo::InParams)); + input_offset += mix_in_params.size() * sizeof(MixInfo::InParams); + } + } + + // Get our total input count + const auto mix_count = mix_in_params.size(); + + if (!behavior_info.IsMixInParameterDirtyOnlyUpdateSupported()) { + // Only verify our buffer count if we're not dirty + std::size_t total_buffer_count{}; + for (std::size_t i = 0; i < mix_count; i++) { + const auto& in = mix_in_params[i]; + total_buffer_count += in.buffer_count; + if (in.dest_mix_id > mix_count && in.dest_mix_id != AudioCommon::NO_MIX && + in.mix_id != AudioCommon::FINAL_MIX) { + LOG_ERROR( + Audio, + "Invalid mix destination, mix_id={:X}, dest_mix_id={:X}, mix_buffer_count={:X}", + in.mix_id, in.dest_mix_id, mix_buffer_count); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; + } + } + + if (total_buffer_count > mix_buffer_count) { + LOG_ERROR(Audio, + "Too many mix buffers used! mix_buffer_count={:X}, requesting_buffers={:X}", + mix_buffer_count, total_buffer_count); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; + } + } + + if (mix_buffer_count == 0) { + LOG_ERROR(Audio, "No mix buffers!"); + return AudioCommon::Audren::ERR_INVALID_PARAMETERS; + } + + bool should_sort = false; + for (std::size_t i = 0; i < mix_count; i++) { + const auto& mix_in = mix_in_params[i]; + std::size_t target_mix{}; + if (behavior_info.IsMixInParameterDirtyOnlyUpdateSupported()) { + target_mix = mix_in.mix_id; + } else { + // Non dirty supported games just use i instead of the actual mix_id + target_mix = i; + } + auto& mix_info = mix_context.GetInfo(target_mix); + auto& mix_info_params = mix_info.GetInParams(); + if (mix_info_params.in_use != mix_in.in_use) { + mix_info_params.in_use = mix_in.in_use; + mix_info.ResetEffectProcessingOrder(); + should_sort = true; + } + + if (mix_in.in_use) { + should_sort |= mix_info.Update(mix_context.GetEdgeMatrix(), mix_in, behavior_info, + splitter_context, effect_context); + } + } + + if (should_sort && behavior_info.IsSplitterSupported()) { + // Sort our splitter data + if (!mix_context.TsortInfo(splitter_context)) { + return AudioCommon::Audren::ERR_SPLITTER_SORT_FAILED; + } + } + + // TODO(ogniK): Sort when splitter is suppoorted + + return RESULT_SUCCESS; +} + +bool InfoUpdater::UpdateSinks(SinkContext& sink_context) { + const auto sink_count = sink_context.GetCount(); + std::vector<SinkInfo::InParams> sink_in_params(sink_count); + const auto total_sink_in = sink_count * sizeof(SinkInfo::InParams); + + if (input_header.size.sink != total_sink_in) { + LOG_ERROR(Audio, "Sinks are an invalid size, expecting 0x{:X} but got 0x{:X}", + total_sink_in, input_header.size.effect); + return false; + } + + if (!AudioCommon::CanConsumeBuffer(in_params.size(), input_offset, total_sink_in)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + std::memcpy(sink_in_params.data(), in_params.data() + input_offset, total_sink_in); + input_offset += total_sink_in; + + // TODO(ogniK): Properly update sinks + if (!sink_in_params.empty()) { + sink_context.UpdateMainSink(sink_in_params[0]); + } + + output_header.size.sink = static_cast<u32>(0x20 * sink_count); + output_offset += 0x20 * sink_count; + return true; +} + +bool InfoUpdater::UpdatePerformanceBuffer() { + output_header.size.performance = 0x10; + output_offset += 0x10; + return true; +} + +bool InfoUpdater::UpdateErrorInfo(BehaviorInfo& in_behavior_info) { + const auto total_beahvior_info_out = sizeof(BehaviorInfo::OutParams); + + if (!AudioCommon::CanConsumeBuffer(out_params.size(), output_offset, total_beahvior_info_out)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + + BehaviorInfo::OutParams behavior_info_out{}; + behavior_info.CopyErrorInfo(behavior_info_out); + + std::memcpy(out_params.data() + output_offset, &behavior_info_out, total_beahvior_info_out); + output_offset += total_beahvior_info_out; + output_header.size.behavior = total_beahvior_info_out; + + return true; +} + +struct RendererInfo { + u64_le elasped_frame_count{}; + INSERT_PADDING_WORDS(2); +}; +static_assert(sizeof(RendererInfo) == 0x10, "RendererInfo is an invalid size"); + +bool InfoUpdater::UpdateRendererInfo(std::size_t elapsed_frame_count) { + const auto total_renderer_info_out = sizeof(RendererInfo); + if (!AudioCommon::CanConsumeBuffer(out_params.size(), output_offset, total_renderer_info_out)) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + RendererInfo out{}; + out.elasped_frame_count = elapsed_frame_count; + std::memcpy(out_params.data() + output_offset, &out, total_renderer_info_out); + output_offset += total_renderer_info_out; + output_header.size.render_info = total_renderer_info_out; + + return true; +} + +bool InfoUpdater::CheckConsumedSize() const { + if (output_offset != out_params.size()) { + LOG_ERROR(Audio, "Output is not consumed! Consumed {}, but requires {}. {} bytes remaining", + output_offset, out_params.size(), out_params.size() - output_offset); + return false; + } + /*if (input_offset != in_params.size()) { + LOG_ERROR(Audio, "Input is not consumed!"); + return false; + }*/ + return true; +} + +bool InfoUpdater::WriteOutputHeader() { + if (!AudioCommon::CanConsumeBuffer(out_params.size(), 0, + sizeof(AudioCommon::UpdateDataHeader))) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + output_header.revision = AudioCommon::CURRENT_PROCESS_REVISION; + const auto& sz = output_header.size; + output_header.total_size += sz.behavior + sz.memory_pool + sz.voice + + sz.voice_channel_resource + sz.effect + sz.mixer + sz.sink + + sz.performance + sz.splitter + sz.render_info; + + std::memcpy(out_params.data(), &output_header, sizeof(AudioCommon::UpdateDataHeader)); + return true; +} + +} // namespace AudioCore diff --git a/src/audio_core/info_updater.h b/src/audio_core/info_updater.h new file mode 100644 index 000000000..06f9d770f --- /dev/null +++ b/src/audio_core/info_updater.h @@ -0,0 +1,58 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <vector> +#include "audio_core/common.h" +#include "common/common_types.h" + +namespace AudioCore { + +class BehaviorInfo; +class ServerMemoryPoolInfo; +class VoiceContext; +class EffectContext; +class MixContext; +class SinkContext; +class SplitterContext; + +class InfoUpdater { +public: + // TODO(ogniK): Pass process handle when we support it + InfoUpdater(const std::vector<u8>& in_params, std::vector<u8>& out_params, + BehaviorInfo& behavior_info); + ~InfoUpdater(); + + bool UpdateBehaviorInfo(BehaviorInfo& in_behavior_info); + bool UpdateMemoryPools(std::vector<ServerMemoryPoolInfo>& memory_pool_info); + bool UpdateVoiceChannelResources(VoiceContext& voice_context); + bool UpdateVoices(VoiceContext& voice_context, + std::vector<ServerMemoryPoolInfo>& memory_pool_info, + VAddr audio_codec_dsp_addr); + bool UpdateEffects(EffectContext& effect_context, bool is_active); + bool UpdateSplitterInfo(SplitterContext& splitter_context); + ResultCode UpdateMixes(MixContext& mix_context, std::size_t mix_buffer_count, + SplitterContext& splitter_context, EffectContext& effect_context); + bool UpdateSinks(SinkContext& sink_context); + bool UpdatePerformanceBuffer(); + bool UpdateErrorInfo(BehaviorInfo& in_behavior_info); + bool UpdateRendererInfo(std::size_t elapsed_frame_count); + bool CheckConsumedSize() const; + + bool WriteOutputHeader(); + +private: + const std::vector<u8>& in_params; + std::vector<u8>& out_params; + BehaviorInfo& behavior_info; + + AudioCommon::UpdateDataHeader input_header{}; + AudioCommon::UpdateDataHeader output_header{}; + + std::size_t input_offset{sizeof(AudioCommon::UpdateDataHeader)}; + std::size_t output_offset{sizeof(AudioCommon::UpdateDataHeader)}; +}; + +} // namespace AudioCore diff --git a/src/audio_core/memory_pool.cpp b/src/audio_core/memory_pool.cpp new file mode 100644 index 000000000..5a3453063 --- /dev/null +++ b/src/audio_core/memory_pool.cpp @@ -0,0 +1,62 @@ + +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "audio_core/memory_pool.h" +#include "common/logging/log.h" + +namespace AudioCore { + +ServerMemoryPoolInfo::ServerMemoryPoolInfo() = default; +ServerMemoryPoolInfo::~ServerMemoryPoolInfo() = default; +bool ServerMemoryPoolInfo::Update(const ServerMemoryPoolInfo::InParams& in_params, + ServerMemoryPoolInfo::OutParams& out_params) { + // Our state does not need to be changed + if (in_params.state != ServerMemoryPoolInfo::State::RequestAttach && + in_params.state != ServerMemoryPoolInfo::State::RequestDetach) { + return true; + } + + // Address or size is null + if (in_params.address == 0 || in_params.size == 0) { + LOG_ERROR(Audio, "Memory pool address or size is zero! address={:X}, size={:X}", + in_params.address, in_params.size); + return false; + } + + // Address or size is not aligned + if ((in_params.address % 0x1000) != 0 || (in_params.size % 0x1000) != 0) { + LOG_ERROR(Audio, "Memory pool address or size is not aligned! address={:X}, size={:X}", + in_params.address, in_params.size); + return false; + } + + if (in_params.state == ServerMemoryPoolInfo::State::RequestAttach) { + cpu_address = in_params.address; + size = in_params.size; + used = true; + out_params.state = ServerMemoryPoolInfo::State::Attached; + } else { + // Unexpected address + if (cpu_address != in_params.address) { + LOG_ERROR(Audio, "Memory pool address differs! Expecting {:X} but address is {:X}", + cpu_address, in_params.address); + return false; + } + + if (size != in_params.size) { + LOG_ERROR(Audio, "Memory pool size differs! Expecting {:X} but size is {:X}", size, + in_params.size); + return false; + } + + cpu_address = 0; + size = 0; + used = false; + out_params.state = ServerMemoryPoolInfo::State::Detached; + } + return true; +} + +} // namespace AudioCore diff --git a/src/audio_core/memory_pool.h b/src/audio_core/memory_pool.h new file mode 100644 index 000000000..8ac503f1c --- /dev/null +++ b/src/audio_core/memory_pool.h @@ -0,0 +1,53 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/swap.h" + +namespace AudioCore { + +class ServerMemoryPoolInfo { +public: + ServerMemoryPoolInfo(); + ~ServerMemoryPoolInfo(); + + enum class State : u32_le { + Invalid = 0x0, + Aquired = 0x1, + RequestDetach = 0x2, + Detached = 0x3, + RequestAttach = 0x4, + Attached = 0x5, + Released = 0x6, + }; + + struct InParams { + u64_le address{}; + u64_le size{}; + ServerMemoryPoolInfo::State state{}; + INSERT_PADDING_WORDS(3); + }; + static_assert(sizeof(ServerMemoryPoolInfo::InParams) == 0x20, "InParams are an invalid size"); + + struct OutParams { + ServerMemoryPoolInfo::State state{}; + INSERT_PADDING_WORDS(3); + }; + static_assert(sizeof(ServerMemoryPoolInfo::OutParams) == 0x10, "OutParams are an invalid size"); + + bool Update(const ServerMemoryPoolInfo::InParams& in_params, + ServerMemoryPoolInfo::OutParams& out_params); + +private: + // There's another entry here which is the DSP address, however since we're not talking to the + // DSP we can just use the same address provided by the guest without needing to remap + u64_le cpu_address{}; + u64_le size{}; + bool used{}; +}; + +} // namespace AudioCore diff --git a/src/audio_core/mix_context.cpp b/src/audio_core/mix_context.cpp new file mode 100644 index 000000000..042891490 --- /dev/null +++ b/src/audio_core/mix_context.cpp @@ -0,0 +1,296 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "audio_core/behavior_info.h" +#include "audio_core/common.h" +#include "audio_core/effect_context.h" +#include "audio_core/mix_context.h" +#include "audio_core/splitter_context.h" + +namespace AudioCore { +MixContext::MixContext() = default; +MixContext::~MixContext() = default; + +void MixContext::Initialize(const BehaviorInfo& behavior_info, std::size_t mix_count, + std::size_t effect_count) { + info_count = mix_count; + infos.resize(info_count); + auto& final_mix = GetInfo(AudioCommon::FINAL_MIX); + final_mix.GetInParams().mix_id = AudioCommon::FINAL_MIX; + sorted_info.reserve(infos.size()); + for (auto& info : infos) { + sorted_info.push_back(&info); + } + + for (auto& info : infos) { + info.SetEffectCount(effect_count); + } + + // Only initialize our edge matrix and node states if splitters are supported + if (behavior_info.IsSplitterSupported()) { + node_states.Initialize(mix_count); + edge_matrix.Initialize(mix_count); + } +} + +void MixContext::UpdateDistancesFromFinalMix() { + // Set all distances to be invalid + for (std::size_t i = 0; i < info_count; i++) { + GetInfo(i).GetInParams().final_mix_distance = AudioCommon::NO_FINAL_MIX; + } + + for (std::size_t i = 0; i < info_count; i++) { + auto& info = GetInfo(i); + auto& in_params = info.GetInParams(); + // Populate our sorted info + sorted_info[i] = &info; + + if (!in_params.in_use) { + continue; + } + + auto mix_id = in_params.mix_id; + // Needs to be referenced out of scope + s32 distance_to_final_mix{AudioCommon::FINAL_MIX}; + for (; distance_to_final_mix < info_count; distance_to_final_mix++) { + if (mix_id == AudioCommon::FINAL_MIX) { + // If we're at the final mix, we're done + break; + } else if (mix_id == AudioCommon::NO_MIX) { + // If we have no more mix ids, we're done + distance_to_final_mix = AudioCommon::NO_FINAL_MIX; + break; + } else { + const auto& dest_mix = GetInfo(mix_id); + const auto dest_mix_distance = dest_mix.GetInParams().final_mix_distance; + + if (dest_mix_distance == AudioCommon::NO_FINAL_MIX) { + // If our current mix isn't pointing to a final mix, follow through + mix_id = dest_mix.GetInParams().dest_mix_id; + } else { + // Our current mix + 1 = final distance + distance_to_final_mix = dest_mix_distance + 1; + break; + } + } + } + + // If we're out of range for our distance, mark it as no final mix + if (distance_to_final_mix >= info_count) { + distance_to_final_mix = AudioCommon::NO_FINAL_MIX; + } + + in_params.final_mix_distance = distance_to_final_mix; + } +} + +void MixContext::CalcMixBufferOffset() { + s32 offset{}; + for (std::size_t i = 0; i < info_count; i++) { + auto& info = GetSortedInfo(i); + auto& in_params = info.GetInParams(); + if (in_params.in_use) { + // Only update if in use + in_params.buffer_offset = offset; + offset += in_params.buffer_count; + } + } +} + +void MixContext::SortInfo() { + // Get the distance to the final mix + UpdateDistancesFromFinalMix(); + + // Sort based on the distance to the final mix + std::sort(sorted_info.begin(), sorted_info.end(), + [](const ServerMixInfo* lhs, const ServerMixInfo* rhs) { + return lhs->GetInParams().final_mix_distance > + rhs->GetInParams().final_mix_distance; + }); + + // Calculate the mix buffer offset + CalcMixBufferOffset(); +} + +bool MixContext::TsortInfo(SplitterContext& splitter_context) { + // If we're not using mixes, just calculate the mix buffer offset + if (!splitter_context.UsingSplitter()) { + CalcMixBufferOffset(); + return true; + } + // Sort our node states + if (!node_states.Tsort(edge_matrix)) { + return false; + } + + // Get our sorted list + const auto sorted_list = node_states.GetIndexList(); + std::size_t info_id{}; + for (auto itr = sorted_list.rbegin(); itr != sorted_list.rend(); ++itr) { + // Set our sorted info + sorted_info[info_id++] = &GetInfo(*itr); + } + + // Calculate the mix buffer offset + CalcMixBufferOffset(); + return true; +} + +std::size_t MixContext::GetCount() const { + return info_count; +} + +ServerMixInfo& MixContext::GetInfo(std::size_t i) { + ASSERT(i < info_count); + return infos.at(i); +} + +const ServerMixInfo& MixContext::GetInfo(std::size_t i) const { + ASSERT(i < info_count); + return infos.at(i); +} + +ServerMixInfo& MixContext::GetSortedInfo(std::size_t i) { + ASSERT(i < info_count); + return *sorted_info.at(i); +} + +const ServerMixInfo& MixContext::GetSortedInfo(std::size_t i) const { + ASSERT(i < info_count); + return *sorted_info.at(i); +} + +ServerMixInfo& MixContext::GetFinalMixInfo() { + return infos.at(AudioCommon::FINAL_MIX); +} + +const ServerMixInfo& MixContext::GetFinalMixInfo() const { + return infos.at(AudioCommon::FINAL_MIX); +} + +EdgeMatrix& MixContext::GetEdgeMatrix() { + return edge_matrix; +} + +const EdgeMatrix& MixContext::GetEdgeMatrix() const { + return edge_matrix; +} + +ServerMixInfo::ServerMixInfo() { + Cleanup(); +} +ServerMixInfo::~ServerMixInfo() = default; + +const ServerMixInfo::InParams& ServerMixInfo::GetInParams() const { + return in_params; +} + +ServerMixInfo::InParams& ServerMixInfo::GetInParams() { + return in_params; +} + +bool ServerMixInfo::Update(EdgeMatrix& edge_matrix, const MixInfo::InParams& mix_in, + BehaviorInfo& behavior_info, SplitterContext& splitter_context, + EffectContext& effect_context) { + in_params.volume = mix_in.volume; + in_params.sample_rate = mix_in.sample_rate; + in_params.buffer_count = mix_in.buffer_count; + in_params.in_use = mix_in.in_use; + in_params.mix_id = mix_in.mix_id; + in_params.node_id = mix_in.node_id; + for (std::size_t i = 0; i < mix_in.mix_volume.size(); i++) { + std::copy(mix_in.mix_volume[i].begin(), mix_in.mix_volume[i].end(), + in_params.mix_volume[i].begin()); + } + + bool require_sort = false; + + if (behavior_info.IsSplitterSupported()) { + require_sort = UpdateConnection(edge_matrix, mix_in, splitter_context); + } else { + in_params.dest_mix_id = mix_in.dest_mix_id; + in_params.splitter_id = AudioCommon::NO_SPLITTER; + } + + ResetEffectProcessingOrder(); + const auto effect_count = effect_context.GetCount(); + for (std::size_t i = 0; i < effect_count; i++) { + auto* effect_info = effect_context.GetInfo(i); + if (effect_info->GetMixID() == in_params.mix_id) { + effect_processing_order[effect_info->GetProcessingOrder()] = static_cast<s32>(i); + } + } + + // TODO(ogniK): Update effect processing order + return require_sort; +} + +bool ServerMixInfo::HasAnyConnection() const { + return in_params.splitter_id != AudioCommon::NO_SPLITTER || + in_params.mix_id != AudioCommon::NO_MIX; +} + +void ServerMixInfo::Cleanup() { + in_params.volume = 0.0f; + in_params.sample_rate = 0; + in_params.buffer_count = 0; + in_params.in_use = false; + in_params.mix_id = AudioCommon::NO_MIX; + in_params.node_id = 0; + in_params.buffer_offset = 0; + in_params.dest_mix_id = AudioCommon::NO_MIX; + in_params.splitter_id = AudioCommon::NO_SPLITTER; + std::memset(in_params.mix_volume.data(), 0, sizeof(float) * in_params.mix_volume.size()); +} + +void ServerMixInfo::SetEffectCount(std::size_t count) { + effect_processing_order.resize(count); + ResetEffectProcessingOrder(); +} + +void ServerMixInfo::ResetEffectProcessingOrder() { + for (auto& order : effect_processing_order) { + order = AudioCommon::NO_EFFECT_ORDER; + } +} + +s32 ServerMixInfo::GetEffectOrder(std::size_t i) const { + return effect_processing_order.at(i); +} + +bool ServerMixInfo::UpdateConnection(EdgeMatrix& edge_matrix, const MixInfo::InParams& mix_in, + SplitterContext& splitter_context) { + // Mixes are identical + if (in_params.dest_mix_id == mix_in.dest_mix_id && + in_params.splitter_id == mix_in.splitter_id && + ((in_params.splitter_id == AudioCommon::NO_SPLITTER) || + !splitter_context.GetInfo(in_params.splitter_id).HasNewConnection())) { + return false; + } + // Remove current edges for mix id + edge_matrix.RemoveEdges(in_params.mix_id); + if (mix_in.dest_mix_id != AudioCommon::NO_MIX) { + // If we have a valid destination mix id, set our edge matrix + edge_matrix.Connect(in_params.mix_id, mix_in.dest_mix_id); + } else if (mix_in.splitter_id != AudioCommon::NO_SPLITTER) { + // Recurse our splitter linked and set our edges + auto& splitter_info = splitter_context.GetInfo(mix_in.splitter_id); + const auto length = splitter_info.GetLength(); + for (s32 i = 0; i < length; i++) { + const auto* splitter_destination = + splitter_context.GetDestinationData(mix_in.splitter_id, i); + if (splitter_destination == nullptr) { + continue; + } + if (splitter_destination->ValidMixId()) { + edge_matrix.Connect(in_params.mix_id, splitter_destination->GetMixId()); + } + } + } + in_params.dest_mix_id = mix_in.dest_mix_id; + in_params.splitter_id = mix_in.splitter_id; + return true; +} + +} // namespace AudioCore diff --git a/src/audio_core/mix_context.h b/src/audio_core/mix_context.h new file mode 100644 index 000000000..6a588eeb4 --- /dev/null +++ b/src/audio_core/mix_context.h @@ -0,0 +1,114 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <array> +#include <vector> +#include "audio_core/common.h" +#include "audio_core/splitter_context.h" +#include "common/common_funcs.h" +#include "common/common_types.h" + +namespace AudioCore { +class BehaviorInfo; +class EffectContext; + +class MixInfo { +public: + struct DirtyHeader { + u32_le magic{}; + u32_le mixer_count{}; + INSERT_PADDING_BYTES(0x18); + }; + static_assert(sizeof(DirtyHeader) == 0x20, "MixInfo::DirtyHeader is an invalid size"); + + struct InParams { + float_le volume{}; + s32_le sample_rate{}; + s32_le buffer_count{}; + bool in_use{}; + INSERT_PADDING_BYTES(3); + s32_le mix_id{}; + s32_le effect_count{}; + u32_le node_id{}; + INSERT_PADDING_WORDS(2); + std::array<std::array<float_le, AudioCommon::MAX_MIX_BUFFERS>, AudioCommon::MAX_MIX_BUFFERS> + mix_volume{}; + s32_le dest_mix_id{}; + s32_le splitter_id{}; + INSERT_PADDING_WORDS(1); + }; + static_assert(sizeof(MixInfo::InParams) == 0x930, "MixInfo::InParams is an invalid size"); +}; + +class ServerMixInfo { +public: + struct InParams { + float volume{}; + s32 sample_rate{}; + s32 buffer_count{}; + bool in_use{}; + s32 mix_id{}; + u32 node_id{}; + std::array<std::array<float_le, AudioCommon::MAX_MIX_BUFFERS>, AudioCommon::MAX_MIX_BUFFERS> + mix_volume{}; + s32 dest_mix_id{}; + s32 splitter_id{}; + s32 buffer_offset{}; + s32 final_mix_distance{}; + }; + ServerMixInfo(); + ~ServerMixInfo(); + + const ServerMixInfo::InParams& GetInParams() const; + ServerMixInfo::InParams& GetInParams(); + + bool Update(EdgeMatrix& edge_matrix, const MixInfo::InParams& mix_in, + BehaviorInfo& behavior_info, SplitterContext& splitter_context, + EffectContext& effect_context); + bool HasAnyConnection() const; + void Cleanup(); + void SetEffectCount(std::size_t count); + void ResetEffectProcessingOrder(); + s32 GetEffectOrder(std::size_t i) const; + +private: + std::vector<s32> effect_processing_order; + InParams in_params{}; + bool UpdateConnection(EdgeMatrix& edge_matrix, const MixInfo::InParams& mix_in, + SplitterContext& splitter_context); +}; + +class MixContext { +public: + MixContext(); + ~MixContext(); + + void Initialize(const BehaviorInfo& behavior_info, std::size_t mix_count, + std::size_t effect_count); + void SortInfo(); + bool TsortInfo(SplitterContext& splitter_context); + + std::size_t GetCount() const; + ServerMixInfo& GetInfo(std::size_t i); + const ServerMixInfo& GetInfo(std::size_t i) const; + ServerMixInfo& GetSortedInfo(std::size_t i); + const ServerMixInfo& GetSortedInfo(std::size_t i) const; + ServerMixInfo& GetFinalMixInfo(); + const ServerMixInfo& GetFinalMixInfo() const; + EdgeMatrix& GetEdgeMatrix(); + const EdgeMatrix& GetEdgeMatrix() const; + +private: + void CalcMixBufferOffset(); + void UpdateDistancesFromFinalMix(); + + NodeStates node_states{}; + EdgeMatrix edge_matrix{}; + std::size_t info_count{}; + std::vector<ServerMixInfo> infos{}; + std::vector<ServerMixInfo*> sorted_info{}; +}; +} // namespace AudioCore diff --git a/src/audio_core/sink_context.cpp b/src/audio_core/sink_context.cpp new file mode 100644 index 000000000..0882b411a --- /dev/null +++ b/src/audio_core/sink_context.cpp @@ -0,0 +1,31 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "audio_core/sink_context.h" + +namespace AudioCore { +SinkContext::SinkContext(std::size_t sink_count) : sink_count(sink_count) {} +SinkContext::~SinkContext() = default; + +std::size_t SinkContext::GetCount() const { + return sink_count; +} + +void SinkContext::UpdateMainSink(SinkInfo::InParams& in) { + in_use = in.in_use; + use_count = in.device.input_count; + std::memcpy(buffers.data(), in.device.input.data(), AudioCommon::MAX_CHANNEL_COUNT); +} + +bool SinkContext::InUse() const { + return in_use; +} + +std::vector<u8> SinkContext::OutputBuffers() const { + std::vector<u8> buffer_ret(use_count); + std::memcpy(buffer_ret.data(), buffers.data(), use_count); + return buffer_ret; +} + +} // namespace AudioCore diff --git a/src/audio_core/sink_context.h b/src/audio_core/sink_context.h new file mode 100644 index 000000000..d7aa72ba7 --- /dev/null +++ b/src/audio_core/sink_context.h @@ -0,0 +1,89 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "audio_core/common.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/swap.h" + +namespace AudioCore { + +enum class SinkTypes : u8 { + Invalid = 0, + Device = 1, + Circular = 2, +}; + +enum class SinkSampleFormat : u32_le { + None = 0, + Pcm8 = 1, + Pcm16 = 2, + Pcm24 = 3, + Pcm32 = 4, + PcmFloat = 5, + Adpcm = 6, +}; + +class SinkInfo { +public: + struct CircularBufferIn { + u64_le address; + u32_le size; + u32_le input_count; + u32_le sample_count; + u32_le previous_position; + SinkSampleFormat sample_format; + std::array<u8, AudioCommon::MAX_CHANNEL_COUNT> input; + bool in_use; + INSERT_UNION_PADDING_BYTES(5); + }; + static_assert(sizeof(SinkInfo::CircularBufferIn) == 0x28, + "SinkInfo::CircularBufferIn is in invalid size"); + + struct DeviceIn { + std::array<u8, 255> device_name; + INSERT_UNION_PADDING_BYTES(1); + s32_le input_count; + std::array<u8, AudioCommon::MAX_CHANNEL_COUNT> input; + INSERT_UNION_PADDING_BYTES(1); + bool down_matrix_enabled; + std::array<float_le, 4> down_matrix_coef; + }; + static_assert(sizeof(SinkInfo::DeviceIn) == 0x11c, "SinkInfo::DeviceIn is an invalid size"); + + struct InParams { + SinkTypes type{}; + bool in_use{}; + INSERT_PADDING_BYTES(2); + u32_le node_id{}; + INSERT_PADDING_WORDS(6); + union { + // std::array<u8, 0x120> raw{}; + SinkInfo::DeviceIn device; + SinkInfo::CircularBufferIn circular_buffer; + }; + }; + static_assert(sizeof(SinkInfo::InParams) == 0x140, "SinkInfo::InParams are an invalid size!"); +}; + +class SinkContext { +public: + explicit SinkContext(std::size_t sink_count); + ~SinkContext(); + + std::size_t GetCount() const; + + void UpdateMainSink(SinkInfo::InParams& in); + bool InUse() const; + std::vector<u8> OutputBuffers() const; + +private: + bool in_use{false}; + s32 use_count{}; + std::array<u8, AudioCommon::MAX_CHANNEL_COUNT> buffers{}; + std::size_t sink_count{}; +}; +} // namespace AudioCore diff --git a/src/audio_core/splitter_context.cpp b/src/audio_core/splitter_context.cpp new file mode 100644 index 000000000..79bb2f516 --- /dev/null +++ b/src/audio_core/splitter_context.cpp @@ -0,0 +1,617 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "audio_core/behavior_info.h" +#include "audio_core/splitter_context.h" +#include "common/alignment.h" +#include "common/assert.h" +#include "common/logging/log.h" + +namespace AudioCore { + +ServerSplitterDestinationData::ServerSplitterDestinationData(s32 id) : id(id) {} +ServerSplitterDestinationData::~ServerSplitterDestinationData() = default; + +void ServerSplitterDestinationData::Update(SplitterInfo::InDestinationParams& header) { + // Log error as these are not actually failure states + if (header.magic != SplitterMagic::DataHeader) { + LOG_ERROR(Audio, "Splitter destination header is invalid!"); + return; + } + + // Incorrect splitter id + if (header.splitter_id != id) { + LOG_ERROR(Audio, "Splitter destination ids do not match!"); + return; + } + + mix_id = header.mix_id; + // Copy our mix volumes + std::copy(header.mix_volumes.begin(), header.mix_volumes.end(), current_mix_volumes.begin()); + if (!in_use && header.in_use) { + // Update mix volumes + std::copy(current_mix_volumes.begin(), current_mix_volumes.end(), last_mix_volumes.begin()); + needs_update = false; + } + in_use = header.in_use; +} + +ServerSplitterDestinationData* ServerSplitterDestinationData::GetNextDestination() { + return next; +} + +const ServerSplitterDestinationData* ServerSplitterDestinationData::GetNextDestination() const { + return next; +} + +void ServerSplitterDestinationData::SetNextDestination(ServerSplitterDestinationData* dest) { + next = dest; +} + +bool ServerSplitterDestinationData::ValidMixId() const { + return GetMixId() != AudioCommon::NO_MIX; +} + +s32 ServerSplitterDestinationData::GetMixId() const { + return mix_id; +} + +bool ServerSplitterDestinationData::IsConfigured() const { + return in_use && ValidMixId(); +} + +float ServerSplitterDestinationData::GetMixVolume(std::size_t i) const { + ASSERT(i < AudioCommon::MAX_MIX_BUFFERS); + return current_mix_volumes.at(i); +} + +const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& +ServerSplitterDestinationData::CurrentMixVolumes() const { + return current_mix_volumes; +} + +const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& +ServerSplitterDestinationData::LastMixVolumes() const { + return last_mix_volumes; +} + +void ServerSplitterDestinationData::MarkDirty() { + needs_update = true; +} + +void ServerSplitterDestinationData::UpdateInternalState() { + if (in_use && needs_update) { + std::copy(current_mix_volumes.begin(), current_mix_volumes.end(), last_mix_volumes.begin()); + } + needs_update = false; +} + +ServerSplitterInfo::ServerSplitterInfo(s32 id) : id(id) {} +ServerSplitterInfo::~ServerSplitterInfo() = default; + +void ServerSplitterInfo::InitializeInfos() { + send_length = 0; + head = nullptr; + new_connection = true; +} + +void ServerSplitterInfo::ClearNewConnectionFlag() { + new_connection = false; +} + +std::size_t ServerSplitterInfo::Update(SplitterInfo::InInfoPrams& header) { + if (header.send_id != id) { + return 0; + } + + sample_rate = header.sample_rate; + new_connection = true; + // We need to update the size here due to the splitter bug being present and providing an + // incorrect size. We're suppose to also update the header here but we just ignore and continue + return (sizeof(s32_le) * (header.length - 1)) + (sizeof(s32_le) * 3); +} + +ServerSplitterDestinationData* ServerSplitterInfo::GetHead() { + return head; +} + +const ServerSplitterDestinationData* ServerSplitterInfo::GetHead() const { + return head; +} + +ServerSplitterDestinationData* ServerSplitterInfo::GetData(std::size_t depth) { + auto current_head = head; + for (std::size_t i = 0; i < depth; i++) { + if (current_head == nullptr) { + return nullptr; + } + current_head = current_head->GetNextDestination(); + } + return current_head; +} + +const ServerSplitterDestinationData* ServerSplitterInfo::GetData(std::size_t depth) const { + auto current_head = head; + for (std::size_t i = 0; i < depth; i++) { + if (current_head == nullptr) { + return nullptr; + } + current_head = current_head->GetNextDestination(); + } + return current_head; +} + +bool ServerSplitterInfo::HasNewConnection() const { + return new_connection; +} + +s32 ServerSplitterInfo::GetLength() const { + return send_length; +} + +void ServerSplitterInfo::SetHead(ServerSplitterDestinationData* new_head) { + head = new_head; +} + +void ServerSplitterInfo::SetHeadDepth(s32 length) { + send_length = length; +} + +SplitterContext::SplitterContext() = default; +SplitterContext::~SplitterContext() = default; + +void SplitterContext::Initialize(BehaviorInfo& behavior_info, std::size_t _info_count, + std::size_t _data_count) { + if (!behavior_info.IsSplitterSupported() || _data_count == 0 || _info_count == 0) { + Setup(0, 0, false); + return; + } + // Only initialize if we're using splitters + Setup(_info_count, _data_count, behavior_info.IsSplitterBugFixed()); +} + +bool SplitterContext::Update(const std::vector<u8>& input, std::size_t& input_offset, + std::size_t& bytes_read) { + const auto UpdateOffsets = [&](std::size_t read) { + input_offset += read; + bytes_read += read; + }; + + if (info_count == 0 || data_count == 0) { + bytes_read = 0; + return true; + } + + if (!AudioCommon::CanConsumeBuffer(input.size(), input_offset, + sizeof(SplitterInfo::InHeader))) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + SplitterInfo::InHeader header{}; + std::memcpy(&header, input.data() + input_offset, sizeof(SplitterInfo::InHeader)); + UpdateOffsets(sizeof(SplitterInfo::InHeader)); + + if (header.magic != SplitterMagic::SplitterHeader) { + LOG_ERROR(Audio, "Invalid header magic! Expecting {:X} but got {:X}", + SplitterMagic::SplitterHeader, header.magic); + return false; + } + + // Clear all connections + for (auto& info : infos) { + info.ClearNewConnectionFlag(); + } + + UpdateInfo(input, input_offset, bytes_read, header.info_count); + UpdateData(input, input_offset, bytes_read, header.data_count); + const auto aligned_bytes_read = Common::AlignUp(bytes_read, 16); + input_offset += aligned_bytes_read - bytes_read; + bytes_read = aligned_bytes_read; + return true; +} + +bool SplitterContext::UsingSplitter() const { + return info_count > 0 && data_count > 0; +} + +ServerSplitterInfo& SplitterContext::GetInfo(std::size_t i) { + ASSERT(i < info_count); + return infos.at(i); +} + +const ServerSplitterInfo& SplitterContext::GetInfo(std::size_t i) const { + ASSERT(i < info_count); + return infos.at(i); +} + +ServerSplitterDestinationData& SplitterContext::GetData(std::size_t i) { + ASSERT(i < data_count); + return datas.at(i); +} + +const ServerSplitterDestinationData& SplitterContext::GetData(std::size_t i) const { + ASSERT(i < data_count); + return datas.at(i); +} + +ServerSplitterDestinationData* SplitterContext::GetDestinationData(std::size_t info, + std::size_t data) { + ASSERT(info < info_count); + auto& cur_info = GetInfo(info); + return cur_info.GetData(data); +} + +const ServerSplitterDestinationData* SplitterContext::GetDestinationData(std::size_t info, + std::size_t data) const { + ASSERT(info < info_count); + auto& cur_info = GetInfo(info); + return cur_info.GetData(data); +} + +void SplitterContext::UpdateInternalState() { + if (data_count == 0) { + return; + } + + for (auto& data : datas) { + data.UpdateInternalState(); + } +} + +std::size_t SplitterContext::GetInfoCount() const { + return info_count; +} + +std::size_t SplitterContext::GetDataCount() const { + return data_count; +} + +void SplitterContext::Setup(std::size_t _info_count, std::size_t _data_count, + bool is_splitter_bug_fixed) { + + info_count = _info_count; + data_count = _data_count; + + for (std::size_t i = 0; i < info_count; i++) { + auto& splitter = infos.emplace_back(static_cast<s32>(i)); + splitter.InitializeInfos(); + } + for (std::size_t i = 0; i < data_count; i++) { + datas.emplace_back(static_cast<s32>(i)); + } + + bug_fixed = is_splitter_bug_fixed; +} + +bool SplitterContext::UpdateInfo(const std::vector<u8>& input, std::size_t& input_offset, + std::size_t& bytes_read, s32 in_splitter_count) { + const auto UpdateOffsets = [&](std::size_t read) { + input_offset += read; + bytes_read += read; + }; + + for (s32 i = 0; i < in_splitter_count; i++) { + if (!AudioCommon::CanConsumeBuffer(input.size(), input_offset, + sizeof(SplitterInfo::InInfoPrams))) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + SplitterInfo::InInfoPrams header{}; + std::memcpy(&header, input.data() + input_offset, sizeof(SplitterInfo::InInfoPrams)); + + // Logged as warning as these don't actually cause a bailout for some reason + if (header.magic != SplitterMagic::InfoHeader) { + LOG_ERROR(Audio, "Bad splitter data header"); + break; + } + + if (header.send_id < 0 || header.send_id > info_count) { + LOG_ERROR(Audio, "Bad splitter data id"); + break; + } + + UpdateOffsets(sizeof(SplitterInfo::InInfoPrams)); + auto& info = GetInfo(header.send_id); + if (!RecomposeDestination(info, header, input, input_offset)) { + LOG_ERROR(Audio, "Failed to recompose destination for splitter!"); + return false; + } + const std::size_t read = info.Update(header); + bytes_read += read; + input_offset += read; + } + return true; +} + +bool SplitterContext::UpdateData(const std::vector<u8>& input, std::size_t& input_offset, + std::size_t& bytes_read, s32 in_data_count) { + const auto UpdateOffsets = [&](std::size_t read) { + input_offset += read; + bytes_read += read; + }; + + for (s32 i = 0; i < in_data_count; i++) { + if (!AudioCommon::CanConsumeBuffer(input.size(), input_offset, + sizeof(SplitterInfo::InDestinationParams))) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + SplitterInfo::InDestinationParams header{}; + std::memcpy(&header, input.data() + input_offset, + sizeof(SplitterInfo::InDestinationParams)); + UpdateOffsets(sizeof(SplitterInfo::InDestinationParams)); + + // Logged as warning as these don't actually cause a bailout for some reason + if (header.magic != SplitterMagic::DataHeader) { + LOG_ERROR(Audio, "Bad splitter data header"); + break; + } + + if (header.splitter_id < 0 || header.splitter_id > data_count) { + LOG_ERROR(Audio, "Bad splitter data id"); + break; + } + GetData(header.splitter_id).Update(header); + } + return true; +} + +bool SplitterContext::RecomposeDestination(ServerSplitterInfo& info, + SplitterInfo::InInfoPrams& header, + const std::vector<u8>& input, + const std::size_t& input_offset) { + // Clear our current destinations + auto* current_head = info.GetHead(); + while (current_head != nullptr) { + auto next_head = current_head->GetNextDestination(); + current_head->SetNextDestination(nullptr); + current_head = next_head; + } + info.SetHead(nullptr); + + s32 size = header.length; + // If the splitter bug is present, calculate fixed size + if (!bug_fixed) { + if (info_count > 0) { + const auto factor = data_count / info_count; + size = std::min(header.length, static_cast<s32>(factor)); + } else { + size = 0; + } + } + + if (size < 1) { + LOG_ERROR(Audio, "Invalid splitter info size! size={:X}", size); + return true; + } + + auto* start_head = &GetData(header.resource_id_base); + current_head = start_head; + std::vector<s32_le> resource_ids(size - 1); + if (!AudioCommon::CanConsumeBuffer(input.size(), input_offset, + resource_ids.size() * sizeof(s32_le))) { + LOG_ERROR(Audio, "Buffer is an invalid size!"); + return false; + } + std::memcpy(resource_ids.data(), input.data() + input_offset, + resource_ids.size() * sizeof(s32_le)); + + for (auto resource_id : resource_ids) { + auto* head = &GetData(resource_id); + current_head->SetNextDestination(head); + current_head = head; + } + + info.SetHead(start_head); + info.SetHeadDepth(size); + + return true; +} + +NodeStates::NodeStates() = default; +NodeStates::~NodeStates() = default; + +void NodeStates::Initialize(std::size_t node_count_) { + // Setup our work parameters + node_count = node_count_; + was_node_found.resize(node_count); + was_node_completed.resize(node_count); + index_list.resize(node_count); + index_stack.Reset(node_count * node_count); +} + +bool NodeStates::Tsort(EdgeMatrix& edge_matrix) { + return DepthFirstSearch(edge_matrix); +} + +std::size_t NodeStates::GetIndexPos() const { + return index_pos; +} + +const std::vector<s32>& NodeStates::GetIndexList() const { + return index_list; +} + +void NodeStates::PushTsortResult(s32 index) { + ASSERT(index < node_count); + index_list[index_pos++] = index; +} + +bool NodeStates::DepthFirstSearch(EdgeMatrix& edge_matrix) { + ResetState(); + for (std::size_t i = 0; i < node_count; i++) { + const auto node_id = static_cast<s32>(i); + + // If we don't have a state, send to our index stack for work + if (GetState(i) == NodeStates::State::NoState) { + index_stack.push(node_id); + } + + // While we have work to do in our stack + while (index_stack.Count() > 0) { + // Get the current node + const auto current_stack_index = index_stack.top(); + // Check if we've seen the node yet + const auto index_state = GetState(current_stack_index); + if (index_state == NodeStates::State::NoState) { + // Mark the node as seen + UpdateState(NodeStates::State::InFound, current_stack_index); + } else if (index_state == NodeStates::State::InFound) { + // We've seen this node before, mark it as completed + UpdateState(NodeStates::State::InCompleted, current_stack_index); + // Update our index list + PushTsortResult(current_stack_index); + // Pop the stack + index_stack.pop(); + continue; + } else if (index_state == NodeStates::State::InCompleted) { + // If our node is already sorted, clear it + index_stack.pop(); + continue; + } + + const auto node_count = edge_matrix.GetNodeCount(); + for (s32 j = 0; j < static_cast<s32>(node_count); j++) { + // Check if our node is connected to our edge matrix + if (!edge_matrix.Connected(current_stack_index, j)) { + continue; + } + + // Check if our node exists + const auto node_state = GetState(j); + if (node_state == NodeStates::State::NoState) { + // Add more work + index_stack.push(j); + } else if (node_state == NodeStates::State::InFound) { + UNREACHABLE_MSG("Node start marked as found"); + ResetState(); + return false; + } + } + } + } + return true; +} + +void NodeStates::ResetState() { + // Reset to the start of our index stack + index_pos = 0; + for (std::size_t i = 0; i < node_count; i++) { + // Mark all nodes as not found + was_node_found[i] = false; + // Mark all nodes as uncompleted + was_node_completed[i] = false; + // Mark all indexes as invalid + index_list[i] = -1; + } +} + +void NodeStates::UpdateState(NodeStates::State state, std::size_t i) { + switch (state) { + case NodeStates::State::NoState: + was_node_found[i] = false; + was_node_completed[i] = false; + break; + case NodeStates::State::InFound: + was_node_found[i] = true; + was_node_completed[i] = false; + break; + case NodeStates::State::InCompleted: + was_node_found[i] = false; + was_node_completed[i] = true; + break; + } +} + +NodeStates::State NodeStates::GetState(std::size_t i) { + ASSERT(i < node_count); + if (was_node_found[i]) { + // If our node exists in our found list + return NodeStates::State::InFound; + } else if (was_node_completed[i]) { + // If node is in the completed list + return NodeStates::State::InCompleted; + } else { + // If in neither + return NodeStates::State::NoState; + } +} + +NodeStates::Stack::Stack() = default; +NodeStates::Stack::~Stack() = default; + +void NodeStates::Stack::Reset(std::size_t size) { + // Mark our stack as empty + stack.resize(size); + stack_size = size; + stack_pos = 0; + std::fill(stack.begin(), stack.end(), 0); +} + +void NodeStates::Stack::push(s32 val) { + ASSERT(stack_pos < stack_size); + stack[stack_pos++] = val; +} + +std::size_t NodeStates::Stack::Count() const { + return stack_pos; +} + +s32 NodeStates::Stack::top() const { + ASSERT(stack_pos > 0); + return stack[stack_pos - 1]; +} + +s32 NodeStates::Stack::pop() { + ASSERT(stack_pos > 0); + stack_pos--; + return stack[stack_pos]; +} + +EdgeMatrix::EdgeMatrix() = default; +EdgeMatrix::~EdgeMatrix() = default; + +void EdgeMatrix::Initialize(std::size_t _node_count) { + node_count = _node_count; + edge_matrix.resize(node_count * node_count); +} + +bool EdgeMatrix::Connected(s32 a, s32 b) { + return GetState(a, b); +} + +void EdgeMatrix::Connect(s32 a, s32 b) { + SetState(a, b, true); +} + +void EdgeMatrix::Disconnect(s32 a, s32 b) { + SetState(a, b, false); +} + +void EdgeMatrix::RemoveEdges(s32 edge) { + for (std::size_t i = 0; i < node_count; i++) { + SetState(edge, static_cast<s32>(i), false); + } +} + +std::size_t EdgeMatrix::GetNodeCount() const { + return node_count; +} + +void EdgeMatrix::SetState(s32 a, s32 b, bool state) { + ASSERT(InRange(a, b)); + edge_matrix.at(a * node_count + b) = state; +} + +bool EdgeMatrix::GetState(s32 a, s32 b) { + ASSERT(InRange(a, b)); + return edge_matrix.at(a * node_count + b); +} + +bool EdgeMatrix::InRange(s32 a, s32 b) const { + const std::size_t pos = a * node_count + b; + return pos < (node_count * node_count); +} + +} // namespace AudioCore diff --git a/src/audio_core/splitter_context.h b/src/audio_core/splitter_context.h new file mode 100644 index 000000000..ea6239fdb --- /dev/null +++ b/src/audio_core/splitter_context.h @@ -0,0 +1,221 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <stack> +#include <vector> +#include "audio_core/common.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/swap.h" + +namespace AudioCore { +class BehaviorInfo; + +class EdgeMatrix { +public: + EdgeMatrix(); + ~EdgeMatrix(); + + void Initialize(std::size_t _node_count); + bool Connected(s32 a, s32 b); + void Connect(s32 a, s32 b); + void Disconnect(s32 a, s32 b); + void RemoveEdges(s32 edge); + std::size_t GetNodeCount() const; + +private: + void SetState(s32 a, s32 b, bool state); + bool GetState(s32 a, s32 b); + + bool InRange(s32 a, s32 b) const; + std::vector<bool> edge_matrix{}; + std::size_t node_count{}; +}; + +class NodeStates { +public: + enum class State { + NoState = 0, + InFound = 1, + InCompleted = 2, + }; + + // Looks to be a fixed size stack. Placed within the NodeStates class based on symbols + class Stack { + public: + Stack(); + ~Stack(); + + void Reset(std::size_t size); + void push(s32 val); + std::size_t Count() const; + s32 top() const; + s32 pop(); + + private: + std::vector<s32> stack{}; + std::size_t stack_size{}; + std::size_t stack_pos{}; + }; + NodeStates(); + ~NodeStates(); + + void Initialize(std::size_t _node_count); + bool Tsort(EdgeMatrix& edge_matrix); + std::size_t GetIndexPos() const; + const std::vector<s32>& GetIndexList() const; + +private: + void PushTsortResult(s32 index); + bool DepthFirstSearch(EdgeMatrix& edge_matrix); + void ResetState(); + void UpdateState(NodeStates::State state, std::size_t i); + NodeStates::State GetState(std::size_t i); + + std::size_t node_count{}; + std::vector<bool> was_node_found{}; + std::vector<bool> was_node_completed{}; + std::size_t index_pos{}; + std::vector<s32> index_list{}; + NodeStates::Stack index_stack{}; +}; + +enum class SplitterMagic : u32_le { + SplitterHeader = Common::MakeMagic('S', 'N', 'D', 'H'), + DataHeader = Common::MakeMagic('S', 'N', 'D', 'D'), + InfoHeader = Common::MakeMagic('S', 'N', 'D', 'I'), +}; + +class SplitterInfo { +public: + struct InHeader { + SplitterMagic magic{}; + s32_le info_count{}; + s32_le data_count{}; + INSERT_PADDING_WORDS(5); + }; + static_assert(sizeof(SplitterInfo::InHeader) == 0x20, + "SplitterInfo::InHeader is an invalid size"); + + struct InInfoPrams { + SplitterMagic magic{}; + s32_le send_id{}; + s32_le sample_rate{}; + s32_le length{}; + s32_le resource_id_base{}; + }; + static_assert(sizeof(SplitterInfo::InInfoPrams) == 0x14, + "SplitterInfo::InInfoPrams is an invalid size"); + + struct InDestinationParams { + SplitterMagic magic{}; + s32_le splitter_id{}; + std::array<float_le, AudioCommon::MAX_MIX_BUFFERS> mix_volumes{}; + s32_le mix_id{}; + bool in_use{}; + INSERT_PADDING_BYTES(3); + }; + static_assert(sizeof(SplitterInfo::InDestinationParams) == 0x70, + "SplitterInfo::InDestinationParams is an invalid size"); +}; + +class ServerSplitterDestinationData { +public: + explicit ServerSplitterDestinationData(s32 id); + ~ServerSplitterDestinationData(); + + void Update(SplitterInfo::InDestinationParams& header); + + ServerSplitterDestinationData* GetNextDestination(); + const ServerSplitterDestinationData* GetNextDestination() const; + void SetNextDestination(ServerSplitterDestinationData* dest); + bool ValidMixId() const; + s32 GetMixId() const; + bool IsConfigured() const; + float GetMixVolume(std::size_t i) const; + const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& CurrentMixVolumes() const; + const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& LastMixVolumes() const; + void MarkDirty(); + void UpdateInternalState(); + +private: + bool needs_update{}; + bool in_use{}; + s32 id{}; + s32 mix_id{}; + std::array<float, AudioCommon::MAX_MIX_BUFFERS> current_mix_volumes{}; + std::array<float, AudioCommon::MAX_MIX_BUFFERS> last_mix_volumes{}; + ServerSplitterDestinationData* next = nullptr; +}; + +class ServerSplitterInfo { +public: + explicit ServerSplitterInfo(s32 id); + ~ServerSplitterInfo(); + + void InitializeInfos(); + void ClearNewConnectionFlag(); + std::size_t Update(SplitterInfo::InInfoPrams& header); + + ServerSplitterDestinationData* GetHead(); + const ServerSplitterDestinationData* GetHead() const; + ServerSplitterDestinationData* GetData(std::size_t depth); + const ServerSplitterDestinationData* GetData(std::size_t depth) const; + + bool HasNewConnection() const; + s32 GetLength() const; + + void SetHead(ServerSplitterDestinationData* new_head); + void SetHeadDepth(s32 length); + +private: + s32 sample_rate{}; + s32 id{}; + s32 send_length{}; + ServerSplitterDestinationData* head = nullptr; + bool new_connection{}; +}; + +class SplitterContext { +public: + SplitterContext(); + ~SplitterContext(); + + void Initialize(BehaviorInfo& behavior_info, std::size_t splitter_count, + std::size_t data_count); + + bool Update(const std::vector<u8>& input, std::size_t& input_offset, std::size_t& bytes_read); + bool UsingSplitter() const; + + ServerSplitterInfo& GetInfo(std::size_t i); + const ServerSplitterInfo& GetInfo(std::size_t i) const; + ServerSplitterDestinationData& GetData(std::size_t i); + const ServerSplitterDestinationData& GetData(std::size_t i) const; + ServerSplitterDestinationData* GetDestinationData(std::size_t info, std::size_t data); + const ServerSplitterDestinationData* GetDestinationData(std::size_t info, + std::size_t data) const; + void UpdateInternalState(); + + std::size_t GetInfoCount() const; + std::size_t GetDataCount() const; + +private: + void Setup(std::size_t info_count, std::size_t data_count, bool is_splitter_bug_fixed); + bool UpdateInfo(const std::vector<u8>& input, std::size_t& input_offset, + std::size_t& bytes_read, s32 in_splitter_count); + bool UpdateData(const std::vector<u8>& input, std::size_t& input_offset, + std::size_t& bytes_read, s32 in_data_count); + bool RecomposeDestination(ServerSplitterInfo& info, SplitterInfo::InInfoPrams& header, + const std::vector<u8>& input, const std::size_t& input_offset); + + std::vector<ServerSplitterInfo> infos{}; + std::vector<ServerSplitterDestinationData> datas{}; + + std::size_t info_count{}; + std::size_t data_count{}; + bool bug_fixed{}; +}; +} // namespace AudioCore diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp index 7be5d5087..cb33926bc 100644 --- a/src/audio_core/stream.cpp +++ b/src/audio_core/stream.cpp @@ -104,11 +104,7 @@ void Stream::PlayNextBuffer(std::chrono::nanoseconds ns_late) { sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples()); - const auto time_stretch_delta = Settings::values.enable_audio_stretching.GetValue() - ? std::chrono::nanoseconds::zero() - : ns_late; - const auto future_time = GetBufferReleaseNS(*active_buffer) - time_stretch_delta; - core_timing.ScheduleEvent(future_time, release_event, {}); + core_timing.ScheduleEvent(GetBufferReleaseNS(*active_buffer) - ns_late, release_event, {}); } void Stream::ReleaseActiveBuffer(std::chrono::nanoseconds ns_late) { diff --git a/src/audio_core/voice_context.cpp b/src/audio_core/voice_context.cpp new file mode 100644 index 000000000..1d8f69844 --- /dev/null +++ b/src/audio_core/voice_context.cpp @@ -0,0 +1,526 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "audio_core/behavior_info.h" +#include "audio_core/voice_context.h" +#include "core/memory.h" + +namespace AudioCore { + +ServerVoiceChannelResource::ServerVoiceChannelResource(s32 id) : id(id) {} +ServerVoiceChannelResource::~ServerVoiceChannelResource() = default; + +bool ServerVoiceChannelResource::InUse() const { + return in_use; +} + +float ServerVoiceChannelResource::GetCurrentMixVolumeAt(std::size_t i) const { + ASSERT(i < AudioCommon::MAX_MIX_BUFFERS); + return mix_volume.at(i); +} + +float ServerVoiceChannelResource::GetLastMixVolumeAt(std::size_t i) const { + ASSERT(i < AudioCommon::MAX_MIX_BUFFERS); + return last_mix_volume.at(i); +} + +void ServerVoiceChannelResource::Update(VoiceChannelResource::InParams& in_params) { + in_use = in_params.in_use; + // Update our mix volumes only if it's in use + if (in_params.in_use) { + mix_volume = in_params.mix_volume; + } +} + +void ServerVoiceChannelResource::UpdateLastMixVolumes() { + last_mix_volume = mix_volume; +} + +const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& +ServerVoiceChannelResource::GetCurrentMixVolume() const { + return mix_volume; +} + +const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& +ServerVoiceChannelResource::GetLastMixVolume() const { + return last_mix_volume; +} + +ServerVoiceInfo::ServerVoiceInfo() { + Initialize(); +} +ServerVoiceInfo::~ServerVoiceInfo() = default; + +void ServerVoiceInfo::Initialize() { + in_params.in_use = false; + in_params.node_id = 0; + in_params.id = 0; + in_params.current_playstate = ServerPlayState::Stop; + in_params.priority = 255; + in_params.sample_rate = 0; + in_params.sample_format = SampleFormat::Invalid; + in_params.channel_count = 0; + in_params.pitch = 0.0f; + in_params.volume = 0.0f; + in_params.last_volume = 0.0f; + in_params.biquad_filter.fill({}); + in_params.wave_buffer_count = 0; + in_params.wave_bufffer_head = 0; + in_params.mix_id = AudioCommon::NO_MIX; + in_params.splitter_info_id = AudioCommon::NO_SPLITTER; + in_params.additional_params_address = 0; + in_params.additional_params_size = 0; + in_params.is_new = false; + out_params.played_sample_count = 0; + out_params.wave_buffer_consumed = 0; + in_params.voice_drop_flag = false; + in_params.buffer_mapped = false; + in_params.wave_buffer_flush_request_count = 0; + in_params.was_biquad_filter_enabled.fill(false); + + for (auto& wave_buffer : in_params.wave_buffer) { + wave_buffer.start_sample_offset = 0; + wave_buffer.end_sample_offset = 0; + wave_buffer.is_looping = false; + wave_buffer.end_of_stream = false; + wave_buffer.buffer_address = 0; + wave_buffer.buffer_size = 0; + wave_buffer.context_address = 0; + wave_buffer.context_size = 0; + wave_buffer.sent_to_dsp = true; + } + + stored_samples.clear(); +} + +void ServerVoiceInfo::UpdateParameters(const VoiceInfo::InParams& voice_in, + BehaviorInfo& behavior_info) { + in_params.in_use = voice_in.is_in_use; + in_params.id = voice_in.id; + in_params.node_id = voice_in.node_id; + in_params.last_playstate = in_params.current_playstate; + switch (voice_in.play_state) { + case PlayState::Paused: + in_params.current_playstate = ServerPlayState::Paused; + break; + case PlayState::Stopped: + if (in_params.current_playstate != ServerPlayState::Stop) { + in_params.current_playstate = ServerPlayState::RequestStop; + } + break; + case PlayState::Started: + in_params.current_playstate = ServerPlayState::Play; + break; + default: + UNREACHABLE_MSG("Unknown playstate {}", voice_in.play_state); + break; + } + + in_params.priority = voice_in.priority; + in_params.sorting_order = voice_in.sorting_order; + in_params.sample_rate = voice_in.sample_rate; + in_params.sample_format = voice_in.sample_format; + in_params.channel_count = voice_in.channel_count; + in_params.pitch = voice_in.pitch; + in_params.volume = voice_in.volume; + in_params.biquad_filter = voice_in.biquad_filter; + in_params.wave_buffer_count = voice_in.wave_buffer_count; + in_params.wave_bufffer_head = voice_in.wave_buffer_head; + if (behavior_info.IsFlushVoiceWaveBuffersSupported()) { + in_params.wave_buffer_flush_request_count += voice_in.wave_buffer_flush_request_count; + } + in_params.mix_id = voice_in.mix_id; + if (behavior_info.IsSplitterSupported()) { + in_params.splitter_info_id = voice_in.splitter_info_id; + } else { + in_params.splitter_info_id = AudioCommon::NO_SPLITTER; + } + + std::memcpy(in_params.voice_channel_resource_id.data(), + voice_in.voice_channel_resource_ids.data(), + sizeof(s32) * in_params.voice_channel_resource_id.size()); + + if (behavior_info.IsVoicePlayedSampleCountResetAtLoopPointSupported()) { + in_params.behavior_flags.is_played_samples_reset_at_loop_point = + voice_in.behavior_flags.is_played_samples_reset_at_loop_point; + } else { + in_params.behavior_flags.is_played_samples_reset_at_loop_point.Assign(0); + } + if (behavior_info.IsVoicePitchAndSrcSkippedSupported()) { + in_params.behavior_flags.is_pitch_and_src_skipped = + voice_in.behavior_flags.is_pitch_and_src_skipped; + } else { + in_params.behavior_flags.is_pitch_and_src_skipped.Assign(0); + } + + if (voice_in.is_voice_drop_flag_clear_requested) { + in_params.voice_drop_flag = false; + } + + if (in_params.additional_params_address != voice_in.additional_params_address || + in_params.additional_params_size != voice_in.additional_params_size) { + in_params.additional_params_address = voice_in.additional_params_address; + in_params.additional_params_size = voice_in.additional_params_size; + // TODO(ogniK): Reattach buffer, do we actually need to? Maybe just signal to the DSP that + // our context is new + } +} + +void ServerVoiceInfo::UpdateWaveBuffers( + const VoiceInfo::InParams& voice_in, + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states, + BehaviorInfo& behavior_info) { + if (voice_in.is_new) { + // Initialize our wave buffers + for (auto& wave_buffer : in_params.wave_buffer) { + wave_buffer.start_sample_offset = 0; + wave_buffer.end_sample_offset = 0; + wave_buffer.is_looping = false; + wave_buffer.end_of_stream = false; + wave_buffer.buffer_address = 0; + wave_buffer.buffer_size = 0; + wave_buffer.context_address = 0; + wave_buffer.context_size = 0; + wave_buffer.sent_to_dsp = true; + } + + // Mark all our wave buffers as invalid + for (std::size_t channel = 0; channel < static_cast<std::size_t>(in_params.channel_count); + channel++) { + for (auto& is_valid : voice_states[channel]->is_wave_buffer_valid) { + is_valid = false; + } + } + } + + // Update our wave buffers + for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { + // Assume that we have at least 1 channel voice state + const auto have_valid_wave_buffer = voice_states[0]->is_wave_buffer_valid[i]; + + UpdateWaveBuffer(in_params.wave_buffer[i], voice_in.wave_buffer[i], in_params.sample_format, + have_valid_wave_buffer, behavior_info); + } +} + +void ServerVoiceInfo::UpdateWaveBuffer(ServerWaveBuffer& out_wavebuffer, + const WaveBuffer& in_wave_buffer, SampleFormat sample_format, + bool is_buffer_valid, BehaviorInfo& behavior_info) { + if (!is_buffer_valid && out_wavebuffer.sent_to_dsp) { + out_wavebuffer.buffer_address = 0; + out_wavebuffer.buffer_size = 0; + } + + if (!in_wave_buffer.sent_to_server || !in_params.buffer_mapped) { + // Validate sample offset sizings + if (sample_format == SampleFormat::Pcm16) { + const auto buffer_size = in_wave_buffer.buffer_size; + if (in_wave_buffer.start_sample_offset < 0 || in_wave_buffer.end_sample_offset < 0 || + (buffer_size < (sizeof(s16) * in_wave_buffer.start_sample_offset)) || + (buffer_size < (sizeof(s16) * in_wave_buffer.end_sample_offset))) { + // TODO(ogniK): Write error info + return; + } + } + // TODO(ogniK): ADPCM Size error + + out_wavebuffer.sent_to_dsp = false; + out_wavebuffer.start_sample_offset = in_wave_buffer.start_sample_offset; + out_wavebuffer.end_sample_offset = in_wave_buffer.end_sample_offset; + out_wavebuffer.is_looping = in_wave_buffer.is_looping; + out_wavebuffer.end_of_stream = in_wave_buffer.end_of_stream; + + out_wavebuffer.buffer_address = in_wave_buffer.buffer_address; + out_wavebuffer.buffer_size = in_wave_buffer.buffer_size; + out_wavebuffer.context_address = in_wave_buffer.context_address; + out_wavebuffer.context_size = in_wave_buffer.context_size; + in_params.buffer_mapped = + in_wave_buffer.buffer_address != 0 && in_wave_buffer.buffer_size != 0; + // TODO(ogniK): Pool mapper attachment + // TODO(ogniK): IsAdpcmLoopContextBugFixed + } +} + +void ServerVoiceInfo::WriteOutStatus( + VoiceInfo::OutParams& voice_out, VoiceInfo::InParams& voice_in, + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states) { + if (voice_in.is_new) { + in_params.is_new = true; + voice_out.wave_buffer_consumed = 0; + voice_out.played_sample_count = 0; + voice_out.voice_dropped = false; + } else if (!in_params.is_new) { + voice_out.wave_buffer_consumed = voice_states[0]->wave_buffer_consumed; + voice_out.played_sample_count = voice_states[0]->played_sample_count; + voice_out.voice_dropped = in_params.voice_drop_flag; + } else { + voice_out.wave_buffer_consumed = 0; + voice_out.played_sample_count = 0; + voice_out.voice_dropped = false; + } +} + +const ServerVoiceInfo::InParams& ServerVoiceInfo::GetInParams() const { + return in_params; +} + +ServerVoiceInfo::InParams& ServerVoiceInfo::GetInParams() { + return in_params; +} + +const ServerVoiceInfo::OutParams& ServerVoiceInfo::GetOutParams() const { + return out_params; +} + +ServerVoiceInfo::OutParams& ServerVoiceInfo::GetOutParams() { + return out_params; +} + +bool ServerVoiceInfo::ShouldSkip() const { + // TODO(ogniK): Handle unmapped wave buffers or parameters + return !in_params.in_use || (in_params.wave_buffer_count == 0) || in_params.voice_drop_flag; +} + +bool ServerVoiceInfo::UpdateForCommandGeneration(VoiceContext& voice_context) { + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT> dsp_voice_states{}; + if (in_params.is_new) { + ResetResources(voice_context); + in_params.last_volume = in_params.volume; + in_params.is_new = false; + } + + const s32 channel_count = in_params.channel_count; + for (s32 i = 0; i < channel_count; i++) { + const auto channel_resource = in_params.voice_channel_resource_id[i]; + dsp_voice_states[i] = + &voice_context.GetDspSharedState(static_cast<std::size_t>(channel_resource)); + } + return UpdateParametersForCommandGeneration(dsp_voice_states); +} + +void ServerVoiceInfo::ResetResources(VoiceContext& voice_context) { + const s32 channel_count = in_params.channel_count; + for (s32 i = 0; i < channel_count; i++) { + const auto channel_resource = in_params.voice_channel_resource_id[i]; + auto& dsp_state = + voice_context.GetDspSharedState(static_cast<std::size_t>(channel_resource)); + dsp_state = {}; + voice_context.GetChannelResource(static_cast<std::size_t>(channel_resource)) + .UpdateLastMixVolumes(); + } +} + +bool ServerVoiceInfo::UpdateParametersForCommandGeneration( + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states) { + const s32 channel_count = in_params.channel_count; + if (in_params.wave_buffer_flush_request_count > 0) { + FlushWaveBuffers(in_params.wave_buffer_flush_request_count, dsp_voice_states, + channel_count); + in_params.wave_buffer_flush_request_count = 0; + } + + switch (in_params.current_playstate) { + case ServerPlayState::Play: { + for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { + if (!in_params.wave_buffer[i].sent_to_dsp) { + for (s32 channel = 0; channel < channel_count; channel++) { + dsp_voice_states[channel]->is_wave_buffer_valid[i] = true; + } + in_params.wave_buffer[i].sent_to_dsp = true; + } + } + in_params.should_depop = false; + return HasValidWaveBuffer(dsp_voice_states[0]); + } + case ServerPlayState::Paused: + case ServerPlayState::Stop: { + in_params.should_depop = in_params.last_playstate == ServerPlayState::Play; + return in_params.should_depop; + } + case ServerPlayState::RequestStop: { + for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { + in_params.wave_buffer[i].sent_to_dsp = true; + for (s32 channel = 0; channel < channel_count; channel++) { + auto* dsp_state = dsp_voice_states[channel]; + + if (dsp_state->is_wave_buffer_valid[i]) { + dsp_state->wave_buffer_index = + (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; + dsp_state->wave_buffer_consumed++; + } + + dsp_state->is_wave_buffer_valid[i] = false; + } + } + + for (s32 channel = 0; channel < channel_count; channel++) { + auto* dsp_state = dsp_voice_states[channel]; + dsp_state->offset = 0; + dsp_state->played_sample_count = 0; + dsp_state->fraction = 0; + dsp_state->sample_history.fill(0); + dsp_state->context = {}; + } + + in_params.current_playstate = ServerPlayState::Stop; + in_params.should_depop = in_params.last_playstate == ServerPlayState::Play; + return in_params.should_depop; + } + default: + UNREACHABLE_MSG("Invalid playstate {}", in_params.current_playstate); + } + + return false; +} + +void ServerVoiceInfo::FlushWaveBuffers( + u8 flush_count, std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states, + s32 channel_count) { + auto wave_head = in_params.wave_bufffer_head; + + for (u8 i = 0; i < flush_count; i++) { + in_params.wave_buffer[wave_head].sent_to_dsp = true; + for (s32 channel = 0; channel < channel_count; channel++) { + auto* dsp_state = dsp_voice_states[channel]; + dsp_state->wave_buffer_consumed++; + dsp_state->is_wave_buffer_valid[wave_head] = false; + dsp_state->wave_buffer_index = + (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; + } + wave_head = (wave_head + 1) % AudioCommon::MAX_WAVE_BUFFERS; + } +} + +bool ServerVoiceInfo::HasValidWaveBuffer(const VoiceState* state) const { + const auto& valid_wb = state->is_wave_buffer_valid; + return std::find(valid_wb.begin(), valid_wb.end(), true) != valid_wb.end(); +} + +VoiceContext::VoiceContext(std::size_t voice_count) : voice_count(voice_count) { + for (std::size_t i = 0; i < voice_count; i++) { + voice_channel_resources.emplace_back(static_cast<s32>(i)); + sorted_voice_info.push_back(&voice_info.emplace_back()); + voice_states.emplace_back(); + dsp_voice_states.emplace_back(); + } +} + +VoiceContext::~VoiceContext() { + sorted_voice_info.clear(); +} + +std::size_t VoiceContext::GetVoiceCount() const { + return voice_count; +} + +ServerVoiceChannelResource& VoiceContext::GetChannelResource(std::size_t i) { + ASSERT(i < voice_count); + return voice_channel_resources.at(i); +} + +const ServerVoiceChannelResource& VoiceContext::GetChannelResource(std::size_t i) const { + ASSERT(i < voice_count); + return voice_channel_resources.at(i); +} + +VoiceState& VoiceContext::GetState(std::size_t i) { + ASSERT(i < voice_count); + return voice_states.at(i); +} + +const VoiceState& VoiceContext::GetState(std::size_t i) const { + ASSERT(i < voice_count); + return voice_states.at(i); +} + +VoiceState& VoiceContext::GetDspSharedState(std::size_t i) { + ASSERT(i < voice_count); + return dsp_voice_states.at(i); +} + +const VoiceState& VoiceContext::GetDspSharedState(std::size_t i) const { + ASSERT(i < voice_count); + return dsp_voice_states.at(i); +} + +ServerVoiceInfo& VoiceContext::GetInfo(std::size_t i) { + ASSERT(i < voice_count); + return voice_info.at(i); +} + +const ServerVoiceInfo& VoiceContext::GetInfo(std::size_t i) const { + ASSERT(i < voice_count); + return voice_info.at(i); +} + +ServerVoiceInfo& VoiceContext::GetSortedInfo(std::size_t i) { + ASSERT(i < voice_count); + return *sorted_voice_info.at(i); +} + +const ServerVoiceInfo& VoiceContext::GetSortedInfo(std::size_t i) const { + ASSERT(i < voice_count); + return *sorted_voice_info.at(i); +} + +s32 VoiceContext::DecodePcm16(s32* output_buffer, ServerWaveBuffer* wave_buffer, s32 channel, + s32 channel_count, s32 buffer_offset, s32 sample_count, + Core::Memory::Memory& memory) { + if (wave_buffer->buffer_address == 0) { + return 0; + } + if (wave_buffer->buffer_size == 0) { + return 0; + } + if (wave_buffer->end_sample_offset < wave_buffer->start_sample_offset) { + return 0; + } + + const auto samples_remaining = + (wave_buffer->end_sample_offset - wave_buffer->start_sample_offset) - buffer_offset; + const auto start_offset = (wave_buffer->start_sample_offset + buffer_offset) * channel_count; + const auto buffer_pos = wave_buffer->buffer_address + start_offset; + + s16* buffer_data = reinterpret_cast<s16*>(memory.GetPointer(buffer_pos)); + + const auto samples_processed = std::min(sample_count, samples_remaining); + + // Fast path + if (channel_count == 1) { + for (std::size_t i = 0; i < samples_processed; i++) { + output_buffer[i] = buffer_data[i]; + } + } else { + for (std::size_t i = 0; i < samples_processed; i++) { + output_buffer[i] = buffer_data[i * channel_count + channel]; + } + } + + return samples_processed; +} + +void VoiceContext::SortInfo() { + for (std::size_t i = 0; i < voice_count; i++) { + sorted_voice_info[i] = &voice_info[i]; + } + + std::sort(sorted_voice_info.begin(), sorted_voice_info.end(), + [](const ServerVoiceInfo* lhs, const ServerVoiceInfo* rhs) { + const auto& lhs_in = lhs->GetInParams(); + const auto& rhs_in = rhs->GetInParams(); + // Sort by priority + if (lhs_in.priority != rhs_in.priority) { + return lhs_in.priority > rhs_in.priority; + } else { + // If the priorities match, sort by sorting order + return lhs_in.sorting_order > rhs_in.sorting_order; + } + }); +} + +void VoiceContext::UpdateStateByDspShared() { + voice_states = dsp_voice_states; +} + +} // namespace AudioCore diff --git a/src/audio_core/voice_context.h b/src/audio_core/voice_context.h new file mode 100644 index 000000000..59d3d7dfb --- /dev/null +++ b/src/audio_core/voice_context.h @@ -0,0 +1,296 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <array> +#include "audio_core/algorithm/interpolate.h" +#include "audio_core/codec.h" +#include "audio_core/common.h" +#include "common/bit_field.h" +#include "common/common_funcs.h" +#include "common/common_types.h" + +namespace Core::Memory { +class Memory; +} + +namespace AudioCore { + +class BehaviorInfo; +class VoiceContext; + +enum class SampleFormat : u8 { + Invalid = 0, + Pcm8 = 1, + Pcm16 = 2, + Pcm24 = 3, + Pcm32 = 4, + PcmFloat = 5, + Adpcm = 6, +}; + +enum class PlayState : u8 { + Started = 0, + Stopped = 1, + Paused = 2, +}; + +enum class ServerPlayState { + Play = 0, + Stop = 1, + RequestStop = 2, + Paused = 3, +}; + +struct BiquadFilterParameter { + bool enabled{}; + INSERT_PADDING_BYTES(1); + std::array<s16, 3> numerator{}; + std::array<s16, 2> denominator{}; +}; +static_assert(sizeof(BiquadFilterParameter) == 0xc, "BiquadFilterParameter is an invalid size"); + +struct WaveBuffer { + u64_le buffer_address{}; + u64_le buffer_size{}; + s32_le start_sample_offset{}; + s32_le end_sample_offset{}; + u8 is_looping{}; + u8 end_of_stream{}; + u8 sent_to_server{}; + INSERT_PADDING_BYTES(5); + u64 context_address{}; + u64 context_size{}; + INSERT_PADDING_BYTES(8); +}; +static_assert(sizeof(WaveBuffer) == 0x38, "WaveBuffer is an invalid size"); + +struct ServerWaveBuffer { + VAddr buffer_address{}; + std::size_t buffer_size{}; + s32 start_sample_offset{}; + s32 end_sample_offset{}; + bool is_looping{}; + bool end_of_stream{}; + VAddr context_address{}; + std::size_t context_size{}; + bool sent_to_dsp{true}; +}; + +struct BehaviorFlags { + BitField<0, 1, u16> is_played_samples_reset_at_loop_point; + BitField<1, 1, u16> is_pitch_and_src_skipped; +}; +static_assert(sizeof(BehaviorFlags) == 0x4, "BehaviorFlags is an invalid size"); + +struct ADPCMContext { + u16 header{}; + s16 yn1{}; + s16 yn2{}; +}; +static_assert(sizeof(ADPCMContext) == 0x6, "ADPCMContext is an invalid size"); + +struct VoiceState { + s64 played_sample_count{}; + s32 offset{}; + s32 wave_buffer_index{}; + std::array<bool, AudioCommon::MAX_WAVE_BUFFERS> is_wave_buffer_valid{}; + s32 wave_buffer_consumed{}; + std::array<s32, AudioCommon::MAX_SAMPLE_HISTORY> sample_history{}; + s32 fraction{}; + VAddr context_address{}; + Codec::ADPCM_Coeff coeff{}; + ADPCMContext context{}; + std::array<s64, 2> biquad_filter_state{}; + std::array<s32, AudioCommon::MAX_MIX_BUFFERS> previous_samples{}; + u32 external_context_size{}; + bool is_external_context_used{}; + bool voice_dropped{}; +}; + +class VoiceChannelResource { +public: + struct InParams { + s32_le id{}; + std::array<float_le, AudioCommon::MAX_MIX_BUFFERS> mix_volume{}; + bool in_use{}; + INSERT_PADDING_BYTES(11); + }; + static_assert(sizeof(VoiceChannelResource::InParams) == 0x70, "InParams is an invalid size"); +}; + +class ServerVoiceChannelResource { +public: + explicit ServerVoiceChannelResource(s32 id); + ~ServerVoiceChannelResource(); + + bool InUse() const; + float GetCurrentMixVolumeAt(std::size_t i) const; + float GetLastMixVolumeAt(std::size_t i) const; + void Update(VoiceChannelResource::InParams& in_params); + void UpdateLastMixVolumes(); + + const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& GetCurrentMixVolume() const; + const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& GetLastMixVolume() const; + +private: + s32 id{}; + std::array<float, AudioCommon::MAX_MIX_BUFFERS> mix_volume{}; + std::array<float, AudioCommon::MAX_MIX_BUFFERS> last_mix_volume{}; + bool in_use{}; +}; + +class VoiceInfo { +public: + struct InParams { + s32_le id{}; + u32_le node_id{}; + u8 is_new{}; + u8 is_in_use{}; + PlayState play_state{}; + SampleFormat sample_format{}; + s32_le sample_rate{}; + s32_le priority{}; + s32_le sorting_order{}; + s32_le channel_count{}; + float_le pitch{}; + float_le volume{}; + std::array<BiquadFilterParameter, 2> biquad_filter{}; + s32_le wave_buffer_count{}; + s16_le wave_buffer_head{}; + INSERT_PADDING_BYTES(6); + u64_le additional_params_address{}; + u64_le additional_params_size{}; + s32_le mix_id{}; + s32_le splitter_info_id{}; + std::array<WaveBuffer, 4> wave_buffer{}; + std::array<u32_le, 6> voice_channel_resource_ids{}; + // TODO(ogniK): Remaining flags + u8 is_voice_drop_flag_clear_requested{}; + u8 wave_buffer_flush_request_count{}; + INSERT_PADDING_BYTES(2); + BehaviorFlags behavior_flags{}; + INSERT_PADDING_BYTES(16); + }; + static_assert(sizeof(VoiceInfo::InParams) == 0x170, "InParams is an invalid size"); + + struct OutParams { + u64_le played_sample_count{}; + u32_le wave_buffer_consumed{}; + u8 voice_dropped{}; + INSERT_PADDING_BYTES(3); + }; + static_assert(sizeof(VoiceInfo::OutParams) == 0x10, "OutParams is an invalid size"); +}; + +class ServerVoiceInfo { +public: + struct InParams { + bool in_use{}; + bool is_new{}; + bool should_depop{}; + SampleFormat sample_format{}; + s32 sample_rate{}; + s32 channel_count{}; + s32 id{}; + s32 node_id{}; + s32 mix_id{}; + ServerPlayState current_playstate{}; + ServerPlayState last_playstate{}; + s32 priority{}; + s32 sorting_order{}; + float pitch{}; + float volume{}; + float last_volume{}; + std::array<BiquadFilterParameter, AudioCommon::MAX_BIQUAD_FILTERS> biquad_filter{}; + s32 wave_buffer_count{}; + s16 wave_bufffer_head{}; + INSERT_PADDING_BYTES(2); + BehaviorFlags behavior_flags{}; + VAddr additional_params_address{}; + std::size_t additional_params_size{}; + std::array<ServerWaveBuffer, AudioCommon::MAX_WAVE_BUFFERS> wave_buffer{}; + std::array<s32, AudioCommon::MAX_CHANNEL_COUNT> voice_channel_resource_id{}; + s32 splitter_info_id{}; + u8 wave_buffer_flush_request_count{}; + bool voice_drop_flag{}; + bool buffer_mapped{}; + std::array<bool, AudioCommon::MAX_BIQUAD_FILTERS> was_biquad_filter_enabled{}; + }; + + struct OutParams { + s64 played_sample_count{}; + s32 wave_buffer_consumed{}; + }; + + ServerVoiceInfo(); + ~ServerVoiceInfo(); + void Initialize(); + void UpdateParameters(const VoiceInfo::InParams& voice_in, BehaviorInfo& behavior_info); + void UpdateWaveBuffers(const VoiceInfo::InParams& voice_in, + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states, + BehaviorInfo& behavior_info); + void UpdateWaveBuffer(ServerWaveBuffer& out_wavebuffer, const WaveBuffer& in_wave_buffer, + SampleFormat sample_format, bool is_buffer_valid, + BehaviorInfo& behavior_info); + void WriteOutStatus(VoiceInfo::OutParams& voice_out, VoiceInfo::InParams& voice_in, + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states); + + const InParams& GetInParams() const; + InParams& GetInParams(); + + const OutParams& GetOutParams() const; + OutParams& GetOutParams(); + + bool ShouldSkip() const; + bool UpdateForCommandGeneration(VoiceContext& voice_context); + void ResetResources(VoiceContext& voice_context); + bool UpdateParametersForCommandGeneration( + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states); + void FlushWaveBuffers(u8 flush_count, + std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states, + s32 channel_count); + +private: + std::vector<s16> stored_samples; + InParams in_params{}; + OutParams out_params{}; + + bool HasValidWaveBuffer(const VoiceState* state) const; +}; + +class VoiceContext { +public: + VoiceContext(std::size_t voice_count); + ~VoiceContext(); + + std::size_t GetVoiceCount() const; + ServerVoiceChannelResource& GetChannelResource(std::size_t i); + const ServerVoiceChannelResource& GetChannelResource(std::size_t i) const; + VoiceState& GetState(std::size_t i); + const VoiceState& GetState(std::size_t i) const; + VoiceState& GetDspSharedState(std::size_t i); + const VoiceState& GetDspSharedState(std::size_t i) const; + ServerVoiceInfo& GetInfo(std::size_t i); + const ServerVoiceInfo& GetInfo(std::size_t i) const; + ServerVoiceInfo& GetSortedInfo(std::size_t i); + const ServerVoiceInfo& GetSortedInfo(std::size_t i) const; + + s32 DecodePcm16(s32* output_buffer, ServerWaveBuffer* wave_buffer, s32 channel, + s32 channel_count, s32 buffer_offset, s32 sample_count, + Core::Memory::Memory& memory); + void SortInfo(); + void UpdateStateByDspShared(); + +private: + std::size_t voice_count{}; + std::vector<ServerVoiceChannelResource> voice_channel_resources{}; + std::vector<VoiceState> voice_states{}; + std::vector<VoiceState> dsp_voice_states{}; + std::vector<ServerVoiceInfo> voice_info{}; + std::vector<ServerVoiceInfo*> sorted_voice_info{}; +}; + +} // namespace AudioCore diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index b96ca9374..d0c405ec7 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -126,6 +126,8 @@ add_library(core STATIC file_sys/vfs_vector.h file_sys/xts_archive.cpp file_sys/xts_archive.h + frontend/applets/controller.cpp + frontend/applets/controller.h frontend/applets/error.cpp frontend/applets/error.h frontend/applets/general_frontend.cpp @@ -244,6 +246,8 @@ add_library(core STATIC hle/service/am/applet_oe.h hle/service/am/applets/applets.cpp hle/service/am/applets/applets.h + hle/service/am/applets/controller.cpp + hle/service/am/applets/controller.h hle/service/am/applets/error.cpp hle/service/am/applets/error.h hle/service/am/applets/general_backend.cpp diff --git a/src/core/arm/dynarmic/arm_dynarmic_cp15.h b/src/core/arm/dynarmic/arm_dynarmic_cp15.h index 7356d252e..dc6f4af3a 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_cp15.h +++ b/src/core/arm/dynarmic/arm_dynarmic_cp15.h @@ -35,8 +35,8 @@ public: std::optional<u8> option) override; ARM_Dynarmic_32& parent; - u32 uprw; - u32 uro; + u32 uprw = 0; + u32 uro = 0; }; } // namespace Core diff --git a/src/core/core.cpp b/src/core/core.cpp index df81e8e2c..81e8cc338 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -178,7 +178,7 @@ struct System::Impl { arp_manager.ResetAll(); telemetry_session = std::make_unique<Core::TelemetrySession>(); - service_manager = std::make_shared<Service::SM::ServiceManager>(); + service_manager = std::make_shared<Service::SM::ServiceManager>(kernel); Service::Init(service_manager, system); GDBStub::DeferStart(); @@ -221,7 +221,7 @@ struct System::Impl { telemetry_session->AddInitialInfo(*app_loader); auto main_process = Kernel::Process::Create(system, "main", Kernel::Process::ProcessType::Userland); - const auto [load_result, load_parameters] = app_loader->Load(*main_process); + const auto [load_result, load_parameters] = app_loader->Load(*main_process, system); if (load_result != Loader::ResultStatus::Success) { LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", static_cast<int>(load_result)); Shutdown(); @@ -629,11 +629,11 @@ Loader::AppLoader& System::GetAppLoader() const { return *impl->app_loader; } -void System::SetFilesystem(std::shared_ptr<FileSys::VfsFilesystem> vfs) { +void System::SetFilesystem(FileSys::VirtualFilesystem vfs) { impl->virtual_filesystem = std::move(vfs); } -std::shared_ptr<FileSys::VfsFilesystem> System::GetFilesystem() const { +FileSys::VirtualFilesystem System::GetFilesystem() const { return impl->virtual_filesystem; } diff --git a/src/core/core.h b/src/core/core.h index 5c6cfbffe..83ded63a5 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -316,9 +316,9 @@ public: Service::SM::ServiceManager& ServiceManager(); const Service::SM::ServiceManager& ServiceManager() const; - void SetFilesystem(std::shared_ptr<FileSys::VfsFilesystem> vfs); + void SetFilesystem(FileSys::VirtualFilesystem vfs); - std::shared_ptr<FileSys::VfsFilesystem> GetFilesystem() const; + FileSys::VirtualFilesystem GetFilesystem() const; void RegisterCheatList(const std::vector<Memory::CheatEntry>& list, const std::array<u8, 0x20>& build_id, VAddr main_region_begin, diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index dc591c730..65d246050 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -23,7 +23,6 @@ #include "common/hex_util.h" #include "common/logging/log.h" #include "common/string_util.h" -#include "core/core.h" #include "core/crypto/aes_util.h" #include "core/crypto/key_manager.h" #include "core/crypto/partition_data_manager.h" @@ -1022,10 +1021,10 @@ void KeyManager::DeriveBase() { } } -void KeyManager::DeriveETicket(PartitionDataManager& data) { +void KeyManager::DeriveETicket(PartitionDataManager& data, + const FileSys::ContentProvider& provider) { // ETicket keys - const auto es = Core::System::GetInstance().GetContentProvider().GetEntry( - 0x0100000000000033, FileSys::ContentRecordType::Program); + const auto es = provider.GetEntry(0x0100000000000033, FileSys::ContentRecordType::Program); if (es == nullptr) { return; diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index 321b75323..0a7220286 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -20,6 +20,10 @@ namespace Common::FS { class IOFile; } +namespace FileSys { +class ContentProvider; +} + namespace Loader { enum class ResultStatus : u16; } @@ -252,7 +256,7 @@ public: bool BaseDeriveNecessary() const; void DeriveBase(); - void DeriveETicket(PartitionDataManager& data); + void DeriveETicket(PartitionDataManager& data, const FileSys::ContentProvider& provider); void PopulateTickets(); void SynthesizeTickets(); diff --git a/src/core/file_sys/bis_factory.cpp b/src/core/file_sys/bis_factory.cpp index e04a54c3c..7c6304ff0 100644 --- a/src/core/file_sys/bis_factory.cpp +++ b/src/core/file_sys/bis_factory.cpp @@ -4,10 +4,10 @@ #include <fmt/format.h> #include "common/file_util.h" -#include "core/core.h" #include "core/file_sys/bis_factory.h" #include "core/file_sys/mode.h" #include "core/file_sys/registered_cache.h" +#include "core/file_sys/vfs.h" namespace FileSys { @@ -81,11 +81,11 @@ VirtualDir BISFactory::OpenPartition(BisPartitionId id) const { } } -VirtualFile BISFactory::OpenPartitionStorage(BisPartitionId id) const { +VirtualFile BISFactory::OpenPartitionStorage(BisPartitionId id, + VirtualFilesystem file_system) const { auto& keys = Core::Crypto::KeyManager::Instance(); - Core::Crypto::PartitionDataManager pdm{ - Core::System::GetInstance().GetFilesystem()->OpenDirectory( - Common::FS::GetUserPath(Common::FS::UserPath::SysDataDir), Mode::Read)}; + Core::Crypto::PartitionDataManager pdm{file_system->OpenDirectory( + Common::FS::GetUserPath(Common::FS::UserPath::SysDataDir), Mode::Read)}; keys.PopulateFromPartitionData(pdm); switch (id) { diff --git a/src/core/file_sys/bis_factory.h b/src/core/file_sys/bis_factory.h index 438d3f8d8..136485881 100644 --- a/src/core/file_sys/bis_factory.h +++ b/src/core/file_sys/bis_factory.h @@ -52,7 +52,7 @@ public: VirtualDir GetModificationDumpRoot(u64 title_id) const; VirtualDir OpenPartition(BisPartitionId id) const; - VirtualFile OpenPartitionStorage(BisPartitionId id) const; + VirtualFile OpenPartitionStorage(BisPartitionId id, VirtualFilesystem file_system) const; VirtualDir GetImageDirectory() const; diff --git a/src/core/file_sys/control_metadata.h b/src/core/file_sys/control_metadata.h index 9ab86e35b..403c4219a 100644 --- a/src/core/file_sys/control_metadata.h +++ b/src/core/file_sys/control_metadata.h @@ -83,7 +83,7 @@ enum class Language : u8 { Italian = 7, Dutch = 8, CanadianFrench = 9, - Portugese = 10, + Portuguese = 10, Russian = 11, Korean = 12, Taiwanese = 13, diff --git a/src/core/file_sys/nca_patch.cpp b/src/core/file_sys/nca_patch.cpp index fe7375e84..5990a2fd5 100644 --- a/src/core/file_sys/nca_patch.cpp +++ b/src/core/file_sys/nca_patch.cpp @@ -12,6 +12,49 @@ #include "core/file_sys/nca_patch.h" namespace FileSys { +namespace { +template <bool Subsection, typename BlockType, typename BucketType> +std::pair<std::size_t, std::size_t> SearchBucketEntry(u64 offset, const BlockType& block, + const BucketType& buckets) { + if constexpr (Subsection) { + const auto& last_bucket = buckets[block.number_buckets - 1]; + if (offset >= last_bucket.entries[last_bucket.number_entries].address_patch) { + return {block.number_buckets - 1, last_bucket.number_entries}; + } + } else { + ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block."); + } + + std::size_t bucket_id = std::count_if( + block.base_offsets.begin() + 1, block.base_offsets.begin() + block.number_buckets, + [&offset](u64 base_offset) { return base_offset <= offset; }); + + const auto& bucket = buckets[bucket_id]; + + if (bucket.number_entries == 1) { + return {bucket_id, 0}; + } + + std::size_t low = 0; + std::size_t mid = 0; + std::size_t high = bucket.number_entries - 1; + while (low <= high) { + mid = (low + high) / 2; + if (bucket.entries[mid].address_patch > offset) { + high = mid - 1; + } else { + if (mid == bucket.number_entries - 1 || + bucket.entries[mid + 1].address_patch > offset) { + return {bucket_id, mid}; + } + + low = mid + 1; + } + } + + UNREACHABLE_MSG("Offset could not be found in BKTR block."); +} +} // Anonymous namespace BKTR::BKTR(VirtualFile base_romfs_, VirtualFile bktr_romfs_, RelocationBlock relocation_, std::vector<RelocationBucket> relocation_buckets_, SubsectionBlock subsection_, @@ -110,46 +153,6 @@ std::size_t BKTR::Read(u8* data, std::size_t length, std::size_t offset) const { return raw_read; } -template <bool Subsection, typename BlockType, typename BucketType> -std::pair<std::size_t, std::size_t> BKTR::SearchBucketEntry(u64 offset, BlockType block, - BucketType buckets) const { - if constexpr (Subsection) { - const auto last_bucket = buckets[block.number_buckets - 1]; - if (offset >= last_bucket.entries[last_bucket.number_entries].address_patch) - return {block.number_buckets - 1, last_bucket.number_entries}; - } else { - ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block."); - } - - std::size_t bucket_id = std::count_if( - block.base_offsets.begin() + 1, block.base_offsets.begin() + block.number_buckets, - [&offset](u64 base_offset) { return base_offset <= offset; }); - - const auto bucket = buckets[bucket_id]; - - if (bucket.number_entries == 1) - return {bucket_id, 0}; - - std::size_t low = 0; - std::size_t mid = 0; - std::size_t high = bucket.number_entries - 1; - while (low <= high) { - mid = (low + high) / 2; - if (bucket.entries[mid].address_patch > offset) { - high = mid - 1; - } else { - if (mid == bucket.number_entries - 1 || - bucket.entries[mid + 1].address_patch > offset) { - return {bucket_id, mid}; - } - - low = mid + 1; - } - } - - UNREACHABLE_MSG("Offset could not be found in BKTR block."); -} - RelocationEntry BKTR::GetRelocationEntry(u64 offset) const { const auto res = SearchBucketEntry<false>(offset, relocation, relocation_buckets); return relocation_buckets[res.first].entries[res.second]; diff --git a/src/core/file_sys/nca_patch.h b/src/core/file_sys/nca_patch.h index 8e64e8378..60c544f8e 100644 --- a/src/core/file_sys/nca_patch.h +++ b/src/core/file_sys/nca_patch.h @@ -117,10 +117,6 @@ public: bool Rename(std::string_view name) override; private: - template <bool Subsection, typename BlockType, typename BucketType> - std::pair<std::size_t, std::size_t> SearchBucketEntry(u64 offset, BlockType block, - BucketType buckets) const; - RelocationEntry GetRelocationEntry(u64 offset) const; RelocationEntry GetNextRelocationEntry(u64 offset) const; diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index c228d253e..b9c09b456 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -27,6 +27,7 @@ #include "core/settings.h" namespace FileSys { +namespace { constexpr u64 SINGLE_BYTE_MODULUS = 0x100; constexpr u64 DLC_BASE_TITLE_ID_MASK = 0xFFFFFFFFFFFFE000; @@ -36,19 +37,28 @@ constexpr std::array<const char*, 14> EXEFS_FILE_NAMES{ "subsdk3", "subsdk4", "subsdk5", "subsdk6", "subsdk7", "subsdk8", "subsdk9", }; -std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { +enum class TitleVersionFormat : u8 { + ThreeElements, ///< vX.Y.Z + FourElements, ///< vX.Y.Z.W +}; + +std::string FormatTitleVersion(u32 version, + TitleVersionFormat format = TitleVersionFormat::ThreeElements) { std::array<u8, sizeof(u32)> bytes{}; - bytes[0] = version % SINGLE_BYTE_MODULUS; + bytes[0] = static_cast<u8>(version % SINGLE_BYTE_MODULUS); for (std::size_t i = 1; i < bytes.size(); ++i) { version /= SINGLE_BYTE_MODULUS; - bytes[i] = version % SINGLE_BYTE_MODULUS; + bytes[i] = static_cast<u8>(version % SINGLE_BYTE_MODULUS); } - if (format == TitleVersionFormat::FourElements) + if (format == TitleVersionFormat::FourElements) { return fmt::format("v{}.{}.{}.{}", bytes[3], bytes[2], bytes[1], bytes[0]); + } return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]); } +// Returns a directory with name matching name case-insensitive. Returns nullptr if directory +// doesn't have a directory with name. VirtualDir FindSubdirectoryCaseless(const VirtualDir dir, std::string_view name) { #ifdef _WIN32 return dir->GetSubdirectory(name); @@ -65,6 +75,43 @@ VirtualDir FindSubdirectoryCaseless(const VirtualDir dir, std::string_view name) #endif } +std::optional<std::vector<Core::Memory::CheatEntry>> ReadCheatFileFromFolder( + u64 title_id, const PatchManager::BuildID& build_id_, const VirtualDir& base_path, bool upper) { + const auto build_id_raw = Common::HexToString(build_id_, upper); + const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2); + const auto file = base_path->GetFile(fmt::format("{}.txt", build_id)); + + if (file == nullptr) { + LOG_INFO(Common_Filesystem, "No cheats file found for title_id={:016X}, build_id={}", + title_id, build_id); + return std::nullopt; + } + + std::vector<u8> data(file->GetSize()); + if (file->Read(data.data(), data.size()) != data.size()) { + LOG_INFO(Common_Filesystem, "Failed to read cheats file for title_id={:016X}, build_id={}", + title_id, build_id); + return std::nullopt; + } + + const Core::Memory::TextCheatParser parser; + return parser.Parse(std::string_view(reinterpret_cast<const char*>(data.data()), data.size())); +} + +void AppendCommaIfNotEmpty(std::string& to, std::string_view with) { + if (to.empty()) { + to += with; + } else { + to += ", "; + to += with; + } +} + +bool IsDirValidAndNonEmpty(const VirtualDir& dir) { + return dir != nullptr && (!dir->GetFiles().empty() || !dir->GetSubdirectories().empty()); +} +} // Anonymous namespace + PatchManager::PatchManager(u64 title_id) : title_id(title_id) {} PatchManager::~PatchManager() = default; @@ -245,7 +292,7 @@ std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso, const std::st return out; } -bool PatchManager::HasNSOPatch(const std::array<u8, 32>& build_id_) const { +bool PatchManager::HasNSOPatch(const BuildID& build_id_) const { const auto build_id_raw = Common::HexToString(build_id_); const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); @@ -265,36 +312,8 @@ bool PatchManager::HasNSOPatch(const std::array<u8, 32>& build_id_) const { return !CollectPatches(patch_dirs, build_id).empty(); } -namespace { -std::optional<std::vector<Core::Memory::CheatEntry>> ReadCheatFileFromFolder( - const Core::System& system, u64 title_id, const std::array<u8, 0x20>& build_id_, - const VirtualDir& base_path, bool upper) { - const auto build_id_raw = Common::HexToString(build_id_, upper); - const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2); - const auto file = base_path->GetFile(fmt::format("{}.txt", build_id)); - - if (file == nullptr) { - LOG_INFO(Common_Filesystem, "No cheats file found for title_id={:016X}, build_id={}", - title_id, build_id); - return std::nullopt; - } - - std::vector<u8> data(file->GetSize()); - if (file->Read(data.data(), data.size()) != data.size()) { - LOG_INFO(Common_Filesystem, "Failed to read cheats file for title_id={:016X}, build_id={}", - title_id, build_id); - return std::nullopt; - } - - Core::Memory::TextCheatParser parser; - return parser.Parse(system, - std::string_view(reinterpret_cast<const char*>(data.data()), data.size())); -} - -} // Anonymous namespace - std::vector<Core::Memory::CheatEntry> PatchManager::CreateCheatList( - const Core::System& system, const std::array<u8, 32>& build_id_) const { + const Core::System& system, const BuildID& build_id_) const { const auto load_dir = system.GetFileSystemController().GetModificationLoadRoot(title_id); if (load_dir == nullptr) { LOG_ERROR(Loader, "Cannot load mods for invalid title_id={:016X}", title_id); @@ -314,14 +333,12 @@ std::vector<Core::Memory::CheatEntry> PatchManager::CreateCheatList( auto cheats_dir = FindSubdirectoryCaseless(subdir, "cheats"); if (cheats_dir != nullptr) { - auto res = ReadCheatFileFromFolder(system, title_id, build_id_, cheats_dir, true); - if (res.has_value()) { + if (const auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, true)) { std::copy(res->begin(), res->end(), std::back_inserter(out)); continue; } - res = ReadCheatFileFromFolder(system, title_id, build_id_, cheats_dir, false); - if (res.has_value()) { + if (const auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, false)) { std::copy(res->begin(), res->end(), std::back_inserter(out)); } } @@ -435,21 +452,11 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, Content return romfs; } -static void AppendCommaIfNotEmpty(std::string& to, const std::string& with) { - if (to.empty()) - to += with; - else - to += ", " + with; -} - -static bool IsDirValidAndNonEmpty(const VirtualDir& dir) { - return dir != nullptr && (!dir->GetFiles().empty() || !dir->GetSubdirectories().empty()); -} - -std::map<std::string, std::string, std::less<>> PatchManager::GetPatchVersionNames( - VirtualFile update_raw) const { - if (title_id == 0) +PatchManager::PatchVersionNames PatchManager::GetPatchVersionNames(VirtualFile update_raw) const { + if (title_id == 0) { return {}; + } + std::map<std::string, std::string, std::less<>> out; const auto& installed = Core::System::GetInstance().GetContentProvider(); const auto& disabled = Settings::values.disabled_addons[title_id]; @@ -472,8 +479,7 @@ std::map<std::string, std::string, std::less<>> PatchManager::GetPatchVersionNam if (meta_ver.value_or(0) == 0) { out.insert_or_assign(update_label, ""); } else { - out.insert_or_assign( - update_label, FormatTitleVersion(*meta_ver, TitleVersionFormat::ThreeElements)); + out.insert_or_assign(update_label, FormatTitleVersion(*meta_ver)); } } else if (update_raw != nullptr) { out.insert_or_assign(update_label, "PACKED"); @@ -562,40 +568,46 @@ std::optional<u32> PatchManager::GetGameVersion() const { return installed.GetEntryVersion(title_id); } -std::pair<std::unique_ptr<NACP>, VirtualFile> PatchManager::GetControlMetadata() const { +PatchManager::Metadata PatchManager::GetControlMetadata() const { const auto& installed = Core::System::GetInstance().GetContentProvider(); const auto base_control_nca = installed.GetEntry(title_id, ContentRecordType::Control); - if (base_control_nca == nullptr) + if (base_control_nca == nullptr) { return {}; + } return ParseControlNCA(*base_control_nca); } -std::pair<std::unique_ptr<NACP>, VirtualFile> PatchManager::ParseControlNCA(const NCA& nca) const { +PatchManager::Metadata PatchManager::ParseControlNCA(const NCA& nca) const { const auto base_romfs = nca.GetRomFS(); - if (base_romfs == nullptr) + if (base_romfs == nullptr) { return {}; + } const auto romfs = PatchRomFS(base_romfs, nca.GetBaseIVFCOffset(), ContentRecordType::Control); - if (romfs == nullptr) + if (romfs == nullptr) { return {}; + } const auto extracted = ExtractRomFS(romfs); - if (extracted == nullptr) + if (extracted == nullptr) { return {}; + } auto nacp_file = extracted->GetFile("control.nacp"); - if (nacp_file == nullptr) + if (nacp_file == nullptr) { nacp_file = extracted->GetFile("Control.nacp"); + } auto nacp = nacp_file == nullptr ? nullptr : std::make_unique<NACP>(nacp_file); VirtualFile icon_file; for (const auto& language : FileSys::LANGUAGE_NAMES) { - icon_file = extracted->GetFile("icon_" + std::string(language) + ".dat"); - if (icon_file != nullptr) + icon_file = extracted->GetFile(std::string("icon_").append(language).append(".dat")); + if (icon_file != nullptr) { break; + } } return {std::move(nacp), icon_file}; diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h index 532f4995f..1f28c6241 100644 --- a/src/core/file_sys/patch_manager.h +++ b/src/core/file_sys/patch_manager.h @@ -22,70 +22,62 @@ namespace FileSys { class NCA; class NACP; -enum class TitleVersionFormat : u8 { - ThreeElements, ///< vX.Y.Z - FourElements, ///< vX.Y.Z.W -}; - -std::string FormatTitleVersion(u32 version, - TitleVersionFormat format = TitleVersionFormat::ThreeElements); - -// Returns a directory with name matching name case-insensitive. Returns nullptr if directory -// doesn't have a directory with name. -VirtualDir FindSubdirectoryCaseless(VirtualDir dir, std::string_view name); - // A centralized class to manage patches to games. class PatchManager { public: + using BuildID = std::array<u8, 0x20>; + using Metadata = std::pair<std::unique_ptr<NACP>, VirtualFile>; + using PatchVersionNames = std::map<std::string, std::string, std::less<>>; + explicit PatchManager(u64 title_id); ~PatchManager(); - u64 GetTitleID() const; + [[nodiscard]] u64 GetTitleID() const; // Currently tracked ExeFS patches: // - Game Updates - VirtualDir PatchExeFS(VirtualDir exefs) const; + [[nodiscard]] VirtualDir PatchExeFS(VirtualDir exefs) const; // Currently tracked NSO patches: // - IPS // - IPSwitch - std::vector<u8> PatchNSO(const std::vector<u8>& nso, const std::string& name) const; + [[nodiscard]] std::vector<u8> PatchNSO(const std::vector<u8>& nso, + const std::string& name) const; // Checks to see if PatchNSO() will have any effect given the NSO's build ID. // Used to prevent expensive copies in NSO loader. - bool HasNSOPatch(const std::array<u8, 0x20>& build_id) const; + [[nodiscard]] bool HasNSOPatch(const BuildID& build_id) const; // Creates a CheatList object with all - std::vector<Core::Memory::CheatEntry> CreateCheatList( - const Core::System& system, const std::array<u8, 0x20>& build_id) const; + [[nodiscard]] std::vector<Core::Memory::CheatEntry> CreateCheatList( + const Core::System& system, const BuildID& build_id) const; // Currently tracked RomFS patches: // - Game Updates // - LayeredFS - VirtualFile PatchRomFS(VirtualFile base, u64 ivfc_offset, - ContentRecordType type = ContentRecordType::Program, - VirtualFile update_raw = nullptr) const; + [[nodiscard]] VirtualFile PatchRomFS(VirtualFile base, u64 ivfc_offset, + ContentRecordType type = ContentRecordType::Program, + VirtualFile update_raw = nullptr) const; // Returns a vector of pairs between patch names and patch versions. // i.e. Update 3.2.2 will return {"Update", "3.2.2"} - std::map<std::string, std::string, std::less<>> GetPatchVersionNames( - VirtualFile update_raw = nullptr) const; + [[nodiscard]] PatchVersionNames GetPatchVersionNames(VirtualFile update_raw = nullptr) const; // If the game update exists, returns the u32 version field in its Meta-type NCA. If that fails, // it will fallback to the Meta-type NCA of the base game. If that fails, the result will be // std::nullopt - std::optional<u32> GetGameVersion() const; + [[nodiscard]] std::optional<u32> GetGameVersion() const; // Given title_id of the program, attempts to get the control data of the update and parse // it, falling back to the base control data. - std::pair<std::unique_ptr<NACP>, VirtualFile> GetControlMetadata() const; + [[nodiscard]] Metadata GetControlMetadata() const; // Version of GetControlMetadata that takes an arbitrary NCA - std::pair<std::unique_ptr<NACP>, VirtualFile> ParseControlNCA(const NCA& nca) const; + [[nodiscard]] Metadata ParseControlNCA(const NCA& nca) const; private: - std::vector<VirtualFile> CollectPatches(const std::vector<VirtualDir>& patch_dirs, - const std::string& build_id) const; + [[nodiscard]] std::vector<VirtualFile> CollectPatches(const std::vector<VirtualDir>& patch_dirs, + const std::string& build_id) const; u64 title_id; }; diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp index 418a39a7e..e967a254e 100644 --- a/src/core/file_sys/romfs_factory.cpp +++ b/src/core/file_sys/romfs_factory.cpp @@ -6,7 +6,6 @@ #include "common/assert.h" #include "common/common_types.h" #include "common/logging/log.h" -#include "core/core.h" #include "core/file_sys/card_image.h" #include "core/file_sys/content_archive.h" #include "core/file_sys/nca_metadata.h" @@ -19,7 +18,9 @@ namespace FileSys { -RomFSFactory::RomFSFactory(Loader::AppLoader& app_loader) { +RomFSFactory::RomFSFactory(Loader::AppLoader& app_loader, ContentProvider& provider, + Service::FileSystem::FileSystemController& controller) + : content_provider{provider}, filesystem_controller{controller} { // Load the RomFS from the app if (app_loader.ReadRomFS(file) != Loader::ResultStatus::Success) { LOG_ERROR(Service_FS, "Unable to read RomFS!"); @@ -46,39 +47,38 @@ ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess(u64 current_process_titl ResultVal<VirtualFile> RomFSFactory::Open(u64 title_id, StorageId storage, ContentRecordType type) const { - std::shared_ptr<NCA> res; - - switch (storage) { - case StorageId::None: - res = Core::System::GetInstance().GetContentProvider().GetEntry(title_id, type); - break; - case StorageId::NandSystem: - res = - Core::System::GetInstance().GetFileSystemController().GetSystemNANDContents()->GetEntry( - title_id, type); - break; - case StorageId::NandUser: - res = Core::System::GetInstance().GetFileSystemController().GetUserNANDContents()->GetEntry( - title_id, type); - break; - case StorageId::SdCard: - res = Core::System::GetInstance().GetFileSystemController().GetSDMCContents()->GetEntry( - title_id, type); - break; - default: - UNIMPLEMENTED_MSG("Unimplemented storage_id={:02X}", static_cast<u8>(storage)); - } - + const std::shared_ptr<NCA> res = GetEntry(title_id, storage, type); if (res == nullptr) { // TODO(DarkLordZach): Find the right error code to use here return RESULT_UNKNOWN; } + const auto romfs = res->GetRomFS(); if (romfs == nullptr) { // TODO(DarkLordZach): Find the right error code to use here return RESULT_UNKNOWN; } + return MakeResult<VirtualFile>(romfs); } +std::shared_ptr<NCA> RomFSFactory::GetEntry(u64 title_id, StorageId storage, + ContentRecordType type) const { + switch (storage) { + case StorageId::None: + return content_provider.GetEntry(title_id, type); + case StorageId::NandSystem: + return filesystem_controller.GetSystemNANDContents()->GetEntry(title_id, type); + case StorageId::NandUser: + return filesystem_controller.GetUserNANDContents()->GetEntry(title_id, type); + case StorageId::SdCard: + return filesystem_controller.GetSDMCContents()->GetEntry(title_id, type); + case StorageId::Host: + case StorageId::GameCard: + default: + UNIMPLEMENTED_MSG("Unimplemented storage_id={:02X}", static_cast<u8>(storage)); + return nullptr; + } +} + } // namespace FileSys diff --git a/src/core/file_sys/romfs_factory.h b/src/core/file_sys/romfs_factory.h index c5d40285c..ec704dfa8 100644 --- a/src/core/file_sys/romfs_factory.h +++ b/src/core/file_sys/romfs_factory.h @@ -13,8 +13,15 @@ namespace Loader { class AppLoader; } // namespace Loader +namespace Service::FileSystem { +class FileSystemController; +} + namespace FileSys { +class ContentProvider; +class NCA; + enum class ContentRecordType : u8; enum class StorageId : u8 { @@ -29,18 +36,26 @@ enum class StorageId : u8 { /// File system interface to the RomFS archive class RomFSFactory { public: - explicit RomFSFactory(Loader::AppLoader& app_loader); + explicit RomFSFactory(Loader::AppLoader& app_loader, ContentProvider& provider, + Service::FileSystem::FileSystemController& controller); ~RomFSFactory(); void SetPackedUpdate(VirtualFile update_raw); - ResultVal<VirtualFile> OpenCurrentProcess(u64 current_process_title_id) const; - ResultVal<VirtualFile> Open(u64 title_id, StorageId storage, ContentRecordType type) const; + [[nodiscard]] ResultVal<VirtualFile> OpenCurrentProcess(u64 current_process_title_id) const; + [[nodiscard]] ResultVal<VirtualFile> Open(u64 title_id, StorageId storage, + ContentRecordType type) const; private: + [[nodiscard]] std::shared_ptr<NCA> GetEntry(u64 title_id, StorageId storage, + ContentRecordType type) const; + VirtualFile file; VirtualFile update_raw; bool updatable; u64 ivfc_offset; + + ContentProvider& content_provider; + Service::FileSystem::FileSystemController& filesystem_controller; }; } // namespace FileSys diff --git a/src/core/frontend/applets/controller.cpp b/src/core/frontend/applets/controller.cpp new file mode 100644 index 000000000..4505da758 --- /dev/null +++ b/src/core/frontend/applets/controller.cpp @@ -0,0 +1,81 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "common/logging/log.h" +#include "core/core.h" +#include "core/frontend/applets/controller.h" +#include "core/hle/service/hid/controllers/npad.h" +#include "core/hle/service/hid/hid.h" +#include "core/hle/service/sm/sm.h" + +namespace Core::Frontend { + +ControllerApplet::~ControllerApplet() = default; + +DefaultControllerApplet::~DefaultControllerApplet() = default; + +void DefaultControllerApplet::ReconfigureControllers(std::function<void()> callback, + ControllerParameters parameters) const { + LOG_INFO(Service_HID, "called, deducing the best configuration based on the given parameters!"); + + auto& npad = + Core::System::GetInstance() + .ServiceManager() + .GetService<Service::HID::Hid>("hid") + ->GetAppletResource() + ->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad); + + auto& players = Settings::values.players; + + const std::size_t min_supported_players = + parameters.enable_single_mode ? 1 : parameters.min_players; + + // Disconnect Handheld first. + npad.DisconnectNPadAtIndex(8); + + // Deduce the best configuration based on the input parameters. + for (std::size_t index = 0; index < players.size() - 2; ++index) { + // First, disconnect all controllers regardless of the value of keep_controllers_connected. + // This makes it easy to connect the desired controllers. + npad.DisconnectNPadAtIndex(index); + + // Only connect the minimum number of required players. + if (index >= min_supported_players) { + continue; + } + + // Connect controllers based on the following priority list from highest to lowest priority: + // Pro Controller -> Dual Joycons -> Left Joycon/Right Joycon -> Handheld + if (parameters.allow_pro_controller) { + npad.AddNewControllerAt( + npad.MapSettingsTypeToNPad(Settings::ControllerType::ProController), index); + } else if (parameters.allow_dual_joycons) { + npad.AddNewControllerAt( + npad.MapSettingsTypeToNPad(Settings::ControllerType::DualJoyconDetached), index); + } else if (parameters.allow_left_joycon && parameters.allow_right_joycon) { + // Assign left joycons to even player indices and right joycons to odd player indices. + // We do this since Captain Toad Treasure Tracker expects a left joycon for Player 1 and + // a right Joycon for Player 2 in 2 Player Assist mode. + if (index % 2 == 0) { + npad.AddNewControllerAt( + npad.MapSettingsTypeToNPad(Settings::ControllerType::LeftJoycon), index); + } else { + npad.AddNewControllerAt( + npad.MapSettingsTypeToNPad(Settings::ControllerType::RightJoycon), index); + } + } else if (index == 0 && parameters.enable_single_mode && parameters.allow_handheld && + !Settings::values.use_docked_mode) { + // We should *never* reach here under any normal circumstances. + npad.AddNewControllerAt(npad.MapSettingsTypeToNPad(Settings::ControllerType::Handheld), + index); + } else { + UNREACHABLE_MSG("Unable to add a new controller based on the given parameters!"); + } + } + + callback(); +} + +} // namespace Core::Frontend diff --git a/src/core/frontend/applets/controller.h b/src/core/frontend/applets/controller.h new file mode 100644 index 000000000..a227f15cd --- /dev/null +++ b/src/core/frontend/applets/controller.h @@ -0,0 +1,48 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <functional> + +#include "common/common_types.h" + +namespace Core::Frontend { + +using BorderColor = std::array<u8, 4>; +using ExplainText = std::array<char, 0x81>; + +struct ControllerParameters { + s8 min_players{}; + s8 max_players{}; + bool keep_controllers_connected{}; + bool enable_single_mode{}; + bool enable_border_color{}; + std::vector<BorderColor> border_colors{}; + bool enable_explain_text{}; + std::vector<ExplainText> explain_text{}; + bool allow_pro_controller{}; + bool allow_handheld{}; + bool allow_dual_joycons{}; + bool allow_left_joycon{}; + bool allow_right_joycon{}; +}; + +class ControllerApplet { +public: + virtual ~ControllerApplet(); + + virtual void ReconfigureControllers(std::function<void()> callback, + ControllerParameters parameters) const = 0; +}; + +class DefaultControllerApplet final : public ControllerApplet { +public: + ~DefaultControllerApplet() override; + + void ReconfigureControllers(std::function<void()> callback, + ControllerParameters parameters) const override; +}; + +} // namespace Core::Frontend diff --git a/src/core/frontend/input.h b/src/core/frontend/input.h index 2b098b7c6..9da0d2829 100644 --- a/src/core/frontend/input.h +++ b/src/core/frontend/input.h @@ -119,11 +119,11 @@ using ButtonDevice = InputDevice<bool>; using AnalogDevice = InputDevice<std::tuple<float, float>>; /** - * A motion device is an input device that returns a tuple of accelerometer state vector and - * gyroscope state vector. + * A motion status is an object that returns a tuple of accelerometer state vector, + * gyroscope state vector, rotation state vector and orientation state matrix. * * For both vectors: - * x+ is the same direction as LEFT on D-pad. + * x+ is the same direction as RIGHT on D-pad. * y+ is normal to the touch screen, pointing outward. * z+ is the same direction as UP on D-pad. * @@ -133,8 +133,22 @@ using AnalogDevice = InputDevice<std::tuple<float, float>>; * For gyroscope state vector: * Orientation is determined by right-hand rule. * Units: deg/sec + * + * For rotation state vector + * Units: rotations + * + * For orientation state matrix + * x vector + * y vector + * z vector + */ +using MotionStatus = std::tuple<Common::Vec3<float>, Common::Vec3<float>, Common::Vec3<float>, + std::array<Common::Vec3f, 3>>; + +/** + * A motion device is an input device that returns a motion status object */ -using MotionDevice = InputDevice<std::tuple<Common::Vec3<float>, Common::Vec3<float>>>; +using MotionDevice = InputDevice<MotionStatus>; /** * A touch device is an input device that returns a tuple of two floats and a bool. The floats are diff --git a/src/core/hle/kernel/client_session.cpp b/src/core/hle/kernel/client_session.cpp index 5ab204b9b..be9eba519 100644 --- a/src/core/hle/kernel/client_session.cpp +++ b/src/core/hle/kernel/client_session.cpp @@ -48,14 +48,15 @@ ResultVal<std::shared_ptr<ClientSession>> ClientSession::Create(KernelCore& kern } ResultCode ClientSession::SendSyncRequest(std::shared_ptr<Thread> thread, - Core::Memory::Memory& memory) { + Core::Memory::Memory& memory, + Core::Timing::CoreTiming& core_timing) { // Keep ServerSession alive until we're done working with it. if (!parent->Server()) { return ERR_SESSION_CLOSED_BY_REMOTE; } // Signal the server session that new data is available - return parent->Server()->HandleSyncRequest(std::move(thread), memory); + return parent->Server()->HandleSyncRequest(std::move(thread), memory, core_timing); } } // namespace Kernel diff --git a/src/core/hle/kernel/client_session.h b/src/core/hle/kernel/client_session.h index c5f760d7d..e5e0690c2 100644 --- a/src/core/hle/kernel/client_session.h +++ b/src/core/hle/kernel/client_session.h @@ -16,6 +16,10 @@ namespace Core::Memory { class Memory; } +namespace Core::Timing { +class CoreTiming; +} + namespace Kernel { class KernelCore; @@ -42,7 +46,8 @@ public: return HANDLE_TYPE; } - ResultCode SendSyncRequest(std::shared_ptr<Thread> thread, Core::Memory::Memory& memory); + ResultCode SendSyncRequest(std::shared_ptr<Thread> thread, Core::Memory::Memory& memory, + Core::Timing::CoreTiming& core_timing); bool ShouldWait(const Thread* thread) const override; diff --git a/src/core/hle/kernel/scheduler.h b/src/core/hle/kernel/scheduler.h index 36e3c26fb..b6f04dcea 100644 --- a/src/core/hle/kernel/scheduler.h +++ b/src/core/hle/kernel/scheduler.h @@ -188,7 +188,7 @@ private: /// Scheduler lock mechanisms. bool is_locked{}; - Common::SpinLock inner_lock{}; + std::mutex inner_lock; std::atomic<s64> scope_lock{}; Core::EmuThreadHandle current_owner{Core::EmuThreadHandle::InvalidHandle()}; diff --git a/src/core/hle/kernel/server_session.cpp b/src/core/hle/kernel/server_session.cpp index 7e6391c6c..8c19f2534 100644 --- a/src/core/hle/kernel/server_session.cpp +++ b/src/core/hle/kernel/server_session.cpp @@ -8,7 +8,6 @@ #include "common/assert.h" #include "common/common_types.h" #include "common/logging/log.h" -#include "core/core.h" #include "core/core_timing.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/client_port.h" @@ -185,10 +184,11 @@ ResultCode ServerSession::CompleteSyncRequest() { } ResultCode ServerSession::HandleSyncRequest(std::shared_ptr<Thread> thread, - Core::Memory::Memory& memory) { + Core::Memory::Memory& memory, + Core::Timing::CoreTiming& core_timing) { const ResultCode result = QueueSyncRequest(std::move(thread), memory); const auto delay = std::chrono::nanoseconds{kernel.IsMulticore() ? 0 : 20000}; - Core::System::GetInstance().CoreTiming().ScheduleEvent(delay, request_event, {}); + core_timing.ScheduleEvent(delay, request_event, {}); return result; } diff --git a/src/core/hle/kernel/server_session.h b/src/core/hle/kernel/server_session.h index 403aaf10b..d23e9ec68 100644 --- a/src/core/hle/kernel/server_session.h +++ b/src/core/hle/kernel/server_session.h @@ -18,8 +18,9 @@ class Memory; } namespace Core::Timing { +class CoreTiming; struct EventType; -} +} // namespace Core::Timing namespace Kernel { @@ -87,12 +88,14 @@ public: /** * Handle a sync request from the emulated application. * - * @param thread Thread that initiated the request. - * @param memory Memory context to handle the sync request under. + * @param thread Thread that initiated the request. + * @param memory Memory context to handle the sync request under. + * @param core_timing Core timing context to schedule the request event under. * * @returns ResultCode from the operation. */ - ResultCode HandleSyncRequest(std::shared_ptr<Thread> thread, Core::Memory::Memory& memory); + ResultCode HandleSyncRequest(std::shared_ptr<Thread> thread, Core::Memory::Memory& memory, + Core::Timing::CoreTiming& core_timing); bool ShouldWait(const Thread* thread) const override; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 01ae57053..bafd1ced7 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -346,7 +346,7 @@ static ResultCode SendSyncRequest(Core::System& system, Handle handle) { SchedulerLock lock(system.Kernel()); thread->InvalidateHLECallback(); thread->SetStatus(ThreadStatus::WaitIPC); - session->SendSyncRequest(SharedFrom(thread), system.Memory()); + session->SendSyncRequest(SharedFrom(thread), system.Memory(), system.CoreTiming()); } if (thread->HasHLECallback()) { diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 7d92b25a3..d7a81f64a 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -1192,7 +1192,7 @@ IApplicationFunctions::IApplicationFunctions(Core::System& system_) {120, nullptr, "ExecuteProgram"}, {121, nullptr, "ClearUserChannel"}, {122, nullptr, "UnpopToUserChannel"}, - {123, nullptr, "GetPreviousProgramIndex"}, + {123, &IApplicationFunctions::GetPreviousProgramIndex, "GetPreviousProgramIndex"}, {124, nullptr, "EnableApplicationAllThreadDumpOnCrash"}, {130, &IApplicationFunctions::GetGpuErrorDetectedSystemEvent, "GetGpuErrorDetectedSystemEvent"}, {140, &IApplicationFunctions::GetFriendInvitationStorageChannelEvent, "GetFriendInvitationStorageChannelEvent"}, @@ -1554,6 +1554,14 @@ void IApplicationFunctions::QueryApplicationPlayStatisticsByUid(Kernel::HLEReque rb.Push<u32>(0); } +void IApplicationFunctions::GetPreviousProgramIndex(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_AM, "(STUBBED) called"); + + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(RESULT_SUCCESS); + rb.Push<s32>(previous_program_index); +} + void IApplicationFunctions::GetGpuErrorDetectedSystemEvent(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_AM, "(STUBBED) called"); diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 6e69796ec..bcc06affe 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -288,11 +288,13 @@ private: void SetApplicationCopyrightVisibility(Kernel::HLERequestContext& ctx); void QueryApplicationPlayStatistics(Kernel::HLERequestContext& ctx); void QueryApplicationPlayStatisticsByUid(Kernel::HLERequestContext& ctx); + void GetPreviousProgramIndex(Kernel::HLERequestContext& ctx); void GetGpuErrorDetectedSystemEvent(Kernel::HLERequestContext& ctx); void GetFriendInvitationStorageChannelEvent(Kernel::HLERequestContext& ctx); bool launch_popped_application_specific = false; bool launch_popped_account_preselect = false; + s32 previous_program_index{-1}; Kernel::EventPair gpu_error_detected_event; Kernel::EventPair friend_invitation_storage_channel_event; Core::System& system; diff --git a/src/core/hle/service/am/applets/applets.cpp b/src/core/hle/service/am/applets/applets.cpp index c3261f3e6..4e0800f9a 100644 --- a/src/core/hle/service/am/applets/applets.cpp +++ b/src/core/hle/service/am/applets/applets.cpp @@ -5,6 +5,7 @@ #include <cstring> #include "common/assert.h" #include "core/core.h" +#include "core/frontend/applets/controller.h" #include "core/frontend/applets/error.h" #include "core/frontend/applets/general_frontend.h" #include "core/frontend/applets/profile_select.h" @@ -15,6 +16,7 @@ #include "core/hle/kernel/writable_event.h" #include "core/hle/service/am/am.h" #include "core/hle/service/am/applets/applets.h" +#include "core/hle/service/am/applets/controller.h" #include "core/hle/service/am/applets/error.h" #include "core/hle/service/am/applets/general_backend.h" #include "core/hle/service/am/applets/profile_select.h" @@ -140,14 +142,14 @@ void Applet::Initialize() { AppletFrontendSet::AppletFrontendSet() = default; -AppletFrontendSet::AppletFrontendSet(ParentalControlsApplet parental_controls, ErrorApplet error, +AppletFrontendSet::AppletFrontendSet(ControllerApplet controller, ECommerceApplet e_commerce, + ErrorApplet error, ParentalControlsApplet parental_controls, PhotoViewer photo_viewer, ProfileSelect profile_select, - SoftwareKeyboard software_keyboard, WebBrowser web_browser, - ECommerceApplet e_commerce) - : parental_controls{std::move(parental_controls)}, error{std::move(error)}, - photo_viewer{std::move(photo_viewer)}, profile_select{std::move(profile_select)}, - software_keyboard{std::move(software_keyboard)}, web_browser{std::move(web_browser)}, - e_commerce{std::move(e_commerce)} {} + SoftwareKeyboard software_keyboard, WebBrowser web_browser) + : controller{std::move(controller)}, e_commerce{std::move(e_commerce)}, error{std::move(error)}, + parental_controls{std::move(parental_controls)}, photo_viewer{std::move(photo_viewer)}, + profile_select{std::move(profile_select)}, software_keyboard{std::move(software_keyboard)}, + web_browser{std::move(web_browser)} {} AppletFrontendSet::~AppletFrontendSet() = default; @@ -164,20 +166,37 @@ const AppletFrontendSet& AppletManager::GetAppletFrontendSet() const { } void AppletManager::SetAppletFrontendSet(AppletFrontendSet set) { - if (set.parental_controls != nullptr) - frontend.parental_controls = std::move(set.parental_controls); - if (set.error != nullptr) + if (set.controller != nullptr) { + frontend.controller = std::move(set.controller); + } + + if (set.e_commerce != nullptr) { + frontend.e_commerce = std::move(set.e_commerce); + } + + if (set.error != nullptr) { frontend.error = std::move(set.error); - if (set.photo_viewer != nullptr) + } + + if (set.parental_controls != nullptr) { + frontend.parental_controls = std::move(set.parental_controls); + } + + if (set.photo_viewer != nullptr) { frontend.photo_viewer = std::move(set.photo_viewer); - if (set.profile_select != nullptr) + } + + if (set.profile_select != nullptr) { frontend.profile_select = std::move(set.profile_select); - if (set.software_keyboard != nullptr) + } + + if (set.software_keyboard != nullptr) { frontend.software_keyboard = std::move(set.software_keyboard); - if (set.web_browser != nullptr) + } + + if (set.web_browser != nullptr) { frontend.web_browser = std::move(set.web_browser); - if (set.e_commerce != nullptr) - frontend.e_commerce = std::move(set.e_commerce); + } } void AppletManager::SetDefaultAppletFrontendSet() { @@ -186,15 +205,23 @@ void AppletManager::SetDefaultAppletFrontendSet() { } void AppletManager::SetDefaultAppletsIfMissing() { - if (frontend.parental_controls == nullptr) { - frontend.parental_controls = - std::make_unique<Core::Frontend::DefaultParentalControlsApplet>(); + if (frontend.controller == nullptr) { + frontend.controller = std::make_unique<Core::Frontend::DefaultControllerApplet>(); + } + + if (frontend.e_commerce == nullptr) { + frontend.e_commerce = std::make_unique<Core::Frontend::DefaultECommerceApplet>(); } if (frontend.error == nullptr) { frontend.error = std::make_unique<Core::Frontend::DefaultErrorApplet>(); } + if (frontend.parental_controls == nullptr) { + frontend.parental_controls = + std::make_unique<Core::Frontend::DefaultParentalControlsApplet>(); + } + if (frontend.photo_viewer == nullptr) { frontend.photo_viewer = std::make_unique<Core::Frontend::DefaultPhotoViewerApplet>(); } @@ -211,10 +238,6 @@ void AppletManager::SetDefaultAppletsIfMissing() { if (frontend.web_browser == nullptr) { frontend.web_browser = std::make_unique<Core::Frontend::DefaultWebBrowserApplet>(); } - - if (frontend.e_commerce == nullptr) { - frontend.e_commerce = std::make_unique<Core::Frontend::DefaultECommerceApplet>(); - } } void AppletManager::ClearAll() { @@ -225,6 +248,8 @@ std::shared_ptr<Applet> AppletManager::GetApplet(AppletId id) const { switch (id) { case AppletId::Auth: return std::make_shared<Auth>(system, *frontend.parental_controls); + case AppletId::Controller: + return std::make_shared<Controller>(system, *frontend.controller); case AppletId::Error: return std::make_shared<Error>(system, *frontend.error); case AppletId::ProfileSelect: diff --git a/src/core/hle/service/am/applets/applets.h b/src/core/hle/service/am/applets/applets.h index e75be86a2..a1f4cf897 100644 --- a/src/core/hle/service/am/applets/applets.h +++ b/src/core/hle/service/am/applets/applets.h @@ -17,6 +17,7 @@ class System; } namespace Core::Frontend { +class ControllerApplet; class ECommerceApplet; class ErrorApplet; class ParentalControlsApplet; @@ -155,19 +156,20 @@ protected: }; struct AppletFrontendSet { - using ParentalControlsApplet = std::unique_ptr<Core::Frontend::ParentalControlsApplet>; + using ControllerApplet = std::unique_ptr<Core::Frontend::ControllerApplet>; + using ECommerceApplet = std::unique_ptr<Core::Frontend::ECommerceApplet>; using ErrorApplet = std::unique_ptr<Core::Frontend::ErrorApplet>; + using ParentalControlsApplet = std::unique_ptr<Core::Frontend::ParentalControlsApplet>; using PhotoViewer = std::unique_ptr<Core::Frontend::PhotoViewerApplet>; using ProfileSelect = std::unique_ptr<Core::Frontend::ProfileSelectApplet>; using SoftwareKeyboard = std::unique_ptr<Core::Frontend::SoftwareKeyboardApplet>; using WebBrowser = std::unique_ptr<Core::Frontend::WebBrowserApplet>; - using ECommerceApplet = std::unique_ptr<Core::Frontend::ECommerceApplet>; AppletFrontendSet(); - AppletFrontendSet(ParentalControlsApplet parental_controls, ErrorApplet error, - PhotoViewer photo_viewer, ProfileSelect profile_select, - SoftwareKeyboard software_keyboard, WebBrowser web_browser, - ECommerceApplet e_commerce); + AppletFrontendSet(ControllerApplet controller, ECommerceApplet e_commerce, ErrorApplet error, + ParentalControlsApplet parental_controls, PhotoViewer photo_viewer, + ProfileSelect profile_select, SoftwareKeyboard software_keyboard, + WebBrowser web_browser); ~AppletFrontendSet(); AppletFrontendSet(const AppletFrontendSet&) = delete; @@ -176,13 +178,14 @@ struct AppletFrontendSet { AppletFrontendSet(AppletFrontendSet&&) noexcept; AppletFrontendSet& operator=(AppletFrontendSet&&) noexcept; - ParentalControlsApplet parental_controls; + ControllerApplet controller; + ECommerceApplet e_commerce; ErrorApplet error; + ParentalControlsApplet parental_controls; PhotoViewer photo_viewer; ProfileSelect profile_select; SoftwareKeyboard software_keyboard; WebBrowser web_browser; - ECommerceApplet e_commerce; }; class AppletManager { diff --git a/src/core/hle/service/am/applets/controller.cpp b/src/core/hle/service/am/applets/controller.cpp new file mode 100644 index 000000000..2151da783 --- /dev/null +++ b/src/core/hle/service/am/applets/controller.cpp @@ -0,0 +1,210 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <algorithm> +#include <cstring> + +#include "common/assert.h" +#include "common/logging/log.h" +#include "common/string_util.h" +#include "core/core.h" +#include "core/frontend/applets/controller.h" +#include "core/hle/result.h" +#include "core/hle/service/am/am.h" +#include "core/hle/service/am/applets/controller.h" +#include "core/hle/service/hid/controllers/npad.h" + +namespace Service::AM::Applets { + +// This error code (0x183ACA) is thrown when the applet fails to initialize. +[[maybe_unused]] constexpr ResultCode ERR_CONTROLLER_APPLET_3101{ErrorModule::HID, 3101}; +// This error code (0x183CCA) is thrown when the u32 result in ControllerSupportResultInfo is 2. +[[maybe_unused]] constexpr ResultCode ERR_CONTROLLER_APPLET_3102{ErrorModule::HID, 3102}; + +static Core::Frontend::ControllerParameters ConvertToFrontendParameters( + ControllerSupportArgPrivate private_arg, ControllerSupportArgHeader header, bool enable_text, + std::vector<IdentificationColor> identification_colors, std::vector<ExplainText> text) { + HID::Controller_NPad::NPadType npad_style_set; + npad_style_set.raw = private_arg.style_set; + + return { + .min_players = std::max(s8(1), header.player_count_min), + .max_players = header.player_count_max, + .keep_controllers_connected = header.enable_take_over_connection, + .enable_single_mode = header.enable_single_mode, + .enable_border_color = header.enable_identification_color, + .border_colors = identification_colors, + .enable_explain_text = enable_text, + .explain_text = text, + .allow_pro_controller = npad_style_set.pro_controller == 1, + .allow_handheld = npad_style_set.handheld == 1, + .allow_dual_joycons = npad_style_set.joycon_dual == 1, + .allow_left_joycon = npad_style_set.joycon_left == 1, + .allow_right_joycon = npad_style_set.joycon_right == 1, + }; +} + +Controller::Controller(Core::System& system_, const Core::Frontend::ControllerApplet& frontend_) + : Applet{system_.Kernel()}, frontend(frontend_) {} + +Controller::~Controller() = default; + +void Controller::Initialize() { + Applet::Initialize(); + + LOG_INFO(Service_HID, "Initializing Controller Applet."); + + LOG_DEBUG(Service_HID, + "Initializing Applet with common_args: arg_version={}, lib_version={}, " + "play_startup_sound={}, size={}, system_tick={}, theme_color={}", + common_args.arguments_version, common_args.library_version, + common_args.play_startup_sound, common_args.size, common_args.system_tick, + common_args.theme_color); + + library_applet_version = LibraryAppletVersion{common_args.library_version}; + + const auto private_arg_storage = broker.PopNormalDataToApplet(); + ASSERT(private_arg_storage != nullptr); + + const auto& private_arg = private_arg_storage->GetData(); + ASSERT(private_arg.size() == sizeof(ControllerSupportArgPrivate)); + + std::memcpy(&controller_private_arg, private_arg.data(), sizeof(ControllerSupportArgPrivate)); + ASSERT_MSG(controller_private_arg.arg_private_size == sizeof(ControllerSupportArgPrivate), + "Unknown ControllerSupportArgPrivate revision={} with size={}", + library_applet_version, controller_private_arg.arg_private_size); + + switch (controller_private_arg.mode) { + case ControllerSupportMode::ShowControllerSupport: { + const auto user_arg_storage = broker.PopNormalDataToApplet(); + ASSERT(user_arg_storage != nullptr); + + const auto& user_arg = user_arg_storage->GetData(); + switch (library_applet_version) { + case LibraryAppletVersion::Version3: + case LibraryAppletVersion::Version4: + case LibraryAppletVersion::Version5: + ASSERT(user_arg.size() == sizeof(ControllerSupportArgOld)); + std::memcpy(&controller_user_arg_old, user_arg.data(), sizeof(ControllerSupportArgOld)); + break; + case LibraryAppletVersion::Version7: + ASSERT(user_arg.size() == sizeof(ControllerSupportArgNew)); + std::memcpy(&controller_user_arg_new, user_arg.data(), sizeof(ControllerSupportArgNew)); + break; + default: + UNIMPLEMENTED_MSG("Unknown ControllerSupportArg revision={} with size={}", + library_applet_version, controller_private_arg.arg_size); + ASSERT(user_arg.size() >= sizeof(ControllerSupportArgNew)); + std::memcpy(&controller_user_arg_new, user_arg.data(), sizeof(ControllerSupportArgNew)); + break; + } + break; + } + case ControllerSupportMode::ShowControllerStrapGuide: + case ControllerSupportMode::ShowControllerFirmwareUpdate: + default: { + UNIMPLEMENTED_MSG("Unimplemented ControllerSupportMode={}", controller_private_arg.mode); + break; + } + } +} + +bool Controller::TransactionComplete() const { + return complete; +} + +ResultCode Controller::GetStatus() const { + return status; +} + +void Controller::ExecuteInteractive() { + UNREACHABLE_MSG("Attempted to call interactive execution on non-interactive applet."); +} + +void Controller::Execute() { + switch (controller_private_arg.mode) { + case ControllerSupportMode::ShowControllerSupport: { + const auto parameters = [this] { + switch (library_applet_version) { + case LibraryAppletVersion::Version3: + case LibraryAppletVersion::Version4: + case LibraryAppletVersion::Version5: + return ConvertToFrontendParameters( + controller_private_arg, controller_user_arg_old.header, + controller_user_arg_old.enable_explain_text, + std::vector<IdentificationColor>( + controller_user_arg_old.identification_colors.begin(), + controller_user_arg_old.identification_colors.end()), + std::vector<ExplainText>(controller_user_arg_old.explain_text.begin(), + controller_user_arg_old.explain_text.end())); + case LibraryAppletVersion::Version7: + default: + return ConvertToFrontendParameters( + controller_private_arg, controller_user_arg_new.header, + controller_user_arg_new.enable_explain_text, + std::vector<IdentificationColor>( + controller_user_arg_new.identification_colors.begin(), + controller_user_arg_new.identification_colors.end()), + std::vector<ExplainText>(controller_user_arg_new.explain_text.begin(), + controller_user_arg_new.explain_text.end())); + } + }(); + + is_single_mode = parameters.enable_single_mode; + + LOG_DEBUG(Service_HID, + "Controller Parameters: min_players={}, max_players={}, " + "keep_controllers_connected={}, enable_single_mode={}, enable_border_color={}, " + "enable_explain_text={}, allow_pro_controller={}, allow_handheld={}, " + "allow_dual_joycons={}, allow_left_joycon={}, allow_right_joycon={}", + parameters.min_players, parameters.max_players, + parameters.keep_controllers_connected, parameters.enable_single_mode, + parameters.enable_border_color, parameters.enable_explain_text, + parameters.allow_pro_controller, parameters.allow_handheld, + parameters.allow_dual_joycons, parameters.allow_left_joycon, + parameters.allow_right_joycon); + + frontend.ReconfigureControllers([this] { ConfigurationComplete(); }, parameters); + break; + } + case ControllerSupportMode::ShowControllerStrapGuide: + case ControllerSupportMode::ShowControllerFirmwareUpdate: + default: { + ConfigurationComplete(); + break; + } + } +} + +void Controller::ConfigurationComplete() { + ControllerSupportResultInfo result_info{}; + + const auto& players = Settings::values.players; + + // If enable_single_mode is enabled, player_count is 1 regardless of any other parameters. + // Otherwise, only count connected players from P1-P8. + result_info.player_count = + is_single_mode ? 1 + : static_cast<s8>(std::count_if( + players.begin(), players.end() - 2, + [](Settings::PlayerInput player) { return player.connected; })); + + result_info.selected_id = HID::Controller_NPad::IndexToNPad( + std::distance(players.begin(), + std::find_if(players.begin(), players.end(), + [](Settings::PlayerInput player) { return player.connected; }))); + + result_info.result = 0; + + LOG_DEBUG(Service_HID, "Result Info: player_count={}, selected_id={}, result={}", + result_info.player_count, result_info.selected_id, result_info.result); + + complete = true; + out_data = std::vector<u8>(sizeof(ControllerSupportResultInfo)); + std::memcpy(out_data.data(), &result_info, out_data.size()); + broker.PushNormalDataFromApplet(std::make_shared<IStorage>(std::move(out_data))); + broker.SignalStateChanged(); +} + +} // namespace Service::AM::Applets diff --git a/src/core/hle/service/am/applets/controller.h b/src/core/hle/service/am/applets/controller.h new file mode 100644 index 000000000..f7bb3fba9 --- /dev/null +++ b/src/core/hle/service/am/applets/controller.h @@ -0,0 +1,123 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <array> +#include <vector> + +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "core/hle/result.h" +#include "core/hle/service/am/applets/applets.h" + +namespace Core { +class System; +} + +namespace Service::AM::Applets { + +using IdentificationColor = std::array<u8, 4>; +using ExplainText = std::array<char, 0x81>; + +enum class LibraryAppletVersion : u32_le { + Version3 = 0x3, // 1.0.0 - 2.3.0 + Version4 = 0x4, // 3.0.0 - 5.1.0 + Version5 = 0x5, // 6.0.0 - 7.0.1 + Version7 = 0x7, // 8.0.0+ +}; + +enum class ControllerSupportMode : u8 { + ShowControllerSupport = 0, + ShowControllerStrapGuide = 1, + ShowControllerFirmwareUpdate = 2, +}; + +enum class ControllerSupportCaller : u8 { + Application = 0, + System = 1, +}; + +struct ControllerSupportArgPrivate { + u32 arg_private_size{}; + u32 arg_size{}; + bool flag_0{}; + bool flag_1{}; + ControllerSupportMode mode{}; + ControllerSupportCaller caller{}; + u32 style_set{}; + u32 joy_hold_type{}; +}; +static_assert(sizeof(ControllerSupportArgPrivate) == 0x14, + "ControllerSupportArgPrivate has incorrect size."); + +struct ControllerSupportArgHeader { + s8 player_count_min{}; + s8 player_count_max{}; + bool enable_take_over_connection{}; + bool enable_left_justify{}; + bool enable_permit_joy_dual{}; + bool enable_single_mode{}; + bool enable_identification_color{}; +}; +static_assert(sizeof(ControllerSupportArgHeader) == 0x7, + "ControllerSupportArgHeader has incorrect size."); + +// LibraryAppletVersion 0x3, 0x4, 0x5 +struct ControllerSupportArgOld { + ControllerSupportArgHeader header{}; + std::array<IdentificationColor, 4> identification_colors{}; + bool enable_explain_text{}; + std::array<ExplainText, 4> explain_text{}; +}; +static_assert(sizeof(ControllerSupportArgOld) == 0x21C, + "ControllerSupportArgOld has incorrect size."); + +// LibraryAppletVersion 0x7 +struct ControllerSupportArgNew { + ControllerSupportArgHeader header{}; + std::array<IdentificationColor, 8> identification_colors{}; + bool enable_explain_text{}; + std::array<ExplainText, 8> explain_text{}; +}; +static_assert(sizeof(ControllerSupportArgNew) == 0x430, + "ControllerSupportArgNew has incorrect size."); + +struct ControllerSupportResultInfo { + s8 player_count{}; + INSERT_PADDING_BYTES(3); + u32 selected_id{}; + u32 result{}; +}; +static_assert(sizeof(ControllerSupportResultInfo) == 0xC, + "ControllerSupportResultInfo has incorrect size."); + +class Controller final : public Applet { +public: + explicit Controller(Core::System& system_, const Core::Frontend::ControllerApplet& frontend_); + ~Controller() override; + + void Initialize() override; + + bool TransactionComplete() const override; + ResultCode GetStatus() const override; + void ExecuteInteractive() override; + void Execute() override; + + void ConfigurationComplete(); + +private: + const Core::Frontend::ControllerApplet& frontend; + + LibraryAppletVersion library_applet_version; + ControllerSupportArgPrivate controller_private_arg; + ControllerSupportArgOld controller_user_arg_old; + ControllerSupportArgNew controller_user_arg_new; + bool complete{false}; + ResultCode status{RESULT_SUCCESS}; + bool is_single_mode{false}; + std::vector<u8> out_data; +}; + +} // namespace Service::AM::Applets diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp index d8359abaa..a2d3ded7b 100644 --- a/src/core/hle/service/audio/audren_u.cpp +++ b/src/core/hle/service/audio/audren_u.cpp @@ -26,7 +26,7 @@ namespace Service::Audio { class IAudioRenderer final : public ServiceFramework<IAudioRenderer> { public: - explicit IAudioRenderer(Core::System& system, AudioCore::AudioRendererParameter audren_params, + explicit IAudioRenderer(Core::System& system, AudioCommon::AudioRendererParameter audren_params, const std::size_t instance_number) : ServiceFramework("IAudioRenderer") { // clang-format off @@ -94,14 +94,15 @@ private: void RequestUpdateImpl(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Audio, "(STUBBED) called"); - auto result = renderer->UpdateAudioRenderer(ctx.ReadBuffer()); + std::vector<u8> output_params(ctx.GetWriteBufferSize()); + auto result = renderer->UpdateAudioRenderer(ctx.ReadBuffer(), output_params); - if (result.Succeeded()) { - ctx.WriteBuffer(result.Unwrap()); + if (result.IsSuccess()) { + ctx.WriteBuffer(output_params); } IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(result.Code()); + rb.Push(result); } void Start(Kernel::HLERequestContext& ctx) { @@ -346,7 +347,7 @@ void AudRenU::OpenAudioRenderer(Kernel::HLERequestContext& ctx) { OpenAudioRendererImpl(ctx); } -static u64 CalculateNumPerformanceEntries(const AudioCore::AudioRendererParameter& params) { +static u64 CalculateNumPerformanceEntries(const AudioCommon::AudioRendererParameter& params) { // +1 represents the final mix. return u64{params.effect_count} + params.submix_count + params.sink_count + params.voice_count + 1; @@ -375,7 +376,7 @@ void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { constexpr u64 upsampler_manager_size = 0x48; // Calculates the part of the size that relates to mix buffers. - const auto calculate_mix_buffer_sizes = [](const AudioCore::AudioRendererParameter& params) { + const auto calculate_mix_buffer_sizes = [](const AudioCommon::AudioRendererParameter& params) { // As of 8.0.0 this is the maximum on voice channels. constexpr u64 max_voice_channels = 6; @@ -397,7 +398,7 @@ void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { }; // Calculates the portion of the size related to the mix data (and the sorting thereof). - const auto calculate_mix_info_size = [](const AudioCore::AudioRendererParameter& params) { + const auto calculate_mix_info_size = [](const AudioCommon::AudioRendererParameter& params) { // The size of the mixing info data structure. constexpr u64 mix_info_size = 0x940; @@ -447,7 +448,7 @@ void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { }; // Calculates the part of the size related to voice channel info. - const auto calculate_voice_info_size = [](const AudioCore::AudioRendererParameter& params) { + const auto calculate_voice_info_size = [](const AudioCommon::AudioRendererParameter& params) { constexpr u64 voice_info_size = 0x220; constexpr u64 voice_resource_size = 0xD0; @@ -461,7 +462,7 @@ void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { }; // Calculates the part of the size related to memory pools. - const auto calculate_memory_pools_size = [](const AudioCore::AudioRendererParameter& params) { + const auto calculate_memory_pools_size = [](const AudioCommon::AudioRendererParameter& params) { const u64 num_memory_pools = sizeof(s32) * (u64{params.effect_count} + params.voice_count); const u64 memory_pool_info_size = 0x20; return Common::AlignUp(num_memory_pools * memory_pool_info_size, info_field_alignment_size); @@ -469,7 +470,7 @@ void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { // Calculates the part of the size related to the splitter context. const auto calculate_splitter_context_size = - [](const AudioCore::AudioRendererParameter& params) -> u64 { + [](const AudioCommon::AudioRendererParameter& params) -> u64 { if (!IsFeatureSupported(AudioFeatures::Splitter, params.revision)) { return 0; } @@ -488,27 +489,29 @@ void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { }; // Calculates the part of the size related to the upsampler info. - const auto calculate_upsampler_info_size = [](const AudioCore::AudioRendererParameter& params) { - constexpr u64 upsampler_info_size = 0x280; - // Yes, using the buffer size over info alignment size is intentional here. - return Common::AlignUp(upsampler_info_size * (u64{params.submix_count} + params.sink_count), - buffer_alignment_size); - }; + const auto calculate_upsampler_info_size = + [](const AudioCommon::AudioRendererParameter& params) { + constexpr u64 upsampler_info_size = 0x280; + // Yes, using the buffer size over info alignment size is intentional here. + return Common::AlignUp(upsampler_info_size * + (u64{params.submix_count} + params.sink_count), + buffer_alignment_size); + }; // Calculates the part of the size related to effect info. - const auto calculate_effect_info_size = [](const AudioCore::AudioRendererParameter& params) { + const auto calculate_effect_info_size = [](const AudioCommon::AudioRendererParameter& params) { constexpr u64 effect_info_size = 0x2B0; return Common::AlignUp(effect_info_size * params.effect_count, info_field_alignment_size); }; // Calculates the part of the size related to audio sink info. - const auto calculate_sink_info_size = [](const AudioCore::AudioRendererParameter& params) { + const auto calculate_sink_info_size = [](const AudioCommon::AudioRendererParameter& params) { const u64 sink_info_size = 0x170; return Common::AlignUp(sink_info_size * params.sink_count, info_field_alignment_size); }; // Calculates the part of the size related to voice state info. - const auto calculate_voice_state_size = [](const AudioCore::AudioRendererParameter& params) { + const auto calculate_voice_state_size = [](const AudioCommon::AudioRendererParameter& params) { const u64 voice_state_size = 0x100; const u64 additional_size = buffer_alignment_size - 1; return Common::AlignUp(voice_state_size * params.voice_count + additional_size, @@ -516,7 +519,7 @@ void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { }; // Calculates the part of the size related to performance statistics. - const auto calculate_perf_size = [](const AudioCore::AudioRendererParameter& params) { + const auto calculate_perf_size = [](const AudioCommon::AudioRendererParameter& params) { // Extra size value appended to the end of the calculation. constexpr u64 appended = 128; @@ -543,79 +546,81 @@ void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { }; // Calculates the part of the size that relates to the audio command buffer. - const auto calculate_command_buffer_size = [](const AudioCore::AudioRendererParameter& params) { - constexpr u64 alignment = (buffer_alignment_size - 1) * 2; + const auto calculate_command_buffer_size = + [](const AudioCommon::AudioRendererParameter& params) { + constexpr u64 alignment = (buffer_alignment_size - 1) * 2; - if (!IsFeatureSupported(AudioFeatures::VariadicCommandBuffer, params.revision)) { - constexpr u64 command_buffer_size = 0x18000; + if (!IsFeatureSupported(AudioFeatures::VariadicCommandBuffer, params.revision)) { + constexpr u64 command_buffer_size = 0x18000; - return command_buffer_size + alignment; - } + return command_buffer_size + alignment; + } - // When the variadic command buffer is supported, this means - // the command generator for the audio renderer can issue commands - // that are (as one would expect), variable in size. So what we need to do - // is determine the maximum possible size for a few command data structures - // then multiply them by the amount of present commands indicated by the given - // respective audio parameters. + // When the variadic command buffer is supported, this means + // the command generator for the audio renderer can issue commands + // that are (as one would expect), variable in size. So what we need to do + // is determine the maximum possible size for a few command data structures + // then multiply them by the amount of present commands indicated by the given + // respective audio parameters. - constexpr u64 max_biquad_filters = 2; - constexpr u64 max_mix_buffers = 24; + constexpr u64 max_biquad_filters = 2; + constexpr u64 max_mix_buffers = 24; - constexpr u64 biquad_filter_command_size = 0x2C; + constexpr u64 biquad_filter_command_size = 0x2C; - constexpr u64 depop_mix_command_size = 0x24; - constexpr u64 depop_setup_command_size = 0x50; + constexpr u64 depop_mix_command_size = 0x24; + constexpr u64 depop_setup_command_size = 0x50; - constexpr u64 effect_command_max_size = 0x540; + constexpr u64 effect_command_max_size = 0x540; - constexpr u64 mix_command_size = 0x1C; - constexpr u64 mix_ramp_command_size = 0x24; - constexpr u64 mix_ramp_grouped_command_size = 0x13C; + constexpr u64 mix_command_size = 0x1C; + constexpr u64 mix_ramp_command_size = 0x24; + constexpr u64 mix_ramp_grouped_command_size = 0x13C; - constexpr u64 perf_command_size = 0x28; + constexpr u64 perf_command_size = 0x28; - constexpr u64 sink_command_size = 0x130; + constexpr u64 sink_command_size = 0x130; - constexpr u64 submix_command_max_size = - depop_mix_command_size + (mix_command_size * max_mix_buffers) * max_mix_buffers; + constexpr u64 submix_command_max_size = + depop_mix_command_size + (mix_command_size * max_mix_buffers) * max_mix_buffers; - constexpr u64 volume_command_size = 0x1C; - constexpr u64 volume_ramp_command_size = 0x20; + constexpr u64 volume_command_size = 0x1C; + constexpr u64 volume_ramp_command_size = 0x20; - constexpr u64 voice_biquad_filter_command_size = - biquad_filter_command_size * max_biquad_filters; - constexpr u64 voice_data_command_size = 0x9C; - const u64 voice_command_max_size = - (params.splitter_count * depop_setup_command_size) + - (voice_data_command_size + voice_biquad_filter_command_size + volume_ramp_command_size + - mix_ramp_grouped_command_size); + constexpr u64 voice_biquad_filter_command_size = + biquad_filter_command_size * max_biquad_filters; + constexpr u64 voice_data_command_size = 0x9C; + const u64 voice_command_max_size = + (params.splitter_count * depop_setup_command_size) + + (voice_data_command_size + voice_biquad_filter_command_size + + volume_ramp_command_size + mix_ramp_grouped_command_size); - // Now calculate the individual elements that comprise the size and add them together. - const u64 effect_commands_size = params.effect_count * effect_command_max_size; + // Now calculate the individual elements that comprise the size and add them together. + const u64 effect_commands_size = params.effect_count * effect_command_max_size; - const u64 final_mix_commands_size = - depop_mix_command_size + volume_command_size * max_mix_buffers; + const u64 final_mix_commands_size = + depop_mix_command_size + volume_command_size * max_mix_buffers; - const u64 perf_commands_size = - perf_command_size * (CalculateNumPerformanceEntries(params) + max_perf_detail_entries); + const u64 perf_commands_size = + perf_command_size * + (CalculateNumPerformanceEntries(params) + max_perf_detail_entries); - const u64 sink_commands_size = params.sink_count * sink_command_size; + const u64 sink_commands_size = params.sink_count * sink_command_size; - const u64 splitter_commands_size = - params.num_splitter_send_channels * max_mix_buffers * mix_ramp_command_size; + const u64 splitter_commands_size = + params.num_splitter_send_channels * max_mix_buffers * mix_ramp_command_size; - const u64 submix_commands_size = params.submix_count * submix_command_max_size; + const u64 submix_commands_size = params.submix_count * submix_command_max_size; - const u64 voice_commands_size = params.voice_count * voice_command_max_size; + const u64 voice_commands_size = params.voice_count * voice_command_max_size; - return effect_commands_size + final_mix_commands_size + perf_commands_size + - sink_commands_size + splitter_commands_size + submix_commands_size + - voice_commands_size + alignment; - }; + return effect_commands_size + final_mix_commands_size + perf_commands_size + + sink_commands_size + splitter_commands_size + submix_commands_size + + voice_commands_size + alignment; + }; IPC::RequestParser rp{ctx}; - const auto params = rp.PopRaw<AudioCore::AudioRendererParameter>(); + const auto params = rp.PopRaw<AudioCommon::AudioRendererParameter>(); u64 size = 0; size += calculate_mix_buffer_sizes(params); @@ -681,7 +686,7 @@ void AudRenU::GetAudioDeviceServiceWithRevisionInfo(Kernel::HLERequestContext& c void AudRenU::OpenAudioRendererImpl(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const auto params = rp.PopRaw<AudioCore::AudioRendererParameter>(); + const auto params = rp.PopRaw<AudioCommon::AudioRendererParameter>(); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); diff --git a/src/core/hle/service/filesystem/filesystem.cpp b/src/core/hle/service/filesystem/filesystem.cpp index 2cee1193c..54a5fb84b 100644 --- a/src/core/hle/service/filesystem/filesystem.cpp +++ b/src/core/hle/service/filesystem/filesystem.cpp @@ -379,7 +379,7 @@ ResultVal<FileSys::VirtualFile> FileSystemController::OpenBISPartitionStorage( return FileSys::ERROR_ENTITY_NOT_FOUND; } - auto part = bis_factory->OpenPartitionStorage(id); + auto part = bis_factory->OpenPartitionStorage(id, system.GetFilesystem()); if (part == nullptr) { return FileSys::ERROR_INVALID_ARGUMENT; } diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index e742497e1..b65d59373 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -193,7 +193,8 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) { controller.battery_level[0] = BATTERY_FULL; controller.battery_level[1] = BATTERY_FULL; controller.battery_level[2] = BATTERY_FULL; - styleset_changed_events[controller_idx].writable->Signal(); + + SignalStyleSetChangedEvent(IndexToNPad(controller_idx)); } void Controller_NPad::OnInit() { @@ -249,6 +250,9 @@ void Controller_NPad::OnLoadInputDevices() { std::transform(players[i].analogs.begin() + Settings::NativeAnalog::STICK_HID_BEGIN, players[i].analogs.begin() + Settings::NativeAnalog::STICK_HID_END, sticks[i].begin(), Input::CreateDevice<Input::AnalogDevice>); + std::transform(players[i].motions.begin() + Settings::NativeMotion::MOTION_HID_BEGIN, + players[i].motions.begin() + Settings::NativeMotion::MOTION_HID_END, + motions[i].begin(), Input::CreateDevice<Input::MotionDevice>); } } @@ -265,6 +269,7 @@ void Controller_NPad::RequestPadStateUpdate(u32 npad_id) { auto& rstick_entry = npad_pad_states[controller_idx].r_stick; const auto& button_state = buttons[controller_idx]; const auto& analog_state = sticks[controller_idx]; + const auto& motion_state = motions[controller_idx]; const auto [stick_l_x_f, stick_l_y_f] = analog_state[static_cast<std::size_t>(JoystickId::Joystick_Left)]->GetStatus(); const auto [stick_r_x_f, stick_r_y_f] = @@ -359,6 +364,45 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* continue; } const u32 npad_index = static_cast<u32>(i); + + const std::array<SixAxisGeneric*, 6> controller_sixaxes{ + &npad.sixaxis_full, &npad.sixaxis_handheld, &npad.sixaxis_dual_left, + &npad.sixaxis_dual_right, &npad.sixaxis_left, &npad.sixaxis_right, + }; + + for (auto* sixaxis_sensor : controller_sixaxes) { + sixaxis_sensor->common.entry_count = 16; + sixaxis_sensor->common.total_entry_count = 17; + + const auto& last_entry = + sixaxis_sensor->sixaxis[sixaxis_sensor->common.last_entry_index]; + + sixaxis_sensor->common.timestamp = core_timing.GetCPUTicks(); + sixaxis_sensor->common.last_entry_index = + (sixaxis_sensor->common.last_entry_index + 1) % 17; + + auto& cur_entry = sixaxis_sensor->sixaxis[sixaxis_sensor->common.last_entry_index]; + + cur_entry.timestamp = last_entry.timestamp + 1; + cur_entry.timestamp2 = cur_entry.timestamp; + } + + // Try to read sixaxis sensor states + std::array<MotionDevice, 2> motion_devices; + + if (sixaxis_sensors_enabled && Settings::values.motion_enabled) { + sixaxis_at_rest = true; + for (std::size_t e = 0; e < motion_devices.size(); ++e) { + const auto& device = motions[i][e]; + if (device) { + std::tie(motion_devices[e].accel, motion_devices[e].gyro, + motion_devices[e].rotation, motion_devices[e].orientation) = + device->GetStatus(); + sixaxis_at_rest = sixaxis_at_rest && motion_devices[e].gyro.Length2() < 0.0001f; + } + } + } + RequestPadStateUpdate(npad_index); auto& pad_state = npad_pad_states[npad_index]; @@ -376,6 +420,18 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* libnx_entry.connection_status.raw = 0; libnx_entry.connection_status.IsConnected.Assign(1); + auto& full_sixaxis_entry = + npad.sixaxis_full.sixaxis[npad.sixaxis_full.common.last_entry_index]; + auto& handheld_sixaxis_entry = + npad.sixaxis_handheld.sixaxis[npad.sixaxis_handheld.common.last_entry_index]; + auto& dual_left_sixaxis_entry = + npad.sixaxis_dual_left.sixaxis[npad.sixaxis_dual_left.common.last_entry_index]; + auto& dual_right_sixaxis_entry = + npad.sixaxis_dual_right.sixaxis[npad.sixaxis_dual_right.common.last_entry_index]; + auto& left_sixaxis_entry = + npad.sixaxis_left.sixaxis[npad.sixaxis_left.common.last_entry_index]; + auto& right_sixaxis_entry = + npad.sixaxis_right.sixaxis[npad.sixaxis_right.common.last_entry_index]; switch (controller_type) { case NPadControllerType::None: @@ -390,6 +446,13 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* main_controller.pad.r_stick = pad_state.r_stick; libnx_entry.connection_status.IsWired.Assign(1); + + if (sixaxis_sensors_enabled && motions[i][0]) { + full_sixaxis_entry.accel = motion_devices[0].accel; + full_sixaxis_entry.gyro = motion_devices[0].gyro; + full_sixaxis_entry.rotation = motion_devices[0].rotation; + full_sixaxis_entry.orientation = motion_devices[0].orientation; + } break; case NPadControllerType::Handheld: handheld_entry.connection_status.raw = 0; @@ -408,6 +471,13 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* libnx_entry.connection_status.IsRightJoyConnected.Assign(1); libnx_entry.connection_status.IsLeftJoyWired.Assign(1); libnx_entry.connection_status.IsRightJoyWired.Assign(1); + + if (sixaxis_sensors_enabled && motions[i][0]) { + handheld_sixaxis_entry.accel = motion_devices[0].accel; + handheld_sixaxis_entry.gyro = motion_devices[0].gyro; + handheld_sixaxis_entry.rotation = motion_devices[0].rotation; + handheld_sixaxis_entry.orientation = motion_devices[0].orientation; + } break; case NPadControllerType::JoyDual: dual_entry.connection_status.raw = 0; @@ -420,6 +490,21 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* libnx_entry.connection_status.IsLeftJoyConnected.Assign(1); libnx_entry.connection_status.IsRightJoyConnected.Assign(1); + + if (sixaxis_sensors_enabled && motions[i][0]) { + // Set motion for the left joycon + dual_left_sixaxis_entry.accel = motion_devices[0].accel; + dual_left_sixaxis_entry.gyro = motion_devices[0].gyro; + dual_left_sixaxis_entry.rotation = motion_devices[0].rotation; + dual_left_sixaxis_entry.orientation = motion_devices[0].orientation; + } + if (sixaxis_sensors_enabled && motions[i][1]) { + // Set motion for the right joycon + dual_right_sixaxis_entry.accel = motion_devices[1].accel; + dual_right_sixaxis_entry.gyro = motion_devices[1].gyro; + dual_right_sixaxis_entry.rotation = motion_devices[1].rotation; + dual_right_sixaxis_entry.orientation = motion_devices[1].orientation; + } break; case NPadControllerType::JoyLeft: left_entry.connection_status.raw = 0; @@ -430,6 +515,13 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* left_entry.pad.r_stick = pad_state.r_stick; libnx_entry.connection_status.IsLeftJoyConnected.Assign(1); + + if (sixaxis_sensors_enabled && motions[i][0]) { + left_sixaxis_entry.accel = motion_devices[0].accel; + left_sixaxis_entry.gyro = motion_devices[0].gyro; + left_sixaxis_entry.rotation = motion_devices[0].rotation; + left_sixaxis_entry.orientation = motion_devices[0].orientation; + } break; case NPadControllerType::JoyRight: right_entry.connection_status.raw = 0; @@ -440,6 +532,13 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* right_entry.pad.r_stick = pad_state.r_stick; libnx_entry.connection_status.IsRightJoyConnected.Assign(1); + + if (sixaxis_sensors_enabled && motions[i][1]) { + right_sixaxis_entry.accel = motion_devices[1].accel; + right_sixaxis_entry.gyro = motion_devices[1].gyro; + right_sixaxis_entry.rotation = motion_devices[1].rotation; + right_sixaxis_entry.orientation = motion_devices[1].orientation; + } break; case NPadControllerType::Pokeball: pokeball_entry.connection_status.raw = 0; @@ -518,13 +617,17 @@ void Controller_NPad::VibrateController(const std::vector<u32>& controller_ids, last_processed_vibration = vibrations.back(); } +Controller_NPad::Vibration Controller_NPad::GetLastVibration() const { + return last_processed_vibration; +} + std::shared_ptr<Kernel::ReadableEvent> Controller_NPad::GetStyleSetChangedEvent(u32 npad_id) const { const auto& styleset_event = styleset_changed_events[NPadIdToIndex(npad_id)]; return styleset_event.readable; } -Controller_NPad::Vibration Controller_NPad::GetLastVibration() const { - return last_processed_vibration; +void Controller_NPad::SignalStyleSetChangedEvent(u32 npad_id) const { + styleset_changed_events[NPadIdToIndex(npad_id)].writable->Signal(); } void Controller_NPad::AddNewControllerAt(NPadControllerType controller, std::size_t npad_index) { @@ -534,7 +637,7 @@ void Controller_NPad::AddNewControllerAt(NPadControllerType controller, std::siz void Controller_NPad::UpdateControllerAt(NPadControllerType controller, std::size_t npad_index, bool connected) { if (!connected) { - DisconnectNPad(IndexToNPad(npad_index)); + DisconnectNPadAtIndex(npad_index); return; } @@ -554,16 +657,19 @@ void Controller_NPad::UpdateControllerAt(NPadControllerType controller, std::siz } void Controller_NPad::DisconnectNPad(u32 npad_id) { - const auto npad_index = NPadIdToIndex(npad_id); - connected_controllers[npad_index].is_connected = false; + DisconnectNPadAtIndex(NPadIdToIndex(npad_id)); +} + +void Controller_NPad::DisconnectNPadAtIndex(std::size_t npad_index) { Settings::values.players[npad_index].connected = false; + connected_controllers[npad_index].is_connected = false; auto& controller = shared_memory_entries[npad_index]; controller.joy_styles.raw = 0; // Zero out controller.device_type.raw = 0; controller.properties.raw = 0; - styleset_changed_events[npad_index].writable->Signal(); + SignalStyleSetChangedEvent(IndexToNPad(npad_index)); } void Controller_NPad::SetGyroscopeZeroDriftMode(GyroscopeZeroDriftMode drift_mode) { @@ -574,6 +680,14 @@ Controller_NPad::GyroscopeZeroDriftMode Controller_NPad::GetGyroscopeZeroDriftMo return gyroscope_zero_drift_mode; } +bool Controller_NPad::IsSixAxisSensorAtRest() const { + return sixaxis_at_rest; +} + +void Controller_NPad::SetSixAxisEnabled(bool six_axis_status) { + sixaxis_sensors_enabled = six_axis_status; +} + void Controller_NPad::MergeSingleJoyAsDualJoy(u32 npad_id_1, u32 npad_id_2) { const auto npad_index_1 = NPadIdToIndex(npad_id_1); const auto npad_index_2 = NPadIdToIndex(npad_id_2); @@ -666,13 +780,13 @@ void Controller_NPad::ClearAllConnectedControllers() { } void Controller_NPad::DisconnectAllConnectedControllers() { - for (ControllerHolder& controller : connected_controllers) { + for (auto& controller : connected_controllers) { controller.is_connected = false; } } void Controller_NPad::ConnectAllDisconnectedControllers() { - for (ControllerHolder& controller : connected_controllers) { + for (auto& controller : connected_controllers) { if (controller.type != NPadControllerType::None && !controller.is_connected) { controller.is_connected = true; } @@ -680,7 +794,7 @@ void Controller_NPad::ConnectAllDisconnectedControllers() { } void Controller_NPad::ClearAllControllers() { - for (ControllerHolder& controller : connected_controllers) { + for (auto& controller : connected_controllers) { controller.type = NPadControllerType::None; controller.is_connected = false; } @@ -728,92 +842,4 @@ bool Controller_NPad::IsControllerSupported(NPadControllerType controller) const return false; } -Controller_NPad::NPadControllerType Controller_NPad::DecideBestController( - NPadControllerType priority) const { - if (IsControllerSupported(priority)) { - return priority; - } - const auto is_docked = Settings::values.use_docked_mode; - if (is_docked && priority == NPadControllerType::Handheld) { - priority = NPadControllerType::JoyDual; - if (IsControllerSupported(priority)) { - return priority; - } - } - std::vector<NPadControllerType> priority_list; - switch (priority) { - case NPadControllerType::ProController: - priority_list.push_back(NPadControllerType::JoyDual); - if (!is_docked) { - priority_list.push_back(NPadControllerType::Handheld); - } - priority_list.push_back(NPadControllerType::JoyLeft); - priority_list.push_back(NPadControllerType::JoyRight); - priority_list.push_back(NPadControllerType::Pokeball); - break; - case NPadControllerType::Handheld: - priority_list.push_back(NPadControllerType::JoyDual); - priority_list.push_back(NPadControllerType::ProController); - priority_list.push_back(NPadControllerType::JoyLeft); - priority_list.push_back(NPadControllerType::JoyRight); - priority_list.push_back(NPadControllerType::Pokeball); - break; - case NPadControllerType::JoyDual: - if (!is_docked) { - priority_list.push_back(NPadControllerType::Handheld); - } - priority_list.push_back(NPadControllerType::ProController); - priority_list.push_back(NPadControllerType::JoyLeft); - priority_list.push_back(NPadControllerType::JoyRight); - priority_list.push_back(NPadControllerType::Pokeball); - break; - case NPadControllerType::JoyLeft: - priority_list.push_back(NPadControllerType::JoyRight); - priority_list.push_back(NPadControllerType::JoyDual); - if (!is_docked) { - priority_list.push_back(NPadControllerType::Handheld); - } - priority_list.push_back(NPadControllerType::ProController); - priority_list.push_back(NPadControllerType::Pokeball); - break; - case NPadControllerType::JoyRight: - priority_list.push_back(NPadControllerType::JoyLeft); - priority_list.push_back(NPadControllerType::JoyDual); - if (!is_docked) { - priority_list.push_back(NPadControllerType::Handheld); - } - priority_list.push_back(NPadControllerType::ProController); - priority_list.push_back(NPadControllerType::Pokeball); - break; - case NPadControllerType::Pokeball: - priority_list.push_back(NPadControllerType::JoyLeft); - priority_list.push_back(NPadControllerType::JoyRight); - priority_list.push_back(NPadControllerType::JoyDual); - if (!is_docked) { - priority_list.push_back(NPadControllerType::Handheld); - } - priority_list.push_back(NPadControllerType::ProController); - break; - default: - priority_list.push_back(NPadControllerType::JoyDual); - if (!is_docked) { - priority_list.push_back(NPadControllerType::Handheld); - } - priority_list.push_back(NPadControllerType::ProController); - priority_list.push_back(NPadControllerType::JoyLeft); - priority_list.push_back(NPadControllerType::JoyRight); - priority_list.push_back(NPadControllerType::JoyDual); - break; - } - - const auto iter = std::find_if(priority_list.begin(), priority_list.end(), - [this](auto type) { return IsControllerSupported(type); }); - if (iter == priority_list.end()) { - UNIMPLEMENTED_MSG("Could not find supported controller!"); - return priority; - } - - return *iter; -} - } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index ad25c6fbf..78e7c320b 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -115,17 +115,23 @@ public: void VibrateController(const std::vector<u32>& controller_ids, const std::vector<Vibration>& vibrations); - std::shared_ptr<Kernel::ReadableEvent> GetStyleSetChangedEvent(u32 npad_id) const; Vibration GetLastVibration() const; + std::shared_ptr<Kernel::ReadableEvent> GetStyleSetChangedEvent(u32 npad_id) const; + void SignalStyleSetChangedEvent(u32 npad_id) const; + // Adds a new controller at an index. void AddNewControllerAt(NPadControllerType controller, std::size_t npad_index); // Adds a new controller at an index with connection status. void UpdateControllerAt(NPadControllerType controller, std::size_t npad_index, bool connected); void DisconnectNPad(u32 npad_id); + void DisconnectNPadAtIndex(std::size_t index); + void SetGyroscopeZeroDriftMode(GyroscopeZeroDriftMode drift_mode); GyroscopeZeroDriftMode GetGyroscopeZeroDriftMode() const; + bool IsSixAxisSensorAtRest() const; + void SetSixAxisEnabled(bool six_axis_status); LedPattern GetLedPattern(u32 npad_id); void SetVibrationEnabled(bool can_vibrate); bool IsVibrationEnabled() const; @@ -248,6 +254,24 @@ private: }; static_assert(sizeof(NPadGeneric) == 0x350, "NPadGeneric is an invalid size"); + struct SixAxisStates { + s64_le timestamp{}; + INSERT_PADDING_WORDS(2); + s64_le timestamp2{}; + Common::Vec3f accel{}; + Common::Vec3f gyro{}; + Common::Vec3f rotation{}; + std::array<Common::Vec3f, 3> orientation{}; + s64_le always_one{1}; + }; + static_assert(sizeof(SixAxisStates) == 0x68, "SixAxisStates is an invalid size"); + + struct SixAxisGeneric { + CommonHeader common{}; + std::array<SixAxisStates, 17> sixaxis{}; + }; + static_assert(sizeof(SixAxisGeneric) == 0x708, "SixAxisGeneric is an invalid size"); + enum class ColorReadError : u32_le { ReadOk = 0, ColorDoesntExist = 1, @@ -277,6 +301,13 @@ private: }; }; + struct MotionDevice { + Common::Vec3f accel; + Common::Vec3f gyro; + Common::Vec3f rotation; + std::array<Common::Vec3f, 3> orientation; + }; + struct NPadEntry { NPadType joy_styles; NPadAssignments pad_assignment; @@ -296,9 +327,12 @@ private: NPadGeneric pokeball_states; NPadGeneric libnx; // TODO(ogniK): Find out what this actually is, libnx seems to only be // relying on this for the time being - INSERT_PADDING_BYTES( - 0x708 * - 6); // TODO(ogniK): SixAxis states, require more information before implementation + SixAxisGeneric sixaxis_full; + SixAxisGeneric sixaxis_handheld; + SixAxisGeneric sixaxis_dual_left; + SixAxisGeneric sixaxis_dual_right; + SixAxisGeneric sixaxis_left; + SixAxisGeneric sixaxis_right; NPadDevice device_type; NPadProperties properties; INSERT_PADDING_WORDS(1); @@ -315,21 +349,24 @@ private: void InitNewlyAddedController(std::size_t controller_idx); bool IsControllerSupported(NPadControllerType controller) const; - NPadControllerType DecideBestController(NPadControllerType priority) const; void RequestPadStateUpdate(u32 npad_id); u32 press_state{}; NPadType style{}; std::array<NPadEntry, 10> shared_memory_entries{}; - std::array< + using ButtonArray = std::array< std::array<std::unique_ptr<Input::ButtonDevice>, Settings::NativeButton::NUM_BUTTONS_HID>, - 10> - buttons; - std::array< + 10>; + using StickArray = std::array< std::array<std::unique_ptr<Input::AnalogDevice>, Settings::NativeAnalog::NUM_STICKS_HID>, - 10> - sticks; + 10>; + using MotionArray = std::array< + std::array<std::unique_ptr<Input::MotionDevice>, Settings::NativeMotion::NUM_MOTION_HID>, + 10>; + ButtonArray buttons; + StickArray sticks; + MotionArray motions; std::vector<u32> supported_npad_id_types{}; NpadHoldType hold_type{NpadHoldType::Vertical}; // Each controller should have their own styleset changed event @@ -338,6 +375,8 @@ private: std::array<ControllerHolder, 10> connected_controllers{}; GyroscopeZeroDriftMode gyroscope_zero_drift_mode{GyroscopeZeroDriftMode::Standard}; bool can_controllers_vibrate{true}; + bool sixaxis_sensors_enabled{true}; + bool sixaxis_at_rest{true}; std::array<ControllerPad, 10> npad_pad_states{}; bool is_in_lr_assignment_mode{false}; Core::System& system; diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 9453134bf..1d96f705f 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -164,8 +164,8 @@ Hid::Hid(Core::System& system) : ServiceFramework("hid"), system(system) { {56, nullptr, "ActivateJoyXpad"}, {58, nullptr, "GetJoyXpadLifoHandle"}, {59, nullptr, "GetJoyXpadIds"}, - {60, nullptr, "ActivateSixAxisSensor"}, - {61, nullptr, "DeactivateSixAxisSensor"}, + {60, &Hid::ActivateSixAxisSensor, "ActivateSixAxisSensor"}, + {61, &Hid::DeactivateSixAxisSensor, "DeactivateSixAxisSensor"}, {62, nullptr, "GetSixAxisSensorLifoHandle"}, {63, nullptr, "ActivateJoySixAxisSensor"}, {64, nullptr, "DeactivateJoySixAxisSensor"}, @@ -329,6 +329,31 @@ void Hid::GetXpadIDs(Kernel::HLERequestContext& ctx) { rb.Push(0); } +void Hid::ActivateSixAxisSensor(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const auto handle{rp.Pop<u32>()}; + const auto applet_resource_user_id{rp.Pop<u64>()}; + applet_resource->GetController<Controller_NPad>(HidController::NPad).SetSixAxisEnabled(true); + LOG_DEBUG(Service_HID, "called, handle={}, applet_resource_user_id={}", handle, + applet_resource_user_id); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(RESULT_SUCCESS); +} + +void Hid::DeactivateSixAxisSensor(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const auto handle{rp.Pop<u32>()}; + const auto applet_resource_user_id{rp.Pop<u64>()}; + applet_resource->GetController<Controller_NPad>(HidController::NPad).SetSixAxisEnabled(false); + + LOG_DEBUG(Service_HID, "called, handle={}, applet_resource_user_id={}", handle, + applet_resource_user_id); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(RESULT_SUCCESS); +} + void Hid::ActivateDebugPad(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; const auto applet_resource_user_id{rp.Pop<u64>()}; @@ -484,13 +509,13 @@ void Hid::IsSixAxisSensorAtRest(Kernel::HLERequestContext& ctx) { const auto handle{rp.Pop<u32>()}; const auto applet_resource_user_id{rp.Pop<u64>()}; - LOG_WARNING(Service_HID, "(STUBBED) called, handle={}, applet_resource_user_id={}", handle, - applet_resource_user_id); + LOG_DEBUG(Service_HID, "called, handle={}, applet_resource_user_id={}", handle, + applet_resource_user_id); IPC::ResponseBuilder rb{ctx, 3}; rb.Push(RESULT_SUCCESS); - // TODO (Hexagon12): Properly implement reading gyroscope values from controllers. - rb.Push(true); + rb.Push(applet_resource->GetController<Controller_NPad>(HidController::NPad) + .IsSixAxisSensorAtRest()); } void Hid::SetSupportedNpadStyleSet(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h index efb07547f..e04aaf1e9 100644 --- a/src/core/hle/service/hid/hid.h +++ b/src/core/hle/service/hid/hid.h @@ -86,6 +86,8 @@ private: void CreateAppletResource(Kernel::HLERequestContext& ctx); void ActivateXpad(Kernel::HLERequestContext& ctx); void GetXpadIDs(Kernel::HLERequestContext& ctx); + void ActivateSixAxisSensor(Kernel::HLERequestContext& ctx); + void DeactivateSixAxisSensor(Kernel::HLERequestContext& ctx); void ActivateDebugPad(Kernel::HLERequestContext& ctx); void ActivateTouchScreen(Kernel::HLERequestContext& ctx); void ActivateMouse(Kernel::HLERequestContext& ctx); diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 5e2d769a4..a0469ffbd 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <array> #include <atomic> #include "common/logging/log.h" @@ -72,10 +73,10 @@ private: std::array<u8, 10> uuid; u8 uuid_length; // TODO(ogniK): Figure out if this is actual the uuid length or does it // mean something else - INSERT_PADDING_BYTES(0x15); + std::array<u8, 0x15> padding_1; u32_le protocol; u32_le tag_type; - INSERT_PADDING_BYTES(0x2c); + std::array<u8, 0x2c> padding_2; }; static_assert(sizeof(TagInfo) == 0x54, "TagInfo is an invalid size"); @@ -213,13 +214,15 @@ private: LOG_DEBUG(Service_NFP, "called"); IPC::ResponseBuilder rb{ctx, 2}; - auto amiibo = nfp_interface.GetAmiiboBuffer(); - TagInfo tag_info{}; - tag_info.uuid = amiibo.uuid; - tag_info.uuid_length = static_cast<u8>(tag_info.uuid.size()); - - tag_info.protocol = 1; // TODO(ogniK): Figure out actual values - tag_info.tag_type = 2; + const auto& amiibo = nfp_interface.GetAmiiboBuffer(); + const TagInfo tag_info{ + .uuid = amiibo.uuid, + .uuid_length = static_cast<u8>(tag_info.uuid.size()), + .padding_1 = {}, + .protocol = 1, // TODO(ogniK): Figure out actual values + .tag_type = 2, + .padding_2 = {}, + }; ctx.WriteBuffer(tag_info); rb.Push(RESULT_SUCCESS); } @@ -236,7 +239,7 @@ private: LOG_DEBUG(Service_NFP, "called"); IPC::ResponseBuilder rb{ctx, 2}; - auto amiibo = nfp_interface.GetAmiiboBuffer(); + const auto& amiibo = nfp_interface.GetAmiiboBuffer(); ctx.WriteBuffer(amiibo.model_info); rb.Push(RESULT_SUCCESS); } diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index 538f28495..76b3533ec 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -72,25 +72,6 @@ namespace Service { -/** - * Creates a function string for logging, complete with the name (or header code, depending - * on what's passed in) the port name, and all the cmd_buff arguments. - */ -[[maybe_unused]] static std::string MakeFunctionString(std::string_view name, - std::string_view port_name, - const u32* cmd_buff) { - // Number of params == bits 0-5 + bits 6-11 - int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F); - - std::string function_string = fmt::format("function '{}': port={}", name, port_name); - for (int i = 1; i <= num_params; ++i) { - function_string += fmt::format(", cmd_buff[{}]=0x{:X}", i, cmd_buff[i]); - } - return function_string; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - ServiceFrameworkBase::ServiceFrameworkBase(const char* service_name, u32 max_sessions, InvokerFn* handler_invoker) : service_name(service_name), max_sessions(max_sessions), handler_invoker(handler_invoker) {} @@ -105,10 +86,9 @@ void ServiceFrameworkBase::InstallAsService(SM::ServiceManager& service_manager) port_installed = true; } -void ServiceFrameworkBase::InstallAsNamedPort() { +void ServiceFrameworkBase::InstallAsNamedPort(Kernel::KernelCore& kernel) { ASSERT(!port_installed); - auto& kernel = Core::System::GetInstance().Kernel(); auto [server_port, client_port] = Kernel::ServerPort::CreatePortPair(kernel, max_sessions, service_name); server_port->SetHleHandler(shared_from_this()); @@ -116,10 +96,9 @@ void ServiceFrameworkBase::InstallAsNamedPort() { port_installed = true; } -std::shared_ptr<Kernel::ClientPort> ServiceFrameworkBase::CreatePort() { +std::shared_ptr<Kernel::ClientPort> ServiceFrameworkBase::CreatePort(Kernel::KernelCore& kernel) { ASSERT(!port_installed); - auto& kernel = Core::System::GetInstance().Kernel(); auto [server_port, client_port] = Kernel::ServerPort::CreatePortPair(kernel, max_sessions, service_name); auto port = MakeResult(std::move(server_port)).Unwrap(); @@ -191,9 +170,6 @@ ResultCode ServiceFrameworkBase::HandleSyncRequest(Kernel::HLERequestContext& co return RESULT_SUCCESS; } -//////////////////////////////////////////////////////////////////////////////////////////////////// -// Module interface - /// Initialize ServiceManager void Init(std::shared_ptr<SM::ServiceManager>& sm, Core::System& system) { // NVFlinger needs to be accessed by several services like Vi and AppletOE so we instantiate it diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h index 022d885b6..a01ef3353 100644 --- a/src/core/hle/service/service.h +++ b/src/core/hle/service/service.h @@ -63,9 +63,9 @@ public: /// Creates a port pair and registers this service with the given ServiceManager. void InstallAsService(SM::ServiceManager& service_manager); /// Creates a port pair and registers it on the kernel's global port registry. - void InstallAsNamedPort(); + void InstallAsNamedPort(Kernel::KernelCore& kernel); /// Creates and returns an unregistered port for the service. - std::shared_ptr<Kernel::ClientPort> CreatePort(); + std::shared_ptr<Kernel::ClientPort> CreatePort(Kernel::KernelCore& kernel); void InvokeRequest(Kernel::HLERequestContext& ctx); diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index d872de16c..9c1da361b 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -19,7 +19,7 @@ constexpr ResultCode ERR_ALREADY_REGISTERED(ErrorModule::SM, 4); constexpr ResultCode ERR_INVALID_NAME(ErrorModule::SM, 6); constexpr ResultCode ERR_SERVICE_NOT_REGISTERED(ErrorModule::SM, 7); -ServiceManager::ServiceManager() = default; +ServiceManager::ServiceManager(Kernel::KernelCore& kernel_) : kernel{kernel_} {} ServiceManager::~ServiceManager() = default; void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) { @@ -27,11 +27,11 @@ void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) { } static ResultCode ValidateServiceName(const std::string& name) { - if (name.size() <= 0 || name.size() > 8) { + if (name.empty() || name.size() > 8) { LOG_ERROR(Service_SM, "Invalid service name! service={}", name); return ERR_INVALID_NAME; } - if (name.find('\0') != std::string::npos) { + if (name.rfind('\0') != std::string::npos) { LOG_ERROR(Service_SM, "A non null terminated service was passed"); return ERR_INVALID_NAME; } @@ -43,13 +43,13 @@ void ServiceManager::InstallInterfaces(std::shared_ptr<ServiceManager> self, ASSERT(self->sm_interface.expired()); auto sm = std::make_shared<SM>(self, kernel); - sm->InstallAsNamedPort(); + sm->InstallAsNamedPort(kernel); self->sm_interface = sm; self->controller_interface = std::make_unique<Controller>(); } -ResultVal<std::shared_ptr<Kernel::ServerPort>> ServiceManager::RegisterService( - std::string name, unsigned int max_sessions) { +ResultVal<std::shared_ptr<Kernel::ServerPort>> ServiceManager::RegisterService(std::string name, + u32 max_sessions) { CASCADE_CODE(ValidateServiceName(name)); @@ -58,7 +58,6 @@ ResultVal<std::shared_ptr<Kernel::ServerPort>> ServiceManager::RegisterService( return ERR_ALREADY_REGISTERED; } - auto& kernel = Core::System::GetInstance().Kernel(); auto [server_port, client_port] = Kernel::ServerPort::CreatePortPair(kernel, max_sessions, name); diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index aabf166b7..6790c86f0 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -48,11 +48,11 @@ class ServiceManager { public: static void InstallInterfaces(std::shared_ptr<ServiceManager> self, Kernel::KernelCore& kernel); - ServiceManager(); + explicit ServiceManager(Kernel::KernelCore& kernel_); ~ServiceManager(); ResultVal<std::shared_ptr<Kernel::ServerPort>> RegisterService(std::string name, - unsigned int max_sessions); + u32 max_sessions); ResultCode UnregisterService(const std::string& name); ResultVal<std::shared_ptr<Kernel::ClientPort>> GetServicePort(const std::string& name); ResultVal<std::shared_ptr<Kernel::ClientSession>> ConnectToService(const std::string& name); @@ -79,6 +79,9 @@ private: /// Map of registered services, retrieved using GetServicePort or ConnectToService. std::unordered_map<std::string, std::shared_ptr<Kernel::ClientPort>> registered_services; + + /// Kernel context + Kernel::KernelCore& kernel; }; } // namespace Service::SM diff --git a/src/core/hle/service/sockets/blocking_worker.h b/src/core/hle/service/sockets/blocking_worker.h index 31ef6b821..2d53e52b6 100644 --- a/src/core/hle/service/sockets/blocking_worker.h +++ b/src/core/hle/service/sockets/blocking_worker.h @@ -29,7 +29,7 @@ namespace Service::Sockets { * Worker abstraction to execute blocking calls on host without blocking the guest thread * * @tparam Service Service where the work is executed - * @tparam ...Types Types of work to execute + * @tparam Types Types of work to execute */ template <class Service, class... Types> class BlockingWorker { @@ -109,9 +109,8 @@ private: while (keep_running) { work_event.Wait(); - const auto visit_fn = [service, &keep_running](auto&& w) { - using T = std::decay_t<decltype(w)>; - if constexpr (std::is_same_v<T, std::monostate>) { + const auto visit_fn = [service, &keep_running]<typename T>(T&& w) { + if constexpr (std::is_same_v<std::decay_t<T>, std::monostate>) { keep_running = false; } else { w.Execute(service); diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp index 803505452..7b9dd42d8 100644 --- a/src/core/hle/service/sockets/bsd.cpp +++ b/src/core/hle/service/sockets/bsd.cpp @@ -491,7 +491,7 @@ std::pair<s32, Errno> BSD::PollImpl(std::vector<u8>& write_buffer, std::vector<u for (PollFD& pollfd : fds) { ASSERT(pollfd.revents == 0); - if (pollfd.fd > MAX_FD || pollfd.fd < 0) { + if (pollfd.fd > static_cast<s32>(MAX_FD) || pollfd.fd < 0) { LOG_ERROR(Service, "File descriptor handle={} is invalid", pollfd.fd); pollfd.revents = 0; return {0, Errno::SUCCESS}; @@ -764,6 +764,7 @@ std::pair<s32, Errno> BSD::SendToImpl(s32 fd, u32 flags, const std::vector<u8>& SockAddrIn guest_addr_in; std::memcpy(&guest_addr_in, addr.data(), sizeof(guest_addr_in)); addr_in = Translate(guest_addr_in); + p_addr_in = &addr_in; } return Translate(file_descriptors[fd]->socket->SendTo(flags, message, p_addr_in)); @@ -795,7 +796,7 @@ s32 BSD::FindFreeFileDescriptorHandle() noexcept { } bool BSD::IsFileDescriptorValid(s32 fd) const noexcept { - if (fd > MAX_FD || fd < 0) { + if (fd > static_cast<s32>(MAX_FD) || fd < 0) { LOG_ERROR(Service, "Invalid file descriptor handle={}", fd); return false; } @@ -809,7 +810,7 @@ bool BSD::IsFileDescriptorValid(s32 fd) const noexcept { bool BSD::IsBlockingSocket(s32 fd) const noexcept { // Inform invalid sockets as non-blocking // This way we avoid using a worker thread as it will fail without blocking host - if (fd > MAX_FD || fd < 0) { + if (fd > static_cast<s32>(MAX_FD) || fd < 0) { return false; } if (!file_descriptors[fd]) { diff --git a/src/core/hle/service/sockets/sockets_translate.cpp b/src/core/hle/service/sockets/sockets_translate.cpp index 2be8f642d..139743e1d 100644 --- a/src/core/hle/service/sockets/sockets_translate.cpp +++ b/src/core/hle/service/sockets/sockets_translate.cpp @@ -131,21 +131,21 @@ u16 TranslatePollEventsToGuest(u16 flags) { Network::SockAddrIn Translate(SockAddrIn value) { ASSERT(value.len == 0 || value.len == sizeof(value)); - Network::SockAddrIn result; - result.family = Translate(static_cast<Domain>(value.family)); - result.ip = value.ip; - result.portno = value.portno >> 8 | value.portno << 8; - return result; + return { + .family = Translate(static_cast<Domain>(value.family)), + .ip = value.ip, + .portno = static_cast<u16>(value.portno >> 8 | value.portno << 8), + }; } SockAddrIn Translate(Network::SockAddrIn value) { - SockAddrIn result; - result.len = sizeof(result); - result.family = static_cast<u8>(Translate(value.family)); - result.portno = value.portno >> 8 | value.portno << 8; - result.ip = value.ip; - result.zeroes = {}; - return result; + return { + .len = sizeof(SockAddrIn), + .family = static_cast<u8>(Translate(value.family)), + .portno = static_cast<u16>(value.portno >> 8 | value.portno << 8), + .ip = value.ip, + .zeroes = {}, + }; } Network::ShutdownHow Translate(ShutdownHow how) { diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index 134e83412..394a1bf26 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -89,7 +89,7 @@ FileType AppLoader_DeconstructedRomDirectory::IdentifyType(const FileSys::Virtua } AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirectory::Load( - Kernel::Process& process) { + Kernel::Process& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } @@ -141,9 +141,9 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect continue; } - const bool should_pass_arguments{std::strcmp(module, "rtld") == 0}; - const auto tentative_next_load_addr{AppLoader_NSO::LoadModule( - process, *module_file, code_size, should_pass_arguments, false)}; + const bool should_pass_arguments = std::strcmp(module, "rtld") == 0; + const auto tentative_next_load_addr = AppLoader_NSO::LoadModule( + process, system, *module_file, code_size, should_pass_arguments, false); if (!tentative_next_load_addr) { return {ResultStatus::ErrorLoadingNSO, {}}; } @@ -168,9 +168,9 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect } const VAddr load_addr{next_load_addr}; - const bool should_pass_arguments{std::strcmp(module, "rtld") == 0}; - const auto tentative_next_load_addr{AppLoader_NSO::LoadModule( - process, *module_file, load_addr, should_pass_arguments, true, pm)}; + const bool should_pass_arguments = std::strcmp(module, "rtld") == 0; + const auto tentative_next_load_addr = AppLoader_NSO::LoadModule( + process, system, *module_file, load_addr, should_pass_arguments, true, pm); if (!tentative_next_load_addr) { return {ResultStatus::ErrorLoadingNSO, {}}; } @@ -192,8 +192,8 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect // Register the RomFS if a ".romfs" file was found if (romfs_iter != files.end() && *romfs_iter != nullptr) { romfs = *romfs_iter; - Core::System::GetInstance().GetFileSystemController().RegisterRomFS( - std::make_unique<FileSys::RomFSFactory>(*this)); + system.GetFileSystemController().RegisterRomFS(std::make_unique<FileSys::RomFSFactory>( + *this, system.GetContentProvider(), system.GetFileSystemController())); } is_loaded = true; diff --git a/src/core/loader/deconstructed_rom_directory.h b/src/core/loader/deconstructed_rom_directory.h index 1c0a354a4..35d340317 100644 --- a/src/core/loader/deconstructed_rom_directory.h +++ b/src/core/loader/deconstructed_rom_directory.h @@ -9,6 +9,10 @@ #include "core/file_sys/program_metadata.h" #include "core/loader/loader.h" +namespace Core { +class System; +} + namespace Loader { /** @@ -37,7 +41,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; ResultStatus ReadIcon(std::vector<u8>& buffer) override; diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 8f7615115..dca1fcb18 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -383,7 +383,8 @@ FileType AppLoader_ELF::IdentifyType(const FileSys::VirtualFile& file) { return FileType::Error; } -AppLoader_ELF::LoadResult AppLoader_ELF::Load(Kernel::Process& process) { +AppLoader_ELF::LoadResult AppLoader_ELF::Load(Kernel::Process& process, + [[maybe_unused]] Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h index 7ef7770a6..3527933ad 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -8,6 +8,10 @@ #include "common/common_types.h" #include "core/loader/loader.h" +namespace Core { +class System; +} + namespace Loader { /// Loads an ELF/AXF file @@ -26,7 +30,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; }; } // namespace Loader diff --git a/src/core/loader/kip.cpp b/src/core/loader/kip.cpp index 40fa03ad1..5981bcd21 100644 --- a/src/core/loader/kip.cpp +++ b/src/core/loader/kip.cpp @@ -43,7 +43,8 @@ FileType AppLoader_KIP::GetFileType() const { : FileType::Error; } -AppLoader::LoadResult AppLoader_KIP::Load(Kernel::Process& process) { +AppLoader::LoadResult AppLoader_KIP::Load(Kernel::Process& process, + [[maybe_unused]] Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/kip.h b/src/core/loader/kip.h index 12ca40269..dee05a7b5 100644 --- a/src/core/loader/kip.h +++ b/src/core/loader/kip.h @@ -6,6 +6,10 @@ #include "core/loader/loader.h" +namespace Core { +class System; +} + namespace FileSys { class KIP; } @@ -26,7 +30,7 @@ public: FileType GetFileType() const override; - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; private: std::unique_ptr<FileSys::KIP> kip; diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 227ecc704..ac60b097a 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -15,6 +15,10 @@ #include "core/file_sys/control_metadata.h" #include "core/file_sys/vfs.h" +namespace Core { +class System; +} + namespace FileSys { class NACP; } // namespace FileSys @@ -154,9 +158,10 @@ public: /** * Load the application and return the created Process instance * @param process The newly created process. + * @param system The system that this process is being loaded under. * @return The status result of the operation. */ - virtual LoadResult Load(Kernel::Process& process) = 0; + virtual LoadResult Load(Kernel::Process& process, Core::System& system) = 0; /** * Get the code (typically .code section) of the application diff --git a/src/core/loader/nax.cpp b/src/core/loader/nax.cpp index a152981a0..49028177b 100644 --- a/src/core/loader/nax.cpp +++ b/src/core/loader/nax.cpp @@ -41,7 +41,8 @@ FileType AppLoader_NAX::GetFileType() const { return IdentifyTypeImpl(*nax); } -AppLoader_NAX::LoadResult AppLoader_NAX::Load(Kernel::Process& process) { +AppLoader_NAX::LoadResult AppLoader_NAX::Load(Kernel::Process& process, + [[maybe_unused]] Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } @@ -65,7 +66,7 @@ AppLoader_NAX::LoadResult AppLoader_NAX::Load(Kernel::Process& process) { return {nca_status, {}}; } - const auto result = nca_loader->Load(process); + const auto result = nca_loader->Load(process, system); if (result.first != ResultStatus::Success) { return result; } diff --git a/src/core/loader/nax.h b/src/core/loader/nax.h index eaec9bf58..c2b7722b5 100644 --- a/src/core/loader/nax.h +++ b/src/core/loader/nax.h @@ -8,10 +8,12 @@ #include "common/common_types.h" #include "core/loader/loader.h" -namespace FileSys { +namespace Core { +class System; +} +namespace FileSys { class NAX; - } // namespace FileSys namespace Loader { @@ -33,7 +35,7 @@ public: FileType GetFileType() const override; - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; u64 ReadRomFSIVFCOffset() const override; diff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cpp index 5a0469978..fa694de37 100644 --- a/src/core/loader/nca.cpp +++ b/src/core/loader/nca.cpp @@ -31,7 +31,7 @@ FileType AppLoader_NCA::IdentifyType(const FileSys::VirtualFile& file) { return FileType::Error; } -AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::Process& process) { +AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::Process& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } @@ -52,14 +52,14 @@ AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::Process& process) { directory_loader = std::make_unique<AppLoader_DeconstructedRomDirectory>(exefs, true); - const auto load_result = directory_loader->Load(process); + const auto load_result = directory_loader->Load(process, system); if (load_result.first != ResultStatus::Success) { return load_result; } if (nca->GetRomFS() != nullptr && nca->GetRomFS()->GetSize() > 0) { - Core::System::GetInstance().GetFileSystemController().RegisterRomFS( - std::make_unique<FileSys::RomFSFactory>(*this)); + system.GetFileSystemController().RegisterRomFS(std::make_unique<FileSys::RomFSFactory>( + *this, system.GetContentProvider(), system.GetFileSystemController())); } is_loaded = true; diff --git a/src/core/loader/nca.h b/src/core/loader/nca.h index e47dc0e47..711070294 100644 --- a/src/core/loader/nca.h +++ b/src/core/loader/nca.h @@ -8,6 +8,10 @@ #include "core/file_sys/vfs.h" #include "core/loader/loader.h" +namespace Core { +class System; +} + namespace FileSys { class NCA; } @@ -33,7 +37,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; u64 ReadRomFSIVFCOffset() const override; diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 906544bc9..9fb5eddad 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -208,7 +208,7 @@ bool AppLoader_NRO::LoadNro(Kernel::Process& process, const FileSys::VfsFile& fi return LoadNroImpl(process, file.ReadAllBytes(), file.GetName()); } -AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::Process& process) { +AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::Process& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } @@ -218,8 +218,8 @@ AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::Process& process) { } if (romfs != nullptr) { - Core::System::GetInstance().GetFileSystemController().RegisterRomFS( - std::make_unique<FileSys::RomFSFactory>(*this)); + system.GetFileSystemController().RegisterRomFS(std::make_unique<FileSys::RomFSFactory>( + *this, system.GetContentProvider(), system.GetFileSystemController())); } is_loaded = true; diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h index 4593d48fb..a2aab2ecc 100644 --- a/src/core/loader/nro.h +++ b/src/core/loader/nro.h @@ -10,6 +10,10 @@ #include "common/common_types.h" #include "core/loader/loader.h" +namespace Core { +class System; +} + namespace FileSys { class NACP; } @@ -37,7 +41,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; ResultStatus ReadIcon(std::vector<u8>& buffer) override; ResultStatus ReadProgramId(u64& out_program_id) override; diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 575330a86..60373cc5f 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -71,7 +71,7 @@ FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& file) { return FileType::NSO; } -std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, +std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, Core::System& system, const FileSys::VfsFile& file, VAddr load_base, bool should_pass_arguments, bool load_into_process, std::optional<FileSys::PatchManager> pm) { @@ -148,7 +148,6 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, // Apply cheats if they exist and the program has a valid title ID if (pm) { - auto& system = Core::System::GetInstance(); system.SetCurrentProcessBuildID(nso_header.build_id); const auto cheats = pm->CreateCheatList(system, nso_header.build_id); if (!cheats.empty()) { @@ -166,7 +165,7 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, return load_base + image_size; } -AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process) { +AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } @@ -175,7 +174,7 @@ AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process) { // Load module const VAddr base_address = process.PageTable().GetCodeRegionStart(); - if (!LoadModule(process, *file, base_address, true, true)) { + if (!LoadModule(process, system, *file, base_address, true, true)) { return {ResultStatus::ErrorLoadingNSO, {}}; } diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index b210830f0..4bd47787d 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -12,6 +12,10 @@ #include "core/file_sys/patch_manager.h" #include "core/loader/loader.h" +namespace Core { +class System; +} + namespace Kernel { class Process; } @@ -80,12 +84,12 @@ public: return IdentifyType(file); } - static std::optional<VAddr> LoadModule(Kernel::Process& process, const FileSys::VfsFile& file, - VAddr load_base, bool should_pass_arguments, - bool load_into_process, + static std::optional<VAddr> LoadModule(Kernel::Process& process, Core::System& system, + const FileSys::VfsFile& file, VAddr load_base, + bool should_pass_arguments, bool load_into_process, std::optional<FileSys::PatchManager> pm = {}); - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; ResultStatus ReadNSOModules(Modules& modules) override; diff --git a/src/core/loader/nsp.cpp b/src/core/loader/nsp.cpp index 13950fc08..15e528fa8 100644 --- a/src/core/loader/nsp.cpp +++ b/src/core/loader/nsp.cpp @@ -71,7 +71,7 @@ FileType AppLoader_NSP::IdentifyType(const FileSys::VirtualFile& file) { return FileType::Error; } -AppLoader_NSP::LoadResult AppLoader_NSP::Load(Kernel::Process& process) { +AppLoader_NSP::LoadResult AppLoader_NSP::Load(Kernel::Process& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } @@ -99,15 +99,14 @@ AppLoader_NSP::LoadResult AppLoader_NSP::Load(Kernel::Process& process) { return {ResultStatus::ErrorNSPMissingProgramNCA, {}}; } - const auto result = secondary_loader->Load(process); + const auto result = secondary_loader->Load(process, system); if (result.first != ResultStatus::Success) { return result; } FileSys::VirtualFile update_raw; if (ReadUpdateRaw(update_raw) == ResultStatus::Success && update_raw != nullptr) { - Core::System::GetInstance().GetFileSystemController().SetPackedUpdate( - std::move(update_raw)); + system.GetFileSystemController().SetPackedUpdate(std::move(update_raw)); } is_loaded = true; diff --git a/src/core/loader/nsp.h b/src/core/loader/nsp.h index 868b028d3..b27deb686 100644 --- a/src/core/loader/nsp.h +++ b/src/core/loader/nsp.h @@ -9,6 +9,10 @@ #include "core/file_sys/vfs.h" #include "core/loader/loader.h" +namespace Core { +class System; +} + namespace FileSys { class NACP; class NSP; @@ -35,7 +39,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& file) override; u64 ReadRomFSIVFCOffset() const override; diff --git a/src/core/loader/xci.cpp b/src/core/loader/xci.cpp index 7186ad1ff..25e83af0f 100644 --- a/src/core/loader/xci.cpp +++ b/src/core/loader/xci.cpp @@ -49,7 +49,7 @@ FileType AppLoader_XCI::IdentifyType(const FileSys::VirtualFile& file) { return FileType::Error; } -AppLoader_XCI::LoadResult AppLoader_XCI::Load(Kernel::Process& process) { +AppLoader_XCI::LoadResult AppLoader_XCI::Load(Kernel::Process& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } @@ -66,15 +66,14 @@ AppLoader_XCI::LoadResult AppLoader_XCI::Load(Kernel::Process& process) { return {ResultStatus::ErrorMissingProductionKeyFile, {}}; } - const auto result = nca_loader->Load(process); + const auto result = nca_loader->Load(process, system); if (result.first != ResultStatus::Success) { return result; } FileSys::VirtualFile update_raw; if (ReadUpdateRaw(update_raw) == ResultStatus::Success && update_raw != nullptr) { - Core::System::GetInstance().GetFileSystemController().SetPackedUpdate( - std::move(update_raw)); + system.GetFileSystemController().SetPackedUpdate(std::move(update_raw)); } is_loaded = true; diff --git a/src/core/loader/xci.h b/src/core/loader/xci.h index 618ae2f47..04aea286f 100644 --- a/src/core/loader/xci.h +++ b/src/core/loader/xci.h @@ -9,6 +9,10 @@ #include "core/file_sys/vfs.h" #include "core/loader/loader.h" +namespace Core { +class System; +} + namespace FileSys { class NACP; class XCI; @@ -35,7 +39,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process) override; + LoadResult Load(Kernel::Process& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& file) override; u64 ReadRomFSIVFCOffset() const override; diff --git a/src/core/memory/cheat_engine.cpp b/src/core/memory/cheat_engine.cpp index e503118dd..29284a42d 100644 --- a/src/core/memory/cheat_engine.cpp +++ b/src/core/memory/cheat_engine.cpp @@ -19,10 +19,24 @@ #include "core/memory/cheat_engine.h" namespace Core::Memory { - +namespace { constexpr auto CHEAT_ENGINE_NS = std::chrono::nanoseconds{1000000000 / 12}; constexpr u32 KEYPAD_BITMASK = 0x3FFFFFF; +std::string_view ExtractName(std::string_view data, std::size_t start_index, char match) { + auto end_index = start_index; + while (data[end_index] != match) { + ++end_index; + if (end_index > data.size() || + (end_index - start_index - 1) > sizeof(CheatDefinition::readable_name)) { + return {}; + } + } + + return data.substr(start_index, end_index - start_index); +} +} // Anonymous namespace + StandardVmCallbacks::StandardVmCallbacks(Core::System& system, const CheatProcessMetadata& metadata) : metadata(metadata), system(system) {} @@ -82,26 +96,9 @@ CheatParser::~CheatParser() = default; TextCheatParser::~TextCheatParser() = default; -namespace { -template <char match> -std::string_view ExtractName(std::string_view data, std::size_t start_index) { - auto end_index = start_index; - while (data[end_index] != match) { - ++end_index; - if (end_index > data.size() || - (end_index - start_index - 1) > sizeof(CheatDefinition::readable_name)) { - return {}; - } - } - - return data.substr(start_index, end_index - start_index); -} -} // Anonymous namespace - -std::vector<CheatEntry> TextCheatParser::Parse(const Core::System& system, - std::string_view data) const { +std::vector<CheatEntry> TextCheatParser::Parse(std::string_view data) const { std::vector<CheatEntry> out(1); - std::optional<u64> current_entry = std::nullopt; + std::optional<u64> current_entry; for (std::size_t i = 0; i < data.size(); ++i) { if (::isspace(data[i])) { @@ -115,7 +112,7 @@ std::vector<CheatEntry> TextCheatParser::Parse(const Core::System& system, return {}; } - const auto name = ExtractName<'}'>(data, i + 1); + const auto name = ExtractName(data, i + 1, '}'); if (name.empty()) { return {}; } @@ -132,7 +129,7 @@ std::vector<CheatEntry> TextCheatParser::Parse(const Core::System& system, current_entry = out.size(); out.emplace_back(); - const auto name = ExtractName<']'>(data, i + 1); + const auto name = ExtractName(data, i + 1, ']'); if (name.empty()) { return {}; } diff --git a/src/core/memory/cheat_engine.h b/src/core/memory/cheat_engine.h index fa039a831..a31002346 100644 --- a/src/core/memory/cheat_engine.h +++ b/src/core/memory/cheat_engine.h @@ -47,8 +47,7 @@ class CheatParser { public: virtual ~CheatParser(); - virtual std::vector<CheatEntry> Parse(const Core::System& system, - std::string_view data) const = 0; + [[nodiscard]] virtual std::vector<CheatEntry> Parse(std::string_view data) const = 0; }; // CheatParser implementation that parses text files @@ -56,7 +55,7 @@ class TextCheatParser final : public CheatParser { public: ~TextCheatParser() override; - std::vector<CheatEntry> Parse(const Core::System& system, std::string_view data) const override; + [[nodiscard]] std::vector<CheatEntry> Parse(std::string_view data) const override; }; // Class that encapsulates a CheatList and manages its interaction with memory and CoreTiming diff --git a/src/core/settings.h b/src/core/settings.h index 80f0d95a7..9834f44bb 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -152,6 +152,7 @@ struct Values { bool vibration_enabled; + bool motion_enabled; std::string motion_device; std::string touch_device; TouchscreenInput touchscreen; diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index c6c423c4b..c507c9891 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -4,7 +4,16 @@ #include <chrono> #include <thread> + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4200) // nonstandard extension used : zero-sized array in struct/union +#endif #include <libusb.h> +#ifdef _MSC_VER +#pragma warning(pop) +#endif + #include "common/logging/log.h" #include "input_common/gcadapter/gc_adapter.h" diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp index ea1a1cee6..062ec66b5 100644 --- a/src/input_common/main.cpp +++ b/src/input_common/main.cpp @@ -12,6 +12,7 @@ #include "input_common/main.h" #include "input_common/motion_emu.h" #include "input_common/touch_from_button.h" +#include "input_common/udp/client.h" #include "input_common/udp/udp.h" #ifdef HAVE_SDL2 #include "input_common/sdl/sdl.h" @@ -40,7 +41,11 @@ struct InputSubsystem::Impl { sdl = SDL::Init(); #endif - udp = CemuhookUDP::Init(); + udp = std::make_shared<InputCommon::CemuhookUDP::Client>(); + udpmotion = std::make_shared<UDPMotionFactory>(udp); + Input::RegisterFactory<Input::MotionDevice>("cemuhookudp", udpmotion); + udptouch = std::make_shared<UDPTouchFactory>(udp); + Input::RegisterFactory<Input::TouchDevice>("cemuhookudp", udptouch); } void Shutdown() { @@ -53,12 +58,17 @@ struct InputSubsystem::Impl { #ifdef HAVE_SDL2 sdl.reset(); #endif - udp.reset(); Input::UnregisterFactory<Input::ButtonDevice>("gcpad"); Input::UnregisterFactory<Input::AnalogDevice>("gcpad"); gcbuttons.reset(); gcanalog.reset(); + + Input::UnregisterFactory<Input::MotionDevice>("cemuhookudp"); + Input::UnregisterFactory<Input::TouchDevice>("cemuhookudp"); + + udpmotion.reset(); + udptouch.reset(); } [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const { @@ -109,14 +119,28 @@ struct InputSubsystem::Impl { return {}; } + [[nodiscard]] MotionMapping GetMotionMappingForDevice( + const Common::ParamPackage& params) const { + if (!params.Has("class") || params.Get("class", "") == "any") { + return {}; + } + if (params.Get("class", "") == "cemuhookudp") { + // TODO return the correct motion device + return {}; + } + return {}; + } + std::shared_ptr<Keyboard> keyboard; std::shared_ptr<MotionEmu> motion_emu; #ifdef HAVE_SDL2 std::unique_ptr<SDL::State> sdl; #endif - std::unique_ptr<CemuhookUDP::State> udp; std::shared_ptr<GCButtonFactory> gcbuttons; std::shared_ptr<GCAnalogFactory> gcanalog; + std::shared_ptr<UDPMotionFactory> udpmotion; + std::shared_ptr<UDPTouchFactory> udptouch; + std::shared_ptr<CemuhookUDP::Client> udp; }; InputSubsystem::InputSubsystem() : impl{std::make_unique<Impl>()} {} @@ -175,6 +199,22 @@ const GCButtonFactory* InputSubsystem::GetGCButtons() const { return impl->gcbuttons.get(); } +UDPMotionFactory* InputSubsystem::GetUDPMotions() { + return impl->udpmotion.get(); +} + +const UDPMotionFactory* InputSubsystem::GetUDPMotions() const { + return impl->udpmotion.get(); +} + +UDPTouchFactory* InputSubsystem::GetUDPTouch() { + return impl->udptouch.get(); +} + +const UDPTouchFactory* InputSubsystem::GetUDPTouch() const { + return impl->udptouch.get(); +} + void InputSubsystem::ReloadInputDevices() { if (!impl->udp) { return; diff --git a/src/input_common/main.h b/src/input_common/main.h index f3fbf696e..dded3f1ef 100644 --- a/src/input_common/main.h +++ b/src/input_common/main.h @@ -21,10 +21,14 @@ namespace Settings::NativeButton { enum Values : int; } +namespace Settings::NativeMotion { +enum Values : int; +} + namespace InputCommon { namespace Polling { -enum class DeviceType { Button, AnalogPreferred }; +enum class DeviceType { Button, AnalogPreferred, Motion }; /** * A class that can be used to get inputs from an input device like controllers without having to @@ -50,6 +54,8 @@ public: class GCAnalogFactory; class GCButtonFactory; +class UDPMotionFactory; +class UDPTouchFactory; class Keyboard; class MotionEmu; @@ -59,6 +65,7 @@ class MotionEmu; */ using AnalogMapping = std::unordered_map<Settings::NativeAnalog::Values, Common::ParamPackage>; using ButtonMapping = std::unordered_map<Settings::NativeButton::Values, Common::ParamPackage>; +using MotionMapping = std::unordered_map<Settings::NativeMotion::Values, Common::ParamPackage>; class InputSubsystem { public: @@ -103,6 +110,9 @@ public: /// Retrieves the button mappings for the given device. [[nodiscard]] ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& device) const; + /// Retrieves the motion mappings for the given device. + [[nodiscard]] MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& device) const; + /// Retrieves the underlying GameCube analog handler. [[nodiscard]] GCAnalogFactory* GetGCAnalogs(); @@ -115,6 +125,18 @@ public: /// Retrieves the underlying GameCube button handler. [[nodiscard]] const GCButtonFactory* GetGCButtons() const; + /// Retrieves the underlying udp motion handler. + [[nodiscard]] UDPMotionFactory* GetUDPMotions(); + + /// Retrieves the underlying udp motion handler. + [[nodiscard]] const UDPMotionFactory* GetUDPMotions() const; + + /// Retrieves the underlying udp touch handler. + [[nodiscard]] UDPTouchFactory* GetUDPTouch(); + + /// Retrieves the underlying udp touch handler. + [[nodiscard]] const UDPTouchFactory* GetUDPTouch() const; + /// Reloads the input devices void ReloadInputDevices(); diff --git a/src/input_common/motion_emu.cpp b/src/input_common/motion_emu.cpp index d4cdf76a3..69fd3c1d2 100644 --- a/src/input_common/motion_emu.cpp +++ b/src/input_common/motion_emu.cpp @@ -56,7 +56,7 @@ public: is_tilting = false; } - std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() { + Input::MotionStatus GetStatus() { std::lock_guard guard{status_mutex}; return status; } @@ -76,7 +76,7 @@ private: Common::Event shutdown_event; - std::tuple<Common::Vec3<float>, Common::Vec3<float>> status; + Input::MotionStatus status; std::mutex status_mutex; // Note: always keep the thread declaration at the end so that other objects are initialized @@ -113,10 +113,19 @@ private: gravity = QuaternionRotate(inv_q, gravity); angular_rate = QuaternionRotate(inv_q, angular_rate); + // TODO: Calculate the correct rotation vector and orientation matrix + const auto matrix4x4 = q.ToMatrix(); + const auto rotation = Common::MakeVec(0.0f, 0.0f, 0.0f); + const std::array orientation{ + Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]), + Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]), + Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10]), + }; + // Update the sensor state { std::lock_guard guard{status_mutex}; - status = std::make_tuple(gravity, angular_rate); + status = std::make_tuple(gravity, angular_rate, rotation, orientation); } } } @@ -131,7 +140,7 @@ public: device = std::make_shared<MotionEmuDevice>(update_millisecond, sensitivity); } - std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() const override { + Input::MotionStatus GetStatus() const override { return device->GetStatus(); } diff --git a/src/input_common/settings.cpp b/src/input_common/settings.cpp index 80c719cf4..b66c05856 100644 --- a/src/input_common/settings.cpp +++ b/src/input_common/settings.cpp @@ -14,6 +14,13 @@ const std::array<const char*, NumButtons> mapping = {{ }}; } +namespace NativeMotion { +const std::array<const char*, NumMotions> mapping = {{ + "motionleft", + "motionright", +}}; +} + namespace NativeAnalog { const std::array<const char*, NumAnalogs> mapping = {{ "lstick", diff --git a/src/input_common/settings.h b/src/input_common/settings.h index 2d258960b..ab0b95cf1 100644 --- a/src/input_common/settings.h +++ b/src/input_common/settings.h @@ -66,6 +66,21 @@ constexpr int NUM_STICKS_HID = NumAnalogs; extern const std::array<const char*, NumAnalogs> mapping; } // namespace NativeAnalog +namespace NativeMotion { +enum Values : int { + MOTIONLEFT, + MOTIONRIGHT, + + NumMotions, +}; + +constexpr int MOTION_HID_BEGIN = MOTIONLEFT; +constexpr int MOTION_HID_END = NumMotions; +constexpr int NUM_MOTION_HID = NumMotions; + +extern const std::array<const char*, NumMotions> mapping; +} // namespace NativeMotion + namespace NativeMouseButton { enum Values { Left, @@ -292,6 +307,7 @@ constexpr int NUM_KEYBOARD_MODS_HID = NumKeyboardMods; using ButtonsRaw = std::array<std::string, NativeButton::NumButtons>; using AnalogsRaw = std::array<std::string, NativeAnalog::NumAnalogs>; +using MotionRaw = std::array<std::string, NativeMotion::NumMotions>; using MouseButtonsRaw = std::array<std::string, NativeMouseButton::NumMouseButtons>; using KeyboardKeysRaw = std::array<std::string, NativeKeyboard::NumKeyboardKeys>; using KeyboardModsRaw = std::array<std::string, NativeKeyboard::NumKeyboardMods>; @@ -314,6 +330,7 @@ struct PlayerInput { ControllerType controller_type; ButtonsRaw buttons; AnalogsRaw analogs; + MotionRaw motions; std::string lstick_mod; std::string rstick_mod; diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index 3f4eaf448..2b6a68d4b 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp @@ -2,14 +2,13 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include <algorithm> -#include <array> #include <chrono> #include <cstring> #include <functional> #include <thread> #include <boost/asio.hpp> #include "common/logging/log.h" +#include "core/settings.h" #include "input_common/udp/client.h" #include "input_common/udp/protocol.h" @@ -131,21 +130,59 @@ static void SocketLoop(Socket* socket) { socket->Loop(); } -Client::Client(std::shared_ptr<DeviceStatus> status, const std::string& host, u16 port, - u8 pad_index, u32 client_id) - : status(std::move(status)) { - StartCommunication(host, port, pad_index, client_id); +Client::Client() { + LOG_INFO(Input, "Udp Initialization started"); + for (std::size_t client = 0; client < clients.size(); client++) { + u8 pad = client % 4; + StartCommunication(client, Settings::values.udp_input_address, + Settings::values.udp_input_port, pad, 24872); + // Set motion parameters + // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode + // Real HW values are unknown, 0.0001 is an approximate to Standard + clients[client].motion.SetGyroThreshold(0.0001f); + } } Client::~Client() { - socket->Stop(); - thread.join(); + Reset(); +} + +std::vector<Common::ParamPackage> Client::GetInputDevices() const { + std::vector<Common::ParamPackage> devices; + for (std::size_t client = 0; client < clients.size(); client++) { + if (!DeviceConnected(client)) { + continue; + } + std::string name = fmt::format("UDP Controller {}", client); + devices.emplace_back(Common::ParamPackage{ + {"class", "cemuhookudp"}, + {"display", std::move(name)}, + {"port", std::to_string(client)}, + }); + } + return devices; } +bool Client::DeviceConnected(std::size_t pad) const { + // Use last timestamp to detect if the socket has stopped sending data + const auto now = std::chrono::system_clock::now(); + u64 time_difference = + std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update) + .count(); + return time_difference < 1000 && clients[pad].active == 1; +} + +void Client::ReloadUDPClient() { + for (std::size_t client = 0; client < clients.size(); client++) { + ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client); + } +} void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { - socket->Stop(); - thread.join(); - StartCommunication(host, port, pad_index, client_id); + // client number must be determined from host / port and pad index + std::size_t client = pad_index; + clients[client].socket->Stop(); + clients[client].thread.join(); + StartCommunication(client, host, port, pad_index, client_id); } void Client::OnVersion(Response::Version data) { @@ -157,23 +194,39 @@ void Client::OnPortInfo(Response::PortInfo data) { } void Client::OnPadData(Response::PadData data) { + // client number must be determined from host / port and pad index + std::size_t client = data.info.id; LOG_TRACE(Input, "PadData packet received"); - if (data.packet_counter <= packet_sequence) { + if (data.packet_counter == clients[client].packet_sequence) { LOG_WARNING( Input, "PadData packet dropped because its stale info. Current count: {} Packet count: {}", - packet_sequence, data.packet_counter); + clients[client].packet_sequence, data.packet_counter); return; } - packet_sequence = data.packet_counter; - // TODO: Check how the Switch handles motions and how the CemuhookUDP motion - // directions correspond to the ones of the Switch - Common::Vec3f accel = Common::MakeVec<float>(data.accel.x, data.accel.y, data.accel.z); - Common::Vec3f gyro = Common::MakeVec<float>(data.gyro.pitch, data.gyro.yaw, data.gyro.roll); - { - std::lock_guard guard(status->update_mutex); + clients[client].active = data.info.is_pad_active; + clients[client].packet_sequence = data.packet_counter; + const auto now = std::chrono::system_clock::now(); + u64 time_difference = std::chrono::duration_cast<std::chrono::microseconds>( + now - clients[client].last_motion_update) + .count(); + clients[client].last_motion_update = now; + Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; + clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y}); + // Gyroscope values are not it the correct scale from better joy. + // Dividing by 312 allows us to make one full turn = 1 turn + // This must be a configurable valued called sensitivity + clients[client].motion.SetGyroscope(raw_gyroscope / 312.0f); + clients[client].motion.UpdateRotation(time_difference); + clients[client].motion.UpdateOrientation(time_difference); + Common::Vec3f gyroscope = clients[client].motion.GetGyroscope(); + Common::Vec3f accelerometer = clients[client].motion.GetAcceleration(); + Common::Vec3f rotation = clients[client].motion.GetRotations(); + std::array<Common::Vec3f, 3> orientation = clients[client].motion.GetOrientation(); - status->motion_status = {accel, gyro}; + { + std::lock_guard guard(clients[client].status.update_mutex); + clients[client].status.motion_status = {accelerometer, gyroscope, rotation, orientation}; // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates // between a simple "tap" and a hard press that causes the touch screen to click. @@ -182,11 +235,11 @@ void Client::OnPadData(Response::PadData data) { float x = 0; float y = 0; - if (is_active && status->touch_calibration) { - const u16 min_x = status->touch_calibration->min_x; - const u16 max_x = status->touch_calibration->max_x; - const u16 min_y = status->touch_calibration->min_y; - const u16 max_y = status->touch_calibration->max_y; + if (is_active && clients[client].status.touch_calibration) { + const u16 min_x = clients[client].status.touch_calibration->min_x; + const u16 max_x = clients[client].status.touch_calibration->max_x; + const u16 min_y = clients[client].status.touch_calibration->min_y; + const u16 max_y = clients[client].status.touch_calibration->max_y; x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / static_cast<float>(max_x - min_x); @@ -194,17 +247,80 @@ void Client::OnPadData(Response::PadData data) { static_cast<float>(max_y - min_y); } - status->touch_status = {x, y, is_active}; + clients[client].status.touch_status = {x, y, is_active}; + + if (configuring) { + UpdateYuzuSettings(client, accelerometer, gyroscope, is_active); + } } } -void Client::StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id) { +void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, + u32 client_id) { SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, [this](Response::PortInfo info) { OnPortInfo(info); }, [this](Response::PadData data) { OnPadData(data); }}; LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); - socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); - thread = std::thread{SocketLoop, this->socket.get()}; + clients[client].socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); + clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; +} + +void Client::Reset() { + for (std::size_t client = 0; client < clients.size(); client++) { + clients[client].socket->Stop(); + clients[client].thread.join(); + } +} + +void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, + const Common::Vec3<float>& gyro, bool touch) { + UDPPadStatus pad; + if (touch) { + pad.touch = PadTouch::Click; + pad_queue[client].Push(pad); + } + for (size_t i = 0; i < 3; ++i) { + if (gyro[i] > 6.0f || gyro[i] < -6.0f) { + pad.motion = static_cast<PadMotion>(i); + pad.motion_value = gyro[i]; + pad_queue[client].Push(pad); + } + if (acc[i] > 2.0f || acc[i] < -2.0f) { + pad.motion = static_cast<PadMotion>(i + 3); + pad.motion_value = acc[i]; + pad_queue[client].Push(pad); + } + } +} + +void Client::BeginConfiguration() { + for (auto& pq : pad_queue) { + pq.Clear(); + } + configuring = true; +} + +void Client::EndConfiguration() { + for (auto& pq : pad_queue) { + pq.Clear(); + } + configuring = false; +} + +DeviceStatus& Client::GetPadState(std::size_t pad) { + return clients[pad].status; +} + +const DeviceStatus& Client::GetPadState(std::size_t pad) const { + return clients[pad].status; +} + +std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() { + return pad_queue; +} + +const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() const { + return pad_queue; } void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h index b8c654755..523dc6a7a 100644 --- a/src/input_common/udp/client.h +++ b/src/input_common/udp/client.h @@ -12,8 +12,12 @@ #include <thread> #include <tuple> #include "common/common_types.h" +#include "common/param_package.h" #include "common/thread.h" +#include "common/threadsafe_queue.h" #include "common/vector_math.h" +#include "core/frontend/input.h" +#include "input_common/motion_input.h" namespace InputCommon::CemuhookUDP { @@ -28,9 +32,30 @@ struct PortInfo; struct Version; } // namespace Response +enum class PadMotion { + GyroX, + GyroY, + GyroZ, + AccX, + AccY, + AccZ, + Undefined, +}; + +enum class PadTouch { + Click, + Undefined, +}; + +struct UDPPadStatus { + PadTouch touch{PadTouch::Undefined}; + PadMotion motion{PadMotion::Undefined}; + f32 motion_value{0.0f}; +}; + struct DeviceStatus { std::mutex update_mutex; - std::tuple<Common::Vec3<float>, Common::Vec3<float>> motion_status; + Input::MotionStatus motion_status; std::tuple<float, float, bool> touch_status; // calibration data for scaling the device's touch area to 3ds @@ -45,22 +70,58 @@ struct DeviceStatus { class Client { public: - explicit Client(std::shared_ptr<DeviceStatus> status, const std::string& host = DEFAULT_ADDR, - u16 port = DEFAULT_PORT, u8 pad_index = 0, u32 client_id = 24872); + // Initialize the UDP client capture and read sequence + Client(); + + // Close and release the client ~Client(); + + // Used for polling + void BeginConfiguration(); + void EndConfiguration(); + + std::vector<Common::ParamPackage> GetInputDevices() const; + + bool DeviceConnected(std::size_t pad) const; + void ReloadUDPClient(); void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0, u32 client_id = 24872); + std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue(); + const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue() const; + + DeviceStatus& GetPadState(std::size_t pad); + const DeviceStatus& GetPadState(std::size_t pad) const; + private: + struct ClientData { + std::unique_ptr<Socket> socket; + DeviceStatus status; + std::thread thread; + u64 packet_sequence = 0; + u8 active; + + // Realtime values + // motion is initalized with PID values for drift correction on joycons + InputCommon::MotionInput motion{0.3f, 0.005f, 0.0f}; + std::chrono::time_point<std::chrono::system_clock> last_motion_update; + }; + + // For shutting down, clear all data, join all threads, release usb + void Reset(); + void OnVersion(Response::Version); void OnPortInfo(Response::PortInfo); void OnPadData(Response::PadData); - void StartCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id); + void StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, + u32 client_id); + void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, + const Common::Vec3<float>& gyro, bool touch); + + bool configuring = false; - std::unique_ptr<Socket> socket; - std::shared_ptr<DeviceStatus> status; - std::thread thread; - u64 packet_sequence = 0; + std::array<ClientData, 4> clients; + std::array<Common::SPSCQueue<UDPPadStatus>, 4> pad_queue; }; /// An async job allowing configuration of the touchpad calibration. diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp index 4b347e47e..eba077a36 100644 --- a/src/input_common/udp/udp.cpp +++ b/src/input_common/udp/udp.cpp @@ -1,105 +1,144 @@ -// Copyright 2018 Citra Emulator Project +// Copyright 2020 yuzu Emulator Project // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <atomic> +#include <list> #include <mutex> -#include <optional> -#include <tuple> - -#include "common/param_package.h" -#include "core/frontend/input.h" -#include "core/settings.h" +#include <utility> +#include "common/assert.h" +#include "common/threadsafe_queue.h" #include "input_common/udp/client.h" #include "input_common/udp/udp.h" -namespace InputCommon::CemuhookUDP { +namespace InputCommon { -class UDPTouchDevice final : public Input::TouchDevice { +class UDPMotion final : public Input::MotionDevice { public: - explicit UDPTouchDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} - std::tuple<float, float, bool> GetStatus() const override { - std::lock_guard guard(status->update_mutex); - return status->touch_status; + UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) + : ip(ip_), port(port_), pad(pad_), client(client_) {} + + Input::MotionStatus GetStatus() const override { + return client->GetPadState(pad).motion_status; } private: - std::shared_ptr<DeviceStatus> status; + const std::string ip; + const int port; + const int pad; + CemuhookUDP::Client* client; + mutable std::mutex mutex; }; -class UDPMotionDevice final : public Input::MotionDevice { -public: - explicit UDPMotionDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} - std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() const override { - std::lock_guard guard(status->update_mutex); - return status->motion_status; - } +/// A motion device factory that creates motion devices from JC Adapter +UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_) + : client(std::move(client_)) {} + +/** + * Creates motion device + * @param params contains parameters for creating the device: + * - "port": the nth jcpad on the adapter + */ +std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) { + const std::string ip = params.Get("ip", "127.0.0.1"); + const int port = params.Get("port", 26760); + const int pad = params.Get("pad_index", 0); + + return std::make_unique<UDPMotion>(ip, port, pad, client.get()); +} -private: - std::shared_ptr<DeviceStatus> status; -}; +void UDPMotionFactory::BeginConfiguration() { + polling = true; + client->BeginConfiguration(); +} -class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> { -public: - explicit UDPTouchFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} - - std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override { - { - std::lock_guard guard(status->update_mutex); - status->touch_calibration = DeviceStatus::CalibrationData{}; - // These default values work well for DS4 but probably not other touch inputs - status->touch_calibration->min_x = params.Get("min_x", 100); - status->touch_calibration->min_y = params.Get("min_y", 50); - status->touch_calibration->max_x = params.Get("max_x", 1800); - status->touch_calibration->max_y = params.Get("max_y", 850); +void UDPMotionFactory::EndConfiguration() { + polling = false; + client->EndConfiguration(); +} + +Common::ParamPackage UDPMotionFactory::GetNextInput() { + Common::ParamPackage params; + CemuhookUDP::UDPPadStatus pad; + auto& queue = client->GetPadQueue(); + for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) { + while (queue[pad_number].Pop(pad)) { + if (pad.motion == CemuhookUDP::PadMotion::Undefined || std::abs(pad.motion_value) < 1) { + continue; + } + params.Set("engine", "cemuhookudp"); + params.Set("ip", "127.0.0.1"); + params.Set("port", 26760); + params.Set("pad_index", static_cast<int>(pad_number)); + params.Set("motion", static_cast<u16>(pad.motion)); + return params; } - return std::make_unique<UDPTouchDevice>(status); } + return params; +} -private: - std::shared_ptr<DeviceStatus> status; -}; - -class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> { +class UDPTouch final : public Input::TouchDevice { public: - explicit UDPMotionFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {} + UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) + : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} - std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { - return std::make_unique<UDPMotionDevice>(status); + std::tuple<float, float, bool> GetStatus() const override { + return client->GetPadState(pad).touch_status; } private: - std::shared_ptr<DeviceStatus> status; + const std::string ip; + const int port; + const int pad; + CemuhookUDP::Client* client; + mutable std::mutex mutex; }; -State::State() { - auto status = std::make_shared<DeviceStatus>(); - client = - std::make_unique<Client>(status, Settings::values.udp_input_address, - Settings::values.udp_input_port, Settings::values.udp_pad_index); - - motion_factory = std::make_shared<UDPMotionFactory>(status); - touch_factory = std::make_shared<UDPTouchFactory>(status); - - Input::RegisterFactory<Input::MotionDevice>("cemuhookudp", motion_factory); - Input::RegisterFactory<Input::TouchDevice>("cemuhookudp", touch_factory); +/// A motion device factory that creates motion devices from JC Adapter +UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_) + : client(std::move(client_)) {} + +/** + * Creates motion device + * @param params contains parameters for creating the device: + * - "port": the nth jcpad on the adapter + */ +std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) { + const std::string ip = params.Get("ip", "127.0.0.1"); + const int port = params.Get("port", 26760); + const int pad = params.Get("pad_index", 0); + + return std::make_unique<UDPTouch>(ip, port, pad, client.get()); } -State::~State() { - Input::UnregisterFactory<Input::TouchDevice>("cemuhookudp"); - Input::UnregisterFactory<Input::MotionDevice>("cemuhookudp"); +void UDPTouchFactory::BeginConfiguration() { + polling = true; + client->BeginConfiguration(); } -std::vector<Common::ParamPackage> State::GetInputDevices() const { - // TODO support binding udp devices - return {}; +void UDPTouchFactory::EndConfiguration() { + polling = false; + client->EndConfiguration(); } -void State::ReloadUDPClient() { - client->ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, - Settings::values.udp_pad_index); +Common::ParamPackage UDPTouchFactory::GetNextInput() { + Common::ParamPackage params; + CemuhookUDP::UDPPadStatus pad; + auto& queue = client->GetPadQueue(); + for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) { + while (queue[pad_number].Pop(pad)) { + if (pad.touch == CemuhookUDP::PadTouch::Undefined) { + continue; + } + params.Set("engine", "cemuhookudp"); + params.Set("ip", "127.0.0.1"); + params.Set("port", 26760); + params.Set("pad_index", static_cast<int>(pad_number)); + params.Set("touch", static_cast<u16>(pad.touch)); + return params; + } + } + return params; } -std::unique_ptr<State> Init() { - return std::make_unique<State>(); -} -} // namespace InputCommon::CemuhookUDP +} // namespace InputCommon diff --git a/src/input_common/udp/udp.h b/src/input_common/udp/udp.h index 672a5c812..ea3fd4175 100644 --- a/src/input_common/udp/udp.h +++ b/src/input_common/udp/udp.h @@ -1,32 +1,57 @@ -// Copyright 2018 Citra Emulator Project +// Copyright 2020 yuzu Emulator Project // Licensed under GPLv2 or any later version // Refer to the license.txt file included. #pragma once #include <memory> -#include <vector> -#include "common/param_package.h" +#include "core/frontend/input.h" +#include "input_common/udp/client.h" -namespace InputCommon::CemuhookUDP { +namespace InputCommon { -class Client; -class UDPMotionFactory; -class UDPTouchFactory; - -class State { +/// A motion device factory that creates motion devices from udp clients +class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> { public: - State(); - ~State(); - void ReloadUDPClient(); - std::vector<Common::ParamPackage> GetInputDevices() const; + explicit UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_); + + std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override; + + Common::ParamPackage GetNextInput(); + + /// For device input configuration/polling + void BeginConfiguration(); + void EndConfiguration(); + + bool IsPolling() const { + return polling; + } private: - std::unique_ptr<Client> client; - std::shared_ptr<UDPMotionFactory> motion_factory; - std::shared_ptr<UDPTouchFactory> touch_factory; + std::shared_ptr<CemuhookUDP::Client> client; + bool polling = false; }; -std::unique_ptr<State> Init(); +/// A touch device factory that creates touch devices from udp clients +class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> { +public: + explicit UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_); + + std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override; + + Common::ParamPackage GetNextInput(); + + /// For device input configuration/polling + void BeginConfiguration(); + void EndConfiguration(); + + bool IsPolling() const { + return polling; + } + +private: + std::shared_ptr<CemuhookUDP::Client> client; + bool polling = false; +}; -} // namespace InputCommon::CemuhookUDP +} // namespace InputCommon diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index d85f1e9d1..f52b55ef3 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt @@ -269,5 +269,5 @@ endif() if (MSVC) target_compile_options(video_core PRIVATE /we4267) else() - target_compile_options(video_core PRIVATE -Werror=conversion -Wno-error=sign-conversion) + target_compile_options(video_core PRIVATE -Werror=conversion -Wno-error=sign-conversion -Werror=switch) endif() diff --git a/src/video_core/engines/fermi_2d.cpp b/src/video_core/engines/fermi_2d.cpp index 6e50661a3..9409c4075 100644 --- a/src/video_core/engines/fermi_2d.cpp +++ b/src/video_core/engines/fermi_2d.cpp @@ -87,12 +87,12 @@ void Fermi2D::HandleSurfaceCopy() { const Common::Rectangle<u32> src_rect{src_blit_x1, src_blit_y1, src_blit_x2, src_blit_y2}; const Common::Rectangle<u32> dst_rect{regs.blit_dst_x, regs.blit_dst_y, dst_blit_x2, dst_blit_y2}; - Config copy_config; - copy_config.operation = regs.operation; - copy_config.filter = regs.blit_control.filter; - copy_config.src_rect = src_rect; - copy_config.dst_rect = dst_rect; - + const Config copy_config{ + .operation = regs.operation, + .filter = regs.blit_control.filter, + .src_rect = src_rect, + .dst_rect = dst_rect, + }; if (!rasterizer->AccelerateSurfaceCopy(regs.src, regs.dst, copy_config)) { UNIMPLEMENTED(); } diff --git a/src/video_core/engines/fermi_2d.h b/src/video_core/engines/fermi_2d.h index 213abfaae..0909709ec 100644 --- a/src/video_core/engines/fermi_2d.h +++ b/src/video_core/engines/fermi_2d.h @@ -145,8 +145,8 @@ public: } regs{}; struct Config { - Operation operation; - Filter filter; + Operation operation{}; + Filter filter{}; Common::Rectangle<u32> src_rect; Common::Rectangle<u32> dst_rect; }; diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp index 3f75fcd2b..ce3a65122 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp @@ -1443,8 +1443,10 @@ private: return expr + ", vec2(0.0), vec2(0.0))"; case TextureType::TextureCube: return expr + ", vec3(0.0), vec3(0.0))"; + default: + UNREACHABLE(); + break; } - UNREACHABLE(); } for (const auto& variant : extras) { diff --git a/src/video_core/renderer_opengl/maxwell_to_gl.h b/src/video_core/renderer_opengl/maxwell_to_gl.h index fe9bd4b5a..a8be2aa37 100644 --- a/src/video_core/renderer_opengl/maxwell_to_gl.h +++ b/src/video_core/renderer_opengl/maxwell_to_gl.h @@ -47,6 +47,8 @@ inline GLenum VertexFormat(Maxwell::VertexAttribute attrib) { return GL_UNSIGNED_INT; case Maxwell::VertexAttribute::Size::Size_10_10_10_2: return GL_UNSIGNED_INT_2_10_10_10_REV; + default: + break; } break; case Maxwell::VertexAttribute::Type::SignedNorm: @@ -70,6 +72,8 @@ inline GLenum VertexFormat(Maxwell::VertexAttribute attrib) { return GL_INT; case Maxwell::VertexAttribute::Size::Size_10_10_10_2: return GL_INT_2_10_10_10_REV; + default: + break; } break; case Maxwell::VertexAttribute::Type::Float: @@ -84,6 +88,8 @@ inline GLenum VertexFormat(Maxwell::VertexAttribute attrib) { case Maxwell::VertexAttribute::Size::Size_32_32_32: case Maxwell::VertexAttribute::Size::Size_32_32_32_32: return GL_FLOAT; + default: + break; } break; } diff --git a/src/video_core/renderer_vulkan/maxwell_to_vk.cpp b/src/video_core/renderer_vulkan/maxwell_to_vk.cpp index f8c77f4fa..d22de1d81 100644 --- a/src/video_core/renderer_vulkan/maxwell_to_vk.cpp +++ b/src/video_core/renderer_vulkan/maxwell_to_vk.cpp @@ -78,9 +78,10 @@ VkSamplerAddressMode WrapMode(const VKDevice& device, Tegra::Texture::WrapMode w case Tegra::Texture::WrapMode::MirrorOnceBorder: UNIMPLEMENTED(); return VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE; + default: + UNIMPLEMENTED_MSG("Unimplemented wrap mode={}", static_cast<u32>(wrap_mode)); + return {}; } - UNIMPLEMENTED_MSG("Unimplemented wrap mode={}", static_cast<u32>(wrap_mode)); - return {}; } VkCompareOp DepthCompareFunction(Tegra::Texture::DepthCompareFunc depth_compare_func) { @@ -298,9 +299,10 @@ VkPrimitiveTopology PrimitiveTopology([[maybe_unused]] const VKDevice& device, return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; case Maxwell::PrimitiveTopology::Patches: return VK_PRIMITIVE_TOPOLOGY_PATCH_LIST; + default: + UNIMPLEMENTED_MSG("Unimplemented topology={}", static_cast<u32>(topology)); + return {}; } - UNIMPLEMENTED_MSG("Unimplemented topology={}", static_cast<u32>(topology)); - return {}; } VkFormat VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttribute::Size size) { @@ -325,6 +327,8 @@ VkFormat VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttrib return VK_FORMAT_R16G16B16A16_UNORM; case Maxwell::VertexAttribute::Size::Size_10_10_10_2: return VK_FORMAT_A2B10G10R10_UNORM_PACK32; + default: + break; } break; case Maxwell::VertexAttribute::Type::SignedNorm: @@ -347,6 +351,8 @@ VkFormat VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttrib return VK_FORMAT_R16G16B16A16_SNORM; case Maxwell::VertexAttribute::Size::Size_10_10_10_2: return VK_FORMAT_A2B10G10R10_SNORM_PACK32; + default: + break; } break; case Maxwell::VertexAttribute::Type::UnsignedScaled: @@ -369,6 +375,8 @@ VkFormat VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttrib return VK_FORMAT_R16G16B16A16_USCALED; case Maxwell::VertexAttribute::Size::Size_10_10_10_2: return VK_FORMAT_A2B10G10R10_USCALED_PACK32; + default: + break; } break; case Maxwell::VertexAttribute::Type::SignedScaled: @@ -391,6 +399,8 @@ VkFormat VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttrib return VK_FORMAT_R16G16B16A16_SSCALED; case Maxwell::VertexAttribute::Size::Size_10_10_10_2: return VK_FORMAT_A2B10G10R10_SSCALED_PACK32; + default: + break; } break; case Maxwell::VertexAttribute::Type::UnsignedInt: @@ -421,6 +431,8 @@ VkFormat VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttrib return VK_FORMAT_R32G32B32A32_UINT; case Maxwell::VertexAttribute::Size::Size_10_10_10_2: return VK_FORMAT_A2B10G10R10_UINT_PACK32; + default: + break; } break; case Maxwell::VertexAttribute::Type::SignedInt: @@ -451,6 +463,8 @@ VkFormat VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttrib return VK_FORMAT_R32G32B32A32_SINT; case Maxwell::VertexAttribute::Size::Size_10_10_10_2: return VK_FORMAT_A2B10G10R10_SINT_PACK32; + default: + break; } break; case Maxwell::VertexAttribute::Type::Float: @@ -471,6 +485,8 @@ VkFormat VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttrib return VK_FORMAT_R32G32B32_SFLOAT; case Maxwell::VertexAttribute::Size::Size_32_32_32_32: return VK_FORMAT_R32G32B32A32_SFLOAT; + default: + break; } break; } diff --git a/src/video_core/renderer_vulkan/wrapper.cpp b/src/video_core/renderer_vulkan/wrapper.cpp index 013865aa4..fe291a148 100644 --- a/src/video_core/renderer_vulkan/wrapper.cpp +++ b/src/video_core/renderer_vulkan/wrapper.cpp @@ -262,6 +262,22 @@ const char* ToString(VkResult result) noexcept { return "VK_ERROR_INVALID_DEVICE_ADDRESS_EXT"; case VkResult::VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT: return "VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT"; + case VkResult::VK_ERROR_UNKNOWN: + return "VK_ERROR_UNKNOWN"; + case VkResult::VK_ERROR_INCOMPATIBLE_VERSION_KHR: + return "VK_ERROR_INCOMPATIBLE_VERSION_KHR"; + case VkResult::VK_THREAD_IDLE_KHR: + return "VK_THREAD_IDLE_KHR"; + case VkResult::VK_THREAD_DONE_KHR: + return "VK_THREAD_DONE_KHR"; + case VkResult::VK_OPERATION_DEFERRED_KHR: + return "VK_OPERATION_DEFERRED_KHR"; + case VkResult::VK_OPERATION_NOT_DEFERRED_KHR: + return "VK_OPERATION_NOT_DEFERRED_KHR"; + case VkResult::VK_PIPELINE_COMPILE_REQUIRED_EXT: + return "VK_PIPELINE_COMPILE_REQUIRED_EXT"; + case VkResult::VK_RESULT_MAX_ENUM: + return "VK_RESULT_MAX_ENUM"; } return "Unknown"; } diff --git a/src/video_core/shader/decode/arithmetic_half.cpp b/src/video_core/shader/decode/arithmetic_half.cpp index a276aee44..88103fede 100644 --- a/src/video_core/shader/decode/arithmetic_half.cpp +++ b/src/video_core/shader/decode/arithmetic_half.cpp @@ -53,6 +53,9 @@ u32 ShaderIR::DecodeArithmeticHalf(NodeBlock& bb, u32 pc) { absolute_a = ((instr.value >> 44) & 1) != 0; absolute_b = ((instr.value >> 54) & 1) != 0; break; + default: + UNREACHABLE(); + break; } Node op_a = UnpackHalfFloat(GetRegister(instr.gpr8), instr.alu_half.type_a); diff --git a/src/video_core/shader/decode/image.cpp b/src/video_core/shader/decode/image.cpp index e75ca4fdb..618d309d2 100644 --- a/src/video_core/shader/decode/image.cpp +++ b/src/video_core/shader/decode/image.cpp @@ -119,6 +119,8 @@ ComponentType GetComponentType(Tegra::Engines::SamplerDescriptor descriptor, return descriptor.r_type; } break; + default: + break; } UNIMPLEMENTED_MSG("Texture format not implemented={}", format); return ComponentType::FLOAT; @@ -220,9 +222,10 @@ u32 GetComponentSize(TextureFormat format, std::size_t component) { return (component == 0 || component == 1) ? 8 : 0; case TextureFormat::G4R4: return (component == 0 || component == 1) ? 4 : 0; + default: + UNIMPLEMENTED_MSG("Texture format not implemented={}", format); + return 0; } - UNIMPLEMENTED_MSG("Texture format not implemented={}", format); - return 0; } std::size_t GetImageComponentMask(TextureFormat format) { @@ -257,9 +260,10 @@ std::size_t GetImageComponentMask(TextureFormat format) { case TextureFormat::R8: case TextureFormat::R1: return std::size_t{R}; + default: + UNIMPLEMENTED_MSG("Texture format not implemented={}", format); + return std::size_t{R | G | B | A}; } - UNIMPLEMENTED_MSG("Texture format not implemented={}", format); - return std::size_t{R | G | B | A}; } std::size_t GetImageTypeNumCoordinates(Tegra::Shader::ImageType image_type) { @@ -463,7 +467,10 @@ u32 ShaderIR::DecodeImage(NodeBlock& bb, u32 pc) { return OperationCode::AtomicImageXor; case Tegra::Shader::ImageAtomicOperation::Exch: return OperationCode::AtomicImageExchange; + default: + break; } + break; default: break; } diff --git a/src/video_core/shader/decode/texture.cpp b/src/video_core/shader/decode/texture.cpp index 29ebf65ba..a03b50e39 100644 --- a/src/video_core/shader/decode/texture.cpp +++ b/src/video_core/shader/decode/texture.cpp @@ -763,7 +763,7 @@ Node4 ShaderIR::GetTld4Code(Instruction instr, TextureType texture_type, bool de Node4 ShaderIR::GetTldCode(Tegra::Shader::Instruction instr) { const auto texture_type{instr.tld.texture_type}; - const bool is_array{instr.tld.is_array}; + const bool is_array{instr.tld.is_array != 0}; const bool lod_enabled{instr.tld.GetTextureProcessMode() == TextureProcessMode::LL}; const std::size_t coord_count{GetCoordCount(texture_type)}; diff --git a/src/yuzu/CMakeLists.txt b/src/yuzu/CMakeLists.txt index 3ea4e5601..cc0291b15 100644 --- a/src/yuzu/CMakeLists.txt +++ b/src/yuzu/CMakeLists.txt @@ -9,6 +9,9 @@ add_executable(yuzu about_dialog.cpp about_dialog.h aboutdialog.ui + applets/controller.cpp + applets/controller.h + applets/controller.ui applets/error.cpp applets/error.h applets/profile_select.cpp @@ -62,12 +65,15 @@ add_executable(yuzu configuration/configure_input.cpp configuration/configure_input.h configuration/configure_input.ui - configuration/configure_input_player.cpp - configuration/configure_input_player.h - configuration/configure_input_player.ui configuration/configure_input_advanced.cpp configuration/configure_input_advanced.h configuration/configure_input_advanced.ui + configuration/configure_input_dialog.cpp + configuration/configure_input_dialog.h + configuration/configure_input_dialog.ui + configuration/configure_input_player.cpp + configuration/configure_input_player.h + configuration/configure_input_player.ui configuration/configure_motion_touch.cpp configuration/configure_motion_touch.h configuration/configure_motion_touch.ui diff --git a/src/yuzu/applets/controller.cpp b/src/yuzu/applets/controller.cpp new file mode 100644 index 000000000..9d45f2a01 --- /dev/null +++ b/src/yuzu/applets/controller.cpp @@ -0,0 +1,601 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <algorithm> + +#include "common/assert.h" +#include "common/string_util.h" +#include "core/core.h" +#include "core/hle/lock.h" +#include "core/hle/service/hid/controllers/npad.h" +#include "core/hle/service/hid/hid.h" +#include "core/hle/service/sm/sm.h" +#include "ui_controller.h" +#include "yuzu/applets/controller.h" +#include "yuzu/configuration/configure_input_dialog.h" +#include "yuzu/main.h" + +namespace { + +constexpr std::array<std::array<bool, 4>, 8> led_patterns = {{ + {1, 0, 0, 0}, + {1, 1, 0, 0}, + {1, 1, 1, 0}, + {1, 1, 1, 1}, + {1, 0, 0, 1}, + {1, 0, 1, 0}, + {1, 0, 1, 1}, + {0, 1, 1, 0}, +}}; + +void UpdateController(Settings::ControllerType controller_type, std::size_t npad_index, + bool connected) { + Core::System& system{Core::System::GetInstance()}; + + if (!system.IsPoweredOn()) { + return; + } + + Service::SM::ServiceManager& sm = system.ServiceManager(); + + auto& npad = + sm.GetService<Service::HID::Hid>("hid") + ->GetAppletResource() + ->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad); + + npad.UpdateControllerAt(npad.MapSettingsTypeToNPad(controller_type), npad_index, connected); +} + +// Returns true if the given controller type is compatible with the given parameters. +bool IsControllerCompatible(Settings::ControllerType controller_type, + Core::Frontend::ControllerParameters parameters) { + switch (controller_type) { + case Settings::ControllerType::ProController: + return parameters.allow_pro_controller; + case Settings::ControllerType::DualJoyconDetached: + return parameters.allow_dual_joycons; + case Settings::ControllerType::LeftJoycon: + return parameters.allow_left_joycon; + case Settings::ControllerType::RightJoycon: + return parameters.allow_right_joycon; + case Settings::ControllerType::Handheld: + return parameters.enable_single_mode && parameters.allow_handheld; + default: + return false; + } +} + +/// Maps the controller type combobox index to Controller Type enum +constexpr Settings::ControllerType GetControllerTypeFromIndex(int index) { + switch (index) { + case 0: + default: + return Settings::ControllerType::ProController; + case 1: + return Settings::ControllerType::DualJoyconDetached; + case 2: + return Settings::ControllerType::LeftJoycon; + case 3: + return Settings::ControllerType::RightJoycon; + case 4: + return Settings::ControllerType::Handheld; + } +} + +/// Maps the Controller Type enum to controller type combobox index +constexpr int GetIndexFromControllerType(Settings::ControllerType type) { + switch (type) { + case Settings::ControllerType::ProController: + default: + return 0; + case Settings::ControllerType::DualJoyconDetached: + return 1; + case Settings::ControllerType::LeftJoycon: + return 2; + case Settings::ControllerType::RightJoycon: + return 3; + case Settings::ControllerType::Handheld: + return 4; + } +} + +} // namespace + +QtControllerSelectorDialog::QtControllerSelectorDialog( + QWidget* parent, Core::Frontend::ControllerParameters parameters_, + InputCommon::InputSubsystem* input_subsystem_) + : QDialog(parent), ui(std::make_unique<Ui::QtControllerSelectorDialog>()), + parameters(std::move(parameters_)), input_subsystem(input_subsystem_) { + ui->setupUi(this); + + player_widgets = { + ui->widgetPlayer1, ui->widgetPlayer2, ui->widgetPlayer3, ui->widgetPlayer4, + ui->widgetPlayer5, ui->widgetPlayer6, ui->widgetPlayer7, ui->widgetPlayer8, + }; + + player_groupboxes = { + ui->groupPlayer1Connected, ui->groupPlayer2Connected, ui->groupPlayer3Connected, + ui->groupPlayer4Connected, ui->groupPlayer5Connected, ui->groupPlayer6Connected, + ui->groupPlayer7Connected, ui->groupPlayer8Connected, + }; + + connected_controller_icons = { + ui->controllerPlayer1, ui->controllerPlayer2, ui->controllerPlayer3, ui->controllerPlayer4, + ui->controllerPlayer5, ui->controllerPlayer6, ui->controllerPlayer7, ui->controllerPlayer8, + }; + + led_patterns_boxes = {{ + {ui->checkboxPlayer1LED1, ui->checkboxPlayer1LED2, ui->checkboxPlayer1LED3, + ui->checkboxPlayer1LED4}, + {ui->checkboxPlayer2LED1, ui->checkboxPlayer2LED2, ui->checkboxPlayer2LED3, + ui->checkboxPlayer2LED4}, + {ui->checkboxPlayer3LED1, ui->checkboxPlayer3LED2, ui->checkboxPlayer3LED3, + ui->checkboxPlayer3LED4}, + {ui->checkboxPlayer4LED1, ui->checkboxPlayer4LED2, ui->checkboxPlayer4LED3, + ui->checkboxPlayer4LED4}, + {ui->checkboxPlayer5LED1, ui->checkboxPlayer5LED2, ui->checkboxPlayer5LED3, + ui->checkboxPlayer5LED4}, + {ui->checkboxPlayer6LED1, ui->checkboxPlayer6LED2, ui->checkboxPlayer6LED3, + ui->checkboxPlayer6LED4}, + {ui->checkboxPlayer7LED1, ui->checkboxPlayer7LED2, ui->checkboxPlayer7LED3, + ui->checkboxPlayer7LED4}, + {ui->checkboxPlayer8LED1, ui->checkboxPlayer8LED2, ui->checkboxPlayer8LED3, + ui->checkboxPlayer8LED4}, + }}; + + explain_text_labels = { + ui->labelPlayer1Explain, ui->labelPlayer2Explain, ui->labelPlayer3Explain, + ui->labelPlayer4Explain, ui->labelPlayer5Explain, ui->labelPlayer6Explain, + ui->labelPlayer7Explain, ui->labelPlayer8Explain, + }; + + emulated_controllers = { + ui->comboPlayer1Emulated, ui->comboPlayer2Emulated, ui->comboPlayer3Emulated, + ui->comboPlayer4Emulated, ui->comboPlayer5Emulated, ui->comboPlayer6Emulated, + ui->comboPlayer7Emulated, ui->comboPlayer8Emulated, + }; + + player_labels = { + ui->labelPlayer1, ui->labelPlayer2, ui->labelPlayer3, ui->labelPlayer4, + ui->labelPlayer5, ui->labelPlayer6, ui->labelPlayer7, ui->labelPlayer8, + }; + + connected_controller_labels = { + ui->labelConnectedPlayer1, ui->labelConnectedPlayer2, ui->labelConnectedPlayer3, + ui->labelConnectedPlayer4, ui->labelConnectedPlayer5, ui->labelConnectedPlayer6, + ui->labelConnectedPlayer7, ui->labelConnectedPlayer8, + }; + + connected_controller_checkboxes = { + ui->checkboxPlayer1Connected, ui->checkboxPlayer2Connected, ui->checkboxPlayer3Connected, + ui->checkboxPlayer4Connected, ui->checkboxPlayer5Connected, ui->checkboxPlayer6Connected, + ui->checkboxPlayer7Connected, ui->checkboxPlayer8Connected, + }; + + // Setup/load everything prior to setting up connections. + // This avoids unintentionally changing the states of elements while loading them in. + SetSupportedControllers(); + DisableUnsupportedPlayers(); + LoadConfiguration(); + + for (std::size_t i = 0; i < NUM_PLAYERS; ++i) { + SetExplainText(i); + UpdateControllerIcon(i); + UpdateLEDPattern(i); + UpdateBorderColor(i); + + connect(player_groupboxes[i], &QGroupBox::toggled, [this, i](bool checked) { + if (checked) { + for (std::size_t index = 0; index <= i; ++index) { + connected_controller_checkboxes[index]->setChecked(checked); + } + } else { + for (std::size_t index = i; index < NUM_PLAYERS; ++index) { + connected_controller_checkboxes[index]->setChecked(checked); + } + } + }); + + connect(emulated_controllers[i], qOverload<int>(&QComboBox::currentIndexChanged), + [this, i](int) { + UpdateControllerIcon(i); + UpdateControllerState(i); + UpdateLEDPattern(i); + CheckIfParametersMet(); + }); + + connect(connected_controller_checkboxes[i], &QCheckBox::stateChanged, [this, i](int state) { + player_groupboxes[i]->setChecked(state == Qt::Checked); + UpdateControllerIcon(i); + UpdateControllerState(i); + UpdateLEDPattern(i); + UpdateBorderColor(i); + CheckIfParametersMet(); + }); + + if (i == 0) { + connect(emulated_controllers[i], qOverload<int>(&QComboBox::currentIndexChanged), + [this](int index) { + UpdateDockedState(GetControllerTypeFromIndex(index) == + Settings::ControllerType::Handheld); + }); + } + } + + connect(ui->inputConfigButton, &QPushButton::clicked, this, + &QtControllerSelectorDialog::CallConfigureInputDialog); + + connect(ui->buttonBox, &QDialogButtonBox::accepted, this, + &QtControllerSelectorDialog::ApplyConfiguration); + + // If keep_controllers_connected is false, forcefully disconnect all controllers + if (!parameters.keep_controllers_connected) { + for (auto player : player_groupboxes) { + player->setChecked(false); + } + } + + CheckIfParametersMet(); + + resize(0, 0); +} + +QtControllerSelectorDialog::~QtControllerSelectorDialog() = default; + +void QtControllerSelectorDialog::ApplyConfiguration() { + // Update the controller state once more, just to be sure they are properly applied. + for (std::size_t index = 0; index < NUM_PLAYERS; ++index) { + UpdateControllerState(index); + } + + const bool pre_docked_mode = Settings::values.use_docked_mode; + Settings::values.use_docked_mode = ui->radioDocked->isChecked(); + OnDockedModeChanged(pre_docked_mode, Settings::values.use_docked_mode); + + Settings::values.vibration_enabled = ui->vibrationGroup->isChecked(); +} + +void QtControllerSelectorDialog::LoadConfiguration() { + for (std::size_t index = 0; index < NUM_PLAYERS; ++index) { + const auto connected = Settings::values.players[index].connected || + (index == 0 && Settings::values.players[8].connected); + player_groupboxes[index]->setChecked(connected); + connected_controller_checkboxes[index]->setChecked(connected); + emulated_controllers[index]->setCurrentIndex( + GetIndexFromControllerType(Settings::values.players[index].controller_type)); + } + + UpdateDockedState(Settings::values.players[8].connected); + + ui->vibrationGroup->setChecked(Settings::values.vibration_enabled); +} + +void QtControllerSelectorDialog::CallConfigureInputDialog() { + const auto max_supported_players = parameters.enable_single_mode ? 1 : parameters.max_players; + + ConfigureInputDialog dialog(this, max_supported_players, input_subsystem); + + dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | + Qt::WindowSystemMenuHint); + dialog.setWindowModality(Qt::WindowModal); + dialog.exec(); + + dialog.ApplyConfiguration(); + + LoadConfiguration(); + CheckIfParametersMet(); +} + +void QtControllerSelectorDialog::CheckIfParametersMet() { + // Here, we check and validate the current configuration against all applicable parameters. + const auto num_connected_players = static_cast<int>( + std::count_if(player_groupboxes.begin(), player_groupboxes.end(), + [this](const QGroupBox* player) { return player->isChecked(); })); + + const auto min_supported_players = parameters.enable_single_mode ? 1 : parameters.min_players; + const auto max_supported_players = parameters.enable_single_mode ? 1 : parameters.max_players; + + // First, check against the number of connected players. + if (num_connected_players < min_supported_players || + num_connected_players > max_supported_players) { + parameters_met = false; + ui->buttonBox->setEnabled(parameters_met); + return; + } + + // Next, check against all connected controllers. + const auto all_controllers_compatible = [this] { + for (std::size_t index = 0; index < NUM_PLAYERS; ++index) { + // Skip controllers that are not used, we only care about the currently connected ones. + if (!player_groupboxes[index]->isChecked() || !player_groupboxes[index]->isEnabled()) { + continue; + } + + const auto compatible = IsControllerCompatible( + GetControllerTypeFromIndex(emulated_controllers[index]->currentIndex()), + parameters); + + // If any controller is found to be incompatible, return false early. + if (!compatible) { + return false; + } + } + + // Reaching here means all currently connected controllers are compatible. + return true; + }(); + + if (!all_controllers_compatible) { + parameters_met = false; + ui->buttonBox->setEnabled(parameters_met); + return; + } + + parameters_met = true; + ui->buttonBox->setEnabled(parameters_met); +} + +void QtControllerSelectorDialog::SetSupportedControllers() { + const QString theme = [this] { + if (QIcon::themeName().contains(QStringLiteral("dark"))) { + return QStringLiteral("_dark"); + } else if (QIcon::themeName().contains(QStringLiteral("midnight"))) { + return QStringLiteral("_midnight"); + } else { + return QString{}; + } + }(); + + if (parameters.enable_single_mode && parameters.allow_handheld) { + ui->controllerSupported1->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_handheld%0); ").arg(theme)); + } else { + ui->controllerSupported1->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_handheld%0_disabled); ").arg(theme)); + } + + if (parameters.allow_dual_joycons) { + ui->controllerSupported2->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_dual_joycon%0); ").arg(theme)); + } else { + ui->controllerSupported2->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_dual_joycon%0_disabled); ").arg(theme)); + } + + if (parameters.allow_left_joycon) { + ui->controllerSupported3->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_joycon_left%0); ").arg(theme)); + } else { + ui->controllerSupported3->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_joycon_left%0_disabled); ").arg(theme)); + } + + if (parameters.allow_right_joycon) { + ui->controllerSupported4->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_joycon_right%0); ").arg(theme)); + } else { + ui->controllerSupported4->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_joycon_right%0_disabled); ").arg(theme)); + } + + if (parameters.allow_pro_controller) { + ui->controllerSupported5->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_pro_controller%0); ").arg(theme)); + } else { + ui->controllerSupported5->setStyleSheet( + QStringLiteral("image: url(:/controller/applet_pro_controller%0_disabled); ") + .arg(theme)); + } + + // enable_single_mode overrides min_players and max_players. + if (parameters.enable_single_mode) { + ui->numberSupportedLabel->setText(QStringLiteral("1")); + return; + } + + if (parameters.min_players == parameters.max_players) { + ui->numberSupportedLabel->setText(QStringLiteral("%1").arg(parameters.max_players)); + } else { + ui->numberSupportedLabel->setText( + QStringLiteral("%1 - %2").arg(parameters.min_players).arg(parameters.max_players)); + } +} + +void QtControllerSelectorDialog::UpdateControllerIcon(std::size_t player_index) { + if (!player_groupboxes[player_index]->isChecked()) { + connected_controller_icons[player_index]->setStyleSheet(QString{}); + player_labels[player_index]->show(); + return; + } + + const QString stylesheet = [this, player_index] { + switch (GetControllerTypeFromIndex(emulated_controllers[player_index]->currentIndex())) { + case Settings::ControllerType::ProController: + return QStringLiteral("image: url(:/controller/applet_pro_controller%0); "); + case Settings::ControllerType::DualJoyconDetached: + return QStringLiteral("image: url(:/controller/applet_dual_joycon%0); "); + case Settings::ControllerType::LeftJoycon: + return QStringLiteral("image: url(:/controller/applet_joycon_left%0); "); + case Settings::ControllerType::RightJoycon: + return QStringLiteral("image: url(:/controller/applet_joycon_right%0); "); + case Settings::ControllerType::Handheld: + return QStringLiteral("image: url(:/controller/applet_handheld%0); "); + default: + return QString{}; + } + }(); + + const QString theme = [this] { + if (QIcon::themeName().contains(QStringLiteral("dark"))) { + return QStringLiteral("_dark"); + } else if (QIcon::themeName().contains(QStringLiteral("midnight"))) { + return QStringLiteral("_midnight"); + } else { + return QString{}; + } + }(); + + connected_controller_icons[player_index]->setStyleSheet(stylesheet.arg(theme)); + player_labels[player_index]->hide(); +} + +void QtControllerSelectorDialog::UpdateControllerState(std::size_t player_index) { + auto& player = Settings::values.players[player_index]; + + player.controller_type = + GetControllerTypeFromIndex(emulated_controllers[player_index]->currentIndex()); + player.connected = player_groupboxes[player_index]->isChecked(); + + // Player 2-8 + if (player_index != 0) { + UpdateController(player.controller_type, player_index, player.connected); + return; + } + + // Player 1 and Handheld + auto& handheld = Settings::values.players[8]; + // If Handheld is selected, copy all the settings from Player 1 to Handheld. + if (player.controller_type == Settings::ControllerType::Handheld) { + handheld = player; + handheld.connected = player_groupboxes[player_index]->isChecked(); + player.connected = false; // Disconnect Player 1 + } else { + player.connected = player_groupboxes[player_index]->isChecked(); + handheld.connected = false; // Disconnect Handheld + } + + UpdateController(player.controller_type, player_index, player.connected); + UpdateController(Settings::ControllerType::Handheld, 8, handheld.connected); +} + +void QtControllerSelectorDialog::UpdateLEDPattern(std::size_t player_index) { + if (!player_groupboxes[player_index]->isChecked() || + GetControllerTypeFromIndex(emulated_controllers[player_index]->currentIndex()) == + Settings::ControllerType::Handheld) { + led_patterns_boxes[player_index][0]->setChecked(false); + led_patterns_boxes[player_index][1]->setChecked(false); + led_patterns_boxes[player_index][2]->setChecked(false); + led_patterns_boxes[player_index][3]->setChecked(false); + return; + } + + led_patterns_boxes[player_index][0]->setChecked(led_patterns[player_index][0]); + led_patterns_boxes[player_index][1]->setChecked(led_patterns[player_index][1]); + led_patterns_boxes[player_index][2]->setChecked(led_patterns[player_index][2]); + led_patterns_boxes[player_index][3]->setChecked(led_patterns[player_index][3]); +} + +void QtControllerSelectorDialog::UpdateBorderColor(std::size_t player_index) { + if (!parameters.enable_border_color || + player_index >= static_cast<std::size_t>(parameters.max_players) || + player_groupboxes[player_index]->styleSheet().contains(QStringLiteral("QGroupBox"))) { + return; + } + + player_groupboxes[player_index]->setStyleSheet( + player_groupboxes[player_index]->styleSheet().append( + QStringLiteral("QGroupBox#groupPlayer%1Connected:checked " + "{ border: 1px solid rgba(%2, %3, %4, %5); }") + .arg(player_index + 1) + .arg(parameters.border_colors[player_index][0]) + .arg(parameters.border_colors[player_index][1]) + .arg(parameters.border_colors[player_index][2]) + .arg(parameters.border_colors[player_index][3]))); +} + +void QtControllerSelectorDialog::SetExplainText(std::size_t player_index) { + if (!parameters.enable_explain_text || + player_index >= static_cast<std::size_t>(parameters.max_players)) { + return; + } + + explain_text_labels[player_index]->setText(QString::fromStdString( + Common::StringFromFixedZeroTerminatedBuffer(parameters.explain_text[player_index].data(), + parameters.explain_text[player_index].size()))); +} + +void QtControllerSelectorDialog::UpdateDockedState(bool is_handheld) { + // Disallow changing the console mode if the controller type is handheld. + ui->radioDocked->setEnabled(!is_handheld); + ui->radioUndocked->setEnabled(!is_handheld); + + ui->radioDocked->setChecked(Settings::values.use_docked_mode); + ui->radioUndocked->setChecked(!Settings::values.use_docked_mode); + + // Also force into undocked mode if the controller type is handheld. + if (is_handheld) { + ui->radioUndocked->setChecked(true); + } +} + +void QtControllerSelectorDialog::DisableUnsupportedPlayers() { + const auto max_supported_players = parameters.enable_single_mode ? 1 : parameters.max_players; + + switch (max_supported_players) { + case 0: + default: + UNREACHABLE(); + return; + case 1: + ui->widgetSpacer->hide(); + ui->widgetSpacer2->hide(); + ui->widgetSpacer3->hide(); + ui->widgetSpacer4->hide(); + break; + case 2: + ui->widgetSpacer->hide(); + ui->widgetSpacer2->hide(); + ui->widgetSpacer3->hide(); + break; + case 3: + ui->widgetSpacer->hide(); + ui->widgetSpacer2->hide(); + break; + case 4: + ui->widgetSpacer->hide(); + break; + case 5: + case 6: + case 7: + case 8: + break; + } + + for (std::size_t index = max_supported_players; index < NUM_PLAYERS; ++index) { + // Disconnect any unsupported players here and disable or hide them if applicable. + Settings::values.players[index].connected = false; + UpdateController(Settings::values.players[index].controller_type, index, false); + // Hide the player widgets when max_supported_controllers is less than or equal to 4. + if (max_supported_players <= 4) { + player_widgets[index]->hide(); + } + + // Disable and hide the following to prevent these from interaction. + player_widgets[index]->setDisabled(true); + connected_controller_checkboxes[index]->setDisabled(true); + connected_controller_labels[index]->hide(); + connected_controller_checkboxes[index]->hide(); + } +} + +QtControllerSelector::QtControllerSelector(GMainWindow& parent) { + connect(this, &QtControllerSelector::MainWindowReconfigureControllers, &parent, + &GMainWindow::ControllerSelectorReconfigureControllers, Qt::QueuedConnection); + connect(&parent, &GMainWindow::ControllerSelectorReconfigureFinished, this, + &QtControllerSelector::MainWindowReconfigureFinished, Qt::QueuedConnection); +} + +QtControllerSelector::~QtControllerSelector() = default; + +void QtControllerSelector::ReconfigureControllers( + std::function<void()> callback, Core::Frontend::ControllerParameters parameters) const { + this->callback = std::move(callback); + emit MainWindowReconfigureControllers(parameters); +} + +void QtControllerSelector::MainWindowReconfigureFinished() { + // Acquire the HLE mutex + std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock); + callback(); +} diff --git a/src/yuzu/applets/controller.h b/src/yuzu/applets/controller.h new file mode 100644 index 000000000..2d6d588c6 --- /dev/null +++ b/src/yuzu/applets/controller.h @@ -0,0 +1,133 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <array> +#include <memory> +#include <QDialog> +#include "core/frontend/applets/controller.h" + +class GMainWindow; +class QCheckBox; +class QComboBox; +class QDialogButtonBox; +class QGroupBox; +class QLabel; + +namespace InputCommon { +class InputSubsystem; +} + +namespace Ui { +class QtControllerSelectorDialog; +} + +class QtControllerSelectorDialog final : public QDialog { + Q_OBJECT + +public: + explicit QtControllerSelectorDialog(QWidget* parent, + Core::Frontend::ControllerParameters parameters_, + InputCommon::InputSubsystem* input_subsystem_); + ~QtControllerSelectorDialog() override; + +private: + // Applies the current configuration. + void ApplyConfiguration(); + + // Loads the current input configuration into the frontend applet. + void LoadConfiguration(); + + // Initializes the "Configure Input" Dialog. + void CallConfigureInputDialog(); + + // Checks the current configuration against the given parameters and + // sets the value of parameters_met. + void CheckIfParametersMet(); + + // Sets the controller icons for "Supported Controller Types". + void SetSupportedControllers(); + + // Updates the controller icons per player. + void UpdateControllerIcon(std::size_t player_index); + + // Updates the controller state (type and connection status) per player. + void UpdateControllerState(std::size_t player_index); + + // Updates the LED pattern per player. + void UpdateLEDPattern(std::size_t player_index); + + // Updates the border color per player. + void UpdateBorderColor(std::size_t player_index); + + // Sets the "Explain Text" per player. + void SetExplainText(std::size_t player_index); + + // Updates the console mode. + void UpdateDockedState(bool is_handheld); + + // Disables and disconnects unsupported players based on the given parameters. + void DisableUnsupportedPlayers(); + + std::unique_ptr<Ui::QtControllerSelectorDialog> ui; + + // Parameters sent in from the backend HLE applet. + Core::Frontend::ControllerParameters parameters; + + InputCommon::InputSubsystem* input_subsystem; + + // This is true if and only if all parameters are met. Otherwise, this is false. + // This determines whether the "OK" button can be clicked to exit the applet. + bool parameters_met{false}; + + static constexpr std::size_t NUM_PLAYERS = 8; + + // Widgets encapsulating the groupboxes and comboboxes per player. + std::array<QWidget*, NUM_PLAYERS> player_widgets; + + // Groupboxes encapsulating the controller icons and LED patterns per player. + std::array<QGroupBox*, NUM_PLAYERS> player_groupboxes; + + // Icons for currently connected controllers/players. + std::array<QWidget*, NUM_PLAYERS> connected_controller_icons; + + // Labels that represent the player numbers in place of the controller icons. + std::array<QLabel*, NUM_PLAYERS> player_labels; + + // LED patterns for currently connected controllers/players. + std::array<std::array<QCheckBox*, 4>, NUM_PLAYERS> led_patterns_boxes; + + // Labels representing additional information known as "Explain Text" per player. + std::array<QLabel*, NUM_PLAYERS> explain_text_labels; + + // Comboboxes with a list of emulated controllers per player. + std::array<QComboBox*, NUM_PLAYERS> emulated_controllers; + + // Labels representing the number of connected controllers + // above the "Connected Controllers" checkboxes. + std::array<QLabel*, NUM_PLAYERS> connected_controller_labels; + + // Checkboxes representing the "Connected Controllers". + std::array<QCheckBox*, NUM_PLAYERS> connected_controller_checkboxes; +}; + +class QtControllerSelector final : public QObject, public Core::Frontend::ControllerApplet { + Q_OBJECT + +public: + explicit QtControllerSelector(GMainWindow& parent); + ~QtControllerSelector() override; + + void ReconfigureControllers(std::function<void()> callback, + Core::Frontend::ControllerParameters parameters) const override; + +signals: + void MainWindowReconfigureControllers(Core::Frontend::ControllerParameters parameters) const; + +private: + void MainWindowReconfigureFinished(); + + mutable std::function<void()> callback; +}; diff --git a/src/yuzu/applets/controller.ui b/src/yuzu/applets/controller.ui new file mode 100644 index 000000000..c4108a979 --- /dev/null +++ b/src/yuzu/applets/controller.ui @@ -0,0 +1,2672 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>QtControllerSelectorDialog</class> + <widget class="QDialog" name="QtControllerSelectorDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>839</width> + <height>630</height> + </rect> + </property> + <property name="windowTitle"> + <string>Controller Applet</string> + </property> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout" stretch="0"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QWidget" name="mainControllerApplet" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,3,0"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QWidget" name="topControllerApplet" native="true"> + <layout class="QHBoxLayout" name="horizontalLayout"> + <property name="spacing"> + <number>10</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>10</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>10</number> + </property> + <item> + <spacer name="controllerAppletHorizontalSpacer2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QWidget" name="controllersSupported" native="true"> + <property name="minimumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_21"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="controllersSupportedLabel"> + <property name="minimumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="font"> + <font> + <weight>75</weight> + <bold>true</bold> + </font> + </property> + <property name="text"> + <string>Supported Controller Types:</string> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="controllerSupported1" native="true"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="minimumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true"/> + </property> + </widget> + </item> + <item> + <widget class="QWidget" name="controllerSupported2" native="true"> + <property name="minimumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true"/> + </property> + </widget> + </item> + <item> + <widget class="QWidget" name="controllerSupported3" native="true"> + <property name="minimumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true"/> + </property> + </widget> + </item> + <item> + <widget class="QWidget" name="controllerSupported4" native="true"> + <property name="minimumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true"/> + </property> + </widget> + </item> + <item> + <widget class="QWidget" name="controllerSupported5" native="true"> + <property name="minimumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true"/> + </property> + </widget> + </item> + <item> + <widget class="QWidget" name="playersSupported" native="true"> + <property name="minimumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>70</width> + <height>70</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_20"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>16</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>16</number> + </property> + <item> + <widget class="QLabel" name="maxSupportedLabel"> + <property name="font"> + <font> + <weight>75</weight> + <bold>true</bold> + </font> + </property> + <property name="text"> + <string>Players:</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="wordWrap"> + <bool>false</bool> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="numberSupportedLabel"> + <property name="font"> + <font> + <pointsize>14</pointsize> + </font> + </property> + <property name="text"> + <string>1 - 8</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <spacer name="controllerAppletHorizontalSpacer3"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="middleControllerApplet" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_3"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <layout class="QGridLayout" name="gridLayout"> + <property name="spacing"> + <number>5</number> + </property> + <item row="1" column="7"> + <widget class="QWidget" name="widgetPlayer4" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_27"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item alignment="Qt::AlignHCenter"> + <widget class="QGroupBox" name="groupPlayer4Connected"> + <property name="minimumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="title"> + <string/> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>false</bool> + </property> + <layout class="QVBoxLayout" name="verticalLayout_7" stretch="1,0"> + <property name="spacing"> + <number>7</number> + </property> + <property name="leftMargin"> + <number>14</number> + </property> + <property name="topMargin"> + <number>7</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>4</number> + </property> + <item> + <widget class="QWidget" name="controllerPlayer4" native="true"> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout_15"> + <property name="topMargin"> + <number>16</number> + </property> + <item alignment="Qt::AlignHCenter|Qt::AlignVCenter"> + <widget class="QLabel" name="labelPlayer4"> + <property name="text"> + <string>P4</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item alignment="Qt::AlignHCenter"> + <widget class="QWidget" name="Player4LEDs" native="true"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_10"> + <property name="spacing"> + <number>4</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QCheckBox" name="checkboxPlayer4LED1"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer4LED2"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer4LED3"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer4LED4"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="Player4Explain" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>150</width> + <height>16777215</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_39"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="labelPlayer4Explain"> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer4Emulated"> + <item> + <property name="text"> + <string>Pro Controller</string> + </property> + </item> + <item> + <property name="text"> + <string>Dual Joycons</string> + </property> + </item> + <item> + <property name="text"> + <string>Left Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Right Joycon</string> + </property> + </item> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer4Profile"> + <item> + <property name="text"> + <string>Use Current Config</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + <item row="1" column="3"> + <widget class="QWidget" name="widgetPlayer2" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_29"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item alignment="Qt::AlignHCenter"> + <widget class="QGroupBox" name="groupPlayer2Connected"> + <property name="minimumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="title"> + <string/> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>false</bool> + </property> + <layout class="QVBoxLayout" name="verticalLayout_5" stretch="1,0"> + <property name="spacing"> + <number>7</number> + </property> + <property name="leftMargin"> + <number>14</number> + </property> + <property name="topMargin"> + <number>7</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>4</number> + </property> + <item> + <widget class="QWidget" name="controllerPlayer2" native="true"> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout_13"> + <property name="topMargin"> + <number>16</number> + </property> + <item alignment="Qt::AlignHCenter|Qt::AlignVCenter"> + <widget class="QLabel" name="labelPlayer2"> + <property name="text"> + <string>P2</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item alignment="Qt::AlignHCenter"> + <widget class="QWidget" name="Player2LEDs" native="true"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_8"> + <property name="spacing"> + <number>4</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QCheckBox" name="checkboxPlayer2LED1"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer2LED2"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer2LED3"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer2LED4"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="Player2Explain" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>150</width> + <height>16777215</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_37"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="labelPlayer2Explain"> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer2Emulated"> + <item> + <property name="text"> + <string>Pro Controller</string> + </property> + </item> + <item> + <property name="text"> + <string>Dual Joycons</string> + </property> + </item> + <item> + <property name="text"> + <string>Left Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Right Joycon</string> + </property> + </item> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer2Profile"> + <item> + <property name="text"> + <string>Use Current Config</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + <item row="1" column="1"> + <widget class="QWidget" name="widgetPlayer1" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_30"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item alignment="Qt::AlignHCenter"> + <widget class="QGroupBox" name="groupPlayer1Connected"> + <property name="minimumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="title"> + <string/> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>false</bool> + </property> + <layout class="QVBoxLayout" name="verticalLayout_4" stretch="1,0"> + <property name="spacing"> + <number>7</number> + </property> + <property name="leftMargin"> + <number>14</number> + </property> + <property name="topMargin"> + <number>7</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>4</number> + </property> + <item> + <widget class="QWidget" name="controllerPlayer1" native="true"> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout_12"> + <property name="topMargin"> + <number>16</number> + </property> + <item alignment="Qt::AlignHCenter|Qt::AlignVCenter"> + <widget class="QLabel" name="labelPlayer1"> + <property name="text"> + <string>P1</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item alignment="Qt::AlignHCenter"> + <widget class="QWidget" name="Player1LEDs" native="true"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <property name="spacing"> + <number>4</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QCheckBox" name="checkboxPlayer1LED1"> + <property name="layoutDirection"> + <enum>Qt::LeftToRight</enum> + </property> + </widget> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer1LED2"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer1LED3"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer1LED4"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="Player1Explain" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>150</width> + <height>16777215</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_36"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="labelPlayer1Explain"> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer1Emulated"> + <item> + <property name="text"> + <string>Pro Controller</string> + </property> + </item> + <item> + <property name="text"> + <string>Dual Joycons</string> + </property> + </item> + <item> + <property name="text"> + <string>Left Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Right Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Handheld</string> + </property> + </item> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer1Profile"> + <item> + <property name="text"> + <string>Use Current Config</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + <item row="1" column="8"> + <widget class="QWidget" name="widgetSpacer2" native="true"> + <property name="minimumSize"> + <size> + <width>25</width> + <height>0</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_31"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="controllerAppletHorizontalSpacer8"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>25</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item row="1" column="4"> + <widget class="QWidget" name="widgetSpacer4" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_33"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="controllerAppletHorizontalSpacer6"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>0</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item row="1" column="6"> + <widget class="QWidget" name="widgetSpacer3" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_32"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="controllerAppletHorizontalSpacer7"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>0</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item row="1" column="5"> + <widget class="QWidget" name="widgetPlayer3" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_28"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item alignment="Qt::AlignHCenter"> + <widget class="QGroupBox" name="groupPlayer3Connected"> + <property name="minimumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="title"> + <string/> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>false</bool> + </property> + <layout class="QVBoxLayout" name="verticalLayout_6" stretch="1,0"> + <property name="spacing"> + <number>7</number> + </property> + <property name="leftMargin"> + <number>14</number> + </property> + <property name="topMargin"> + <number>7</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>4</number> + </property> + <item> + <widget class="QWidget" name="controllerPlayer3" native="true"> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout_14"> + <property name="topMargin"> + <number>16</number> + </property> + <item alignment="Qt::AlignHCenter|Qt::AlignVCenter"> + <widget class="QLabel" name="labelPlayer3"> + <property name="text"> + <string>P3</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item alignment="Qt::AlignHCenter"> + <widget class="QWidget" name="Player3LEDs" native="true"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_9"> + <property name="spacing"> + <number>4</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QCheckBox" name="checkboxPlayer3LED1"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer3LED2"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer3LED3"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer3LED4"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="Player3Explain" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>150</width> + <height>16777215</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_38"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="labelPlayer3Explain"> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer3Emulated"> + <property name="editable"> + <bool>false</bool> + </property> + <item> + <property name="text"> + <string>Pro Controller</string> + </property> + </item> + <item> + <property name="text"> + <string>Dual Joycons</string> + </property> + </item> + <item> + <property name="text"> + <string>Left Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Right Joycon</string> + </property> + </item> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer3Profile"> + <item> + <property name="text"> + <string>Use Current Config</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + <item row="0" column="1"> + <widget class="QWidget" name="widgetSpacer5" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>25</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_34"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="controllerAppletVerticalSpacer3"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>25</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item row="6" column="5"> + <widget class="QWidget" name="widgetPlayer7" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_25"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item alignment="Qt::AlignHCenter"> + <widget class="QGroupBox" name="groupPlayer7Connected"> + <property name="minimumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="title"> + <string/> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>false</bool> + </property> + <layout class="QVBoxLayout" name="verticalLayout_10" stretch="1,0"> + <property name="spacing"> + <number>7</number> + </property> + <property name="leftMargin"> + <number>14</number> + </property> + <property name="topMargin"> + <number>7</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>4</number> + </property> + <item> + <widget class="QWidget" name="controllerPlayer7" native="true"> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout_18"> + <property name="topMargin"> + <number>16</number> + </property> + <item alignment="Qt::AlignHCenter|Qt::AlignVCenter"> + <widget class="QLabel" name="labelPlayer7"> + <property name="text"> + <string>P7</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item alignment="Qt::AlignHCenter"> + <widget class="QWidget" name="Player7LEDs" native="true"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_13"> + <property name="spacing"> + <number>4</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QCheckBox" name="checkboxPlayer7LED1"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer7LED2"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer7LED3"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer7LED4"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="Player7Explain" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>150</width> + <height>16777215</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_42"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="labelPlayer7Explain"> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer7Emulated"> + <item> + <property name="text"> + <string>Pro Controller</string> + </property> + </item> + <item> + <property name="text"> + <string>Dual Joycons</string> + </property> + </item> + <item> + <property name="text"> + <string>Left Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Right Joycon</string> + </property> + </item> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer7Profile"> + <item> + <property name="text"> + <string>Use Current Config</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + <item row="6" column="7"> + <widget class="QWidget" name="widgetPlayer8" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_26"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item alignment="Qt::AlignHCenter"> + <widget class="QGroupBox" name="groupPlayer8Connected"> + <property name="minimumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="title"> + <string/> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>false</bool> + </property> + <layout class="QVBoxLayout" name="verticalLayout_11" stretch="1,0"> + <property name="spacing"> + <number>7</number> + </property> + <property name="leftMargin"> + <number>14</number> + </property> + <property name="topMargin"> + <number>7</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>4</number> + </property> + <item> + <widget class="QWidget" name="controllerPlayer8" native="true"> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout_19"> + <property name="topMargin"> + <number>16</number> + </property> + <item alignment="Qt::AlignHCenter|Qt::AlignVCenter"> + <widget class="QLabel" name="labelPlayer8"> + <property name="text"> + <string>P8</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item alignment="Qt::AlignHCenter"> + <widget class="QWidget" name="Player8LEDs" native="true"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_14"> + <property name="spacing"> + <number>4</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QCheckBox" name="checkboxPlayer8LED1"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer8LED2"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer8LED3"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer8LED4"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="Player8Explain" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>150</width> + <height>16777215</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_35"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="labelPlayer8Explain"> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer8Emulated"> + <item> + <property name="text"> + <string>Pro Controller</string> + </property> + </item> + <item> + <property name="text"> + <string>Dual Joycons</string> + </property> + </item> + <item> + <property name="text"> + <string>Left Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Right Joycon</string> + </property> + </item> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer8Profile"> + <item> + <property name="text"> + <string>Use Current Config</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + <item row="6" column="1"> + <widget class="QWidget" name="widgetPlayer5" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_23"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item alignment="Qt::AlignHCenter"> + <widget class="QGroupBox" name="groupPlayer5Connected"> + <property name="minimumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="title"> + <string/> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>false</bool> + </property> + <layout class="QVBoxLayout" name="verticalLayout_8" stretch="1,0"> + <property name="spacing"> + <number>7</number> + </property> + <property name="leftMargin"> + <number>14</number> + </property> + <property name="topMargin"> + <number>7</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>4</number> + </property> + <item> + <widget class="QWidget" name="controllerPlayer5" native="true"> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout_16"> + <property name="topMargin"> + <number>16</number> + </property> + <item alignment="Qt::AlignHCenter|Qt::AlignVCenter"> + <widget class="QLabel" name="labelPlayer5"> + <property name="text"> + <string>P5</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item alignment="Qt::AlignHCenter"> + <widget class="QWidget" name="Player5LEDs" native="true"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_11"> + <property name="spacing"> + <number>4</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QCheckBox" name="checkboxPlayer5LED1"> + <property name="layoutDirection"> + <enum>Qt::LeftToRight</enum> + </property> + </widget> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer5LED2"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer5LED3"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer5LED4"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="Player5Explain" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>150</width> + <height>16777215</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_40"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="labelPlayer5Explain"> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer5Emulated"> + <item> + <property name="text"> + <string>Pro Controller</string> + </property> + </item> + <item> + <property name="text"> + <string>Dual Joycons</string> + </property> + </item> + <item> + <property name="text"> + <string>Left Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Right Joycon</string> + </property> + </item> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer5Profile"> + <item> + <property name="text"> + <string>Use Current Config</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + <item row="6" column="3"> + <widget class="QWidget" name="widgetPlayer6" native="true"> + <layout class="QVBoxLayout" name="verticalLayout_24"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item alignment="Qt::AlignHCenter"> + <widget class="QGroupBox" name="groupPlayer6Connected"> + <property name="minimumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>100</width> + <height>100</height> + </size> + </property> + <property name="title"> + <string/> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>false</bool> + </property> + <layout class="QVBoxLayout" name="verticalLayout_9" stretch="1,0"> + <property name="spacing"> + <number>7</number> + </property> + <property name="leftMargin"> + <number>14</number> + </property> + <property name="topMargin"> + <number>7</number> + </property> + <property name="rightMargin"> + <number>14</number> + </property> + <property name="bottomMargin"> + <number>4</number> + </property> + <item> + <widget class="QWidget" name="controllerPlayer6" native="true"> + <property name="styleSheet"> + <string notr="true"/> + </property> + <layout class="QVBoxLayout" name="verticalLayout_17"> + <property name="topMargin"> + <number>16</number> + </property> + <item alignment="Qt::AlignHCenter|Qt::AlignVCenter"> + <widget class="QLabel" name="labelPlayer6"> + <property name="text"> + <string>P6</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item alignment="Qt::AlignHCenter"> + <widget class="QWidget" name="Player6LEDs" native="true"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_12"> + <property name="spacing"> + <number>4</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QCheckBox" name="checkboxPlayer6LED1"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer6LED2"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer6LED3"/> + </item> + <item> + <widget class="QCheckBox" name="checkboxPlayer6LED4"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="Player6Explain" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>10</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>150</width> + <height>16777215</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_41"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="labelPlayer6Explain"> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer6Emulated"> + <item> + <property name="text"> + <string>Pro Controller</string> + </property> + </item> + <item> + <property name="text"> + <string>Dual Joycons</string> + </property> + </item> + <item> + <property name="text"> + <string>Left Joycon</string> + </property> + </item> + <item> + <property name="text"> + <string>Right Joycon</string> + </property> + </item> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboPlayer6Profile"> + <item> + <property name="text"> + <string>Use Current Config</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + <item row="10" column="1"> + <widget class="QWidget" name="widgetSpacer" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>25</height> + </size> + </property> + <layout class="QVBoxLayout" name="verticalLayout_22"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="controllerAppletVerticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>25</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item row="1" column="2"> + <widget class="QWidget" name="widgetSpacer6" native="true"> + <layout class="QHBoxLayout" name="horizontalLayout_15"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="controllerAppletHorizontalSpacer5"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>0</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item row="1" column="0"> + <widget class="QWidget" name="widgetSpacer7" native="true"> + <property name="minimumSize"> + <size> + <width>25</width> + <height>0</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_16"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="controllerAppletHorizontalSpacer4"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>25</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item row="2" column="1"> + <widget class="QWidget" name="widgetSpacer9" native="true"> + <property name="minimumSize"> + <size> + <width>0</width> + <height>25</height> + </size> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_17"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <spacer name="controllerAppletVerticalSpacer2"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>25</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="bottomControllerApplet" native="true"> + <layout class="QHBoxLayout" name="horizontalLayout_6"> + <property name="spacing"> + <number>15</number> + </property> + <property name="leftMargin"> + <number>15</number> + </property> + <property name="topMargin"> + <number>8</number> + </property> + <property name="rightMargin"> + <number>15</number> + </property> + <property name="bottomMargin"> + <number>15</number> + </property> + <item> + <widget class="QGroupBox" name="handheldGroup"> + <property name="maximumSize"> + <size> + <width>16777215</width> + <height>16777215</height> + </size> + </property> + <property name="title"> + <string>Console Mode</string> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_3"> + <property name="spacing"> + <number>6</number> + </property> + <property name="leftMargin"> + <number>6</number> + </property> + <property name="topMargin"> + <number>6</number> + </property> + <property name="rightMargin"> + <number>3</number> + </property> + <property name="bottomMargin"> + <number>6</number> + </property> + <item> + <widget class="QRadioButton" name="radioDocked"> + <property name="text"> + <string>Docked</string> + </property> + <property name="checked"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="QRadioButton" name="radioUndocked"> + <property name="text"> + <string>Undocked</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QGroupBox" name="vibrationGroup"> + <property name="title"> + <string>Vibration</string> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_5"> + <property name="leftMargin"> + <number>3</number> + </property> + <property name="topMargin"> + <number>3</number> + </property> + <property name="rightMargin"> + <number>3</number> + </property> + <property name="bottomMargin"> + <number>3</number> + </property> + <item> + <widget class="QSpinBox" name="vibrationSpin"> + <property name="minimumSize"> + <size> + <width>65</width> + <height>0</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>65</width> + <height>16777215</height> + </size> + </property> + <property name="suffix"> + <string>%</string> + </property> + <property name="minimum"> + <number>1</number> + </property> + <property name="maximum"> + <number>200</number> + </property> + <property name="value"> + <number>100</number> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QGroupBox" name="motionGroup"> + <property name="title"> + <string>Motion</string> + </property> + <property name="checkable"> + <bool>true</bool> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_4"> + <property name="leftMargin"> + <number>3</number> + </property> + <property name="topMargin"> + <number>3</number> + </property> + <property name="rightMargin"> + <number>3</number> + </property> + <property name="bottomMargin"> + <number>3</number> + </property> + <item> + <widget class="QPushButton" name="motionButton"> + <property name="minimumSize"> + <size> + <width>57</width> + <height>0</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>55</width> + <height>16777215</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">min-width: 55px;</string> + </property> + <property name="text"> + <string>Configure</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QGroupBox" name="inputConfigGroup"> + <property name="title"> + <string>Input Config</string> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_7"> + <property name="leftMargin"> + <number>3</number> + </property> + <property name="topMargin"> + <number>3</number> + </property> + <property name="rightMargin"> + <number>3</number> + </property> + <property name="bottomMargin"> + <number>3</number> + </property> + <item> + <widget class="QPushButton" name="inputConfigButton"> + <property name="maximumSize"> + <size> + <width>65</width> + <height>16777215</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">min-width: 55px;</string> + </property> + <property name="text"> + <string>Open</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="connectedControllers" native="true"> + <layout class="QGridLayout" name="gridLayout_2"> + <property name="leftMargin"> + <number>5</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <property name="spacing"> + <number>3</number> + </property> + <item row="1" column="4"> + <widget class="QCheckBox" name="checkboxPlayer4Connected"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="labelControllers"> + <property name="text"> + <string>Controllers</string> + </property> + </widget> + </item> + <item row="1" column="2"> + <widget class="QCheckBox" name="checkboxPlayer2Connected"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLabel" name="labelConnectedPlayer1"> + <property name="text"> + <string>1</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="1" column="3"> + <widget class="QCheckBox" name="checkboxPlayer3Connected"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QCheckBox" name="checkboxPlayer1Connected"> + <property name="layoutDirection"> + <enum>Qt::LeftToRight</enum> + </property> + <property name="checked"> + <bool>false</bool> + </property> + </widget> + </item> + <item row="0" column="2"> + <widget class="QLabel" name="labelConnectedPlayer2"> + <property name="text"> + <string>2</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="0" column="4"> + <widget class="QLabel" name="labelConnectedPlayer4"> + <property name="text"> + <string>4</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="0" column="3"> + <widget class="QLabel" name="labelConnectedPlayer3"> + <property name="text"> + <string>3</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="0" column="0"> + <widget class="QLabel" name="labelConnected"> + <property name="text"> + <string>Connected</string> + </property> + </widget> + </item> + <item row="1" column="7"> + <widget class="QCheckBox" name="checkboxPlayer7Connected"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="0" column="5"> + <widget class="QLabel" name="labelConnectedPlayer5"> + <property name="text"> + <string>5</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="1" column="6"> + <widget class="QCheckBox" name="checkboxPlayer6Connected"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="0" column="7"> + <widget class="QLabel" name="labelConnectedPlayer7"> + <property name="text"> + <string>7</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="1" column="5"> + <widget class="QCheckBox" name="checkboxPlayer5Connected"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="0" column="6"> + <widget class="QLabel" name="labelConnectedPlayer6"> + <property name="text"> + <string>6</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="0" column="8"> + <widget class="QLabel" name="labelConnectedPlayer8"> + <property name="text"> + <string>8</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="1" column="8"> + <widget class="QCheckBox" name="checkboxPlayer8Connected"> + <property name="text"> + <string/> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <spacer name="controllerAppletHorizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>0</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item alignment="Qt::AlignBottom"> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="enabled"> + <bool>true</bool> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>buttonBox</sender> + <signal>accepted()</signal> + <receiver>QtControllerSelectorDialog</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>20</x> + <y>20</y> + </hint> + <hint type="destinationlabel"> + <x>20</x> + <y>20</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 2bc55a26a..d2913d613 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -36,6 +36,11 @@ const std::array<int, Settings::NativeButton::NumButtons> Config::default_button Qt::Key_H, Qt::Key_G, Qt::Key_D, Qt::Key_C, Qt::Key_B, Qt::Key_V, }; +const std::array<int, Settings::NativeMotion::NumMotions> Config::default_motions = { + Qt::Key_7, + Qt::Key_8, +}; + const std::array<std::array<int, 4>, Settings::NativeAnalog::NumAnalogs> Config::default_analogs{{ { Qt::Key_Up, @@ -284,6 +289,22 @@ void Config::ReadPlayerValues() { } } + for (int i = 0; i < Settings::NativeMotion::NumMotions; ++i) { + const std::string default_param = + InputCommon::GenerateKeyboardParam(default_motions[i]); + auto& player_motions = player.motions[i]; + + player_motions = qt_config + ->value(QStringLiteral("player_%1_").arg(p) + + QString::fromUtf8(Settings::NativeMotion::mapping[i]), + QString::fromStdString(default_param)) + .toString() + .toStdString(); + if (player_motions.empty()) { + player_motions = default_param; + } + } + for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) { const std::string default_param = InputCommon::GenerateAnalogParamFromKeys( default_analogs[i][0], default_analogs[i][1], default_analogs[i][2], @@ -424,6 +445,7 @@ void Config::ReadControlValues() { Settings::values.vibration_enabled = ReadSetting(QStringLiteral("vibration_enabled"), true).toBool(); + Settings::values.motion_enabled = ReadSetting(QStringLiteral("motion_enabled"), true).toBool(); Settings::values.use_docked_mode = ReadSetting(QStringLiteral("use_docked_mode"), false).toBool(); @@ -922,6 +944,14 @@ void Config::SavePlayerValues() { QString::fromStdString(player.buttons[i]), QString::fromStdString(default_param)); } + for (int i = 0; i < Settings::NativeMotion::NumMotions; ++i) { + const std::string default_param = + InputCommon::GenerateKeyboardParam(default_motions[i]); + WriteSetting(QStringLiteral("player_%1_").arg(p) + + QString::fromStdString(Settings::NativeMotion::mapping[i]), + QString::fromStdString(player.motions[i]), + QString::fromStdString(default_param)); + } for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) { const std::string default_param = InputCommon::GenerateAnalogParamFromKeys( default_analogs[i][0], default_analogs[i][1], default_analogs[i][2], @@ -1062,6 +1092,7 @@ void Config::SaveControlValues() { SaveMotionTouchValues(); WriteSetting(QStringLiteral("vibration_enabled"), Settings::values.vibration_enabled, true); + WriteSetting(QStringLiteral("motion_enabled"), Settings::values.motion_enabled, true); WriteSetting(QStringLiteral("motion_device"), QString::fromStdString(Settings::values.motion_device), QStringLiteral("engine:motion_emu,update_period:100,sensitivity:0.01")); diff --git a/src/yuzu/configuration/config.h b/src/yuzu/configuration/config.h index ca0d29c6c..5d8e45d78 100644 --- a/src/yuzu/configuration/config.h +++ b/src/yuzu/configuration/config.h @@ -23,6 +23,7 @@ public: void Save(); static const std::array<int, Settings::NativeButton::NumButtons> default_buttons; + static const std::array<int, Settings::NativeMotion::NumMotions> default_motions; static const std::array<std::array<int, 4>, Settings::NativeAnalog::NumAnalogs> default_analogs; static const std::array<int, 2> default_stick_mod; static const std::array<int, Settings::NativeMouseButton::NumMouseButtons> diff --git a/src/yuzu/configuration/configure_input.cpp b/src/yuzu/configuration/configure_input.cpp index ae3e31762..2725fcb2b 100644 --- a/src/yuzu/configuration/configure_input.cpp +++ b/src/yuzu/configuration/configure_input.cpp @@ -70,7 +70,8 @@ ConfigureInput::ConfigureInput(QWidget* parent) ConfigureInput::~ConfigureInput() = default; -void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem) { +void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem, + std::size_t max_players) { player_controllers = { new ConfigureInputPlayer(this, 0, ui->consoleInputSettings, input_subsystem), new ConfigureInputPlayer(this, 1, ui->consoleInputSettings, input_subsystem), @@ -93,6 +94,11 @@ void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem) { ui->checkboxPlayer7Connected, ui->checkboxPlayer8Connected, }; + std::array<QLabel*, 8> player_connected_labels = { + ui->label, ui->label_3, ui->label_4, ui->label_5, + ui->label_6, ui->label_7, ui->label_8, ui->label_9, + }; + for (std::size_t i = 0; i < player_tabs.size(); ++i) { player_tabs[i]->setLayout(new QHBoxLayout(player_tabs[i])); player_tabs[i]->layout()->addWidget(player_controllers[i]); @@ -112,6 +118,13 @@ void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem) { connect(player_connected[i], &QCheckBox::stateChanged, [this, i](int state) { player_controllers[i]->ConnectPlayer(state == Qt::Checked); }); + + // Remove/hide all the elements that exceed max_players, if applicable. + if (i >= max_players) { + ui->tabWidget->removeTab(static_cast<int>(max_players)); + player_connected[i]->hide(); + player_connected_labels[i]->hide(); + } } // Only the first player can choose handheld mode so connect the signal just to player 1 connect(player_controllers[0], &ConfigureInputPlayer::HandheldStateChanged, @@ -133,6 +146,10 @@ void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem) { CallConfigureDialog<ConfigureMotionTouch>(*this, input_subsystem); }); + connect(ui->motionButton, &QPushButton::clicked, [this, input_subsystem] { + CallConfigureDialog<ConfigureMotionTouch>(*this, input_subsystem); + }); + connect(ui->buttonClearAll, &QPushButton::clicked, [this] { ClearAll(); }); connect(ui->buttonRestoreDefaults, &QPushButton::clicked, [this] { RestoreDefaults(); }); @@ -159,6 +176,7 @@ void ConfigureInput::ApplyConfiguration() { OnDockedModeChanged(pre_docked_mode, Settings::values.use_docked_mode); Settings::values.vibration_enabled = ui->vibrationGroup->isChecked(); + Settings::values.motion_enabled = ui->motionGroup->isChecked(); } void ConfigureInput::changeEvent(QEvent* event) { @@ -175,10 +193,10 @@ void ConfigureInput::RetranslateUI() { void ConfigureInput::LoadConfiguration() { LoadPlayerControllerIndices(); - UpdateDockedState(Settings::values.players[0].controller_type == - Settings::ControllerType::Handheld); + UpdateDockedState(Settings::values.players[8].connected); ui->vibrationGroup->setChecked(Settings::values.vibration_enabled); + ui->motionGroup->setChecked(Settings::values.motion_enabled); } void ConfigureInput::LoadPlayerControllerIndices() { @@ -205,17 +223,18 @@ void ConfigureInput::RestoreDefaults() { ui->radioDocked->setChecked(true); ui->radioUndocked->setChecked(false); ui->vibrationGroup->setChecked(true); + ui->motionGroup->setChecked(true); } void ConfigureInput::UpdateDockedState(bool is_handheld) { - // If the controller type is handheld only, disallow changing docked mode + // Disallow changing the console mode if the controller type is handheld. ui->radioDocked->setEnabled(!is_handheld); ui->radioUndocked->setEnabled(!is_handheld); ui->radioDocked->setChecked(Settings::values.use_docked_mode); ui->radioUndocked->setChecked(!Settings::values.use_docked_mode); - // If its handheld only, force docked mode off (since you can't play handheld in a dock) + // Also force into undocked mode if the controller type is handheld. if (is_handheld) { ui->radioUndocked->setChecked(true); } diff --git a/src/yuzu/configuration/configure_input.h b/src/yuzu/configuration/configure_input.h index d08a24f96..0e8b2fd4e 100644 --- a/src/yuzu/configuration/configure_input.h +++ b/src/yuzu/configuration/configure_input.h @@ -37,7 +37,7 @@ public: ~ConfigureInput() override; /// Initializes the input dialog with the given input subsystem. - void Initialize(InputCommon::InputSubsystem* input_subsystem_); + void Initialize(InputCommon::InputSubsystem* input_subsystem_, std::size_t max_players = 8); /// Save all button configurations to settings file. void ApplyConfiguration(); diff --git a/src/yuzu/configuration/configure_input_dialog.cpp b/src/yuzu/configuration/configure_input_dialog.cpp new file mode 100644 index 000000000..1866003c2 --- /dev/null +++ b/src/yuzu/configuration/configure_input_dialog.cpp @@ -0,0 +1,37 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "ui_configure_input_dialog.h" +#include "yuzu/configuration/configure_input_dialog.h" + +ConfigureInputDialog::ConfigureInputDialog(QWidget* parent, std::size_t max_players, + InputCommon::InputSubsystem* input_subsystem) + : QDialog(parent), ui(std::make_unique<Ui::ConfigureInputDialog>()), + input_widget(new ConfigureInput(this)) { + ui->setupUi(this); + + input_widget->Initialize(input_subsystem, max_players); + + ui->inputLayout->addWidget(input_widget); + + RetranslateUI(); +} + +ConfigureInputDialog::~ConfigureInputDialog() = default; + +void ConfigureInputDialog::ApplyConfiguration() { + input_widget->ApplyConfiguration(); +} + +void ConfigureInputDialog::changeEvent(QEvent* event) { + if (event->type() == QEvent::LanguageChange) { + RetranslateUI(); + } + + QDialog::changeEvent(event); +} + +void ConfigureInputDialog::RetranslateUI() { + ui->retranslateUi(this); +} diff --git a/src/yuzu/configuration/configure_input_dialog.h b/src/yuzu/configuration/configure_input_dialog.h new file mode 100644 index 000000000..d1bd865f9 --- /dev/null +++ b/src/yuzu/configuration/configure_input_dialog.h @@ -0,0 +1,38 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <memory> +#include <QDialog> +#include "yuzu/configuration/configure_input.h" + +class QPushButton; + +namespace InputCommon { +class InputSubsystem; +} + +namespace Ui { +class ConfigureInputDialog; +} + +class ConfigureInputDialog : public QDialog { + Q_OBJECT + +public: + explicit ConfigureInputDialog(QWidget* parent, std::size_t max_players, + InputCommon::InputSubsystem* input_subsystem); + ~ConfigureInputDialog() override; + + void ApplyConfiguration(); + +private: + void changeEvent(QEvent* event) override; + void RetranslateUI(); + + std::unique_ptr<Ui::ConfigureInputDialog> ui; + + ConfigureInput* input_widget; +}; diff --git a/src/yuzu/configuration/configure_input_dialog.ui b/src/yuzu/configuration/configure_input_dialog.ui new file mode 100644 index 000000000..b92ddb200 --- /dev/null +++ b/src/yuzu/configuration/configure_input_dialog.ui @@ -0,0 +1,57 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>ConfigureInputDialog</class> + <widget class="QDialog" name="ConfigureInputDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>70</width> + <height>540</height> + </rect> + </property> + <property name="windowTitle"> + <string>Configure Input</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <property name="spacing"> + <number>2</number> + </property> + <property name="leftMargin"> + <number>9</number> + </property> + <property name="topMargin"> + <number>9</number> + </property> + <property name="rightMargin"> + <number>9</number> + </property> + <property name="bottomMargin"> + <number>9</number> + </property> + <item> + <layout class="QHBoxLayout" name="inputLayout"/> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="standardButtons"> + <set>QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>buttonBox</sender> + <signal>accepted()</signal> + <receiver>ConfigureInputDialog</receiver> + <slot>accept()</slot> + </connection> + </connections> +</ui> diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp index 13ecb3dc5..698cb1940 100644 --- a/src/yuzu/configuration/configure_input_player.cpp +++ b/src/yuzu/configuration/configure_input_player.cpp @@ -18,6 +18,7 @@ #include "core/hle/service/sm/sm.h" #include "input_common/gcadapter/gc_poller.h" #include "input_common/main.h" +#include "input_common/udp/udp.h" #include "ui_configure_input_player.h" #include "yuzu/configuration/config.h" #include "yuzu/configuration/configure_input_player.h" @@ -149,6 +150,14 @@ QString ButtonToText(const Common::ParamPackage& param) { return GetKeyName(param.Get("code", 0)); } + if (param.Get("engine", "") == "cemuhookudp") { + if (param.Has("pad_index")) { + const QString motion_str = QString::fromStdString(param.Get("pad_index", "")); + return QObject::tr("Motion %1").arg(motion_str); + } + return GetKeyName(param.Get("code", 0)); + } + if (param.Get("engine", "") == "sdl") { if (param.Has("hat")) { const QString hat_str = QString::fromStdString(param.Get("hat", "")); @@ -247,6 +256,11 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i ui->buttonSL, ui->buttonSR, ui->buttonHome, ui->buttonScreenshot, }; + mod_buttons = { + ui->buttonLStickMod, + ui->buttonRStickMod, + }; + analog_map_buttons = {{ { ui->buttonLStickUp, @@ -262,6 +276,11 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i }, }}; + motion_map = { + ui->buttonMotionLeft, + ui->buttonMotionRight, + }; + analog_map_deadzone_label = {ui->labelLStickDeadzone, ui->labelRStickDeadzone}; analog_map_deadzone_slider = {ui->sliderLStickDeadzone, ui->sliderRStickDeadzone}; analog_map_modifier_groupbox = {ui->buttonLStickModGroup, ui->buttonRStickModGroup}; @@ -271,7 +290,7 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i analog_map_range_spinbox = {ui->spinboxLStickRange, ui->spinboxRStickRange}; const auto ConfigureButtonClick = [&](QPushButton* button, Common::ParamPackage* param, - int default_val) { + int default_val, InputCommon::Polling::DeviceType type) { connect(button, &QPushButton::clicked, [=, this] { HandleClick( button, @@ -291,22 +310,56 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i } *param = std::move(params); }, - InputCommon::Polling::DeviceType::Button); + type); }); }; for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) { auto* const button = button_map[button_id]; + if (button == nullptr) { continue; } + ConfigureButtonClick(button_map[button_id], &buttons_param[button_id], - Config::default_buttons[button_id]); + Config::default_buttons[button_id], + InputCommon::Polling::DeviceType::Button); + + button->setContextMenuPolicy(Qt::CustomContextMenu); + + connect(button, &QPushButton::customContextMenuRequested, + [=, this](const QPoint& menu_location) { + QMenu context_menu; + context_menu.addAction(tr("Clear"), [&] { + buttons_param[button_id].Clear(); + button_map[button_id]->setText(tr("[not set]")); + }); + context_menu.exec(button_map[button_id]->mapToGlobal(menu_location)); + }); } - // Handle clicks for the modifier buttons as well. - ConfigureButtonClick(ui->buttonLStickMod, &lstick_mod, Config::default_stick_mod[0]); - ConfigureButtonClick(ui->buttonRStickMod, &rstick_mod, Config::default_stick_mod[1]); + for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) { + auto* const button = motion_map[motion_id]; + if (button == nullptr) { + continue; + } + + ConfigureButtonClick(motion_map[motion_id], &motions_param[motion_id], + Config::default_motions[motion_id], + InputCommon::Polling::DeviceType::Motion); + + button->setContextMenuPolicy(Qt::CustomContextMenu); + + connect(button, &QPushButton::customContextMenuRequested, + [=, this](const QPoint& menu_location) { + QMenu context_menu; + context_menu.addAction(tr("Clear"), [&] { + motions_param[motion_id].Clear(); + motion_map[motion_id]->setText(tr("[not set]")); + }); + context_menu.exec(motion_map[motion_id]->mapToGlobal(menu_location)); + }); + } for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) { for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) { @@ -325,8 +378,38 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i }, InputCommon::Polling::DeviceType::AnalogPreferred); }); + + analog_button->setContextMenuPolicy(Qt::CustomContextMenu); + + connect(analog_button, &QPushButton::customContextMenuRequested, + [=, this](const QPoint& menu_location) { + QMenu context_menu; + context_menu.addAction(tr("Clear"), [&] { + analogs_param[analog_id].Clear(); + analog_map_buttons[analog_id][sub_button_id]->setText(tr("[not set]")); + }); + context_menu.exec(analog_map_buttons[analog_id][sub_button_id]->mapToGlobal( + menu_location)); + }); } + // Handle clicks for the modifier buttons as well. + ConfigureButtonClick(mod_buttons[analog_id], &stick_mod_param[analog_id], + Config::default_stick_mod[analog_id], + InputCommon::Polling::DeviceType::Button); + + mod_buttons[analog_id]->setContextMenuPolicy(Qt::CustomContextMenu); + + connect(mod_buttons[analog_id], &QPushButton::customContextMenuRequested, + [=, this](const QPoint& menu_location) { + QMenu context_menu; + context_menu.addAction(tr("Clear"), [&] { + stick_mod_param[analog_id].Clear(); + mod_buttons[analog_id]->setText(tr("[not set]")); + }); + context_menu.exec(mod_buttons[analog_id]->mapToGlobal(menu_location)); + }); + connect(analog_map_range_spinbox[analog_id], qOverload<int>(&QSpinBox::valueChanged), [=, this] { const auto spinbox_value = analog_map_range_spinbox[analog_id]->value(); @@ -385,9 +468,11 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i UpdateControllerIcon(); UpdateControllerAvailableButtons(); + UpdateMotionButtons(); connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged), [this](int) { UpdateControllerIcon(); UpdateControllerAvailableButtons(); + UpdateMotionButtons(); }); connect(ui->comboDevices, qOverload<int>(&QComboBox::currentIndexChanged), this, @@ -417,6 +502,13 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i return; } } + if (input_subsystem->GetUDPMotions()->IsPolling()) { + params = input_subsystem->GetUDPMotions()->GetNextInput(); + if (params.Has("engine")) { + SetPollingResult(params, false); + return; + } + } for (auto& poller : device_pollers) { params = poller->GetNextInput(); if (params.Has("engine")) { @@ -448,6 +540,10 @@ void ConfigureInputPlayer::ApplyConfiguration() { return; } + auto& motions = player.motions; + std::transform(motions_param.begin(), motions_param.end(), motions.begin(), + [](const Common::ParamPackage& param) { return param.Serialize(); }); + player.controller_type = static_cast<Settings::ControllerType>(ui->comboControllerType->currentIndex()); player.connected = ui->groupConnectedController->isChecked(); @@ -501,6 +597,8 @@ void ConfigureInputPlayer::LoadConfiguration() { [](const std::string& str) { return Common::ParamPackage(str); }); std::transform(player.analogs.begin(), player.analogs.end(), analogs_param.begin(), [](const std::string& str) { return Common::ParamPackage(str); }); + std::transform(player.motions.begin(), player.motions.end(), motions_param.begin(), + [](const std::string& str) { return Common::ParamPackage(str); }); } UpdateUI(); @@ -530,20 +628,23 @@ void ConfigureInputPlayer::RestoreDefaults() { InputCommon::GenerateKeyboardParam(Config::default_buttons[button_id])}; } - // Reset Modifier Buttons - lstick_mod = - Common::ParamPackage(InputCommon::GenerateKeyboardParam(Config::default_stick_mod[0])); - rstick_mod = - Common::ParamPackage(InputCommon::GenerateKeyboardParam(Config::default_stick_mod[1])); - - // Reset Analogs + // Reset Analogs and Modifier Buttons for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) { for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) { Common::ParamPackage params{InputCommon::GenerateKeyboardParam( Config::default_analogs[analog_id][sub_button_id])}; SetAnalogParam(params, analogs_param[analog_id], analog_sub_buttons[sub_button_id]); } + + stick_mod_param[analog_id] = Common::ParamPackage( + InputCommon::GenerateKeyboardParam(Config::default_stick_mod[analog_id])); + } + + for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) { + motions_param[motion_id] = Common::ParamPackage{ + InputCommon::GenerateKeyboardParam(Config::default_motions[motion_id])}; } + UpdateUI(); UpdateInputDevices(); ui->comboControllerType->setCurrentIndex(0); @@ -552,25 +653,33 @@ void ConfigureInputPlayer::RestoreDefaults() { void ConfigureInputPlayer::ClearAll() { for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) { const auto* const button = button_map[button_id]; - if (button == nullptr || !button->isEnabled()) { + if (button == nullptr) { continue; } buttons_param[button_id].Clear(); } - lstick_mod.Clear(); - rstick_mod.Clear(); - for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) { for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) { const auto* const analog_button = analog_map_buttons[analog_id][sub_button_id]; - if (analog_button == nullptr || !analog_button->isEnabled()) { + if (analog_button == nullptr) { continue; } analogs_param[analog_id].Clear(); } + + stick_mod_param[analog_id].Clear(); + } + + for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) { + const auto* const motion_button = motion_map[motion_id]; + if (motion_button == nullptr) { + continue; + } + + motions_param[motion_id].Clear(); } UpdateUI(); @@ -582,8 +691,9 @@ void ConfigureInputPlayer::UpdateUI() { button_map[button]->setText(ButtonToText(buttons_param[button])); } - ui->buttonLStickMod->setText(ButtonToText(lstick_mod)); - ui->buttonRStickMod->setText(ButtonToText(rstick_mod)); + for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) { + motion_map[motion_id]->setText(ButtonToText(motions_param[motion_id])); + } for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) { for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) { @@ -597,6 +707,8 @@ void ConfigureInputPlayer::UpdateUI() { AnalogToText(analogs_param[analog_id], analog_sub_buttons[sub_button_id])); } + mod_buttons[analog_id]->setText(ButtonToText(stick_mod_param[analog_id])); + const auto deadzone_label = analog_map_deadzone_label[analog_id]; const auto deadzone_slider = analog_map_deadzone_slider[analog_id]; const auto modifier_groupbox = analog_map_modifier_groupbox[analog_id]; @@ -646,10 +758,10 @@ void ConfigureInputPlayer::UpdateMappingWithDefaults() { const auto& device = input_devices[ui->comboDevices->currentIndex()]; auto button_mapping = input_subsystem->GetButtonMappingForDevice(device); auto analog_mapping = input_subsystem->GetAnalogMappingForDevice(device); - for (int i = 0; i < buttons_param.size(); ++i) { + for (std::size_t i = 0; i < buttons_param.size(); ++i) { buttons_param[i] = button_mapping[static_cast<Settings::NativeButton::Values>(i)]; } - for (int i = 0; i < analogs_param.size(); ++i) { + for (std::size_t i = 0; i < analogs_param.size(); ++i) { analogs_param[i] = analog_mapping[static_cast<Settings::NativeAnalog::Values>(i)]; } @@ -659,7 +771,11 @@ void ConfigureInputPlayer::UpdateMappingWithDefaults() { void ConfigureInputPlayer::HandleClick( QPushButton* button, std::function<void(const Common::ParamPackage&)> new_input_setter, InputCommon::Polling::DeviceType type) { - button->setText(tr("[waiting]")); + if (button == ui->buttonMotionLeft || button == ui->buttonMotionRight) { + button->setText(tr("Shake!")); + } else { + button->setText(tr("[waiting]")); + } button->setFocus(); // The first two input devices are always Any and Keyboard/Mouse. If the user filtered to a @@ -683,6 +799,10 @@ void ConfigureInputPlayer::HandleClick( input_subsystem->GetGCAnalogs()->BeginConfiguration(); } + if (type == InputCommon::Polling::DeviceType::Motion) { + input_subsystem->GetUDPMotions()->BeginConfiguration(); + } + timeout_timer->start(2500); // Cancel after 2.5 seconds poll_timer->start(50); // Check for new inputs every 50ms } @@ -700,6 +820,8 @@ void ConfigureInputPlayer::SetPollingResult(const Common::ParamPackage& params, input_subsystem->GetGCButtons()->EndConfiguration(); input_subsystem->GetGCAnalogs()->EndConfiguration(); + input_subsystem->GetUDPMotions()->EndConfiguration(); + if (!abort) { (*input_setter)(params); } @@ -832,6 +954,37 @@ void ConfigureInputPlayer::UpdateControllerAvailableButtons() { } } +void ConfigureInputPlayer::UpdateMotionButtons() { + if (debug) { + // Motion isn't used with the debug controller, hide both groupboxes. + ui->buttonMotionLeftGroup->hide(); + ui->buttonMotionRightGroup->hide(); + return; + } + + // Show/hide the "Motion 1/2" groupboxes depending on the currently selected controller. + switch (GetControllerTypeFromIndex(ui->comboControllerType->currentIndex())) { + case Settings::ControllerType::ProController: + case Settings::ControllerType::LeftJoycon: + case Settings::ControllerType::Handheld: + // Show "Motion 1" and hide "Motion 2". + ui->buttonMotionLeftGroup->show(); + ui->buttonMotionRightGroup->hide(); + break; + case Settings::ControllerType::RightJoycon: + // Show "Motion 2" and hide "Motion 1". + ui->buttonMotionLeftGroup->hide(); + ui->buttonMotionRightGroup->show(); + break; + case Settings::ControllerType::DualJoyconDetached: + default: + // Show both "Motion 1/2". + ui->buttonMotionLeftGroup->show(); + ui->buttonMotionRightGroup->show(); + break; + } +} + void ConfigureInputPlayer::showEvent(QShowEvent* event) { if (bottom_row == nullptr) { return; diff --git a/src/yuzu/configuration/configure_input_player.h b/src/yuzu/configuration/configure_input_player.h index a25bc3bd9..ce443dec5 100644 --- a/src/yuzu/configuration/configure_input_player.h +++ b/src/yuzu/configuration/configure_input_player.h @@ -107,6 +107,9 @@ private: /// Hides and disables controller settings based on the current controller type. void UpdateControllerAvailableButtons(); + /// Shows or hides motion groupboxes based on the current controller type. + void UpdateMotionButtons(); + /// Gets the default controller mapping for this device and auto configures the input to match. void UpdateMappingWithDefaults(); @@ -128,14 +131,17 @@ private: std::array<Common::ParamPackage, Settings::NativeButton::NumButtons> buttons_param; std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs> analogs_param; + std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs> stick_mod_param; + std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions> motions_param; static constexpr int ANALOG_SUB_BUTTONS_NUM = 4; /// Each button input is represented by a QPushButton. std::array<QPushButton*, Settings::NativeButton::NumButtons> button_map; + /// Each motion input is represented by a QPushButton. + std::array<QPushButton*, Settings::NativeMotion::NumMotions> motion_map; /// Extra buttons for the modifiers. - Common::ParamPackage lstick_mod; - Common::ParamPackage rstick_mod; + std::array<QPushButton*, Settings::NativeAnalog::NumAnalogs> mod_buttons; /// A group of four QPushButtons represent one analog input. The buttons each represent up, /// down, left, right, respectively. diff --git a/src/yuzu/configuration/configure_input_player.ui b/src/yuzu/configuration/configure_input_player.ui index 9bc681894..e03461d9d 100644 --- a/src/yuzu/configuration/configure_input_player.ui +++ b/src/yuzu/configuration/configure_input_player.ui @@ -1983,6 +1983,9 @@ <property name="spacing"> <number>3</number> </property> + <property name="topMargin"> + <number>0</number> + </property> <item> <spacer name="horizontalSpacerMiscButtons1"> <property name="orientation"> @@ -1990,21 +1993,119 @@ </property> <property name="sizeHint" stdset="0"> <size> - <width>40</width> - <height>0</height> + <width>20</width> + <height>20</height> </size> </property> </spacer> </item> <item> + <widget class="QGroupBox" name="buttonMotionLeftGroup"> + <property name="title"> + <string>Motion 1</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <layout class="QVBoxLayout" name="buttonDpadLeftVerticalLayout_2"> + <property name="spacing"> + <number>3</number> + </property> + <property name="leftMargin"> + <number>3</number> + </property> + <property name="topMargin"> + <number>3</number> + </property> + <property name="rightMargin"> + <number>3</number> + </property> + <property name="bottomMargin"> + <number>3</number> + </property> + <item> + <widget class="QPushButton" name="buttonMotionLeft"> + <property name="minimumSize"> + <size> + <width>57</width> + <height>0</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>55</width> + <height>16777215</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">min-width: 55px;</string> + </property> + <property name="text"> + <string>Left</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QGroupBox" name="buttonMotionRightGroup"> + <property name="title"> + <string>Motion 2</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <layout class="QVBoxLayout" name="buttonDpadRightVerticalLayout_2"> + <property name="spacing"> + <number>3</number> + </property> + <property name="leftMargin"> + <number>3</number> + </property> + <property name="topMargin"> + <number>3</number> + </property> + <property name="rightMargin"> + <number>3</number> + </property> + <property name="bottomMargin"> + <number>3</number> + </property> + <item> + <widget class="QPushButton" name="buttonMotionRight"> + <property name="minimumSize"> + <size> + <width>57</width> + <height>0</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>55</width> + <height>16777215</height> + </size> + </property> + <property name="styleSheet"> + <string notr="true">min-width: 55px;</string> + </property> + <property name="text"> + <string>Right</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> <spacer name="horizontalSpacerMiscButtons4"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> - <width>40</width> - <height>0</height> + <width>20</width> + <height>20</height> </size> </property> </spacer> diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index a1b61d119..bb3a08ac7 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -11,6 +11,7 @@ #endif // VFS includes must be before glad as they will conflict with Windows file api, which uses defines. +#include "applets/controller.h" #include "applets/error.h" #include "applets/profile_select.h" #include "applets/software_keyboard.h" @@ -19,7 +20,9 @@ #include "configuration/configure_per_game.h" #include "core/file_sys/vfs.h" #include "core/file_sys/vfs_real.h" +#include "core/frontend/applets/controller.h" #include "core/frontend/applets/general_frontend.h" +#include "core/frontend/applets/software_keyboard.h" #include "core/hle/service/acc/profile_manager.h" #include "core/hle/service/am/applet_ae.h" #include "core/hle/service/am/applet_oe.h" @@ -84,7 +87,6 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual #include "core/file_sys/romfs.h" #include "core/file_sys/savedata_factory.h" #include "core/file_sys/submission_package.h" -#include "core/frontend/applets/software_keyboard.h" #include "core/hle/kernel/process.h" #include "core/hle/service/am/am.h" #include "core/hle/service/filesystem/filesystem.h" @@ -283,6 +285,23 @@ GMainWindow::~GMainWindow() { delete render_window; } +void GMainWindow::ControllerSelectorReconfigureControllers( + const Core::Frontend::ControllerParameters& parameters) { + QtControllerSelectorDialog dialog(this, parameters, input_subsystem.get()); + dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | + Qt::WindowSystemMenuHint); + dialog.setWindowModality(Qt::WindowModal); + dialog.exec(); + + emit ControllerSelectorReconfigureFinished(); + + // Don't forget to apply settings. + Settings::Apply(); + config->Save(); + + UpdateStatusButtons(); +} + void GMainWindow::ProfileSelectorSelectProfile() { const Service::Account::ProfileManager manager; int index = 0; @@ -291,10 +310,12 @@ void GMainWindow::ProfileSelectorSelectProfile() { dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint); dialog.setWindowModality(Qt::WindowModal); + if (dialog.exec() == QDialog::Rejected) { emit ProfileSelectorFinishedSelection(std::nullopt); return; } + index = dialog.GetIndex(); } @@ -966,13 +987,14 @@ bool GMainWindow::LoadROM(const QString& filename) { system.SetFilesystem(vfs); system.SetAppletFrontendSet({ - nullptr, // Parental Controls - std::make_unique<QtErrorDisplay>(*this), // - nullptr, // Photo Viewer - std::make_unique<QtProfileSelector>(*this), // - std::make_unique<QtSoftwareKeyboard>(*this), // - std::make_unique<QtWebBrowser>(*this), // - nullptr, // E-Commerce + std::make_unique<QtControllerSelector>(*this), // Controller Selector + nullptr, // E-Commerce + std::make_unique<QtErrorDisplay>(*this), // Error Display + nullptr, // Parental Controls + nullptr, // Photo Viewer + std::make_unique<QtProfileSelector>(*this), // Profile Selector + std::make_unique<QtSoftwareKeyboard>(*this), // Software Keyboard + std::make_unique<QtWebBrowser>(*this), // Web Browser }); system.RegisterHostThread(); @@ -2047,6 +2069,7 @@ void GMainWindow::OnStartGame() { emu_thread->SetRunning(true); + qRegisterMetaType<Core::Frontend::ControllerParameters>("Core::Frontend::ControllerParameters"); qRegisterMetaType<Core::Frontend::SoftwareKeyboardParameters>( "Core::Frontend::SoftwareKeyboardParameters"); qRegisterMetaType<Core::System::ResultStatus>("Core::System::ResultStatus"); @@ -2569,8 +2592,10 @@ void GMainWindow::OnReinitializeKeys(ReinitializeKeyBehavior behavior) { const auto function = [this, &keys, &pdm] { keys.PopulateFromPartitionData(pdm); - Core::System::GetInstance().GetFileSystemController().CreateFactories(*vfs); - keys.DeriveETicket(pdm); + + auto& system = Core::System::GetInstance(); + system.GetFileSystemController().CreateFactories(*vfs); + keys.DeriveETicket(pdm, system.GetContentProvider()); }; QString errors; diff --git a/src/yuzu/main.h b/src/yuzu/main.h index 0ce66a1ca..afcfa68a9 100644 --- a/src/yuzu/main.h +++ b/src/yuzu/main.h @@ -37,6 +37,7 @@ enum class InstalledEntryType; class GameListPlaceholder; namespace Core::Frontend { +struct ControllerParameters; struct SoftwareKeyboardParameters; } // namespace Core::Frontend @@ -116,9 +117,12 @@ signals: void UpdateInstallProgress(); + void ControllerSelectorReconfigureFinished(); + void ErrorDisplayFinished(); void ProfileSelectorFinishedSelection(std::optional<Common::UUID> uuid); + void SoftwareKeyboardFinishedText(std::optional<std::u16string> text); void SoftwareKeyboardFinishedCheckDialog(); @@ -127,6 +131,8 @@ signals: public slots: void OnLoadComplete(); + void ControllerSelectorReconfigureControllers( + const Core::Frontend::ControllerParameters& parameters); void ErrorDisplayDisplayError(QString body); void ProfileSelectorSelectProfile(); void SoftwareKeyboardGetText(const Core::Frontend::SoftwareKeyboardParameters& parameters); diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index e9f1c6500..23448e747 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -290,6 +290,8 @@ void Config::ReadValues() { Settings::values.vibration_enabled = sdl2_config->GetBoolean("ControlsGeneral", "vibration_enabled", true); + Settings::values.motion_enabled = + sdl2_config->GetBoolean("ControlsGeneral", "motion_enabled", true); Settings::values.touchscreen.enabled = sdl2_config->GetBoolean("ControlsGeneral", "touch_enabled", true); Settings::values.touchscreen.device = diff --git a/src/yuzu_tester/config.cpp b/src/yuzu_tester/config.cpp index aaf59129a..bc273fb51 100644 --- a/src/yuzu_tester/config.cpp +++ b/src/yuzu_tester/config.cpp @@ -76,6 +76,7 @@ void Config::ReadValues() { } Settings::values.vibration_enabled = true; + Settings::values.motion_enabled = true; Settings::values.touchscreen.enabled = ""; Settings::values.touchscreen.device = ""; Settings::values.touchscreen.finger = 0; |