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