int handle_uevents(fd_set *rset) { struct client *citer; if(lsock == -1) { return -1; } if(FD_ISSET(lsock, rset)) { /* got an incoming connection */ int s; if((s = accept(lsock, 0, 0)) == -1) { perror("error while accepting connection on the UNIX socket"); } else { if(!add_client(CLIENT_UNIX, &s)) { perror("failed to add client"); } } } /* all the UNIX socket clients */ citer = first_client(); while(citer) { struct client *c = citer; citer = next_client(); if(get_client_type(c) == CLIENT_UNIX) { int s = get_client_socket(c); if(FD_ISSET(s, rset)) { int rdbytes; float sens; /* got a request from a client, decode and execute it */ /* XXX currently only sensitivity comes from clients */ while((rdbytes = read(s, &sens, sizeof sens)) <= 0 && errno == EINTR); if(rdbytes <= 0) { /* something went wrong... disconnect client */ close(get_client_socket(c)); remove_client(c); continue; } set_client_sensitivity(c, sens); } } } return 0; }
static void new_positions(t_server *server, t_client *clients) { char *str; clients = first_client(clients); while (clients) { if (clients->fd_type == FD_CLIENT) { asprintf(&str, "ppo #%d %d %d %d\n", clients->socket, \ clients->player->x, clients->player->y, \ clients->player->orientation); broadcast(server->clients, str, FD_GRAPHIC); free(str); } clients = clients->next; } }
static void dispatch_event(spnav_event *ev) { struct client *c, *citer; static struct timeval prev_motion_time; if(ev->type == EVENT_MOTION) { struct timeval tv; gettimeofday(&tv, 0); ev->motion.period = msec_dif(tv, prev_motion_time); prev_motion_time = tv; } citer = first_client(); while(citer) { c = citer; citer = next_client(); send_event(ev, c); } }
int main(int argc, char **argv) { int i, ret, become_daemon = 1; for(i=1; i<argc; i++) { if(argv[i][0] == '-' && argv[i][2] == 0) { switch(argv[i][1]) { case 'd': become_daemon = !become_daemon; break; case 'v': verbose = 1; break; case 'h': printf("usage: %s [options]\n", argv[0]); printf("options:\n"); printf(" -d\tdo not daemonize\n"); printf(" -v\tverbose output\n"); printf(" -h\tprint this usage information\n"); return 0; default: fprintf(stderr, "unrecognized argument: %s\n", argv[i]); return 1; } } else { fprintf(stderr, "unexpected argument: %s\n", argv[i]); return 1; } } if(become_daemon) { daemonize(); } write_pid_file(); puts("Spacenav daemon " VERSION); read_cfg("/etc/spnavrc", &cfg); if(init_clients() == -1) { return 1; } signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); signal(SIGSEGV, sig_handler); signal(SIGHUP, sig_handler); signal(SIGUSR1, sig_handler); signal(SIGUSR2, sig_handler); if(init_dev() == -1) { init_hotplug(); } init_unix(); #ifdef USE_X11 init_x11(); #endif atexit(cleanup); for(;;) { fd_set rset; int fd, max_fd = 0; struct client *c; FD_ZERO(&rset); /* set the device fd if it's open, otherwise set the hotplug fd */ if((fd = get_dev_fd()) != -1 || (fd = get_hotplug_fd()) != -1) { FD_SET(fd, &rset); if(fd > max_fd) max_fd = fd; } /* the UNIX domain socket listening for connections */ if((fd = get_unix_socket()) != -1) { FD_SET(fd, &rset); if(fd > max_fd) max_fd = fd; } /* all the UNIX socket clients */ c = first_client(); while(c) { if(get_client_type(c) == CLIENT_UNIX) { int s = get_client_socket(c); assert(s >= 0); FD_SET(s, &rset); if(s > max_fd) max_fd = s; } c = next_client(); } /* and the X server socket */ #ifdef USE_X11 if((fd = get_x11_socket()) != -1) { FD_SET(fd, &rset); if(fd > max_fd) max_fd = fd; } #endif do { ret = select(max_fd + 1, &rset, 0, 0, 0); } while(ret == -1 && errno == EINTR); if(ret > 0) { handle_events(&rset); } } return 0; /* unreachable */ }