Change the gmp download URL to https://gmplib.org/download
[vlc/gmpfix.git] / bin / darwinvlc.c
blob0a35dc58914f17960d2f4db113921aade7276fd6
1 /*****************************************************************************
2 * darwinvlc.c: the darwin-specific VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2013 the VideoLAN team
5 * $Id$
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 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc/vlc.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <locale.h>
38 #include <signal.h>
39 #ifdef HAVE_PTHREAD_H
40 # include <pthread.h>
41 #endif
42 #include <unistd.h>
43 #include <TargetConditionals.h>
44 #import <CoreFoundation/CoreFoundation.h>
46 extern void vlc_enable_override (void);
48 static bool signal_ignored (int signum)
50 struct sigaction sa;
52 if (sigaction (signum, NULL, &sa))
53 return false;
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)
60 pthread_t *ps = data;
61 pthread_kill (*ps, SIGTERM);
64 static void exit_timeout (int signum)
66 (void) 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);
83 #ifndef NDEBUG
84 /* Activate malloc checking routines to detect heap corruptions. */
85 setenv ("MALLOC_CHECK_", "2", 1);
86 #endif
88 #ifdef TOP_BUILDDIR
89 setenv ("VLC_PLUGIN_PATH", TOP_BUILDDIR"/modules", 1);
90 setenv ("VLC_DATA_PATH", TOP_SRCDIR"/share", 1);
91 #endif
93 #ifndef ALLOW_RUN_AS_ROOT
94 if (geteuid () == 0)
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]);
100 return 1;
102 #endif
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());
112 sigset_t set;
114 sigemptyset (&set);
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 + 3];
154 int argc = 0;
156 argv[argc++] = "--no-ignore-config";
157 argv[argc++] = "--media-library";
158 argv[argc++] = "--stats";
160 /* overwrite system language on Mac */
161 #if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR // TARGET_OS_MAC is unspecific
162 char *lang = NULL;
164 for (int i = 0; i < i_argc; i++) {
165 if (!strncmp(ppsz_argv[i], "--language", 10)) {
166 lang = strstr(ppsz_argv[i], "=");
167 ppsz_argv++, i_argc--;
168 continue;
171 if (lang && strncmp( lang, "auto", 4 )) {
172 char tmp[11];
173 snprintf(tmp, 11, "LANG%s", lang);
174 putenv(tmp);
177 if (!lang) {
178 CFStringRef language;
179 language = (CFStringRef)CFPreferencesCopyAppValue(CFSTR("language"),
180 kCFPreferencesCurrentApplication);
181 if (language) {
182 CFIndex length = CFStringGetLength(language) + 1;
183 if (length > 0) {
184 CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
185 lang = (char *)malloc(maxSize);
186 CFStringGetCString(language, lang, maxSize - 1, kCFStringEncodingUTF8);
188 if (strncmp( lang, "auto", 4 )) {
189 char tmp[11];
190 snprintf(tmp, 11, "LANG=%s", lang);
191 putenv(tmp);
193 CFRelease(language);
196 #endif
198 ppsz_argv++; i_argc--; /* skip executable path */
200 /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
201 * is the PSN - process serial number (a unique PID-ish thingie)
202 * still ok for real Darwin & when run from command line
203 * for example -psn_0_9306113 */
204 if (i_argc >= 1 && !strncmp (*ppsz_argv, "-psn" , 4))
205 ppsz_argv++, i_argc--;
207 memcpy (argv + argc, ppsz_argv, i_argc * sizeof (*argv));
208 argc += i_argc;
209 argv[argc] = NULL;
211 vlc_enable_override ();
213 pthread_t self = pthread_self ();
215 /* Initialize libvlc */
216 libvlc_instance_t *vlc = libvlc_new (argc, argv);
217 if (vlc == NULL)
218 return 1;
220 int ret = 1;
221 libvlc_set_exit_handler (vlc, vlc_kill, &self);
222 libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION, PACKAGE_NAME);
223 libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
225 libvlc_playlist_play (vlc, -1, 0, NULL);
227 libvlc_add_intf (vlc, "hotkeys,none");
229 if (libvlc_add_intf (vlc, NULL))
230 goto out;
232 /* Qt4 insists on catching SIGCHLD via signal handler. To work around that,
233 * unblock it after all our child threads are created. */
234 sigdelset (&set, SIGCHLD);
235 pthread_sigmask (SIG_SETMASK, &set, NULL);
237 /* Do not dequeue SIGHUP if it is ignored (nohup) */
238 if (signal_ignored (SIGHUP))
239 sigdelset (&set, SIGHUP);
240 /* Ignore SIGPIPE */
241 sigdelset (&set, SIGPIPE);
243 int signum;
244 sigwait (&set, &signum);
246 /* Restore default signal behaviour after 3 seconds */
247 sigemptyset (&set);
248 sigaddset (&set, SIGINT);
249 sigaddset (&set, SIGALRM);
250 signal (SIGINT, SIG_IGN);
251 signal (SIGALRM, exit_timeout);
252 pthread_sigmask (SIG_UNBLOCK, &set, NULL);
253 alarm (3);
255 ret = 0;
256 /* Cleanup */
257 out:
258 libvlc_release (vlc);
260 return ret;