Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / piano / piano.c
1 /*
2  * piano.c - a piano emulator
3  */
4 static const char rcsid[] =
5   "$FreeBSD: src/games/piano/piano.c,v 1.7 1999/12/12 03:22:37 billf Exp $";
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <curses.h>
10
11 #include <unistd.h>
12 #include <sys/file.h>
13
14 char *myname;
15 int verbose;
16 static char *initcmd = "t160 o1 l16 ml";
17
18 static char usage_msg[] =
19         "simple keyboard player V0.8086\n"
20         "usage: %s [-v][-i str]\n"
21         "\t-i str defaults 't160 o1 l16 ml'\n"
22         "function: play by console keyboard\n"
23         "\tESC to exit. Note keys are ...\n"
24         "\t1 2   4 5   7 8 9   - = \\\n"
25         "\t Q W E R T Y U I O P [ ]\n"
26         ;
27
28 struct kdef_t {
29         int ch;
30         char *str;
31 };
32
33 static char *kstr[256];
34
35 static struct kdef_t kdef[] = {
36         /* white key */
37         { '\t', "<g>" },
38         { 'q', "<a>" },
39         { 'w', "<b>" },
40         { 'e', "c" },
41         { 'r', "d" },
42         { 't', "e" },
43         { 'y', "f" },
44         { 'u', "g" },
45         { 'i', "a" },
46         { 'o', "b" },
47         { 'p', ">c<" },
48         { '[', ">d<" },
49         { ']', ">e<" },
50         { '\n', ">f<" },
51         { '\r', ">f<" },
52         /* black key */
53         { '`', "<f#>" },
54         { '1', "<g#>" },
55         { '2', "<a#>" },
56         /*{ '3', "<b#>" },*/
57         { '4', "c#" },
58         { '5', "d#" },
59         /*{ '6', "e#" },*/
60         { '7', "f#" },
61         { '8', "g#" },
62         { '9', "a#" },
63         /*{ '0', "b#" },*/
64         { '-', ">c#<" },
65         { '=', ">d#<" },
66         /*{ '\', ">e#<" },*/
67         { '\177', ">f#<" },
68         { '\0', NULL }
69 };
70
71 static int
72 init_kstr(void)
73 {
74         struct kdef_t *mv = kdef;
75         while (mv->str != NULL) {
76                 kstr[mv->ch] = mv->str;
77                 mv++;
78         }/* while */
79         return 0;
80 }/* init_kstr */
81
82 static int
83 fdputs(const char *s, int fd, int echo)
84 {
85         int err, len = strlen(s);
86         write(fd, s, len);
87         err = write(fd, "\n", 1);
88         if (echo) {
89                 fputs(s, stdout);
90         }
91         return err;
92 }/* fdputs */
93
94 static int
95 outspkr(const char *s)
96 {
97         int err = -1, fd = open("/dev/speaker", O_WRONLY);
98         if (fd >= 0) {
99                 fdputs(initcmd, fd, 0);
100                 err = fdputs(s, fd, verbose);
101                 close(fd);
102         }
103         return err;
104 }/* outspkr */
105
106 static int
107 nain(void)
108 {
109         int ch;
110         initscr();
111         noecho();
112         nonl();
113         raw();
114         init_kstr();
115         while ((ch = getch()) != '\033') {
116                 if (kstr[ch] != NULL) {
117                         outspkr(kstr[ch]);
118                 }
119                 else {
120                         if (verbose) {
121                                 switch (ch) {
122                                 case ' ':
123                                         fputs(" ", stdout);
124                                         break;
125                                 case '\b':
126                                         fputs("\b", stdout);
127                                         break;
128                                 }/* switch */
129                         }
130                 }
131         }/* while */
132         endwin();
133         return 0;
134 }/* nain */
135
136 int
137 main(int argc, char *argv[])
138 {
139         int ch, ex, show_usage = 0;
140         myname = argv[0];
141         while ((ch = getopt(argc, argv, "-vi:")) != -1) {
142                 switch (ch) {
143                 default:
144                 case 'V':
145                         show_usage++;
146                         break;
147                 case 'v':
148                         verbose++;
149                         break;
150                 case 'i':
151                         initcmd = optarg;
152                         break;
153                 }/* switch */
154         }/* while */
155         ex = 1;
156         if (show_usage) {
157                 fprintf(stderr, usage_msg, myname);
158         }
159         else {
160                 printf("Type ESC to exit.\n");
161                 ex = 0;
162                 nain();
163         }
164         return ex;
165 }/* main */