tools: Count argc after parsing GOption on Windows
[platform/upstream/gstreamer.git] / subprojects / gstreamer / gst / gstmacos.m
1 #include "gstmacos.h"
2 #include <Cocoa/Cocoa.h>
3
4 typedef struct _ThreadArgs ThreadArgs;
5
6 struct _ThreadArgs {
7   void* main_func;
8   int argc;
9   char **argv;
10   gpointer user_data;
11   gboolean is_simple;
12 };
13
14 int
15 gst_thread_func (ThreadArgs *args)
16 {
17   int ret;
18   if (args->is_simple) {
19     ret = ((GstMainFuncSimple) args->main_func) (args->user_data);
20   } else {
21     ret = ((GstMainFunc) args->main_func) (args->argc, args->argv, args->user_data);
22   }
23
24   [NSApp terminate: nil];
25   return ret;
26 }
27
28 int
29 run_main_with_nsapp (ThreadArgs args)
30 {
31   GThread *gst_thread;
32
33   [NSApplication sharedApplication]; 
34   gst_thread = g_thread_new ("macos-gst-thread", (GThreadFunc) gst_thread_func, &args);
35   [NSApp run];
36
37   return GPOINTER_TO_INT (g_thread_join (gst_thread));
38 }
39
40 /**
41  * gst_macos_main:
42  * @main_func: (scope async): pointer to the main function to be called
43  * @argc: the amount of arguments passed in @argv
44  * @argv: (array length=argc): an array of arguments to be passed to the main function
45  * @user_data: (nullable): user data to be passed to the main function
46  *
47  * Starts an NSApplication on the main thread before calling 
48  * the provided main() function on a secondary thread. 
49  * 
50  * This ensures that GStreamer can correctly perform actions 
51  * such as creating a GL window, which require a Cocoa main loop 
52  * to be running on the main thread.
53  *
54  * Do not call this function more than once - especially while
55  * another one is still running - as that will cause unpredictable
56  * behaviour and most likely completely fail.
57  *
58  * Returns: the return value of the provided main_func
59  *
60  * Since: 1.22
61  */
62 int
63 gst_macos_main (GstMainFunc main_func, int argc, char **argv, gpointer user_data)
64 {
65   ThreadArgs args;
66
67   args.argc = argc;
68   args.argv = argv;
69   args.main_func = main_func;
70   args.user_data = user_data;
71   args.is_simple = FALSE;
72
73   return run_main_with_nsapp (args);
74 }
75
76 /**
77  * gst_macos_main_simple:
78  * @main_func: (scope async): pointer to the main function to be called
79  * @user_data: (nullable): user data to be passed to the main function
80  *
81  * Simplified variant of gst_macos_main(), meant to be used with bindings
82  * for languages which do not have to pass argc and argv like C does.
83  * See gst_macos_main() for a more detailed description.
84  *
85  * Returns: the return value of the provided main_func
86  *
87  * Since: 1.22
88  */
89 int 
90 gst_macos_main_simple (GstMainFuncSimple main_func, gpointer user_data)
91 {
92   ThreadArgs args;
93
94   args.argc = 0;
95   args.argv = NULL;
96   args.main_func = main_func;
97   args.user_data = user_data;
98   args.is_simple = TRUE;
99
100   return run_main_with_nsapp (args);
101 }