dbda188f782fa605f920ad5f76e8d7dd9b4dd7c0
[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( strcmp(preferences->theme, mwindow->preferences->theme) != 0 )
215                 mwindow->restart_status = -1; // reload, need new bcresources
216         if( strcmp(preferences->plugin_icons, mwindow->preferences->plugin_icons) != 0 )
217                 mwindow->restart_status = -1;
218
219         mwindow->edl->copy_session(edl, 1);
220         mwindow->preferences->copy_from(preferences);
221         mwindow->init_brender();
222
223         BC_Signals::set_catch_segv(mwindow->preferences->trap_sigsegv);
224         BC_Signals::set_catch_intr(mwindow->preferences->trap_sigintr);
225         BC_WindowBase::get_resources()->popupmenu_btnup = mwindow->preferences->popupmenu_btnup;
226         BC_WindowBase::get_resources()->grab_input_focus = mwindow->preferences->grab_input_focus;
227         BC_WindowBase::get_resources()->textbox_focus_policy = mwindow->preferences->textbox_focus_policy;
228         if( mwindow->preferences->trap_sigsegv || mwindow->preferences->trap_sigintr ) {
229                 BC_Trace::enable_locks();
230         }
231         else {
232                 BC_Trace::disable_locks();
233         }
234
235         mwindow->reset_android_remote();
236         int ffmpeg_early_probe = mwindow->preferences->get_file_probe_armed("FFPMEG_Early");
237         mwindow->gui->ffmpeg_toggle->update(ffmpeg_early_probe);
238         mwindow->gui->ffmpeg_toggle->set_tooltip(ffmpeg_early_probe ?
239                 FFMPEG_EARLY_TIP : FFMPEG_LATE_TIP);
240         mwindow->gui->mainshbtns->load(mwindow->preferences);
241
242 //edl->session->recording_format->dump();
243 //mwindow->edl->session->recording_format->dump();
244
245         if(((mwindow->edl->session->output_w % 4) ||
246                 (mwindow->edl->session->output_h % 4)) &&
247                 mwindow->edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL)
248         {
249                 MainError::show_error(
250                         _("This project's dimensions are not multiples of 4 so\n"
251                         "it can't be rendered by OpenGL."));
252         }
253
254
255         if(redraw_meters)
256         {
257                 mwindow->cwindow->gui->lock_window("PreferencesThread::apply_settings");
258                 mwindow->cwindow->gui->meters->change_format(edl->session->meter_format,
259                         edl->session->min_meter_db,
260                         edl->session->max_meter_db);
261                 mwindow->cwindow->gui->unlock_window();
262
263
264
265                 for(int i = 0; i < mwindow->vwindows.size(); i++) {
266                         VWindow *vwindow = mwindow->vwindows.get(i);
267                         if( !vwindow->is_running() ) continue;
268                         vwindow->gui->lock_window("PreferencesThread::apply_settings");
269                         vwindow->gui->meters->change_format(edl->session->meter_format,
270                                 edl->session->min_meter_db,
271                                 edl->session->max_meter_db);
272                         vwindow->gui->unlock_window();
273
274                 }
275
276
277                 mwindow->gui->lock_window("PreferencesThread::apply_settings 1");
278                 mwindow->gui->set_meter_format(edl->session->meter_format,
279                         edl->session->min_meter_db,
280                         edl->session->max_meter_db);
281                 mwindow->gui->unlock_window();
282
283
284
285                 mwindow->lwindow->gui->lock_window("PreferencesThread::apply_settings");
286                 mwindow->lwindow->gui->panel->change_format(edl->session->meter_format,
287                         edl->session->min_meter_db,
288                         edl->session->max_meter_db);
289                 mwindow->lwindow->gui->unlock_window();
290         }
291
292         if(redraw_overlays)
293         {
294                 mwindow->gui->lock_window("PreferencesThread::apply_settings 2");
295                 mwindow->gui->draw_overlays(1);
296                 mwindow->gui->unlock_window();
297         }
298
299         if(redraw_times)
300         {
301                 mwindow->gui->lock_window("PreferencesThread::apply_settings 3");
302                 mwindow->gui->update(0, 0, 1, 0, 0, 1, 0);
303                 mwindow->gui->redraw_time_dependancies();
304                 mwindow->gui->unlock_window();
305         }
306
307         if(rerender)
308         {
309 //printf("PreferencesThread::apply_settings 1\n");
310 // This doesn't stop and restart, only reloads the assets before
311 // the next play command.
312                 mwindow->cwindow->playback_engine->que->send_command(CURRENT_FRAME,
313                         CHANGE_ALL,
314                         mwindow->edl,
315                         1);
316 //printf("PreferencesThread::apply_settings 10\n");
317         }
318
319         if(redraw_times || redraw_overlays)
320         {
321                 mwindow->gui->lock_window("PreferencesThread::apply_settings 4");
322                 mwindow->gui->flush();
323                 mwindow->gui->unlock_window();
324         }
325
326         return 0;
327 }
328
329 const char* PreferencesThread::category_to_text(int category)
330 {
331         PlaybackConfig *playback_config = edl->session->playback_config;
332         switch(category)
333         {
334                 case PLAYBACK_A:
335                         return playback_config->active_config==0 ?
336                                  _("*Playback A") : _("Playback A");
337                 case PLAYBACK_B:
338                         return playback_config->active_config==1 ?
339                                  _("*Playback B") : _("Playback B");
340                 case RECORD:
341                         return _("Recording");
342                 case PERFORMANCE:
343                         return _("Performance");
344                 case INTERFACE:
345                         return _("Interface");
346                 case APPEARANCE:
347                         return _("Appearance");
348                 case ABOUT:
349                         return _("About");
350         }
351         return "";
352 }
353
354 int PreferencesThread::text_to_category(const char *category)
355 {
356 SET_TRACE
357         int min_result = -1, result, result_num = 0;
358         for(int i = 0; i < CATEGORIES; i++)
359         {
360                 result = labs(strcmp(category_to_text(i), category));
361                 if(result < min_result || min_result < 0)
362                 {
363                         min_result = result;
364                         result_num = i;
365                 }
366         }
367 SET_TRACE
368         return result_num;
369 }
370
371
372
373
374
375
376
377
378 PreferencesWindow::PreferencesWindow(MWindow *mwindow,
379         PreferencesThread *thread, int x, int y, int w, int h)
380  : BC_Window(_(PROGRAM_NAME ": Preferences"), x,y, w,h,w,h, 1)
381 {
382         this->mwindow = mwindow;
383         this->thread = thread;
384         dialog = 0;
385         category = 0;
386 }
387
388 PreferencesWindow::~PreferencesWindow()
389 {
390         lock_window("PreferencesWindow::~PreferencesWindow");
391         delete category;
392
393
394         if(dialog) delete dialog;
395
396
397         for(int i = 0; i < categories.total; i++)
398                 delete categories.values[i];
399         unlock_window();
400 }
401
402 void PreferencesWindow::create_objects()
403 {
404         BC_Button *button;
405
406         lock_window("PreferencesWindow::create_objects");
407         set_icon(mwindow->theme->get_image("mwindow_icon"));
408         mwindow->theme->draw_preferences_bg(this);
409         flash();
410
411         int x = mwindow->theme->preferencescategory_x;
412         int y = mwindow->theme->preferencescategory_y;
413         for(int i = 0; i < CATEGORIES; i++)
414         {
415                 add_subwindow(category_button[i] = new PreferencesButton(mwindow,
416                         thread, x, y, i, 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::APPEARANCE:
523                         add_subwindow(dialog = new AppearancePrefs(mwindow, this));
524                         break;
525
526                 case PreferencesThread::ABOUT:
527                         add_subwindow(dialog = new AboutPrefs(mwindow, this));
528                         break;
529         }
530
531 //PRINT_TRACE
532         if(dialog)
533         {
534                 dialog->draw_top_background(this, 0, 0, dialog->get_w(), dialog->get_h());
535 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
536                 dialog->create_objects();
537 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
538                 dialog->lower_window();
539                 dialog->show_window(0);
540         }
541
542         if(dialog2)
543         {
544                 dialog2->hide_window(0);
545                 delete dialog2;
546         }
547
548         return 0;
549 }
550
551
552
553
554
555
556
557
558
559
560
561 PreferencesButton::PreferencesButton(MWindow *mwindow,
562         PreferencesThread *thread,
563         int x,
564         int y,
565         int category,
566         const char *text,
567         VFrame **images)
568  : BC_GenericButton(x, y, text, images)
569 {
570         this->mwindow = mwindow;
571         this->thread = thread;
572         this->category = category;
573 }
574
575 int PreferencesButton::handle_event()
576 {
577         thread->window->set_current_dialog(category);
578         return 1;
579 }
580
581
582
583
584
585
586
587
588
589 PreferencesDialog::PreferencesDialog(MWindow *mwindow,
590         PreferencesWindow *pwindow)
591  : BC_SubWindow(10,
592         40,
593         pwindow->get_w() - 20,
594         pwindow->get_h() - BC_GenericButton::calculate_h() - 10 - 40)
595 {
596         this->pwindow = pwindow;
597         this->mwindow = mwindow;
598         preferences = pwindow->thread->preferences;
599 }
600
601 PreferencesDialog::~PreferencesDialog()
602 {
603 }
604
605 // ============================== category window
606
607
608
609
610 PreferencesApply::PreferencesApply(MWindow *mwindow, PreferencesThread *thread)
611  : BC_GenericButton(thread->window->get_w() / 2 - BC_GenericButton::calculate_w(thread->window, _("Apply")) / 2,
612         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
613         _("Apply"))
614 {
615         this->mwindow = mwindow;
616         this->thread = thread;
617 }
618 int PreferencesApply::handle_event()
619 {
620         thread->apply_settings();
621         return 1;
622 }
623 int PreferencesApply::resize_event(int w, int h)
624 {
625         reposition_window(w/2 - get_w()/2, h-get_h()-10);
626         return 1;
627 }
628
629
630
631
632 PreferencesOK::PreferencesOK(MWindow *mwindow, PreferencesThread *thread)
633  : BC_GenericButton(10,
634         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
635         _("OK"))
636 {
637         this->mwindow = mwindow;
638         this->thread = thread;
639 }
640 int PreferencesOK::keypress_event()
641 {
642         if(get_keypress() == RETURN)
643         {
644                 thread->window->set_done(0);
645                 return 1;
646         }
647         return 0;
648 }
649 int PreferencesOK::handle_event()
650 {
651         thread->window->set_done(0);
652         return 1;
653 }
654 int PreferencesOK::resize_event(int w, int h)
655 {
656         reposition_window(10, h-get_h()-10);
657         return 1;
658 }
659
660
661
662 PreferencesCancel::PreferencesCancel(MWindow *mwindow, PreferencesThread *thread)
663  : BC_GenericButton(thread->window->get_w() - BC_GenericButton::calculate_w(thread->window, _("Cancel")) - 10,
664         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
665         _("Cancel"))
666 {
667         this->mwindow = mwindow;
668         this->thread = thread;
669 }
670 int PreferencesCancel::keypress_event()
671 {
672         if(get_keypress() == ESC)
673         {
674                 thread->window->set_done(1);
675                 return 1;
676         }
677         return 0;
678 }
679 int PreferencesCancel::handle_event()
680 {
681         thread->window->set_done(1);
682         return 1;
683 }
684 int PreferencesCancel::resize_event(int w, int h)
685 {
686         reposition_window(w-get_w()-10, h-get_h()-10);
687         return 1;
688 }
689
690
691
692
693
694
695
696
697
698
699 PreferencesCategory::PreferencesCategory(MWindow *mwindow, PreferencesThread *thread, int x, int y)
700  : BC_PopupTextBox(thread->window,
701                 &thread->window->categories,
702                 thread->category_to_text(thread->current_dialog),
703                 x,
704                 y,
705                 200,
706                 150)
707 {
708         this->mwindow = mwindow;
709         this->thread = thread;
710 }
711
712 PreferencesCategory::~PreferencesCategory()
713 {
714 }
715
716 int PreferencesCategory::handle_event()
717 {
718 SET_TRACE
719         thread->window->set_current_dialog(thread->text_to_category(get_text()));
720 SET_TRACE
721         return 1;
722 }