1 /*****************************************************************************
2 * darwinvlc.m: OS X specific main executable for VLC media player
3 *****************************************************************************
4 * Copyright (C) 2013-2015 VLC authors and VideoLAN
6 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
7 * David Fuhrmann <dfuhrmann at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_charset.h>
37 #import <CoreFoundation/CoreFoundation.h>
38 #import <Cocoa/Cocoa.h>
41 #import <Breakpad/Breakpad.h>
46 * Handler called when VLC asks to terminate the program.
48 static void vlc_terminate(void *data)
52 dispatch_async(dispatch_get_main_queue(), ^{
54 * Stop the main loop. When using the CoreFoundation mainloop, simply
55 * CFRunLoopStop can be used.
57 * But this does not work when having an interface.
58 * In this case, [NSApp stop:nil] needs to be used, but the used flag is only
59 * evaluated at the end of main loop event processing. This is always true
60 * in the case of code inside a action method. But here, this is
61 * not true and thus we need to send an dummy event to make sure the stop
62 * flag is actually processed by the main loop.
65 CFRunLoopStop(CFRunLoopGetCurrent());
69 NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined
70 location:NSMakePoint(0,0)
78 [NSApp postEvent:event atStart:YES];
85 BreakpadRef initBreakpad()
89 /* Create caches directory in case it does not exist */
90 NSFileManager *fileManager = [NSFileManager defaultManager];
91 NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
92 NSString *bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
93 NSString *cacheAppPath = [cachePath stringByAppendingPathComponent:bundleName];
94 if (![fileManager fileExistsAtPath:cacheAppPath]) {
95 [fileManager createDirectoryAtPath:cacheAppPath withIntermediateDirectories:NO attributes:nil error:nil];
98 /* Get Info.plist config */
99 NSMutableDictionary *breakpad_config = [[[NSBundle mainBundle] infoDictionary] mutableCopy];
101 /* Use in-process reporting */
102 [breakpad_config setObject:[NSNumber numberWithBool:YES]
103 forKey:@BREAKPAD_IN_PROCESS];
105 /* Set dump location */
106 [breakpad_config setObject:cacheAppPath
107 forKey:@BREAKPAD_DUMP_DIRECTORY];
109 bp = BreakpadCreate(breakpad_config);
114 /*****************************************************************************
115 * main: parse command line, start interface and spawn threads.
116 *****************************************************************************/
117 int main(int i_argc, const char *ppsz_argv[])
120 BreakpadRef breakpad = NULL;
122 if (!getenv("VLC_DISABLE_BREAKPAD"))
123 breakpad = initBreakpad();
126 /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
127 * if it is blocked in all thread.
128 * Note: this is NOT an excuse for not protecting against SIGPIPE. If
129 * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
130 signal(SIGPIPE, SIG_IGN);
131 /* Restore SIGCHLD in case our parent process ignores it. */
132 signal(SIGCHLD, SIG_DFL);
135 /* Activate malloc checking routines to detect heap corruptions. */
136 setenv("MALLOC_CHECK_", "2", 1);
140 setenv("VLC_PLUGIN_PATH", TOP_BUILDDIR"/modules", 1);
141 setenv("VLC_DATA_PATH", TOP_SRCDIR"/share", 1);
142 setenv("VLC_LIB_PATH", TOP_BUILDDIR"/modules", 1);
145 #ifndef ALLOW_RUN_AS_ROOT
148 fprintf(stderr, "VLC is not supposed to be run as root. Sorry.\n"
149 "If you need to use real-time priorities and/or privileged TCP ports\n"
150 "you can use %s-wrapper (make sure it is Set-UID root and\n"
151 "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
156 setlocale(LC_ALL, "");
158 if (isatty(STDERR_FILENO))
159 /* This message clutters error logs. It is printed only on a TTY.
160 * Fortunately, LibVLC prints version info with -vv anyway. */
161 fprintf(stderr, "VLC media player %s (revision %s)\n",
162 libvlc_get_version(), libvlc_get_changeset());
168 * The darwin version of VLC used GCD to dequeue interesting signals.
169 * For this to work, those signals must be blocked.
171 * There are two advantages over traditional signal handlers:
172 * - handling is done on a separate thread: no need to worry about async-safety,
173 * - EINTR is not generated: other threads need not handle that error.
174 * That being said, some LibVLC programs do not use sigwait(). Therefore
175 * EINTR must still be handled cleanly, notably from poll() calls.
177 * Signals that request a clean shutdown.
178 * We have to handle SIGTERM cleanly because of daemon mode. */
179 sigaddset(&set, SIGINT);
180 sigaddset(&set, SIGTERM);
182 /* SIGPIPE can happen and would crash the process. On modern systems,
183 * the MSG_NOSIGNAL flag protects socket write operations against SIGPIPE.
184 * But we still need to block SIGPIPE when:
185 * - writing to pipes,
186 * - using write() instead of send() for code not specific to sockets.
187 * LibVLC code assumes that SIGPIPE is blocked. Other LibVLC applications
188 * shall block it (or handle it somehow) too.
190 sigaddset(&set, SIGPIPE);
192 /* SIGCHLD must be dequeued to clean up zombie child processes.
193 * Furthermore the handler must not be set to SIG_IGN (see above).
194 * We cannot pragmatically handle EINTR, short reads and short writes
195 * in every code paths (including underlying libraries). So we just
196 * block SIGCHLD in all threads, and dequeue it below. */
197 sigaddset(&set, SIGCHLD);
199 /* Block all these signals */
200 pthread_sigmask(SIG_SETMASK, &set, NULL);
202 /* Handle signals with GCD */
203 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
204 dispatch_source_t sigIntSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, queue);
205 dispatch_source_t sigTermSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, queue);
206 dispatch_source_t sigChldSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGCHLD, 0, queue);
208 if (!sigIntSource || !sigTermSource || !sigChldSource)
211 dispatch_source_set_event_handler(sigIntSource, ^{
214 dispatch_source_set_event_handler(sigTermSource, ^{
218 dispatch_source_set_event_handler(sigChldSource, ^{
220 while(waitpid(-1, &status, WNOHANG) > 0)
224 dispatch_resume(sigIntSource);
225 dispatch_resume(sigTermSource);
226 dispatch_resume(sigChldSource);
229 /* Handle parameters */
230 const char *argv[i_argc + 2];
233 argv[argc++] = "--no-ignore-config";
234 argv[argc++] = "--media-library";
236 /* Overwrite system language */
237 CFPropertyListRef lang_pref = CFPreferencesCopyAppValue(CFSTR("language"),
238 kCFPreferencesCurrentApplication);
241 if (CFGetTypeID(lang_pref) == CFStringGetTypeID()) {
242 char *lang = FromCFString(lang_pref, kCFStringEncodingUTF8);
243 if (strncmp(lang, "auto", 4)) {
245 snprintf(tmp, 11, "LANG=%s", lang);
250 CFRelease(lang_pref);
253 ppsz_argv++; i_argc--; /* skip executable path */
255 /* When VLC.app is run by double clicking in Mac OS X < 10.9, the 2nd arg
256 * is the PSN - process serial number (a unique PID-ish thingie)
257 * still ok for real Darwin & when run from command line
258 * for example -psn_0_9306113 */
259 if (i_argc >= 1 && !strncmp(*ppsz_argv, "-psn" , 4))
260 ppsz_argv++, i_argc--;
262 memcpy (argv + argc, ppsz_argv, i_argc * sizeof(*argv));
266 /* Initialize libvlc */
267 libvlc_instance_t *vlc = libvlc_new(argc, argv);
272 libvlc_set_exit_handler(vlc, vlc_terminate, NULL);
273 libvlc_set_app_id(vlc, "org.VideoLAN.VLC", PACKAGE_VERSION, PACKAGE_NAME);
274 libvlc_set_user_agent(vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
276 if (libvlc_add_intf(vlc, NULL)) {
277 fprintf(stderr, "VLC cannot start any interface. Exiting.\n");
280 libvlc_playlist_play(vlc);
283 * Run the main loop. If the mac interface is not initialized, only the CoreFoundation
284 * runloop is used. Otherwise, [NSApp run] needs to be called, which setups more stuff
285 * before actually starting the loop.
299 dispatch_release(sigIntSource);
300 dispatch_release(sigTermSource);
301 dispatch_release(sigChldSource);
307 BreakpadRelease(breakpad);