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