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