Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Sources/GraphiteEvents/EventTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventType>, 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<EventType>, 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<EventType>, 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<EventType>, 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 {
Expand Down
7 changes: 5 additions & 2 deletions Sources/GraphiteEvents/HotKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions Sources/GraphiteEvents/SecureEventInput.swift
Original file line number Diff line number Diff line change
@@ -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 } }
}
19 changes: 19 additions & 0 deletions Sources/GraphiteEvents/TextServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 } }
}
Expand Down
19 changes: 19 additions & 0 deletions Tests/GraphiteEventsTests/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
23 changes: 23 additions & 0 deletions Tests/GraphiteEventsTests/SecureEventInputTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 11 additions & 0 deletions Tests/GraphiteEventsTests/TextServicesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading