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