xref: /haiku/src/kits/midi/MidiSettings.cpp (revision 13581b3d2a71545960b98fefebc5225b5bf29072)
1 /*
2  * Copyright 2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <MidiSettings.h>
7 
8 #include <File.h>
9 #include <FindDirectory.h>
10 #include <Path.h>
11 
12 #include <driver_settings.h>
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #define SETTINGS_FILE "Media/midi_settings"
19 
20 namespace BPrivate {
21 
22 status_t
23 read_midi_settings(struct midi_settings* settings)
24 {
25 	if (settings == NULL)
26 		return B_ERROR;
27 
28 	BPath path;
29 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
30 	if (status != B_OK)
31 		return status;
32 
33 	path.Append(SETTINGS_FILE);
34 	void* handle = load_driver_settings(path.Path());
35 	if (handle == NULL)
36 		return B_ERROR;
37 
38 	const char* soundfont = get_driver_parameter(handle, "soundfont", NULL,
39 		NULL);
40 	if (soundfont == NULL) {
41 		unload_driver_settings(handle);
42 		return B_ERROR;
43 	}
44 	strlcpy(settings->soundfont_file, soundfont,
45 		sizeof(settings->soundfont_file));
46 
47 	unload_driver_settings(handle);
48 	return B_OK;
49 }
50 
51 
52 status_t
53 write_midi_settings(struct midi_settings settings)
54 {
55 	BPath path;
56 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
57 	if (status != B_OK)
58 		return status;
59 
60 	path.Append(SETTINGS_FILE);
61 
62 	BFile file;
63 	if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE)
64 		!= B_OK)
65 		return B_ERROR;
66 
67 	char buffer[B_FILE_NAME_LENGTH + 128];
68 	snprintf(buffer, sizeof(buffer), "# Midi\n\tsoundfont \"%s\"\n",
69 			settings.soundfont_file);
70 
71 	size_t bufferSize = strlen(buffer);
72 	if (file.InitCheck() != B_OK
73 		|| file.Write(buffer, bufferSize) != (ssize_t)bufferSize)
74 		return B_ERROR;
75 
76 	return B_OK;
77 }
78 
79 }
80