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