xref: /haiku/src/kits/midi/MidiSettings.cpp (revision 6aff37d1c79e20748c683ae224bd629f88a5b0be)
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 		return B_ERROR;
42 	strlcpy(settings->soundfont_file, soundfont,
43 		sizeof(settings->soundfont_file));
44 
45 	unload_driver_settings(handle);
46 	return B_OK;
47 }
48 
49 
50 status_t
51 write_midi_settings(struct midi_settings settings)
52 {
53 	BPath path;
54 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
55 	if (status != B_OK)
56 		return status;
57 
58 	path.Append(SETTINGS_FILE);
59 
60 	BFile file;
61 	if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE)
62 		!= B_OK)
63 		return B_ERROR;
64 
65 	char buffer[B_FILE_NAME_LENGTH + 128];
66 	snprintf(buffer, sizeof(buffer), "# Midi\n\tsoundfont \"%s\"\n",
67 			settings.soundfont_file);
68 
69 	size_t bufferSize = strlen(buffer);
70 	if (file.InitCheck() != B_OK
71 		|| file.Write(buffer, bufferSize) != (ssize_t)bufferSize)
72 		return B_ERROR;
73 
74 	return B_OK;
75 }
76 
77 }
78