2024-03-09 14:48:54 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-03-27 22:22:56 +00:00
|
|
|
import Cocoa
|
2024-03-12 19:25:23 +00:00
|
|
|
import Carbon.HIToolbox
|
|
|
|
|
2024-03-09 14:48:54 +00:00
|
|
|
class InputHelper: NSObject {
|
2024-03-19 18:50:44 +00:00
|
|
|
var option: OptionHelper?
|
2024-03-09 14:48:54 +00:00
|
|
|
var lock = NSCondition()
|
|
|
|
private var input: OpaquePointer?
|
|
|
|
|
2024-03-12 19:25:23 +00:00
|
|
|
let keymap: [mp_keymap] = [
|
|
|
|
// special keys
|
2024-04-27 20:11:33 +00:00
|
|
|
.init(kVK_Return, MP_KEY_ENTER), .init(kVK_Escape, MP_KEY_ESC),
|
|
|
|
.init(kVK_Delete, MP_KEY_BACKSPACE), .init(kVK_Tab, MP_KEY_TAB),
|
2024-03-12 21:48:01 +00:00
|
|
|
.init(kVK_VolumeUp, MP_KEY_VOLUME_UP), .init(kVK_VolumeDown, MP_KEY_VOLUME_DOWN),
|
|
|
|
.init(kVK_Mute, MP_KEY_MUTE),
|
2024-03-12 19:25:23 +00:00
|
|
|
|
|
|
|
// cursor keys
|
2024-04-27 20:11:33 +00:00
|
|
|
.init(kVK_UpArrow, MP_KEY_UP), .init(kVK_DownArrow, MP_KEY_DOWN),
|
2024-03-12 21:44:41 +00:00
|
|
|
.init(kVK_LeftArrow, MP_KEY_LEFT), .init(kVK_RightArrow, MP_KEY_RIGHT),
|
2024-03-12 19:25:23 +00:00
|
|
|
|
|
|
|
// navigation block
|
2024-04-27 20:11:33 +00:00
|
|
|
.init(kVK_Help, MP_KEY_INSERT), .init(kVK_ForwardDelete, MP_KEY_DELETE),
|
|
|
|
.init(kVK_Home, MP_KEY_HOME), .init(kVK_End, MP_KEY_END),
|
2024-03-12 21:44:41 +00:00
|
|
|
.init(kVK_PageUp, MP_KEY_PAGE_UP), .init(kVK_PageDown, MP_KEY_PAGE_DOWN),
|
2024-03-12 19:25:23 +00:00
|
|
|
|
|
|
|
// F-keys
|
2024-04-27 20:11:33 +00:00
|
|
|
.init(kVK_F1, MP_KEY_F + 1), .init(kVK_F2, MP_KEY_F + 2), .init(kVK_F3, MP_KEY_F + 3),
|
|
|
|
.init(kVK_F4, MP_KEY_F + 4), .init(kVK_F5, MP_KEY_F + 5), .init(kVK_F6, MP_KEY_F + 6),
|
|
|
|
.init(kVK_F7, MP_KEY_F + 7), .init(kVK_F8, MP_KEY_F + 8), .init(kVK_F9, MP_KEY_F + 9),
|
2024-03-12 21:44:41 +00:00
|
|
|
.init(kVK_F10, MP_KEY_F + 10), .init(kVK_F11, MP_KEY_F + 11), .init(kVK_F12, MP_KEY_F + 12),
|
|
|
|
.init(kVK_F13, MP_KEY_F + 13), .init(kVK_F14, MP_KEY_F + 14), .init(kVK_F15, MP_KEY_F + 15),
|
|
|
|
.init(kVK_F16, MP_KEY_F + 16), .init(kVK_F17, MP_KEY_F + 17), .init(kVK_F18, MP_KEY_F + 18),
|
|
|
|
.init(kVK_F19, MP_KEY_F + 19), .init(kVK_F20, MP_KEY_F + 20),
|
2024-03-12 19:25:23 +00:00
|
|
|
|
|
|
|
// numpad
|
2024-03-12 21:44:41 +00:00
|
|
|
.init(kVK_ANSI_KeypadPlus, Int32(Character("+").asciiValue ?? 0)),
|
|
|
|
.init(kVK_ANSI_KeypadMinus, Int32(Character("-").asciiValue ?? 0)),
|
|
|
|
.init(kVK_ANSI_KeypadMultiply, Int32(Character("*").asciiValue ?? 0)),
|
|
|
|
.init(kVK_ANSI_KeypadDivide, Int32(Character("/").asciiValue ?? 0)),
|
|
|
|
.init(kVK_ANSI_KeypadEnter, MP_KEY_KPENTER), .init(kVK_ANSI_KeypadDecimal, MP_KEY_KPDEC),
|
2024-04-27 20:11:33 +00:00
|
|
|
.init(kVK_ANSI_Keypad0, MP_KEY_KP0), .init(kVK_ANSI_Keypad1, MP_KEY_KP1),
|
|
|
|
.init(kVK_ANSI_Keypad2, MP_KEY_KP2), .init(kVK_ANSI_Keypad3, MP_KEY_KP3),
|
|
|
|
.init(kVK_ANSI_Keypad4, MP_KEY_KP4), .init(kVK_ANSI_Keypad5, MP_KEY_KP5),
|
|
|
|
.init(kVK_ANSI_Keypad6, MP_KEY_KP6), .init(kVK_ANSI_Keypad7, MP_KEY_KP7),
|
|
|
|
.init(kVK_ANSI_Keypad8, MP_KEY_KP8), .init(kVK_ANSI_Keypad9, MP_KEY_KP9),
|
2024-03-12 19:25:23 +00:00
|
|
|
|
2024-03-12 21:44:41 +00:00
|
|
|
.init(0, 0)
|
2024-03-12 19:25:23 +00:00
|
|
|
]
|
|
|
|
|
2024-03-26 20:50:35 +00:00
|
|
|
init(_ input: OpaquePointer? = nil, _ option: OptionHelper? = nil) {
|
2024-03-09 14:48:54 +00:00
|
|
|
super.init()
|
|
|
|
self.input = input
|
2024-03-19 18:50:44 +00:00
|
|
|
self.option = option
|
2024-03-09 14:48:54 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 20:50:35 +00:00
|
|
|
func put(
|
2024-03-12 19:25:23 +00:00
|
|
|
key: Int32,
|
|
|
|
modifiers: NSEvent.ModifierFlags = .init(rawValue: 0),
|
|
|
|
type: NSEvent.EventType = .applicationDefined
|
|
|
|
) {
|
2024-03-09 23:51:46 +00:00
|
|
|
lock.withLock {
|
2024-03-12 19:25:23 +00:00
|
|
|
putKey(key, modifiers: modifiers, type: type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func putKey(
|
|
|
|
_ key: Int32,
|
|
|
|
modifiers: NSEvent.ModifierFlags = .init(rawValue: 0),
|
|
|
|
type: NSEvent.EventType = .applicationDefined
|
|
|
|
) {
|
|
|
|
if key < 1 { return }
|
|
|
|
|
|
|
|
guard let input = input else { return }
|
|
|
|
let code = key | mapModifier(modifiers) | mapType(type)
|
|
|
|
mp_input_put_key(input, code)
|
|
|
|
|
|
|
|
if type == .keyUp {
|
|
|
|
mp_input_put_key(input, MP_INPUT_RELEASE_ALL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func processKey(event: NSEvent) -> Bool {
|
|
|
|
if event.type != .keyDown && event.type != .keyUp { return false }
|
|
|
|
if NSApp.mainMenu?.performKeyEquivalent(with: event) ?? false || event.isARepeat { return true }
|
|
|
|
|
|
|
|
return lock.withLock {
|
|
|
|
let mpkey = lookup_keymap_table(keymap, Int32(event.keyCode))
|
|
|
|
if mpkey > 0 {
|
|
|
|
putKey(mpkey, modifiers: event.modifierFlags, type: event.type)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let chars = event.characters, let charsNoMod = event.charactersIgnoringModifiers else { return false }
|
2024-04-21 15:02:36 +00:00
|
|
|
var key = (useAltGr() && event.modifierFlags.contains(.optionRight)) ? chars : charsNoMod
|
|
|
|
if key.isEmpty { key = mapDeadKey(event) }
|
|
|
|
if key.isEmpty { return false }
|
2024-03-12 19:25:23 +00:00
|
|
|
key.withCString {
|
|
|
|
var bstr = bstr0($0)
|
|
|
|
putKey(bstr_decode_utf8(bstr, &bstr), modifiers: event.modifierFlags, type: event.type)
|
|
|
|
}
|
|
|
|
return true
|
2024-03-09 23:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:34:29 +00:00
|
|
|
func processMouse(event: NSEvent) {
|
|
|
|
if !mouseEnabled() { return }
|
|
|
|
lock.withLock {
|
|
|
|
putKey(map(button: event.buttonNumber), modifiers: event.modifierFlags, type: event.type)
|
|
|
|
if event.clickCount > 1 {
|
|
|
|
putKey(map(button: event.buttonNumber), modifiers: event.modifierFlags, type: .keyUp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 22:58:10 +00:00
|
|
|
func processWheel(event: NSEvent) {
|
|
|
|
if !mouseEnabled() { return }
|
|
|
|
lock.withLock {
|
2024-03-14 21:35:45 +00:00
|
|
|
guard let input = input else { return }
|
|
|
|
let modifiers = event.modifierFlags
|
|
|
|
let precise = event.hasPreciseScrollingDeltas
|
|
|
|
var deltaX = event.deltaX * 0.1
|
|
|
|
var deltaY = event.deltaY * 0.1
|
|
|
|
|
|
|
|
if !precise {
|
|
|
|
deltaX = modifiers.contains(.shift) ? event.scrollingDeltaY : event.scrollingDeltaX
|
|
|
|
deltaY = modifiers.contains(.shift) ? event.scrollingDeltaX : event.scrollingDeltaY
|
2024-03-14 22:58:10 +00:00
|
|
|
}
|
2024-03-14 21:35:45 +00:00
|
|
|
|
|
|
|
var key = deltaY > 0 ? SWIFT_WHEEL_UP : SWIFT_WHEEL_DOWN
|
|
|
|
var delta = Double(deltaY)
|
|
|
|
if abs(deltaX) > abs(deltaY) {
|
|
|
|
key = deltaX > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT
|
|
|
|
delta = Double(deltaX)
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_input_put_wheel(input, key | mapModifier(modifiers), precise ? abs(delta) : 1)
|
2024-03-14 22:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 23:51:46 +00:00
|
|
|
func draggable(at pos: NSPoint) -> Bool {
|
|
|
|
lock.withLock {
|
|
|
|
guard let input = input else { return false }
|
|
|
|
return !mp_input_test_dragging(input, Int32(pos.x), Int32(pos.y))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mouseEnabled() -> Bool {
|
|
|
|
lock.withLock {
|
|
|
|
guard let input = input else { return true }
|
|
|
|
return mp_input_mouse_enabled(input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 23:36:05 +00:00
|
|
|
func setMouse(position: NSPoint) {
|
|
|
|
if !mouseEnabled() { return }
|
2024-03-09 23:51:46 +00:00
|
|
|
lock.withLock {
|
|
|
|
guard let input = input else { return }
|
2024-03-13 23:36:05 +00:00
|
|
|
mp_input_set_mouse_pos(input, Int32(position.x), Int32(position.y))
|
2024-03-09 23:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult @objc func command(_ cmd: String) -> Bool {
|
2024-03-09 14:48:54 +00:00
|
|
|
lock.withLock {
|
|
|
|
guard let input = input else { return false }
|
|
|
|
let cCmd = UnsafePointer<Int8>(strdup(cmd))
|
|
|
|
let mpvCmd = mp_input_parse_cmd(input, bstr0(cCmd), "")
|
|
|
|
mp_input_queue_cmd(input, mpvCmd)
|
|
|
|
free(UnsafeMutablePointer(mutating: cCmd))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:25:23 +00:00
|
|
|
private func mapType(_ type: NSEvent.EventType) -> Int32 {
|
2024-04-27 20:11:33 +00:00
|
|
|
let typeMapping: [NSEvent.EventType: UInt32] = [
|
2024-03-12 19:25:23 +00:00
|
|
|
.keyDown: MP_KEY_STATE_DOWN,
|
|
|
|
.keyUp: MP_KEY_STATE_UP,
|
2024-03-13 22:34:29 +00:00
|
|
|
.leftMouseDown: MP_KEY_STATE_DOWN,
|
|
|
|
.leftMouseUp: MP_KEY_STATE_UP,
|
|
|
|
.rightMouseDown: MP_KEY_STATE_DOWN,
|
|
|
|
.rightMouseUp: MP_KEY_STATE_UP,
|
|
|
|
.otherMouseDown: MP_KEY_STATE_DOWN,
|
2024-04-27 20:11:33 +00:00
|
|
|
.otherMouseUp: MP_KEY_STATE_UP
|
2024-03-12 19:25:23 +00:00
|
|
|
]
|
|
|
|
|
2024-04-27 20:11:33 +00:00
|
|
|
return Int32(typeMapping[type] ?? 0)
|
2024-03-12 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
2024-03-09 23:51:46 +00:00
|
|
|
private func mapModifier(_ modifiers: NSEvent.ModifierFlags) -> Int32 {
|
2024-04-27 20:11:33 +00:00
|
|
|
var mask: UInt32 = 0
|
2024-03-09 23:51:46 +00:00
|
|
|
|
|
|
|
if modifiers.contains(.shift) {
|
|
|
|
mask |= MP_KEY_MODIFIER_SHIFT
|
|
|
|
}
|
|
|
|
if modifiers.contains(.control) {
|
|
|
|
mask |= MP_KEY_MODIFIER_CTRL
|
|
|
|
}
|
|
|
|
if modifiers.contains(.command) {
|
|
|
|
mask |= MP_KEY_MODIFIER_META
|
|
|
|
}
|
2024-03-12 19:25:23 +00:00
|
|
|
if modifiers.contains(.optionLeft) || modifiers.contains(.optionRight) && !useAltGr() {
|
2024-03-09 23:51:46 +00:00
|
|
|
mask |= MP_KEY_MODIFIER_ALT
|
|
|
|
}
|
|
|
|
|
|
|
|
return Int32(mask)
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:34:29 +00:00
|
|
|
private func map(button: Int) -> Int32 {
|
2024-04-27 20:11:33 +00:00
|
|
|
let buttonMapping: [Int: Int32] = [
|
2024-03-13 22:34:29 +00:00
|
|
|
0: SWIFT_MBTN_LEFT,
|
|
|
|
1: SWIFT_MBTN_RIGHT,
|
|
|
|
2: SWIFT_MBTN_MID,
|
2024-03-13 23:23:14 +00:00
|
|
|
3: SWIFT_MBTN_FORWARD,
|
2024-04-27 20:11:33 +00:00
|
|
|
4: SWIFT_MBTN_BACK
|
2024-03-13 22:34:29 +00:00
|
|
|
]
|
|
|
|
|
2024-04-27 20:11:33 +00:00
|
|
|
return Int32(buttonMapping[button] ?? SWIFT_MBTN9 + Int32(button - 5))
|
2024-03-13 22:34:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 15:02:36 +00:00
|
|
|
func mapDeadKey(_ event: NSEvent) -> String {
|
|
|
|
let keyboard = TISCopyCurrentKeyboardInputSource().takeRetainedValue()
|
|
|
|
let layoutPtr = TISGetInputSourceProperty(keyboard, kTISPropertyUnicodeKeyLayoutData)
|
|
|
|
let layoutData = unsafeBitCast(layoutPtr, to: CFData.self)
|
|
|
|
let layout = unsafeBitCast(CFDataGetBytePtr(layoutData), to: UnsafePointer<UCKeyboardLayout>.self)
|
|
|
|
let maxLength = 2
|
|
|
|
let modifiers: UInt32 = UInt32(event.modifierFlags.rawValue >> 16) & 0xff
|
|
|
|
var deadKeyState: UInt32 = 0
|
|
|
|
var length = 0
|
|
|
|
var chars = [UniChar](repeating: 0, count: maxLength)
|
|
|
|
let err = UCKeyTranslate(layout, event.keyCode, UInt16(kUCKeyActionDisplay), modifiers,
|
|
|
|
UInt32(LMGetKbdType()), 0, &deadKeyState, maxLength, &length, &chars)
|
|
|
|
|
|
|
|
if err != noErr || length < 1 { return "" }
|
|
|
|
return String(utf16CodeUnits: chars, count: length)
|
|
|
|
}
|
|
|
|
|
2024-04-03 18:12:31 +00:00
|
|
|
@objc func open(files: [String], append: Bool = false) {
|
2024-03-09 14:48:54 +00:00
|
|
|
lock.withLock {
|
|
|
|
guard let input = input else { return }
|
2024-03-19 22:15:42 +00:00
|
|
|
if (option?.vo.drag_and_drop ?? -1) == -2 { return }
|
2024-03-09 23:51:46 +00:00
|
|
|
|
2024-04-03 18:12:31 +00:00
|
|
|
var action = DND_APPEND
|
|
|
|
if !append {
|
|
|
|
action = NSEvent.modifierFlags.contains(.shift) ? DND_APPEND : DND_REPLACE
|
2024-04-27 20:11:33 +00:00
|
|
|
if (option?.vo.drag_and_drop ?? -1) >= 0 {
|
2024-04-03 18:12:31 +00:00
|
|
|
action = mp_dnd_action(UInt32(option?.vo.drag_and_drop ?? Int32(DND_REPLACE.rawValue)))
|
|
|
|
}
|
2024-03-09 23:51:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 20:11:33 +00:00
|
|
|
let filesClean = files.map { $0.hasPrefix("file:///.file/id=") ? (URL(string: $0)?.path ?? $0) : $0 }
|
2024-03-09 23:51:46 +00:00
|
|
|
var filesPtr = filesClean.map { UnsafeMutablePointer<CChar>(strdup($0)) }
|
|
|
|
mp_event_drop_files(input, Int32(files.count), &filesPtr, action)
|
|
|
|
for charPtr in filesPtr { free(UnsafeMutablePointer(mutating: charPtr)) }
|
2024-03-09 14:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:25:23 +00:00
|
|
|
private func useAltGr() -> Bool {
|
|
|
|
guard let input = input else { return false }
|
|
|
|
return mp_input_use_alt_gr(input)
|
2024-03-09 14:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func wakeup() {
|
|
|
|
lock.withLock {
|
|
|
|
guard let input = input else { return }
|
|
|
|
mp_input_wakeup(input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 20:50:35 +00:00
|
|
|
func signal(input: OpaquePointer? = nil) {
|
2024-03-09 14:48:54 +00:00
|
|
|
lock.withLock {
|
|
|
|
self.input = input
|
|
|
|
if input != nil { lock.signal() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func wait() {
|
|
|
|
lock.withLock { while input == nil { lock.wait() } }
|
|
|
|
}
|
|
|
|
}
|