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