-
Notifications
You must be signed in to change notification settings - Fork 83
/
main.c
387 lines (312 loc) · 8.67 KB
/
main.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <limits.h>
#include <stdbool.h>
#include <getopt.h>
#include <time.h>
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alure.h>
#else
#include <AL/al.h>
#include <AL/alure.h>
#endif
#include "buckle.h"
#define SRC_INVALID INT_MAX
#define DEFAULT_MUTE_KEYCODE 0x46 /* Scroll Lock */
#define TEST_ERROR(_msg) \
error = alGetError(); \
if (error != AL_NO_ERROR) { \
fprintf(stderr, _msg "\n"); \
exit(1); \
}
static void usage(char *exe);
static void list_devices(void);
static double find_key_loc(int code);
/*
* Horizontal position on keyboard for each key as they are located on my model-M
*/
static int keyloc[][32] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6e, 0x66, 0x68, 0x1c, 0x45, 0x62, 0x37, 0x4a, -1 },
{ 0x01, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0x6f, 0x6b, 0x6d, 0x47, 0x48, 0x49, 0x4e, -1 },
{ 0x29, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x4b, 0x4c, 0x4d, -1 },
{ 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x2b, 0x4f, 0x50, 0x51, 0x60, -1 },
{ 0x3a, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x1c, 0x52, 0x53, -1 },
{ 0x2a, 0x56, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, -1 },
{ 0x1d, 0x7d, 0x5b, 0x38, 0x39, 0x64, 0x61, 0x67, -1 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6c, 0x6a, -1 },
};
/*
* Horizontal position on keyboard of the pragmatic center of the row, since keys come in different sizes and shapes
*/
static double midloc[] = {
7.5,
7.5,
7.5,
6.5,
6.5,
6.5,
4.5,
};
static int opt_verbose = 0;
static int opt_no_click = 0;
static int opt_stereo_width = 50;
static int opt_gain = 100;
static int opt_fallback_sound = 0;
static int opt_mute_keycode = DEFAULT_MUTE_KEYCODE;
static const char *opt_device = NULL;
static const char *opt_path_audio = PATH_AUDIO;
static int muted = 0;
static const char short_opts[] = "d:fg:hlm:Mp:s:cv";
static const struct option long_opts[] = {
{ "device", required_argument, NULL, 'd' },
{ "fallback-sound", no_argument, NULL, 'f' },
{ "gain", required_argument, NULL, 'g' },
{ "help", no_argument, NULL, 'h' },
{ "list-devices", no_argument, NULL, 'l' },
{ "mute-keycode", required_argument, NULL, 'm' },
{ "mute", no_argument, NULL, 'M' },
{ "audio-path", required_argument, NULL, 'p' },
{ "stereo-width", required_argument, NULL, 's' },
{ "no-click", no_argument, NULL, 'c' },
{ "verbose", no_argument, NULL, 'v' },
{ 0, 0, 0, 0 }
};
int main(int argc, char **argv)
{
int c;
int rv = EXIT_SUCCESS;
int idx;
while( (c = getopt_long(argc, argv,
short_opts, long_opts, &idx)) != -1) {
switch(c) {
case 'd':
opt_device = optarg;
break;
case 'f':
opt_fallback_sound = 1;
break;
case 'g':
opt_gain = atoi(optarg);
break;
case 'h':
usage(argv[0]);
return 0;
case 'l':
list_devices();
return 0;
case 'm':
opt_mute_keycode = strtol(optarg, NULL, 0);
break;
case 'M':
muted = !muted;
break;
case 'p':
opt_path_audio = optarg;
break;
case 's':
opt_stereo_width = atoi(optarg);
break;
case 'c':
opt_no_click++;
break;
case 'v':
opt_verbose++;
break;
default:
usage(argv[0]);
return 1;
break;
}
}
if(opt_verbose) {
open_console();
}
/* Create openal context */
ALCdevice *device = NULL;
ALCcontext *context = NULL;
ALfloat listenerOri[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f };
ALCenum error;
if (!opt_device) {
opt_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
}
printd("Opening OpenAL audio device \"%s\"", opt_device);
device = alcOpenDevice(opt_device);
if (!device) {
fprintf(stderr, "unable to open default device\n");
rv = EXIT_FAILURE;
goto out;
}
context = alcCreateContext(device, NULL);
if (!alcMakeContextCurrent(context)) {
fprintf(stderr, "failed to make default context\n");
return -1;
}
TEST_ERROR("make default context");
alListener3f(AL_POSITION, 0, 0, 0);
alListener3f(AL_VELOCITY, 0, 0, 0);
alListenerfv(AL_ORIENTATION, listenerOri);
/* Path to data files can also be specified by environment, this is
* used by the snap package */
const char *env_path = getenv("BUCKLESPRING_WAV_DIR");
if (env_path) {
opt_path_audio = env_path;
}
printd("Using wav dir: \"%s\"\n", opt_path_audio);
scan(opt_verbose);
out:
device = alcGetContextsDevice(context);
alcMakeContextCurrent(NULL);
if(context) alcDestroyContext(context);
if(device) alcCloseDevice(device);
return rv;
}
static void usage(char *exe)
{
fprintf(stderr,
"bucklespring version " VERSION "\n"
"usage: %s [options]\n"
"\n"
"options:\n"
"\n"
" -d, --device=DEVICE use OpenAL audio device DEVICE\n"
" -f, --fallback-sound use a fallback sound for unknown keys\n"
" -g, --gain=GAIN set playback gain [0..100]\n"
" -m, --mute-keycode=CODE use CODE as mute key (default 0x46 for scroll lock)\n"
" -M, --mute start the program muted\n"
" -c, --no-click don't play a sound on mouse click\n"
" -h, --help show help\n"
" -l, --list-devices list available OpenAL audio devices\n"
" -p, --audio-path=PATH load .wav files from directory PATH\n"
" -s, --stereo-width=WIDTH set stereo width [0..100]\n"
" -v, --verbose increase verbosity / debugging\n",
exe
);
}
static void list_devices(void)
{
const ALCchar *devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
const ALCchar *device = devices, *next = devices + 1;
size_t len = 0;
printf("Available audio devices:");
while (device && *device != '\0' && next && *next != '\0') {
fprintf(stdout, " \"%s\"", device);
len = strlen(device);
device += (len + 1);
next += (len + 2);
}
printf("\n");
}
void printd(const char *fmt, ...)
{
if(opt_verbose) {
char buf[256];
va_list va;
va_start(va, fmt);
vsnprintf(buf, sizeof(buf), fmt, va);
va_end(va);
fprintf(stderr, "%s\n", buf);
}
}
/*
* Find horizontal position of the given key on the keyboard. returns -1.0 for
* left to 1.0 for right
*/
static double find_key_loc(int code)
{
int row;
int col, keycol = 0;
for(row=0; row<8; row++) {
for(col=0; col<32; col++) {
if(keyloc[row][col] == code) keycol = col+1;
if(keyloc[row][col] == -1) break;
}
if(keycol) {
return ((double) keycol-midloc[row])/(col-midloc[row]);
}
}
return 0;
}
/*
* To silence play temporarily, press mute key (default ScrollLock) within 2
* seconds, same to unmute
*/
static void handle_mute_key(int mute_key)
{
static time_t t_prev;
static int count = 0;
if(mute_key) {
time_t t_now = time(NULL);
if(t_now - t_prev < 2) {
count ++;
if(count == 2) {
muted = !muted;
printd("Mute %s", muted ? "enabled" : "disabled");
count = 0;
}
} else {
count = 1;
}
t_prev = t_now;
} else {
count = 0;
}
}
/*
* Play audio file for given keycode. Wav files are loaded on demand
*/
int play(int code, int press)
{
ALCenum error;
printd("scancode %d/0x%x", code, code);
if (code == 0xff && opt_no_click) return 0;
/* Check for mute sequence: ScrollLock down+up+down */
if (press) {
handle_mute_key(code == opt_mute_keycode);
}
static ALuint buf[512] = { 0 };
static ALuint src[512] = { 0 };
int idx = code + press * 256;
if(src[idx] == 0) {
char fname[256];
snprintf(fname, sizeof(fname), "%s/%02x-%d.wav", opt_path_audio, code, press);
printd("Loading audio file \"%s\"", fname);
buf[idx] = alureCreateBufferFromFile(fname);
if(buf[idx] == 0) {
if(opt_fallback_sound) {
snprintf(fname, sizeof(fname), "%s/%02x-%d.wav", opt_path_audio, 0x31, press);
buf[idx] = alureCreateBufferFromFile(fname);
} else {
fprintf(stderr, "Error opening audio file \"%s\": %s\n", fname, alureGetErrorString());
}
if(buf[idx] == 0) {
src[idx] = SRC_INVALID;
return -1;
}
}
alGenSources((ALuint)1, &src[idx]);
TEST_ERROR("source generation");
double x = find_key_loc(code);
if (opt_stereo_width > 0) {
alSource3f(src[idx], AL_POSITION, -x, 0, (100 - opt_stereo_width) / 100.0);
}
alSourcef(src[idx], AL_GAIN, opt_gain / 100.0);
alSourcei(src[idx], AL_BUFFER, buf[idx]);
TEST_ERROR("buffer binding");
}
if(src[idx] != 0 && src[idx] != SRC_INVALID) {
if (!muted)
alSourcePlay(src[idx]);
TEST_ERROR("source playing");
}
return 0;
}
/*
* End
*/