Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/c.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ jobs:
run: sudo apt-get install libcurl4-openssl-dev libsdl2-dev
- name: make
run: make
- name: make with sound support
run: make with-sound

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ before_script:

script:
- make
- make with-sound

# whitelist branches to avoid testing feature branches twice (as branch and as pull request)
branches:
Expand Down
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
OBJS = doh.o
TARGET = doh
OBJS = doh.o sound/doh-sound.o
SOUND_LIBS = -lSDL2 -lpthread
LDLIBS = $(SOUND_LIBS) `curl-config --libs`
LDLIBS = `curl-config --libs`
CFLAGS := $(CFLAGS) -W -Wall -pedantic -g `curl-config --cflags`
MANUAL = doh.1

Expand All @@ -15,5 +14,13 @@ install:
install -m 0755 $(TARGET) $(DESTDIR)$(BINDIR)
install -m 0744 $(MANUAL) $(MANDIR)/man1/

with-sound: OBJS = doh.o sound/doh-sound.o
with-sound: LDLIBS = -lSDL2 -lpthread `curl-config --libs`
with-sound: CFLAGS := $(CFLAGS) -DHAS_SOUND -W -Wall -pedantic -g `curl-config --cflags`
with-sound:
if [ "$$(./sdl2-util.sh --check)" = "no" ]; then \
./sdl2-util.sh --install; \
fi && $(MAKE) clean && $(MAKE) LDLIBS="$(LDLIBS)" CFLAGS="$(CFLAGS)" OBJS="$(OBJS)"

clean:
rm -f $(OBJS) $(TARGET)
11 changes: 10 additions & 1 deletion doh.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ enum iptrans { v4, v6, v46 };
#include "version.h"

/* doh-sound related stuff */
#ifdef HAS_SOUND
#include "sound/doh-sound.h"
#include <pthread.h>
#include <errno.h>
pthread_t sound_thread;
static int playing = false;
#endif

#ifdef _WIN32
#define FMT_SIZE_T "Iu"
Expand Down Expand Up @@ -722,7 +725,9 @@ static void help(const char *msg)
fprintf(stderr, "Usage: doh [options] <host> [URL]\n"
" -h this help\n"
" -k insecure mode - don't validate TLS certificate\n"
#ifdef HAS_SOUND
" -s play doh sound\n"
#endif
" -t test mode\n"
" -v verbose mode\n"
" -4 use only IPv4 transport\n"
Expand Down Expand Up @@ -788,14 +793,16 @@ int main(int argc, char **argv)
case 'h': /* help */
help(NULL);
break;
#ifdef HAS_SOUND
case 's':
if (pthread_create(&sound_thread, NULL, play_sound, NULL) != 0) {
fprintf(stderr, "playing sound failed\n");
fprintf(stderr, "pthread_create failed: %s\n", strerror(errno));
playing = false;
} else {
playing = true;
}
break;
#endif
default:
help("unrecognized option");
break;
Expand Down Expand Up @@ -951,9 +958,11 @@ int main(int argc, char **argv)
printf("CNAME: %s\n", d.cname[i].alloc);
}

#ifdef HAS_SOUND
if (playing) {
pthread_join(sound_thread, NULL);
}
#endif

doh_cleanup(&d);
if (headers != NULL)
Expand Down
25 changes: 25 additions & 0 deletions sdl2-util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
#set -x
install_SDL2() {
curl --silent -L https://www.libsdl.org/release/SDL2-2.0.10.tar.gz | tar xz -k
cd SDL2-2.0.10
./configure
make
sudo make install
cd ../
}
check_SDL2_installed() {
echo -e "#include<SDL2/SDL.h>\nint main(){}" | gcc -lSDL2 -xc - && echo "yes" || echo "no"
}
if [ "$1" = "--check" ]; then
check_SDL2_installed
elif [ "$1" = "--install" ]; then
if [ "$(check_SDL2_installed)" = "yes" ]; then
echo "SDL2 already installed" >&2
# cleanup
rm a.out
else
echo "SDL2 is not installed, installing..." >&2
install_SDL2
fi
fi
2 changes: 1 addition & 1 deletion sound/doh-sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void *play_sound(void *args)

// wait until we're done playing
while (audio_len > 0) {
SDL_Delay(100);
SDL_Delay(10);
}

// shut everything down
Expand Down