Mostly MIDI fixes - Alex
[terminatorX.git] / src / tX_global.c
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2003  Alexander K├Ânig
4  
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9  
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  
19     File: tX_global.c
20  
21     Description:  This file contains the routines for handling the
22                   "globals" block. Intializing, reading setup from
23                   disk and storing it.
24                   
25     Changes:
26     
27     21 Jul 1999: introduced the lowpass globals.
28         There were some changes in between...
29         14 Jul 2002: switched to libxml instead of binary saving.
30 */    
31
32 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include "tX_types.h"
36 #include "tX_global.h"
37 #include "string.h"
38 #include <libxml/xmlmemory.h>
39 #include <libxml/parser.h>
40
41 #define TX_XML_RC_VERSION "1.0"
42
43 tx_global globals;
44
45 void get_rc_name(char *buffer)
46 {
47         strcpy(buffer,"");
48         if (globals.alternate_rc)
49         {
50                 strcpy(buffer, globals.alternate_rc);
51         }
52         else 
53         {
54                 if (getenv("HOME"))
55                 {
56                         strcpy(buffer, getenv("HOME"));
57                         if (buffer[strlen(buffer)-1]!='/')
58                         strcat(buffer, "/");
59                 }
60                 strcat(buffer, ".terminatorXrc");
61         }
62 }
63
64 void set_global_defaults() {
65         globals.startup_set = 0;
66         globals.store_globals = 1;
67         globals.no_gui = 0;
68         globals.alternate_rc = 0;
69         
70         strcpy(globals.xinput_device, "");
71         globals.xinput_enable=0;
72         
73         globals.update_idle=18;
74         globals.update_delay=1;
75         
76         strcpy(globals.oss_device, "/dev/dsp");
77         globals.oss_buff_no=2;
78         globals.oss_buff_size=9;
79         globals.oss_samplerate=44100;
80
81         strcpy(globals.alsa_device, "00-00: Default");  
82         globals.alsa_buff_no=2;
83         globals.alsa_buff_size=1024;
84         globals.alsa_samplerate=44100;
85         
86         globals.sense_cycles=12;
87         
88         globals.mouse_speed=0.8;
89         
90         globals.width=800;
91         globals.height=440;     
92         
93         globals.tooltips=1;
94         
95         globals.use_stdout=0;
96         globals.use_stdout_from_conf_file=0;
97         
98         globals.show_nag=1;
99         globals.prelis=1;
100         
101         strcpy(globals.last_fn,"");
102         
103         globals.pitch=1.0;
104         globals.volume=1.0;
105         globals.gui_wrap=3;
106         
107         globals.flash_response=0.95;
108         
109         globals.button_type=BUTTON_TYPE_BOTH;
110                 
111         globals.true_block_size=0;
112         
113         strcpy(globals.tables_filename, "");
114         strcpy(globals.record_filename, "tX_record.wav");
115         strcpy(globals.file_editor, "");
116                 
117 #ifdef USE_OSS
118         globals.audiodevice_type=OSS;           
119 #else
120 #ifdef USE_ALSA
121         globals.audiodevice_type=ALSA;
122 #endif  
123 #endif          
124         globals.use_stdout_cmdline=0;
125         globals.current_path = NULL;
126         globals.pitch=1.0;
127         globals.volume=1.0;
128         
129         strcpy(globals.lrdf_path, "/usr/share/ladspa/rdf:/usr/local/share/ladspa/rdf");
130         globals.fullscreen_enabled=1;
131 }
132
133 int load_globals_xml() {
134         char rc_name[PATH_MAX]="";      
135         char device_type[16]="oss";
136         xmlDocPtr doc;
137         xmlNodePtr cur;
138         int elementFound;
139         double dvalue;
140
141         get_rc_name(rc_name);
142         
143         doc = xmlParseFile(rc_name);
144         
145         if (doc == NULL ) {
146                 fprintf(stderr, "tX: err: Error parsing terminatorXrc.\n");
147                 return 1;
148         }
149         
150         cur = xmlDocGetRootElement(doc);
151         
152         if (cur == NULL) {
153                 fprintf(stderr,"tX: err: terminatorXrc contains no elements.\n");
154                 xmlFreeDoc(doc);
155                 return 2;
156         }
157         
158         if (xmlStrcmp(cur->name, (const xmlChar *) "terminatorXrc")) {
159                 fprintf(stderr,"tX: err: This terminatorXrc is not a terminatorXrc.");
160                 xmlFreeDoc(doc);
161                 return 3;
162         }
163                 
164         for (cur=cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
165                 if (cur->type == XML_ELEMENT_NODE) {                    
166                         elementFound=0;
167
168                         restore_int("store_globals", globals.store_globals);
169
170                         restore_string("audio_driver", device_type);
171                         
172                         restore_string("oss_device", globals.oss_device);
173                         restore_int("oss_buff_no", globals.oss_buff_no);
174                         restore_int("oss_buff_size", globals.oss_buff_size);
175                         restore_int("oss_samplerate", globals.oss_samplerate);
176
177                         restore_string("alsa_device", globals.alsa_device);
178                         restore_int("alsa_buff_no", globals.alsa_buff_no);
179                         restore_int("alsa_buff_size", globals.alsa_buff_size);
180                         restore_int("alsa_samplerate", globals.alsa_samplerate);
181
182                         restore_string("xinput_device", globals.xinput_device);
183                         restore_int("xinput_enable", globals.xinput_enable);
184                         restore_int("update_idle", globals.update_idle);
185                         restore_int("update_delay", globals.update_delay);
186                         restore_int("sense_cycles", globals.sense_cycles);
187                         restore_float("mouse_speed", globals.mouse_speed);
188                         restore_int("width", globals.width);
189                         restore_int("height", globals.height);
190                         restore_int("tooltips", globals.tooltips);
191                         restore_int("use_stdout", globals.use_stdout);
192                         restore_int("show_nag", globals.show_nag);
193                         restore_int("prelis", globals.prelis);
194                         restore_string("last_fn", globals.last_fn);
195                         restore_float("pitch", globals.pitch);
196                         restore_float("volume", globals.volume);
197                         restore_float("flash_response", globals.flash_response);
198                         restore_int("button_type", globals.button_type);
199                         restore_string("tables_filename", globals.tables_filename);
200                         restore_string("record_filename", globals.record_filename);
201                         restore_string("file_editor", globals.file_editor);
202                         restore_string("lrdf_path", globals.lrdf_path);
203                         
204                         restore_int("fullscreen_enabled", globals.fullscreen_enabled);
205
206                         if (!elementFound) {
207                                 fprintf(stderr, "tX: Unhandled XML element: \"%s\"\n", cur->name);
208                         }
209                 }
210         }
211
212         xmlFreeDoc(doc);
213         
214         if (strcmp(device_type, "alsa")==0) globals.audiodevice_type=ALSA;
215         else if (strcmp(device_type, "jack")==0) globals.audiodevice_type=JACK;
216         else globals.audiodevice_type=OSS;
217         
218         return 0;
219 }
220
221 void store_globals() {
222         char rc_name[PATH_MAX]="";
223         char device_type[16];
224         char indent[]="\t";
225         FILE *rc;
226         char tmp_xml_buffer[4096];
227         
228         get_rc_name(rc_name);
229
230         rc=fopen(rc_name, "w");
231         
232         switch (globals.audiodevice_type) {
233                 case JACK:
234                         strcpy(device_type, "jack");
235                         break;
236                 case ALSA:
237                         strcpy(device_type, "alsa");
238                         break;
239                 case OSS:
240                 default:
241                         strcpy(device_type, "oss");
242                 
243         }
244         
245         if (rc) {               
246                 fprintf(rc, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n\n");
247                 fprintf(rc, "<!-- Warning: this file will be rewritten by terminatorX on exit!\n     Don\'t waste your time adding comments - they will be erased -->\n\n" );
248                 fprintf(rc, "<terminatorXrc version=\"%s\">\n", TX_XML_RC_VERSION);
249
250                 store_int("store_globals", globals.store_globals);
251
252                 store_string("audio_driver", device_type);
253                 
254                 store_string("oss_device", globals.oss_device);
255                 store_int("oss_buff_no", globals.oss_buff_no);
256                 store_int("oss_buff_size", globals.oss_buff_size);
257                 store_int("oss_samplerate", globals.oss_samplerate);
258
259                 store_string("alsa_device", globals.alsa_device);
260                 store_int("alsa_buff_no", globals.alsa_buff_no);
261                 store_int("alsa_buff_size", globals.alsa_buff_size);
262                 store_int("alsa_samplerate", globals.alsa_samplerate);          
263                 
264                 store_string("xinput_device", globals.xinput_device);
265                 store_int("xinput_enable", globals.xinput_enable);
266                 store_int("update_idle", globals.update_idle);
267                 store_int("update_delay", globals.update_delay);
268                 store_int("sense_cycles", globals.sense_cycles);
269                 store_float("mouse_speed", globals.mouse_speed);
270                 store_int("width", globals.width);
271                 store_int("height", globals.height);
272                 store_int("tooltips", globals.tooltips);
273                 store_int("use_stdout", globals.use_stdout);
274                 // globals.use_stdout_from_conf_file=0; What the heck is this?
275                 store_int("show_nag", globals.show_nag);
276                 store_int("prelis", globals.prelis);
277                 store_string("last_fn", globals.last_fn);
278                 store_float("pitch", globals.pitch);
279                 store_float("volume", globals.volume);
280                 store_float("flash_response", globals.flash_response);
281                 store_int("button_type", globals.button_type);
282                 store_string("tables_filename", globals.tables_filename);
283                 store_string("record_filename", globals.record_filename);
284                 store_string("file_editor", globals.file_editor);
285                 store_string("lrdf_path", globals.lrdf_path);
286                 store_int("fullscreen_enabled", globals.fullscreen_enabled);
287                 
288                 fprintf(rc,"</terminatorXrc>\n");
289         }
290 }
291
292 #ifdef ENABLE_TX_LEGACY
293 extern void load_globals_old();
294 #endif
295
296 void load_globals() {
297         set_global_defaults();
298
299         if (load_globals_xml()!=0) {
300                 fprintf(stderr, "tX: Failed loading terminatorXrc - trying to load old binary rc.\n");
301 #ifdef ENABLE_TX_LEGACY         
302                 load_globals_old();
303 #endif          
304         }
305 }
306
307 char *encode_xml(char *dest, const char *src) {
308         int i, t, max;
309         
310         dest[0]=0;
311         t=0;
312         max=strlen(src);
313         
314         for (i=0; i<max; i++) {
315                 switch (src[i]) {
316                         case '<': dest[t]=0; strcat(dest, "&lt;"); t+=4; break;
317                         case '>': dest[t]=0; strcat(dest, "&gt;"); t+=4; break;
318                         case '&': dest[t]=0; strcat(dest, "&amp;"); t+=5; break;
319                         case '"': dest[t]=0; strcat(dest, "&quot;"); t+=6; break;
320                         case '\'': strcat(dest, "&apos;"); t+=7; break;
321                         default: dest[t]=src[i]; t++;
322                 }
323         }
324         dest[t]=0;
325         
326         //tX_debug("encode_xml: from \"%s\" to \"%s\".", src, dest); 
327         return dest;
328 }