merge hv v6, rework trace methods
[goodguy/history.git] / cinelerra-5.1 / cinelerra / preferencesthread.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "aboutprefs.h"
23 #include "asset.h"
24 #include "audiodevice.inc"
25 #include "bcsignals.h"
26 #include "bctrace.h"
27 #include "cache.h"
28 #include "cplayback.h"
29 #include "cwindow.h"
30 #include "cwindowgui.h"
31 #include "bchash.h"
32 #include "edl.h"
33 #include "edlsession.h"
34 #include "filesystem.h"
35 #include "fonts.h"
36 #include "interfaceprefs.h"
37 #include "keys.h"
38 #include "language.h"
39 #include "levelwindow.h"
40 #include "levelwindowgui.h"
41 #include "mainclock.h"
42 #include "mainerror.h"
43 #include "mbuttons.h"
44 #include "meterpanel.h"
45 #include "mutex.h"
46 #include "mwindow.h"
47 #include "mwindowgui.h"
48 #include "patchbay.h"
49 #include "performanceprefs.h"
50 #include "playbackengine.h"
51 #include "playbackprefs.h"
52 #include "preferences.h"
53 #include "recordprefs.h"
54 #include "shbtnprefs.h"
55 #include "theme.h"
56 #include "trackcanvas.h"
57 #include "transportque.h"
58 #include "vwindow.h"
59 #include "vwindowgui.h"
60
61 #include <string.h>
62
63
64
65 #define WIDTH 770
66 #define HEIGHT 690
67
68
69 PreferencesMenuitem::PreferencesMenuitem(MWindow *mwindow)
70  : BC_MenuItem(_("Preferences..."), _("Shift-P"), 'P')
71 {
72         this->mwindow = mwindow;
73
74         set_shift(1);
75         thread = new PreferencesThread(mwindow);
76 }
77
78 PreferencesMenuitem::~PreferencesMenuitem()
79 {
80         delete thread;
81 }
82
83
84 int PreferencesMenuitem::handle_event()
85 {
86         mwindow->gui->unlock_window();
87         thread->start();
88         mwindow->gui->lock_window("PreferencesMenuitem::handle_event");
89         return 1;
90 }
91
92
93
94
95 PreferencesThread::PreferencesThread(MWindow *mwindow)
96  : BC_DialogThread()
97 {
98         this->mwindow = mwindow;
99         window = 0;
100         thread_running = 0;
101 }
102
103 PreferencesThread::~PreferencesThread()
104 {
105         close_window();
106 }
107
108 BC_Window* PreferencesThread::new_gui()
109 {
110
111         preferences = new Preferences;
112         edl = new EDL;
113         edl->create_objects();
114         current_dialog = mwindow->defaults->get("DEFAULTPREF", 0);
115         preferences->copy_from(mwindow->preferences);
116         edl->copy_session(mwindow->edl);
117         redraw_indexes = 0;
118         redraw_meters = 0;
119         redraw_times = 0;
120         redraw_overlays = 0;
121         close_assets = 0;
122         reload_plugins = 0;
123         //int need_new_indexes = 0;
124         rerender = 0;
125
126         mwindow->gui->lock_window("NewThread::new_gui");
127         int scr_x = mwindow->gui->get_screen_x(0, -1);
128         int scr_w = mwindow->gui->get_screen_w(0, -1);
129         int scr_h = mwindow->gui->get_screen_h(0, -1);
130
131         int w = WIDTH, h = HEIGHT;
132         int min_w = mwindow->theme->preferencescategory_x;
133         for(int i = 0; i < CATEGORIES; i++) {
134                 min_w += PreferencesButton::calculate_w(mwindow->gui, category_to_text(i)) -
135                         mwindow->theme->preferences_category_overlap;
136         }
137         if( w < min_w ) w = min_w;
138         int x = scr_x + scr_w / 2 - w / 2;
139         int y = scr_h / 2 - h / 2;
140
141         window = new PreferencesWindow(mwindow, this, x, y, w, h);
142         window->create_objects();
143         mwindow->gui->unlock_window();
144
145         thread_running = 1;
146         return window;
147 }
148
149 void PreferencesThread::handle_close_event(int result)
150 {
151         thread_running = 0;
152         if(!result)
153         {
154                 apply_settings();
155                 mwindow->save_defaults();
156         }
157
158         window = 0;
159         delete preferences;
160         edl->Garbage::remove_user();
161         preferences = 0;
162         edl = 0;
163
164         mwindow->defaults->update("DEFAULTPREF", current_dialog);
165         if( mwindow->restart() )
166                 mwindow->gui->set_done(0);
167 }
168
169
170
171 int PreferencesThread::update_framerate()
172 {
173         if(thread_running)
174         {
175                 lock_gui("PreferencesThread::update_framerate");
176                 PreferencesWindow *window = (PreferencesWindow*)get_gui();
177                 if(window) window->update_framerate();
178                 unlock_gui();
179         }
180         return 0;
181 }
182
183
184 void PreferencesThread::update_rates()
185 {
186         if(thread_running)
187         {
188                 lock_gui("PreferencesThread::update_framerate");
189                 PreferencesWindow *window = (PreferencesWindow*)get_gui();
190                 if(window) window->update_rates();
191                 unlock_gui();
192         }
193 }
194
195 int PreferencesThread::apply_settings()
196 {
197 // Compare sessions
198
199         PlaybackConfig *this_playback_config = edl->session->playback_config;
200         AudioOutConfig *this_aconfig = this_playback_config->aconfig;
201         VideoOutConfig *this_vconfig = this_playback_config->vconfig;
202         PlaybackConfig *playback_config = mwindow->edl->session->playback_config;
203         AudioOutConfig *aconfig = playback_config->aconfig;
204         VideoOutConfig *vconfig = playback_config->vconfig;
205
206         rerender =
207                 edl->session->need_rerender(mwindow->edl->session) ||
208                 (preferences->force_uniprocessor != mwindow->preferences->force_uniprocessor) ||
209                 this_playback_config->active_config != playback_config->active_config ||
210                 (*this_aconfig != *aconfig) || (*this_vconfig != *vconfig) ||
211                 !preferences->brender_asset->equivalent(*mwindow->preferences->brender_asset, 0, 1);
212
213         if( strcmp(preferences->theme, mwindow->preferences->theme) != 0 )
214                 mwindow->restart_status = -1; // reload, need new bcresources
215
216         mwindow->edl->copy_session(edl, 1);
217         mwindow->preferences->copy_from(preferences);
218         mwindow->init_brender();
219
220         BC_Signals::set_catch_segv(mwindow->preferences->trap_sigsegv);
221         BC_Signals::set_catch_intr(mwindow->preferences->trap_sigintr);
222         BC_WindowBase::get_resources()->popupmenu_btnup = mwindow->preferences->popupmenu_btnup;
223         if( mwindow->preferences->trap_sigsegv || mwindow->preferences->trap_sigintr ) {
224                 BC_Trace::enable_locks();
225         }
226         else {
227                 BC_Trace::disable_locks();
228         }
229
230         mwindow->reset_android_remote();
231         mwindow->gui->ffmpeg_toggle->update(mwindow->preferences->ffmpeg_early_probe);
232         mwindow->gui->ffmpeg_toggle->set_tooltip( mwindow->preferences->ffmpeg_early_probe ?
233                 _("Try FFMpeg first") : _("Try FFMpeg last") );
234         mwindow->gui->mainshbtns->load(mwindow->preferences);
235         double tc_position = 
236                 mwindow->edl->session->get_frame_offset() / mwindow->edl->session->frame_rate;
237         mwindow->gui->mainclock->set_position_offset(tc_position);
238
239 //edl->session->recording_format->dump();
240 //mwindow->edl->session->recording_format->dump();
241
242         if(((mwindow->edl->session->output_w % 4) ||
243                 (mwindow->edl->session->output_h % 4)) &&
244                 mwindow->edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL)
245         {
246                 MainError::show_error(
247                         _("This project's dimensions are not multiples of 4 so\n"
248                         "it can't be rendered by OpenGL."));
249         }
250
251
252         if(redraw_meters)
253         {
254                 mwindow->cwindow->gui->lock_window("PreferencesThread::apply_settings");
255                 mwindow->cwindow->gui->meters->change_format(edl->session->meter_format,
256                         edl->session->min_meter_db,
257                         edl->session->max_meter_db);
258                 mwindow->cwindow->gui->unlock_window();
259
260
261
262                 for(int i = 0; i < mwindow->vwindows.size(); i++) {
263                         VWindow *vwindow = mwindow->vwindows.get(i);
264                         if( !vwindow->is_running() ) continue;
265                         vwindow->gui->lock_window("PreferencesThread::apply_settings");
266                         vwindow->gui->meters->change_format(edl->session->meter_format,
267                                 edl->session->min_meter_db,
268                                 edl->session->max_meter_db);
269                         vwindow->gui->unlock_window();
270
271                 }
272
273
274                 mwindow->gui->lock_window("PreferencesThread::apply_settings 1");
275                 mwindow->gui->set_meter_format(edl->session->meter_format,
276                         edl->session->min_meter_db,
277                         edl->session->max_meter_db);
278                 mwindow->gui->unlock_window();
279
280
281
282                 mwindow->lwindow->gui->lock_window("PreferencesThread::apply_settings");
283                 mwindow->lwindow->gui->panel->change_format(edl->session->meter_format,
284                         edl->session->min_meter_db,
285                         edl->session->max_meter_db);
286                 mwindow->lwindow->gui->unlock_window();
287         }
288
289         if(redraw_overlays)
290         {
291                 mwindow->gui->lock_window("PreferencesThread::apply_settings 2");
292                 mwindow->gui->draw_overlays(1);
293                 mwindow->gui->unlock_window();
294         }
295
296         if(redraw_times)
297         {
298                 mwindow->gui->lock_window("PreferencesThread::apply_settings 3");
299                 mwindow->gui->update(0, 0, 1, 0, 0, 1, 0);
300                 mwindow->gui->redraw_time_dependancies();
301                 mwindow->gui->unlock_window();
302         }
303
304         if(rerender)
305         {
306 //printf("PreferencesThread::apply_settings 1\n");
307 // This doesn't stop and restart, only reloads the assets before
308 // the next play command.
309                 mwindow->cwindow->playback_engine->que->send_command(CURRENT_FRAME,
310                         CHANGE_ALL,
311                         mwindow->edl,
312                         1);
313 //printf("PreferencesThread::apply_settings 10\n");
314         }
315
316         if(redraw_times || redraw_overlays)
317         {
318                 mwindow->gui->lock_window("PreferencesThread::apply_settings 4");
319                 mwindow->gui->flush();
320                 mwindow->gui->unlock_window();
321         }
322
323         return 0;
324 }
325
326 const char* PreferencesThread::category_to_text(int category)
327 {
328         PlaybackConfig *playback_config = edl->session->playback_config;
329         switch(category)
330         {
331                 case PLAYBACK_A:
332                         return playback_config->active_config==0 ?
333                                  _("*Playback A") : _("Playback A");
334                 case PLAYBACK_B:
335                         return playback_config->active_config==1 ?
336                                  _("*Playback B") : _("Playback B");
337                 case RECORD:
338                         return _("Recording");
339                 case PERFORMANCE:
340                         return _("Performance");
341                 case INTERFACE:
342                         return _("Interface");
343                 case ABOUT:
344                         return _("About");
345         }
346         return "";
347 }
348
349 int PreferencesThread::text_to_category(const char *category)
350 {
351 SET_TRACE
352         int min_result = -1, result, result_num = 0;
353         for(int i = 0; i < CATEGORIES; i++)
354         {
355                 result = labs(strcmp(category_to_text(i), category));
356                 if(result < min_result || min_result < 0)
357                 {
358                         min_result = result;
359                         result_num = i;
360                 }
361         }
362 SET_TRACE
363         return result_num;
364 }
365
366
367
368
369
370
371
372
373 PreferencesWindow::PreferencesWindow(MWindow *mwindow,
374         PreferencesThread *thread, int x, int y, int w, int h)
375  : BC_Window(_(PROGRAM_NAME ": Preferences"), x,y, w,h,w,h, 1)
376 {
377         this->mwindow = mwindow;
378         this->thread = thread;
379         dialog = 0;
380         category = 0;
381 }
382
383 PreferencesWindow::~PreferencesWindow()
384 {
385         lock_window("PreferencesWindow::~PreferencesWindow");
386         delete category;
387
388
389         if(dialog) delete dialog;
390
391
392         for(int i = 0; i < categories.total; i++)
393                 delete categories.values[i];
394         unlock_window();
395 }
396
397 void PreferencesWindow::create_objects()
398 {
399         BC_Button *button;
400
401         lock_window("PreferencesWindow::create_objects");
402         set_icon(mwindow->theme->get_image("mwindow_icon"));
403         mwindow->theme->draw_preferences_bg(this);
404         flash();
405
406         int x = mwindow->theme->preferencescategory_x;
407         int y = mwindow->theme->preferencescategory_y;
408         for(int i = 0; i < CATEGORIES; i++)
409         {
410                 add_subwindow(category_button[i] = new PreferencesButton(mwindow,
411                         thread,
412                         x,
413                         y,
414                         i,
415                         thread->category_to_text(i),
416                         (i == thread->current_dialog) ?
417                                 mwindow->theme->get_image_set("category_button_checked") :
418                                 mwindow->theme->get_image_set("category_button")));
419                 x += category_button[i]->get_w() -
420                         mwindow->theme->preferences_category_overlap;
421         }
422
423
424 //      for(int i = 0; i < CATEGORIES; i++)
425 //              categories.append(new BC_ListBoxItem(thread->category_to_text(i)));
426 //      category = new PreferencesCategory(mwindow,
427 //              thread,
428 //              mwindow->theme->preferencescategory_x,
429 //              mwindow->theme->preferencescategory_y);
430 //      category->create_objects();
431
432
433         add_subwindow(button = new PreferencesOK(mwindow, thread));
434         add_subwindow(new PreferencesApply(mwindow, thread));
435         add_subwindow(new PreferencesCancel(mwindow, thread));
436
437         set_current_dialog(thread->current_dialog);
438
439         show_window();
440         unlock_window();
441 }
442
443 int PreferencesWindow::update_framerate()
444 {
445         lock_window("PreferencesWindow::update_framerate");
446         if(thread->current_dialog < PreferencesThread::RECORD)
447         {
448                 dialog->draw_framerate(1);
449 //              flash();
450         }
451         unlock_window();
452         return 0;
453 }
454
455
456 void PreferencesWindow::update_rates()
457 {
458         lock_window("PreferencesWindow::update_rates");
459         if(thread->current_dialog == PreferencesThread::PERFORMANCE)
460         {
461                 dialog->update_rates();
462         }
463         unlock_window();
464 }
465
466
467 int PreferencesWindow::set_current_dialog(int number)
468 {
469         if(dialog) delete dialog;
470         dialog = 0;
471         thread->current_dialog = number;
472
473 //PRINT_TRACE
474         PreferencesDialog *dialog2 = dialog;
475         dialog = 0;
476 //PRINT_TRACE
477
478 // Redraw category buttons
479         for(int i = 0; i < CATEGORIES; i++)
480         {
481                 if(i == number)
482                 {
483                         category_button[i]->set_images(
484                                 mwindow->theme->get_image_set("category_button_checked"));
485                 }
486                 else
487                 {
488                         category_button[i]->set_images(
489                                 mwindow->theme->get_image_set("category_button"));
490                 }
491                 category_button[i]->draw_face(0);
492
493 // Copy face to background for next button's overlap.
494 // Still can't do state changes right.
495         }
496
497
498 //PRINT_TRACE
499         PlaybackConfig *playback_config = thread->edl->session->playback_config;
500         switch(number)
501         {
502                 case PreferencesThread::PLAYBACK_A:
503                 case PreferencesThread::PLAYBACK_B:
504                         playback_config->load_defaults(mwindow->defaults,
505                                 number == PreferencesThread::PLAYBACK_A ? 0 : 1);
506                         add_subwindow(dialog = new PlaybackPrefs(mwindow, this, number));
507                         break;
508
509                 case PreferencesThread::RECORD:
510                         add_subwindow(dialog = new RecordPrefs(mwindow, this));
511                         break;
512
513                 case PreferencesThread::PERFORMANCE:
514                         add_subwindow(dialog = new PerformancePrefs(mwindow, this));
515                         break;
516
517                 case PreferencesThread::INTERFACE:
518                         add_subwindow(dialog = new InterfacePrefs(mwindow, this));
519                         break;
520
521                 case PreferencesThread::ABOUT:
522                         add_subwindow(dialog = new AboutPrefs(mwindow, this));
523                         break;
524         }
525
526 //PRINT_TRACE
527         if(dialog)
528         {
529                 dialog->draw_top_background(this, 0, 0, dialog->get_w(), dialog->get_h());
530 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
531                 dialog->create_objects();
532 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
533                 dialog->lower_window();
534                 dialog->show_window(0);
535         }
536
537         if(dialog2)
538         {
539                 dialog2->hide_window(0);
540                 delete dialog2;
541         }
542
543         return 0;
544 }
545
546
547
548
549
550
551
552
553
554
555
556 PreferencesButton::PreferencesButton(MWindow *mwindow,
557         PreferencesThread *thread,
558         int x,
559         int y,
560         int category,
561         const char *text,
562         VFrame **images)
563  : BC_GenericButton(x, y, text, images)
564 {
565         this->mwindow = mwindow;
566         this->thread = thread;
567         this->category = category;
568 }
569
570 int PreferencesButton::handle_event()
571 {
572         thread->window->set_current_dialog(category);
573         return 1;
574 }
575
576
577
578
579
580
581
582
583
584 PreferencesDialog::PreferencesDialog(MWindow *mwindow,
585         PreferencesWindow *pwindow)
586  : BC_SubWindow(10,
587         40,
588         pwindow->get_w() - 20,
589         pwindow->get_h() - BC_GenericButton::calculate_h() - 10 - 40)
590 {
591         this->pwindow = pwindow;
592         this->mwindow = mwindow;
593         preferences = pwindow->thread->preferences;
594 }
595
596 PreferencesDialog::~PreferencesDialog()
597 {
598 }
599
600 // ============================== category window
601
602
603
604
605 PreferencesApply::PreferencesApply(MWindow *mwindow, PreferencesThread *thread)
606  : BC_GenericButton(thread->window->get_w() / 2 - BC_GenericButton::calculate_w(thread->window, _("Apply")) / 2,
607         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
608         _("Apply"))
609 {
610         this->mwindow = mwindow;
611         this->thread = thread;
612 }
613 int PreferencesApply::handle_event()
614 {
615         thread->apply_settings();
616         return 1;
617 }
618 int PreferencesApply::resize_event(int w, int h)
619 {
620         reposition_window(w/2 - get_w()/2, h-get_h()-10);
621         return 1;
622 }
623
624
625
626
627 PreferencesOK::PreferencesOK(MWindow *mwindow, PreferencesThread *thread)
628  : BC_GenericButton(10,
629         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
630         _("OK"))
631 {
632         this->mwindow = mwindow;
633         this->thread = thread;
634 }
635 int PreferencesOK::keypress_event()
636 {
637         if(get_keypress() == RETURN)
638         {
639                 thread->window->set_done(0);
640                 return 1;
641         }
642         return 0;
643 }
644 int PreferencesOK::handle_event()
645 {
646         thread->window->set_done(0);
647         return 1;
648 }
649 int PreferencesOK::resize_event(int w, int h)
650 {
651         reposition_window(10, h-get_h()-10);
652         return 1;
653 }
654
655
656
657 PreferencesCancel::PreferencesCancel(MWindow *mwindow, PreferencesThread *thread)
658  : BC_GenericButton(thread->window->get_w() - BC_GenericButton::calculate_w(thread->window, _("Cancel")) - 10,
659         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
660         _("Cancel"))
661 {
662         this->mwindow = mwindow;
663         this->thread = thread;
664 }
665 int PreferencesCancel::keypress_event()
666 {
667         if(get_keypress() == ESC)
668         {
669                 thread->window->set_done(1);
670                 return 1;
671         }
672         return 0;
673 }
674 int PreferencesCancel::handle_event()
675 {
676         thread->window->set_done(1);
677         return 1;
678 }
679 int PreferencesCancel::resize_event(int w, int h)
680 {
681         reposition_window(w-get_w()-10, h-get_h()-10);
682         return 1;
683 }
684
685
686
687
688
689
690
691
692
693
694 PreferencesCategory::PreferencesCategory(MWindow *mwindow, PreferencesThread *thread, int x, int y)
695  : BC_PopupTextBox(thread->window,
696                 &thread->window->categories,
697                 thread->category_to_text(thread->current_dialog),
698                 x,
699                 y,
700                 200,
701                 150)
702 {
703         this->mwindow = mwindow;
704         this->thread = thread;
705 }
706
707 PreferencesCategory::~PreferencesCategory()
708 {
709 }
710
711 int PreferencesCategory::handle_event()
712 {
713 SET_TRACE
714         thread->window->set_current_dialog(thread->text_to_category(get_text()));
715 SET_TRACE
716         return 1;
717 }