Skip to content

Fix BLE Logitech sleep wake handling - #1182

Merged
lujjjh merged 6 commits into
mainfrom
fix/ble-logitech-sleep-wake
May 7, 2026
Merged

lujjjh merged 6 commits into
mainfrom
fix/ble-logitech-sleep-wake

Conversation

@lujjjh

@lujjjh lujjjh commented May 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Stop device observation and Logitech control monitoring across sleep and inactive user sessions so BLE HID++ diversion is restored before sleep.
  • Avoid periodic background HID++ inventory polling for direct BLE Logitech devices while preserving BLE Logitech controls.
  • Cache direct BLE Logitech battery readings across sleep/wake and refresh them from real device activity.
  • Remove the automatic device picker battery refresh path that could query direct BLE Logitech metadata at startup.
  • Clean up PointerDevice HID callbacks before closing and deallocating its report buffer.

Fixes #1177

Testing

  • git diff --check
  • xcodebuild -quiet -derivedDataPath /tmp/linearmouse-deriveddata-1177-review-build2 -project LinearMouse.xcodeproj -scheme LinearMouse -configuration Debug -destination 'platform=macOS' build
  • xcodebuild -quiet -derivedDataPath /tmp/linearmouse-deriveddata-1177-review-test -project LinearMouse.xcodeproj -scheme LinearMouse -configuration Debug -destination 'platform=macOS' test

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses unintended wake-ups on macOS caused by BLE Logitech HID++ activity by stopping device observation/monitoring across sleep and inactive sessions, reducing background HID++ polling for direct BLE devices, and caching direct BLE battery readings to avoid unnecessary queries.

Changes:

  • Stop LinearMouse services during NSWorkspace.willSleepNotification and inactive user sessions; restart only when allowed.
  • Avoid periodic HID++ inventory polling for direct BLE Logitech devices; refresh direct BLE battery opportunistically and cache results across sleep/wake.
  • Harden teardown of HID resources and expand unit tests around Logitech BLE/USB matching and inventory behavior.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Modules/PointerKit/Sources/PointerKit/PointerDevice.swift Unregister HID callbacks before unscheduling/closing; deallocate report buffer after close.
LinearMouseUnitTests/Device/VendorSpecificDeviceMetadataTests.swift Add/rename tests for Logitech BLE/USB matching, inventory BLE inclusion behavior, and controls monitor conditions.
LinearMouse/UI/Header/DeviceIndicator/DevicePickerSheet/DevicePickerState.swift Stop triggering device-picker-driven BLE battery refresh on cached models.
LinearMouse/UI/Header/DeviceIndicator/DevicePickerSheet/DevicePickerBatteryCoordinator.swift Remove device-picker coordinator that could query direct BLE metadata on startup.
LinearMouse/Model/DeviceModel.swift Remove automatic device-picker battery refresh path from model initialization.
LinearMouse/Device/VendorSpecific/Logitech/LogitechHIDPPDeviceMetadataProvider.swift Refine controls-monitor “needed” logic for direct BLE devices; add worker-stop coordination during shutdown.
LinearMouse/Device/VendorSpecific/ConnectedLogitechDeviceInventory.swift Add opt-in BLE inclusion and cancellation hook to avoid implicit BLE polling.
LinearMouse/Device/VendorSpecific/BatteryDeviceMonitor.swift Cache direct BLE Logitech battery readings across sleep/wake; add controlled refresh logic and cancellation checks.
LinearMouse/Device/DeviceManager.swift Avoid applying pointer speed updates when the manager is not running.
LinearMouse/Device/Device.swift Trigger direct BLE Logitech battery refresh based on real device activity (markActive).
LinearMouse/AppDelegate.swift Gate start/stop based on session active state and sleep/wake notifications.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1736 to +1741
reconfigurationSemaphore.signal()
notificationEndpoint?.wake()
thread?.cancel()
if let thread, thread !== Thread.current {
_ = stoppedSemaphore?.wait(timeout: .now() + Constants.stopTimeout)
}
Comment on lines 138 to 141
func markActive(reason: String) {
manager?.markDeviceActive(self, reason: reason)
BatteryDeviceMonitor.shared.refreshDirectLogitechBluetoothBatteryIfNeeded(for: self)
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Comment on lines +24 to +30
// Preserve direct BLE Logitech battery data across sleep/wake. DeviceManager may briefly
// report no BLE device while macOS rebuilds HID services after wake.
private var directLogitechBluetoothCache = [String: ConnectedBatteryDeviceInfo]()
private var directLogitechBluetoothSuccessfulRefreshDates = [String: Date]()
private var directLogitechBluetoothFailedRefreshDates = [String: Date]()
private var directLogitechBluetoothRefreshesInFlight = Set<String>()
private let directLogitechBluetoothLock = NSLock()
@lujjjh
lujjjh merged commit 1c76d74 into main May 7, 2026
1 check passed
@lujjjh
lujjjh deleted the fix/ble-logitech-sleep-wake branch May 7, 2026 11:26
@lujjjh lujjjh added the bug Something isn't working label May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mac wakes up from sleep

2 participants