Skip to content

Commit

Permalink
Work on HID (Swift side)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Dec 16, 2024
1 parent 022c8f4 commit ca0e5e0
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 17 deletions.
7 changes: 6 additions & 1 deletion Emulator/VAmiga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1154,23 +1154,28 @@ JoystickAPI::trigger(GamePadAction event)
void
JoystickAPI::configureHID(u16 vendorID, u16 productID, u16 version)
{
/*
joystick->configureHID(vendorID, productID, version);
emu->isDirty = true;
*/
}

void
JoystickAPI::trigger(isize page, isize usage, isize value, u16 vendorID, u16 productID, u16 version)
{
/*
joystick->trigger(page, usage, value, vendorID, productID, version);
emu->isDirty = true;
*/
}

void
JoystickAPI::trigger(isize page, isize usage, isize value)
{
// emu->put(CMD_HID_EVENT, GamePadCmd { .port = joystick->objid, ... });
/*
joystick->trigger(page, usage, value);
emu->isDirty = true;
*/
}


Expand Down
108 changes: 106 additions & 2 deletions GUI/Dialogs/Preferences/DeviceDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,78 @@
* An object of this class is used inside the PreferencesController
*/

enum HIDEvent {

case HID_A0
case HID_A0_REV
case HID_A1
case HID_A1_REV
case HID_A12
case HID_A2
case HID_A2_REV
case HID_A20
case HID_A3
case HID_A3_REV
case HID_A31
case HID_A4
case HID_A4_REV
case HID_A5
case HID_A5_REV
case HID_A6
case HID_A6_REV
case HID_A7
case HID_A8
case HID_B0
case HID_B1
case HID_B10
case HID_B11
case HID_B12
case HID_B13
case HID_B14
case HID_B15
case HID_B16
case HID_B17
case HID_B19
case HID_B2
case HID_B20
case HID_B21
case HID_B22
case HID_B23
case HID_B24
case HID_B25
case HID_B26
case HID_B27
case HID_B3
case HID_B4
case HID_B5
case HID_B6
case HID_B7
case HID_B8
case HID_B9
}

struct MyData {

var name = String()

var vendorID: Int
var productID: Int
var version: Int

var leftx = HIDEvent.HID_A0
var rightx = HIDEvent.HID_A2
var lefty = HIDEvent.HID_A1
var righty = HIDEvent.HID_A3
var button1 = HIDEvent.HID_B0
var button2 = HIDEvent.HID_B1
}

let data = [
MyData(name: "Generic", vendorID: 0, productID: 0, version: 0, leftx: .HID_A0, rightx: .HID_A2, lefty: .HID_A1, righty: .HID_A3, button1: .HID_B2, button2: .HID_B1),
MyData(name: "Competition Pro", vendorID: 1035, productID: 25907, version: 256, leftx: .HID_A0, lefty: .HID_A1, button1: .HID_B0, button2: .HID_B1)
]


class DeviceDatabase {

// Mapping scheme ( VendorID -> (ProductID -> Dictionary) )
Expand Down Expand Up @@ -63,7 +135,39 @@ class DeviceDatabase {
custom = obj
}
}


//
// Querying the database
//

func query(vendorID: String, productID: String, version: String) -> MyData {

let vendor = Int(vendorID, radix: 10) ?? 0
let product = Int(productID, radix: 10) ?? 0
let version = Int(version, radix: 10) ?? 0

return query(vendorID: vendor, productID: product, version: version)
}

func query(vendorID: Int, productID: Int, version: Int) -> MyData {

for entry in data {

if entry.vendorID != vendorID { continue }
if entry.productID != productID { continue }
if entry.version != version { continue }

return entry
}

return data[0]
}


//
// Old code
//

