XQuartz: Reduce code duplication in X11.app
(cherry picked from commit b81809cd91a9f90b7f2de77b1dcf514cee87c32d)
This commit is contained in:
parent
f3042a63be
commit
2c24231fc2
|
@ -39,8 +39,8 @@
|
||||||
#define DEFAULT_CLIENT "/usr/X11/bin/xterm"
|
#define DEFAULT_CLIENT "/usr/X11/bin/xterm"
|
||||||
#define DEFAULT_STARTX "/usr/X11/bin/startx"
|
#define DEFAULT_STARTX "/usr/X11/bin/startx"
|
||||||
|
|
||||||
static int launcher_main(int argc, char **argv);
|
static int execute(const char *command);
|
||||||
static int server_main(int argc, char **argv);
|
static char *command_from_prefs(const char *key, const char *default_value);
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
Display *display;
|
Display *display;
|
||||||
|
@ -50,7 +50,7 @@ int main(int argc, char **argv) {
|
||||||
for(i=0; i < argc; i++) {
|
for(i=0; i < argc; i++) {
|
||||||
fprintf(stderr, "\targv[%u] = %s\n", (unsigned)i, argv[i]);
|
fprintf(stderr, "\targv[%u] = %s\n", (unsigned)i, argv[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we have a process serial number and it's our only arg, act as if
|
/* If we have a process serial number and it's our only arg, act as if
|
||||||
* the user double clicked the app bundle: launch app_to_run if possible
|
* the user double clicked the app bundle: launch app_to_run if possible
|
||||||
*/
|
*/
|
||||||
|
@ -61,32 +61,32 @@ int main(int argc, char **argv) {
|
||||||
fprintf(stderr, "X11.app: Closing the display and sleeping for 2s to allow the X server to start up.\n");
|
fprintf(stderr, "X11.app: Closing the display and sleeping for 2s to allow the X server to start up.\n");
|
||||||
/* Could open the display, start the launcher */
|
/* Could open the display, start the launcher */
|
||||||
XCloseDisplay(display);
|
XCloseDisplay(display);
|
||||||
|
|
||||||
/* Give 2 seconds for the server to start...
|
/* Give 2 seconds for the server to start...
|
||||||
* TODO: *Really* fix this race condition
|
* TODO: *Really* fix this race condition
|
||||||
*/
|
*/
|
||||||
usleep(2000);
|
usleep(2000);
|
||||||
return launcher_main(argc, argv);
|
return execute(command_from_prefs("app_to_run", DEFAULT_CLIENT));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start the server */
|
/* Start the server */
|
||||||
fprintf(stderr, "X11.app: Could not connect to server. Starting X server.");
|
fprintf(stderr, "X11.app: Could not connect to server. Starting X server.");
|
||||||
return server_main(argc, argv);
|
return execute(command_from_prefs("startx_script", DEFAULT_STARTX));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int myexecvp(const char *command) {
|
static int execute(const char *command) {
|
||||||
const char *newargv[7];
|
const char *newargv[7];
|
||||||
const char **s;
|
const char **s;
|
||||||
|
|
||||||
newargv[0] = "/usr/bin/login";
|
newargv[0] = "/usr/bin/login";
|
||||||
newargv[1] = "-fp";
|
newargv[1] = "-fp";
|
||||||
newargv[2] = getlogin();
|
newargv[2] = getlogin();
|
||||||
newargv[3] = "/bin/sh";
|
newargv[3] = "/bin/sh";
|
||||||
newargv[4] = "-c";
|
newargv[4] = "-c";
|
||||||
newargv[5] = command;
|
newargv[5] = command;
|
||||||
newargv[6] = NULL;
|
newargv[6] = NULL;
|
||||||
|
|
||||||
fprintf(stderr, "X11.app: Launching %s:\n", command);
|
fprintf(stderr, "X11.app: Launching %s:\n", command);
|
||||||
for(s=newargv; *s; s++) {
|
for(s=newargv; *s; s++) {
|
||||||
fprintf(stderr, "\targv[%d] = %s\n", s - newargv, *s);
|
fprintf(stderr, "\targv[%d] = %s\n", s - newargv, *s);
|
||||||
|
@ -97,42 +97,33 @@ static int myexecvp(const char *command) {
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int launcher_main (int argc, char **argv) {
|
static char *command_from_prefs(const char *key, const char *default_value) {
|
||||||
char *command = DEFAULT_CLIENT;
|
char *command = NULL;
|
||||||
|
|
||||||
CFPropertyListRef PlistRef = CFPreferencesCopyAppValue(CFSTR("app_to_run"), kCFPreferencesCurrentApplication);
|
CFStringRef cfKey = CFStringCreateWithPascalString(NULL, key, kCFStringEncodingASCII);
|
||||||
|
CFPropertyListRef PlistRef = CFPreferencesCopyAppValue(cfKey, kCFPreferencesCurrentApplication);
|
||||||
if ((PlistRef == NULL) || (CFGetTypeID(PlistRef) != CFStringGetTypeID())) {
|
|
||||||
CFPreferencesSetAppValue(CFSTR("app_to_run"), CFSTR(DEFAULT_CLIENT), kCFPreferencesCurrentApplication);
|
if ((PlistRef == NULL) || (CFGetTypeID(PlistRef) != CFStringGetTypeID())) {
|
||||||
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
|
CFStringRef cfDefaultValue = CFStringCreateWithPascalString(NULL, default_value, kCFStringEncodingASCII);
|
||||||
} else {
|
|
||||||
int len = CFStringGetLength((CFStringRef)PlistRef)+1;
|
|
||||||
command = (char *)malloc(len);
|
|
||||||
CFStringGetCString((CFStringRef)PlistRef, command, len, kCFStringEncodingASCII);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (PlistRef)
|
CFPreferencesSetAppValue(cfKey, cfDefaultValue, kCFPreferencesCurrentApplication);
|
||||||
CFRelease(PlistRef);
|
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
|
||||||
|
|
||||||
return myexecvp(command);
|
int len = strlen(default_value) + 1;
|
||||||
}
|
command = (char *)malloc(len * sizeof(char));
|
||||||
|
if(!command)
|
||||||
int server_main (int argc, char **argv) {
|
return NULL;
|
||||||
char *command = DEFAULT_STARTX;
|
strcpy(command, default_value);
|
||||||
|
} else {
|
||||||
CFPropertyListRef PlistRef = CFPreferencesCopyAppValue(CFSTR("startx_script"), kCFPreferencesCurrentApplication);
|
int len = CFStringGetLength((CFStringRef)PlistRef) + 1;
|
||||||
|
command = (char *)malloc(len * sizeof(char));
|
||||||
if ((PlistRef == NULL) || (CFGetTypeID(PlistRef) != CFStringGetTypeID())) {
|
if(!command)
|
||||||
CFPreferencesSetAppValue(CFSTR("startx_script"), CFSTR(DEFAULT_STARTX), kCFPreferencesCurrentApplication);
|
return NULL;
|
||||||
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
|
CFStringGetCString((CFStringRef)PlistRef, command, len, kCFStringEncodingASCII);
|
||||||
} else {
|
|
||||||
int len = CFStringGetLength((CFStringRef)PlistRef)+1;
|
|
||||||
command = (char *)malloc(len);
|
|
||||||
CFStringGetCString((CFStringRef)PlistRef, command, len, kCFStringEncodingASCII);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PlistRef)
|
if (PlistRef)
|
||||||
CFRelease(PlistRef);
|
CFRelease(PlistRef);
|
||||||
|
|
||||||
return myexecvp(command);
|
return command;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue