-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInputQueue.swift
More file actions
103 lines (90 loc) · 3.47 KB
/
InputQueue.swift
File metadata and controls
103 lines (90 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// InputQueue.swift
// SwiftAndroid
//
// Created by Alsey Coleman Miller on 2/27/26.
//
#if os(Android)
import Android
import CAndroidNDK
#endif
import AndroidLooper
/// Input Queue
///
/// Provides a mechanism for your native activity to retrieve and process input events from the Android input system.
/// AInputQueue is an opaque handle for the native input queue associated with a window.
///
/// [See Also](https://developer.android.com/ndk/reference/group/input#ainputqueue)
public struct InputQueue: ~Copyable {
// MARK: - Properties
internal let pointer: OpaquePointer
// MARK: - Initialization
internal init(_ pointer: OpaquePointer) {
self.pointer = pointer
}
// MARK: - Methods
/// Add this input queue to a looper for processing.
///
/// - Parameters:
/// - looper: The looper to use when invoking callbacks.
/// - identifier: The identifier to use when performing callbacks.
/// - callback: The function to call when an event is available.
/// - data: A private data pointer to supply to the callback.
public func attachLooper(
_ looper: borrowing Looper,
identifier: Int32,
callback: ALooper_callbackFunc?,
data: UnsafeMutableRawPointer?
) {
looper.withUnsafePointer { looperPointer in
AInputQueue_attachLooper(self.pointer, looperPointer, identifier, callback, data)
}
}
/// Remove the input queue from the looper it is currently attached to.
public func detachLooper() {
AInputQueue_detachLooper(pointer)
}
/// Returns true if there are one or more events available in the input queue.
///
/// Returns 1 if the queue has events; 0 if it does not have events; and a negative value if there is an error.
public func hasEvents() -> Int32 {
AInputQueue_hasEvents(pointer)
}
/// Returns the next available event from the queue.
///
/// Returns a negative value if no events are available or an error has occurred, otherwise returns a non-negative value indicating the number of events available.
///
/// - Parameter outEvent: Will be set to the next input event.
/// - Returns: The result code.
@discardableResult
public func getEvent(_ outEvent: inout InputEvent?) -> Int32 {
var eventPointer: OpaquePointer?
let result = AInputQueue_getEvent(pointer, &eventPointer)
if result >= 0, let eventPointer {
outEvent = InputEvent(eventPointer)
} else {
outEvent = nil
}
return result
}
/// Sends the key for standard pre-dispatching.
///
/// Returns 0 if pre-dispatch was completed, 1 if the event should be pre-dispatched, or a negative value on error.
///
/// - Parameter event: The input event to pre-dispatch.
/// - Returns: The result code.
@discardableResult
public func preDispatchEvent(_ event: borrowing InputEvent) -> Int32 {
AInputQueue_preDispatchEvent(pointer, event.pointer)
}
/// Report that dispatching has finished with the given event.
///
/// This must be called after receiving an event with `getEvent()`.
///
/// - Parameters:
/// - event: The event that was handled.
/// - handled: Whether the event was handled (1) or not (0).
public func finishEvent(_ event: borrowing InputEvent, handled: Bool) {
AInputQueue_finishEvent(pointer, event.pointer, handled ? 1 : 0)
}
}