func save() {

debug(.hid)
Expand All @@ -73,7 +177,7 @@ class DeviceDatabase {

debug(.hid, "\(custom)")
}

//
// Querying the database
//
Expand Down
94 changes: 80 additions & 14 deletions GUI/Peripherals/GamePad.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class GamePad {
// The Amiga port this device is connected to (1, 2, or nil)
var port: Int?

// GamePad properties (derived from database)
var traits: MyData = data[0]

// Reference to the HID device
var device: IOHIDDevice?
var vendorID: String { return device?.vendorID ?? "" }
Expand Down Expand Up @@ -104,7 +107,15 @@ class GamePad {
self.manager = manager
self.device = device
self.type = type


traits = db.query(vendorID: vendorID, productID: productID, version: version)

print("Traits:")
print(" Name: \(traits.name)")
print(" vendorID: \(traits.vendorID)")
print(" productID: \(traits.productID)")
print(" version: \(traits.version)")

name = db.name(vendorID: vendorID, productID: productID) ?? device?.name ?? ""
icon = db.icon(vendorID: vendorID, productID: productID)

Expand Down Expand Up @@ -300,7 +311,16 @@ class GamePad {
return nil
}
}


func mapHAxisRev(value: IOHIDValue, element: IOHIDElement) -> [GamePadAction]? {

if let v = mapAnalogAxis(value: value, element: element) {
return v == 2 ? [.PULL_LEFT] : v == -2 ? [.PULL_RIGHT] : [.RELEASE_X]
} else {
return nil
}
}

func mapVAxis(value: IOHIDValue, element: IOHIDElement) -> [GamePadAction]? {

if let v = mapAnalogAxis(value: value, element: element) {
Expand Down Expand Up @@ -329,16 +349,7 @@ class GamePad {
let usagePage = Int(IOHIDElementGetUsagePage(element))
let usage = Int(IOHIDElementGetUsage(element))

// track("usagePage = \(usagePage) usage = \(usage) value = \(intValue)")

// New code (uses experimental HID interface)
let cp = port == 1 ? amiga.controlPort1 : amiga.controlPort2
cp?.joystick.triggerPage(usagePage, usage: usage, value: intValue)


//
// Old code
//
print("usagePage = \(usagePage) usage = \(usage) value = \(intValue)")

var events: [GamePadAction]?

Expand Down Expand Up @@ -374,9 +385,63 @@ class GamePad {
}

if usagePage == kHIDPage_GenericDesktop {

switch usage {


case kHIDUsage_GD_X: // A0

events =
traits.leftx == .HID_A0 || traits.rightx == .HID_A0 ? mapHAxis(value: value, element: element) :
traits.leftx == .HID_A0_REV || traits.rightx == .HID_A0_REV ? mapHAxisRev(value: value, element: element) :
traits.lefty == .HID_A0 || traits.righty == .HID_A0 ? mapVAxis(value: value, element: element) :
traits.lefty == .HID_A0_REV || traits.righty == .HID_A0_REV ? mapVAxisRev(value: value, element: element) :
mapHAxis(value: value, element: element)

case kHIDUsage_GD_Y: // A1

events =
traits.leftx == .HID_A1 || traits.rightx == .HID_A1 ? mapHAxis(value: value, element: element) :
traits.leftx == .HID_A1_REV || traits.rightx == .HID_A1_REV ? mapHAxisRev(value: value, element: element) :
traits.lefty == .HID_A1 || traits.righty == .HID_A1 ? mapVAxis(value: value, element: element) :
traits.lefty == .HID_A1_REV || traits.righty == .HID_A1_REV ? mapVAxisRev(value: value, element: element) :
mapHAxis(value: value, element: element)

case kHIDUsage_GD_Z: // A2

events =
traits.leftx == .HID_A2 || traits.rightx == .HID_A2 ? mapHAxis(value: value, element: element) :
traits.leftx == .HID_A2_REV || traits.rightx == .HID_A2_REV ? mapHAxisRev(value: value, element: element) :
traits.lefty == .HID_A2 || traits.righty == .HID_A2 ? mapVAxis(value: value, element: element) :
traits.lefty == .HID_A2_REV || traits.righty == .HID_A2_REV ? mapVAxisRev(value: value, element: element) :
mapHAxis(value: value, element: element)

case kHIDUsage_GD_Rx: // A3

events =
traits.leftx == .HID_A3 || traits.rightx == .HID_A3 ? mapHAxis(value: value, element: element) :
traits.leftx == .HID_A3_REV || traits.rightx == .HID_A3_REV ? mapHAxisRev(value: value, element: element) :
traits.lefty == .HID_A3 || traits.righty == .HID_A3 ? mapVAxis(value: value, element: element) :
traits.lefty == .HID_A3_REV || traits.righty == .HID_A3_REV ? mapVAxisRev(value: value, element: element) :
mapHAxis(value: value, element: element)

case kHIDUsage_GD_Ry: // A4

events =
traits.leftx == .HID_A4 || traits.rightx == .HID_A4 ? mapHAxis(value: value, element: element) :
traits.leftx == .HID_A4_REV || traits.rightx == .HID_A4_REV ? mapHAxisRev(value: value, element: element) :
traits.lefty == .HID_A4 || traits.righty == .HID_A4 ? mapVAxis(value: value, element: element) :
traits.lefty == .HID_A4_REV || traits.righty == .HID_A4_REV ? mapVAxisRev(value: value, element: element) :
mapHAxis(value: value, element: element)

case kHIDUsage_GD_Rz: // A5

events =
traits.leftx == .HID_A5 || traits.rightx == .HID_A5 ? mapHAxis(value: value, element: element) :
traits.leftx == .HID_A5_REV || traits.rightx == .HID_A5_REV ? mapHAxisRev(value: value, element: element) :
traits.lefty == .HID_A5 || traits.righty == .HID_A5 ? mapVAxis(value: value, element: element) :
traits.lefty == .HID_A5_REV || traits.righty == .HID_A5_REV ? mapVAxisRev(value: value, element: element) :
mapHAxis(value: value, element: element)
/*
case kHIDUsage_GD_X where lScheme == Schemes.A0A1: // A0
events = mapHAxis(value: value, element: element)

Expand Down Expand Up @@ -424,6 +489,7 @@ class GamePad {

case 0x93 where hScheme == Schemes.U90U93:
events = intValue != 0 ? [.PULL_LEFT] : [.RELEASE_X]
*/

case kHIDUsage_GD_Hatswitch:

Expand Down

0 comments on commit ca0e5e0

Please sign in to comment.