From 69d44d992c685fd2e0a795448371d209d4522f44 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 4 Jan 2014 17:09:41 +0100 Subject: [PATCH] cocoa: refactor files drag and drop on the Dock icon Use the newly added `mp_event_drop_files` core function instead of having logic in the platform dependent code. --- osdep/macosx_application.m | 40 +++++++++++++++----------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/osdep/macosx_application.m b/osdep/macosx_application.m index 1ae1752bf1..8632cfc987 100644 --- a/osdep/macosx_application.m +++ b/osdep/macosx_application.m @@ -22,6 +22,7 @@ #include "common/msg.h" #include "input/input.h" +#include "input/event.h" #include "input/keycodes.h" #include "osdep/macosx_application_objc.h" @@ -55,21 +56,6 @@ Application *mpv_shared_app(void) return (Application *)[Application sharedApplication]; } -static NSString *escape_loadfile_name(NSString *input) -{ - NSArray *mappings = @[ - @{ @"in": @"\\", @"out": @"\\\\" }, - @{ @"in": @"\"", @"out": @"\\\"" }, - ]; - - for (NSDictionary *mapping in mappings) { - input = [input stringByReplacingOccurrencesOfString:mapping[@"in"] - withString:mapping[@"out"]]; - } - - return input; -} - @implementation Application @synthesize files = _files; @synthesize argumentsList = _arguments_list; @@ -287,17 +273,23 @@ static NSString *escape_loadfile_name(NSString *input) } } + +- (void)handleFilesArray:(NSArray *)files +{ + size_t num_files = [files count]; + char **files_utf8 = talloc_array(NULL, char*, num_files); + [files enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL *_){ + char *filename = (char *)[obj UTF8String]; + size_t bytes = [obj lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; + files_utf8[i] = talloc_memdup(files_utf8, filename, bytes + 1); + }]; + mp_event_drop_files(self.inputContext, num_files, files_utf8); + talloc_free(files_utf8); +} + - (void)handleFiles { - void *ctx = talloc_new(NULL); - [self.files enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL *_){ - const char *file = [escape_loadfile_name(obj) UTF8String]; - const char *append = (i == 0) ? "" : " append"; - char *cmd = talloc_asprintf(ctx, "raw loadfile \"%s\"%s", file, append); - mp_cmd_t *cmdt = mp_input_parse_cmd(self.inputContext, bstr0(cmd), ""); - mp_input_queue_cmd(self.inputContext, cmdt); - }]; - talloc_free(ctx); + [self handleFilesArray:self.files]; } @end