Mostly MIDI fixes - Alex
[terminatorX.git] / terminatorX / src / tX_mastergui.cc
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_mastergui.cc
20  
21     Description: This implements the main (aka master) gtk+ GUI of terminatorX
22                  It serves as a container for the vtt-guis.
23 */    
24
25 #include <gtk/gtk.h>
26 #include <gdk/gdkx.h>
27 #include <X11/Xlib.h>
28 #include <math.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <gdk/gdkkeysyms.h>
32 #include "version.h"
33 #include "tX_global.h"
34 #include "tX_engine.h"
35 #include "tX_vttgui.h"
36 #include "tX_vtt.h"
37 #include "tX_flash.h"
38 #include "tX_dialog.h"
39 #include "tX_loaddlg.h"
40 #include "tX_seqpar.h"
41 #include "tX_pbutton.h"
42 #include "tX_sequencer.h"
43 #include "tX_mastergui.h"
44 #include "tX_knobloader.h"
45 #include <sys/time.h>
46 #include <sys/wait.h>
47
48 #ifdef USE_SCHEDULER
49 #include <sys/resource.h>
50 #endif
51
52 #define TX_SET_ID_10 "terminatorX turntable set file - version 1.0 - data:"
53 #define TX_SET_ID_11 "terminatorX turntable set file - version 1.1 - data:"
54 #define TX_SET_ID_12 "terminatorX turntable set file - version 1.2 - data:"
55 #define TX_SET_ID_13 "terminatorX turntable set file - version 1.3 - data:"
56 #define TX_SET_ID_14 "terminatorX turntable set file - version 1.4 - data:"
57
58 int audioon=0;
59 int sequencer_ready=1;
60
61 GtkWidget *tt_parent;
62 GtkWidget *control_parent;
63 GtkWidget *audio_parent;
64 GtkWidget *main_window;
65 GtkWidget *wav_progress;
66 GtkWidget *grab_button;
67 GtkWidget *main_flash_l;
68 GtkWidget *main_flash_r;
69
70 GtkWidget *seq_rec_btn;
71 GtkWidget *seq_play_btn;
72 GtkWidget *seq_stop_btn;
73 GtkAdjustment *seq_adj;
74 GtkWidget *seq_slider;
75 GtkWidget *seq_entry;
76 GtkWidget *panel_bar;
77
78 int buttons_on_panel_bar=0;
79
80 int seq_adj_care=1;
81 int seq_stop_override=0;
82
83 GtkAdjustment *volume_adj;
84 GtkAdjustment *pitch_adj;
85
86 /* seq-pars */
87 tX_seqpar_master_volume sp_master_volume;
88 tX_seqpar_master_pitch sp_master_pitch;
89
90 GtkWidget *engine_btn;
91
92 GtkWidget *main_menubar;
93 GtkWidget *rec_menu_item;
94 GtkWidget *fullscreen_item;
95
96 int rec_dont_care=0;
97 gint update_tag;
98
99 #define connect_entry(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "activate", (GtkSignalFunc) func, (void *) ptr);
100 #define connect_adj(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "value_changed", (GtkSignalFunc) func, (void *) ptr);
101 #define connect_button(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "clicked", (GtkSignalFunc) func, (void *) ptr);
102
103 Window xwindow;
104 #define WID_DYN TRUE, TRUE, 0
105 #define WID_FIX FALSE, FALSE, 0
106 extern void add_vtt(GtkWidget *ctrl, GtkWidget *audio, char *fn);
107 extern void destroy_gui(vtt_class *vtt);
108 extern void gui_show_frame(vtt_class *vtt, int show);
109
110 GdkWindow *save_dialog_win=NULL;
111 GdkWindow *load_dialog_win=NULL;
112 GtkWidget *save_dialog=NULL;
113 GtkWidget *load_dialog=NULL;
114
115 GdkWindow *rec_dialog_win=NULL;
116 GtkWidget *rec_dialog=NULL;
117
118 GtkWidget *no_of_vtts=NULL;
119 GtkWidget *used_mem=NULL;
120
121 int stop_update=0;
122 int update_delay;
123
124 vtt_class *old_focus=NULL;
125
126 int grab_status=0;
127 int last_grab_status=0;
128
129 GtkTooltips *gui_tooltips=NULL;
130
131 void gui_set_tooltip(GtkWidget *wid, char *tip)
132 {
133         gtk_tooltips_set_tip(gui_tooltips, wid, tip, NULL);
134 }
135
136 void turn_audio_off(void)
137 {
138         if (audioon) 
139         {
140                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0);
141                 while (gtk_events_pending()) gtk_main_iteration();              
142         }
143 }
144
145
146 gint pos_update(gpointer data)
147 {
148         f_prec temp;
149
150         if (stop_update) 
151         {               
152                 cleanup_all_vtts();
153                 tX_seqpar :: update_all_graphics();
154                 if (old_focus) gui_show_frame(old_focus, 0);
155                 old_focus=NULL;
156                 gtk_tx_flash_clear(main_flash_l);
157                 gtk_tx_flash_clear(main_flash_r);
158                 gdk_flush();    
159                 update_tag=0;
160                 return(FALSE);
161         }
162         else
163         {
164                 update_all_vtts();
165                 
166                 /*left vu meter */
167                 temp=vtt_class::mix_max_l;
168                 vtt_class::mix_max_l=0;
169                 gtk_tx_flash_set_level(main_flash_l, temp);
170
171                 /*right vu meter */
172                 temp=vtt_class::mix_max_r;
173                 vtt_class::mix_max_r=0;
174                 gtk_tx_flash_set_level(main_flash_r, temp);
175                 
176                 if (vtt_class::focused_vtt!=old_focus)
177                 {
178                         if (old_focus) gui_show_frame(old_focus, 0);
179                         old_focus=vtt_class::focused_vtt;
180                         if (old_focus) gui_show_frame(old_focus, 1);                    
181                 }
182                 if (grab_status!=last_grab_status)
183                 {
184                         last_grab_status=grab_status;
185                         if (!grab_status) 
186                         {
187                                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(grab_button), 0);
188                         }
189                 }
190                 gdk_flush();    
191                 update_delay--;
192                 
193                 if (update_delay < 0)
194                 {
195                         seq_update();
196                         tX_seqpar :: update_all_graphics();
197                         update_delay=globals.update_delay;
198                 }
199                 return(TRUE);
200         }
201 }
202
203 void mg_update_status()
204 {
205         FILE *procfs;
206         pid_t mypid;
207         char filename[PATH_MAX];
208         char buffer[256];
209         int found=0;    
210         int mem;
211         
212         mypid=getpid();
213         sprintf(filename, "/proc/%i/status", mypid);
214         procfs=fopen(filename, "r");
215         if (procfs)
216         {
217                 while((!feof(procfs)) && !found)
218                 {
219                         fgets(buffer, 256, procfs);
220                         if (strncmp("VmSize:", buffer, 7)==0)
221                         {
222                                 found=1;
223                                 sscanf(buffer, "VmSize: %i kB", &mem);
224                                 sprintf(buffer, "%i", mem);
225                                 gtk_label_set(GTK_LABEL(used_mem), buffer);
226                         }
227                 }
228         }
229         fclose(procfs); 
230         
231         sprintf(buffer, "%i", vtt_class::vtt_amount);
232         gtk_label_set(GTK_LABEL(no_of_vtts), buffer);
233 }
234
235 GtkSignalFunc new_table(GtkWidget *, char *fn)
236 {
237         turn_audio_off();
238                 
239                 if (fn) 
240                 {
241                         ld_create_loaddlg(TX_LOADDLG_MODE_SINGLE, 1);
242                         ld_set_filename(fn);
243                 }
244                 
245                 add_vtt(control_parent, audio_parent, fn);                              
246                 
247                 if (fn) ld_destroy();           
248         mg_update_status();
249         return NULL;
250 }
251
252 GtkSignalFunc drop_new_table(GtkWidget *widget, GdkDragContext *context,
253                 gint x, gint y, GtkSelectionData *selection_data,
254                 guint info, guint time, vtt_class *vtt)
255 {
256         char filename[PATH_MAX];
257         char *fn;
258         
259         strncpy(filename, (char *) selection_data->data, (size_t) selection_data->length);
260         filename[selection_data->length]=0;
261
262         fn = strchr (filename, '\r');
263         *fn=0;  
264         
265         fn = strchr (filename, ':');
266         if (fn) fn++; else fn=(char *) selection_data->data;
267         
268         new_table(NULL, fn);
269
270         strcpy (filename, "dont segfault workaround ;)");
271         return NULL;
272 }
273
274
275 /* Loading saved setups */
276
277 GtkSignalFunc cancel_load_tables(GtkWidget *wid)
278 {
279         gtk_widget_destroy(load_dialog);
280         load_dialog=NULL;
281         load_dialog_win=NULL;
282         return(0);
283 }
284
285 void load_tt_part(char * buffer)
286 {
287         FILE *in;
288         char idbuff[256];
289         char wbuf[PATH_MAX];
290         xmlDocPtr doc;
291         
292         turn_audio_off();
293         
294         strcpy(globals.tables_filename, buffer);
295         
296         doc = xmlParseFile(buffer);
297         if (doc) {
298                 vtt_class::load_all(doc, buffer);
299                 xmlFreeDoc(doc);
300         } 
301         
302 #ifdef ENABLE_TX_LEGACY
303         else {  
304                 in=fopen(buffer, "r");  
305         
306                 if (in) {
307                         fread(idbuff, strlen(TX_SET_ID_10), 1, in);
308                         if (strncmp(idbuff, TX_SET_ID_10, strlen(TX_SET_ID_10))==0) {
309                                 if (vtt_class::load_all_10(in, buffer)) tx_note("Error while reading set.", true);
310                         } else if (strncmp(idbuff, TX_SET_ID_11, strlen(TX_SET_ID_11))==0)      {
311                                 if (vtt_class::load_all_11(in, buffer)) tx_note("Error while reading set.", true);                      
312                         } else if (strncmp(idbuff, TX_SET_ID_12, strlen(TX_SET_ID_12))==0) {
313                                 if (vtt_class::load_all_12(in, buffer)) tx_note("Error while reading set.", true);                      
314                         } else if (strncmp(idbuff, TX_SET_ID_13, strlen(TX_SET_ID_13))==0) {
315                                 if (vtt_class::load_all_13(in, buffer)) tx_note("Error while reading set.", true);                      
316                         } else if (strncmp(idbuff, TX_SET_ID_14, strlen(TX_SET_ID_14))==0) {
317                                 if (vtt_class::load_all_14(in, buffer)) tx_note("Error while reading set.", true);                      
318                         }       else {
319                                 tx_note("This file is not a terminatorX set-file.", true);
320                                 fclose(in);
321                                 return;
322                         }
323                         fclose(in);     
324                 } else {
325                         strcpy(idbuff, "Failed to access file: \"");    // I'm stealing the unrelated sting for a temp :)
326                         strcat(idbuff, globals.tables_filename);
327                         strcat(idbuff, "\"");
328                         tx_note(idbuff, true);
329                         
330                         return;
331                 }
332         }
333 #else
334         else {
335                 strcpy(idbuff, "Failed to access file: \"");    // I'm stealing the unrelated sting for a temp :)
336                 strcat(idbuff, globals.tables_filename);
337                 strcat(idbuff, "\"");
338                 tx_note(idbuff, true);
339                 
340                 return;
341         }
342 #endif  
343         
344         tX_seqpar :: init_all_graphics();
345         vg_init_all_non_seqpars();
346                 
347         gtk_adjustment_set_value(volume_adj, 2.0-globals.volume);
348         gtk_adjustment_set_value(pitch_adj, globals.pitch);
349         sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
350         gtk_window_set_title(GTK_WINDOW(main_window), wbuf);            
351 }
352
353 void do_load_tables(GtkWidget *wid)
354 {
355         char buffer[PATH_MAX];
356         
357         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(load_dialog)));
358         
359         gtk_widget_destroy(load_dialog);
360         
361         load_dialog=NULL;
362         load_dialog_win=NULL;
363
364         load_tt_part(buffer);   
365 }
366
367 GtkSignalFunc load_tables()
368 {
369         if (load_dialog_win) 
370         {
371                 gdk_window_raise(load_dialog_win);
372                 return 0;
373         }
374         
375         load_dialog=gtk_file_selection_new("Load Set"); 
376         
377         gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(load_dialog));
378         gtk_file_selection_complete(GTK_FILE_SELECTION(load_dialog), "*.tX");
379         
380         if (strlen(globals.tables_filename))
381         {
382                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(load_dialog), globals.tables_filename);
383         }
384         
385         gtk_widget_show(load_dialog);
386         
387         load_dialog_win=load_dialog->window;
388         
389         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(load_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_load_tables), NULL);
390         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(load_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_load_tables), NULL); 
391         gtk_signal_connect (GTK_OBJECT(load_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_load_tables), NULL);        
392         
393         return NULL;
394 }
395
396 GtkSignalFunc drop_set(GtkWidget *widget, GdkDragContext *context,
397                 gint x, gint y, GtkSelectionData *selection_data,
398                 guint info, guint time, vtt_class *vtt)
399 {
400         char filename[PATH_MAX];
401         char *fn;
402         
403         strncpy(filename, (char *) selection_data->data, (size_t) selection_data->length);
404         filename[selection_data->length]=0;
405
406         fn = strchr (filename, '\r');
407         *fn=0;  
408         
409         fn = strchr (filename, ':');
410         if (fn) fn++; else fn=(char *) selection_data->data;
411         
412         load_tt_part(fn);
413
414         strcpy (filename, "dont segfault workaround ;)");
415         return NULL;
416 }
417
418
419 /* save tables */
420
421 GtkSignalFunc cancel_save_tables(GtkWidget *wid)
422 {
423         gtk_widget_destroy(save_dialog);
424         save_dialog=NULL;
425         save_dialog_win=NULL;
426         return(0);
427 }
428
429 void do_save_tables(GtkWidget *wid)
430 {
431         FILE *out;
432         char buffer[PATH_MAX];
433         char wbuf[PATH_MAX];
434         char *ext;
435         
436         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(save_dialog)));
437         strcpy(globals.tables_filename, buffer);
438         
439         gtk_widget_destroy(save_dialog);
440         
441         save_dialog=NULL;
442         save_dialog_win=NULL;
443         
444         ext=strrchr(buffer, '.');
445         
446         if (ext)
447         {
448                 if (strcmp(ext, ".tX")) strcat(buffer, ".tX");
449         }
450         else
451         {
452                 strcat(buffer, ".tX");
453         }
454         
455         out=fopen(buffer, "w");
456         
457         if (out)
458         {
459                 if (vtt_class::save_all(out)) tx_note("Error while saving set.", true);
460                 fclose(out);
461                 sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
462                 gtk_window_set_title(GTK_WINDOW(main_window), wbuf);                            
463         }
464         else
465         {
466                 tx_note("Failed to open file for write access.", true);
467         }
468 }
469
470 GtkSignalFunc save_tables()
471 {
472         if (save_dialog_win) 
473         {
474                 gdk_window_raise(save_dialog_win);
475                 return 0;
476         }
477         
478         save_dialog=gtk_file_selection_new("Save Set"); 
479         
480         if (strlen(globals.tables_filename))
481         {
482                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(save_dialog), globals.tables_filename);
483         }
484         
485         gtk_widget_show(save_dialog);
486         
487         save_dialog_win=save_dialog->window;
488         
489         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(save_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_save_tables), NULL);
490         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(save_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_save_tables), NULL); 
491         gtk_signal_connect (GTK_OBJECT(save_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_save_tables), NULL);        
492
493         return NULL;
494 }
495
496 GtkSignalFunc master_volume_changed (GtkWidget *wid, void *d)
497 {
498         sp_master_volume.receive_gui_value((float) 2.0-GTK_ADJUSTMENT(wid)->value);
499         return NULL;    
500 }
501
502 GtkSignalFunc master_pitch_changed(GtkWidget *wid, void *d)
503 {
504         sp_master_pitch.receive_gui_value((float) GTK_ADJUSTMENT(wid)->value);  
505         return NULL;    
506 }
507
508 GtkSignalFunc saturate_changed(GtkWidget *w, void *d)
509 {
510         vtt_class::enable_saturate (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)));
511         return NULL;    
512 }
513
514 void mg_enable_critical_buttons(int enable)
515 {
516         gtk_widget_set_sensitive(seq_rec_btn, enable);
517         gtk_widget_set_sensitive(seq_play_btn, enable);
518         gtk_widget_set_sensitive(seq_slider, enable);
519
520         gtk_widget_set_sensitive(rec_menu_item, enable);
521         vg_enable_critical_buttons(enable);
522 }
523
524
525 GtkSignalFunc seq_stop(GtkWidget *w, void *);
526
527 GtkSignalFunc audio_on(GtkWidget *w, void *d)
528 {
529         tX_engine_error res;
530         
531         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
532         {               
533                 sequencer_ready=0;
534                 mg_enable_critical_buttons(0);
535                 res=tX_engine::get_instance()->run();
536                 sequencer_ready=1;
537
538                 if (res!=NO_ERROR)
539                 {
540                         mg_enable_critical_buttons(1);
541                         switch(res)
542                         {
543                                 case ERROR_BUSY:
544                                 tx_note("Error starting engine: engine is already running.", true);
545                                 break;
546                                 case ERROR_AUDIO:
547                                 tx_note("Error starting engine: failed to access audiodevice.", true);
548                                 break;
549                                 case ERROR_TAPE:
550                                 tx_note("Error starting engine: failed to open the recording file.", true);
551                                 break;
552                                 default:tx_note("Error starting engine: Unknown error.", true);
553                         }
554                         return 0;
555                 }
556
557                 stop_update=0;
558                 audioon=1;
559                 update_delay=globals.update_delay;
560                 update_tag=gtk_timeout_add(globals.update_idle, (GtkFunction) pos_update, NULL);
561                 gtk_widget_set_sensitive(grab_button, 1);
562         }
563         else
564         {               
565                 if (!sequencer_ready) return NULL;
566                 gtk_widget_set_sensitive(grab_button, 0);
567                 tX_engine::get_instance()->stop();
568                 stop_update=1;
569                 audioon=0;
570                 if (tX_engine::get_instance()->get_recording_request()) {
571                         tX_engine::get_instance()->set_recording_request(false);
572                         rec_dont_care=1;
573                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 0);
574                         rec_dont_care=0;
575                 }
576                 seq_stop(NULL, NULL);
577                 mg_enable_critical_buttons(1);
578         }
579         
580         return NULL;
581 }
582
583 GtkSignalFunc cancel_rec(GtkWidget *wid)
584 {
585         gtk_widget_destroy(rec_dialog);
586         rec_dialog=NULL;
587         rec_dialog_win=NULL;
588         rec_dont_care=0;
589         return(0);
590 }
591
592 void do_rec(GtkWidget *wid)
593 {
594         char buffer[PATH_MAX];
595         
596         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(rec_dialog)));
597
598         if (strlen(buffer))
599         {
600                 strcpy(globals.record_filename, buffer);                
601                 tX_engine::get_instance()->set_recording_request(true);
602                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 1);
603         }
604         
605         rec_dont_care=0;
606         
607         gtk_widget_destroy(rec_dialog);
608         
609         rec_dialog=NULL;
610         rec_dialog_win=NULL;
611 }
612
613 GtkSignalFunc select_rec_file()
614 {
615         if (rec_dialog_win) 
616         {
617                 gdk_window_raise(rec_dialog_win);
618                 return 0;
619         }
620         
621         rec_dialog=gtk_file_selection_new("Record To Disk");    
622         
623         if (strlen(globals.record_filename))
624         {
625                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(rec_dialog), globals.record_filename);
626         }
627         
628         gtk_widget_show(rec_dialog);
629         
630         rec_dialog_win=rec_dialog->window;
631         
632         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(rec_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_rec), NULL);
633         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(rec_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_rec), NULL);  
634         gtk_signal_connect (GTK_OBJECT(rec_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_rec), NULL); 
635         
636         return NULL;
637 }
638
639 GtkSignalFunc tape_on(GtkWidget *w, void *d)
640 {
641         if (rec_dont_care) return 0;
642
643         if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w)))
644         {       
645                 {
646                         rec_dont_care=1;
647                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w), 0);
648                         select_rec_file();
649                 }
650         }
651         else
652         {
653                         tX_engine::get_instance()->set_recording_request(false);
654         }
655         
656         return NULL;
657 }
658
659 void grab_on(GtkWidget *w, void *d)
660 {
661         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {
662                 tX_engine::get_instance()->set_grab_request();
663         }
664         grab_status=1;
665 }
666
667 void grab_off()
668 {
669         grab_status=0;
670 }
671
672 void quit()
673 {
674         turn_audio_off();
675         if (update_tag)
676         gtk_timeout_remove(update_tag);
677         globals.width=main_window->allocation.width;
678         globals.height=main_window->allocation.height;
679
680         gtk_main_quit();
681 }
682
683 void mplcfitx()
684 /* Most Probably Least Called Function In terminatorX :) */
685 {
686         show_about(0);
687 }
688
689 GtkSignalFunc seq_play(GtkWidget *w, void *)
690 {
691         if ((sequencer.is_empty()) &&   (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(seq_rec_btn)))) {
692                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
693                  {
694                         tx_note("Sequencer playback triggered - but no events recorded yet - nothing to playback!\n\nTo perform live with terminatorX just activate the audio engine with the \"Power\" button.");
695                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
696                  }
697         } else {
698                 if (seq_stop_override) return NULL;
699                         
700                 seq_adj_care=0;
701                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
702                 sequencer.trig_play();
703         
704                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 1);
705         }
706         
707         return NULL;
708 }
709
710 GtkSignalFunc seq_stop(GtkWidget *w, void *)
711 {
712         if (!sequencer_ready) return NULL;
713         sequencer.trig_stop();
714         seq_adj_care=1;
715         seq_stop_override=1;
716         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_play_btn), 0);
717         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_rec_btn), 0);
718         while (gtk_events_pending()) gtk_main_iteration();              
719         seq_stop_override=0;
720         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0); 
721         gtk_widget_set_sensitive(seq_slider, 1);        
722         gtk_widget_set_sensitive(engine_btn, 1);
723         gtk_widget_set_sensitive(seq_rec_btn, 1);
724
725         return NULL;
726 }
727
728 GtkSignalFunc seq_rec(GtkWidget *w, void *)
729 {
730         seq_adj_care=0;
731         gtk_widget_set_sensitive(seq_slider, 0);
732
733         if (seq_stop_override) return NULL;
734         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
735         gtk_widget_set_sensitive(engine_btn, 0);
736         gtk_widget_set_sensitive(seq_rec_btn, 0);
737         sequencer.trig_rec();
738         
739         return NULL;
740 }
741
742 void seq_update_entry(const guint32 timestamp)
743 {
744         char buffer[20];
745         guint32 samples;
746         guint32 minu,sec,hun;   
747         guint32 sr;
748         
749         samples=timestamp*vtt_class::get_mix_buffer_size();
750         sr=vtt_class::get_last_sample_rate();
751         
752         if (samples>0)
753         {
754                 minu=samples/(sr*60);
755                 samples-=(sr*60)*minu;
756         
757                 sec=samples/sr;
758                 samples-=sr*sec;
759         
760                 hun=samples/(sr/100);
761         }
762         else
763         {
764                 minu=sec=hun=0;
765         }
766         
767         sprintf(buffer, "%02i:%02i.%02i", minu, sec, hun);
768         gtk_entry_set_text(GTK_ENTRY(seq_entry), buffer);
769 }
770
771 void seq_update()
772 {
773         seq_update_entry(sequencer.get_timestamp());
774         gtk_adjustment_set_value(seq_adj, sequencer.get_timestamp_as_float());
775         
776 }
777 void seq_slider_released(GtkWidget *wid, void *d)
778 {
779         seq_adj_care=0;
780         gtk_widget_set_sensitive(seq_slider, 0);        
781         sequencer.forward_to_start_timestamp(0);
782         gtk_widget_set_sensitive(seq_slider, 1);        
783         seq_adj_care=1;
784 }
785 void sequencer_move(GtkWidget *wid, void *d)
786 {
787         guint32 pos;
788         
789         if (seq_adj_care)
790         {
791                 pos=sequencer.set_start_timestamp((float) GTK_ADJUSTMENT(wid)->value);
792                 seq_update_entry(pos);  
793         }
794 }
795
796 #define add_sep();      dummy=gtk_hseparator_new ();\
797         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);\
798         gtk_widget_show(dummy);\
799
800 #define add_sep2();     dummy=gtk_hseparator_new ();\
801         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);\
802         gtk_widget_show(dummy);\
803
804 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data);
805 void display_help();
806 void display_browser();
807
808 void create_master_menu() {
809         GtkWidget *menu_item;
810         GtkWidget *sub_menu;
811         GtkAccelGroup* accel_group=gtk_accel_group_new();
812         gtk_window_add_accel_group(GTK_WINDOW(main_window), accel_group);
813
814         /* FILE */
815         menu_item = gtk_menu_item_new_with_mnemonic ("_File");
816         gtk_widget_show (menu_item);
817         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
818
819         sub_menu = gtk_menu_new ();
820         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
821
822         menu_item = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
823         gtk_widget_show (menu_item);
824         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
825
826         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
827         gtk_widget_show (menu_item);
828         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
829         g_signal_connect(menu_item, "activate", (GCallback) load_tables, NULL);
830
831         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
832         gtk_widget_show (menu_item);
833         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
834         g_signal_connect(menu_item, "activate", (GCallback) save_tables, NULL);
835
836         /*menu_item = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
837         gtk_widget_show (menu_item);
838         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);*/
839
840         menu_item = gtk_menu_item_new ();
841         gtk_widget_show (menu_item);
842         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
843         gtk_widget_set_sensitive (menu_item, FALSE);
844
845         menu_item = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
846         gtk_widget_show (menu_item);
847         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
848         g_signal_connect(menu_item, "activate", (GCallback) quit, NULL);
849
850         /* Turntables */
851         menu_item = gtk_menu_item_new_with_mnemonic ("_Turntables");
852         gtk_widget_show (menu_item);
853         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
854         
855         sub_menu = gtk_menu_new ();
856         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
857
858         menu_item = gtk_menu_item_new_with_mnemonic("_Add Turntable");
859         gtk_widget_show (menu_item);
860         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
861         g_signal_connect(menu_item, "activate", (GCallback) new_table, NULL);
862
863         menu_item = gtk_check_menu_item_new_with_mnemonic("_Record Audio To Disk");
864         rec_menu_item = menu_item;
865         gtk_widget_show (menu_item);
866         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
867         g_signal_connect(menu_item, "activate", (GCallback) tape_on, NULL);
868
869         /* Options */
870         menu_item = gtk_menu_item_new_with_mnemonic ("_Options");
871         gtk_widget_show (menu_item);
872         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
873
874         sub_menu = gtk_menu_new ();
875         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
876                 
877         menu_item = gtk_check_menu_item_new_with_mnemonic("_Fullscreen");
878         fullscreen_item = menu_item;
879         gtk_widget_show (menu_item);
880         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
881
882         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.fullscreen_enabled);
883         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F11, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
884         g_signal_connect(menu_item, "activate", (GCallback) fullscreen_toggle, NULL);
885         
886         menu_item = gtk_menu_item_new ();
887         gtk_widget_show (menu_item);
888         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
889         gtk_widget_set_sensitive (menu_item, FALSE);
890
891         menu_item = gtk_menu_item_new_with_mnemonic("_Preferences");
892         gtk_widget_show (menu_item);
893         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
894         g_signal_connect(menu_item, "activate", (GCallback) display_options, NULL);
895
896         /* HELP */ 
897         menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
898         gtk_widget_show (menu_item);
899         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
900         gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_item), TRUE);
901         
902         sub_menu = gtk_menu_new ();
903         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
904
905         menu_item = gtk_menu_item_new_with_mnemonic ("_Contents");
906         gtk_widget_show (menu_item);
907         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
908         g_signal_connect(menu_item, "activate", (GCallback) display_help, NULL);
909         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F1, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
910
911         menu_item = gtk_menu_item_new_with_mnemonic ("_About");
912         gtk_widget_show (menu_item);
913         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
914         g_signal_connect(menu_item, "activate", (GCallback) mplcfitx, NULL);
915         
916         menu_item = gtk_menu_item_new ();
917         gtk_widget_show (menu_item);
918         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
919         gtk_widget_set_sensitive (menu_item, FALSE);
920
921         menu_item = gtk_menu_item_new_with_mnemonic ("_Visit terminatorX.cx");
922         gtk_widget_show (menu_item);
923         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
924         g_signal_connect(menu_item, "activate", (GCallback) display_browser, NULL);
925 }
926
927 void create_mastergui(int x, int y)
928 {
929         GtkWidget *mother_of_all_boxen;
930         GtkWidget *main_vbox;
931         GtkWidget *right_hbox;
932         GtkWidget *left_hbox;
933         GtkWidget *control_box;
934         //GtkWidget *sequencer_box;
935         GtkAdjustment *dumadj;
936         GtkWidget *dummy;
937         GtkWidget *master_vol_box;
938         GtkWidget *status_box;
939         
940         static GtkTargetEntry drop_types [] = {
941                 { "text/uri-list", 0, 0}
942         };
943         static gint n_drop_types = sizeof (drop_types) / sizeof(drop_types[0]);
944         
945         gui_tooltips=gtk_tooltips_new();
946
947         main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
948
949         gtk_window_set_wmclass(GTK_WINDOW(main_window), "terminatorX", "tX_mastergui");
950
951         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
952         
953         gtk_container_set_border_width(GTK_CONTAINER(main_window), 5);
954
955         gtk_widget_realize(main_window);
956         
957         mother_of_all_boxen=gtk_vbox_new(FALSE, 5);
958         gtk_container_add(GTK_CONTAINER(main_window), mother_of_all_boxen);
959         gtk_widget_show(mother_of_all_boxen);   
960         
961         main_menubar=gtk_menu_bar_new();
962         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_menubar, WID_FIX);
963         gtk_widget_show(main_menubar);  
964         
965         create_master_menu();
966         
967         main_vbox=gtk_hbox_new(FALSE, 5);
968         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_vbox, WID_DYN);
969         gtk_widget_show(main_vbox);
970         
971         left_hbox=gtk_vbox_new(FALSE, 5);
972         gtk_box_pack_start(GTK_BOX(main_vbox), left_hbox, WID_DYN);
973         gtk_widget_show(left_hbox);
974         
975         control_box=gtk_hbox_new(FALSE, 5);
976         gtk_box_pack_start(GTK_BOX(left_hbox), control_box, WID_FIX);
977         gtk_widget_show(control_box);
978         
979 /*      dummy=gtk_hseparator_new();
980         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
981         gtk_widget_show(dummy);*/
982
983
984         dummy=gtk_hseparator_new();
985         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
986         gtk_widget_show(dummy);
987
988     /* control_box contents */
989
990         dummy=tx_xpm_label_box(TX_ICON_AUDIOENGINE, "Audio");
991         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
992         gtk_widget_show(dummy);
993         
994         dummy=tx_xpm_button_new(TX_ICON_POWER,"Power ", 1);
995         connect_button(dummy,audio_on, NULL);
996         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
997         gui_set_tooltip(dummy, "Turn the audio engine on/off.");
998         gtk_widget_show(dummy);
999         engine_btn=dummy;
1000         
1001         grab_button=tx_xpm_button_new(TX_ICON_GRAB, "Mouse Grab ", 1);
1002         gtk_box_pack_start(GTK_BOX(control_box), grab_button, WID_FIX);
1003         connect_button(grab_button, grab_on, NULL);
1004         gui_set_tooltip(grab_button, "Enter the mouse grab mode operation. Press <ESCAPE> to exit grab mode.");
1005         gtk_widget_show(grab_button);
1006
1007         /*dummy=gtk_check_button_new_with_label("Record");
1008         rec_btn=dummy;
1009         connect_button(dummy,tape_on, NULL);
1010         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1011         gui_set_tooltip(dummy, "Record the audio the terminatorX' audio engine renders. You will be prompted to enter a name for the target wav-file.");
1012         gtk_widget_show(dummy);*/
1013
1014         dummy=gtk_vseparator_new();
1015         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1016         gtk_widget_show(dummy);
1017     
1018         dummy=tx_xpm_label_box(TX_ICON_SEQUENCER, "Seq.");
1019         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1020         gtk_widget_show(dummy);
1021
1022         dummy=tx_xpm_button_new(TX_ICON_PLAY,"Play ", 1);
1023         connect_button(dummy, seq_play, NULL);
1024         seq_play_btn=dummy;
1025         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1026         gui_set_tooltip(dummy, "Playback previously recorded events from the sequencer. This will turn on the audio engine automagically.");
1027         gtk_widget_show(dummy);
1028
1029         dummy=tx_xpm_button_new(TX_ICON_STOP,"Stop ", 0);
1030         seq_stop_btn=dummy;
1031         connect_button(dummy, seq_stop, NULL);  
1032         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1033         gui_set_tooltip(dummy, "Stop the playback of sequencer events.");
1034         gtk_widget_show(dummy);
1035
1036         dummy=tx_xpm_button_new(TX_ICON_RECORD,"Record ", 1);
1037         connect_button(dummy, seq_rec, NULL);
1038         seq_rec_btn=dummy;
1039         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1040         gui_set_tooltip(dummy, "Enable recording of *events* into the sequencer. All touched controls will be recorded. Existing events for the song-time recording will be overwritten for touched controls.");
1041         gtk_widget_show(dummy);
1042
1043         dummy=gtk_label_new("Pos:");
1044         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1045         gtk_widget_show(dummy);
1046         
1047         dummy=gtk_entry_new_with_max_length(12);
1048         seq_entry=dummy;
1049         //gtk_widget_set_usize(dummy, 65, 20);
1050         gtk_entry_set_text(GTK_ENTRY(dummy), "00:00.00");
1051         gtk_entry_set_width_chars(GTK_ENTRY(dummy), 9);
1052         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1053         gtk_widget_show(dummy);
1054
1055         dumadj=(GtkAdjustment*) gtk_adjustment_new(0, 0, 100, 0.1, 1, 1);
1056         seq_adj=dumadj;
1057         connect_adj(dumadj, sequencer_move, NULL);      
1058         dummy=gtk_hscale_new(dumadj);
1059         gtk_widget_set_usize(dummy, 65, 20);
1060         seq_slider=dummy;
1061         gtk_signal_connect(GTK_OBJECT(seq_slider), "button-release-event", (GtkSignalFunc) seq_slider_released, NULL);
1062         gtk_scale_set_draw_value(GTK_SCALE(dummy), FALSE);
1063         
1064         gui_set_tooltip(dummy, "Select the start position for the sequencer in song-time.");
1065         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1066         gtk_widget_show(dummy);
1067         
1068         dummy=gtk_hbox_new(FALSE,2); //gtk_hpaned_new ();
1069         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_DYN);
1070         gtk_widget_show(dummy);
1071         
1072         tt_parent=dummy;
1073
1074     panel_bar=gtk_hbox_new(TRUE,2);
1075         gtk_box_pack_start(GTK_BOX(left_hbox), panel_bar, WID_FIX);
1076
1077         control_parent=gtk_hbox_new(FALSE,0);
1078         gtk_box_pack_start(GTK_BOX(tt_parent), control_parent, WID_FIX);
1079         //gtk_paned_pack1(GTK_PANED(tt_parent), control_parent, FALSE, FALSE);
1080         gtk_widget_show(control_parent);
1081
1082         dummy=gtk_vseparator_new();
1083         gtk_box_pack_start(GTK_BOX(tt_parent), dummy, WID_FIX);
1084         gtk_widget_show(dummy);
1085
1086         audio_parent=gtk_vbox_new(FALSE,0);
1087         gtk_box_pack_start(GTK_BOX(tt_parent), audio_parent, WID_DYN);
1088         //gtk_paned_pack2(GTK_PANED(tt_parent), audio_parent, TRUE, FALSE);
1089         gtk_widget_show(audio_parent);
1090         
1091         dummy=gtk_vseparator_new();
1092         gtk_box_pack_start(GTK_BOX(main_vbox), dummy, WID_FIX);
1093         gtk_widget_show(dummy);
1094                 
1095         right_hbox=gtk_vbox_new(FALSE, 5);
1096         gtk_box_pack_start(GTK_BOX(main_vbox), right_hbox, WID_FIX);
1097         gtk_widget_show(right_hbox);
1098
1099         /* Master */
1100         
1101         dummy=gtk_label_new(NULL);
1102         //gtk_label_set_markup(GTK_LABEL(dummy),"<span foreground='#000080'>Master</span>");
1103         gtk_label_set_markup(GTK_LABEL(dummy),"<b>Master</b>");
1104         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1105         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1106         gtk_widget_show(dummy); 
1107
1108         dummy=gtk_hseparator_new();
1109         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1110         gtk_widget_show(dummy);
1111
1112          /* Pitch */
1113          
1114         /*dummy=gtk_label_new("Pitch:");
1115         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1116         gtk_widget_show(dummy);*/
1117
1118         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.pitch, -3, 3, 0.001, 0.001, 0.01);
1119         pitch_adj=dumadj;
1120         connect_adj(dumadj, master_pitch_changed, NULL);
1121         
1122         tX_extdial *pdial=new tX_extdial("Pitch", pitch_adj, true);
1123         gtk_box_pack_start(GTK_BOX(right_hbox), pdial->get_widget(), WID_FIX);
1124         gui_set_tooltip(pdial->get_entry(), "Use this dial to adjust the master pitch (affecting *all* turntables).");
1125         
1126 /*      dummy=gtk_hscale_new(dumadj);
1127         gtk_scale_set_digits(GTK_SCALE(dummy), 2);
1128         gtk_scale_set_value_pos(GTK_SCALE(dummy), GTK_POS_LEFT);
1129         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1130         gui_set_tooltip(dummy, "Use this scale to adjust the master pitch (affecting *all* turntables).");
1131         gtk_widget_show(dummy);
1132 */
1133         dummy=gtk_hseparator_new();
1134         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1135         gtk_widget_show(dummy);
1136         
1137         /* Volume */
1138         master_vol_box=gtk_hbox_new(FALSE, 2);
1139         gtk_box_pack_start(GTK_BOX(right_hbox), master_vol_box, WID_DYN);
1140         gtk_widget_show(master_vol_box);        
1141         
1142         dumadj=(GtkAdjustment*) gtk_adjustment_new(2.0-globals.volume, 0, 2, 0.01, 0.05, 0.005);
1143         volume_adj=dumadj;
1144
1145         connect_adj(dumadj, master_volume_changed, NULL);       
1146         dummy=gtk_vscale_new(dumadj);
1147         gtk_scale_set_draw_value(GTK_SCALE(dummy), False);
1148         gtk_box_pack_end(GTK_BOX(master_vol_box), dummy, WID_FIX);
1149         gtk_widget_show(dummy); 
1150         gui_set_tooltip(dummy, "Adjust the master volume. This parameter will effect *all* turntables in the set.");
1151         
1152 #ifdef USE_FLASH        
1153         main_flash_r=gtk_tx_flash_new();
1154         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_r, WID_DYN);
1155         gtk_widget_show(main_flash_r);
1156
1157         main_flash_l=gtk_tx_flash_new();
1158         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_l, WID_DYN);
1159         gtk_widget_show(main_flash_l);
1160 #endif  
1161         dummy=gtk_label_new("Volume");
1162         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1163         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1164         gtk_widget_show(dummy);
1165
1166         /* STATUS BOX */ 
1167         dummy=gtk_hseparator_new();
1168         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1169         gtk_widget_show(dummy);
1170         
1171         status_box=gtk_vbox_new(FALSE, 0);
1172         gtk_box_pack_start(GTK_BOX(right_hbox), status_box, WID_FIX);
1173         gtk_widget_show(status_box);
1174         
1175         dummy=gtk_label_new("0");
1176         used_mem=dummy;
1177         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1178         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1179         gtk_widget_show(dummy);
1180
1181         dummy=gtk_label_new("Mem/kB:");
1182         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1183         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1184         gtk_widget_show(dummy);
1185         
1186         add_sep2();
1187
1188         dummy=gtk_label_new("1");
1189         no_of_vtts=dummy;
1190         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1191         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1192         gtk_widget_show(dummy);
1193
1194         dummy=gtk_label_new("Vtts:");
1195         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1196         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1197         gtk_widget_show(dummy);
1198
1199         add_sep2();
1200
1201         dummy=gtk_label_new(VERSION);
1202         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1203         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1204         gtk_widget_show(dummy);
1205
1206         dummy=gtk_label_new("Release:");
1207         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1208         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1209         gtk_widget_show(dummy);
1210         
1211         add_sep2();
1212
1213         dummy=gtk_label_new(NULL);
1214         gtk_label_set_markup(GTK_LABEL(dummy), "<b>Status</b>");
1215         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1216         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1217         gtk_widget_show(dummy);
1218         
1219         /* END GUI */
1220         
1221         gtk_window_set_default_size(GTK_WINDOW(main_window), x, y);     
1222         gtk_widget_set_sensitive(grab_button, 0);
1223
1224         new_table(NULL, NULL); // to give the user something to start with ;)
1225
1226         gtk_signal_connect (GTK_OBJECT(main_window), "delete-event", (GtkSignalFunc) quit, NULL);       
1227         
1228         if (globals.tooltips) gtk_tooltips_enable(gui_tooltips);
1229         else gtk_tooltips_disable(gui_tooltips);
1230 }
1231
1232 gfloat old_percent=-1;
1233
1234 void wav_progress_update(gfloat percent)
1235 {
1236         percent=floor(percent*10.0)/10.0; //Updating statusbars with gtk-themes eats up hell of a lot CPU-time
1237                                           // which is why we update every 10% only.
1238         
1239         if (wav_progress)
1240         {
1241                 if (old_percent != percent)
1242                 {
1243                         old_percent = percent;
1244                         gtk_progress_bar_update(GTK_PROGRESS_BAR(wav_progress), percent);
1245                         while (gtk_events_pending()) gtk_main_iteration();      
1246                 }
1247         }
1248
1249 }
1250
1251 void note_destroy(GtkWidget *widget, GtkWidget *mbox)
1252 {
1253         gtk_widget_destroy(GTK_WIDGET(mbox));
1254 }
1255
1256 void tx_note(const char *message, bool isError)
1257 {
1258         char buffer[4096]="terminatorX ";
1259         if (isError) {
1260                 strcat(buffer, "note:\n\n");
1261         } else {
1262                 strcat(buffer, "error:\n\n");
1263         }
1264         
1265         strcat(buffer, message);
1266         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1267                 GTK_DIALOG_DESTROY_WITH_PARENT,
1268                 isError ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1269         gtk_dialog_run(GTK_DIALOG(dialog));
1270         gtk_widget_destroy(dialog);     
1271 }
1272
1273
1274 void tx_l_note(const char *message)
1275 {
1276         char buffer[4096]="Plugin info:\n\n";
1277         strcat(buffer, message);
1278         
1279         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1280                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1281         gtk_dialog_run(GTK_DIALOG(dialog));
1282         gtk_widget_destroy(dialog);     
1283 }
1284
1285
1286 void add_to_panel_bar(GtkWidget *button) {
1287         buttons_on_panel_bar++;
1288         gtk_box_pack_start(GTK_BOX(panel_bar), button, WID_DYN);
1289         gtk_widget_show(panel_bar);
1290 }
1291
1292 void remove_from_panel_bar(GtkWidget *button) {
1293         buttons_on_panel_bar--;
1294         gtk_container_remove(GTK_CONTAINER(panel_bar), button);
1295         if (buttons_on_panel_bar==0) gtk_widget_hide(panel_bar);
1296 }
1297
1298 /* Fullscreen code... */
1299 #define _WIN_LAYER_TOP          -1
1300 #define _WIN_LAYER_NORMAL       4
1301 #define _NET_WM_STATE_REMOVE    0
1302 #define _NET_WM_STATE_ADD       1
1303 #define _NET_WM_STATE_TOGGLE    2
1304
1305 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data) {
1306         XEvent xev;
1307         Window win=GDK_WINDOW_XID(main_window->window);
1308         Display *disp=GDK_WINDOW_XDISPLAY(main_window->window);
1309         
1310         globals.fullscreen_enabled=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(fullscreen_item));
1311         
1312         /* Top layer.. */
1313         xev.xclient.type = ClientMessage;
1314         xev.xclient.serial = 0;
1315         xev.xclient.send_event = True;
1316         xev.xclient.display = disp;
1317         xev.xclient.window = win;
1318         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_WIN_LAYER");
1319         xev.xclient.format = 32;
1320         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _WIN_LAYER_TOP : _WIN_LAYER_NORMAL ;
1321         XSendEvent(disp, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1322                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1323                 &xev);
1324         
1325         /* Fullscreen */
1326         xev.xclient.type = ClientMessage;
1327         xev.xclient.serial = 0;
1328         xev.xclient.send_event = True;
1329         xev.xclient.display = disp;
1330         xev.xclient.window = win;
1331         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_WM_STATE");
1332         xev.xclient.format = 32;
1333         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1334         xev.xclient.data.l[1] = gdk_x11_atom_to_xatom (gdk_atom_intern ("_NET_WM_STATE_FULLSCREEN", TRUE));
1335         xev.xclient.data.l[2] = gdk_x11_atom_to_xatom (GDK_NONE);
1336         XSendEvent(gdk_display, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1337                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1338                 &xev);  
1339 }
1340
1341 void fullscreen_setup() {
1342         if (globals.fullscreen_enabled) {
1343                 fullscreen_toggle(NULL, NULL);
1344         }
1345 }
1346
1347 void display_mastergui()
1348 {
1349         GtkWidget *top;
1350         gtk_widget_realize(main_window);
1351         tX_set_icon(main_window, "terminatorX");
1352         load_knob_pixs(main_window);
1353         gtk_widget_show(main_window);
1354         fullscreen_setup();     
1355         top=gtk_widget_get_toplevel(main_window);
1356         xwindow=GDK_WINDOW_XWINDOW(top->window);
1357 }
1358
1359 pid_t help_child=0;
1360 GTimer *help_timer=NULL;
1361 int help_tag=-1;
1362
1363 int help_checker() {
1364         gdouble time;
1365         gulong ms;
1366         int status;
1367         int result=waitpid(help_child, &status, WNOHANG);
1368         
1369         if (result==0) {
1370                 time=g_timer_elapsed(help_timer, &ms);
1371                 if (time > 5) {
1372                         /* 5 seconds and it's still running - so we assume everything's OK. */
1373                         tX_debug("No longer waiting for gnome-help..");
1374                         gtk_idle_remove(help_tag);
1375                         help_tag=-1;
1376                 }
1377         } else {
1378                 /* We are still here and the child exited - that could mean trouble. */
1379                 tx_note("Couldn't run the gnome-help command (alias \"yelp\") to display the terminatorX manual. Please ensure that \"yelp\" is installed.", true);             
1380                 
1381                 gtk_idle_remove(help_tag);
1382                 help_tag=-1;
1383         }
1384         return TRUE;    
1385 }
1386
1387 #ifndef INSTALL_PREFIX
1388 #define INSTALL_PREFIX "/usr/local/share"
1389 #endif
1390
1391 void display_help() {   
1392         help_child=fork();
1393
1394         if (help_tag!=-1) {
1395                 gtk_idle_remove(help_tag);
1396                 if (help_timer) g_timer_destroy(help_timer);
1397                 help_child=0;
1398                 help_tag=-1;
1399                 help_timer=NULL;
1400         }
1401         
1402         if (help_child==0) {
1403                 // child
1404                 execlp("gnome-help","gnome-help","ghelp:/" INSTALL_PREFIX "/terminatorX/doc/terminatorX-manual/C/terminatorX-manual.xml", NULL);
1405                 _exit(-1);
1406         } else if (help_child==-1) {
1407                 tx_note("System error: couldn't fork() to run the help process.", true);
1408         } else {
1409                 help_timer=g_timer_new();
1410                 g_timer_start(help_timer);
1411         
1412                 help_tag=gtk_idle_add((GtkFunction) help_checker, NULL);
1413         }
1414 }
1415
1416 pid_t browser_child=0;
1417 GTimer *browser_timer=NULL;
1418 int browser_tag=-1;
1419
1420 int browser_checker() {
1421         gdouble time;
1422         gulong ms;
1423         int status;
1424         int result=waitpid(browser_child, &status, WNOHANG);
1425         
1426         if (result==0) {
1427                 time=g_timer_elapsed(browser_timer, &ms);
1428                 if (time > 5) {
1429                         /* 5 seconds and it's still running - so we assume everything's OK. */
1430                         tX_debug("No longer waiting for a browser..");
1431                         gtk_idle_remove(browser_tag);
1432                         browser_tag=-1;
1433                 }
1434         } else {
1435                 /* We are still here and the child exited - that could mean trouble. */
1436                 tx_note("Failed to run a suitable web browser - if there's one installed on this system, please run it and forward yourself to:\nhttp://terminatorX.cx", true);         
1437                 
1438                 gtk_idle_remove(browser_tag);
1439                 browser_tag=-1;
1440         }
1441         return TRUE;    
1442 }
1443
1444 void display_browser() {        
1445         browser_child=fork();
1446
1447         if (browser_tag!=-1) {
1448                 gtk_idle_remove(browser_tag);
1449                 if (browser_timer) g_timer_destroy(browser_timer);
1450                 browser_child=0;
1451                 browser_tag=-1;
1452                 browser_timer=NULL;
1453         }
1454         
1455         if (browser_child==0) {
1456                 // child
1457                 execlp("mozilla","mozilla","http://terminatorX.cx", NULL);
1458                 execlp("netscape","netscape","http://terminatorX.cx", NULL);
1459                 execlp("galeon","galeon","http://terminatorX.cx", NULL);
1460                 execlp("konqueror","konqueror","http://terminatorX.cx", NULL);          
1461                 _exit(-1);
1462         } else if (browser_child==-1) {
1463                 tx_note("System error: couldn't fork() to run the browser process.", true);
1464         } else {
1465                 browser_timer=g_timer_new();
1466                 g_timer_start(browser_timer);
1467         
1468                 browser_tag=gtk_idle_add((GtkFunction) browser_checker, NULL);
1469         }
1470 }