From 26e779c9aa74b84e50d4ca2ea14adf4bc1d961ac 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. (cherry picked from commit 1e7831070f6ae1af0a1a29b0d680ef2907bf8cf6) --- old-makefile | 2 +- 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 + player/core.h | 1 - player/main-fn-unix.c | 15 ------------- player/main-fn-win.c | 55 ---------------------------------------------- player/main.c | 1 + wscript_build.py | 9 +++++--- 12 files changed, 84 insertions(+), 82 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 delete mode 100644 player/main-fn-unix.c delete mode 100644 player/main-fn-win.c diff --git a/old-makefile b/old-makefile index d7b1f1f1dc..1cbde2f31f 100644 --- a/old-makefile +++ b/old-makefile @@ -335,7 +335,7 @@ all: $(ALL_TARGETS) %.o: %.c $(CC) $(DEPFLAGS) $(CFLAGS) -c -o $@ $< -mpv: $(OBJECTS) player/main-fn-unix.o +mpv: $(OBJECTS) osdep/main-fn-unix.o $(CC) -o $@ $^ $(EXTRALIBS) input/input.c: input/input_conf.h 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[]); diff --git a/player/core.h b/player/core.h index da9401be36..ea7694d02a 100644 --- a/player/core.h +++ b/player/core.h @@ -404,7 +404,6 @@ struct track *select_track(struct MPContext *mpctx, enum stream_type type, int tid, int ffid, char **langs); // main.c -int mpv_main(int argc, char *argv[]); int mp_initialize(struct MPContext *mpctx, char **argv); struct MPContext *mp_create(void); void mp_destroy(struct MPContext *mpctx); diff --git a/player/main-fn-unix.c b/player/main-fn-unix.c deleted file mode 100644 index 87e1681987..0000000000 --- a/player/main-fn-unix.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "config.h" -#include "core.h" - -#if HAVE_COCOA -#include "osdep/macosx_application.h" -#endif - -int main(int argc, char *argv[]) -{ -#if HAVE_COCOA - return cocoa_main(mpv_main, argc, argv); -#else - return mpv_main(argc, argv); -#endif -} diff --git a/player/main-fn-win.c b/player/main-fn-win.c deleted file mode 100644 index 28fc5b3c24..0000000000 --- a/player/main-fn-win.c +++ /dev/null @@ -1,55 +0,0 @@ -#include - -#include "config.h" - -#include "osdep/io.h" -#include "osdep/terminal.h" - -#include "core.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/player/main.c b/player/main.c index 1d56f0ceda..3719f2156a 100644 --- a/player/main.c +++ b/player/main.c @@ -30,6 +30,7 @@ #include "osdep/io.h" #include "osdep/terminal.h" #include "osdep/timer.h" +#include "osdep/main-fn.h" #include "common/av_log.h" #include "common/codecs.h" diff --git a/wscript_build.py b/wscript_build.py index 89ec67c471..93642c4693 100644 --- a/wscript_build.py +++ b/wscript_build.py @@ -71,9 +71,12 @@ def build(ctx): source = "demux/ebml.c", target = "ebml_defs.c") - main_fn_c = { - 'win32': 'player/main-fn-win.c', - }.get(ctx.env.DEST_OS, "player/main-fn-unix.c") + if ctx.env.DEST_OS == 'win32': + main_fn_c = 'osdep/main-fn-win.c' + elif ctx.dependency_satisfied('cocoa'): + main_fn_c = 'osdep/main-fn-cocoa.c' + else: + main_fn_c = 'osdep/main-fn-unix.c' getch2_c = { 'win32': 'osdep/terminal-win.c', -- cgit v1.2.3