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