initial commit
[profile/ivi/xorg-x11-server.git] / hw / xquartz / quartzStartup.c
1 /**************************************************************
2  *
3  * Startup code for the Quartz Darwin X Server
4  *
5  * Copyright (c) 2001-2004 Torrey T. Lyons. All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Except as contained in this notice, the name(s) of the above copyright
26  * holders shall not be used in advertising or otherwise to promote the sale,
27  * use or other dealings in this Software without prior written authorization.
28  */
29
30 #include "sanitizedCarbon.h"
31
32 #ifdef HAVE_DIX_CONFIG_H
33 #include <dix-config.h>
34 #endif
35
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <CoreFoundation/CoreFoundation.h>
39 #include "quartzCommon.h"
40 #include "X11Controller.h"
41 #include "darwin.h"
42 #include "darwinEvents.h"
43 #include "quartzAudio.h"
44 #include "quartz.h"
45 #include "opaque.h"
46 #include "micmap.h"
47
48 #include <assert.h>
49
50 #include <pthread.h>
51
52 int dix_main(int argc, char **argv, char **envp);
53
54 struct arg {
55     int argc;
56     char **argv;
57     char **envp;
58 };
59
60 static void server_thread (void *arg) {
61     struct arg args = *((struct arg *)arg);
62     free(arg);
63     exit (dix_main(args.argc, args.argv, args.envp));
64 }
65
66 static pthread_t create_thread (void *func, void *arg) {
67     pthread_attr_t attr;
68     pthread_t tid;
69         
70     pthread_attr_init (&attr);
71     pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
72     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
73     pthread_create (&tid, &attr, func, arg);
74     pthread_attr_destroy (&attr);
75         
76     return tid;
77 }
78
79 void QuartzInitServer(int argc, char **argv, char **envp) {
80     struct arg *args = (struct arg*)malloc(sizeof(struct arg));
81     if(!args)
82         FatalError("Could not allocate memory.\n");
83     
84     args->argc = argc;
85     args->argv = argv;
86     args->envp = envp;
87     
88     APPKIT_THREAD_ID = pthread_self();
89     SERVER_THREAD_ID = create_thread(server_thread, args);
90
91     if (!SERVER_THREAD_ID) {
92         FatalError("can't create secondary thread\n");
93     }
94 }
95
96 int server_main(int argc, char **argv, char **envp) {
97     int         i;
98     int         fd[2];
99
100     /* Unset CFProcessPath, so our children don't inherit this kludge we need
101      * to load our nib.  If an xterm gets this set, then it fails to
102      * 'open hi.txt' properly.
103      */
104     unsetenv("CFProcessPath");
105     
106     // Make a pipe to pass events
107     assert( pipe(fd) == 0 );
108     darwinEventReadFD = fd[0];
109     darwinEventWriteFD = fd[1];
110     fcntl(darwinEventReadFD, F_SETFL, O_NONBLOCK);
111
112     for (i = 1; i < argc; i++) {
113         // Display version info without starting Mac OS X UI if requested
114         if (!strcmp( argv[i], "-showconfig" ) || !strcmp( argv[i], "-version" )) {
115             DarwinPrintBanner();
116             exit(0);
117         }
118     }
119
120     /* Create the audio mutex */
121     QuartzAudioInit();
122
123     X11ControllerMain(argc, argv, envp);
124     exit(0);
125 }