Advertisement
Guest User

dualsense_haptics.swift

a guest
Feb 18th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.78 KB | None | 0 0
  1. #!/usr/bin/env swift
  2. // script to enable audio haptics on the DualSense controller
  3. //
  4. // Run it with `swift dualsense_haptics.swift` in the command line after installing the Xcode developer tools
  5.  
  6. import IOKit.hid
  7. import Foundation
  8.  
  9. let hidMgr = IOHIDManagerCreate(kCFAllocatorDefault, IOOptionBits(kIOHIDOptionsTypeNone))
  10. IOHIDManagerSetDeviceMatching(hidMgr, nil)
  11. IOHIDManagerScheduleWithRunLoop(hidMgr, CFRunLoopGetCurrent(), CFRunLoopMode.defaultMode.rawValue)
  12.  
  13. // we want only the DualSense controller
  14. var matchingDevices = NSMutableDictionary()
  15. matchingDevices[kIOHIDVendorIDKey] = 0x054c
  16. matchingDevices[kIOHIDProductIDKey] = 0x0ce6
  17.  
  18. IOHIDManagerSetDeviceMatching(hidMgr, matchingDevices)
  19.  
  20. // give the hid manager some time to start up
  21. while true {
  22.     let res = CFRunLoopRunInMode(CFRunLoopMode.defaultMode, 0, false)
  23.     if res == .finished || res == .timedOut {
  24.         break
  25.     }
  26. }
  27.  
  28. if let devices = IOHIDManagerCopyDevices(hidMgr) {
  29.     let devices = devices as NSSet
  30.     if devices.count == 0 {
  31.         print("could not find any connected DualSense controller")
  32.     }
  33.     for device in devices {
  34.         let device = device as! IOHIDDevice
  35.         if IOHIDDeviceOpen(device, IOOptionBits(kIOHIDOptionsTypeSeizeDevice)) == kIOReturnSuccess {
  36.             var outputReport: [UInt8] = [0x2, 0x1]
  37.             for _ in 0..<62 {
  38.                 outputReport.append(0)
  39.             }
  40.             if IOHIDDeviceSetReport(device, kIOHIDReportTypeOutput, 0, outputReport, 64) == kIOReturnSuccess {
  41.                 print("successfully enabled haptics")
  42.             } else {
  43.                 print("could not enable haptics")
  44.             }
  45.         } else {
  46.             print("could not connect to device")
  47.         }
  48.     }
  49. } else {
  50.     print("could not get devices :(")
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement