#include "config.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
static int xpid;
Display *DPY;
Bool
startx(void)
{
int timeout = 8;
if (getenv("DISPLAY") != NULL)
return (False);
if (XF86Config_path == NULL) {
char *home, filename[PATH_MAX];
char commandline[PATH_MAX * 4];
int c_pos;
int len;
if (XFree86_path)
c_pos = XmuSnprintf(commandline, sizeof(commandline),
"%s/"__XSERVERNAME__" :8 -configure ",XFree86_path);
else
c_pos = XmuSnprintf(commandline, sizeof(commandline),
"%s/bin/"__XSERVERNAME__" :8 -configure ", XFree86Dir);
if (XF86Module_path && ((len = sizeof(commandline) - c_pos) > 0))
c_pos += XmuSnprintf(commandline + c_pos,len,
" -modulepath %s",XF86Module_path);
if (XF86Font_path && ((len = sizeof(commandline) - c_pos) > 0))
c_pos += XmuSnprintf(commandline + c_pos,len,
" -fontpath %s",XF86Font_path);
if (XF86RGB_path && ((len = sizeof(commandline) - c_pos) > 0))
c_pos += XmuSnprintf(commandline + c_pos,len,
" -rgbpath %s",XF86RGB_path);
if (system(commandline) != 0) {
fprintf(stderr, "Failed to run \"X -configure\".\n");
exit(1);
}
if ((home = getenv("HOME")) == NULL)
home = "/";
#ifndef QNX4
XmuSnprintf(filename, sizeof(filename), "%s/"__XCONFIGFILE__".new", home);
#else
XmuSnprintf(filename, sizeof(filename), "//%d%s/"__XCONFIGFILE__".new",
getnid(), home);
#endif
XF86Config_path = XtNewString(filename);
}
putenv("DISPLAY=:8");
switch (xpid = fork()) {
case 0: {
char path[PATH_MAX];
if (XFree86_path)
XmuSnprintf(path, sizeof(path), "%s/"__XSERVERNAME__, XFree86_path);
else
XmuSnprintf(path, sizeof(path), "%s/bin/"__XSERVERNAME__, XFree86Dir);
execl(path, "X", ":8", "+accessx","-allowMouseOpenFail",
"-xf86config", XF86Config_path, (void *)NULL);
exit(-127);
} break;
case -1:
fprintf(stderr, "Cannot fork.\n");
exit(1);
break;
default:
break;
}
while (timeout > 0) {
int status;
sleep(timeout -= 2);
if (waitpid(xpid, &status, WNOHANG | WUNTRACED) == xpid)
break;
else {
DPY = XOpenDisplay(NULL);
if (DPY != NULL)
break;
}
}
if (DPY == NULL) {
fprintf(stderr, "Cannot connect to X server.\n");
exit(1);
}
return (True);
}
void
endx(void)
{
if (xpid != 0)
kill(xpid, SIGTERM);
}