Skip to content

Commit

Permalink
Work on game controller DB
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Dec 20, 2024
1 parent 2506aef commit a84a6fa
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 71 deletions.
21 changes: 16 additions & 5 deletions GUI/Dialogs/Preferences/DeviceDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class DeviceDatabase {

init() {

reset()
}

func reset() {

database = [:]
parse(file: "gamecontrollerdb")
}

Expand All @@ -74,14 +80,19 @@ class DeviceDatabase {

func parse(line: String) {

if let range = line.range(of: ",") {
// Eliminate newline characters (if any)
var descriptor = line.replacingOccurrences(of: "\n", with: "")

// Eliminate unneeded commas
descriptor = descriptor.trimmingCharacters(in: CharacterSet(charactersIn: ","))

// Split off the GUID and fill the database
if let range = descriptor.range(of: ",") {

let prefix = String(line[..<range.lowerBound])
let prefix = String(descriptor[..<range.lowerBound])
if prefix.hasPrefix("0") && prefix.count > 28 {

var suffix = line // String(line[range.upperBound...])
if let last = suffix.last, last == "," { suffix.removeLast() }
self.database[prefix] = suffix
self.database[prefix] = descriptor
}
}
}
Expand Down
59 changes: 31 additions & 28 deletions GUI/Dialogs/Preferences/DevicesPrefs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,16 @@ extension PreferencesController {
devUsage.stringValue = property(kIOHIDPrimaryUsageKey)
}

if let mapping = pad?.db.seekDevice(vendorID: property(kIOHIDVendorIDKey),
productID: property(kIOHIDProductIDKey),
version: property(kIOHIDVersionNumberKey)) {

print("DEVICE FOUND")
devHidMapping.stringValue = mapping
// HID mapping
devHidMapping.focusRingType = .none
if let descriptor = pad?.db.seekDevice(vendorID: property(kIOHIDVendorIDKey),
productID: property(kIOHIDProductIDKey),
version: property(kIOHIDVersionNumberKey)) {

let trimmed = descriptor.trimmingCharacters(in: CharacterSet(charactersIn: ","))
devHidMapping.string = trimmed.replacingOccurrences(of: ",", with: ",\n")
}

/*
devLeftScheme.selectItem(withTag: db.left(vendorID: vend, productID: prod))
devRightScheme.selectItem(withTag: db.right(vendorID: vend, productID: prod))
devHatScheme.selectItem(withTag: db.hatSwitch(vendorID: vend, productID: prod))
*/

if pad?.isKnown == true {
devInfoBoxTitle.stringValue = ""
devInfoBoxTitle.textColor = .secondaryLabelColor
Expand All @@ -73,7 +69,7 @@ extension PreferencesController {

let hide = pad == nil || pad?.isMouse == true
devImage.isHidden = hide
devHidMappingBox.isHidden = hide
// devHidMappingBox.isHidden = hide
devHidEvent.isHidden = hide
devAction.isHidden = hide
devAction2.isHidden = hide
Expand Down Expand Up @@ -121,6 +117,7 @@ extension PreferencesController {

func selectDevicesTab() {

devHidEvent.stringValue = ""
devAction.stringValue = ""
devAction2.stringValue = ""
refreshDevicesTab()
Expand All @@ -135,28 +132,34 @@ extension PreferencesController {
refresh()
}

@IBAction func devMappingAction(_ sender: NSTextField!) {
@IBAction func devMappingAction(_ sender: NSTextView!) {

print("\(sender.stringValue)")
db.parse(line: sender.stringValue)
print("\(sender.string)")
db.parse(line: sender.string)
gamePadManager.updateHidMapping()
refresh()
}

/*
@IBAction func devPresetAction(_ sender: NSPopUpButton!) {

assert(sender.selectedTag() == 0)

if let device = selectedDev {

let db = myAppDelegate.database
db.setLeft(vendorID: device.vendorID, productID: device.productID, nil)
db.setRight(vendorID: device.vendorID, productID: device.productID, nil)
db.setHatSwitch(vendorID: device.vendorID, productID: device.productID, nil)
device.updateMappingScheme()
}


myAppDelegate.database.reset()
gamePadManager.updateHidMapping()
refresh()
}
*/
}

extension PreferencesController : NSTextViewDelegate {

func textDidChange(_ notification: Notification) {

if let textView = notification.object as? NSTextView {

print("Text changed: \(textView.string)")
db.parse(line: textView.string)
gamePadManager.updateHidMapping()
}
}

}
4 changes: 2 additions & 2 deletions GUI/Dialogs/Preferences/PreferencesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class PreferencesController: DialogController {
@IBOutlet weak var devAction: NSTextField!
@IBOutlet weak var devAction2: NSTextField!
@IBOutlet weak var devHidEvent: NSTextField!
@IBOutlet weak var devHidMapping: NSTextField!
@IBOutlet weak var devHidMappingBox: NSBox!
@IBOutlet weak var devHidMapping: NSTextView!
// @IBOutlet weak var devHidMappingBox: NSBox!

//
// Methods
Expand Down
9 changes: 7 additions & 2 deletions GUI/Peripherals/GamePad.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ class GamePad {
self.device = device
self.type = type

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

name = traits.name
icon = NSImage(named: isMouse ? "devMouseTemplate" : "devGamepad1Template")!

updateMapping()
}

func updateMapping() {

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

func property(key: String) -> String? {
Expand Down
9 changes: 8 additions & 1 deletion GUI/Peripherals/GamePadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,14 @@ class GamePadManager {

if Int.hid != 0 { listDevices() }
}


func updateHidMapping() {

// Only GamePads 3 and 4 are HID devices
gamePads[3]?.updateMapping()
gamePads[4]?.updateMapping()
}

func listDevices() {

print("Input devices:")
Expand Down
72 changes: 39 additions & 33 deletions GUI/XIB files/Preferences.xib
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
<outlet property="devAction" destination="nWG-QZ-bo4" id="Zgf-zu-X8d"/>
<outlet property="devAction2" destination="VZ1-e1-hp3" id="Egv-Y2-NKP"/>
<outlet property="devHidEvent" destination="5Wj-hw-28o" id="TDN-bg-AVB"/>
<outlet property="devHidMapping" destination="hXs-cB-KHP" id="7Mr-lq-W4J"/>
<outlet property="devHidMappingBox" destination="wyc-OW-ikE" id="ut7-tE-yF7"/>
<outlet property="devHidMapping" destination="lue-lV-Oit" id="b9I-AI-HIO"/>
<outlet property="devImage" destination="Ljn-nh-euA" id="Wnb-BN-uD3"/>
<outlet property="devInfoBox" destination="Dto-Ac-z5q" id="jr1-T2-yMV"/>
<outlet property="devInfoBoxTitle" destination="v81-ep-HXC" id="8cl-mS-uY1"/>
Expand Down Expand Up @@ -1105,7 +1104,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<box verticalHuggingPriority="750" boxType="separator" id="vQF-Af-cYM">
<rect key="frame" x="17" y="55" width="561" height="5"/>
<rect key="frame" x="20" y="55" width="563" height="5"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
</box>
<segmentedControl verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="V9X-FA-6vA">
Expand All @@ -1123,10 +1122,10 @@
</connections>
</segmentedControl>
<box fixedFrame="YES" title="Box" titlePosition="noTitle" translatesAutoresizingMaskIntoConstraints="NO" id="Dto-Ac-z5q">
<rect key="frame" x="17" y="244" width="565" height="123"/>
<rect key="frame" x="17" y="244" width="569" height="123"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" id="Sf3-3T-kwN">
<rect key="frame" x="4" y="5" width="557" height="115"/>
<rect key="frame" x="4" y="5" width="561" height="115"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Q7W-l5-jgx">
Expand Down Expand Up @@ -1321,7 +1320,7 @@
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<popUpButton hidden="YES" id="aQA-bJ-P9F" userLabel="System">
<popUpButton id="aQA-bJ-P9F" userLabel="System">
<rect key="frame" x="14" y="13" width="128" height="25"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<popUpButtonCell key="cell" type="push" title="Revert to..." bezelStyle="rounded" imagePosition="right" alignment="left" lineBreakMode="truncatingTail" borderStyle="border" imageScaling="proportionallyDown" inset="2" pullsDown="YES" selectedItem="peY-Oq-nfP" id="6J4-6V-T2c" userLabel="System">
Expand All @@ -1342,17 +1341,6 @@
<action selector="devPresetAction:" target="-2" id="dZ4-Ae-iBN"/>
</connections>
</popUpButton>
<button verticalHuggingPriority="750" id="Wmt-KA-dLs">
<rect key="frame" x="527" y="10" width="59" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="OK" bezelStyle="rounded" alignment="center" borderStyle="border" inset="2" id="fiW-x3-79i">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="okAction:" target="-2" id="rny-v1-c5A"/>
</connections>
</button>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="nWG-QZ-bo4">
<rect key="frame" x="99" y="91" width="116" height="16"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
Expand Down Expand Up @@ -1380,33 +1368,51 @@
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<box fixedFrame="YES" title="Mapping" translatesAutoresizingMaskIntoConstraints="NO" id="wyc-OW-ikE">
<rect key="frame" x="318" y="68" width="264" height="160"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" id="kqh-Wc-nwv">
<rect key="frame" x="4" y="5" width="256" height="140"/>
<scrollView fixedFrame="YES" borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" translatesAutoresizingMaskIntoConstraints="NO" id="H8h-C1-gQm">
<rect key="frame" x="305" y="79" width="278" height="149"/>
<autoresizingMask key="autoresizingMask"/>
<clipView key="contentView" drawsBackground="NO" id="ZbA-Ci-i6F">
<rect key="frame" x="0.0" y="0.0" width="263" height="149"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField focusRingType="none" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="hXs-cB-KHP">
<rect key="frame" x="0.0" y="0.0" width="256" height="140"/>
<textView wantsLayer="YES" importsGraphics="NO" richText="NO" verticallyResizable="YES" spellingCorrection="YES" smartInsertDelete="YES" id="lue-lV-Oit">
<rect key="frame" x="0.0" y="0.0" width="263" height="149"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<textFieldCell key="cell" controlSize="small" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" title="Lorem ipsum" drawsBackground="YES" id="bWq-gU-0xk">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
</textFieldCell>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
<size key="minSize" width="263" height="149"/>
<size key="maxSize" width="263" height="10000000"/>
<connections>
<action selector="devMappingAction:" target="-2" id="rfX-9B-T9H"/>
<outlet property="delegate" destination="-2" id="sDV-l9-oMY"/>
</connections>
</textField>
</textView>
</subviews>
</view>
</box>
</clipView>
<scroller key="horizontalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="YES" id="Byk-18-gFQ">
<rect key="frame" x="-100" y="-100" width="225" height="15"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" verticalHuggingPriority="750" horizontal="NO" id="8Eo-NY-Mg1">
<rect key="frame" x="263" y="0.0" width="15" height="149"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ljn-nh-euA">
<rect key="frame" x="20" y="79" width="277" height="149"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyUpOrDown" image="gamepad" id="pzJ-iH-NVU"/>
</imageView>
<button verticalHuggingPriority="750" id="Wmt-KA-dLs">
<rect key="frame" x="531" y="10" width="59" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="OK" bezelStyle="rounded" alignment="center" borderStyle="border" inset="2" id="fiW-x3-79i">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="okAction:" target="-2" id="rny-v1-c5A"/>
</connections>
</button>
</subviews>
</view>
</tabViewItem>
Expand Down

0 comments on commit a84a6fa

Please sign in to comment.