First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / utils / xorgcfg / startx.c
blob7b730c9aaa6355f4df7a3cbd2f88cb6076828fee
1 /*
2 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * CONECTIVA LINUX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
22 * Except as contained in this notice, the name of Conectiva Linux shall
23 * not be used in advertising or otherwise to promote the sale, use or other
24 * dealings in this Software without prior written authorization from
25 * Conectiva Linux.
27 * Author: Paulo César Pereira de Andrade <pcpa@conectiva.com.br>
31 #include "config.h"
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <signal.h>
37 * Initialization
39 static int xpid;
40 Display *DPY;
43 * Implementation
45 Bool
46 startx(void)
48 int timeout = 8;
50 if (getenv("DISPLAY") != NULL)
51 /* already running Xserver */
52 return (False);
54 if (XF86Config_path == NULL) {
55 char *home, filename[PATH_MAX];
56 char commandline[PATH_MAX * 4];
57 int c_pos;
58 int len;
59 /*
60 * The name of the 4.0 binary is XFree86. X might also
61 * be the name of the 3.3 binary. Therefore don't change
62 * name to 'X'.
64 if (XFree86_path)
65 c_pos = XmuSnprintf(commandline, sizeof(commandline),
66 "%s/"__XSERVERNAME__" :8 -configure ",XFree86_path);
67 else
68 c_pos = XmuSnprintf(commandline, sizeof(commandline),
69 "%s/bin/"__XSERVERNAME__" :8 -configure ", XFree86Dir);
70 if (XF86Module_path && ((len = sizeof(commandline) - c_pos) > 0))
71 c_pos += XmuSnprintf(commandline + c_pos,len,
72 " -modulepath %s",XF86Module_path);
73 if (XF86Font_path && ((len = sizeof(commandline) - c_pos) > 0))
74 c_pos += XmuSnprintf(commandline + c_pos,len,
75 " -fontpath %s",XF86Font_path);
76 if (XF86RGB_path && ((len = sizeof(commandline) - c_pos) > 0))
77 c_pos += XmuSnprintf(commandline + c_pos,len,
78 " -rgbpath %s",XF86RGB_path);
80 if (system(commandline) != 0) {
81 fprintf(stderr, "Failed to run \"X -configure\".\n");
82 exit(1);
85 if ((home = getenv("HOME")) == NULL)
86 home = "/";
88 #ifndef QNX4
89 XmuSnprintf(filename, sizeof(filename), "%s/"__XCONFIGFILE__".new", home);
90 #else
91 XmuSnprintf(filename, sizeof(filename), "//%d%s/"__XCONFIGFILE__".new",
92 getnid(), home);
93 #endif
95 /* this memory is never released, even if the value of XF86Config_path is
96 * changed.
98 XF86Config_path = XtNewString(filename);
101 putenv("DISPLAY=:8");
103 switch (xpid = fork()) {
104 case 0: {
105 char path[PATH_MAX];
106 /* Don't change to X! see above */
107 if (XFree86_path)
108 XmuSnprintf(path, sizeof(path), "%s/"__XSERVERNAME__, XFree86_path);
109 else
110 XmuSnprintf(path, sizeof(path), "%s/bin/"__XSERVERNAME__, XFree86Dir);
111 execl(path, "X", ":8", /*"+xinerama",*/ "+accessx","-allowMouseOpenFail",
112 "-xf86config", XF86Config_path, (void *)NULL);
113 exit(-127);
114 } break;
115 case -1:
116 fprintf(stderr, "Cannot fork.\n");
117 exit(1);
118 break;
119 default:
120 break;
123 while (timeout > 0) {
124 int status;
126 sleep(timeout -= 2);
127 if (waitpid(xpid, &status, WNOHANG | WUNTRACED) == xpid)
128 break;
129 else {
130 DPY = XOpenDisplay(NULL);
131 if (DPY != NULL)
132 break;
136 if (DPY == NULL) {
137 fprintf(stderr, "Cannot connect to X server.\n");
138 exit(1);
141 return (True);
144 void
145 endx(void)
147 if (xpid != 0)
148 kill(xpid, SIGTERM);