aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw27@cam.ac.uk>2008-11-23 20:13:53 +0000
committerThomas White <taw27@cam.ac.uk>2008-11-23 20:13:53 +0000
commitfd595cda1f4bca58e8fcc77d199a36a7f8e627fc (patch)
treea7fbaaa304ed3a4a46530ff174fafcd506c17fcf
parent4f3f5162c6e49d97579999bb33be3325d4afd5a8 (diff)
Trigger moo when image is clicked
-rw-r--r--src/accelerometers.c9
-rw-r--r--src/audio.c32
-rw-r--r--src/audio.h4
-rw-r--r--src/mainwindow.c13
-rw-r--r--src/types.h2
5 files changed, 43 insertions, 17 deletions
diff --git a/src/accelerometers.c b/src/accelerometers.c
index a0fed1b..464134b 100644
--- a/src/accelerometers.c
+++ b/src/accelerometers.c
@@ -148,11 +148,10 @@ void accelerometer_update(AccelHandle *accel) {
static void *accel_work(void *data) {
AccelHandle *accel;
- AudioContext *audio;
int pos = 0;
accel = accelerometer_open();
- audio = audio_setup();
+ audio_setup();
while ( 1 ) {
@@ -161,13 +160,13 @@ static void *accel_work(void *data) {
if ( accel->lval > 1000 ) pos = 1000;
if ( (accel->lval < -1000) && (pos == 1000) ) {
pos = 0;
- audio_trigger_moo(audio);
+ audio_trigger_moo();
}
- usleep(250000);
+ usleep(25000);
}
- audio_shutdown(audio);
+ audio_shutdown();
return NULL;
diff --git a/src/audio.c b/src/audio.c
index ed37a89..765ca77 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -22,6 +22,7 @@
*
*/
+/* SDL's headers don't like this. Grr. */
//#ifdef HAVE_CONFIG_H
//#include <config.h>
//#endif
@@ -35,9 +36,12 @@
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
+#include <assert.h>
#include "types.h"
+static AudioContext *audio_context = NULL;
+
static void audio_mix(void *data, Uint8 *stream8, int len) {
AudioContext *a = data;
@@ -54,14 +58,23 @@ static void audio_mix(void *data, Uint8 *stream8, int len) {
if ( a->moo_pos < a->moo_len ) {
samp = a->moo_buf[a->moo_pos++];
stream[j] += samp;
+ } else {
+ a->mootex = 0;
}
}
}
-void audio_trigger_moo(AudioContext *a) {
+void audio_trigger_moo() {
+ AudioContext *a = audio_context;
+
+ if ( a->mootex != 0 ) return;
+
+ printf("Moo!\n");
+ a->mootex = 1;
+
if ( a->aplay_fallback ) {
pid_t pid;
@@ -77,9 +90,8 @@ void audio_trigger_moo(AudioContext *a) {
execlp("aplay", "aplay", DATADIR"/openmoocow/moo.wav", NULL);
}
} /* else forked successfully, parent process */
- printf("Waiting...\n"); fflush(stdout);
waitpid(pid, &status, 0);
- printf("Done mooing\n"); fflush(stdout);
+ a->mootex = 0;
} else {
if ( a->moo_pos == a->moo_len ) {
@@ -90,7 +102,7 @@ void audio_trigger_moo(AudioContext *a) {
}
/* SDL audio initial setup */
-AudioContext *audio_setup() {
+void audio_setup() {
AudioContext *a;
SDL_AudioSpec fmt;
@@ -101,7 +113,9 @@ AudioContext *audio_setup() {
/* Create audio context */
a = malloc(sizeof(AudioContext));
- if ( a == NULL ) return NULL;
+ assert(a != NULL);
+
+ a->mootex = 0;
/* 16-bit mono audio at 44.1 kHz */
fmt.freq = 44100;
@@ -115,19 +129,19 @@ AudioContext *audio_setup() {
if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
a->aplay_fallback = 1;
- return a;
+ return;
}
a->aplay_fallback = 0;
if ( SDL_LoadWAV(DATADIR"/openmoocow/moo.wav", &wave, &data, &dlen) == NULL ) {
fprintf(stderr, "Couldn't load moo sound: %s\n", SDL_GetError());
- return a;
+ return;
}
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 1, 44100);
cvt.buf = malloc(dlen*cvt.len_mult);
if ( cvt.buf == NULL ) {
fprintf(stderr, "Not enough memory to convert audio \n");
- return a;
+ return;
}
memcpy(cvt.buf, data, dlen);
cvt.len = dlen;
@@ -140,7 +154,7 @@ AudioContext *audio_setup() {
SDL_PauseAudio(0);
- return a;
+ audio_context = a;
}
diff --git a/src/audio.h b/src/audio.h
index fc4e9f6..a8f0032 100644
--- a/src/audio.h
+++ b/src/audio.h
@@ -32,8 +32,8 @@
#include "types.h"
extern AudioContext *audio_setup(void);
-extern void audio_trigger_moo(AudioContext *a);
-extern void audio_shutdown(AudioContext *a);
+extern void audio_trigger_moo(void);
+extern void audio_shutdown(void);
#endif /* AUDIO_H */
diff --git a/src/mainwindow.c b/src/mainwindow.c
index f226049..93a1fd9 100644
--- a/src/mainwindow.c
+++ b/src/mainwindow.c
@@ -30,15 +30,22 @@
#include <stdlib.h>
#include "types.h"
+#include "audio.h"
static gint mainwindow_closed(GtkWidget *widget, MainWindow *mw) {
gtk_exit(0);
return 0;
}
+static gint mainwindow_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data) {
+ audio_trigger_moo();
+ return 0;
+}
+
MainWindow *mainwindow_open(void) {
MainWindow *mw;
+ GtkWidget *eb;
mw = malloc(sizeof(*mw));
if ( mw == NULL ) return NULL;
@@ -47,9 +54,13 @@ MainWindow *mainwindow_open(void) {
gtk_window_set_default_size(GTK_WINDOW(mw->window), 240, 320);
gtk_window_set_title(GTK_WINDOW(mw->window), "OpenMooCow");
+ eb = gtk_event_box_new();
mw->cow = gtk_image_new_from_file(PIXMAPDIR"/cow.png");
- gtk_container_add(GTK_CONTAINER(mw->window), mw->cow);
+ gtk_container_add(GTK_CONTAINER(eb), mw->cow);
+ gtk_container_add(GTK_CONTAINER(mw->window), eb);
+ gtk_widget_add_events(GTK_WIDGET(eb), GDK_BUTTON_PRESS_MASK);
+ g_signal_connect(G_OBJECT(eb), "button_press_event", G_CALLBACK(mainwindow_clicked), mw);
g_signal_connect(G_OBJECT(mw->window), "destroy", G_CALLBACK(mainwindow_closed), mw);
gtk_widget_show_all(mw->window);
diff --git a/src/types.h b/src/types.h
index 9a2b1bd..7837ff2 100644
--- a/src/types.h
+++ b/src/types.h
@@ -61,6 +61,8 @@ typedef struct {
int aplay_fallback;
+ unsigned int mootex;
+
} AudioContext;
typedef struct {