int main() { draw_init(); draw_set_canvas(canv, (void *)buff); if(w_connect(&priv_fd, &ifd, &ofd)) return -1; poll_fd = open("/dev/poll/0", 0); pipe_in = open("/dev/pipe/0", 0); pipe_out = open("/dev/pipe/0", 0); poll_set_event(poll_fd, pipe_out, POLL_TYPE_READ); if(fork() == 0) { dup2(pipe_in, 0); dup2(pipe_out, 1); dup2(pipe_out, 2); _exit(execl("/bin/sh", "*sh*", NULL)); } w_send_wcreate(ofd, 0, 0, 0, C*FW, (R+1)*FH, "WTerm"); w_wait_reply(ifd, &hwnd, &shm_key); shm_at(shm_key, buff, SHM_RW); printf("hwnd: %x\n",hwnd); poll_set_event(poll_fd, ifd, POLL_TYPE_READ); draw_title(); term_init(); main_loop(); w_disconnect(priv_fd, ifd, ofd); shm_dt(shm_key, buff); printf("exit with exit code 0\n"); return 0; }
int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) { int poll_fd = open("/dev/poll/0", 0); int timer_fd = -1; int fd; int i, tick, ret, n; n = 0; for(fd = 0; fd < nfds; fd++) { if(FD_ISSET(fd, rfds)) { n++; poll_set_event(poll_fd, fd, POLL_TYPE_READ); } if(FD_ISSET(fd, wfds)) { n++; poll_set_event(poll_fd, fd, POLL_TYPE_WRITE); } if(FD_ISSET(fd, efds)) { n++; poll_set_event(poll_fd, fd, POLL_TYPE_EXCEPT); } } if(timeout) { tick = timeout->tv_sec * HZ + timeout->tv_usec * HZ / 1000000; timer_fd = open("/dev/timer/0", 0); n++; poll_set_event(poll_fd, timer_fd, POLL_TYPE_READ); write(timer_fd, &tick, sizeof(tick)); } struct s_poll_event evs[n]; if(read(poll_fd, evs, n*sizeof(struct s_poll_event)) == -1) { errno = EINVAL; ret = -1; goto clean; } ret = n / sizeof(struct s_poll_event); FD_ZERO(rfds); FD_ZERO(wfds); FD_ZERO(efds); for(i = 0; i < n; i++) { fd = evs[i].fd; if(fd == timer_fd) { n--; continue; } if(evs[i].type & POLL_TYPE_READ) FD_SET(fd, rfds); if(evs[i].type & POLL_TYPE_WRITE) FD_SET(fd, wfds); if(evs[i].type & POLL_TYPE_EXCEPT) FD_SET(fd, efds); } clean: close(poll_fd); if(timer_fd != -1) close(timer_fd); return ret; }