Fixing things - Alex
[terminatorX.git] / src / tX_global.c
index 515a4bd6fb1be96719855d77df222e95b3e7e8a8..de281dab24a8cc87a743bb1aacdff320b837121a 100644 (file)
@@ -1,6 +1,6 @@
 /*
     terminatorX - realtime audio scratching software
-    Copyright (C) 1999-2002  Alexander König
+    Copyright (C) 1999-2003  Alexander König
  
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -29,6 +29,7 @@
        14 Jul 2002: switched to libxml instead of binary saving.
 */    
 
+#include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "tX_types.h"
 #include "string.h"
 #include <libxml/xmlmemory.h>
 #include <libxml/parser.h>
+#include <libxml/encoding.h>
+
 
 #define TX_XML_RC_VERSION "1.0"
 
 tx_global globals;
-
-void get_rc_name_old(char *buffer)
-{
-       strcpy(buffer,"");
-
-       if (getenv("HOME"))
-       {
-               strcpy(buffer, getenv("HOME"));
-               if (buffer[strlen(buffer)-1]!='/')
-               strcat(buffer, "/");
-       }
-       
-       strcat(buffer, ".terminatorX3rc.bin");
-}
+int _store_compress_xml=0;
 
 void get_rc_name(char *buffer)
 {
@@ -80,18 +70,23 @@ void set_global_defaults() {
        globals.no_gui = 0;
        globals.alternate_rc = 0;
        
-       strcpy(globals.audio_device, "/dev/dsp");
-       
        strcpy(globals.xinput_device, "");
        globals.xinput_enable=0;
        
-       globals.update_idle=18;
+       globals.update_idle=14;
        globals.update_delay=1;
        
-       globals.buff_no=2;
-       globals.buff_size=9;
+       strcpy(globals.oss_device, "/dev/dsp");
+       globals.oss_buff_no=2;
+       globals.oss_buff_size=9;
+       globals.oss_samplerate=44100;
+
+       strcpy(globals.alsa_device, "00-00: Default");  
+       globals.alsa_buffer_time=10000;
+       globals.alsa_period_time=5000;
+       globals.alsa_samplerate=44100;
        
-       globals.sense_cycles=12;
+       globals.sense_cycles=80;
        
        globals.mouse_speed=0.8;
        
@@ -123,73 +118,31 @@ void set_global_defaults() {
        strcpy(globals.file_editor, "");
                
 #ifdef USE_OSS
-       globals.audiodevice_type=TX_AUDIODEVICE_TYPE_OSS;               
+       globals.audiodevice_type=OSS;           
 #else
 #ifdef USE_ALSA
-       globals.audiodevice_type=TX_AUDIODEVICE_TYPE_ALSA;
+       globals.audiodevice_type=ALSA;
 #endif 
 #endif         
-       globals.audiodevice_buffersize=4096;
-       strcpy(globals.audiodevice_oss_devicename, "/dev/dsp");
-       globals.audiodevice_alsa_card=0;
-       globals.audiodevice_alsa_pcm=0;         
-
        globals.use_stdout_cmdline=0;
        globals.current_path = NULL;
        globals.pitch=1.0;
-       globals.volume=1.0;     
-       if (!globals.true_block_size) globals.true_block_size=1<globals.buff_size;
-
-}
-
-void load_globals_old()
-{      
-       char rc_name[PATH_MAX]="";      
-       FILE *rc;
-       get_rc_name_old(rc_name);
+       globals.volume=1.0;
        
-       rc=fopen(rc_name, "r");
-       if (rc)
-       {
-               fread(&globals, sizeof(tx_global), 1, rc);
-               fclose(rc);
-       }
-       else
-       {
-               fprintf(stderr, "tX: .rc-file '%s' doesn't exist, reverting to defaults\n", rc_name);
-               set_global_defaults();
-       }
-
-       /* i'll have to keep these as they're in the code
-          everywhere but I think it doesn't make sense resetting
-         to old values on startup....
-       */
-       globals.use_stdout_cmdline=0;
-       globals.current_path = NULL;
-       globals.pitch=1.0;
-       globals.volume=1.0;     
-       if (!globals.true_block_size) globals.true_block_size=1<globals.buff_size;
-}
-
-void load_globals() {
-       if (load_globals_xml()!=0) {
-               fprintf(stderr, "tX: Failed loading terminatorXrc - trying to load old binary rc.\n");
-               load_globals_old();
-       }
+       strcpy(globals.lrdf_path, "/usr/share/ladspa/rdf:/usr/local/share/ladspa/rdf");
+       globals.fullscreen_enabled=1;
+       globals.compress_set_files=0;
 }
 
-#define restore_int(s, i); if ((!elementFound) && (!xmlStrcmp(cur->name, (const xmlChar *) s))) { elementFound=1; if (xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)) { sscanf(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1), "%i", &i); }}
-#define restore_float(s, i); if ((!elementFound) && (!xmlStrcmp(cur->name, (const xmlChar *) s))) { elementFound=1; if  (xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)) {sscanf(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1), "%lf", &dvalue); i=dvalue;}}
-#define restore_string(s, i); if ((!elementFound) && (!xmlStrcmp(cur->name, (const xmlChar *) s))) { elementFound=1; if (xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)) {strcpy(i, xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)); }}
-
 int load_globals_xml() {
        char rc_name[PATH_MAX]="";      
+       char device_type[16]="oss";
        xmlDocPtr doc;
        xmlNodePtr cur;
        int elementFound;
        double dvalue;
-
-       set_global_defaults();
+       char tmp_xml_buffer[4096];
+       
        get_rc_name(rc_name);
        
        doc = xmlParseFile(rc_name);
@@ -212,19 +165,29 @@ int load_globals_xml() {
                xmlFreeDoc(doc);
                return 3;
        }
-       
+               
        for (cur=cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
                if (cur->type == XML_ELEMENT_NODE) {                    
                        elementFound=0;
 
                        restore_int("store_globals", globals.store_globals);
-                       restore_string("audio_device", globals.audio_device);
+
+                       restore_string("audio_driver", device_type);
+                       
+                       restore_string("oss_device", globals.oss_device);
+                       restore_int("oss_buff_no", globals.oss_buff_no);
+                       restore_int("oss_buff_size", globals.oss_buff_size);
+                       restore_int("oss_samplerate", globals.oss_samplerate);
+
+                       restore_string("alsa_device", globals.alsa_device);
+                       restore_int("alsa_buffer_time", globals.alsa_buffer_time);
+                       restore_int("alsa_period_time", globals.alsa_period_time);
+                       restore_int("alsa_samplerate", globals.alsa_samplerate);
+
                        restore_string("xinput_device", globals.xinput_device);
                        restore_int("xinput_enable", globals.xinput_enable);
                        restore_int("update_idle", globals.update_idle);
                        restore_int("update_delay", globals.update_delay);
-                       restore_int("buff_no", globals.buff_no);
-                       restore_int("buff_size", globals.buff_size);
                        restore_int("sense_cycles", globals.sense_cycles);
                        restore_float("mouse_speed", globals.mouse_speed);
                        restore_int("width", globals.width);
@@ -238,45 +201,78 @@ int load_globals_xml() {
                        restore_float("volume", globals.volume);
                        restore_float("flash_response", globals.flash_response);
                        restore_int("button_type", globals.button_type);
-                       restore_int("true_block_size", globals.true_block_size);
                        restore_string("tables_filename", globals.tables_filename);
                        restore_string("record_filename", globals.record_filename);
                        restore_string("file_editor", globals.file_editor);
+                       restore_string("lrdf_path", globals.lrdf_path);
+                       
+                       restore_int("compress_set_files", globals.compress_set_files);
+                       restore_int("fullscreen_enabled", globals.fullscreen_enabled);
 
                        if (!elementFound) {
                                fprintf(stderr, "tX: Unhandled XML element: \"%s\"\n", cur->name);
                        }
                }
        }
+
+       xmlFreeDoc(doc);
+       
+       if (strcmp(device_type, "alsa")==0) globals.audiodevice_type=ALSA;
+       else if (strcmp(device_type, "jack")==0) globals.audiodevice_type=JACK;
+       else globals.audiodevice_type=OSS;
        
        return 0;
 }
 
-#define store_int(s, i); fprintf(rc, "\t<%s>%i</%s>\n", s,(int) i, s);
-#define store_float(s, i); fprintf(rc, "\t<%s>%lf</%s>\n", s,(double) i, s);
-#define store_string(s, i); fprintf(rc, "\t<%s>%s</%s>\n", s, i, s);
-
 void store_globals() {
        char rc_name[PATH_MAX]="";
+       char device_type[16];
+       char indent[]="\t";
        FILE *rc;
+       gzFile rz;
+       _store_compress_xml=0;
+       char tmp_xml_buffer[4096];
        
        get_rc_name(rc_name);
 
        rc=fopen(rc_name, "w");
        
+       switch (globals.audiodevice_type) {
+               case JACK:
+                       strcpy(device_type, "jack");
+                       break;
+               case ALSA:
+                       strcpy(device_type, "alsa");
+                       break;
+               case OSS:
+               default:
+                       strcpy(device_type, "oss");
+               
+       }
+       
        if (rc) {               
-               fprintf(rc, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n\n");
+               fprintf(rc, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n");
                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" );
                fprintf(rc, "<terminatorXrc version=\"%s\">\n", TX_XML_RC_VERSION);
 
                store_int("store_globals", globals.store_globals);
-               store_string("audio_device", globals.audio_device);
+
+               store_string("audio_driver", device_type);
+               
+               store_string("oss_device", globals.oss_device);
+               store_int("oss_buff_no", globals.oss_buff_no);
+               store_int("oss_buff_size", globals.oss_buff_size);
+               store_int("oss_samplerate", globals.oss_samplerate);
+
+               store_string("alsa_device", globals.alsa_device);
+               store_int("alsa_buffer_time", globals.alsa_buffer_time);
+               store_int("alsa_period_time", globals.alsa_period_time);
+               store_int("alsa_samplerate", globals.alsa_samplerate);          
+               
                store_string("xinput_device", globals.xinput_device);
                store_int("xinput_enable", globals.xinput_enable);
                store_int("update_idle", globals.update_idle);
                store_int("update_delay", globals.update_delay);
-               store_int("buff_no", globals.buff_no);
-               store_int("buff_size", globals.buff_size);
                store_int("sense_cycles", globals.sense_cycles);
                store_float("mouse_speed", globals.mouse_speed);
                store_int("width", globals.width);
@@ -291,11 +287,75 @@ void store_globals() {
                store_float("volume", globals.volume);
                store_float("flash_response", globals.flash_response);
                store_int("button_type", globals.button_type);
-               store_int("true_block_size", globals.true_block_size);
                store_string("tables_filename", globals.tables_filename);
                store_string("record_filename", globals.record_filename);
                store_string("file_editor", globals.file_editor);
+               store_string("lrdf_path", globals.lrdf_path);
+               store_int("compress_set_files", globals.compress_set_files);
+               store_int("fullscreen_enabled", globals.fullscreen_enabled);
                
                fprintf(rc,"</terminatorXrc>\n");
        }
 }
+
+#ifdef ENABLE_TX_LEGACY
+extern void load_globals_old();
+#endif
+
+void load_globals() {
+       set_global_defaults();
+
+       if (load_globals_xml()!=0) {
+               fprintf(stderr, "tX: Failed loading terminatorXrc - trying to load old binary rc.\n");
+#ifdef ENABLE_TX_LEGACY                
+               load_globals_old();
+#endif         
+       }
+}
+
+char *encode_xml(char *dest, const char *src) {
+       int i, t, max;
+       char tmp[4096];
+       
+       tmp[0]=0;
+       t=0;
+       max=strlen(src);
+       
+       for (i=0; i<max; i++) {
+               switch (src[i]) {
+                       case '<': tmp[t]=0; strcat(tmp, "&lt;"); t+=4; break;
+                       case '>': tmp[t]=0; strcat(tmp, "&gt;"); t+=4; break;
+                       case '&': tmp[t]=0; strcat(tmp, "&amp;"); t+=5; break;
+                       case '"': tmp[t]=0; strcat(tmp, "&quot;"); t+=6; break;
+                       case '\'': strcat(tmp, "&apos;"); t+=7; break;
+                       default: tmp[t]=src[i]; t++;
+               }
+       }
+       tmp[t]=0;
+
+       int outlen=4096;
+       int inlen=t;
+       
+       int res=isolat1ToUTF8((unsigned char *) dest, &outlen, (unsigned char *) tmp, &inlen);
+       dest[outlen]=0;
+       if (res<0) {
+               tX_error("failed to encode string (%s) to UTF-8.", src);
+       }
+       
+       //tX_debug("encode_xml: from \"%s\" to \"%s\".", src, dest); 
+       return dest;
+}
+
+char *decode_xml(char *dest, const char *src) {
+       int inlen=strlen(src);
+       int outlen=4096;
+       
+       int res=UTF8Toisolat1(dest, &outlen, src, &inlen);
+       dest[outlen]=0;
+       
+       if (res<0) {
+               tX_error("failed to decode UTF-8 string (%s).", src);
+       }
+       
+       return dest;
+}