aboutsummaryrefslogtreecommitdiff
path: root/games/piano/piano.c
blob: 8e16b3ade6d20741a90c6243940e95d0851c98c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * piano.c - a piano emulator
 */
static char rcsid[] = "$Id: piano.c,v 1.4 1997/02/22 14:46:57 peter Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>

#include <unistd.h>
#include <sys/file.h>

char *myname;
int verbose;
static char *initcmd = "t160 o1 l16 ml";

static char usage_msg[] =
	"simple keyboard player V0.8086\n"
	"usage: %s [-v][-i str]\n"
	"\t-i str defaults 't160 o1 l16 ml'\n"
	"function: play by console keyboard\n"
	"\tESC to exit. Note keys are ...\n"
	"\t1 2   4 5   7 8 9   - = \\\n"
	"\t Q W E R T Y U I O P [ ]\n"
	;

struct kdef_t {
	int ch;
	char *str;
};

static char *kstr[256];

static struct kdef_t kdef[] = {
	/* white key */
	{ '\t', "<g>" },
	{ 'q', "<a>" },
	{ 'w', "<b>" },
	{ 'e', "c" },
	{ 'r', "d" },
	{ 't', "e" },
	{ 'y', "f" },
	{ 'u', "g" },
	{ 'i', "a" },
	{ 'o', "b" },
	{ 'p', ">c<" },
	{ '[', ">d<" },
	{ ']', ">e<" },
	{ '\n', ">f<" },
	{ '\r', ">f<" },
	/* black key */
	{ '`', "<f#>" },
	{ '1', "<g#>" },
	{ '2', "<a#>" },
	/*{ '3', "<b#>" },*/
	{ '4', "c#" },
	{ '5', "d#" },
	/*{ '6', "e#" },*/
	{ '7', "f#" },
	{ '8', "g#" },
	{ '9', "a#" },
	/*{ '0', "b#" },*/
	{ '-', ">c#<" },
	{ '=', ">d#<" },
	/*{ '\', ">e#<" },*/
	{ '\177', ">f#<" },
	{ '\0', NULL }
};

static int
init_kstr(void)
{
	struct kdef_t *mv = kdef;
	while (mv->str != NULL) {
		kstr[mv->ch] = mv->str;
		mv++;
	}/* while */
	return 0;
}/* init_kstr */

static int
fdputs(const char *s, int fd, int echo)
{
	int err, len = strlen(s);
	write(fd, s, len);
	err = write(fd, "\n", 1);
	if (echo) {
		fputs(s, stdout);
	}
	return err;
}/* fdputs */

static int
outspkr(const char *s)
{
	int err = -1, fd = open("/dev/speaker", O_WRONLY);
	if (fd >= 0) {
		fdputs(initcmd, fd, 0);
		err = fdputs(s, fd, verbose);
		close(fd);
	}
	return err;
}/* outspkr */

static int
nain(void)
{
	int ch;
	initscr();
	noecho();
	nonl();
	raw();
	init_kstr();
	while ((ch = getch()) != '\033') {
		if (kstr[ch] != NULL) {
			outspkr(kstr[ch]);
		}
		else {
			if (verbose) {
				switch (ch) {
				case ' ':
					fputs(" ", stdout);
					break;
				case '\b':
					fputs("\b", stdout);
					break;
				}/* switch */
			}
		}
	}/* while */
	endwin();
	return 0;
}/* nain */

int
main(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind, opterr;
	int ch, ex, show_usage = 0;
	myname = argv[0];
	while ((ch = getopt(argc, argv, "-vi:")) != -1) {
		switch (ch) {
		default:
		case 'V':
			show_usage++;
			break;
		case 'v':
			verbose++;
			break;
		case 'i':
			initcmd = optarg;
			break;
		}/* switch */
	}/* while */
	ex = 1;
	if (show_usage) {
		fprintf(stderr, usage_msg, myname);
	}
	else {
		printf("Type ESC to exit.\n");
		ex = 0;
		nain();
	}
	return ex;
}/* main */