summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorzlago2024-09-01 18:57:23 +0200
committerzlago2024-09-01 18:57:23 +0200
commit3b284977d433ea4e39553bda329c4643c646cbc4 (patch)
tree96ad5d270895cef9b055abb47bd1ea58cbca2c10 /src
parent90b967d9d1b5970969f758c7993d244936fff324 (diff)
commandline params
Diffstat (limited to 'src')
-rw-r--r--src/sdl.c40
1 files changed, 31 insertions, 9 deletions
diff --git a/src/sdl.c b/src/sdl.c
index 8deb8ca..21ce1a8 100644
--- a/src/sdl.c
+++ b/src/sdl.c
@@ -35,7 +35,9 @@ void audio_callback(void *userdata_void, unsigned char *stream_void, int const l
 	}
 }
 
-int main(void) {
+int (*file_ext(char *file))(struct blob *, struct userdata *);
+
+int main(int argc, char **argv) {
 	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
 		eprintf("failed to init SDL: %s\n");
 		return EXIT_FAILURE;
@@ -72,6 +74,21 @@ int main(void) {
 	};
 	SDL_AudioDeviceID audio = SDL_OpenAudioDevice(NULL, 0, &desired, NULL, 0);
 	
+	if (argc == 2) {
+		int (*module_func)(struct blob *, struct userdata *) = file_ext(argv[1]);
+		if (module_func == NULL) {
+			eprintf("%s: unrecognized file extension\n", argv[1]);
+			goto arg_load_done;
+		}
+		struct blob file = load_file(argv[1]);
+		if (file.data == NULL) {
+			perror(argv[1]);
+			goto arg_load_done;
+		}
+		module_func(&file, &userdata);
+	}
+	arg_load_done:
+	
 	SDL_PauseAudioDevice(audio, 0);
 	while (true) {
 		SDL_Event evt;
@@ -105,14 +122,7 @@ int main(void) {
 						break;
 					}
 					struct userdata newuser = {.callback = NULL, .freefunc = NULL};
-					int (*module_func)(struct blob *, struct userdata *) = NULL;
-					size_t len = strlen(evt.drop.file);
-					#define ext(extension) memcmp(evt.drop.file + len - sizeof (extension) + 1, extension, sizeof (extension))
-					if ((ext(".mptm") && ext(".mod") && ext(".xm") && ext(".s3m") && ext(".it")) == 0) {
-						module_func = module_openmpt;
-					} else if ((ext(".mid") && ext(".midi")) == 0) {
-						module_func = module_fluidsynth;
-					}
+					int (*module_func)(struct blob *, struct userdata *) = file_ext(evt.drop.file);
 					if (module_func == NULL) {
 						eprintf("%s: unrecognized file extension\n", evt.drop.file);
 						free(file.data);
@@ -155,6 +165,18 @@ int main(void) {
 	return EXIT_SUCCESS;
 }
 
+int (*file_ext(char *file))(struct blob *, struct userdata *) {
+	size_t len = strlen(file);
+	#define ext(extension) memcmp(file + len - sizeof (extension) + 1, extension, sizeof (extension))
+	if ((ext(".mptm") && ext(".mod") && ext(".xm") && ext(".s3m") && ext(".it")) == 0) {
+		return module_openmpt;
+	} else if ((ext(".mid") && ext(".midi")) == 0) {
+		return module_fluidsynth;
+	}
+	#undef ext
+	return NULL;
+}
+
 struct blob load_file(char const *const name) {
 	const size_t START_SIZE = 1;
 	FILE *file = fopen(name, "rb");