diff --git a/Sources/GraphiteEvents/EventTarget.swift b/Sources/GraphiteEvents/EventTarget.swift index 2fdada3..356131d 100644 --- a/Sources/GraphiteEvents/EventTarget.swift +++ b/Sources/GraphiteEvents/EventTarget.swift @@ -30,14 +30,30 @@ public final class EventTarget { return id } - public func remove(_ id: UInt64) { registrations.removeAll { $0.id == id } } - public func addTypes(_ types: Set, to id: UInt64) { - guard let index = registrations.firstIndex(where: { $0.id == id }) else { return } + /// Removes an installed handler. False for one never installed or + /// already removed, which RemoveEventHandler answers with a parameter + /// error. + @discardableResult + public func remove(_ id: UInt64) -> Bool { + guard let index = registrations.firstIndex(where: { $0.id == id }) else { return false } + registrations.remove(at: index) + return true + } + + /// AddEventTypesToHandler: false for a handler that isn't installed. + @discardableResult + public func addTypes(_ types: Set, to id: UInt64) -> Bool { + guard let index = registrations.firstIndex(where: { $0.id == id }) else { return false } registrations[index].types.formUnion(types) + return true } - public func removeTypes(_ types: Set, from id: UInt64) { - guard let index = registrations.firstIndex(where: { $0.id == id }) else { return } + + /// RemoveEventTypesFromHandler: false for a handler that isn't installed. + @discardableResult + public func removeTypes(_ types: Set, from id: UInt64) -> Bool { + guard let index = registrations.firstIndex(where: { $0.id == id }) else { return false } registrations[index].types.subtract(types) + return true } public func send(_ event: inout CarbonEvent) -> EventHandlingResult { diff --git a/Sources/GraphiteEvents/HotKey.swift b/Sources/GraphiteEvents/HotKey.swift index 18adffd..009a6a3 100644 --- a/Sources/GraphiteEvents/HotKey.swift +++ b/Sources/GraphiteEvents/HotKey.swift @@ -33,8 +33,11 @@ public final class HotKeyRegistry { return true } - public func unregister(keyCode: UInt32, modifiers: UInt32) { - registrations.removeValue(forKey: Combination(keyCode: keyCode, modifiers: modifiers)) + /// False for a combination that isn't registered, which + /// UnregisterEventHotKey answers with a parameter error. + @discardableResult + public func unregister(keyCode: UInt32, modifiers: UInt32) -> Bool { + registrations.removeValue(forKey: Combination(keyCode: keyCode, modifiers: modifiers)) != nil } /// Delivers to the registered target. The four-character signature and diff --git a/Sources/GraphiteEvents/SecureEventInput.swift b/Sources/GraphiteEvents/SecureEventInput.swift new file mode 100644 index 0000000..d55fb75 --- /dev/null +++ b/Sources/GraphiteEvents/SecureEventInput.swift @@ -0,0 +1,27 @@ +import Synchronization + +/// Secure event input: what password fields turn on so that other programs +/// can't read the keys typed. Calls nest: it stays on until each +/// EnableSecureEventInput has its DisableSecureEventInput. Disabling when +/// it is not on is a parameter error, and leaves it off. +public final class SecureEventInput: Sendable { + private let count = Mutex(0) + + public init() {} + + /// EnableSecureEventInput. + public func enable() { count.withLock { $0 += 1 } } + + /// DisableSecureEventInput: false if it was not enabled. + @discardableResult + public func disable() -> Bool { + count.withLock { count in + guard count > 0 else { return false } + count -= 1 + return true + } + } + + /// IsSecureEventInputEnabled. + public var isEnabled: Bool { count.withLock { $0 > 0 } } +} diff --git a/Sources/GraphiteEvents/TextServices.swift b/Sources/GraphiteEvents/TextServices.swift index 41ef5fd..6c3a5ab 100644 --- a/Sources/GraphiteEvents/TextServices.swift +++ b/Sources/GraphiteEvents/TextServices.swift @@ -11,6 +11,7 @@ public final class TSMDocument: Sendable { /// The application's reference constant. public let refCon: Int private let state = Mutex((active: false, usesInputWindow: false)) + private let properties = Mutex<[FourCharCode: [UInt8]]>([:]) init(interfaces: [FourCharCode], refCon: Int) { self.interfaces = interfaces @@ -24,6 +25,24 @@ public final class TSMDocument: Sendable { /// inline (UseInputWindow). public var usesInputWindow: Bool { state.withLock { $0.usesInputWindow } } + /// TSMSetDocumentProperty: the bytes kept under a tag, replacing any + /// already there. + public func setProperty(_ tag: FourCharCode, bytes: [UInt8]) { + properties.withLock { $0[tag] = bytes } + } + + /// The bytes kept under a tag, nil if none were set (TSMGetDocumentProperty's + /// errAEDescNotFound-like -2528). + public func property(_ tag: FourCharCode) -> [UInt8]? { + properties.withLock { $0[tag] } + } + + /// TSMRemoveDocumentProperty: removing a property never set is not an + /// error. + public func removeProperty(_ tag: FourCharCode) { + _ = properties.withLock { $0.removeValue(forKey: tag) } + } + fileprivate func setActive(_ active: Bool) { state.withLock { $0.active = active } } fileprivate func setUsesInputWindow(_ value: Bool) { state.withLock { $0.usesInputWindow = value } } } diff --git a/Tests/GraphiteEventsTests/EventTests.swift b/Tests/GraphiteEventsTests/EventTests.swift index 93e5cd6..3ee89e3 100644 --- a/Tests/GraphiteEventsTests/EventTests.swift +++ b/Tests/GraphiteEventsTests/EventTests.swift @@ -185,4 +185,23 @@ private final class WaitingHost: EventLoopHost, ClassicEventHost { registry.unregister(keyCode: 12, modifiers: 256) #expect(registry.send(keyCode: 12, modifiers: 256, pressed: true, time: 1) == .notHandled) } + + @Test func removingAndRetypingAnUnknownHandlerIsReported() { + let target = EventTarget() + let type = EventType(eventClass: "test", kind: 1) + let id = target.install(types: [type]) { _, _ in .handled } + #expect(target.addTypes([EventType(eventClass: "test", kind: 2)], to: id)) + #expect(target.removeTypes([type], from: id)) + #expect(target.remove(id)) + #expect(!target.remove(id)) + #expect(!target.addTypes([type], to: id)) + #expect(!target.removeTypes([type], from: id)) + } + + @Test func unregisteringAHotKeyNotRegisteredIsReported() { + let registry = HotKeyRegistry() + #expect(registry.register(keyCode: 0, modifiers: 256, id: HotKeyID(signature: "abcd", id: 7), target: EventTarget())) + #expect(registry.unregister(keyCode: 0, modifiers: 256)) + #expect(!registry.unregister(keyCode: 0, modifiers: 256)) + } } diff --git a/Tests/GraphiteEventsTests/SecureEventInputTests.swift b/Tests/GraphiteEventsTests/SecureEventInputTests.swift new file mode 100644 index 0000000..b32e1b2 --- /dev/null +++ b/Tests/GraphiteEventsTests/SecureEventInputTests.swift @@ -0,0 +1,23 @@ +import Testing +import GraphiteEvents + +@Suite struct SecureEventInputTests { + @Test func enablingNests() { + let input = SecureEventInput() + #expect(!input.isEnabled) + input.enable() + input.enable() + #expect(input.disable()) + #expect(input.isEnabled) + #expect(input.disable()) + #expect(!input.isEnabled) + } + + @Test func disablingWhenOffIsReportedAndStaysOff() { + let input = SecureEventInput() + #expect(!input.disable()) + #expect(!input.isEnabled) + input.enable() + #expect(input.isEnabled) + } +} diff --git a/Tests/GraphiteEventsTests/TextServicesTests.swift b/Tests/GraphiteEventsTests/TextServicesTests.swift index 677733f..6148d00 100644 --- a/Tests/GraphiteEventsTests/TextServicesTests.swift +++ b/Tests/GraphiteEventsTests/TextServicesTests.swift @@ -44,4 +44,15 @@ import GraphiteEvents #expect(throws: MacError.parameter) { try services.activate(document) } #expect(throws: MacError.parameter) { try services.fix(document) } } + + @Test func documentKeepsPropertiesUnderTags() { + let document = TextServices().newDocument(interfaces: ["udoc"], refCon: 0) + #expect(document.property("prop") == nil) + document.setProperty("prop", bytes: [1, 2]) + document.setProperty("prop", bytes: [3]) + #expect(document.property("prop") == [3]) + document.removeProperty("prop") + document.removeProperty("prop") + #expect(document.property("prop") == nil) + } }