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