diff --git a/platforms/joystick/joystick_driver_test.go b/platforms/joystick/joystick_driver_test.go index c6949d0c..33412058 100644 --- a/platforms/joystick/joystick_driver_test.go +++ b/platforms/joystick/joystick_driver_test.go @@ -48,7 +48,7 @@ func TestDriverHalt(t *testing.T) { assert.Nil(t, d.Halt()) } -func TestDriverHandleEvent(t *testing.T) { +func TestDriverHandleEventDS3(t *testing.T) { sem := make(chan bool) d, tj := initTestDriver("dualshock3") tj.axisCount = 6 @@ -104,7 +104,7 @@ func TestDriverHandleEvent(t *testing.T) { } } -func TestDriverHandleEventJSON(t *testing.T) { +func TestDriverHandleEventJSONDS3(t *testing.T) { sem := make(chan bool) d, tj := initTestDriver("./configs/dualshock3.json") tj.axisCount = 6 @@ -160,6 +160,62 @@ func TestDriverHandleEventJSON(t *testing.T) { } } +func TestDriverHandleEventDS4(t *testing.T) { + sem := make(chan bool) + d, tj := initTestDriver("dualshock4") + tj.axisCount = 6 + tj.buttonCount = 17 + + if err := d.initConfig(); err != nil { + t.Errorf("initConfig() error: %v", err) + } + + d.initEvents() + + // left x stick + _ = d.On(d.Event("left_x"), func(data interface{}) { + assert.Equal(t, int(255), data.(int)) + sem <- true + }) + _ = d.handleAxes(js.State{ + AxisData: []int{255, 0, 0, 0, 0, 0}, + Buttons: 0, + }) + select { + case <-sem: + case <-time.After(1 * time.Second): + t.Errorf("Button Event \"left_x\" was not published") + } + + // square button press + _ = d.On(d.Event("square_press"), func(data interface{}) { + sem <- true + }) + _ = d.handleButtons(js.State{ + AxisData: []int{255, 0, 0, 0, 0, 0}, + Buttons: 1 << d.findID("square", d.config.Buttons), + }) + select { + case <-sem: + case <-time.After(1 * time.Second): + t.Errorf("Button Event \"square_press\" was not published") + } + + // square button release + _ = d.On(d.Event("square_release"), func(data interface{}) { + sem <- true + }) + _ = d.handleButtons(js.State{ + AxisData: []int{255, 0, 0, 0, 0, 0}, + Buttons: 0, + }) + select { + case <-sem: + case <-time.After(1 * time.Second): + t.Errorf("Button Event \"square_release\" was not published") + } +} + func TestDriverInvalidConfig(t *testing.T) { d, _ := initTestDriver("./configs/doesnotexist") err := d.Start() diff --git a/platforms/joystick/joystick_dualshock4.go b/platforms/joystick/joystick_dualshock4_darwin.go similarity index 84% rename from platforms/joystick/joystick_dualshock4.go rename to platforms/joystick/joystick_dualshock4_darwin.go index 77d4dcc8..6e60c071 100644 --- a/platforms/joystick/joystick_dualshock4.go +++ b/platforms/joystick/joystick_dualshock4_darwin.go @@ -14,15 +14,15 @@ var dualshock4Config = joystickConfig{ }, { Name: "right_x", - ID: 3, + ID: 2, }, { Name: "right_y", - ID: 4, + ID: 3, }, { Name: "l2", - ID: 2, + ID: 4, }, { Name: "r2", @@ -32,19 +32,19 @@ var dualshock4Config = joystickConfig{ Buttons: []pair{ { Name: "square", - ID: 3, + ID: 0, }, { Name: "triangle", - ID: 2, + ID: 3, }, { Name: "circle", - ID: 1, + ID: 2, }, { Name: "x", - ID: 0, + ID: 1, }, { Name: "l1", @@ -64,7 +64,7 @@ var dualshock4Config = joystickConfig{ }, { Name: "share", - ID: 8, + ID: 13, }, { Name: "options", @@ -72,7 +72,19 @@ var dualshock4Config = joystickConfig{ }, { Name: "home", + ID: 12, + }, + { + Name: "left_joystick", ID: 10, }, + { + Name: "right_joystick", + ID: 11, + }, + { + Name: "panel", + ID: 13, + }, }, } diff --git a/platforms/joystick/joystick_dualshock4_linux.go b/platforms/joystick/joystick_dualshock4_linux.go new file mode 100644 index 00000000..bb8f7a4e --- /dev/null +++ b/platforms/joystick/joystick_dualshock4_linux.go @@ -0,0 +1,94 @@ +package joystick + +var dualshock4Config = joystickConfig{ + Name: "Dualshock4 Controller", + GUID: "2222", + Axis: []pair{ + { + Name: "left_x", + ID: 0, + }, + { + Name: "left_y", + ID: 1, + }, + { + Name: "right_x", + ID: 3, + }, + { + Name: "right_y", + ID: 4, + }, + { + Name: "l2", + ID: 2, + }, + { + Name: "r2", + ID: 5, + }, + { + Name: "right_left", + ID: 6, + }, + { + Name: "up_down", + ID: 7, + }, + }, + Buttons: []pair{ + { + Name: "square", + ID: 3, + }, + { + Name: "triangle", + ID: 2, + }, + { + Name: "circle", + ID: 1, + }, + { + Name: "x", + ID: 0, + }, + { + Name: "l1", + ID: 4, + }, + { + Name: "l2", + ID: 6, + }, + { + Name: "r1", + ID: 5, + }, + { + Name: "r2", + ID: 7, + }, + { + Name: "share", + ID: 8, + }, + { + Name: "options", + ID: 9, + }, + { + Name: "home", + ID: 10, + }, + { + Name: "left_joystick", + ID: 11, + }, + { + Name: "right_joystick", + ID: 12, + }, + }, +} diff --git a/platforms/joystick/joystick_dualshock4_windows.go b/platforms/joystick/joystick_dualshock4_windows.go new file mode 100644 index 00000000..d05f7753 --- /dev/null +++ b/platforms/joystick/joystick_dualshock4_windows.go @@ -0,0 +1,94 @@ +package joystick + +var dualshock4Config = joystickConfig{ + Name: "Dualshock4 Controller", + GUID: "2222", + Axis: []pair{ + { + Name: "left_x", + ID: 0, + }, + { + Name: "left_y", + ID: 1, + }, + { + Name: "right_x", + ID: 2, + }, + { + Name: "right_y", + ID: 3, + }, + { + Name: "l2", + ID: 5, + }, + { + Name: "r2", + ID: 4, + }, + { + Name: "right_left", + ID: 6, + }, + { + Name: "up_down", + ID: 7, + }, + }, + Buttons: []pair{ + { + Name: "square", + ID: 0, + }, + { + Name: "triangle", + ID: 3, + }, + { + Name: "circle", + ID: 2, + }, + { + Name: "x", + ID: 1, + }, + { + Name: "l1", + ID: 4, + }, + { + Name: "l2", + ID: 6, + }, + { + Name: "r1", + ID: 5, + }, + { + Name: "r2", + ID: 7, + }, + { + Name: "share", + ID: 8, + }, + { + Name: "options", + ID: 9, + }, + { + Name: "home", + ID: 12, + }, + { + Name: "left_joystick", + ID: 10, + }, + { + Name: "right_joystick", + ID: 11, + }, + }, +}