Counters should be unsigned.
[terminatorX.git] / src / tX_global.c
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2016  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, see <http://www.gnu.org/licenses/>.
17  
18     File: tX_global.c
19  
20     Description:  This file contains the routines for handling the
21                   "globals" block. Intializing, reading setup from
22                   disk and storing it.
23 */    
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include "tX_types.h"
29 #include "tX_global.h"
30 #include "string.h"
31 #include <libxml/xmlmemory.h>
32 #include <libxml/parser.h>
33 #include <libxml/encoding.h>
34
35
36 #define TX_XML_RC_VERSION "1.0"
37
38 tx_global globals;
39 int _store_compress_xml=0;
40
41 #ifdef USE_ALSA_MIDI_IN
42 extern void tX_midiin_store_connections(FILE *rc, char *indent);
43 extern void tX_midiin_restore_connections(xmlNodePtr node);
44 #endif
45
46 void get_rc_name(char *buffer, int length)
47 {
48         strncpy(buffer,"", length);
49         if (globals.alternate_rc) {
50                 strncpy(buffer, globals.alternate_rc, length);
51         } else {
52                 if (getenv("HOME")) {
53                         strncpy(buffer, getenv("HOME"), length-16);
54                         if (buffer[strlen(buffer)-1]!='/')
55                         strcat(buffer, "/");
56                 }
57                 strcat(buffer, ".terminatorXrc");
58         }
59 }
60
61 void set_global_defaults() {
62         globals.startup_set = 0;
63         globals.store_globals = 1;
64         globals.no_gui = 0;
65         globals.alternate_rc = 0;
66         
67         strcpy(globals.xinput_device, "");
68         globals.xinput_enable=0;
69         
70         globals.update_idle=14;
71         globals.update_delay=1;
72         
73         strcpy(globals.oss_device, "/dev/dsp");
74         globals.oss_buff_no=2;
75         globals.oss_buff_size=9;
76         globals.oss_samplerate=44100;
77
78         strcpy(globals.alsa_device_id, "hw:0,0");       
79         globals.alsa_buffer_time=80000;
80         globals.alsa_period_time=20000;
81         globals.alsa_samplerate=44100;
82
83         globals.pulse_buffer_length=320;
84
85         globals.sense_cycles=80;
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         globals.knob_size_override=0;
108         
109         globals.button_type=BUTTON_TYPE_BOTH;
110                 
111         globals.true_block_size=0;
112         
113         strcpy(globals.tables_filename, "");
114         strcpy(globals.record_filename, "tX_record.wav");
115         strcpy(globals.file_editor, "");
116
117 #ifdef USE_PULSE
118         globals.audiodevice_type=ALSA;
119 #else   
120 #ifdef USE_ALSA
121         globals.audiodevice_type=ALSA;
122 #else
123 #ifdef USE_OSS
124         globals.audiodevice_type=OSS;
125 #endif  
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, sizeof(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_int("pulse_buffer_length", globals.pulse_buffer_length);
214
215                         restore_string("xinput_device", globals.xinput_device);
216                         restore_int("xinput_enable", globals.xinput_enable);
217                         restore_int("update_idle", globals.update_idle);
218                         restore_int("update_delay", globals.update_delay);
219                         restore_int("sense_cycles", globals.sense_cycles);
220                         restore_float("mouse_speed", globals.mouse_speed);
221                         restore_int("width", globals.width);
222                         restore_int("height", globals.height);
223                         restore_int("filename_length", globals.filename_length);
224                         restore_int("tooltips", globals.tooltips);
225                         restore_int("use_stdout", globals.use_stdout);
226                         restore_int("show_nag", globals.show_nag);
227                         restore_int("prelis", globals.prelis);
228                         restore_string("last_fn", globals.last_fn);
229                         restore_float("pitch", globals.pitch);
230                         restore_float("volume", globals.volume);
231                         restore_float("flash_response", globals.flash_response);
232                         restore_int("knob_size_override", globals.knob_size_override);
233                         restore_int("button_type", globals.button_type);
234                         restore_string("tables_filename", globals.tables_filename);
235                         restore_string("record_filename", globals.record_filename);
236                         restore_string("file_editor", globals.file_editor);
237                         restore_string("lrdf_path", globals.lrdf_path);
238                         restore_string("last_path", globals.current_path);
239                         
240                         restore_int("compress_set_files", globals.compress_set_files);
241                         restore_int("fullscreen_enabled", globals.fullscreen_enabled);
242                         restore_int("confirm_events", globals.confirm_events);
243                         restore_float("vtt_inertia", globals.vtt_inertia);
244                         restore_int("restore_midi_connections", globals.restore_midi_connections);
245                         
246                         restore_string("wav_display_bg_focus", globals.wav_display_bg_focus);
247                         restore_string("wav_display_bg_no_focus", globals.wav_display_bg_no_focus);
248                         restore_string("wav_display_fg_focus", globals.wav_display_fg_focus);
249                         restore_string("wav_display_fg_no_focus", globals.wav_display_fg_no_focus);
250                         restore_string("wav_display_cursor", globals.wav_display_cursor);
251                         restore_string("wav_display_cursor_mute", globals.wav_display_cursor_mute);
252                         
253                         restore_string("vu_meter_bg", globals.vu_meter_bg);
254                         restore_string("vu_meter_normal", globals.vu_meter_normal);
255                         restore_string("vu_meter_loud", globals.vu_meter_loud);
256                         
257                         restore_float("vu_meter_border_intensity", globals.vu_meter_border_intensity);
258
259                         restore_int("quit_confirm", globals.quit_confirm);
260                         restore_int("use_realtime", globals.use_realtime);
261                         restore_int("auto_assign_midi", globals.auto_assign_midi);
262                         restore_int("force_nonrt_plugins", globals.force_nonrt_plugins);
263                         restore_int("verbose_plugin_loading", globals.verbose_plugin_loading);
264
265 #ifdef USE_ALSA_MIDI_IN
266                         if (!elementFound && (xmlStrcmp(cur->name, (xmlChar *) "midi_connections")==0)) {
267                                 if (globals.restore_midi_connections) {
268                                         tX_midiin_restore_connections(cur);
269                                 }
270                                 elementFound=1;
271                         }
272 #endif                  
273
274                         if (!elementFound) {
275                                 fprintf(stderr, "tX: Unhandled XML element: \"%s\"\n", cur->name);
276                         }
277                 }
278         }
279
280         xmlFreeDoc(doc);
281         
282         if (strcmp(device_type, "alsa")==0) { globals.audiodevice_type=ALSA; }
283         else if (strcmp(device_type, "jack")==0) { globals.audiodevice_type=JACK; }
284         else if (strcmp(device_type, "pulse")==0) { globals.audiodevice_type=PULSE; }
285         else globals.audiodevice_type=OSS;
286         
287         return 0;
288 }
289
290 void store_globals() {
291         char rc_name[PATH_MAX+256]="";
292         char device_type[16];
293         char indent[32]="\t";
294         FILE *rc=NULL;
295         gzFile rz=NULL;
296         char tmp_xml_buffer[4096];
297         _store_compress_xml=0;
298         
299         get_rc_name(rc_name, sizeof(rc_name));
300
301         rc=fopen(rc_name, "w");
302         
303         switch (globals.audiodevice_type) {
304                 case PULSE:
305                         strcpy(device_type, "pulse");
306                         break;
307                 case JACK:
308                         strcpy(device_type, "jack");
309                         break;
310                 case ALSA:
311                         strcpy(device_type, "alsa");
312                         break;
313                 case OSS:
314                 default:
315                         strcpy(device_type, "oss");
316         }
317         
318         if (rc) {               
319                 fprintf(rc, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n");
320                 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" );
321                 fprintf(rc, "<terminatorXrc version=\"%s\">\n", TX_XML_RC_VERSION);
322
323                 store_int("store_globals", globals.store_globals);
324
325                 store_string("audio_driver", device_type);
326                 
327                 store_string("oss_device", globals.oss_device);
328                 store_int("oss_buff_no", globals.oss_buff_no);
329                 store_int("oss_buff_size", globals.oss_buff_size);
330                 store_int("oss_samplerate", globals.oss_samplerate);
331
332                 store_string("alsa_device_id", globals.alsa_device_id);
333                 store_int("alsa_buffer_time", globals.alsa_buffer_time);
334                 store_int("alsa_period_time", globals.alsa_period_time);
335                 store_int("alsa_samplerate", globals.alsa_samplerate);          
336                 store_int("alsa_free_hwstats", globals.alsa_free_hwstats);
337                 
338                 store_int("pulse_buffer_length", globals.pulse_buffer_length);
339
340                 store_string("xinput_device", globals.xinput_device);
341                 store_int("xinput_enable", globals.xinput_enable);
342                 store_int("update_idle", globals.update_idle);
343                 store_int("update_delay", globals.update_delay);
344                 store_int("sense_cycles", globals.sense_cycles);
345                 store_float("mouse_speed", globals.mouse_speed);
346                 store_int("width", globals.width);
347                 store_int("height", globals.height);
348                 store_int("filename_length", globals.filename_length);
349                 store_int("tooltips", globals.tooltips);
350                 store_int("use_stdout", globals.use_stdout);
351                 // globals.use_stdout_from_conf_file=0; What the heck is this?
352                 store_int("show_nag", globals.show_nag);
353                 store_int("prelis", globals.prelis);
354                 store_string("last_fn", globals.last_fn);
355                 store_float("pitch", globals.pitch);
356                 store_float("volume", globals.volume);
357                 store_float("flash_response", globals.flash_response);
358                 store_int("knob_size_override", globals.knob_size_override);
359                 store_int("button_type", globals.button_type);
360                 store_string("tables_filename", globals.tables_filename);
361                 store_string("record_filename", globals.record_filename);
362                 store_string("file_editor", globals.file_editor);
363                 store_string("lrdf_path", globals.lrdf_path);
364                 store_int("compress_set_files", globals.compress_set_files);
365                 store_int("fullscreen_enabled", globals.fullscreen_enabled);
366                 store_int("confirm_events", globals.confirm_events);
367                 store_float("vtt_inertia", globals.vtt_inertia);
368
369                 store_string("last_path", globals.current_path);
370                 store_int("restore_midi_connections", globals.restore_midi_connections);
371
372                 store_string("wav_display_bg_focus", globals.wav_display_bg_focus);
373                 store_string("wav_display_bg_no_focus", globals.wav_display_bg_no_focus);
374                 store_string("wav_display_fg_focus", globals.wav_display_fg_focus);
375                 store_string("wav_display_fg_no_focus", globals.wav_display_fg_no_focus);
376                 store_string("wav_display_cursor", globals.wav_display_cursor);
377                 store_string("wav_display_cursor_mute", globals.wav_display_cursor_mute);
378
379                 store_string("vu_meter_bg", globals.vu_meter_bg);
380                 store_string("vu_meter_normal", globals.vu_meter_normal);
381                 store_string("vu_meter_loud", globals.vu_meter_loud);
382                         
383                 store_float("vu_meter_border_intensity", globals.vu_meter_border_intensity);
384
385                 store_int("quit_confirm", globals.quit_confirm);
386                 store_int("use_realtime", globals.use_realtime);
387                 store_int("auto_assign_midi", globals.auto_assign_midi);
388                 
389                 store_int("verbose_plugin_loading", globals.verbose_plugin_loading);
390                 store_int("force_nonrt_plugins", globals.force_nonrt_plugins);
391
392 #ifdef USE_ALSA_MIDI_IN
393                 tX_midiin_store_connections(rc, indent);
394 #endif          
395
396                 fprintf(rc,"</terminatorXrc>\n");
397         }
398 }
399
400 #ifdef ENABLE_TX_LEGACY
401 extern void load_globals_old();
402 #endif
403
404 void load_globals() {
405         set_global_defaults();
406
407         if (load_globals_xml()!=0) {
408                 fprintf(stderr, "tX: Failed loading terminatorXrc - trying to load old binary rc.\n");
409 #ifdef ENABLE_TX_LEGACY         
410                 load_globals_old();
411 #endif          
412         }
413 }
414
415 char *encode_xml(char *dest, const char *src) {
416         int i, t, max;
417         char tmp[4096];
418         int outlen=4096;
419         int inlen;
420         int res;
421         
422         tmp[0]=0;
423         t=0;
424         max=strlen(src);
425         
426         for (i=0; i<max; i++) {
427                 switch (src[i]) {
428                         case '<': tmp[t]=0; strcat(tmp, "&lt;"); t+=4; break;
429                         case '>': tmp[t]=0; strcat(tmp, "&gt;"); t+=4; break;
430                         case '&': tmp[t]=0; strcat(tmp, "&amp;"); t+=5; break;
431                         case '"': tmp[t]=0; strcat(tmp, "&quot;"); t+=6; break;
432                         case '\'': strcat(tmp, "&apos;"); t+=7; break;
433                         default: tmp[t]=src[i]; t++;
434                 }
435         }
436         tmp[t]=0;
437
438         inlen=t;
439         res=isolat1ToUTF8((unsigned char *) dest, &outlen, (unsigned char *) tmp, &inlen);
440         dest[outlen]=0;
441         if (res<0) {
442                 tX_error("failed to encode string (%s) to UTF-8.", src);
443         }
444         
445         return dest;
446 }
447
448 char *decode_xml(char *dest, const char *src) {
449         int inlen=strlen(src);
450         int outlen=4096;
451         
452         int res=UTF8Toisolat1((unsigned char *) dest, &outlen, (const unsigned char *) src, &inlen);
453         dest[outlen]=0;
454         
455         if (res<0) {
456                 tX_error("failed to decode UTF-8 string (%s).", src);
457         }
458         
459         return dest;
460 }