Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi-user scenario #113

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion LinearMouse/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

import Combine
import SwiftUI
import os.log

@main
class AppDelegate: NSObject, NSApplicationDelegate {
private static let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "AppDelegate")

private let autoUpdateManager = AutoUpdateManager.shared
private let statusItem = StatusItem.shared
private var defaultsSubscription: AnyCancellable!
Expand All @@ -33,6 +36,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}
self.update(defaults)

NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.sessionDidResignActiveNotification, object: nil,
queue: nil, using: { _ in
DispatchQueue.main.async {
os_log("Session inactive", log: Self.log, type: .debug)
if let eventTap = self.eventTap {
eventTap.disable()
}
DeviceManager.shared.pause()
}
})

NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.sessionDidBecomeActiveNotification, object: nil,
queue: nil, using: { _ in
DispatchQueue.main.async {
os_log("Session active", log: Self.log, type: .debug)
if let eventTap = self.eventTap {
eventTap.enable()
}
DeviceManager.shared.resume()
}
})
}
}

Expand Down Expand Up @@ -60,6 +85,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func applicationWillTerminate(_ notification: Notification) {
DeviceManager.shared.restorePointerSpeedToInitialValue()
DeviceManager.shared.pause()
}
}
22 changes: 22 additions & 0 deletions LinearMouse/Device/DeviceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DeviceManager {

private static let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "DeviceManager")

private var paused = false
private let eventSystemClient = IOHIDEventSystemClientCreate(kCFAllocatorDefault).takeRetainedValue()
private var devices = Set<Device>()

Expand All @@ -35,6 +36,17 @@ class DeviceManager {
IOHIDEventSystemClientScheduleWithDispatchQueue(eventSystemClient, DispatchQueue.main)
}

func pause() {
restorePointerSpeedToInitialValue()
paused = true
}

func resume() {
paused = false
discoverServiceClients()
renewPointerSpeed()
}

private func setupServiceClients() {
let usageMouse = [
kIOHIDDeviceUsagePageKey: kHIDPage_GenericDesktop,
Expand All @@ -55,6 +67,10 @@ class DeviceManager {
}
}
}, nil, nil)
discoverServiceClients()
}

private func discoverServiceClients() {
let serviceClients = IOHIDEventSystemClientCopyServices(eventSystemClient) as! [IOHIDServiceClient]
for serviceClient in serviceClients {
add(serviceClient: serviceClient)
Expand Down Expand Up @@ -109,6 +125,9 @@ class DeviceManager {
}

func renewPointerSpeed() {
guard !self.paused else {
return
}
if let acceleration = lastPointerAcceleration,
let sensitivity = lastPointerSensitivity,
let disableAcceleration = lastDisablePointerAcceleration {
Expand All @@ -117,6 +136,9 @@ class DeviceManager {
}

func renewPointerSpeed(forDevice device: Device) {
guard !self.paused else {
return
}
if let acceleration = lastPointerAcceleration,
let sensitivity = lastPointerSensitivity,
let disableAcceleration = lastDisablePointerAcceleration {
Expand Down