/** * Run the gtk event loop. * * The same as the standard gtk_main loop except this ensures active * FD are added to the gtk poll event set. */ static void nsgtk_main(void) { fd_set read_fd_set, write_fd_set, exc_fd_set; int max_fd; GPollFD *fd_list[1000]; unsigned int fd_count; while (!nsgtk_complete) { max_fd = -1; fd_count = 0; FD_ZERO(&read_fd_set); FD_ZERO(&write_fd_set); FD_ZERO(&exc_fd_set); fetch_fdset(&read_fd_set, &write_fd_set, &exc_fd_set, &max_fd); for (int i = 0; i <= max_fd; i++) { if (FD_ISSET(i, &read_fd_set)) { GPollFD *fd = malloc(sizeof *fd); fd->fd = i; fd->events = G_IO_IN | G_IO_HUP | G_IO_ERR; g_main_context_add_poll(0, fd, 0); fd_list[fd_count++] = fd; } if (FD_ISSET(i, &write_fd_set)) { GPollFD *fd = malloc(sizeof *fd); fd->fd = i; fd->events = G_IO_OUT | G_IO_ERR; g_main_context_add_poll(0, fd, 0); fd_list[fd_count++] = fd; } if (FD_ISSET(i, &exc_fd_set)) { GPollFD *fd = malloc(sizeof *fd); fd->fd = i; fd->events = G_IO_ERR; g_main_context_add_poll(0, fd, 0); fd_list[fd_count++] = fd; } } schedule_run(); gtk_main_iteration(); for (unsigned int i = 0; i != fd_count; i++) { g_main_context_remove_poll(0, fd_list[i]); free(fd_list[i]); } } }
void nsbeos_gui_poll(void) { fd_set read_fd_set, write_fd_set, exc_fd_set; int max_fd; struct timeval timeout; unsigned int fd_count = 0; bigtime_t next_schedule = 0; /* get any active fetcher fd */ fetch_fdset(&read_fd_set, &write_fd_set, &exc_fd_set, &max_fd); /* run the scheduler */ schedule_run(); // our own event pipe FD_SET(sEventPipe[0], &read_fd_set); // max of all the fds in the set, plus one for select() max_fd = MAX(max_fd, sEventPipe[0]) + 1; // compute schedule timeout if (earliest_callback_timeout != B_INFINITE_TIMEOUT) { next_schedule = earliest_callback_timeout - system_time(); } else { next_schedule = earliest_callback_timeout; } // we're quite late already... if (next_schedule < 0) next_schedule = 0; timeout.tv_sec = (long)(next_schedule / 1000000LL); timeout.tv_usec = (long)(next_schedule % 1000000LL); //LOG("gui_poll: select(%d, ..., %Ldus", max_fd, next_schedule); fd_count = select(max_fd, &read_fd_set, &write_fd_set, &exc_fd_set, &timeout); //LOG("select: %d\n", fd_count); if (fd_count > 0 && FD_ISSET(sEventPipe[0], &read_fd_set)) { BMessage *message; int len = read(sEventPipe[0], &message, sizeof(void *)); //LOG("gui_poll: BMessage ? %d read", len); if (len == sizeof(void *)) { //LOG("gui_poll: BMessage.what %-4.4s\n", &(message->what)); nsbeos_dispatch_event(message); } } }