First import
[xorg_rtime.git] / xorg-server-1.4 / hw / darwin / quartz / XDarwinStartup.c
blob8041e32d5484bbfc88eb9d0101437a35d863abb0
1 /**************************************************************
3 * Startup program for Darwin X servers
5 * This program selects the appropriate X server to launch:
6 * XDarwin IOKit X server (default)
7 * XDarwinQuartz A soft link to the Quartz X server
8 * executable (-quartz etc. option)
10 * If told to idle, the program will simply pause and not
11 * launch any X server. This is to support startx being
12 * run by XDarwin.app.
14 **************************************************************/
16 * Copyright (c) 2001-2002 Torrey T. Lyons. All Rights Reserved.
18 * Permission is hereby granted, free of charge, to any person obtaining a
19 * copy of this software and associated documentation files (the "Software"),
20 * to deal in the Software without restriction, including without limitation
21 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 * and/or sell copies of the Software, and to permit persons to whom the
23 * Software is furnished to do so, subject to the following conditions:
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * TORREY T. LYONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
33 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE.
36 * Except as contained in this notice, the name of Torrey T. Lyons shall not
37 * be used in advertising or otherwise to promote the sale, use or other
38 * dealings in this Software without prior written authorization from
39 * Torrey T. Lyons.
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <sys/syslimits.h>
47 #include <ApplicationServices/ApplicationServices.h>
49 // Macros to build the path name
50 #ifndef XBINDIR
51 #define XBINDIR /usr/X11/bin
52 #endif
53 #define STR(s) #s
54 #define XSTRPATH(s) STR(s) "/"
55 #define XPATH(file) XSTRPATH(XBINDIR) STR(file)
57 int main(
58 int argc,
59 char *argv[] )
61 int i, j, quartzMode = -1;
62 char **newargv;
64 // Check if we are going to run in Quartz mode or idle
65 // to support startx from the Quartz server. The last
66 // parameter in the list is the one used.
67 for (i = argc-1; i; i--) {
68 if (!strcmp(argv[i], "-idle")) {
69 pause();
70 return 0;
72 } else if (!strcmp(argv[i], "-quartz") ||
73 !strcmp(argv[i], "-rootless") ||
74 !strcmp(argv[i], "-fullscreen"))
76 quartzMode = 1;
77 break;
79 } else if (!strcmp(argv[i], "-iokit")) {
80 quartzMode = 0;
81 break;
85 if (quartzMode == -1) {
86 #ifdef HAS_CG_MACH_PORT
87 // Check if the CoreGraphics window server is running.
88 // Mike Paquette says this is the fastest way to determine if it is running.
89 CFMachPortRef cgMachPortRef = CGWindowServerCFMachPort();
90 if (cgMachPortRef == NULL)
91 quartzMode = 0;
92 else
93 quartzMode = 1;
94 #else
95 // On older systems we assume IOKit mode.
96 quartzMode = 0;
97 #endif
100 if (quartzMode) {
101 // Launch the X server for the quartz modes
103 char quartzPath[PATH_MAX+1];
104 int pathLength;
105 OSStatus theStatus;
106 CFURLRef appURL;
107 CFStringRef appPath;
108 Boolean success;
110 // Build the new argument list
111 newargv = (char **) malloc((argc+2) * sizeof(char *));
112 for (j = argc; j; j--)
113 newargv[j] = argv[j];
114 newargv[argc] = "-nostartx";
115 newargv[argc+1] = NULL;
117 // Use the XDarwinQuartz soft link if it is valid
118 pathLength = readlink(XPATH(XDarwinQuartz), quartzPath, PATH_MAX);
119 if (pathLength != -1) {
120 quartzPath[pathLength] = '\0';
121 newargv[0] = quartzPath;
122 execv(newargv[0], newargv);
125 // Otherwise query LaunchServices for the location of the XDarwin application
126 theStatus = LSFindApplicationForInfo(kLSUnknownCreator,
127 CFSTR("org.x.x11"),
128 NULL, NULL, &appURL);
129 if (theStatus) {
130 fprintf(stderr, "Could not find the XDarwin application. (Error = 0x%lx)\n", theStatus);
131 fprintf(stderr, "Launch XDarwin once from the Finder.\n");
132 return theStatus;
135 appPath = CFURLCopyFileSystemPath (appURL, kCFURLPOSIXPathStyle);
136 success = CFStringGetCString(appPath, quartzPath, PATH_MAX, CFStringGetSystemEncoding());
137 if (! success) {
138 fprintf(stderr, "Could not find path to XDarwin application.\n");
139 return success;
142 // Launch the XDarwin application
143 strncat(quartzPath, "/Contents/MacOS/XDarwin", PATH_MAX);
144 newargv[0] = quartzPath;
145 execv(newargv[0], newargv);
146 fprintf(stderr, "Could not start XDarwin application at %s.\n", newargv[0]);
147 return errno;
149 } else {
151 // Build the new argument list
152 newargv = (char **) malloc((argc+1) * sizeof(char *));
153 for (j = argc; j; j--)
154 newargv[j] = argv[j];
155 newargv[0] = "XDarwin";
156 newargv[argc] = NULL;
158 // Launch the IOKit X server
159 execvp(newargv[0], newargv);
160 fprintf(stderr, "Could not start XDarwin IOKit X server.\n");
161 return errno;