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