Implement command line options
[oggquiz.git] / ui.c
blobb41d31f33ab092bd37d29f1b80874bc8b51654a3
1 /*-
2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
6 * Tobias Rehbein
7 */
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <err.h>
13 #include "oggquiz.h"
14 #include "common.h"
15 #include "ui.h"
17 static void print_oggfile(oggfile_t * oggfile);
19 extern options_t options;
20 static int tty = -1;
22 void
23 ui_setup()
25 if (tty == -1)
26 if ((tty = open("/dev/tty", O_RDONLY)) == -1)
27 err(1, "could not open /dev/tty");
30 void
31 ui_display_quiz(ui_model_t * model)
33 int i;
35 puts("+-----------+-------------------+-------------------+");
36 printf("| Next turn | current turn: %3d | current player: %d |\n",
37 model->turn, model->current_player + 1);
38 puts("+-----------+-------------------+-------------------+\n");
40 for (i = 0; i < options.choices; i++) {
41 printf("%d. %s\n", i + 1, model->oggfiles[i].artist);
42 printf(" %s\n", model->oggfiles[i].album);
43 printf(" %s\n\n", model->oggfiles[i].title);
47 void
48 ui_display_result(ui_model_t * model)
50 int i;
51 char mark;
53 if (model->correct == model->guess)
54 printf("\nYou are right!\n");
55 else {
56 printf("\nSorry, you are wrong!\n");
57 printf("\nYour guess:\n");
58 print_oggfile(model->guess);
61 printf("\nYou are listening to:\n");
62 print_oggfile(model->correct);
63 puts("\nScoreboard:\n");
64 for (i = 0; i < options.players; i++) {
65 if (i == model->current_player)
66 mark = '*';
67 else
68 mark = ' ';
69 printf("%c Player %d: %5d %c\n", mark, i + 1, model->scores[i], mark);
71 puts("");
74 char
75 ui_get_answer()
77 int answer;
78 char canswer;
80 printf("What are you listening to? (1-%d, 'q' to quit)\n", options.choices);
81 for (;;) {
82 if (read(tty, &answer, 2) == -1)
83 err(1, "could not read from /dev/tty");
84 canswer = (char)answer;
85 if (canswer == 'q' || (canswer >= '1' && canswer <= '0' + options.choices))
86 break;
88 return canswer;
91 void
92 ui_pause()
94 char in;
96 printf("press return to continue...\n");
97 for (;;) {
98 if (read(tty, &in, 1) == -1)
99 err(1, "could not read from /dev/tty");
100 if (in == '\n')
101 break;
105 void
106 ui_teardown()
108 if (tty != -1) {
109 if (close(tty) == -1)
110 err(1, "could not close fd for /dev/tty");
111 tty = -1;
115 static void
116 print_oggfile(oggfile_t * oggfile)
118 printf(" %s\n", oggfile->artist);
119 printf(" %s\n", oggfile->album);
120 printf(" %s\n", oggfile->title);