d5ae85ddc25441f05e880d9386c29011c9e0a604
[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 #include <libxml/encoding.h>
41
42
43 #define TX_XML_RC_VERSION "1.0"
44
45 tx_global globals;
46
47 void get_rc_name(char *buffer)
48 {
49         strcpy(buffer,"");
50         if (globals.alternate_rc)
51         {
52                 strcpy(buffer, globals.alternate_rc);
53         }
54         else 
55         {
56                 if (getenv("HOME"))
57                 {
58                         strcpy(buffer, getenv("HOME"));
59                         if (buffer[strlen(buffer)-1]!='/')
60                         strcat(buffer, "/");
61                 }
62                 strcat(buffer, ".terminatorXrc");
63         }
64 }
65
66 void set_global_defaults() {
67         globals.startup_set = 0;
68         globals.store_globals = 1;
69         globals.no_gui = 0;
70         globals.alternate_rc = 0;
71         
72         strcpy(globals.xinput_device, "");
73         globals.xinput_enable=0;
74         
75         globals.update_idle=18;
76         globals.update_delay=1;
77         
78         strcpy(globals.oss_device, "/dev/dsp");
79         globals.oss_buff_no=2;
80         globals.oss_buff_size=9;
81         globals.oss_samplerate=44100;
82
83         strcpy(globals.alsa_device, "00-00: Default");  
84         globals.alsa_buffer_time=10000;
85         globals.alsa_period_time=5000;
86         globals.alsa_samplerate=44100;
87         
88         globals.sense_cycles=12;
89         
90         globals.mouse_speed=0.8;
91         
92         globals.width=800;
93         globals.height=440;     
94         
95         globals.tooltips=1;
96         
97         globals.use_stdout=0;
98         globals.use_stdout_from_conf_file=0;
99         
100         globals.show_nag=1;
101         globals.prelis=1;
102         
103         strcpy(globals.last_fn,"");
104         
105         globals.pitch=1.0;
106         globals.volume=1.0;
107         globals.gui_wrap=3;
108         
109         globals.flash_response=0.95;
110         
111         globals.button_type=BUTTON_TYPE_BOTH;
112                 
113         globals.true_block_size=0;
114         
115         strcpy(globals.tables_filename, "");
116         strcpy(globals.record_filename, "tX_record.wav");
117         strcpy(globals.file_editor, "");
118                 
119 #ifdef USE_OSS
120         globals.audiodevice_type=OSS;           
121 #else
122 #ifdef USE_ALSA
123         globals.audiodevice_type=ALSA;
124 #endif  
125 #endif          
126         globals.use_stdout_cmdline=0;
127         globals.current_path = NULL;
128         globals.pitch=1.0;
129         globals.volume=1.0;
130         
131         strcpy(globals.lrdf_path, "/usr/share/ladspa/rdf:/usr/local/share/ladspa/rdf");
132         globals.fullscreen_enabled=1;
133 }
134
135 int load_globals_xml() {
136         char rc_name[PATH_MAX]="";      
137         char device_type[16]="oss";
138         xmlDocPtr doc;
139         xmlNodePtr cur;
140         int elementFound;
141         double dvalue;
142         char tmp_xml_buffer[4096];
143         
144         get_rc_name(rc_name);
145         
146         doc = xmlParseFile(rc_name);
147         
148         if (doc == NULL ) {
149                 fprintf(stderr, "tX: err: Error parsing terminatorXrc.\n");
150                 return 1;
151         }
152         
153         cur = xmlDocGetRootElement(doc);
154         
155         if (cur == NULL) {
156                 fprintf(stderr,"tX: err: terminatorXrc contains no elements.\n");
157                 xmlFreeDoc(doc);
158                 return 2;
159         }
160         
161         if (xmlStrcmp(cur->name, (const xmlChar *) "terminatorXrc")) {
162                 fprintf(stderr,"tX: err: This terminatorXrc is not a terminatorXrc.");
163                 xmlFreeDoc(doc);
164                 return 3;
165         }
166                 
167         for (cur=cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
168                 if (cur->type == XML_ELEMENT_NODE) {                    
169                         elementFound=0;
170
171                         restore_int("store_globals", globals.store_globals);
172
173                         restore_string("audio_driver", device_type);
174                         
175                         restore_string("oss_device", globals.oss_device);
176                         restore_int("oss_buff_no", globals.oss_buff_no);
177                         restore_int("oss_buff_size", globals.oss_buff_size);
178                         restore_int("oss_samplerate", globals.oss_samplerate);
179
180                         restore_string("alsa_device", globals.alsa_device);
181                         restore_int("alsa_buffer_time", globals.alsa_buffer_time);
182                         restore_int("alsa_period_time", globals.alsa_period_time);
183                         restore_int("alsa_samplerate", globals.alsa_samplerate);
184
185                         restore_string("xinput_device", globals.xinput_device);
186                         restore_int("xinput_enable", globals.xinput_enable);
187                         restore_int("update_idle", globals.update_idle);
188                         restore_int("update_delay", globals.update_delay);
189                         restore_int("sense_cycles", globals.sense_cycles);
190                         restore_float("mouse_speed", globals.mouse_speed);
191                         restore_int("width", globals.width);
192                         restore_int("height", globals.height);
193                         restore_int("tooltips", globals.tooltips);
194                         restore_int("use_stdout", globals.use_stdout);
195                         restore_int("show_nag", globals.show_nag);
196                         restore_int("prelis", globals.prelis);
197                         restore_string("last_fn", globals.last_fn);
198                         restore_float("pitch", globals.pitch);
199                         restore_float("volume", globals.volume);
200                         restore_float("flash_response", globals.flash_response);
201                         restore_int("button_type", globals.button_type);
202                         restore_string("tables_filename", globals.tables_filename);
203                         restore_string("record_filename", globals.record_filename);
204                         restore_string("file_editor", globals.file_editor);
205                         restore_string("lrdf_path", globals.lrdf_path);
206                         
207                         restore_int("fullscreen_enabled", globals.fullscreen_enabled);
208
209                         if (!elementFound) {
210                                 fprintf(stderr, "tX: Unhandled XML element: \"%s\"\n", cur->name);
211                         }
212                 }
213         }
214
215         xmlFreeDoc(doc);
216         
217         if (strcmp(device_type, "alsa")==0) globals.audiodevice_type=ALSA;
218         else if (strcmp(device_type, "jack")==0) globals.audiodevice_type=JACK;
219         else globals.audiodevice_type=OSS;
220         
221         return 0;
222 }
223
224 void store_globals() {
225         char rc_name[PATH_MAX]="";
226         char device_type[16];
227         char indent[]="\t";
228         FILE *rc;
229         char tmp_xml_buffer[4096];
230         
231         get_rc_name(rc_name);
232
233         rc=fopen(rc_name, "w");
234         
235         switch (globals.audiodevice_type) {
236                 case JACK:
237                         strcpy(device_type, "jack");
238                         break;
239                 case ALSA:
240                         strcpy(device_type, "alsa");
241                         break;
242                 case OSS:
243                 default:
244                         strcpy(device_type, "oss");
245                 
246         }
247         
248         if (rc) {               
249                 fprintf(rc, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n");
250                 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" );
251                 fprintf(rc, "<terminatorXrc version=\"%s\">\n", TX_XML_RC_VERSION);
252
253                 store_int("store_globals", globals.store_globals);
254
255                 store_string("audio_driver", device_type);
256                 
257                 store_string("oss_device", globals.oss_device);
258                 store_int("oss_buff_no", globals.oss_buff_no);
259                 store_int("oss_buff_size", globals.oss_buff_size);
260                 store_int("oss_samplerate", globals.oss_samplerate);
261
262                 store_string("alsa_device", globals.alsa_device);
263                 store_int("alsa_buffer_time", globals.alsa_buffer_time);
264                 store_int("alsa_period_time", globals.alsa_period_time);
265                 store_int("alsa_samplerate", globals.alsa_samplerate);          
266                 
267                 store_string("xinput_device", globals.xinput_device);
268                 store_int("xinput_enable", globals.xinput_enable);
269                 store_int("update_idle", globals.update_idle);
270                 store_int("update_delay", globals.update_delay);
271                 store_int("sense_cycles", globals.sense_cycles);
272                 store_float("mouse_speed", globals.mouse_speed);
273                 store_int("width", globals.width);
274                 store_int("height", globals.height);
275                 store_int("tooltips", globals.tooltips);
276                 store_int("use_stdout", globals.use_stdout);
277                 // globals.use_stdout_from_conf_file=0; What the heck is this?
278                 store_int("show_nag", globals.show_nag);
279                 store_int("prelis", globals.prelis);
280                 store_string("last_fn", globals.last_fn);
281                 store_float("pitch", globals.pitch);
282                 store_float("volume", globals.volume);
283                 store_float("flash_response", globals.flash_response);
284                 store_int("button_type", globals.button_type);
285                 store_string("tables_filename", globals.tables_filename);
286                 store_string("record_filename", globals.record_filename);
287                 store_string("file_editor", globals.file_editor);
288                 store_string("lrdf_path", globals.lrdf_path);
289                 store_int("fullscreen_enabled", globals.fullscreen_enabled);
290                 
291                 fprintf(rc,"</terminatorXrc>\n");
292         }
293 }
294
295 #ifdef ENABLE_TX_LEGACY
296 extern void load_globals_old();
297 #endif
298
299 void load_globals() {
300         set_global_defaults();
301
302         if (load_globals_xml()!=0) {
303                 fprintf(stderr, "tX: Failed loading terminatorXrc - trying to load old binary rc.\n");
304 #ifdef ENABLE_TX_LEGACY         
305                 load_globals_old();
306 #endif          
307         }
308 }
309
310 char *encode_xml(char *dest, const char *src) {
311         int i, t, max;
312         char tmp[4096];
313         
314         tmp[0]=0;
315         t=0;
316         max=strlen(src);
317         
318         for (i=0; i<max; i++) {
319                 switch (src[i]) {
320                         case '<': tmp[t]=0; strcat(tmp, "&lt;"); t+=4; break;
321                         case '>': tmp[t]=0; strcat(tmp, "&gt;"); t+=4; break;
322                         case '&': tmp[t]=0; strcat(tmp, "&amp;"); t+=5; break;
323                         case '"': tmp[t]=0; strcat(tmp, "&quot;"); t+=6; break;
324                         case '\'': strcat(tmp, "&apos;"); t+=7; break;
325                         default: tmp[t]=src[i]; t++;
326                 }
327         }
328         tmp[t]=0;
329
330         int outlen=4096;
331         int inlen=t;
332         
333         int res=isolat1ToUTF8((unsigned char *) dest, &outlen, (unsigned char *) tmp, &inlen);
334         dest[outlen]=0;
335         if (res<0) {
336                 tX_error("failed to encode string (%s) to UTF-8.", src);
337         }
338         
339         //tX_debug("encode_xml: from \"%s\" to \"%s\".", src, dest); 
340         return dest;
341 }
342
343 char *decode_xml(char *dest, const char *src) {
344         int inlen=strlen(src);
345         int outlen=4096;
346         
347         int res=UTF8Toisolat1(dest, &outlen, src, &inlen);
348         dest[outlen]=0;
349         
350         if (res<0) {
351                 tX_error("failed to decode UTF-8 string (%s).", src);
352         }
353         
354         return dest;
355 }
356