Verbose plugin loading, code and minor gui cleanups - Alex
[terminatorX.git] / src / tX_global.c
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2004  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 #ifdef USE_ALSA_MIDI_IN
49 extern void tX_midiin_store_connections(FILE *rc, char *indent);
50 extern void tX_midiin_restore_connections(xmlNodePtr node);
51 #endif
52
53 void get_rc_name(char *buffer)
54 {
55         strcpy(buffer,"");
56         if (globals.alternate_rc) {
57                 strcpy(buffer, globals.alternate_rc);
58         } else {
59                 if (getenv("HOME")) {
60                         strcpy(buffer, getenv("HOME"));
61                         if (buffer[strlen(buffer)-1]!='/')
62                         strcat(buffer, "/");
63                 }
64                 strcat(buffer, ".terminatorXrc");
65         }
66 }
67
68 void set_global_defaults() {
69         globals.startup_set = 0;
70         globals.store_globals = 1;
71         globals.no_gui = 0;
72         globals.alternate_rc = 0;
73         
74         strcpy(globals.xinput_device, "");
75         globals.xinput_enable=0;
76         
77         globals.update_idle=14;
78         globals.update_delay=1;
79         
80         strcpy(globals.oss_device, "/dev/dsp");
81         globals.oss_buff_no=2;
82         globals.oss_buff_size=9;
83         globals.oss_samplerate=44100;
84
85         strcpy(globals.alsa_device_id, "hw:0,0");       
86         globals.alsa_buffer_time=80000;
87         globals.alsa_period_time=20000;
88         globals.alsa_samplerate=44100;
89         
90         globals.sense_cycles=80;
91         
92         globals.mouse_speed=0.8;
93         
94         globals.width=800;
95         globals.height=440;     
96         
97         globals.tooltips=1;
98         
99         globals.use_stdout=0;
100         globals.use_stdout_from_conf_file=0;
101         
102         globals.show_nag=1;
103         globals.prelis=1;
104         
105         strcpy(globals.last_fn,"");
106         
107         globals.pitch=1.0;
108         globals.volume=1.0;
109         globals.gui_wrap=3;
110         
111         globals.flash_response=0.95;
112         
113         globals.button_type=BUTTON_TYPE_BOTH;
114                 
115         globals.true_block_size=0;
116         
117         strcpy(globals.tables_filename, "");
118         strcpy(globals.record_filename, "tX_record.wav");
119         strcpy(globals.file_editor, "");
120                 
121 #ifdef USE_OSS
122         globals.audiodevice_type=OSS;           
123 #else
124 #ifdef USE_ALSA
125         globals.audiodevice_type=ALSA;
126 #endif  
127 #endif          
128         globals.use_stdout_cmdline=0;
129         strcpy(globals.current_path, "");
130         strcpy(globals.lrdf_path, "/usr/share/ladspa/rdf:/usr/local/share/ladspa/rdf");
131         globals.fullscreen_enabled=0;
132         globals.confirm_events=0;
133         globals.compress_set_files=0;
134         
135         globals.vtt_inertia=10.0;
136         
137         globals.alsa_free_hwstats=0;
138         globals.filename_length=20;
139         globals.restore_midi_connections=1;
140         
141         strcpy(globals.wav_display_bg_focus, "#00004C");
142         strcpy(globals.wav_display_bg_no_focus, "#000000");
143
144         strcpy(globals.wav_display_fg_focus, "#FFFF00");
145         strcpy(globals.wav_display_fg_no_focus, "#00FF00");
146
147         strcpy(globals.wav_display_cursor, "#FF6666");
148         strcpy(globals.wav_display_cursor_mute, "#FFFFFF");     
149         
150         strcpy(globals.vu_meter_bg, "#000000");
151         strcpy(globals.vu_meter_loud, "#FF0000");
152         strcpy(globals.vu_meter_normal, "#00FF00");     
153         globals.vu_meter_border_intensity=0.7;
154         globals.quit_confirm=1;
155         globals.use_realtime=1;
156         globals.auto_assign_midi=0;
157         
158         globals.verbose_plugin_loading=0;
159         globals.force_nonrt_plugins=0;  
160 }
161
162 int load_globals_xml() {
163         char rc_name[PATH_MAX]="";      
164         char device_type[16]="oss";
165         xmlDocPtr doc;
166         xmlNodePtr cur;
167         int elementFound;
168         double dvalue;
169         char tmp_xml_buffer[4096];
170         
171         get_rc_name(rc_name);
172         
173         doc = xmlParseFile(rc_name);
174         
175         if (doc == NULL ) {
176                 fprintf(stderr, "tX: err: Error parsing terminatorXrc.\n");
177                 return 1;
178         }
179         
180         cur = xmlDocGetRootElement(doc);
181         
182         if (cur == NULL) {
183                 fprintf(stderr,"tX: err: terminatorXrc contains no elements.\n");
184                 xmlFreeDoc(doc);
185                 return 2;
186         }
187         
188         if (xmlStrcmp(cur->name, (const xmlChar *) "terminatorXrc")) {
189                 fprintf(stderr,"tX: err: This terminatorXrc is not a terminatorXrc.");
190                 xmlFreeDoc(doc);
191                 return 3;
192         }
193                 
194         for (cur=cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
195                 if (cur->type == XML_ELEMENT_NODE) {                    
196                         elementFound=0;
197
198                         restore_int("store_globals", globals.store_globals);
199
200                         restore_string("audio_driver", device_type);
201                         
202                         restore_string("oss_device", globals.oss_device);
203                         restore_int("oss_buff_no", globals.oss_buff_no);
204                         restore_int("oss_buff_size", globals.oss_buff_size);
205                         restore_int("oss_samplerate", globals.oss_samplerate);
206
207                         restore_string("alsa_device_id", globals.alsa_device_id);
208                         restore_int("alsa_buffer_time", globals.alsa_buffer_time);
209                         restore_int("alsa_period_time", globals.alsa_period_time);
210                         restore_int("alsa_samplerate", globals.alsa_samplerate);
211                         restore_int("alsa_free_hwstats", globals.alsa_free_hwstats);
212                         
213                         restore_string("xinput_device", globals.xinput_device);
214                         restore_int("xinput_enable", globals.xinput_enable);
215                         restore_int("update_idle", globals.update_idle);
216                         restore_int("update_delay", globals.update_delay);
217                         restore_int("sense_cycles", globals.sense_cycles);
218                         restore_float("mouse_speed", globals.mouse_speed);
219                         restore_int("width", globals.width);
220                         restore_int("height", globals.height);
221                         restore_int("filename_length", globals.filename_length);
222                         restore_int("tooltips", globals.tooltips);
223                         restore_int("use_stdout", globals.use_stdout);
224                         restore_int("show_nag", globals.show_nag);
225                         restore_int("prelis", globals.prelis);
226                         restore_string("last_fn", globals.last_fn);
227                         restore_float("pitch", globals.pitch);
228                         restore_float("volume", globals.volume);
229                         restore_float("flash_response", globals.flash_response);
230                         restore_int("button_type", globals.button_type);
231                         restore_string("tables_filename", globals.tables_filename);
232                         restore_string("record_filename", globals.record_filename);
233                         restore_string("file_editor", globals.file_editor);
234                         restore_string("lrdf_path", globals.lrdf_path);
235                         restore_string("last_path", globals.current_path);
236                         
237                         restore_int("compress_set_files", globals.compress_set_files);
238                         restore_int("fullscreen_enabled", globals.fullscreen_enabled);
239                         restore_int("confirm_events", globals.confirm_events);
240                         restore_float("vtt_inertia", globals.vtt_inertia);
241                         restore_int("restore_midi_connections", globals.restore_midi_connections);
242                         
243                         restore_string("wav_display_bg_focus", globals.wav_display_bg_focus);
244                         restore_string("wav_display_bg_no_focus", globals.wav_display_bg_no_focus);
245                         restore_string("wav_display_fg_focus", globals.wav_display_fg_focus);
246                         restore_string("wav_display_fg_no_focus", globals.wav_display_fg_no_focus);
247                         restore_string("wav_display_cursor", globals.wav_display_cursor);
248                         restore_string("wav_display_cursor_mute", globals.wav_display_cursor_mute);
249                         
250                         restore_string("vu_meter_bg", globals.vu_meter_bg);
251                         restore_string("vu_meter_normal", globals.vu_meter_normal);
252                         restore_string("vu_meter_loud", globals.vu_meter_loud);
253                         
254                         restore_float("vu_meter_border_intensity", globals.vu_meter_border_intensity);
255
256                         restore_int("quit_confirm", globals.quit_confirm);
257                         restore_int("use_realtime", globals.use_realtime);
258                         restore_int("auto_assign_midi", globals.auto_assign_midi);
259                         restore_int("force_nonrt_plugins", globals.force_nonrt_plugins);
260                         restore_int("verbose_plugin_loading", globals.verbose_plugin_loading);
261
262 #ifdef USE_ALSA_MIDI_IN
263                         if (!elementFound && (xmlStrcmp(cur->name, (xmlChar *) "midi_connections")==0)) {
264                                 if (globals.restore_midi_connections) {
265                                         tX_midiin_restore_connections(cur);
266                                 }
267                                 elementFound=1;
268                         }
269 #endif                  
270
271                         if (!elementFound) {
272                                 fprintf(stderr, "tX: Unhandled XML element: \"%s\"\n", cur->name);
273                         }
274                 }
275         }
276
277         xmlFreeDoc(doc);
278         
279         if (strcmp(device_type, "alsa")==0) globals.audiodevice_type=ALSA;
280         else if (strcmp(device_type, "jack")==0) globals.audiodevice_type=JACK;
281         else globals.audiodevice_type=OSS;
282         
283         return 0;
284 }
285
286 void store_globals() {
287         char rc_name[PATH_MAX+256]="";
288         char device_type[16];
289         char indent[32]="\t";
290         FILE *rc=NULL;
291         gzFile rz=NULL;
292         char tmp_xml_buffer[4096];
293         _store_compress_xml=0;
294         
295         get_rc_name(rc_name);
296
297         rc=fopen(rc_name, "w");
298         
299         switch (globals.audiodevice_type) {
300                 case JACK:
301                         strcpy(device_type, "jack");
302                         break;
303                 case ALSA:
304                         strcpy(device_type, "alsa");
305                         break;
306                 case OSS:
307                 default:
308                         strcpy(device_type, "oss");
309         }
310         
311         if (rc) {               
312                 fprintf(rc, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n");
313                 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" );
314                 fprintf(rc, "<terminatorXrc version=\"%s\">\n", TX_XML_RC_VERSION);
315
316                 store_int("store_globals", globals.store_globals);
317
318                 store_string("audio_driver", device_type);
319                 
320                 store_string("oss_device", globals.oss_device);
321                 store_int("oss_buff_no", globals.oss_buff_no);
322                 store_int("oss_buff_size", globals.oss_buff_size);
323                 store_int("oss_samplerate", globals.oss_samplerate);
324
325                 store_string("alsa_device_id", globals.alsa_device_id);
326                 store_int("alsa_buffer_time", globals.alsa_buffer_time);
327                 store_int("alsa_period_time", globals.alsa_period_time);
328                 store_int("alsa_samplerate", globals.alsa_samplerate);          
329                 store_int("alsa_free_hwstats", globals.alsa_free_hwstats);
330                 
331                 store_string("xinput_device", globals.xinput_device);
332                 store_int("xinput_enable", globals.xinput_enable);
333                 store_int("update_idle", globals.update_idle);
334                 store_int("update_delay", globals.update_delay);
335                 store_int("sense_cycles", globals.sense_cycles);
336                 store_float("mouse_speed", globals.mouse_speed);
337                 store_int("width", globals.width);
338                 store_int("height", globals.height);
339                 store_int("filename_length", globals.filename_length);
340                 store_int("tooltips", globals.tooltips);
341                 store_int("use_stdout", globals.use_stdout);
342                 // globals.use_stdout_from_conf_file=0; What the heck is this?
343                 store_int("show_nag", globals.show_nag);
344                 store_int("prelis", globals.prelis);
345                 store_string("last_fn", globals.last_fn);
346                 store_float("pitch", globals.pitch);
347                 store_float("volume", globals.volume);
348                 store_float("flash_response", globals.flash_response);
349                 store_int("button_type", globals.button_type);
350                 store_string("tables_filename", globals.tables_filename);
351                 store_string("record_filename", globals.record_filename);
352                 store_string("file_editor", globals.file_editor);
353                 store_string("lrdf_path", globals.lrdf_path);
354                 store_int("compress_set_files", globals.compress_set_files);
355                 store_int("fullscreen_enabled", globals.fullscreen_enabled);
356                 store_int("confirm_events", globals.confirm_events);
357                 store_float("vtt_inertia", globals.vtt_inertia);
358
359                 store_string("last_path", globals.current_path);
360                 store_int("restore_midi_connections", globals.restore_midi_connections);
361
362                 store_string("wav_display_bg_focus", globals.wav_display_bg_focus);
363                 store_string("wav_display_bg_no_focus", globals.wav_display_bg_no_focus);
364                 store_string("wav_display_fg_focus", globals.wav_display_fg_focus);
365                 store_string("wav_display_fg_no_focus", globals.wav_display_fg_no_focus);
366                 store_string("wav_display_cursor", globals.wav_display_cursor);
367                 store_string("wav_display_cursor_mute", globals.wav_display_cursor_mute);
368
369                 store_string("vu_meter_bg", globals.vu_meter_bg);
370                 store_string("vu_meter_normal", globals.vu_meter_normal);
371                 store_string("vu_meter_loud", globals.vu_meter_loud);
372                         
373                 store_float("vu_meter_border_intensity", globals.vu_meter_border_intensity);
374
375                 store_int("quit_confirm", globals.quit_confirm);
376                 store_int("use_realtime", globals.use_realtime);
377                 store_int("auto_assign_midi", globals.auto_assign_midi);
378                 
379                 store_int("verbose_plugin_loading", globals.verbose_plugin_loading);
380                 store_int("force_nonrt_plugins", globals.force_nonrt_plugins);
381
382 #ifdef USE_ALSA_MIDI_IN
383                 tX_midiin_store_connections(rc, indent);
384 #endif          
385
386                 fprintf(rc,"</terminatorXrc>\n");
387         }
388 }
389
390 #ifdef ENABLE_TX_LEGACY
391 extern void load_globals_old();
392 #endif
393
394 void load_globals() {
395         set_global_defaults();
396
397         if (load_globals_xml()!=0) {
398                 fprintf(stderr, "tX: Failed loading terminatorXrc - trying to load old binary rc.\n");
399 #ifdef ENABLE_TX_LEGACY         
400                 load_globals_old();
401 #endif          
402         }
403 }
404
405 char *encode_xml(char *dest, const char *src) {
406         int i, t, max;
407         char tmp[4096];
408         int outlen=4096;
409         int inlen;
410         int res;
411         
412         tmp[0]=0;
413         t=0;
414         max=strlen(src);
415         
416         for (i=0; i<max; i++) {
417                 switch (src[i]) {
418                         case '<': tmp[t]=0; strcat(tmp, "&lt;"); t+=4; break;
419                         case '>': tmp[t]=0; strcat(tmp, "&gt;"); t+=4; break;
420                         case '&': tmp[t]=0; strcat(tmp, "&amp;"); t+=5; break;
421                         case '"': tmp[t]=0; strcat(tmp, "&quot;"); t+=6; break;
422                         case '\'': strcat(tmp, "&apos;"); t+=7; break;
423                         default: tmp[t]=src[i]; t++;
424                 }
425         }
426         tmp[t]=0;
427
428         inlen=t;
429         res=isolat1ToUTF8((unsigned char *) dest, &outlen, (unsigned char *) tmp, &inlen);
430         dest[outlen]=0;
431         if (res<0) {
432                 tX_error("failed to encode string (%s) to UTF-8.", src);
433         }
434         
435         return dest;
436 }
437
438 char *decode_xml(char *dest, const char *src) {
439         int inlen=strlen(src);
440         int outlen=4096;
441         
442         int res=UTF8Toisolat1(dest, &outlen, src, &inlen);
443         dest[outlen]=0;
444         
445         if (res<0) {
446                 tX_error("failed to decode UTF-8 string (%s).", src);
447         }
448         
449         return dest;
450 }