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