890ff0adceea4dd996aed3d3fab83deb1335e2ca
[goodguy/history.git] / cinelerra-5.1 / cinelerra / localsession.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "automation.h"
23 #include "awindowgui.h"
24 #include "clip.h"
25 #include "bchash.h"
26 #include "edl.h"
27 #include "filesystem.h"
28 #include "filexml.h"
29 #include "floatauto.h"
30 #include "language.h"
31 #include "localsession.h"
32
33
34 static const char *xml_autogrouptypes_titlesmax[] =
35 {
36         "AUTOGROUPTYPE_AUDIO_FADE_MAX",
37         "AUTOGROUPTYPE_VIDEO_FADE_MAX",
38         "AUTOGROUPTYPE_ZOOM_MAX",
39         "AUTOGROUPTYPE_SPEED_MAX",
40         "AUTOGROUPTYPE_X_MAX",
41         "AUTOGROUPTYPE_Y_MAX",
42         "AUTOGROUPTYPE_INT255_MAX"
43 };
44
45 static const char *xml_autogrouptypes_titlesmin[] =
46 {
47         "AUTOGROUPTYPE_AUDIO_FADE_MIN",
48         "AUTOGROUPTYPE_VIDEO_FADE_MIN",
49         "AUTOGROUPTYPE_ZOOM_MIN",
50         "AUTOGROUPTYPE_SPEED_MIN",
51         "AUTOGROUPTYPE_X_MIN",
52         "AUTOGROUPTYPE_Y_MIN",
53         "AUTOGROUPTYPE_INT255_MIN"
54 };
55
56
57 LocalSession::LocalSession(EDL *edl)
58 {
59         this->edl = edl;
60
61         selectionstart = selectionend = 0;
62         in_point = out_point = -1;
63         awindow_folder = AW_CLIP_FOLDER;
64         sprintf(clip_title, _("Program"));
65         strcpy(clip_notes, _("Hello world"));
66         strcpy(clip_icon, "");
67         clipboard_length = 0;
68         loop_playback = 0;
69         loop_start = loop_end = 0;
70         playback_start = -1;
71         playback_end = 0;
72         preview_start = 0;  preview_end = -1;
73         zoom_sample = DEFAULT_ZOOM_TIME;
74         zoom_y = 0;
75         zoom_track = 0;
76         x_pane = y_pane = -1;
77
78         for(int i = 0; i < TOTAL_PANES; i++) {
79                 view_start[i] = 0;
80                 track_start[i] = 0;
81         }
82
83         automation_mins[AUTOGROUPTYPE_AUDIO_FADE] = -80;
84         automation_maxs[AUTOGROUPTYPE_AUDIO_FADE] = 6;
85
86         automation_mins[AUTOGROUPTYPE_VIDEO_FADE] = 0;
87         automation_maxs[AUTOGROUPTYPE_VIDEO_FADE] = 100;
88
89         automation_mins[AUTOGROUPTYPE_ZOOM] = 0.005;
90         automation_maxs[AUTOGROUPTYPE_ZOOM] = 5.000;
91
92         automation_mins[AUTOGROUPTYPE_SPEED] = 0.005;
93         automation_maxs[AUTOGROUPTYPE_SPEED] = 5.000;
94
95         automation_mins[AUTOGROUPTYPE_X] = -100;
96         automation_maxs[AUTOGROUPTYPE_X] = 100;
97
98         automation_mins[AUTOGROUPTYPE_Y] = -100;
99         automation_maxs[AUTOGROUPTYPE_Y] = 100;
100
101         automation_mins[AUTOGROUPTYPE_INT255] = 0;
102         automation_maxs[AUTOGROUPTYPE_INT255] = 255;
103
104         zoombar_showautotype = AUTOGROUPTYPE_AUDIO_FADE;
105
106         floatauto_type = FloatAuto::SMOOTH;
107
108         red = green = blue = 0;
109         red_max = green_max = blue_max = 0;
110         use_max = 0;
111 }
112
113 LocalSession::~LocalSession()
114 {
115 }
116
117 void LocalSession::copy_from(LocalSession *that)
118 {
119         strcpy(clip_title, that->clip_title);
120         strcpy(clip_notes, that->clip_notes);
121         strcpy(clip_icon, that->clip_icon);
122         awindow_folder = that->awindow_folder;
123         in_point = that->in_point;
124         loop_playback = that->loop_playback;
125         loop_start = that->loop_start;
126         loop_end = that->loop_end;
127         out_point = that->out_point;
128         selectionend = that->selectionend;
129         selectionstart = that->selectionstart;
130         x_pane = that->x_pane;
131         y_pane = that->y_pane;
132
133         for(int i = 0; i < TOTAL_PANES; i++)
134         {
135                 view_start[i] = that->view_start[i];
136                 track_start[i] = that->track_start[i];
137         }
138
139         zoom_sample = that->zoom_sample;
140         zoom_y = that->zoom_y;
141         zoom_track = that->zoom_track;
142         preview_start = that->preview_start;
143         preview_end = that->preview_end;
144         red = that->red;
145         green = that->green;
146         blue = that->blue;
147         red_max = that->red_max;
148         green_max = that->green_max;
149         blue_max = that->blue_max;
150         use_max = that->use_max;
151
152         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
153                 automation_mins[i] = that->automation_mins[i];
154                 automation_maxs[i] = that->automation_maxs[i];
155         }
156         floatauto_type = that->floatauto_type;
157 }
158
159 void LocalSession::save_xml(FileXML *file, double start)
160 {
161         file->tag.set_title("LOCALSESSION");
162
163         file->tag.set_property("IN_POINT", in_point - start);
164         file->tag.set_property("LOOP_PLAYBACK", loop_playback);
165         file->tag.set_property("LOOP_START", loop_start - start);
166         file->tag.set_property("LOOP_END", loop_end - start);
167         file->tag.set_property("OUT_POINT", out_point - start);
168         file->tag.set_property("SELECTION_START", selectionstart - start);
169         file->tag.set_property("SELECTION_END", selectionend - start);
170         file->tag.set_property("CLIP_TITLE", clip_title);
171         file->tag.set_property("CLIP_ICON", clip_icon);
172         file->tag.set_property("AWINDOW_FOLDER", awindow_folder);
173         file->tag.set_property("X_PANE", x_pane);
174         file->tag.set_property("Y_PANE", y_pane);
175
176         char string[BCTEXTLEN];
177         for(int i = 0; i < TOTAL_PANES; i++)
178         {
179                 sprintf(string, "TRACK_START%d", i);
180                 file->tag.set_property(string, track_start[i]);
181                 sprintf(string, "VIEW_START%d", i);
182                 file->tag.set_property(string, view_start[i]);
183         }
184
185         file->tag.set_property("ZOOM_SAMPLE", zoom_sample);
186 //printf("EDLSession::save_session 1\n");
187         file->tag.set_property("ZOOMY", zoom_y);
188 //printf("EDLSession::save_session 1 %d\n", zoom_track);
189         file->tag.set_property("ZOOM_TRACK", zoom_track);
190
191         double preview_start = this->preview_start - start;
192         if( preview_start < 0 ) preview_start = 0;
193         double preview_end = this->preview_end - start;
194         if( preview_end < preview_start ) preview_end = -1;
195         file->tag.set_property("PREVIEW_START", preview_start);
196         file->tag.set_property("PREVIEW_END", preview_end);
197         file->tag.set_property("FLOATAUTO_TYPE", floatauto_type);
198
199         file->tag.set_property("RED", red);
200         file->tag.set_property("GREEN", green);
201         file->tag.set_property("BLUE", blue);
202         file->tag.set_property("RED_MAX", red_max);
203         file->tag.set_property("GREEN_MAX", green_max);
204         file->tag.set_property("BLUE_MAX", blue_max);
205         file->tag.set_property("USE_MAX", use_max);
206
207         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
208                 if (!Automation::autogrouptypes_fixedrange[i]) {
209                         file->tag.set_property(xml_autogrouptypes_titlesmin[i],automation_mins[i]);
210                         file->tag.set_property(xml_autogrouptypes_titlesmax[i],automation_maxs[i]);
211                 }
212         }
213         file->append_tag();
214         file->append_newline();
215
216 //this used to be a property, now used as tag member
217 //      file->tag.set_property("CLIP_NOTES", clip_notes);
218         file->tag.set_title("CLIP_NOTES");   file->append_tag();
219         file->append_text(clip_notes);
220         file->tag.set_title("/CLIP_NOTES");  file->append_tag();
221         file->append_newline();
222
223         file->tag.set_title("/LOCALSESSION");
224         file->append_tag();
225         file->append_newline();
226         file->append_newline();
227 }
228
229 void LocalSession::synchronize_params(LocalSession *that)
230 {
231         loop_playback = that->loop_playback;
232         loop_start = that->loop_start;
233         loop_end = that->loop_end;
234         preview_start = that->preview_start;
235         preview_end = that->preview_end;
236         red = that->red;
237         green = that->green;
238         blue = that->blue;
239         red_max = that->red_max;
240         green_max = that->green_max;
241         blue_max = that->blue_max;
242 }
243
244
245 void LocalSession::load_xml(FileXML *file, unsigned long load_flags)
246 {
247         if(load_flags & LOAD_SESSION)
248         {
249 // moved to EDL::load_xml for paste to fill silence.
250 //              clipboard_length = 0;
251 // Overwritten by MWindow::load_filenames
252                 file->tag.get_property("CLIP_TITLE", clip_title);
253                 clip_notes[0] = 0;
254                 file->tag.get_property("CLIP_NOTES", clip_notes);
255                 clip_icon[0] = 0;
256                 file->tag.get_property("CLIP_ICON", clip_icon);
257 // kludge if possible
258                 if( !clip_icon[0] ) {
259                         char *cp = clip_notes;
260                         int year, mon, mday, hour, min, sec;
261                         while( *cp && *cp++ != ':' );
262                         if( *cp && sscanf(cp, "%d/%02d/%02d %02d:%02d:%02d,",
263                                         &year, &mon, &mday, &hour, &min, &sec) == 6 ) {
264                                 sprintf(clip_icon, "clip_%02d%02d%02d-%02d%02d%02d.png",
265                                         year, mon, mday, hour, min, sec);
266                         }
267                 }
268                 const char *folder = file->tag.get_property("FOLDER");
269                 if( folder ) {
270                         awindow_folder = AWindowGUI::folder_number(folder);
271                         if( awindow_folder < 0 ) awindow_folder = AW_MEDIA_FOLDER;
272                 }
273                 awindow_folder = file->tag.get_property("AWINDOW_FOLDER", awindow_folder);
274                 loop_playback = file->tag.get_property("LOOP_PLAYBACK", 0);
275                 loop_start = file->tag.get_property("LOOP_START", (double)0);
276                 loop_end = file->tag.get_property("LOOP_END", (double)0);
277                 selectionstart = file->tag.get_property("SELECTION_START", (double)0);
278                 selectionend = file->tag.get_property("SELECTION_END", (double)0);
279                 x_pane = file->tag.get_property("X_PANE", -1);
280                 y_pane = file->tag.get_property("Y_PANE", -1);
281
282
283                 char string[BCTEXTLEN];
284                 for(int i = 0; i < TOTAL_PANES; i++)
285                 {
286                         sprintf(string, "TRACK_START%d", i);
287                         track_start[i] = file->tag.get_property(string, track_start[i]);
288                         sprintf(string, "VIEW_START%d", i);
289                         view_start[i] = file->tag.get_property(string, view_start[i]);
290                 }
291
292                 zoom_sample = file->tag.get_property("ZOOM_SAMPLE", zoom_sample);
293                 zoom_y = file->tag.get_property("ZOOMY", zoom_y);
294                 zoom_track = file->tag.get_property("ZOOM_TRACK", zoom_track);
295                 preview_start = file->tag.get_property("PREVIEW_START", preview_start);
296                 preview_end = file->tag.get_property("PREVIEW_END", preview_end);
297                 red = file->tag.get_property("RED", red);
298                 green = file->tag.get_property("GREEN", green);
299                 blue = file->tag.get_property("BLUE", blue);
300                 red_max = file->tag.get_property("RED_MAX", red_max);
301                 green_max = file->tag.get_property("GREEN_MAX", green_max);
302                 blue_max = file->tag.get_property("BLUE_MAX", blue_max);
303                 use_max = file->tag.get_property("USE_MAX", use_max);
304
305                 for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
306                         if (!Automation::autogrouptypes_fixedrange[i]) {
307                                 automation_mins[i] = file->tag.get_property(xml_autogrouptypes_titlesmin[i],automation_mins[i]);
308                                 automation_maxs[i] = file->tag.get_property(xml_autogrouptypes_titlesmax[i],automation_maxs[i]);
309                         }
310                 }
311                 floatauto_type = file->tag.get_property("FLOATAUTO_TYPE", floatauto_type);
312         }
313
314
315 // on operations like cut, paste, slice, clear... we should also undo the cursor position as users
316 // expect - this is additionally important in keyboard-only editing in viewer window
317         if(load_flags & LOAD_SESSION || load_flags & LOAD_TIMEBAR)
318         {
319                 selectionstart = file->tag.get_property("SELECTION_START", (double)0);
320                 selectionend = file->tag.get_property("SELECTION_END", (double)0);
321         }
322
323
324
325         if(load_flags & LOAD_TIMEBAR)
326         {
327                 in_point = file->tag.get_property("IN_POINT", (double)-1);
328                 out_point = file->tag.get_property("OUT_POINT", (double)-1);
329         }
330
331         while( !file->read_tag() ) {
332                 if( file->tag.title_is("/LOCALSESSION") ) break;
333                 if( file->tag.title_is("CLIP_NOTES") ) {
334                         file->read_text_until("/CLIP_NOTES",
335                                 clip_notes, sizeof(clip_notes)-1, 1);
336                 }
337         }
338 }
339
340 void LocalSession::boundaries()
341 {
342         zoom_sample = MAX(1, zoom_sample);
343 }
344
345 int LocalSession::load_defaults(BC_Hash *defaults)
346 {
347         loop_playback = defaults->get("LOOP_PLAYBACK", 0);
348         loop_start = defaults->get("LOOP_START", (double)0);
349         loop_end = defaults->get("LOOP_END", (double)0);
350         selectionstart = defaults->get("SELECTIONSTART", selectionstart);
351         selectionend = defaults->get("SELECTIONEND", selectionend);
352 //      track_start = defaults->get("TRACK_START", 0);
353 //      view_start = defaults->get("VIEW_START", 0);
354         zoom_sample = defaults->get("ZOOM_SAMPLE", DEFAULT_ZOOM_TIME);
355         zoom_y = defaults->get("ZOOMY", 64);
356         zoom_track = defaults->get("ZOOM_TRACK", 64);
357         red = defaults->get("RED", 0.0);
358         green = defaults->get("GREEN", 0.0);
359         blue = defaults->get("BLUE", 0.0);
360         red_max = defaults->get("RED_MAX", 0.0);
361         green_max = defaults->get("GREEN_MAX", 0.0);
362         blue_max = defaults->get("BLUE_MAX", 0.0);
363         use_max = defaults->get("USE_MAX", 0);
364
365         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
366                 if (!Automation::autogrouptypes_fixedrange[i]) {
367                         automation_mins[i] = defaults->get(xml_autogrouptypes_titlesmin[i], automation_mins[i]);
368                         automation_maxs[i] = defaults->get(xml_autogrouptypes_titlesmax[i], automation_maxs[i]);
369                 }
370         }
371
372         floatauto_type = defaults->get("FLOATAUTO_TYPE", floatauto_type);
373         x_pane = defaults->get("X_PANE", x_pane);
374         y_pane = defaults->get("Y_PANE", y_pane);
375
376         return 0;
377 }
378
379 int LocalSession::save_defaults(BC_Hash *defaults)
380 {
381         defaults->update("LOOP_PLAYBACK", loop_playback);
382         defaults->update("LOOP_START", loop_start);
383         defaults->update("LOOP_END", loop_end);
384         defaults->update("SELECTIONSTART", selectionstart);
385         defaults->update("SELECTIONEND", selectionend);
386 //      defaults->update("TRACK_START", track_start);
387 //      defaults->update("VIEW_START", view_start);
388         defaults->update("ZOOM_SAMPLE", zoom_sample);
389         defaults->update("ZOOMY", zoom_y);
390         defaults->update("ZOOM_TRACK", zoom_track);
391         defaults->update("RED", red);
392         defaults->update("GREEN", green);
393         defaults->update("BLUE", blue);
394         defaults->update("RED_MAX", red_max);
395         defaults->update("GREEN_MAX", green_max);
396         defaults->update("BLUE_MAX", blue_max);
397         defaults->update("USE_MAX", use_max);
398
399         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
400                 if (!Automation::autogrouptypes_fixedrange[i]) {
401                         defaults->update(xml_autogrouptypes_titlesmin[i], automation_mins[i]);
402                         defaults->update(xml_autogrouptypes_titlesmax[i], automation_maxs[i]);
403                 }
404         }
405
406         defaults->update("FLOATAUTO_TYPE", floatauto_type);
407         defaults->update("X_PANE", x_pane);
408         defaults->update("Y_PANE", y_pane);
409         return 0;
410 }
411
412 void LocalSession::set_selectionstart(double value)
413 {
414         this->selectionstart = value;
415 }
416
417 void LocalSession::set_selectionend(double value)
418 {
419         this->selectionend = value;
420 }
421
422 void LocalSession::set_inpoint(double value)
423 {
424         in_point = value;
425 }
426
427 void LocalSession::set_outpoint(double value)
428 {
429         out_point = value;
430 }
431
432 void LocalSession::unset_inpoint()
433 {
434         in_point = -1;
435 }
436
437 void LocalSession::unset_outpoint()
438 {
439         out_point = -1;
440 }
441
442 void LocalSession::set_playback_start(double value)
443 {
444         if( playback_end < 0 ) return;
445         playback_start = value;
446         playback_end = -1;
447 }
448
449 void LocalSession::set_playback_end(double value)
450 {
451         if( value < playback_start ) {
452                 if( playback_end < 0 )
453                         playback_end = playback_start;
454                 playback_start = value;
455         }
456         else if( playback_end < 0 || value > playback_end )
457                 playback_end = value;
458 }
459
460
461 double LocalSession::get_selectionstart(int highlight_only)
462 {
463         if(highlight_only || !EQUIV(selectionstart, selectionend))
464                 return selectionstart;
465
466         if(in_point >= 0)
467                 return in_point;
468         else
469         if(out_point >= 0)
470                 return out_point;
471         else
472                 return selectionstart;
473 }
474
475 double LocalSession::get_selectionend(int highlight_only)
476 {
477         if(highlight_only || !EQUIV(selectionstart, selectionend))
478                 return selectionend;
479
480         if(out_point >= 0)
481                 return out_point;
482         else
483         if(in_point >= 0)
484                 return in_point;
485         else
486                 return selectionend;
487 }
488
489 double LocalSession::get_inpoint()
490 {
491         return in_point;
492 }
493
494 double LocalSession::get_outpoint()
495 {
496         return out_point;
497 }
498
499 int LocalSession::inpoint_valid()
500 {
501         return in_point >= 0;
502 }
503
504 int LocalSession::outpoint_valid()
505 {
506         return out_point >= 0;
507 }
508