1 /*****************************************************************************
2 * darwinvlc.c: the darwin-specific VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2013 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * Lots of other people, see the libvlc AUTHORS file
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
43 #include <TargetConditionals.h>
44 #import <CoreFoundation/CoreFoundation.h>
46 extern void vlc_enable_override (void);
48 static bool signal_ignored (int signum
)
52 if (sigaction (signum
, NULL
, &sa
))
54 return ((sa
.sa_flags
& SA_SIGINFO
)
55 ? (void *)sa
.sa_sigaction
: (void *)sa
.sa_handler
) == SIG_IGN
;
58 static void vlc_kill (void *data
)
61 pthread_kill (*ps
, SIGTERM
);
64 static void exit_timeout (int signum
)
67 signal (SIGINT
, SIG_DFL
);
70 /*****************************************************************************
71 * main: parse command line, start interface and spawn threads.
72 *****************************************************************************/
73 int main( int i_argc
, const char *ppsz_argv
[] )
75 /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
76 * if it is blocked in all thread.
77 * Note: this is NOT an excuse for not protecting against SIGPIPE. If
78 * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
79 signal (SIGPIPE
, SIG_IGN
);
80 /* Restore SIGCHLD in case our parent process ignores it. */
81 signal (SIGCHLD
, SIG_DFL
);
84 /* Activate malloc checking routines to detect heap corruptions. */
85 setenv ("MALLOC_CHECK_", "2", 1);
89 setenv ("VLC_PLUGIN_PATH", TOP_BUILDDIR
"/modules", 1);
90 setenv ("VLC_DATA_PATH", TOP_SRCDIR
"/share", 1);
93 #ifndef ALLOW_RUN_AS_ROOT
96 fprintf (stderr
, "VLC is not supposed to be run as root. Sorry.\n"
97 "If you need to use real-time priorities and/or privileged TCP ports\n"
98 "you can use %s-wrapper (make sure it is Set-UID root and\n"
99 "cannot be run by non-trusted users first).\n", ppsz_argv
[0]);
104 setlocale (LC_ALL
, "");
106 if (isatty (STDERR_FILENO
))
107 /* This message clutters error logs. It is printed only on a TTY.
108 * Fortunately, LibVLC prints version info with -vv anyway. */
109 fprintf (stderr
, "VLC media player %s (revision %s)\n",
110 libvlc_get_version(), libvlc_get_changeset());
115 /* VLC uses sigwait() to dequeue interesting signals.
116 * For this to work, those signals must be blocked in all threads,
117 * including the thread calling sigwait() (see the man page for details).
119 * There are two advantages to sigwait() over traditional signal handlers:
120 * - delivery is synchronous: no need to worry about async-safety,
121 * - EINTR is not generated: other threads need not handle that error.
122 * That being said, some LibVLC programs do not use sigwait(). Therefore
123 * EINTR must still be handled cleanly, notably from poll() calls.
125 * Signals that request a clean shutdown, and force an unclean shutdown
126 * if they are triggered again 2+ seconds later.
127 * We have to handle SIGTERM cleanly because of daemon mode. */
128 sigaddset (&set
, SIGINT
);
129 sigaddset (&set
, SIGHUP
);
130 sigaddset (&set
, SIGQUIT
);
131 sigaddset (&set
, SIGTERM
);
133 /* SIGPIPE can happen and would crash the process. On modern systems,
134 * the MSG_NOSIGNAL flag protects socket write operations against SIGPIPE.
135 * But we still need to block SIGPIPE when:
136 * - writing to pipes,
137 * - using write() instead of send() for code not specific to sockets.
138 * LibVLC code assumes that SIGPIPE is blocked. Other LibVLC applications
139 * shall block it (or handle it somehow) too.
141 sigaddset (&set
, SIGPIPE
);
143 /* SIGCHLD must be dequeued to clean up zombie child processes.
144 * Furthermore the handler must not be set to SIG_IGN (see above).
145 * We cannot pragmatically handle EINTR, short reads and short writes
146 * in every code paths (including underlying libraries). So we just
147 * block SIGCHLD in all threads, and dequeue it below. */
148 sigaddset (&set
, SIGCHLD
);
150 /* Block all these signals */
151 pthread_sigmask (SIG_SETMASK
, &set
, NULL
);
153 const char *argv
[i_argc
+ 2];
156 argv
[argc
++] = "--no-ignore-config";
157 argv
[argc
++] = "--media-library";
159 /* overwrite system language on Mac */
160 #if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR // TARGET_OS_MAC is unspecific
163 for (int i
= 0; i
< i_argc
; i
++) {
164 if (!strncmp(ppsz_argv
[i
], "--language", 10)) {
165 lang
= strstr(ppsz_argv
[i
], "=");
166 ppsz_argv
++, i_argc
--;
170 if (lang
&& strncmp( lang
, "auto", 4 )) {
172 snprintf(tmp
, 11, "LANG%s", lang
);
177 CFStringRef language
;
178 language
= (CFStringRef
)CFPreferencesCopyAppValue(CFSTR("language"),
179 kCFPreferencesCurrentApplication
);
181 CFIndex length
= CFStringGetLength(language
) + 1;
183 CFIndex maxSize
= CFStringGetMaximumSizeForEncoding(length
, kCFStringEncodingUTF8
);
184 lang
= (char *)malloc(maxSize
);
185 CFStringGetCString(language
, lang
, maxSize
- 1, kCFStringEncodingUTF8
);
187 if (strncmp( lang
, "auto", 4 )) {
189 snprintf(tmp
, 11, "LANG=%s", lang
);
197 ppsz_argv
++; i_argc
--; /* skip executable path */
199 /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
200 * is the PSN - process serial number (a unique PID-ish thingie)
201 * still ok for real Darwin & when run from command line
202 * for example -psn_0_9306113 */
203 if (i_argc
>= 1 && !strncmp (*ppsz_argv
, "-psn" , 4))
204 ppsz_argv
++, i_argc
--;
206 memcpy (argv
+ argc
, ppsz_argv
, i_argc
* sizeof (*argv
));
210 vlc_enable_override ();
212 pthread_t self
= pthread_self ();
214 /* Initialize libvlc */
215 libvlc_instance_t
*vlc
= libvlc_new (argc
, argv
);
220 libvlc_set_exit_handler (vlc
, vlc_kill
, &self
);
221 libvlc_set_app_id (vlc
, "org.VideoLAN.VLC", PACKAGE_VERSION
, PACKAGE_NAME
);
222 libvlc_set_user_agent (vlc
, "VLC media player", "VLC/"PACKAGE_VERSION
);
224 libvlc_add_intf (vlc
, "hotkeys,none");
226 libvlc_playlist_play (vlc
, -1, 0, NULL
);
227 if (libvlc_add_intf (vlc
, NULL
))
230 /* Qt4 insists on catching SIGCHLD via signal handler. To work around that,
231 * unblock it after all our child threads are created. */
232 sigdelset (&set
, SIGCHLD
);
233 pthread_sigmask (SIG_SETMASK
, &set
, NULL
);
235 /* Do not dequeue SIGHUP if it is ignored (nohup) */
236 if (signal_ignored (SIGHUP
))
237 sigdelset (&set
, SIGHUP
);
239 sigdelset (&set
, SIGPIPE
);
242 sigwait (&set
, &signum
);
244 /* Restore default signal behaviour after 3 seconds */
246 sigaddset (&set
, SIGINT
);
247 sigaddset (&set
, SIGALRM
);
248 signal (SIGINT
, SIG_IGN
);
249 signal (SIGALRM
, exit_timeout
);
250 pthread_sigmask (SIG_UNBLOCK
, &set
, NULL
);
256 libvlc_release (vlc
);