44using System . Linq ;
55using System . Net . NetworkInformation ;
66using System . Numerics ;
7- using System . Text ;
7+ using System . Configuration ;
88using System . Threading ;
99using System . Threading . Tasks ;
1010
11+ using Nefarius . ViGEm . Client ;
12+ using Nefarius . ViGEm . Client . Targets ;
13+ using Nefarius . ViGEm . Client . Targets . Xbox360 ;
14+
1115namespace BetterJoyForCemu {
1216 public class Joycon {
1317 float timing = 120.0f ;
@@ -197,7 +201,13 @@ public byte[] GetData() {
197201 public PhysicalAddress PadMacAddress = new PhysicalAddress ( new byte [ ] { 01 , 02 , 03 , 04 , 05 , 06 } ) ;
198202 public ulong Timestamp = 0 ;
199203 public int packetCounter = 0 ;
200- //
204+ // For XInput
205+ public Xbox360Controller xin ;
206+ Xbox360Report report ;
207+
208+ int rumblePeriod = Int32 . Parse ( ConfigurationSettings . AppSettings [ "RumblePeriod" ] ) ;
209+ int lowFreq = Int32 . Parse ( ConfigurationSettings . AppSettings [ "LowFreqRumble" ] ) ;
210+ int highFreq = Int32 . Parse ( ConfigurationSettings . AppSettings [ "HighFreqRumble" ] ) ;
201211
202212 public Joycon ( IntPtr handle_ , bool imu , bool localize , float alpha , bool left , int id = 0 , bool isPro = false , bool usb = false ) {
203213 handle = handle_ ;
@@ -210,7 +220,21 @@ public Joycon(IntPtr handle_, bool imu, bool localize, float alpha, bool left, i
210220 PadId = id ;
211221 this . isPro = isPro ;
212222 isUSB = usb ;
223+
224+ if ( isLeft || isPro ) {
225+ xin = new Xbox360Controller ( Program . emClient ) ;
226+ xin . FeedbackReceived += ReceiveRumble ;
227+ report = new Xbox360Report ( ) ;
228+ }
229+ }
230+
231+ public void ReceiveRumble ( object sender , Nefarius . ViGEm . Client . Targets . Xbox360 . Xbox360FeedbackReceivedEventArgs e ) {
232+ SetRumble ( lowFreq , highFreq , ( float ) e . LargeMotor / ( float ) 255 , rumblePeriod ) ;
233+
234+ if ( other != null )
235+ other . SetRumble ( lowFreq , highFreq , ( float ) e . LargeMotor / ( float ) 255 , rumblePeriod ) ;
213236 }
237+
214238 public void DebugPrint ( String s , DebugType d ) {
215239 if ( debug_type == DebugType . NONE ) return ;
216240 if ( d == DebugType . ALL || d == debug_type || debug_type == DebugType . ALL ) {
@@ -343,6 +367,9 @@ private int ReceiveRaw() {
343367 packetCounter ++ ;
344368 if ( Program . server != null )
345369 Program . server . NewReportIncoming ( this ) ;
370+
371+ if ( xin != null )
372+ xin . SendReport ( report ) ;
346373 }
347374
348375 if ( ts_en == raw_buf [ 1 ] ) {
@@ -457,9 +484,25 @@ private int ProcessButtonsAndStick(byte[] report_buf) {
457484 buttons [ ( int ) Button . STICK2 ] = ( ( report_buf [ 4 ] & ( ! isLeft ? 0x08 : 0x04 ) ) != 0 ) ;
458485 buttons [ ( int ) Button . SHOULDER2_1 ] = ( report_buf [ 3 + ( ! isLeft ? 2 : 0 ) ] & 0x40 ) != 0 ;
459486 buttons [ ( int ) Button . SHOULDER2_2 ] = ( report_buf [ 3 + ( ! isLeft ? 2 : 0 ) ] & 0x80 ) != 0 ;
487+
488+ report . SetButtonState ( Xbox360Buttons . A , buttons [ ( int ) Button . B ] ) ;
489+ report . SetButtonState ( Xbox360Buttons . B , buttons [ ( int ) Button . A ] ) ;
490+ report . SetButtonState ( Xbox360Buttons . Y , buttons [ ( int ) Button . X ] ) ;
491+ report . SetButtonState ( Xbox360Buttons . X , buttons [ ( int ) Button . Y ] ) ;
492+ report . SetButtonState ( Xbox360Buttons . Up , buttons [ ( int ) Button . DPAD_UP ] ) ;
493+ report . SetButtonState ( Xbox360Buttons . Down , buttons [ ( int ) Button . DPAD_DOWN ] ) ;
494+ report . SetButtonState ( Xbox360Buttons . Left , buttons [ ( int ) Button . DPAD_LEFT ] ) ;
495+ report . SetButtonState ( Xbox360Buttons . Right , buttons [ ( int ) Button . DPAD_RIGHT ] ) ;
496+ report . SetButtonState ( Xbox360Buttons . Back , buttons [ ( int ) Button . MINUS ] ) ;
497+ report . SetButtonState ( Xbox360Buttons . Start , buttons [ ( int ) Button . PLUS ] ) ;
498+ report . SetButtonState ( Xbox360Buttons . Guide , buttons [ ( int ) Button . HOME ] ) ;
499+ report . SetButtonState ( Xbox360Buttons . LeftShoulder , buttons [ ( int ) Button . SHOULDER_1 ] ) ;
500+ report . SetButtonState ( Xbox360Buttons . RightShoulder , buttons [ ( int ) Button . SHOULDER2_1 ] ) ;
501+ report . SetButtonState ( Xbox360Buttons . LeftThumb , buttons [ ( int ) Button . STICK ] ) ;
502+ report . SetButtonState ( Xbox360Buttons . RightThumb , buttons [ ( int ) Button . STICK2 ] ) ;
460503 }
461504
462- if ( isLeft && other != null ) {
505+ if ( other != null ) {
463506 buttons [ ( int ) Button . B ] = other . buttons [ ( int ) Button . DPAD_DOWN ] ;
464507 buttons [ ( int ) Button . A ] = other . buttons [ ( int ) Button . DPAD_RIGHT ] ;
465508 buttons [ ( int ) Button . X ] = other . buttons [ ( int ) Button . DPAD_UP ] ;
@@ -468,24 +511,35 @@ private int ProcessButtonsAndStick(byte[] report_buf) {
468511 buttons [ ( int ) Button . STICK2 ] = other . buttons [ ( int ) Button . STICK ] ;
469512 buttons [ ( int ) Button . SHOULDER2_1 ] = other . buttons [ ( int ) Button . SHOULDER_1 ] ;
470513 buttons [ ( int ) Button . SHOULDER2_2 ] = other . buttons [ ( int ) Button . SHOULDER_2 ] ;
514+ }
471515
516+ if ( isLeft && other != null ) {
472517 buttons [ ( int ) Button . HOME ] = other . buttons [ ( int ) Button . HOME ] ;
473518 buttons [ ( int ) Button . PLUS ] = other . buttons [ ( int ) Button . PLUS ] ;
474519 }
475520
476521 if ( ! isLeft && other != null ) {
477- buttons [ ( int ) Button . B ] = other . buttons [ ( int ) Button . DPAD_DOWN ] ;
478- buttons [ ( int ) Button . A ] = other . buttons [ ( int ) Button . DPAD_RIGHT ] ;
479- buttons [ ( int ) Button . X ] = other . buttons [ ( int ) Button . DPAD_UP ] ;
480- buttons [ ( int ) Button . Y ] = other . buttons [ ( int ) Button . DPAD_LEFT ] ;
481-
482- buttons [ ( int ) Button . STICK2 ] = other . buttons [ ( int ) Button . STICK ] ;
483- buttons [ ( int ) Button . SHOULDER2_1 ] = other . buttons [ ( int ) Button . SHOULDER_1 ] ;
484- buttons [ ( int ) Button . SHOULDER2_2 ] = other . buttons [ ( int ) Button . SHOULDER_2 ] ;
485-
486522 buttons [ ( int ) Button . MINUS ] = other . buttons [ ( int ) Button . MINUS ] ;
487523 }
488524
525+ if ( ! isPro && other != null && xin != null ) {
526+ report . SetButtonState ( Xbox360Buttons . A , buttons [ ( int ) ( isLeft ? Button . B : Button . DPAD_DOWN ) ] ) ;
527+ report . SetButtonState ( Xbox360Buttons . B , buttons [ ( int ) ( isLeft ? Button . A : Button . DPAD_RIGHT ) ] ) ;
528+ report . SetButtonState ( Xbox360Buttons . Y , buttons [ ( int ) ( isLeft ? Button . X : Button . DPAD_UP ) ] ) ;
529+ report . SetButtonState ( Xbox360Buttons . X , buttons [ ( int ) ( isLeft ? Button . Y : Button . DPAD_LEFT ) ] ) ;
530+ report . SetButtonState ( Xbox360Buttons . Up , buttons [ ( int ) ( isLeft ? Button . DPAD_UP : Button . X ) ] ) ;
531+ report . SetButtonState ( Xbox360Buttons . Down , buttons [ ( int ) ( isLeft ? Button . DPAD_DOWN : Button . B ) ] ) ;
532+ report . SetButtonState ( Xbox360Buttons . Left , buttons [ ( int ) ( isLeft ? Button . DPAD_LEFT : Button . Y ) ] ) ;
533+ report . SetButtonState ( Xbox360Buttons . Right , buttons [ ( int ) ( isLeft ? Button . DPAD_RIGHT : Button . A ) ] ) ;
534+ report . SetButtonState ( Xbox360Buttons . Back , buttons [ ( int ) Button . MINUS ] ) ;
535+ report . SetButtonState ( Xbox360Buttons . Start , buttons [ ( int ) Button . PLUS ] ) ;
536+ report . SetButtonState ( Xbox360Buttons . Guide , buttons [ ( int ) Button . HOME ] ) ;
537+ report . SetButtonState ( Xbox360Buttons . LeftShoulder , buttons [ ( int ) ( isLeft ? Button . SHOULDER_1 : Button . SHOULDER2_1 ) ] ) ;
538+ report . SetButtonState ( Xbox360Buttons . RightShoulder , buttons [ ( int ) ( isLeft ? Button . SHOULDER2_1 : Button . SHOULDER_1 ) ] ) ;
539+ report . SetButtonState ( Xbox360Buttons . LeftThumb , buttons [ ( int ) ( isLeft ? Button . STICK : Button . STICK2 ) ] ) ;
540+ report . SetButtonState ( Xbox360Buttons . RightThumb , buttons [ ( int ) ( isLeft ? Button . STICK2 : Button . STICK ) ] ) ;
541+ }
542+
489543 lock ( buttons_up ) {
490544 lock ( buttons_down ) {
491545 for ( int i = 0 ; i < buttons . Length ; ++ i ) {
@@ -495,6 +549,16 @@ private int ProcessButtonsAndStick(byte[] report_buf) {
495549 }
496550 }
497551 }
552+
553+ if ( xin != null ) {
554+ report . SetAxis ( Xbox360Axes . LeftThumbX , ( short ) Math . Max ( Int16 . MinValue , Math . Min ( Int16 . MaxValue , stick [ 0 ] * ( stick [ 0 ] > 0 ? Int16 . MaxValue : - Int16 . MinValue ) ) ) ) ;
555+ report . SetAxis ( Xbox360Axes . LeftThumbY , ( short ) Math . Max ( Int16 . MinValue , Math . Min ( Int16 . MaxValue , stick [ 1 ] * ( stick [ 0 ] > 0 ? Int16 . MaxValue : - Int16 . MinValue ) ) ) ) ;
556+ report . SetAxis ( Xbox360Axes . RightThumbX , ( short ) Math . Max ( Int16 . MinValue , Math . Min ( Int16 . MaxValue , stick2 [ 0 ] * ( stick [ 0 ] > 0 ? Int16 . MaxValue : - Int16 . MinValue ) ) ) ) ;
557+ report . SetAxis ( Xbox360Axes . RightThumbY , ( short ) Math . Max ( Int16 . MinValue , Math . Min ( Int16 . MaxValue , stick2 [ 1 ] * ( stick [ 0 ] > 0 ? Int16 . MaxValue : - Int16 . MinValue ) ) ) ) ;
558+ report . SetAxis ( Xbox360Axes . LeftTrigger , ( short ) ( buttons [ ( int ) ( isLeft ? Button . SHOULDER_2 : Button . SHOULDER2_2 ) ] ? Int16 . MaxValue : 0 ) ) ;
559+ report . SetAxis ( Xbox360Axes . RightTrigger , ( short ) ( buttons [ ( int ) ( isLeft ? Button . SHOULDER2_2 : Button . SHOULDER_2 ) ] ? Int16 . MaxValue : 0 ) ) ;
560+ }
561+
498562 return 0 ;
499563 }
500564 private void ExtractIMUValues ( byte [ ] report_buf , int n = 0 ) {
0 commit comments