From 52680474756de3b1b0ddc03f11c63224c0e38973 Mon Sep 17 00:00:00 2001 From: Grazvydas Ignotas Date: Sat, 29 May 2010 16:06:47 +0300 Subject: [PATCH] op_test_inputs: sound test and validation version code --- Makefile | 5 +- op_test_inputs.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index f5a8fe4..7a9692a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ clean: $(RM) *.o $(BIN) op_runfbapp: LDFLAGS += -lpthread -lX11 -op_test_inputs: LDFLAGS += -lts +op_test_inputs: LDFLAGS += -lpthread -lts $(INSTALL): mkdir -p $(INSTALL) @@ -25,3 +25,6 @@ install: $(INSTALL) $(BIN) cp ofbset $(INSTALL)/ cp scripts/op_gammatool $(INSTALL)/ cp scripts/op_test_inputs $(INSTALL)/ + +vld: + $(CC) -Wall -O2 -DVALIDATION -s op_test_inputs.c -o op_test_inputs -Wl,-Bstatic -lpthread -lc -Wl,-Bdynamic -lts diff --git a/op_test_inputs.c b/op_test_inputs.c index 90e9b00..a4af31a 100644 --- a/op_test_inputs.c +++ b/op_test_inputs.c @@ -26,6 +26,7 @@ */ #define _GNU_SOURCE #include +#include #include #include #include @@ -37,6 +38,8 @@ #include "font.c" +#define array_size(x) (sizeof(x) / sizeof(x[0])) + typedef struct { int x, y; int code; @@ -226,7 +229,7 @@ static void redraw_keys_lid(unsigned short *fb) text_out16(fb, 10, 260, lid_closed ? 0x07e0 : 0x7bef, "LID"); } -static void setkey(int code, int val) +static void set_key(int code, int val) { key_item *key = NULL; int i; @@ -251,6 +254,105 @@ static void setkey(int code, int val) } } +/* sound test */ +#include +#include + +static int snd_test_l, snd_test_r, snd_test_quit; +static pthread_cond_t snd_cond = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t snd_mutex = PTHREAD_MUTEX_INITIALIZER; + +static void *sound_thread(void *arg) +{ + int i, ret, randfd, dspfd; + int frag, bits, stereo, rate; + short buf[8*1024];//2*22050 / 10]; + + randfd = open("/dev/urandom", O_RDONLY); + if (randfd == -1) { + perror("open(\"/dev/urandom\")"); + return NULL; + } + + dspfd = open("/dev/dsp", O_WRONLY); + if (dspfd == -1) { + perror("open(\"/dev/dsp\")"); + close(randfd); + return NULL; + } + + frag = (2 << 16) | 13; + ret = ioctl(dspfd, SNDCTL_DSP_SETFRAGMENT, &frag); + if (ret < 0) + perror("SNDCTL_DSP_SETFRAGMENT"); + + stereo = 1; bits = 16; rate = 22050; + ret = ioctl(dspfd, SNDCTL_DSP_STEREO, &stereo); + if (ret == 0) + ret = ioctl(dspfd, SNDCTL_DSP_SETFMT, &bits); + if (ret == 0) + ret = ioctl(dspfd, SNDCTL_DSP_SPEED, &rate); + if (ret < 0) + perror("failed to set audio format"); + + while (1) { + pthread_mutex_lock(&snd_mutex); + if (!snd_test_l && !snd_test_r) + pthread_cond_wait(&snd_cond, &snd_mutex); + pthread_mutex_unlock(&snd_mutex); + + if (snd_test_quit) + break; + + ret = read(randfd, buf, sizeof(buf)); + if (ret == -1) { + perror("urandom read"); + break; + } + + if (!snd_test_l) + for (i = 0; i < array_size(buf) / 2; i++) + buf[i * 2] = 0; + if (!snd_test_r) + for (i = 0; i < array_size(buf) / 2; i++) + buf[i * 2 + 1] = 0; + + ret = write(dspfd, buf, sizeof(buf)); + if (ret == -1) { + perror("dsp write"); + break; + } + } + + close(randfd); + close(dspfd); + + return NULL; +} + +static void sound_init(void) +{ + pthread_t tid; + int ret; + + ret = pthread_create(&tid, NULL, sound_thread, NULL); + if (ret != 0) { + fprintf(stderr, "pthread_create sound_thread: %d\n", ret); + return; + } + pthread_detach(tid); +} + +static void sound_do(int l, int r) +{ + pthread_mutex_lock(&snd_mutex); + snd_test_l = l; + snd_test_r = r; + if (l || r || snd_test_quit) + pthread_cond_signal(&snd_cond); + pthread_mutex_unlock(&snd_mutex); +} + enum { DEV_PWRBTN, DEV_KEYPAD, @@ -286,6 +388,8 @@ int main(int argc, char *argv[]) memset(screen, 0, 800*480*2); + sound_init(); + for (id = 0; ; id++) { char fname[64]; @@ -353,7 +457,11 @@ int main(int argc, char *argv[]) if (ifd[DEV_LNUB] == -1) printf("Warning: couldn't find nub1 device\n"); if (ifd[DEV_RNUB] == -1) printf("Warning: couldn't find nub2 device\n"); +#ifdef VALIDATION + text_out16_small(screen, 220, 3, 0x7bef, "press menu/pandora to shutdown, alt/start to exit"); +#else text_out16_small(screen, 320, 3, 0x7bef, "Press L+R to exit"); +#endif while (!pressed_l || !pressed_r) { @@ -414,11 +522,25 @@ int main(int argc, char *argv[]) case EV_SYN: break; case EV_KEY: - setkey(ev[i].code, ev[i].value); + set_key(ev[i].code, ev[i].value); +#ifndef VALIDATION if (ev[i].code == KEY_RIGHTSHIFT) pressed_l = !!ev[i].value; if (ev[i].code == KEY_RIGHTCTRL) pressed_r = !!ev[i].value; +#else + if (ev[i].code == KEY_LEFTALT && !!ev[i].value) + goto end; + if (ev[i].code == KEY_MENU && !!ev[i].value) { + sync(); + system("poweroff -f"); + goto end; + } +#endif + if (ev[i].code == KEY_LEFT) + sound_do(!!ev[i].value, snd_test_r); + if (ev[i].code == KEY_RIGHT) + sound_do(snd_test_l, !!ev[i].value); break; case EV_ABS: which = (fd == ifd[DEV_LNUB]) ? 0 : 1; @@ -446,6 +568,8 @@ int main(int argc, char *argv[]) } } + snd_test_quit = 1; + sound_do(0, 0); end: if (ts != NULL) -- 2.39.5