From 1e7831070f6ae1af0a1a29b0d680ef2907bf8cf6 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 2 May 2015 18:47:57 +0200 Subject: build: move main-fn files to osdep And split the Cocoa and Unix cases. Simplify the Cocoa case slightly by calling mpv_main directly, instead of passing a function pointer. Also add a comment explaining why Cocoa needs a special case at all. --- osdep/macosx_application.h | 4 +--- osdep/macosx_application.m | 7 +++--- osdep/main-fn-cocoa.c | 10 +++++++++ osdep/main-fn-unix.c | 6 +++++ osdep/main-fn-win.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++ osdep/main-fn.h | 1 + 6 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 osdep/main-fn-cocoa.c create mode 100644 osdep/main-fn-unix.c create mode 100644 osdep/main-fn-win.c create mode 100644 osdep/main-fn.h (limited to 'osdep') diff --git a/osdep/macosx_application.h b/osdep/macosx_application.h index c48d1154a0..fea34a3705 100644 --- a/osdep/macosx_application.h +++ b/osdep/macosx_application.h @@ -18,8 +18,6 @@ #ifndef MPV_MACOSX_APPLICATION #define MPV_MACOSX_APPLICATION -typedef int (*mpv_main_fn)(int, char**); - // Menu Keys identifing menu items typedef enum { MPM_H_SIZE, @@ -30,7 +28,7 @@ typedef enum { } MPMenuKey; // multithreaded wrapper for mpv_main -int cocoa_main(mpv_main_fn mpv_main, int argc, char *argv[]); +int cocoa_main(int argc, char *argv[]); void cocoa_register_menu_item_action(MPMenuKey key, void* action); #endif /* MPV_MACOSX_APPLICATION */ diff --git a/osdep/macosx_application.m b/osdep/macosx_application.m index 2ff3386f76..595c47e3c6 100644 --- a/osdep/macosx_application.m +++ b/osdep/macosx_application.m @@ -26,6 +26,7 @@ #include "osdep/macosx_compat.h" #import "osdep/macosx_events_objc.h" #include "osdep/threads.h" +#include "osdep/main-fn.h" #define MPV_PROTOCOL @"mpv://" @@ -252,7 +253,6 @@ static void terminate_cocoa_application(void) @end struct playback_thread_ctx { - mpv_main_fn mpv_main; int *argc; char ***argv; }; @@ -269,7 +269,7 @@ static void *playback_thread(void *ctx_obj) mpthread_set_name("playback core (OSX)"); @autoreleasepool { struct playback_thread_ctx *ctx = (struct playback_thread_ctx*) ctx_obj; - int r = ctx->mpv_main(*ctx->argc, *ctx->argv); + int r = mpv_main(*ctx->argc, *ctx->argv); terminate_cocoa_application(); // normally never reached - unless the cocoa mainloop hasn't started yet exit(r); @@ -361,13 +361,12 @@ static bool bundle_started_from_finder(int argc, char **argv) } } -int cocoa_main(mpv_main_fn mpv_main, int argc, char *argv[]) +int cocoa_main(int argc, char *argv[]) { @autoreleasepool { application_instantiated = true; struct playback_thread_ctx ctx = {0}; - ctx.mpv_main = mpv_main; ctx.argc = &argc; ctx.argv = &argv; diff --git a/osdep/main-fn-cocoa.c b/osdep/main-fn-cocoa.c new file mode 100644 index 0000000000..eeed127ead --- /dev/null +++ b/osdep/main-fn-cocoa.c @@ -0,0 +1,10 @@ +#include "osdep/macosx_application.h" + +// This is needed because Cocoa absolutely requires creating the NSApplication +// singleton and running it in the "main" thread. It is apparently not +// possible to do this on a separate thread at all. It is not known how +// Apple managed this colossal fuckup. +int main(int argc, char *argv[]) +{ + return cocoa_main(argc, argv); +} diff --git a/osdep/main-fn-unix.c b/osdep/main-fn-unix.c new file mode 100644 index 0000000000..c30c4a91ec --- /dev/null +++ b/osdep/main-fn-unix.c @@ -0,0 +1,6 @@ +#include "main-fn.h" + +int main(int argc, char *argv[]) +{ + return mpv_main(argc, argv); +} diff --git a/osdep/main-fn-win.c b/osdep/main-fn-win.c new file mode 100644 index 0000000000..d8dbcd5a8b --- /dev/null +++ b/osdep/main-fn-win.c @@ -0,0 +1,55 @@ +#include + +#include "config.h" + +#include "common/common.h" +#include "osdep/io.h" +#include "osdep/terminal.h" +#include "osdep/main-fn.h" + +int wmain(int argc, wchar_t *argv[]); + +// mpv does its own wildcard expansion in the option parser +int _dowildcard = 0; + +static bool is_valid_handle(HANDLE h) +{ + return h != INVALID_HANDLE_VALUE && h != NULL && + GetFileType(h) != FILE_TYPE_UNKNOWN; +} + +static bool has_redirected_stdio(void) +{ + return is_valid_handle(GetStdHandle(STD_INPUT_HANDLE)) || + is_valid_handle(GetStdHandle(STD_OUTPUT_HANDLE)) || + is_valid_handle(GetStdHandle(STD_ERROR_HANDLE)); +} + +int wmain(int argc, wchar_t *argv[]) +{ + // If started from the console wrapper (see osdep/win32-console-wrapper.c), + // attach to the console and set up the standard IO handles + bool has_console = terminal_try_attach(); + + // If mpv is started from Explorer, the Run dialog or the Start Menu, it + // will have no console and no standard IO handles. In this case, the user + // is expecting mpv to show some UI, so enable the pseudo-GUI profile. + bool gui = !has_console && !has_redirected_stdio(); + + int argv_len = 0; + char **argv_u8 = NULL; + + // Build mpv's UTF-8 argv, and add the pseudo-GUI profile if necessary + if (argv[0]) + MP_TARRAY_APPEND(NULL, argv_u8, argv_len, mp_to_utf8(argv_u8, argv[0])); + if (gui) + MP_TARRAY_APPEND(NULL, argv_u8, argv_len, "--profile=pseudo-gui"); + for (int i = 1; i < argc; i++) + MP_TARRAY_APPEND(NULL, argv_u8, argv_len, mp_to_utf8(argv_u8, argv[i])); + MP_TARRAY_APPEND(NULL, argv_u8, argv_len, NULL); + + int ret = mpv_main(argv_len - 1, argv_u8); + + talloc_free(argv_u8); + return ret; +} diff --git a/osdep/main-fn.h b/osdep/main-fn.h new file mode 100644 index 0000000000..8f20308c8b --- /dev/null +++ b/osdep/main-fn.h @@ -0,0 +1 @@ +int mpv_main(int argc, char *argv[]); -- cgit v1.2.3