Skip to content

Commit

Permalink
Rename console listener to HID listener (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored Apr 6, 2024
1 parent 370f0dc commit e9d0a62
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 52 deletions.
14 changes: 7 additions & 7 deletions macos/QMK Toolbox.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
3A62C86526A96AED001C655A /* reset_right.eep in Resources */ = {isa = PBXBuildFile; fileRef = 3A62C86326A96AED001C655A /* reset_right.eep */; };
3A62C86626A96AED001C655A /* reset_left.eep in Resources */ = {isa = PBXBuildFile; fileRef = 3A62C86426A96AED001C655A /* reset_left.eep */; };
3A708D982848A23300394E52 /* USBListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A708D972848A23300394E52 /* USBListener.swift */; };
3A708D9A284901F500394E52 /* HIDConsoleListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A708D99284901F500394E52 /* HIDConsoleListener.swift */; };
3A708D9A284901F500394E52 /* HIDListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A708D99284901F500394E52 /* HIDListener.swift */; };
3A708D9C284916F700394E52 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A708D9B284916F700394E52 /* AppDelegate.swift */; };
3A7492CC27DF96BF0073A5A0 /* hid_bootloader_cli in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3A7492CB27DF96BF0073A5A0 /* hid_bootloader_cli */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
3A8DE01A284636780012063A /* HIDConsoleDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A8DE019284636780012063A /* HIDConsoleDevice.swift */; };
Expand Down Expand Up @@ -134,7 +134,7 @@
3A62C86326A96AED001C655A /* reset_right.eep */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = reset_right.eep; path = ../../../common/reset_right.eep; sourceTree = "<group>"; };
3A62C86426A96AED001C655A /* reset_left.eep */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = reset_left.eep; path = ../../../common/reset_left.eep; sourceTree = "<group>"; };
3A708D972848A23300394E52 /* USBListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = USBListener.swift; sourceTree = "<group>"; };
3A708D99284901F500394E52 /* HIDConsoleListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HIDConsoleListener.swift; sourceTree = "<group>"; };
3A708D99284901F500394E52 /* HIDListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HIDListener.swift; sourceTree = "<group>"; };
3A708D9B284916F700394E52 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3A7492CB27DF96BF0073A5A0 /* hid_bootloader_cli */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = hid_bootloader_cli; sourceTree = "<group>"; };
3A7770D822BD3B8200398C40 /* libftdi1.2.5.0.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libftdi1.2.5.0.dylib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -187,7 +187,7 @@
092963FA1F5C8B2C004F2D3F /* QMK Toolbox */ = {
isa = PBXGroup;
children = (
3A3DDC4C26AE27F600A04A99 /* HIDConsole */,
3A3DDC4C26AE27F600A04A99 /* HID */,
3AC739EB26E23A9800D8B258 /* KeyTester */,
3AC7BE4B27254FB800FED7DB /* USB */,
3A708D9B284916F700394E52 /* AppDelegate.swift */,
Expand All @@ -214,14 +214,14 @@
name = "Supporting Files";
sourceTree = "<group>";
};
3A3DDC4C26AE27F600A04A99 /* HIDConsole */ = {
3A3DDC4C26AE27F600A04A99 /* HID */ = {
isa = PBXGroup;
children = (
3A8DE019284636780012063A /* HIDConsoleDevice.swift */,
3A708D99284901F500394E52 /* HIDConsoleListener.swift */,
3A708D99284901F500394E52 /* HIDListener.swift */,
3A92873C292CF7A10015D961 /* HIDConsoleViewController.swift */,
);
path = HIDConsole;
path = HID;
sourceTree = "<group>";
};
3A53FB5B268A33F10020BAB0 /* Resources */ = {
Expand Down Expand Up @@ -375,7 +375,7 @@
buildActionMask = 2147483647;
files = (
3A8DE01A284636780012063A /* HIDConsoleDevice.swift in Sources */,
3A708D9A284901F500394E52 /* HIDConsoleListener.swift in Sources */,
3A708D9A284901F500394E52 /* HIDListener.swift in Sources */,
3A92873D292CF7A10015D961 /* HIDConsoleViewController.swift in Sources */,
3A37607A283E769300C19B3F /* KeyView.swift in Sources */,
3A32CF59284141D60016D7B7 /* APM32DFUDevice.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import Cocoa

class HIDConsoleViewController: NSViewController, HIDConsoleListenerDelegate {
class HIDConsoleViewController: NSViewController, HIDListenerDelegate {
@IBOutlet var consoleListBox: NSComboBox!

@IBOutlet var logTextView: LogTextView!

override func viewDidLoad() {
consoleListener = HIDConsoleListener()
consoleListener.delegate = self
consoleListener.start()
hidListener = HIDListener()
hidListener.delegate = self
hidListener.start()
}

override func viewWillDisappear() {
consoleListener.stop()
hidListener.stop()
}

@IBAction
Expand All @@ -22,24 +22,24 @@ class HIDConsoleViewController: NSViewController, HIDConsoleListenerDelegate {

// MARK: HID Console Devices

var consoleListener: HIDConsoleListener!
var hidListener: HIDListener!
var lastReportedDevice: HIDConsoleDevice?

func consoleDeviceDidConnect(_ device: HIDConsoleDevice) {
func hidDeviceDidConnect(_ device: HIDConsoleDevice) {
lastReportedDevice = device
updateConsoleList()
logTextView.logHID("HID console connected: \(device)")
}

func consoleDeviceDidDisconnect(_ device: HIDConsoleDevice) {
func hidDeviceDidDisconnect(_ device: HIDConsoleDevice) {
lastReportedDevice = nil
updateConsoleList()
logTextView.logHID("HID console disconnected: \(device)")
}

func consoleDevice(_ device: HIDConsoleDevice, didReceiveReport report: String) {
let selectedDevice = consoleListBox.indexOfSelectedItem
if selectedDevice == 0 || consoleListener.devices[selectedDevice - 1] == device {
if selectedDevice == 0 || hidListener.devices[selectedDevice - 1] == device {
if lastReportedDevice != device {
logTextView.logHID("\(device.manufacturer ?? "") \(device.product ?? "")")
lastReportedDevice = device
Expand All @@ -53,7 +53,7 @@ class HIDConsoleViewController: NSViewController, HIDConsoleListenerDelegate {
consoleListBox.deselectItem(at: selectedItem)
consoleListBox.removeAllItems()

consoleListener.devices.forEach { device in
hidListener.devices.forEach { device in
consoleListBox.addItem(withObjectValue: device.description)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import IOKit.hid
let CONSOLE_USAGE_PAGE: UInt16 = 0xFF31
let CONSOLE_USAGE: UInt16 = 0x0074

protocol HIDConsoleListenerDelegate: AnyObject {
func consoleDeviceDidConnect(_ device: HIDConsoleDevice)
protocol HIDListenerDelegate: AnyObject {
func hidDeviceDidConnect(_ device: HIDConsoleDevice)

func consoleDeviceDidDisconnect(_ device: HIDConsoleDevice)
func hidDeviceDidDisconnect(_ device: HIDConsoleDevice)

func consoleDevice(_ device: HIDConsoleDevice, didReceiveReport report: String)
}

class HIDConsoleListener: HIDConsoleDeviceDelegate {
weak var delegate: HIDConsoleListenerDelegate?
class HIDListener: HIDConsoleDeviceDelegate {
weak var delegate: HIDListenerDelegate?

private var hidManager: IOHIDManager

Expand All @@ -30,12 +30,12 @@ class HIDConsoleListener: HIDConsoleDeviceDelegate {
IOHIDManagerOpen(hidManager, IOOptionBits(kIOHIDOptionsTypeNone))

let matchingCallback: IOHIDDeviceCallback = { context, result, sender, device in
let listener: HIDConsoleListener = Unmanaged<HIDConsoleListener>.fromOpaque(context!).takeUnretainedValue()
let listener: HIDListener = Unmanaged<HIDListener>.fromOpaque(context!).takeUnretainedValue()
listener.deviceConnected(device)
}

let removalCallback: IOHIDDeviceCallback = { context, result, sender, device in
let listener: HIDConsoleListener = Unmanaged<HIDConsoleListener>.fromOpaque(context!).takeUnretainedValue()
let listener: HIDListener = Unmanaged<HIDListener>.fromOpaque(context!).takeUnretainedValue()
listener.deviceDisconnected(device)
}

Expand All @@ -52,15 +52,15 @@ class HIDConsoleListener: HIDConsoleDeviceDelegate {
let consoleDevice = HIDConsoleDevice(device)
consoleDevice.delegate = self
devices.append(consoleDevice)
delegate?.consoleDeviceDidConnect(consoleDevice)
delegate?.hidDeviceDidConnect(consoleDevice)
}

func deviceDisconnected(_ device: IOHIDDevice) {
let discardedItems = devices.filter { $0.hidDevice === device }
devices = devices.filter { !discardedItems.contains($0) }

for d in discardedItems {
delegate?.consoleDeviceDidDisconnect(d)
delegate?.hidDeviceDidDisconnect(d)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace QMK_Toolbox.HidConsole
namespace QMK_Toolbox.Hid
{
public class HidConsoleDevice
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.ComponentModel;
using System.Windows.Forms;

namespace QMK_Toolbox.HidConsole
namespace QMK_Toolbox.Hid
{
public partial class HidConsoleWindow : Form
{
Expand All @@ -29,27 +29,27 @@ private void HidConsoleWindow_Load(object sender, EventArgs e)
{
CenterToParent();

consoleListener.consoleDeviceConnected += ConsoleDeviceConnected;
consoleListener.consoleDeviceDisconnected += ConsoleDeviceDisconnected;
consoleListener.consoleReportReceived += ConsoleReportReceived;
consoleListener.Start();
hidListener.hidDeviceConnected += HidDeviceConnected;
hidListener.hidDeviceDisconnected += HidDeviceDisconnected;
hidListener.consoleReportReceived += ConsoleReportReceived;
hidListener.Start();
}

private void HidConsoleWindow_FormClosing(object sender, FormClosingEventArgs e)
{
consoleListener.consoleDeviceConnected -= ConsoleDeviceConnected;
consoleListener.consoleDeviceDisconnected -= ConsoleDeviceDisconnected;
consoleListener.consoleReportReceived -= ConsoleReportReceived;
consoleListener.Dispose();
hidListener.hidDeviceConnected -= HidDeviceConnected;
hidListener.hidDeviceDisconnected -= HidDeviceDisconnected;
hidListener.consoleReportReceived -= ConsoleReportReceived;
hidListener.Dispose();
}
#endregion

#region HID Console
private readonly HidConsoleListener consoleListener = new();
private readonly HidListener hidListener = new();

private HidConsoleDevice lastReportedDevice;

private void ConsoleDeviceConnected(HidConsoleDevice device)
private void HidDeviceConnected(HidConsoleDevice device)
{
Invoke(new Action(() =>
{
Expand All @@ -59,7 +59,7 @@ private void ConsoleDeviceConnected(HidConsoleDevice device)
}));
}

private void ConsoleDeviceDisconnected(HidConsoleDevice device)
private void HidDeviceDisconnected(HidConsoleDevice device)
{
Invoke(new Action(() =>
{
Expand All @@ -74,7 +74,7 @@ private void ConsoleReportReceived(HidConsoleDevice device, string report)
Invoke(new Action(() =>
{
int selectedDevice = consoleList.SelectedIndex;
if (selectedDevice == 0 || consoleListener.Devices[selectedDevice - 1] == device)
if (selectedDevice == 0 || hidListener.Devices[selectedDevice - 1] == device)
{
if (lastReportedDevice != device)
{
Expand All @@ -91,7 +91,7 @@ private void UpdateConsoleList()
var selected = consoleList.SelectedIndex != -1 ? consoleList.SelectedIndex : 0;
consoleList.Items.Clear();

foreach (var device in consoleListener.Devices)
foreach (var device in hidListener.Devices)
{
if (device != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
using System.Linq;
using System.Management;

namespace QMK_Toolbox.HidConsole
namespace QMK_Toolbox.Hid
{
public class HidConsoleListener : IDisposable
public class HidListener : IDisposable
{
private const ushort ConsoleUsagePage = 0xFF31;
private const ushort ConsoleUsage = 0x0074;

public List<HidConsoleDevice> Devices { get; private set; }

public delegate void HidConsoleDeviceEventDelegate(HidConsoleDevice device);
public delegate void HidDeviceEventDelegate(HidConsoleDevice device);
public delegate void HidConsoleReportReceivedDelegate(HidConsoleDevice device, string data);

public HidConsoleDeviceEventDelegate consoleDeviceConnected;
public HidConsoleDeviceEventDelegate consoleDeviceDisconnected;
public HidDeviceEventDelegate hidDeviceConnected;
public HidDeviceEventDelegate hidDeviceDisconnected;
public HidConsoleReportReceivedDelegate consoleReportReceived;

private void EnumerateHidDevices(bool connected)
Expand All @@ -36,12 +36,12 @@ private void EnumerateHidDevices(bool connected)

if (device != null && !listed)
{
HidConsoleDevice consoleDevice = new(device)
HidConsoleDevice hidDevice = new(device)
{
consoleReportReceived = HidConsoleReportReceived
};
Devices.Add(consoleDevice);
consoleDeviceConnected?.Invoke(consoleDevice);
Devices.Add(hidDevice);
hidDeviceConnected?.Invoke(hidDevice);
}
}
}
Expand All @@ -59,7 +59,7 @@ private void EnumerateHidDevices(bool connected)
}
Devices.Remove(device);
device.consoleReportReceived = null;
consoleDeviceDisconnected?.Invoke(device);
hidDeviceDisconnected?.Invoke(device);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion windows/QMK Toolbox/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using QMK_Toolbox.Helpers;
using QMK_Toolbox.HidConsole;
using QMK_Toolbox.Hid;
using QMK_Toolbox.KeyTester;
using QMK_Toolbox.Properties;
using QMK_Toolbox.Usb;
Expand Down

0 comments on commit e9d0a62

Please sign in to comment.