/[gluid]/utils.c
ViewVC logotype

Annotation of /utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (hide annotations)
Wed Sep 30 16:30:02 2009 UTC (10 years, 7 months ago) by ben
File MIME type: text/plain
File size: 3714 byte(s)
allow multiple instances (up to 16)
1 ben 1 #include <stdlib.h>
2     #include <stdio.h>
3     #include <glib.h>
4     #include <gtk/gtk.h>
5     #include <sys/socket.h>
6     #include <unistd.h>
7     #include <arpa/inet.h>
8     #include <netinet/in.h>
9     #include <string.h>
10 ben 10 #include "type.h"
11     #include "utils.h"
12 ben 1
13 ben 36 int get_unique_id (GKeyFile* kf) {
14     GError* error = NULL;
15     gint id;
16    
17     id = g_key_file_get_integer (kf, "fluidsynth", "id", &error);
18     if(error) {
19     g_warning("read_config: %s\n", error->message);
20     g_error_free(error);
21     error = NULL;
22     id = 0;
23     }
24     id=(id+1)%16; /* up to 16 fluidsynths */
25     g_key_file_set_integer(kf, "fluidsynth", "id", id);
26     return id;
27     }
28    
29 ben 1 void quit(AppData* d) {
30     g_free(d->config_path);
31     g_free(d->sfpath);
32     g_free(d->audio);
33     g_free(d->midi);
34 ben 32 g_free(d->reverb);
35     g_free(d->chorus);
36 ben 1 gtk_main_quit();
37     }
38    
39     void read_config (AppData* d) {
40     GKeyFile* kf = NULL;
41     GError* error = NULL;
42 ben 36 gint id;
43 ben 1
44     kf = g_key_file_new();
45     g_key_file_load_from_file(kf, d->config_path, G_KEY_FILE_NONE, &error);
46     if(error) {
47     g_free(d->sfpath);
48     d->sfpath = g_strdup("/usr/share/sf2");
49     g_free(d->audio);
50     d->audio = g_strdup("jack");
51     g_free(d->midi);
52     d->midi = g_strdup("jack");
53 ben 32 g_free(d->reverb);
54 ben 36 d->reverb = g_strdup("yes");
55 ben 32 g_free(d->chorus);
56 ben 36 d->chorus = g_strdup("yes");
57     d->id = 0;
58    
59 ben 1 write_config(d);
60     g_error_free(error);
61     error = NULL;
62     g_key_file_load_from_file(kf, d->config_path, G_KEY_FILE_NONE, &error);
63     }
64     d->sfpath = g_strdup(g_key_file_get_value (kf, "fluidsynth", "sfpath", &error));
65     if(error) {
66     g_warning("read_config: %s\n", error->message);
67     g_error_free(error);
68     error = NULL;
69     }
70     d->audio = g_key_file_get_value(kf, "fluidsynth", "audio", &error);
71     if(error) {
72     g_warning("read_config: %s\n", error->message);
73     g_error_free(error);
74     error = NULL;
75     }
76     d->midi = g_key_file_get_value(kf, "fluidsynth", "midi", &error);
77     if(error) {
78     g_warning("read_config: %s\n", error->message);
79     g_error_free(error);
80     error = NULL;
81     }
82 ben 32 d->reverb = g_key_file_get_value(kf, "fluidsynth", "reverb", &error);
83     if(error) {
84     g_warning("read_config: %s\n", error->message);
85     g_error_free(error);
86     error = NULL;
87     }
88     d->chorus = g_key_file_get_value(kf, "fluidsynth", "chorus", &error);
89     if(error) {
90     g_warning("read_config: %s\n", error->message);
91     g_error_free(error);
92     error = NULL;
93     }
94 ben 36 id = g_key_file_get_integer (kf, "fluidsynth", "id", &error);
95     if(error) {
96     g_warning("read_config: %s\n", error->message);
97     g_error_free(error);
98     error = NULL;
99     id = 0;
100     }
101     d->id=(id+1)%16; /* up to 16 fluidsynths */
102    
103 ben 1 g_key_file_free(kf);
104 ben 36
105 ben 1 if (!d->sfpath || d->sfpath[0] == '\0') {
106     g_free(d->sfpath);
107     d->sfpath = g_strdup("/usr/share/sf2");
108     }
109 ben 32 if (!d->audio || d->audio[0] == '\0') {
110 ben 1 g_free(d->audio);
111     d->audio = g_strdup("jack");
112     }
113 ben 32 if (!d->midi || d->midi[0] == '\0') {
114 ben 1 g_free(d->midi);
115     d->midi = g_strdup("jack");
116     }
117 ben 32 if (!d->reverb || d->reverb[0] == '\0') {
118     g_free(d->reverb);
119     d->reverb = g_strdup("yes");
120     }
121     if (!d->chorus || d->chorus[0] == '\0') {
122     g_free(d->chorus);
123     d->chorus = g_strdup("yes");
124     }
125 ben 36 write_config(d); /* we must re-write because of id */
126 ben 1 }
127    
128     void write_config (AppData* d) {
129     GKeyFile* kf = NULL;
130     gchar* data = NULL;
131     gsize len;
132     FILE* fp = NULL;
133    
134     kf = g_key_file_new();
135     g_key_file_set_value (kf, "fluidsynth", "sfpath", d->sfpath);
136     g_key_file_set_value (kf, "fluidsynth", "audio", d->audio);
137     g_key_file_set_value (kf, "fluidsynth", "midi", d->midi);
138 ben 32 g_key_file_set_value (kf, "fluidsynth", "reverb", d->reverb);
139     g_key_file_set_value (kf, "fluidsynth", "chorus", d->chorus);
140 ben 36 g_key_file_set_integer(kf, "fluidsynth", "id", d->id);
141 ben 1 data = g_key_file_to_data(kf, &len, NULL);
142     fp = fopen(d->config_path, "w");
143     /* FIXME: check error (disk full, e.g.) */
144     fputs(data, fp);
145     fclose(fp);
146     g_free(data);
147     }
148    

  ViewVC Help
Powered by ViewVC 1.1.26