GamepadInputConfig.cs
1 using Ryujinx.Ava.UI.ViewModels; 2 using Ryujinx.Common.Configuration.Hid; 3 using Ryujinx.Common.Configuration.Hid.Controller; 4 using Ryujinx.Common.Configuration.Hid.Controller.Motion; 5 using System; 6 7 namespace Ryujinx.Ava.UI.Models.Input 8 { 9 public class GamepadInputConfig : BaseModel 10 { 11 public bool EnableCemuHookMotion { get; set; } 12 public string DsuServerHost { get; set; } 13 public int DsuServerPort { get; set; } 14 public int Slot { get; set; } 15 public int AltSlot { get; set; } 16 public bool MirrorInput { get; set; } 17 public int Sensitivity { get; set; } 18 public double GyroDeadzone { get; set; } 19 20 public float WeakRumble { get; set; } 21 public float StrongRumble { get; set; } 22 23 public string Id { get; set; } 24 public ControllerType ControllerType { get; set; } 25 public PlayerIndex PlayerIndex { get; set; } 26 27 private StickInputId _leftJoystick; 28 public StickInputId LeftJoystick 29 { 30 get => _leftJoystick; 31 set 32 { 33 _leftJoystick = value; 34 OnPropertyChanged(); 35 } 36 } 37 38 private bool _leftInvertStickX; 39 public bool LeftInvertStickX 40 { 41 get => _leftInvertStickX; 42 set 43 { 44 _leftInvertStickX = value; 45 OnPropertyChanged(); 46 } 47 } 48 49 private bool _leftInvertStickY; 50 public bool LeftInvertStickY 51 { 52 get => _leftInvertStickY; 53 set 54 { 55 _leftInvertStickY = value; 56 OnPropertyChanged(); 57 } 58 } 59 60 private bool _leftRotate90; 61 public bool LeftRotate90 62 { 63 get => _leftRotate90; 64 set 65 { 66 _leftRotate90 = value; 67 OnPropertyChanged(); 68 } 69 } 70 71 private GamepadInputId _leftStickButton; 72 public GamepadInputId LeftStickButton 73 { 74 get => _leftStickButton; 75 set 76 { 77 _leftStickButton = value; 78 OnPropertyChanged(); 79 } 80 } 81 82 private StickInputId _rightJoystick; 83 public StickInputId RightJoystick 84 { 85 get => _rightJoystick; 86 set 87 { 88 _rightJoystick = value; 89 OnPropertyChanged(); 90 } 91 } 92 93 private bool _rightInvertStickX; 94 public bool RightInvertStickX 95 { 96 get => _rightInvertStickX; 97 set 98 { 99 _rightInvertStickX = value; 100 OnPropertyChanged(); 101 } 102 } 103 104 private bool _rightInvertStickY; 105 public bool RightInvertStickY 106 { 107 get => _rightInvertStickY; 108 set 109 { 110 _rightInvertStickY = value; 111 OnPropertyChanged(); 112 } 113 } 114 115 private bool _rightRotate90; 116 public bool RightRotate90 117 { 118 get => _rightRotate90; 119 set 120 { 121 _rightRotate90 = value; 122 OnPropertyChanged(); 123 } 124 } 125 126 private GamepadInputId _rightStickButton; 127 public GamepadInputId RightStickButton 128 { 129 get => _rightStickButton; 130 set 131 { 132 _rightStickButton = value; 133 OnPropertyChanged(); 134 } 135 } 136 137 private GamepadInputId _dpadUp; 138 public GamepadInputId DpadUp 139 { 140 get => _dpadUp; 141 set 142 { 143 _dpadUp = value; 144 OnPropertyChanged(); 145 } 146 } 147 148 private GamepadInputId _dpadDown; 149 public GamepadInputId DpadDown 150 { 151 get => _dpadDown; 152 set 153 { 154 _dpadDown = value; 155 OnPropertyChanged(); 156 } 157 } 158 159 private GamepadInputId _dpadLeft; 160 public GamepadInputId DpadLeft 161 { 162 get => _dpadLeft; 163 set 164 { 165 _dpadLeft = value; 166 OnPropertyChanged(); 167 } 168 } 169 170 private GamepadInputId _dpadRight; 171 public GamepadInputId DpadRight 172 { 173 get => _dpadRight; 174 set 175 { 176 _dpadRight = value; 177 OnPropertyChanged(); 178 } 179 } 180 181 private GamepadInputId _buttonL; 182 public GamepadInputId ButtonL 183 { 184 get => _buttonL; 185 set 186 { 187 _buttonL = value; 188 OnPropertyChanged(); 189 } 190 } 191 192 private GamepadInputId _buttonMinus; 193 public GamepadInputId ButtonMinus 194 { 195 get => _buttonMinus; 196 set 197 { 198 _buttonMinus = value; 199 OnPropertyChanged(); 200 } 201 } 202 203 private GamepadInputId _leftButtonSl; 204 public GamepadInputId LeftButtonSl 205 { 206 get => _leftButtonSl; 207 set 208 { 209 _leftButtonSl = value; 210 OnPropertyChanged(); 211 } 212 } 213 214 private GamepadInputId _leftButtonSr; 215 public GamepadInputId LeftButtonSr 216 { 217 get => _leftButtonSr; 218 set 219 { 220 _leftButtonSr = value; 221 OnPropertyChanged(); 222 } 223 } 224 225 private GamepadInputId _buttonZl; 226 public GamepadInputId ButtonZl 227 { 228 get => _buttonZl; 229 set 230 { 231 _buttonZl = value; 232 OnPropertyChanged(); 233 } 234 } 235 236 private GamepadInputId _buttonA; 237 public GamepadInputId ButtonA 238 { 239 get => _buttonA; 240 set 241 { 242 _buttonA = value; 243 OnPropertyChanged(); 244 } 245 } 246 247 private GamepadInputId _buttonB; 248 public GamepadInputId ButtonB 249 { 250 get => _buttonB; 251 set 252 { 253 _buttonB = value; 254 OnPropertyChanged(); 255 } 256 } 257 258 private GamepadInputId _buttonX; 259 public GamepadInputId ButtonX 260 { 261 get => _buttonX; 262 set 263 { 264 _buttonX = value; 265 OnPropertyChanged(); 266 } 267 } 268 269 private GamepadInputId _buttonY; 270 public GamepadInputId ButtonY 271 { 272 get => _buttonY; 273 set 274 { 275 _buttonY = value; 276 OnPropertyChanged(); 277 } 278 } 279 280 private GamepadInputId _buttonR; 281 public GamepadInputId ButtonR 282 { 283 get => _buttonR; 284 set 285 { 286 _buttonR = value; 287 OnPropertyChanged(); 288 } 289 } 290 291 private GamepadInputId _buttonPlus; 292 public GamepadInputId ButtonPlus 293 { 294 get => _buttonPlus; 295 set 296 { 297 _buttonPlus = value; 298 OnPropertyChanged(); 299 } 300 } 301 302 private GamepadInputId _rightButtonSl; 303 public GamepadInputId RightButtonSl 304 { 305 get => _rightButtonSl; 306 set 307 { 308 _rightButtonSl = value; 309 OnPropertyChanged(); 310 } 311 } 312 313 private GamepadInputId _rightButtonSr; 314 public GamepadInputId RightButtonSr 315 { 316 get => _rightButtonSr; 317 set 318 { 319 _rightButtonSr = value; 320 OnPropertyChanged(); 321 } 322 } 323 324 private GamepadInputId _buttonZr; 325 public GamepadInputId ButtonZr 326 { 327 get => _buttonZr; 328 set 329 { 330 _buttonZr = value; 331 OnPropertyChanged(); 332 } 333 } 334 335 private float _deadzoneLeft; 336 public float DeadzoneLeft 337 { 338 get => _deadzoneLeft; 339 set 340 { 341 _deadzoneLeft = MathF.Round(value, 3); 342 OnPropertyChanged(); 343 } 344 } 345 346 private float _deadzoneRight; 347 public float DeadzoneRight 348 { 349 get => _deadzoneRight; 350 set 351 { 352 _deadzoneRight = MathF.Round(value, 3); 353 OnPropertyChanged(); 354 } 355 } 356 357 private float _rangeLeft; 358 public float RangeLeft 359 { 360 get => _rangeLeft; 361 set 362 { 363 _rangeLeft = MathF.Round(value, 3); 364 OnPropertyChanged(); 365 } 366 } 367 368 private float _rangeRight; 369 public float RangeRight 370 { 371 get => _rangeRight; 372 set 373 { 374 _rangeRight = MathF.Round(value, 3); 375 OnPropertyChanged(); 376 } 377 } 378 379 private float _triggerThreshold; 380 public float TriggerThreshold 381 { 382 get => _triggerThreshold; 383 set 384 { 385 _triggerThreshold = MathF.Round(value, 3); 386 OnPropertyChanged(); 387 } 388 } 389 390 private bool _enableMotion; 391 public bool EnableMotion 392 { 393 get => _enableMotion; 394 set 395 { 396 _enableMotion = value; 397 OnPropertyChanged(); 398 } 399 } 400 401 private bool _enableRumble; 402 public bool EnableRumble 403 { 404 get => _enableRumble; 405 set 406 { 407 _enableRumble = value; 408 OnPropertyChanged(); 409 } 410 } 411 412 public GamepadInputConfig(InputConfig config) 413 { 414 if (config != null) 415 { 416 Id = config.Id; 417 ControllerType = config.ControllerType; 418 PlayerIndex = config.PlayerIndex; 419 420 if (config is not StandardControllerInputConfig controllerInput) 421 { 422 return; 423 } 424 425 LeftJoystick = controllerInput.LeftJoyconStick.Joystick; 426 LeftInvertStickX = controllerInput.LeftJoyconStick.InvertStickX; 427 LeftInvertStickY = controllerInput.LeftJoyconStick.InvertStickY; 428 LeftRotate90 = controllerInput.LeftJoyconStick.Rotate90CW; 429 LeftStickButton = controllerInput.LeftJoyconStick.StickButton; 430 431 RightJoystick = controllerInput.RightJoyconStick.Joystick; 432 RightInvertStickX = controllerInput.RightJoyconStick.InvertStickX; 433 RightInvertStickY = controllerInput.RightJoyconStick.InvertStickY; 434 RightRotate90 = controllerInput.RightJoyconStick.Rotate90CW; 435 RightStickButton = controllerInput.RightJoyconStick.StickButton; 436 437 DpadUp = controllerInput.LeftJoycon.DpadUp; 438 DpadDown = controllerInput.LeftJoycon.DpadDown; 439 DpadLeft = controllerInput.LeftJoycon.DpadLeft; 440 DpadRight = controllerInput.LeftJoycon.DpadRight; 441 ButtonL = controllerInput.LeftJoycon.ButtonL; 442 ButtonMinus = controllerInput.LeftJoycon.ButtonMinus; 443 LeftButtonSl = controllerInput.LeftJoycon.ButtonSl; 444 LeftButtonSr = controllerInput.LeftJoycon.ButtonSr; 445 ButtonZl = controllerInput.LeftJoycon.ButtonZl; 446 447 ButtonA = controllerInput.RightJoycon.ButtonA; 448 ButtonB = controllerInput.RightJoycon.ButtonB; 449 ButtonX = controllerInput.RightJoycon.ButtonX; 450 ButtonY = controllerInput.RightJoycon.ButtonY; 451 ButtonR = controllerInput.RightJoycon.ButtonR; 452 ButtonPlus = controllerInput.RightJoycon.ButtonPlus; 453 RightButtonSl = controllerInput.RightJoycon.ButtonSl; 454 RightButtonSr = controllerInput.RightJoycon.ButtonSr; 455 ButtonZr = controllerInput.RightJoycon.ButtonZr; 456 457 DeadzoneLeft = controllerInput.DeadzoneLeft; 458 DeadzoneRight = controllerInput.DeadzoneRight; 459 RangeLeft = controllerInput.RangeLeft; 460 RangeRight = controllerInput.RangeRight; 461 TriggerThreshold = controllerInput.TriggerThreshold; 462 463 if (controllerInput.Motion != null) 464 { 465 EnableMotion = controllerInput.Motion.EnableMotion; 466 GyroDeadzone = controllerInput.Motion.GyroDeadzone; 467 Sensitivity = controllerInput.Motion.Sensitivity; 468 469 if (controllerInput.Motion is CemuHookMotionConfigController cemuHook) 470 { 471 EnableCemuHookMotion = true; 472 DsuServerHost = cemuHook.DsuServerHost; 473 DsuServerPort = cemuHook.DsuServerPort; 474 Slot = cemuHook.Slot; 475 AltSlot = cemuHook.AltSlot; 476 MirrorInput = cemuHook.MirrorInput; 477 } 478 } 479 480 if (controllerInput.Rumble != null) 481 { 482 EnableRumble = controllerInput.Rumble.EnableRumble; 483 WeakRumble = controllerInput.Rumble.WeakRumble; 484 StrongRumble = controllerInput.Rumble.StrongRumble; 485 } 486 } 487 } 488 489 public InputConfig GetConfig() 490 { 491 var config = new StandardControllerInputConfig 492 { 493 Id = Id, 494 Backend = InputBackendType.GamepadSDL2, 495 PlayerIndex = PlayerIndex, 496 ControllerType = ControllerType, 497 LeftJoycon = new LeftJoyconCommonConfig<GamepadInputId> 498 { 499 DpadUp = DpadUp, 500 DpadDown = DpadDown, 501 DpadLeft = DpadLeft, 502 DpadRight = DpadRight, 503 ButtonL = ButtonL, 504 ButtonMinus = ButtonMinus, 505 ButtonSl = LeftButtonSl, 506 ButtonSr = LeftButtonSr, 507 ButtonZl = ButtonZl, 508 }, 509 RightJoycon = new RightJoyconCommonConfig<GamepadInputId> 510 { 511 ButtonA = ButtonA, 512 ButtonB = ButtonB, 513 ButtonX = ButtonX, 514 ButtonY = ButtonY, 515 ButtonPlus = ButtonPlus, 516 ButtonSl = RightButtonSl, 517 ButtonSr = RightButtonSr, 518 ButtonR = ButtonR, 519 ButtonZr = ButtonZr, 520 }, 521 LeftJoyconStick = new JoyconConfigControllerStick<GamepadInputId, StickInputId> 522 { 523 Joystick = LeftJoystick, 524 InvertStickX = LeftInvertStickX, 525 InvertStickY = LeftInvertStickY, 526 Rotate90CW = LeftRotate90, 527 StickButton = LeftStickButton, 528 }, 529 RightJoyconStick = new JoyconConfigControllerStick<GamepadInputId, StickInputId> 530 { 531 Joystick = RightJoystick, 532 InvertStickX = RightInvertStickX, 533 InvertStickY = RightInvertStickY, 534 Rotate90CW = RightRotate90, 535 StickButton = RightStickButton, 536 }, 537 Rumble = new RumbleConfigController 538 { 539 EnableRumble = EnableRumble, 540 WeakRumble = WeakRumble, 541 StrongRumble = StrongRumble, 542 }, 543 Version = InputConfig.CurrentVersion, 544 DeadzoneLeft = DeadzoneLeft, 545 DeadzoneRight = DeadzoneRight, 546 RangeLeft = RangeLeft, 547 RangeRight = RangeRight, 548 TriggerThreshold = TriggerThreshold, 549 }; 550 551 if (EnableCemuHookMotion) 552 { 553 config.Motion = new CemuHookMotionConfigController 554 { 555 EnableMotion = EnableMotion, 556 MotionBackend = MotionInputBackendType.CemuHook, 557 GyroDeadzone = GyroDeadzone, 558 Sensitivity = Sensitivity, 559 DsuServerHost = DsuServerHost, 560 DsuServerPort = DsuServerPort, 561 Slot = Slot, 562 AltSlot = AltSlot, 563 MirrorInput = MirrorInput, 564 }; 565 } 566 else 567 { 568 config.Motion = new StandardMotionConfigController 569 { 570 EnableMotion = EnableMotion, 571 MotionBackend = MotionInputBackendType.GamepadDriver, 572 GyroDeadzone = GyroDeadzone, 573 Sensitivity = Sensitivity, 574 }; 575 } 576 577 return config; 578 } 579 } 580 }