History

mac/events: remove redundant functions and optimise input helper usage

some redundant functions that jump through hoops.

der richter committed 2 years ago

Commit 18fb7141 parent 055e9cd12 files · +35 −115Browse files

Unified · Split

12 files
  1. osdep/mac/application.m +6 −21
  2. osdep/mac/application_objc.h +0 −3
  3. osdep/mac/events.h +0 −3
  4. osdep/mac/events.m +5 −51
  5. osdep/mac/events_objc.h +0 −8
  6. osdep/mac/input_helper.swift +2 −2
  7. osdep/mac/menu_bar.swift +5 −11
  8. osdep/mac/remote_command_center.swift +2 −5
  9. osdep/mac/touchbar.h +0 −2
  10. osdep/mac/touchbar.m +9 −3
  11. video/out/mac/view.swift +5 −5
  12. video/out/mac/window.swift +1 −1
osdep/mac/application.m+6−21
@@ -117,7 +117,7 @@ static void terminate_cocoa_application(void) · 116 unmodified lines
117117 {
118118 if ([self modalWindow] || ![_eventsResponder processKeyEvent:event])
119119 [super sendEvent:event];
120− [_eventsResponder wakeup];
120+ [_eventsResponder.inputHelper wakeup];
121121 }
122122
123123 - (id)init
@@ -162,7 +162,6 @@ static const char mac_icon[] = · 38 unmodified lines
162162 - (NSTouchBar *)makeTouchBar
163163 {
164164 TouchBar *tBar = [[TouchBar alloc] init];
165− [tBar setApp:self];
166165 tBar.delegate = tBar;
167166 tBar.customizationIdentifier = customID;
168167 tBar.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar];
@@ -202,17 +201,6 @@ static const char mac_icon[] = · 33 unmodified lines
202201 return &vo_sub_opts;
203202 }
204203
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−
216204 - (void)applicationWillFinishLaunching:(NSNotification *)notification
217205 {
218206 NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
@@ -225,7 +213,8 @@ static const char mac_icon[] = · 6 unmodified lines
225213 - (void)handleQuitEvent:(NSAppleEventDescriptor *)event
226214 withReplyEvent:(NSAppleEventDescriptor *)replyEvent
227215 {
228− [self stopMPV:"quit"];
216+ if (![_eventsResponder.inputHelper command:@"quit"])
217+ terminate_cocoa_application();
229218 }
230219
231220 - (void)getUrl:(NSAppleEventDescriptor *)event
@@ -240,7 +229,7 @@ static const char mac_icon[] = · 8 unmodified lines
240229 range:NSMakeRange(0, [MPV_PROTOCOL length])];
241230
242231 url = [url stringByRemovingPercentEncoding];
243− [_eventsResponder handleFilesArray:@[url]];
232+ [_eventsResponder.inputHelper openWithFiles:@[url]];
244233 }
245234
246235 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
@@ -249,14 +238,10 @@ static const char mac_icon[] = · 2 unmodified lines
249238 mpv_shared_app().openCount--;
250239 return;
251240 }
252− [self openFiles:filenames];
253−}
254241
255−- (void)openFiles:(NSArray *)filenames
256−{
257242 SEL cmpsel = @selector(localizedStandardCompare:);
258243 NSArray *files = [filenames sortedArrayUsingSelector:cmpsel];
259− [_eventsResponder handleFilesArray:files];
244+ [_eventsResponder.inputHelper openWithFiles:files];
260245 }
261246 @end
262247
@@ -357,7 +342,7 @@ int cocoa_main(int argc, char *argv[]) · 94 unmodified lines
357342 }
358343
359344 mp_thread_create(&playback_thread_id, playback_thread, &ctx);
360− [[EventsResponder sharedInstance] waitForInputContext];
345+ [[EventsResponder sharedInstance].inputHelper wait];
361346 cocoa_run_runloop();
362347
363348 // This should never be reached: cocoa_run_runloop blocks until the
osdep/mac/application_objc.h+0−3
@@ -27,9 +27,6 @@ struct mpv_handle; · 26 unmodified lines
2727
2828 - (NSImage *)getMPVIcon;
2929 - (void)processEvent:(struct mpv_event *)event;
30−- (void)queueCommand:(char *)cmd;
31−- (void)stopMPV:(char *)cmd;
32−- (void)openFiles:(NSArray *)filenames;
3330 - (void)initCocoaCb:(struct mpv_handle *)ctx;
3431 + (const struct m_sub_options *)getMacOSConf;
3532 + (const struct m_sub_options *)getVoSubConf;
osdep/mac/events.h+0−3
@@ -24,9 +24,6 @@ · 23 unmodified lines
2424 struct input_ctx;
2525 struct mpv_handle;
2626
27−void cocoa_put_key(int keycode);
28−void cocoa_put_key_with_modifiers(int keycode, int modifiers);
29−
3027 void cocoa_init_media_keys(void);
3128 void cocoa_uninit_media_keys(void);
3229
osdep/mac/events.m+5−51
@@ -129,20 +129,9 @@ void cocoa_uninit_media_keys(void) · 128 unmodified lines
129129 [[EventsResponder sharedInstance] stopMediaKeys];
130130 }
131131
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−
143132 void cocoa_set_input_context(struct input_ctx *input_context)
144133 {
145− [[EventsResponder sharedInstance] setInputContext:input_context];
134+ [[EventsResponder sharedInstance].inputHelper signalWithInput:input_context];
146135 }
147136
148137 static void wakeup(void *context)
@@ -186,36 +175,6 @@ void cocoa_init_cocoa_cb(void) · 37 unmodified lines
186175 return responder;
187176 }
188177
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−
219178 - (void)setIsApplication:(BOOL)isApplication
220179 {
221180 _is_application = isApplication;
@@ -304,7 +263,7 @@ void cocoa_init_cocoa_cb(void) · 82 unmodified lines
304263 if (cocoaModifiers & NSEventModifierFlagControl)
305264 mask |= MP_KEY_MODIFIER_CTRL;
306265 if (LeftAltPressed(cocoaModifiers) ||
307− (RightAltPressed(cocoaModifiers) && ![self useAltGr]))
266+ (RightAltPressed(cocoaModifiers) && ![_inputHelper useAltGr]))
308267 mask |= MP_KEY_MODIFIER_ALT;
309268 if (cocoaModifiers & NSEventModifierFlagCommand)
310269 mask |= MP_KEY_MODIFIER_META;
@@ -329,9 +288,9 @@ void cocoa_init_cocoa_cb(void) · 18 unmodified lines
329288 -(BOOL)handleMPKey:(int)key withMask:(int)mask
330289 {
331290 if (key > 0) {
332− cocoa_put_key(key | mask);
291+ [_inputHelper putKey:key | mask modifiers:0];
333292 if (mask & MP_KEY_STATE_UP)
334− cocoa_put_key(MP_INPUT_RELEASE_ALL);
293+ [_inputHelper putKey:MP_INPUT_RELEASE_ALL modifiers:0];
335294 return YES;
336295 } else {
337296 return NO;
@@ -344,7 +303,7 @@ void cocoa_init_cocoa_cb(void) · 6 unmodified lines
344303
345304 NSString *chars;
346305
347− if ([self useAltGr] && RightAltPressed([event modifierFlags])) {
306+ if ([_inputHelper useAltGr] && RightAltPressed([event modifierFlags])) {
348307 chars = [event characters];
349308 } else {
350309 chars = [event charactersIgnoringModifiers];
@@ -369,9 +328,4 @@ void cocoa_init_cocoa_cb(void) · 18 unmodified lines
369328 return false;
370329 }
371330
372−- (void)handleFilesArray:(NSArray *)files
373−{
374− [_inputHelper openWithFiles:files];
375−}
376−
377331 @end
osdep/mac/events_objc.h+0−8
@@ -27,16 +27,8 @@ struct input_ctx; · 26 unmodified lines
2727 @interface EventsResponder : NSObject
2828
2929 + (EventsResponder *)sharedInstance;
30−- (void)setInputContext:(struct input_ctx *)ctx;
3130 - (void)setIsApplication:(BOOL)isApplication;
3231
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;
4032 - (bool)processKeyEvent:(NSEvent *)event;
4133
4234 - (BOOL)handleMPKey:(int)key withMask:(int)mask;
osdep/mac/input_helper.swift+2−2
@@ -26,10 +26,10 @@ class InputHelper: NSObject { · 25 unmodified lines
2626 self.mpv = mpv
2727 }
2828
29− @objc func putKey(_ key: Int32) {
29+ @objc func putKey(_ key: Int32, modifiers: NSEvent.ModifierFlags = .init(rawValue: 0)) {
3030 lock.withLock {
3131 guard let input = input else { return }
32− mp_input_put_key(input, key)
32+ mp_input_put_key(input, key | mapModifier(modifiers))
3333 }
3434 }
3535
osdep/mac/menu_bar.swift+5−11
@@ -318,9 +318,7 @@ class MenuBar: NSObject { · 317 unmodified lines
318318
319319 @objc func quit(_ menuItem: MenuItem) {
320320 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)
324322 }
325323
326324 @objc func openFiles() {
@@ -329,7 +327,7 @@ class MenuBar: NSObject { · 2 unmodified lines
329327 panel.canChooseDirectories = true
330328
331329 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 })
333331 }
334332 }
335333
@@ -337,9 +335,7 @@ class MenuBar: NSObject { · 1 unmodified line
337335 let panel = NSOpenPanel()
338336
339337 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)\"")
343339 }
344340 }
345341
@@ -359,15 +355,13 @@ class MenuBar: NSObject { · 13 unmodified lines
359355 }
360356
361357 if alert.runModal() == .alertFirstButtonReturn && input.stringValue.count > 0 {
362− (NSApp as? Application)?.openFiles([input.stringValue])
358+ EventsResponder.sharedInstance().inputHelper.open(files: [input.stringValue])
363359 }
364360 }
365361
366362 @objc func command(_ menuItem: MenuItem) {
367363 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)
371365 }
372366
373367 @objc func url(_ menuItem: MenuItem) {
osdep/mac/remote_command_center.swift+2−5
@@ -165,11 +165,8 @@ class RemoteCommandCenter: NSObject { · 164 unmodified lines
165165 return .commandFailed
166166 }
167167
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
173170 }
174171
175172 @objc func processEvent(_ event: UnsafeMutablePointer<mpv_event>) {
osdep/mac/touchbar.h+0−2
@@ -16,7 +16,6 @@ · 15 unmodified lines
1616 */
1717
1818 #import <Cocoa/Cocoa.h>
19−#import "osdep/mac/application_objc.h"
2019
2120 #define BASE_ID @"io.mpv.touchbar"
2221 static NSTouchBarCustomizationIdentifier customID = BASE_ID;
@@ -37,7 +36,6 @@ struct mpv_event; · 14 unmodified lines
3736
3837 -(void)processEvent:(struct mpv_event *)event;
3938
40−@property(nonatomic, retain) Application *app;
4139 @property(nonatomic, retain) NSDictionary *touchbarItems;
4240 @property(nonatomic, assign) double duration;
4341 @property(nonatomic, assign) double position;
osdep/mac/touchbar.m+9−3
@@ -17,10 +17,16 @@ · 16 unmodified lines
1717
1818 #include "player/client.h"
1919 #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
2027
2128 @implementation TouchBar
2229
23−@synthesize app = _app;
2430 @synthesize touchbarItems = _touchbar_items;
2531 @synthesize duration = _duration;
2632 @synthesize position = _position;
@@ -231,7 +237,7 @@ · 204 unmodified lines
231237 - (void)buttonAction:(NSButton *)sender
232238 {
233239 NSString *identifier = [self getIdentifierFromView:sender];
234− [self.app queueCommand:(char *)[self.touchbarItems[identifier][@"cmd"] UTF8String]];
240+ [[EventsResponder sharedInstance].inputHelper command:self.touchbarItems[identifier][@"cmd"]];
235241 }
236242
237243 - (void)seekbarChanged:(NSSlider *)slider
@@ -239,7 +245,7 @@ · 1 unmodified line
239245 NSString *identifier = [self getIdentifierFromView:slider];
240246 NSString *seek = [NSString stringWithFormat:
241247 self.touchbarItems[identifier][@"cmd"], slider.doubleValue];
242− [self.app queueCommand:(char *)[seek UTF8String]];
248+ [[EventsResponder sharedInstance].inputHelper command:seek];
243249 }
244250
245251 - (NSString *)formatTime:(int)time
video/out/mac/view.swift+5−5
@@ -53,7 +53,7 @@ class View: NSView, CALayerDelegate { · 52 unmodified lines
5353 addTrackingArea(tracker!)
5454
5555 if containsMouseLocation() {
56− cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0)
56+ input?.putKey(SWIFT_KEY_MOUSE_LEAVE)
5757 }
5858 }
5959
@@ -118,14 +118,14 @@ class View: NSView, CALayerDelegate { · 58 unmodified lines
118118
119119 override func mouseEntered(with event: NSEvent) {
120120 if input?.mouseEnabled() ?? true {
121− cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_ENTER, 0)
121+ input?.putKey(SWIFT_KEY_MOUSE_ENTER)
122122 }
123123 common.updateCursorVisibility()
124124 }
125125
126126 override func mouseExited(with event: NSEvent) {
127127 if input?.mouseEnabled() ?? true {
128− cocoa_put_key_with_modifiers(SWIFT_KEY_MOUSE_LEAVE, 0)
128+ input?.putKey(SWIFT_KEY_MOUSE_LEAVE)
129129 }
130130 common.titleBar?.hide()
131131 common.setCursorVisibility(true)
@@ -202,7 +202,7 @@ class View: NSView, CALayerDelegate { · 70 unmodified lines
202202 func signalMouseEvent(_ event: NSEvent, _ state: UInt32) {
203203 hasMouseDown = state == MP_KEY_STATE_DOWN
204204 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)
206206 }
207207
208208 func signalMouseMovement(_ event: NSEvent) {
@@ -250,7 +250,7 @@ class View: NSView, CALayerDelegate { · 41 unmodified lines
250250 mpkey = deltaX > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT
251251 }
252252
253− cocoa_put_key_with_modifiers(mpkey, Int32(modifiers.rawValue))
253+ input?.putKey(mpkey, modifiers: modifiers)
254254 }
255255 }
256256
video/out/mac/window.swift+1−1
@@ -556,7 +556,7 @@ class Window: NSWindow, NSWindowDelegate { · 555 unmodified lines
556556 }
557557
558558 func windowShouldClose(_ sender: NSWindow) -> Bool {
559− cocoa_put_key(MP_KEY_CLOSE_WIN)
559+ input?.putKey(MP_KEY_CLOSE_WIN)
560560 return false
561561 }
562562

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t