9 #include <sys/select.h>
15 int parse_args(int argc, char **argv);
16 void print_usage(const char *argv0);
19 static const char *termfile = "/dev/tty";
20 static struct termios saved_term;
21 static struct timeval tv0;
23 int main(int argc, char **argv)
29 if(parse_args(argc, argv) == -1) {
37 gettimeofday(&tv0, 0);
39 tv.tv_sec = tick_interval / 1000;
40 tv.tv_usec = (tick_interval % 1000) * 1000;
47 while((res = select(1, &rdset, 0, 0, &tv)) == -1 && errno == EINTR);
49 if(res > 0 && FD_ISSET(0, &rdset)) {
50 while((c = fgetc(stdin)) >= 0) {
59 tv.tv_sec = next / 1000;
60 tv.tv_usec = (next % 1000) * 1000;
73 if((fd = open(termfile, O_RDWR | O_NONBLOCK)) == -1) {
74 fprintf(stderr, "failed to open terminal device: %s: %s\n", termfile, strerror(errno));
78 if(tcgetattr(fd, &term) == -1) {
79 fprintf(stderr, "failed to get terminal attributes: %s\n", strerror(errno));
83 term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
84 term.c_oflag &= ~OPOST;
85 term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
86 term.c_cflag = (term.c_cflag & ~(CSIZE | PARENB)) | CS8;
88 if(tcsetattr(fd, TCSAFLUSH, &term) == -1) {
89 fprintf(stderr, "failed to change terminal attributes: %s\n", strerror(errno));
100 open("ansitris.log", O_WRONLY | O_CREAT | O_TRUNC, 0664);
103 if(init_game() == -1) {
113 tcsetattr(0, TCSAFLUSH, &saved_term);
116 int parse_args(int argc, char **argv)
120 for(i=1; i<argc; i++) {
121 if(argv[i][0] == '-') {
122 if(argv[i][2] == 0) {
125 termfile = argv[++i];
133 print_usage(argv[0]);
137 fprintf(stderr, "invalid option: %s\n", argv[i]);
138 print_usage(argv[0]);
142 fprintf(stderr, "invalid option: %s\n", argv[i]);
143 print_usage(argv[0]);
147 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
148 print_usage(argv[0]);
155 void print_usage(const char *argv0)
157 printf("Usage: %s [options]\n", argv0);
158 printf("Options:\n");
159 printf(" -t <dev>: terminal device (default: /dev/tty)\n");
160 printf(" -b: use bell for sound ques (default: off)\n");
161 printf(" -h: print usage information and exit\n");
168 gettimeofday(&tv, 0);
170 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;