add fileppm, fix renderfarm/brender deadlock, fix audio meters, add gbrp + cin_pix_fm...
[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         PreferencesWindow *window = (PreferencesWindow*)get_gui();
229         if( window ) window->unlock_window();
230         mwindow->init_brender();
231         if( window ) window->lock_window("PreferencesThread::apply_settings 5");
232
233         if( strcmp(preferences->theme, mwindow->preferences->theme) != 0 )
234                 mwindow->restart_status = -1; // reload, need new bcresources
235         if( strcmp(preferences->plugin_icons, mwindow->preferences->plugin_icons) != 0 )
236                 mwindow->restart_status = -1;
237
238         mwindow->edl->copy_session(edl, 1);
239         mwindow->preferences->copy_from(preferences);
240
241         BC_Signals::set_catch_segv(mwindow->preferences->trap_sigsegv);
242         BC_Signals::set_catch_intr(mwindow->preferences->trap_sigintr);
243         BC_WindowBase::get_resources()->popupmenu_btnup = mwindow->preferences->popupmenu_btnup;
244         BC_WindowBase::get_resources()->grab_input_focus = mwindow->preferences->grab_input_focus;
245         BC_WindowBase::get_resources()->textbox_focus_policy = mwindow->preferences->textbox_focus_policy;
246         if( mwindow->preferences->trap_sigsegv || mwindow->preferences->trap_sigintr ) {
247                 BC_Trace::enable_locks();
248         }
249         else {
250                 BC_Trace::disable_locks();
251         }
252
253         mwindow->reset_android_remote();
254         int ffmpeg_early_probe = mwindow->preferences->get_file_probe_armed("FFMPEG_Early");
255         mwindow->gui->ffmpeg_toggle->update(ffmpeg_early_probe);
256         mwindow->gui->ffmpeg_toggle->set_tooltip(ffmpeg_early_probe ?
257                 FFMPEG_EARLY_TIP : FFMPEG_LATE_TIP);
258         mwindow->gui->mainshbtns->load(mwindow->preferences);
259
260 //edl->session->recording_format->dump();
261 //mwindow->edl->session->recording_format->dump();
262
263         if(((mwindow->edl->session->output_w % 4) ||
264                 (mwindow->edl->session->output_h % 4)) &&
265                 mwindow->edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL)
266         {
267                 MainError::show_error(
268                         _("This project's dimensions are not multiples of 4 so\n"
269                         "it can't be rendered by OpenGL."));
270         }
271
272
273         if(redraw_meters)
274         {
275                 mwindow->cwindow->gui->lock_window("PreferencesThread::apply_settings");
276                 mwindow->cwindow->gui->meters->change_format(edl->session->meter_format,
277                         edl->session->min_meter_db,
278                         edl->session->max_meter_db);
279                 mwindow->cwindow->gui->unlock_window();
280
281
282
283                 for(int i = 0; i < mwindow->vwindows.size(); i++) {
284                         VWindow *vwindow = mwindow->vwindows.get(i);
285                         if( !vwindow->is_running() ) continue;
286                         vwindow->gui->lock_window("PreferencesThread::apply_settings");
287                         vwindow->gui->meters->change_format(edl->session->meter_format,
288                                 edl->session->min_meter_db,
289                                 edl->session->max_meter_db);
290                         vwindow->gui->unlock_window();
291
292                 }
293
294
295                 mwindow->gui->lock_window("PreferencesThread::apply_settings 1");
296                 mwindow->gui->set_meter_format(edl->session->meter_format,
297                         edl->session->min_meter_db,
298                         edl->session->max_meter_db);
299                 mwindow->gui->unlock_window();
300
301
302
303                 mwindow->lwindow->gui->lock_window("PreferencesThread::apply_settings");
304                 mwindow->lwindow->gui->panel->change_format(edl->session->meter_format,
305                         edl->session->min_meter_db,
306                         edl->session->max_meter_db);
307                 mwindow->lwindow->gui->unlock_window();
308         }
309
310         if(redraw_overlays)
311         {
312                 mwindow->gui->lock_window("PreferencesThread::apply_settings 2");
313                 mwindow->gui->show_cursor(0);
314                 mwindow->gui->draw_overlays(1);
315                 mwindow->gui->unlock_window();
316         }
317
318         if(redraw_times)
319         {
320                 mwindow->gui->lock_window("PreferencesThread::apply_settings 3");
321                 mwindow->gui->update(0, 0, 1, 0, 0, 1, 0);
322                 mwindow->gui->redraw_time_dependancies();
323                 mwindow->gui->unlock_window();
324         }
325
326         if(rerender)
327         {
328 //printf("PreferencesThread::apply_settings 1\n");
329 // This doesn't stop and restart, only reloads the assets before
330 // the next play command.
331                 mwindow->cwindow->refresh_frame(CHANGE_ALL, mwindow->edl);
332 //printf("PreferencesThread::apply_settings 10\n");
333         }
334
335         if(redraw_times || redraw_overlays)
336         {
337                 mwindow->gui->lock_window("PreferencesThread::apply_settings 4");
338                 mwindow->gui->flush();
339                 mwindow->gui->unlock_window();
340         }
341
342         return 0;
343 }
344
345 const char* PreferencesThread::category_to_text(int category)
346 {
347         PlaybackConfig *playback_config = edl->session->playback_config;
348         switch(category)
349         {
350                 case PLAYBACK_A:
351                         return playback_config->active_config==0 ?
352                                  _("*Playback A") : _("Playback A");
353                 case PLAYBACK_B:
354                         return playback_config->active_config==1 ?
355                                  _("*Playback B") : _("Playback B");
356                 case RECORD:
357                         return _("Recording");
358                 case PERFORMANCE:
359                         return _("Performance");
360                 case INTERFACE:
361                         return _("Interface");
362                 case APPEARANCE:
363                         return _("Appearance");
364                 case ABOUT:
365                         return _("About");
366         }
367         return "";
368 }
369
370 int PreferencesThread::text_to_category(const char *category)
371 {
372 SET_TRACE
373         int min_result = -1, result, result_num = 0;
374         for(int i = 0; i < CATEGORIES; i++)
375         {
376                 result = labs(strcmp(category_to_text(i), category));
377                 if(result < min_result || min_result < 0)
378                 {
379                         min_result = result;
380                         result_num = i;
381                 }
382         }
383 SET_TRACE
384         return result_num;
385 }
386
387
388
389
390
391
392
393
394 PreferencesWindow::PreferencesWindow(MWindow *mwindow,
395         PreferencesThread *thread, int x, int y, int w, int h)
396  : BC_Window(_(PROGRAM_NAME ": Preferences"), x,y, w,h,w,h, 1)
397 {
398         this->mwindow = mwindow;
399         this->thread = thread;
400         dialog = 0;
401         category = 0;
402 }
403
404 PreferencesWindow::~PreferencesWindow()
405 {
406         lock_window("PreferencesWindow::~PreferencesWindow");
407         delete category;
408
409
410         if(dialog) delete dialog;
411
412
413         for(int i = 0; i < categories.total; i++)
414                 delete categories.values[i];
415         unlock_window();
416 }
417
418 void PreferencesWindow::create_objects()
419 {
420         BC_Button *button;
421
422         lock_window("PreferencesWindow::create_objects");
423         set_icon(mwindow->theme->get_image("mwindow_icon"));
424         mwindow->theme->draw_preferences_bg(this);
425         flash();
426
427         int x = mwindow->theme->preferencescategory_x;
428         int y = mwindow->theme->preferencescategory_y;
429         for(int i = 0; i < CATEGORIES; i++)
430         {
431                 add_subwindow(category_button[i] = new PreferencesButton(mwindow,
432                         thread, x, y, i, thread->category_to_text(i),
433                         (i == thread->current_dialog) ?
434                                 mwindow->theme->get_image_set("category_button_checked") :
435                                 mwindow->theme->get_image_set("category_button")));
436                 x += category_button[i]->get_w() -
437                         mwindow->theme->preferences_category_overlap;
438         }
439
440
441 //      for(int i = 0; i < CATEGORIES; i++)
442 //              categories.append(new BC_ListBoxItem(thread->category_to_text(i)));
443 //      category = new PreferencesCategory(mwindow,
444 //              thread,
445 //              mwindow->theme->preferencescategory_x,
446 //              mwindow->theme->preferencescategory_y);
447 //      category->create_objects();
448
449
450         add_subwindow(button = new PreferencesOK(mwindow, thread));
451         add_subwindow(new PreferencesApply(mwindow, thread));
452         add_subwindow(new PreferencesCancel(mwindow, thread));
453
454         set_current_dialog(thread->current_dialog);
455
456         show_window();
457         unlock_window();
458 }
459
460 int PreferencesWindow::update_framerate()
461 {
462         lock_window("PreferencesWindow::update_framerate");
463         if(thread->current_dialog < PreferencesThread::RECORD)
464         {
465                 dialog->draw_framerate(1);
466 //              flash();
467         }
468         unlock_window();
469         return 0;
470 }
471
472
473 void PreferencesWindow::update_rates()
474 {
475         lock_window("PreferencesWindow::update_rates");
476         if(thread->current_dialog == PreferencesThread::PERFORMANCE)
477         {
478                 dialog->update_rates();
479         }
480         unlock_window();
481 }
482
483
484 int PreferencesWindow::set_current_dialog(int number)
485 {
486         if(dialog) delete dialog;
487         dialog = 0;
488         thread->current_dialog = number;
489
490 //PRINT_TRACE
491         PreferencesDialog *dialog2 = dialog;
492         dialog = 0;
493 //PRINT_TRACE
494
495 // Redraw category buttons
496         for(int i = 0; i < CATEGORIES; i++)
497         {
498                 if(i == number)
499                 {
500                         category_button[i]->set_images(
501                                 mwindow->theme->get_image_set("category_button_checked"));
502                 }
503                 else
504                 {
505                         category_button[i]->set_images(
506                                 mwindow->theme->get_image_set("category_button"));
507                 }
508                 category_button[i]->draw_face(0);
509
510 // Copy face to background for next button's overlap.
511 // Still can't do state changes right.
512         }
513
514
515 //PRINT_TRACE
516         PlaybackConfig *playback_config = thread->edl->session->playback_config;
517         switch(number)
518         {
519                 case PreferencesThread::PLAYBACK_A:
520                 case PreferencesThread::PLAYBACK_B:
521                         playback_config->load_defaults(mwindow->defaults,
522                                 number == PreferencesThread::PLAYBACK_A ? 0 : 1);
523                         add_subwindow(dialog = new PlaybackPrefs(mwindow, this, number));
524                         break;
525
526                 case PreferencesThread::RECORD:
527                         add_subwindow(dialog = new RecordPrefs(mwindow, this));
528                         break;
529
530                 case PreferencesThread::PERFORMANCE:
531                         add_subwindow(dialog = new PerformancePrefs(mwindow, this));
532                         break;
533
534                 case PreferencesThread::INTERFACE:
535                         add_subwindow(dialog = new InterfacePrefs(mwindow, this));
536                         break;
537
538                 case PreferencesThread::APPEARANCE:
539                         add_subwindow(dialog = new AppearancePrefs(mwindow, this));
540                         break;
541
542                 case PreferencesThread::ABOUT:
543                         add_subwindow(dialog = new AboutPrefs(mwindow, this));
544                         break;
545         }
546
547 //PRINT_TRACE
548         if(dialog)
549         {
550                 dialog->draw_top_background(this, 0, 0, dialog->get_w(), dialog->get_h());
551 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
552                 dialog->create_objects();
553 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
554                 dialog->lower_window();
555                 dialog->show_window(0);
556         }
557
558         if(dialog2)
559         {
560                 dialog2->hide_window(0);
561                 delete dialog2;
562         }
563
564         return 0;
565 }
566
567
568
569
570
571
572
573
574
575
576
577 PreferencesButton::PreferencesButton(MWindow *mwindow,
578         PreferencesThread *thread,
579         int x,
580         int y,
581         int category,
582         const char *text,
583         VFrame **images)
584  : BC_GenericButton(x, y, text, images)
585 {
586         this->mwindow = mwindow;
587         this->thread = thread;
588         this->category = category;
589 }
590
591 int PreferencesButton::handle_event()
592 {
593         thread->window->set_current_dialog(category);
594         return 1;
595 }
596
597
598
599
600
601
602
603
604
605 PreferencesDialog::PreferencesDialog(MWindow *mwindow,
606         PreferencesWindow *pwindow)
607  : BC_SubWindow(10,
608         40,
609         pwindow->get_w() - 20,
610         pwindow->get_h() - BC_GenericButton::calculate_h() - 10 - 40)
611 {
612         this->pwindow = pwindow;
613         this->mwindow = mwindow;
614         preferences = pwindow->thread->preferences;
615 }
616
617 PreferencesDialog::~PreferencesDialog()
618 {
619 }
620
621 // ============================== category window
622
623
624
625
626 PreferencesApply::PreferencesApply(MWindow *mwindow, PreferencesThread *thread)
627  : BC_GenericButton(thread->window->get_w() / 2 - BC_GenericButton::calculate_w(thread->window, _("Apply")) / 2,
628         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
629         _("Apply"))
630 {
631         this->mwindow = mwindow;
632         this->thread = thread;
633 }
634 int PreferencesApply::handle_event()
635 {
636         thread->apply_settings();
637         return 1;
638 }
639 int PreferencesApply::resize_event(int w, int h)
640 {
641         reposition_window(w/2 - get_w()/2, h-get_h()-10);
642         return 1;
643 }
644
645
646
647
648 PreferencesOK::PreferencesOK(MWindow *mwindow, PreferencesThread *thread)
649  : BC_GenericButton(10,
650         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
651         _("OK"))
652 {
653         this->mwindow = mwindow;
654         this->thread = thread;
655 }
656 int PreferencesOK::keypress_event()
657 {
658         if(get_keypress() == RETURN)
659         {
660                 thread->window->set_done(0);
661                 return 1;
662         }
663         return 0;
664 }
665 int PreferencesOK::handle_event()
666 {
667         thread->window->set_done(0);
668         return 1;
669 }
670 int PreferencesOK::resize_event(int w, int h)
671 {
672         reposition_window(10, h-get_h()-10);
673         return 1;
674 }
675
676
677
678 PreferencesCancel::PreferencesCancel(MWindow *mwindow, PreferencesThread *thread)
679  : BC_GenericButton(thread->window->get_w() - BC_GenericButton::calculate_w(thread->window, _("Cancel")) - 10,
680         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
681         _("Cancel"))
682 {
683         this->mwindow = mwindow;
684         this->thread = thread;
685 }
686 int PreferencesCancel::keypress_event()
687 {
688         if(get_keypress() == ESC)
689         {
690                 thread->window->set_done(1);
691                 return 1;
692         }
693         return 0;
694 }
695 int PreferencesCancel::handle_event()
696 {
697         thread->window->set_done(1);
698         return 1;
699 }
700 int PreferencesCancel::resize_event(int w, int h)
701 {
702         reposition_window(w-get_w()-10, h-get_h()-10);
703         return 1;
704 }
705
706
707
708
709
710
711
712
713
714
715 PreferencesCategory::PreferencesCategory(MWindow *mwindow, PreferencesThread *thread, int x, int y)
716  : BC_PopupTextBox(thread->window,
717                 &thread->window->categories,
718                 thread->category_to_text(thread->current_dialog),
719                 x,
720                 y,
721                 200,
722                 150)
723 {
724         this->mwindow = mwindow;
725         this->thread = thread;
726 }
727
728 PreferencesCategory::~PreferencesCategory()
729 {
730 }
731
732 int PreferencesCategory::handle_event()
733 {
734 SET_TRACE
735         thread->window->set_current_dialog(thread->text_to_category(get_text()));
736 SET_TRACE
737         return 1;
738 }