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