Merged Arthur's MIDI patch, starting on the menubar, gui fixes and
[terminatorX.git] / src / tX_global.c
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2002  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 <stdio.h>
33 #include <stdlib.h>
34 #include "tX_types.h"
35 #include "tX_global.h"
36 #include "string.h"
37 #include <libxml/xmlmemory.h>
38 #include <libxml/parser.h>
39
40 #define TX_XML_RC_VERSION "1.0"
41
42 tx_global globals;
43
44 void get_rc_name(char *buffer)
45 {
46         strcpy(buffer,"");
47         if (globals.alternate_rc)
48         {
49                 strcpy(buffer, globals.alternate_rc);
50         }
51         else 
52         {
53                 if (getenv("HOME"))
54                 {
55                         strcpy(buffer, getenv("HOME"));
56                         if (buffer[strlen(buffer)-1]!='/')
57                         strcat(buffer, "/");
58                 }
59                 strcat(buffer, ".terminatorXrc");
60         }
61 }
62
63 void set_global_defaults() {
64         globals.startup_set = 0;
65         globals.store_globals = 1;
66         globals.no_gui = 0;
67         globals.alternate_rc = 0;
68         
69         strcpy(globals.audio_device, "/dev/dsp");
70         
71         strcpy(globals.xinput_device, "");
72         globals.xinput_enable=0;
73         
74         globals.update_idle=18;
75         globals.update_delay=1;
76         
77         globals.buff_no=2;
78         globals.buff_size=9;
79         
80         globals.sense_cycles=12;
81         
82         globals.mouse_speed=0.8;
83         
84         globals.width=800;
85         globals.height=440;     
86         
87         globals.tooltips=1;
88         
89         globals.use_stdout=0;
90         globals.use_stdout_from_conf_file=0;
91         
92         globals.show_nag=1;
93         globals.prelis=1;
94         
95         strcpy(globals.last_fn,"");
96         
97         globals.pitch=1.0;
98         globals.volume=1.0;
99         globals.gui_wrap=3;
100         
101         globals.flash_response=0.95;
102         
103         globals.button_type=BUTTON_TYPE_BOTH;
104                 
105         globals.true_block_size=0;
106         
107         strcpy(globals.tables_filename, "");
108         strcpy(globals.record_filename, "tX_record.wav");
109         strcpy(globals.file_editor, "");
110                 
111 #ifdef USE_OSS
112         globals.audiodevice_type=TX_AUDIODEVICE_TYPE_OSS;               
113 #else
114 #ifdef USE_ALSA
115         globals.audiodevice_type=TX_AUDIODEVICE_TYPE_ALSA;
116 #endif  
117 #endif          
118         globals.audiodevice_buffersize=4096;
119         strcpy(globals.audiodevice_oss_devicename, "/dev/dsp");
120         globals.audiodevice_alsa_card=0;
121         globals.audiodevice_alsa_pcm=0;         
122
123         globals.use_stdout_cmdline=0;
124         globals.current_path = NULL;
125         globals.pitch=1.0;
126         globals.volume=1.0;
127         globals.fullscreen_enabled=1;
128         if (!globals.true_block_size) globals.true_block_size=1<globals.buff_size;
129
130 }
131
132 int load_globals_xml() {
133         char rc_name[PATH_MAX]="";      
134         xmlDocPtr doc;
135         xmlNodePtr cur;
136         int elementFound;
137         double dvalue;
138
139         get_rc_name(rc_name);
140         
141         doc = xmlParseFile(rc_name);
142         
143         if (doc == NULL ) {
144                 fprintf(stderr, "tX: err: Error parsing terminatorXrc.\n");
145                 return 1;
146         }
147         
148         cur = xmlDocGetRootElement(doc);
149         
150         if (cur == NULL) {
151                 fprintf(stderr,"tX: err: terminatorXrc contains no elements.\n");
152                 xmlFreeDoc(doc);
153                 return 2;
154         }
155         
156         if (xmlStrcmp(cur->name, (const xmlChar *) "terminatorXrc")) {
157                 fprintf(stderr,"tX: err: This terminatorXrc is not a terminatorXrc.");
158                 xmlFreeDoc(doc);
159                 return 3;
160         }
161         
162         for (cur=cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
163                 if (cur->type == XML_ELEMENT_NODE) {                    
164                         elementFound=0;
165
166                         restore_int("store_globals", globals.store_globals);
167                         restore_string("audio_device", globals.audio_device);
168                         restore_string("xinput_device", globals.xinput_device);
169                         restore_int("xinput_enable", globals.xinput_enable);
170                         restore_int("update_idle", globals.update_idle);
171                         restore_int("update_delay", globals.update_delay);
172                         restore_int("buff_no", globals.buff_no);
173                         restore_int("buff_size", globals.buff_size);
174                         restore_int("sense_cycles", globals.sense_cycles);
175                         restore_float("mouse_speed", globals.mouse_speed);
176                         restore_int("width", globals.width);
177                         restore_int("height", globals.height);
178                         restore_int("tooltips", globals.tooltips);
179                         restore_int("use_stdout", globals.use_stdout);
180                         restore_int("show_nag", globals.show_nag);
181                         restore_int("prelis", globals.prelis);
182                         restore_string("last_fn", globals.last_fn);
183                         restore_float("pitch", globals.pitch);
184                         restore_float("volume", globals.volume);
185                         restore_float("flash_response", globals.flash_response);
186                         restore_int("button_type", globals.button_type);
187                         restore_int("true_block_size", globals.true_block_size);
188                         restore_string("tables_filename", globals.tables_filename);
189                         restore_string("record_filename", globals.record_filename);
190                         restore_string("file_editor", globals.file_editor);
191                         restore_int("fullscreen_enabled", globals.fullscreen_enabled);
192
193                         if (!elementFound) {
194                                 fprintf(stderr, "tX: Unhandled XML element: \"%s\"\n", cur->name);
195                         }
196                 }
197         }
198
199         xmlFreeDoc(doc);
200         
201         return 0;
202 }
203
204 void store_globals() {
205         char rc_name[PATH_MAX]="";
206         char indent[]="\t";
207         FILE *rc;
208         char tmp_xml_buffer[4096];
209         
210         get_rc_name(rc_name);
211
212         rc=fopen(rc_name, "w");
213         
214         if (rc) {               
215                 fprintf(rc, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n\n");
216                 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" );
217                 fprintf(rc, "<terminatorXrc version=\"%s\">\n", TX_XML_RC_VERSION);
218
219                 store_int("store_globals", globals.store_globals);
220                 store_string("audio_device", globals.audio_device);
221                 store_string("xinput_device", globals.xinput_device);
222                 store_int("xinput_enable", globals.xinput_enable);
223                 store_int("update_idle", globals.update_idle);
224                 store_int("update_delay", globals.update_delay);
225                 store_int("buff_no", globals.buff_no);
226                 store_int("buff_size", globals.buff_size);
227                 store_int("sense_cycles", globals.sense_cycles);
228                 store_float("mouse_speed", globals.mouse_speed);
229                 store_int("width", globals.width);
230                 store_int("height", globals.height);
231                 store_int("tooltips", globals.tooltips);
232                 store_int("use_stdout", globals.use_stdout);
233                 // globals.use_stdout_from_conf_file=0; What the heck is this?
234                 store_int("show_nag", globals.show_nag);
235                 store_int("prelis", globals.prelis);
236                 store_string("last_fn", globals.last_fn);
237                 store_float("pitch", globals.pitch);
238                 store_float("volume", globals.volume);
239                 store_float("flash_response", globals.flash_response);
240                 store_int("button_type", globals.button_type);
241                 store_int("true_block_size", globals.true_block_size);
242                 store_string("tables_filename", globals.tables_filename);
243                 store_string("record_filename", globals.record_filename);
244                 store_string("file_editor", globals.file_editor);
245                 store_int("fullscreen_enabled", globals.fullscreen_enabled);
246                 
247                 fprintf(rc,"</terminatorXrc>\n");
248         }
249 }
250
251 #ifdef ENABLE_TX_LEGACY
252 extern void load_globals_old();
253 #endif
254
255 void load_globals() {
256         set_global_defaults();
257
258         if (load_globals_xml()!=0) {
259                 fprintf(stderr, "tX: Failed loading terminatorXrc - trying to load old binary rc.\n");
260 #ifdef ENABLE_TX_LEGACY         
261                 load_globals_old();
262 #endif          
263         }
264 }
265
266 char *encode_xml(char *dest, const char *src) {
267         int i, t, max;
268         
269         dest[0]=0;
270         t=0;
271         max=strlen(src);
272         
273         for (i=0; i<max; i++) {
274                 switch (src[i]) {
275                         case '<': dest[t]=0; strcat(dest, "&lt;"); t+=4; break;
276                         case '>': dest[t]=0; strcat(dest, "&gt;"); t+=4; break;
277                         case '&': dest[t]=0; strcat(dest, "&amp;"); t+=5; break;
278                         case '"': dest[t]=0; strcat(dest, "&quot;"); t+=6; break;
279                         case '\'': strcat(dest, "&apos;"); t+=7; break;
280                         default: dest[t]=src[i]; t++;
281                 }
282         }
283         dest[t]=0;
284         
285         tX_debug("encode_xml: from \"%s\" to \"%s\".", src, dest); 
286         return dest;
287 }