5e33f2ef6fb40f9885aec3098fe8bfcf409488e3
[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 #ifdef USE_ALSA 
71         strcpy(globals.audio_device, "plughw:0,0");     
72 #else   
73         strcpy(globals.audio_device, "/dev/dsp");
74 #endif  
75         
76         strcpy(globals.xinput_device, "");
77         globals.xinput_enable=0;
78         
79         globals.update_idle=18;
80         globals.update_delay=1;
81         
82         globals.buff_no=2;
83         globals.buff_size=9;
84         
85         globals.sense_cycles=12;
86         
87         globals.mouse_speed=0.8;
88         
89         globals.width=800;
90         globals.height=440;     
91         
92         globals.tooltips=1;
93         
94         globals.use_stdout=0;
95         globals.use_stdout_from_conf_file=0;
96         
97         globals.show_nag=1;
98         globals.prelis=1;
99         
100         strcpy(globals.last_fn,"");
101         
102         globals.pitch=1.0;
103         globals.volume=1.0;
104         globals.gui_wrap=3;
105         
106         globals.flash_response=0.95;
107         
108         globals.button_type=BUTTON_TYPE_BOTH;
109                 
110         globals.true_block_size=0;
111         
112         strcpy(globals.tables_filename, "");
113         strcpy(globals.record_filename, "tX_record.wav");
114         strcpy(globals.file_editor, "");
115                 
116 #ifdef USE_OSS
117         globals.audiodevice_type=OSS;           
118 #else
119 #ifdef USE_ALSA
120         globals.audiodevice_type=ALSA;
121 #endif  
122 #endif          
123         globals.audiodevice_buffersize=4096;
124         strcpy(globals.audiodevice_oss_devicename, "/dev/dsp");
125         globals.audiodevice_alsa_card=0;
126         globals.audiodevice_alsa_pcm=0;         
127
128         globals.use_stdout_cmdline=0;
129         globals.current_path = NULL;
130         globals.pitch=1.0;
131         globals.volume=1.0;
132         globals.fullscreen_enabled=1;
133         if (!globals.true_block_size) globals.true_block_size=1<globals.buff_size;
134
135 }
136
137 int load_globals_xml() {
138         char rc_name[PATH_MAX]="";      
139         xmlDocPtr doc;
140         xmlNodePtr cur;
141         int elementFound;
142         double dvalue;
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                         restore_string("audio_device", globals.audio_device);
173                         restore_string("xinput_device", globals.xinput_device);
174                         restore_int("xinput_enable", globals.xinput_enable);
175                         restore_int("update_idle", globals.update_idle);
176                         restore_int("update_delay", globals.update_delay);
177                         restore_int("buff_no", globals.buff_no);
178                         restore_int("buff_size", globals.buff_size);
179                         restore_int("sense_cycles", globals.sense_cycles);
180                         restore_float("mouse_speed", globals.mouse_speed);
181                         restore_int("width", globals.width);
182                         restore_int("height", globals.height);
183                         restore_int("tooltips", globals.tooltips);
184                         restore_int("use_stdout", globals.use_stdout);
185                         restore_int("show_nag", globals.show_nag);
186                         restore_int("prelis", globals.prelis);
187                         restore_string("last_fn", globals.last_fn);
188                         restore_float("pitch", globals.pitch);
189                         restore_float("volume", globals.volume);
190                         restore_float("flash_response", globals.flash_response);
191                         restore_int("button_type", globals.button_type);
192                         restore_int("true_block_size", globals.true_block_size);
193                         restore_string("tables_filename", globals.tables_filename);
194                         restore_string("record_filename", globals.record_filename);
195                         restore_string("file_editor", globals.file_editor);
196                         restore_int("fullscreen_enabled", globals.fullscreen_enabled);
197
198                         if (!elementFound) {
199                                 fprintf(stderr, "tX: Unhandled XML element: \"%s\"\n", cur->name);
200                         }
201                 }
202         }
203
204         xmlFreeDoc(doc);
205         
206         return 0;
207 }
208
209 void store_globals() {
210         char rc_name[PATH_MAX]="";
211         char indent[]="\t";
212         FILE *rc;
213         char tmp_xml_buffer[4096];
214         
215         get_rc_name(rc_name);
216
217         rc=fopen(rc_name, "w");
218         
219         if (rc) {               
220                 fprintf(rc, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n\n");
221                 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" );
222                 fprintf(rc, "<terminatorXrc version=\"%s\">\n", TX_XML_RC_VERSION);
223
224                 store_int("store_globals", globals.store_globals);
225                 store_string("audio_device", globals.audio_device);
226                 store_string("xinput_device", globals.xinput_device);
227                 store_int("xinput_enable", globals.xinput_enable);
228                 store_int("update_idle", globals.update_idle);
229                 store_int("update_delay", globals.update_delay);
230                 store_int("buff_no", globals.buff_no);
231                 store_int("buff_size", globals.buff_size);
232                 store_int("sense_cycles", globals.sense_cycles);
233                 store_float("mouse_speed", globals.mouse_speed);
234                 store_int("width", globals.width);
235                 store_int("height", globals.height);
236                 store_int("tooltips", globals.tooltips);
237                 store_int("use_stdout", globals.use_stdout);
238                 // globals.use_stdout_from_conf_file=0; What the heck is this?
239                 store_int("show_nag", globals.show_nag);
240                 store_int("prelis", globals.prelis);
241                 store_string("last_fn", globals.last_fn);
242                 store_float("pitch", globals.pitch);
243                 store_float("volume", globals.volume);
244                 store_float("flash_response", globals.flash_response);
245                 store_int("button_type", globals.button_type);
246                 store_int("true_block_size", globals.true_block_size);
247                 store_string("tables_filename", globals.tables_filename);
248                 store_string("record_filename", globals.record_filename);
249                 store_string("file_editor", globals.file_editor);
250                 store_int("fullscreen_enabled", globals.fullscreen_enabled);
251                 
252                 fprintf(rc,"</terminatorXrc>\n");
253         }
254 }
255
256 #ifdef ENABLE_TX_LEGACY
257 extern void load_globals_old();
258 #endif
259
260 void load_globals() {
261         set_global_defaults();
262
263         if (load_globals_xml()!=0) {
264                 fprintf(stderr, "tX: Failed loading terminatorXrc - trying to load old binary rc.\n");
265 #ifdef ENABLE_TX_LEGACY         
266                 load_globals_old();
267 #endif          
268         }
269 }
270
271 char *encode_xml(char *dest, const char *src) {
272         int i, t, max;
273         
274         dest[0]=0;
275         t=0;
276         max=strlen(src);
277         
278         for (i=0; i<max; i++) {
279                 switch (src[i]) {
280                         case '<': dest[t]=0; strcat(dest, "&lt;"); t+=4; break;
281                         case '>': dest[t]=0; strcat(dest, "&gt;"); t+=4; break;
282                         case '&': dest[t]=0; strcat(dest, "&amp;"); t+=5; break;
283                         case '"': dest[t]=0; strcat(dest, "&quot;"); t+=6; break;
284                         case '\'': strcat(dest, "&apos;"); t+=7; break;
285                         default: dest[t]=src[i]; t++;
286                 }
287         }
288         dest[t]=0;
289         
290         tX_debug("encode_xml: from \"%s\" to \"%s\".", src, dest); 
291         return dest;
292 }