From 73f41f38c51ebf3f54a209f8ee1b32fddb713fde Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Date: Mon, 7 Jul 2008 10:55:58 -0700 Subject: [PATCH] XQuartz: Some fd handoff cleanup. (cherry picked from commit 9c20a4804d97e67a988f00f49866997209cce518) --- hw/xquartz/X11Application.m | 6 ++++ hw/xquartz/darwinEvents.c | 49 +++++++++++++++++++-------- hw/xquartz/darwinEvents.h | 3 ++ hw/xquartz/mach-startup/bundle-main.c | 20 +++++++++-- hw/xquartz/mach-startup/stub.c | 33 +++++++++++++----- 5 files changed, 86 insertions(+), 25 deletions(-) diff --git a/hw/xquartz/X11Application.m b/hw/xquartz/X11Application.m index c6c9c59ab..1f90b240c 100644 --- a/hw/xquartz/X11Application.m +++ b/hw/xquartz/X11Application.m @@ -202,6 +202,12 @@ static void message_kit_thread (SEL selector, NSObject *arg) { for_appkit = YES; for_x = YES; +// fprintf(stderr, "fd_add_count: %d\n", fd_add_count); + if(fd_add_count) { + DarwinProcessFDAdditionQueue(); + fprintf(stderr, "ran it - fd_add_count: %d\n", fd_add_count); + } + switch (type) { case NSLeftMouseDown: case NSRightMouseDown: case NSOtherMouseDown: case NSLeftMouseUp: case NSRightMouseUp: case NSOtherMouseUp: diff --git a/hw/xquartz/darwinEvents.c b/hw/xquartz/darwinEvents.c index 911aac7de..900ee4387 100644 --- a/hw/xquartz/darwinEvents.c +++ b/hw/xquartz/darwinEvents.c @@ -79,6 +79,11 @@ void QuartzModeEQInit(void); static int old_flags = 0; // last known modifier state +#define FD_ADD_MAX 128 +static int fd_add[FD_ADD_MAX]; +int fd_add_count = 0; +static pthread_mutex_t fd_add_lock = PTHREAD_MUTEX_INITIALIZER; + static xEvent *darwinEvents = NULL; static pthread_mutex_t mieq_lock = PTHREAD_MUTEX_INITIALIZER; @@ -232,15 +237,6 @@ static void DarwinSimulateMouseClick( DarwinUpdateModifiers(KeyPress, modifierMask); } -static void kXquartzListenOnOpenFDHandler(int screenNum, xEventPtr xe, DeviceIntPtr dev, int nevents) { - size_t i; - TA_SERVER(); - - for (i=0; iretval = EXIT_FAILURE; pthread_cond_broadcast(&data->cond); @@ -213,7 +215,7 @@ static void socket_handoff_thread(void *arg) { pthread_mutex_unlock(&data->lock); if(connect(handoff_fd, servaddr, servaddr_len) < 0) { - fprintf(stderr, "Failed to connect to socket: %s - %s\n", filename, strerror(errno)); + fprintf(stderr, "X11.app: Failed to connect to socket: %s - %d - %s\n", filename, errno, strerror(errno)); return; } @@ -226,6 +228,10 @@ static void socket_handoff_thread(void *arg) { kern_return_t do_prep_fd_handoff(mach_port_t port, string_t socket_filename) { handoff_data_t handoff_data; +#ifdef DEBUG + fprintf(stderr, "X11.app: Prepping for fd handoff.\n"); +#endif + /* Initialize our data */ pthread_mutex_init(&handoff_data.lock, NULL); pthread_cond_init(&handoff_data.cond, NULL); @@ -235,6 +241,10 @@ kern_return_t do_prep_fd_handoff(mach_port_t port, string_t socket_filename) { create_thread(socket_handoff_thread, &handoff_data); +#ifdef DEBUG + fprintf(stderr, "X11.app: Thread created for handoff. Waiting on return value.\n"); +#endif + /* Wait for our return value */ pthread_cond_wait(&handoff_data.cond, &handoff_data.lock); pthread_mutex_unlock(&handoff_data.lock); @@ -242,6 +252,10 @@ kern_return_t do_prep_fd_handoff(mach_port_t port, string_t socket_filename) { /* Cleanup */ pthread_cond_destroy(&handoff_data.cond); pthread_mutex_destroy(&handoff_data.lock); + +#ifdef DEBUG + fprintf(stderr, "X11.app: Sending return value: %d\n", handoff_data.retval); +#endif return handoff_data.retval; } diff --git a/hw/xquartz/mach-startup/stub.c b/hw/xquartz/mach-startup/stub.c index d7b248b15..9928aa9ae 100644 --- a/hw/xquartz/mach-startup/stub.c +++ b/hw/xquartz/mach-startup/stub.c @@ -57,6 +57,8 @@ #define XSERVER_VERSION "?" #endif +#define DEBUG 1 + static char x11_path[PATH_MAX + 1]; static void set_x11_path() { @@ -132,23 +134,27 @@ static int create_socket(char *filename_out) { servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(filename_out); ret_fd = socket(PF_UNIX, SOCK_STREAM, 0); - if(ret_fd == 0) { - fprintf(stderr, "Failed to create socket (try %d / %d): %s - %s\n", (int)try+1, (int)try_max, filename_out, strerror(errno)); + if(ret_fd == -1) { + fprintf(stderr, "Xquartz: Failed to create socket (try %d / %d): %s - %s\n", (int)try+1, (int)try_max, filename_out, strerror(errno)); continue; } if(bind(ret_fd, servaddr, servaddr_len) != 0) { - fprintf(stderr, "Failed to bind socket: %s - %s\n", filename_out, strerror(errno)); + fprintf(stderr, "Xquartz: Failed to bind socket: %d - %s\n", errno, strerror(errno)); close(ret_fd); return 0; } if(listen(ret_fd, 10) != 0) { - fprintf(stderr, "Failed to listen to socket: %s - %s\n", filename_out, strerror(errno)); + fprintf(stderr, "Xquartz: Failed to listen to socket: %s - %d - %s\n", filename_out, errno, strerror(errno)); close(ret_fd); return 0; } - + +#ifdef DEBUG + fprintf(stderr, "Xquartz: Listening on socket for fd handoff: %s\n", filename_out); +#endif + return ret_fd; } @@ -186,19 +192,30 @@ static void send_fd_handoff(int handoff_fd, int launchd_fd) { *((int*)CMSG_DATA(cmsg)) = launchd_fd; +#ifdef DEBUG + fprintf(stderr, "Xquartz: Waiting for fd handoff connection.\n"); +#endif connected_fd = accept(handoff_fd, NULL, NULL); if(connected_fd == -1) { - fprintf(stderr, "Failed to accept incoming connection on socket: %s\n", strerror(errno)); + fprintf(stderr, "Xquartz: Failed to accept incoming connection on socket: %s\n", strerror(errno)); return; } +#ifdef DEBUG + fprintf(stderr, "Xquartz: Handoff connection established. Sending message.\n"); +#endif if(sendmsg(connected_fd, &msg, 0) < 0) { - fprintf(stderr, "Error sending $DISPLAY file descriptor: %s\n", strerror(errno)); + fprintf(stderr, "Xquartz: Error sending $DISPLAY file descriptor: %s\n", strerror(errno)); return; } +#ifdef DEBUG + fprintf(stderr, "Xquartz: Message sent. Closing.\n"); +#endif close(connected_fd); - fprintf(stderr, "send %d %d %d %s\n", handoff_fd, launchd_fd, errno, strerror(errno)); +#ifdef DEBUG + fprintf(stderr, "Xquartz: end of send debug: %d %d %d %s\n", handoff_fd, launchd_fd, errno, strerror(errno)); +#endif } int main(int argc, char **argv, char **envp) {