some redundant functions that jump through hoops.
Unified · Split
| @@ -117,7 +117,7 @@ static void terminate_cocoa_application(void) · 116 unmodified lines | ||
| 117 | 117 | { |
| 118 | 118 | if ([self modalWindow] || ![_eventsResponder processKeyEvent:event]) |
| 119 | 119 | [super sendEvent:event]; |
| 120 | − [_eventsResponder wakeup]; | |
| 120 | + [_eventsResponder.inputHelper wakeup]; | |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | - (id)init |
| @@ -162,7 +162,6 @@ static const char mac_icon[] = · 38 unmodified lines | ||
| 162 | 162 | - (NSTouchBar *)makeTouchBar |
| 163 | 163 | { |
| 164 | 164 | TouchBar *tBar = [[TouchBar alloc] init]; |
| 165 | − [tBar setApp:self]; | |
| 166 | 165 | tBar.delegate = tBar; |
| 167 | 166 | tBar.customizationIdentifier = customID; |
| 168 | 167 | tBar.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar]; |
| @@ -202,17 +201,6 @@ static const char mac_icon[] = · 33 unmodified lines | ||
| 202 | 201 | return &vo_sub_opts; |
| 203 | 202 | } |
| 204 | 203 | |
| 205 | −- (void)queueCommand:(char *)cmd | |
| 206 | −{ | |
| 207 | − [_eventsResponder queueCommand:cmd]; | |
| 208 | −} | |
| 209 | − | |
| 210 | −- (void)stopMPV:(char *)cmd | |
| 211 | −{ | |
| 212 | − if (![_eventsResponder queueCommand:cmd]) | |
| 213 | − terminate_cocoa_application(); | |
| 214 | −} | |
| 215 | − | |
| 216 | 204 | - (void)applicationWillFinishLaunching:(NSNotification *)notification |
| 217 | 205 | { |
| 218 | 206 | NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager]; |
| @@ -225,7 +213,8 @@ static const char mac_icon[] = · 6 unmodified lines | ||
| 225 | 213 | - (void)handleQuitEvent:(NSAppleEventDescriptor *)event |
| 226 | 214 | withReplyEvent:(NSAppleEventDescriptor *)replyEvent |
| 227 | 215 | { |
| 228 | − [self stopMPV:"quit"]; | |
| 216 | + if (![_eventsResponder.inputHelper command:@"quit"]) | |
| 217 | + terminate_cocoa_application(); | |
| 229 | 218 | } |
| 230 | 219 | |
| 231 | 220 | - (void)getUrl:(NSAppleEventDescriptor *)event |
| @@ -240,7 +229,7 @@ static const char mac_icon[] = · 8 unmodified lines | ||
| 240 | 229 | range:NSMakeRange(0, [MPV_PROTOCOL length])]; |
| 241 | 230 | |
| 242 | 231 | url = [url stringByRemovingPercentEncoding]; |
| 243 | − [_eventsResponder handleFilesArray:@[url]]; | |
| 232 | + [_eventsResponder.inputHelper openWithFiles:@[url]]; | |
| 244 | 233 | } |
| 245 | 234 | |
| 246 | 235 | - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames |
| @@ -249,14 +238,10 @@ static const char mac_icon[] = · 2 unmodified lines | ||
| 249 | 238 | mpv_shared_app().openCount--; |
| 250 | 239 | return; |
| 251 | 240 | } |
| 252 | − [self openFiles:filenames]; | |
| 253 | −} | |
| 254 | 241 | |
| 255 | −- (void)openFiles:(NSArray *)filenames | |
| 256 | −{ | |
| 257 | 242 | SEL cmpsel = @selector(localizedStandardCompare:); |
| 258 | 243 | NSArray *files = [filenames sortedArrayUsingSelector:cmpsel]; |
| 259 | − [_eventsResponder handleFilesArray:files]; | |
| 244 | + [_eventsResponder.inputHelper openWithFiles:files]; | |
| 260 | 245 | } |
| 261 | 246 | @end |
| 262 | 247 | |
| @@ -357,7 +342,7 @@ int cocoa_main(int argc, char *argv[]) · 94 unmodified lines | ||
| 357 | 342 | } |
| 358 | 343 | |
| 359 | 344 | mp_thread_create(&playback_thread_id, playback_thread, &ctx); |
| 360 | − [[EventsResponder sharedInstance] waitForInputContext]; | |
| 345 | + [[EventsResponder sharedInstance].inputHelper wait]; | |
| 361 | 346 | cocoa_run_runloop(); |
| 362 | 347 | |
| 363 | 348 | // This should never be reached: cocoa_run_runloop blocks until the |
| @@ -27,9 +27,6 @@ struct mpv_handle; · 26 unmodified lines | ||
| 27 | 27 | |
| 28 | 28 | - (NSImage *)getMPVIcon; |
| 29 | 29 | - (void)processEvent:(struct mpv_event *)event; |
| 30 | −- (void)queueCommand:(char *)cmd; | |
| 31 | −- (void)stopMPV:(char *)cmd; | |
| 32 | −- (void)openFiles:(NSArray *)filenames; | |
| 33 | 30 | - (void)initCocoaCb:(struct mpv_handle *)ctx; |
| 34 | 31 | + (const struct m_sub_options *)getMacOSConf; |
| 35 | 32 | + (const struct m_sub_options *)getVoSubConf; |
| @@ -24,9 +24,6 @@ · 23 unmodified lines | ||
| 24 | 24 | struct input_ctx; |
| 25 | 25 | struct mpv_handle; |
| 26 | 26 | |
| 27 | −void cocoa_put_key(int keycode); | |
| 28 | −void cocoa_put_key_with_modifiers(int keycode, int modifiers); | |
| 29 | − | |
| 30 | 27 | void cocoa_init_media_keys(void); |
| 31 | 28 | void cocoa_uninit_media_keys(void); |
| 32 | 29 | |
| @@ -129,20 +129,9 @@ void cocoa_uninit_media_keys(void) · 128 unmodified lines | ||
| 129 | 129 | [[EventsResponder sharedInstance] stopMediaKeys]; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | −void cocoa_put_key(int keycode) | |
| 133 | −{ | |
| 134 | − [[EventsResponder sharedInstance] putKey:keycode]; | |
| 135 | −} | |
| 136 | − | |
| 137 | −void cocoa_put_key_with_modifiers(int keycode, int modifiers) | |
| 138 | −{ | |
| 139 | − keycode |= [[EventsResponder sharedInstance] mapKeyModifiers:modifiers]; | |
| 140 | − cocoa_put_key(keycode); | |
| 141 | −} | |
| 142 | − | |
| 143 | 132 | void cocoa_set_input_context(struct input_ctx *input_context) |
| 144 | 133 | { |
| 145 | − [[EventsResponder sharedInstance] setInputContext:input_context]; | |
| 134 | + [[EventsResponder sharedInstance].inputHelper signalWithInput:input_context]; | |
| 146 | 135 | } |
| 147 | 136 | |
| 148 | 137 | static void wakeup(void *context) |
| @@ -186,36 +175,6 @@ void cocoa_init_cocoa_cb(void) · 37 unmodified lines | ||
| 186 | 175 | return responder; |
| 187 | 176 | } |
| 188 | 177 | |
| 189 | −- (void)waitForInputContext | |
| 190 | −{ | |
| 191 | − [_inputHelper wait]; | |
| 192 | −} | |
| 193 | − | |
| 194 | −- (void)setInputContext:(struct input_ctx *)ctx | |
| 195 | −{ | |
| 196 | − [_inputHelper signalWithInput:ctx]; | |
| 197 | −} | |
| 198 | − | |
| 199 | −- (void)wakeup | |
| 200 | −{ | |
| 201 | − [_inputHelper wakeup]; | |
| 202 | −} | |
| 203 | − | |
| 204 | −- (bool)queueCommand:(char *)cmd | |
| 205 | −{ | |
| 206 | − return [_inputHelper command:[NSString stringWithUTF8String:cmd]]; | |
| 207 | −} | |
| 208 | − | |
| 209 | −- (void)putKey:(int)keycode | |
| 210 | −{ | |
| 211 | − [_inputHelper putKey:keycode]; | |
| 212 | −} | |
| 213 | − | |
| 214 | −- (BOOL)useAltGr | |
| 215 | −{ | |
| 216 | − return [_inputHelper useAltGr]; | |
| 217 | −} | |
| 218 | − | |
| 219 | 178 | - (void)setIsApplication:(BOOL)isApplication |
| 220 | 179 | { |
| 221 | 180 | _is_application = isApplication; |
| @@ -304,7 +263,7 @@ void cocoa_init_cocoa_cb(void) · 82 unmodified lines | ||
| 304 | 263 | if (cocoaModifiers & NSEventModifierFlagControl) |
| 305 | 264 | mask |= MP_KEY_MODIFIER_CTRL; |
| 306 | 265 | if (LeftAltPressed(cocoaModifiers) || |
| 307 | − (RightAltPressed(cocoaModifiers) && ![self useAltGr])) | |
| 266 | + (RightAltPressed(cocoaModifiers) && ![_inputHelper useAltGr])) | |
| 308 | 267 | mask |= MP_KEY_MODIFIER_ALT; |
| 309 | 268 | if (cocoaModifiers & NSEventModifierFlagCommand) |
| 310 | 269 | mask |= MP_KEY_MODIFIER_META; |
| @@ -329,9 +288,9 @@ void cocoa_init_cocoa_cb(void) · 18 unmodified lines | ||
| 329 | 288 | -(BOOL)handleMPKey:(int)key withMask:(int)mask |
| 330 | 289 | { |
| 331 | 290 | if (key > 0) { |
| 332 | − cocoa_put_key(key | mask); | |
| 291 | + [_inputHelper putKey:key | mask modifiers:0]; | |
| 333 | 292 | if (mask & MP_KEY_STATE_UP) |
| 334 | − cocoa_put_key(MP_INPUT_RELEASE_ALL); | |
| 293 | + [_inputHelper putKey:MP_INPUT_RELEASE_ALL modifiers:0]; | |
| 335 | 294 | return YES; |
| 336 | 295 | } else { |
| 337 | 296 | return NO; |
| @@ -344,7 +303,7 @@ void cocoa_init_cocoa_cb(void) · 6 unmodified lines | ||
| 344 | 303 | |
| 345 | 304 | NSString *chars; |
| 346 | 305 | |
| 347 | − if ([self useAltGr] && RightAltPressed([event modifierFlags])) { | |
| 306 | + if ([_inputHelper useAltGr] && RightAltPressed([event modifierFlags])) { | |
| 348 | 307 | chars = [event characters]; |
| 349 | 308 | } else { |
| 350 | 309 | chars = [event charactersIgnoringModifiers]; |
| @@ -369,9 +328,4 @@ void cocoa_init_cocoa_cb(void) · 18 unmodified lines | ||
| 369 | 328 | return false; |
| 370 | 329 | } |
| 371 | 330 | |
| 372 | −- (void)handleFilesArray:(NSArray *)files | |
| 373 | −{ | |
| 374 | − [_inputHelper openWithFiles:files]; | |
| 375 | −} | |
| 376 | − | |
| 377 | 331 | @end |
| @@ -27,16 +27,8 @@ struct input_ctx; · 26 unmodified lines | ||
| 27 | 27 | @interface EventsResponder : NSObject |
| 28 | 28 | |
| 29 | 29 | + (EventsResponder *)sharedInstance; |
| 30 | −- (void)setInputContext:(struct input_ctx *)ctx; | |
| 31 | 30 | - (void)setIsApplication:(BOOL)isApplication; |
| 32 | 31 | |
| 33 | −/// Blocks until inputContext is present. | |
| 34 | −- (void)waitForInputContext; | |
| 35 | −- (void)wakeup; | |
| 36 | −- (void)putKey:(int)keycode; | |
| 37 | −- (void)handleFilesArray:(NSArray *)files; | |
| 38 | − | |
| 39 | −- (bool)queueCommand:(char *)cmd; | |
| 40 | 32 | - (bool)processKeyEvent:(NSEvent *)event; |
| 41 | 33 | |
| 42 | 34 | - (BOOL)handleMPKey:(int)key withMask:(int)mask; |
| @@ -26,10 +26,10 @@ class InputHelper: NSObject { · 25 unmodified lines | ||
| 26 | 26 | self.mpv = mpv |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | − @objc func putKey(_ key: Int32) { | |
| 29 | + @objc func putKey(_ key: Int32, modifiers: NSEvent.ModifierFlags = .init(rawValue: 0)) { | |
| 30 | 30 | lock.withLock { |
| 31 | 31 | guard let input = input else { return } |
| 32 | − mp_input_put_key(input, key) | |
| 32 | + mp_input_put_key(input, key | mapModifier(modifiers)) | |
| 33 | 33 | } |
| 34 | 34 | } |
| 35 | 35 | |
| @@ -318,9 +318,7 @@ class MenuBar: NSObject { · 317 unmodified lines | ||
| 318 | 318 | |
| 319 | 319 | @objc func quit(_ menuItem: MenuItem) { |
| 320 | 320 | guard let menuConfig = menuItem.config else { return } |
| 321 | − menuConfig.command.withCString { | |
| 322 | − (NSApp as? Application)?.stopMPV(UnsafeMutablePointer<CChar>(mutating: $0)) | |
| 323 | − } | |
| 321 | + EventsResponder.sharedInstance().inputHelper.command(menuConfig.command) | |
| 324 | 322 | } |
| 325 | 323 | |
| 326 | 324 | @objc func openFiles() { |
| @@ -329,7 +327,7 @@ class MenuBar: NSObject { · 2 unmodified lines | ||
| 329 | 327 | panel.canChooseDirectories = true |
| 330 | 328 | |
| 331 | 329 | if panel.runModal() == .OK { |
| 332 | − (NSApp as? Application)?.openFiles(panel.urls.map { $0.path }) | |
| 330 | + EventsResponder.sharedInstance().inputHelper.open(files: panel.urls.map { $0.path }) | |
| 333 | 331 | } |
| 334 | 332 | } |
| 335 | 333 | |
| @@ -337,9 +335,7 @@ class MenuBar: NSObject { · 1 unmodified line | ||
| 337 | 335 | let panel = NSOpenPanel() |
| 338 | 336 | |
| 339 | 337 | if panel.runModal() == .OK, let url = panel.urls.first { |
| 340 | − "loadlist \"\(url.path)\"".withCString { | |
| 341 | − EventsResponder.sharedInstance().queueCommand(UnsafeMutablePointer<CChar>(mutating: $0)) | |
| 342 | − } | |
| 338 | + EventsResponder.sharedInstance().inputHelper.command("loadlist \"\(url.path)\"") | |
| 343 | 339 | } |
| 344 | 340 | } |
| 345 | 341 | |
| @@ -359,15 +355,13 @@ class MenuBar: NSObject { · 13 unmodified lines | ||
| 359 | 355 | } |
| 360 | 356 | |
| 361 | 357 | if alert.runModal() == .alertFirstButtonReturn && input.stringValue.count > 0 { |
| 362 | − (NSApp as? Application)?.openFiles([input.stringValue]) | |
| 358 | + EventsResponder.sharedInstance().inputHelper.open(files: [input.stringValue]) | |
| 363 | 359 | } |
| 364 | 360 | } |
| 365 | 361 | |
| 366 | 362 | @objc func command(_ menuItem: MenuItem) { |
| 367 | 363 | guard let menuConfig = menuItem.config else { return } |
| 368 | − menuConfig.command.withCString { | |
| 369 | − EventsResponder.sharedInstance().queueCommand(UnsafeMutablePointer<CChar>(mutating: $0)) | |
| 370 | − } | |
| 364 | + EventsResponder.sharedInstance().inputHelper.command(menuConfig.command) | |
| 371 | 365 | } |
| 372 | 366 | |
| 373 | 367 | @objc func url(_ menuItem: MenuItem) { |
| @@ -165,11 +165,8 @@ class RemoteCommandCenter: NSObject { · 164 unmodified lines | ||
| 165 | 165 | return .commandFailed |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | − let success = String(format: "seek %.02f absolute", posEvent.positionTime).withCString { | |
| 169 | − EventsResponder.sharedInstance().queueCommand(UnsafeMutablePointer<Int8>(mutating: $0)) | |
| 170 | − } | |
| 171 | − | |
| 172 | − return success ? .success : .commandFailed | |
| 168 | + let cmd = String(format: "seek %.02f absolute", posEvent.positionTime) | |
| 169 | + return EventsResponder.sharedInstance().inputHelper.command(cmd) ? .success : .commandFailed | |
| 173 | 170 | } |
| 174 | 171 | |
| 175 | 172 | @objc func processEvent(_ event: UnsafeMutablePointer<mpv_event>) { |
| @@ -16,7 +16,6 @@ · 15 unmodified lines | ||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | #import <Cocoa/Cocoa.h> |
| 19 | −#import "osdep/mac/application_objc.h" | |
| 20 | 19 | |
| 21 | 20 | #define BASE_ID @"io.mpv.touchbar" |
| 22 | 21 | static NSTouchBarCustomizationIdentifier customID = BASE_ID; |
| @@ -37,7 +36,6 @@ struct mpv_event; · 14 unmodified lines | ||
| 37 | 36 | |
| 38 | 37 | -(void)processEvent:(struct mpv_event *)event; |
| 39 | 38 | |
| 40 | −@property(nonatomic, retain) Application *app; | |
| 41 | 39 | @property(nonatomic, retain) NSDictionary *touchbarItems; |
| 42 | 40 | @property(nonatomic, assign) double duration; |
| 43 | 41 | @property(nonatomic, assign) double position; |
| @@ -17,10 +17,16 @@ · 16 unmodified lines | ||
| 17 | 17 | |
| 18 | 18 | #include "player/client.h" |
| 19 | 19 | #import "osdep/mac/touchbar.h" |
| 20 | +#import "osdep/mac/events_objc.h" | |
| 21 | + | |
| 22 | +#include "config.h" | |
| 23 | + | |
| 24 | +#if HAVE_SWIFT | |
| 25 | +#include "osdep/mac/swift.h" | |
| 26 | +#endif | |
| 20 | 27 | |
| 21 | 28 | @implementation TouchBar |
| 22 | 29 | |
| 23 | −@synthesize app = _app; | |
| 24 | 30 | @synthesize touchbarItems = _touchbar_items; |
| 25 | 31 | @synthesize duration = _duration; |
| 26 | 32 | @synthesize position = _position; |
| @@ -231,7 +237,7 @@ · 204 unmodified lines | ||
| 231 | 237 | - (void)buttonAction:(NSButton *)sender |
| 232 | 238 | { |
| 233 | 239 | NSString *identifier = [self getIdentifierFromView:sender]; |
| 234 | − [self.app queueCommand:(char *)[self.touchbarItems[identifier][@"cmd"] UTF8String]]; | |
| 240 | + [[EventsResponder sharedInstance].inputHelper command:self.touchbarItems[identifier][@"cmd"]]; | |
| 235 | 241 | } |
| 236 | 242 | |
| 237 | 243 | - (void)seekbarChanged:(NSSlider *)slider |
| @@ -239,7 +245,7 @@ · 1 unmodified line | ||
| 239 | 245 | NSString *identifier = [self getIdentifierFromView:slider]; |
| 240 | 246 | NSString *seek = [NSString stringWithFormat: |
| 241 | 247 | self.touchbarItems[identifier][@"cmd"], slider.doubleValue]; |
| 242 | − [self.app queueCommand:(char *)[seek UTF8String]]; | |
| 248 | + [[EventsResponder sharedInstance].inputHelper command:seek]; | |
| 243 | 249 | } |
| 244 | 250 | |
| 245 | 251 | - (NSString *)formatTime:(int)time |
| @@ -53,7 +53,7 @@ class View: NSView, CALayerDelegate { · 52 unmodified lines | ||
| 53 | 53 | addTrackingArea(tracker!) |
| 54 | 54 | |
| 55 | 55 | if containsMouseLocation() { |
| 56 | − cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0) | |
| 56 | + input?.putKey(SWIFT_KEY_MOUSE_LEAVE) | |
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | |
| @@ -118,14 +118,14 @@ class View: NSView, CALayerDelegate { · 58 unmodified lines | ||
| 118 | 118 | |
| 119 | 119 | override func mouseEntered(with event: NSEvent) { |
| 120 | 120 | if input?.mouseEnabled() ?? true { |
| 121 | − cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_ENTER, 0) | |
| 121 | + input?.putKey(SWIFT_KEY_MOUSE_ENTER) | |
| 122 | 122 | } |
| 123 | 123 | common.updateCursorVisibility() |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | override func mouseExited(with event: NSEvent) { |
| 127 | 127 | if input?.mouseEnabled() ?? true { |
| 128 | − cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0) | |
| 128 | + input?.putKey(SWIFT_KEY_MOUSE_LEAVE) | |
| 129 | 129 | } |
| 130 | 130 | common.titleBar?.hide() |
| 131 | 131 | common.setCursorVisibility(true) |
| @@ -202,7 +202,7 @@ class View: NSView, CALayerDelegate { · 70 unmodified lines | ||
| 202 | 202 | func signalMouseEvent(_ event: NSEvent, _ state: UInt32) { |
| 203 | 203 | hasMouseDown = state == MP_KEY_STATE_DOWN |
| 204 | 204 | let mpkey = getMpvButton(event) |
| 205 | − cocoa_put_key_with_modifiers((mpkey | Int32(state)), Int32(event.modifierFlags.rawValue)) | |
| 205 | + input?.putKey((mpkey | Int32(state)), modifiers: event.modifierFlags) | |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | func signalMouseMovement(_ event: NSEvent) { |
| @@ -250,7 +250,7 @@ class View: NSView, CALayerDelegate { · 41 unmodified lines | ||
| 250 | 250 | mpkey = deltaX > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | − cocoa_put_key_with_modifiers(mpkey, Int32(modifiers.rawValue)) | |
| 253 | + input?.putKey(mpkey, modifiers: modifiers) | |
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | |
| @@ -556,7 +556,7 @@ class Window: NSWindow, NSWindowDelegate { · 555 unmodified lines | ||
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | func windowShouldClose(_ sender: NSWindow) -> Bool { |
| 559 | − cocoa_put_key(MP_KEY_CLOSE_WIN) | |
| 559 | + input?.putKey(MP_KEY_CLOSE_WIN) | |
| 560 | 560 | return false |
| 561 | 561 | } |
| 562 | 562 | |