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