prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / pluginset.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 "bcsignals.h"
23 #include "edl.h"
24 #include "edlsession.h"
25 #include "filexml.h"
26 #include "keyframe.h"
27 #include "keyframes.h"
28 #include "plugin.h"
29 #include "pluginautos.h"
30 #include "pluginset.h"
31 #include "track.h"
32
33 #include <string.h>
34
35 PluginSet::PluginSet(EDL *edl, Track *track)
36  : Edits(edl, track)
37 {
38         record = 1;
39 }
40
41 PluginSet::~PluginSet()
42 {
43         while(last) delete last;
44 }
45
46
47 PluginSet& PluginSet::operator=(PluginSet& plugins)
48 {
49 printf("PluginSet::operator= 1\n");
50         copy_from(&plugins);
51         return *this;
52 }
53
54 void PluginSet::copy_from(PluginSet *src)
55 {
56         while(last) delete last;
57         for(Plugin *current = (Plugin*)src->first; current; current = (Plugin*)NEXT)
58         {
59                 Plugin *new_plugin;
60                 append(new_plugin = (Plugin*)create_edit());
61                 new_plugin->copy_from(current);
62         }
63         this->record = src->record;
64 }
65
66 Plugin* PluginSet::get_first_plugin()
67 {
68 // Called when a new pluginset is added.
69 // Get first non-silence plugin in the plugin set.
70         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
71         {
72                 if(current && current->plugin_type != PLUGIN_NONE)
73                 {
74                         return current;
75                 }
76         }
77         return 0;
78 }
79
80 int64_t PluginSet::plugin_change_duration(int64_t input_position, 
81         int64_t input_length, 
82         int reverse)
83 {
84         int result = input_length;
85         Edit *current;
86
87         if(reverse)
88         {
89                 int input_start = input_position - input_length;
90                 for(current = last; current; current = PREVIOUS)
91                 {
92                         int start = current->startproject;
93                         int end = start + current->length;
94                         if(end > input_start && end < input_position)
95                         {
96                                 result = input_position - end;
97                                 return result;
98                         }
99                         else
100                         if(start > input_start && start < input_position)
101                         {
102                                 result = input_position - start;
103                                 return result;
104                         }
105                 }
106         }
107         else
108         {
109                 int input_end = input_position + input_length;
110                 for(current = first; current; current = NEXT)
111                 {
112                         int start = current->startproject;
113                         int end = start + current->length;
114                         if(start > input_position && start < input_end)
115                         {
116                                 result = start - input_position;
117                                 return result;
118                         }
119                         else
120                         if(end > input_position && end < input_end)
121                         {
122                                 result = end - input_position;
123                                 return result;
124                         }
125                 }
126         }
127         return input_length;
128 }
129
130 void PluginSet::synchronize_params(PluginSet *plugin_set)
131 {
132         for(Plugin *this_plugin = (Plugin*)first, *that_plugin = (Plugin*)plugin_set->first;
133                 this_plugin && that_plugin;
134                 this_plugin = (Plugin*)this_plugin->next, that_plugin = (Plugin*)that_plugin->next)
135         {
136                 this_plugin->synchronize_params(that_plugin);
137         }
138 }
139
140 Plugin* PluginSet::insert_plugin(const char *title, 
141         int64_t unit_position, 
142         int64_t unit_length,
143         int plugin_type,
144         SharedLocation *shared_location,
145         KeyFrame *default_keyframe,
146         int do_optimize)
147 {
148         Plugin *plugin = (Plugin*)paste_silence(unit_position, 
149                 unit_position + unit_length);
150
151
152         if(title) strcpy(plugin->title, title);
153
154         if(shared_location) plugin->shared_location = *shared_location;
155
156         plugin->plugin_type = plugin_type;
157
158         if(default_keyframe) 
159                 *plugin->keyframes->default_auto = *default_keyframe;
160         plugin->keyframes->default_auto->position = unit_position;
161
162 // May delete the plugin we just added so not desirable while loading.
163         if(do_optimize) optimize();
164         return plugin;
165 }
166
167 Edit* PluginSet::create_edit()
168 {
169         Plugin* result = new Plugin(edl, this, "");
170         return result;
171 }
172
173 Edit* PluginSet::insert_edit_after(Edit *previous_edit)
174 {
175         Plugin *current = new Plugin(edl, this, "");
176         List<Edit>::insert_after(previous_edit, current);
177         return (Edit*)current;
178 }
179
180
181 int PluginSet::get_number()
182 {
183         return track->plugin_set.number_of(this);
184 }
185
186 void PluginSet::clear(int64_t start, int64_t end, int edit_autos)
187 {
188         if(edit_autos)
189         {
190 // Clear keyframes
191                 for(Plugin *current = (Plugin*)first;
192                         current;
193                         current = (Plugin*)NEXT)
194                 {
195                         current->keyframes->clear(start, end, 1);
196                 }
197         }
198
199 // Clear edits
200         Edits::clear(start, end);
201 }
202
203 //void PluginSet::clear_recursive(int64_t start, int64_t end)
204 //{
205 //printf("PluginSet::clear_recursive 1\n");
206 //      clear(start, end, 1);
207 //}
208
209 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
210 {
211 // Plugin keyframes are shifted in shift_effects
212 }
213
214 void PluginSet::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
215 {
216 // Effects are shifted in length extension
217 }
218
219
220 void PluginSet::clear_keyframes(int64_t start, int64_t end)
221 {
222         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
223         {
224                 current->clear_keyframes(start, end);
225         }
226 }
227
228 void PluginSet::copy_keyframes(int64_t start, 
229         int64_t end, 
230         FileXML *file, 
231         int default_only,
232         int active_only)
233 {
234         file->tag.set_title("PLUGINSET");       
235         file->append_tag();
236         file->append_newline();
237
238         for(Plugin *current = (Plugin*)first; 
239                 current; 
240                 current = (Plugin*)NEXT)
241         {
242                 current->copy_keyframes(start, end, file, default_only, active_only);
243         }
244
245         file->tag.set_title("/PLUGINSET");      
246         file->append_tag();
247         file->append_newline();
248 }
249
250
251 void PluginSet::paste_keyframes(int64_t start, 
252         int64_t length, 
253         FileXML *file, 
254         int default_only,
255         int active_only)
256 {
257         int result = 0;
258         int first_keyframe = 1;
259         Plugin *current;
260
261
262         while(!result)
263         {
264                 result = file->read_tag();
265
266                 if(!result)
267                 {
268                         if(file->tag.title_is("/PLUGINSET"))
269                                 result = 1;
270                         else
271                         if(file->tag.title_is("KEYFRAME"))
272                         {
273                                 int64_t position = file->tag.get_property("POSITION", 0);
274                                 if(first_keyframe && default_only)
275                                 {
276                                         position = start;
277                                 }
278                                 else
279                                 {
280                                         position += start;
281                                 }
282
283 // Get plugin owning keyframe
284                                 for(current = (Plugin*)last; 
285                                         current;
286                                         current = (Plugin*)PREVIOUS)
287                                 {
288 // We want keyframes to exist beyond the end of the last plugin to
289 // make editing intuitive, but it will always be possible to 
290 // paste keyframes from one plugin into an incompatible plugin.
291                                         if(position >= current->startproject)
292                                         {
293                                                 KeyFrame *keyframe = 0;
294                                                 if(file->tag.get_property("DEFAULT", 0) || default_only)
295                                                 {
296                                                         keyframe = (KeyFrame*)current->keyframes->default_auto;
297                                                 }
298                                                 else
299                                                 if(!default_only)
300                                                 {
301                                                         keyframe = 
302                                                                 (KeyFrame*)current->keyframes->insert_auto(position);
303                                                 }
304
305                                                 if(keyframe)
306                                                 {
307                                                         keyframe->load(file);
308                                                         keyframe->position = position;
309                                                 }
310                                                 break;
311                                         }
312                                 }
313
314                                 first_keyframe = 0;
315                         }
316                 }
317         }
318 }
319
320 void PluginSet::shift_effects(int64_t start, int64_t length, int edit_autos)
321 {
322         for(Plugin *current = (Plugin*)first;
323                 current;
324                 current = (Plugin*)NEXT)
325         {
326 // Shift beginning of this effect
327                 if(current->startproject >= start)
328                 {
329                         current->startproject += length;
330                 }
331                 else
332 // Extend end of this effect.
333 // In loading new files, the effect should extend to fill the entire track.
334 // In muting, the effect must extend to fill the gap if another effect follows.
335 // The user should use Settings->edit effects to disable this.
336                 if(current->startproject + current->length >= start)
337                 {
338                         current->length += length;
339                 }
340
341 // Shift keyframes in this effect.
342 // If the default keyframe lands on the starting point, it must be shifted
343 // since the effect start is shifted.
344                 if(edit_autos && current->keyframes->default_auto->position >= start)
345                         current->keyframes->default_auto->position += length;
346
347                 if(edit_autos) current->keyframes->paste_silence(start, start + length);
348         }
349 }
350
351 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
352 {
353         file->tag.set_title("PLUGINSET");       
354         file->tag.set_property("RECORD", record);
355         file->append_tag();
356         file->append_newline();
357
358         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
359         {
360                 current->copy(start, end, file);
361         }
362
363         file->tag.set_title("/PLUGINSET");      
364         file->append_tag();
365         file->append_newline();
366 }
367
368 void PluginSet::save(FileXML *file)
369 {
370         copy(0, length(), file);
371 }
372
373 void PluginSet::load(FileXML *file, uint32_t load_flags)
374 {
375         int result = 0;
376 // Current plugin being amended
377         Plugin *plugin = (Plugin*)first;
378         int64_t startproject = 0;
379
380         record = file->tag.get_property("RECORD", record);
381         do{
382                 result = file->read_tag();
383
384
385                 if(!result)
386                 {
387                         if(file->tag.title_is("/PLUGINSET"))
388                         {
389                                 result = 1;
390                         }
391                         else
392                         if(file->tag.title_is("PLUGIN"))
393                         {
394                                 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
395                                 int plugin_type = file->tag.get_property("TYPE", 1);
396                                 char title[BCTEXTLEN];
397                                 title[0] = 0;
398                                 file->tag.get_property("TITLE", title);
399                                 SharedLocation shared_location;
400                                 shared_location.load(file);
401
402
403                                 if(load_flags & LOAD_EDITS)
404                                 {
405                                         plugin = insert_plugin(title, 
406                                                 startproject, 
407                                                 length,
408                                                 plugin_type,
409                                                 &shared_location,
410                                                 0,
411                                                 0);
412                                         plugin->load(file);
413                                         startproject += length;
414                                 }
415                                 else
416                                 if(load_flags & LOAD_AUTOMATION)
417                                 {
418                                         if(plugin)
419                                         {
420                                                 plugin->load(file);
421                                                 plugin = (Plugin*)plugin->next;
422                                         }
423                                 }
424                         }
425                 }
426         }while(!result);
427 }
428
429
430
431 int PluginSet::optimize()
432 {
433         int result = 1;
434         Plugin *current_edit;
435
436
437 // Delete keyframes out of range
438         for(current_edit = (Plugin*)first;
439                 current_edit; 
440                 current_edit = (Plugin*)current_edit->next)
441         {
442                 current_edit->keyframes->default_auto->position = 0;
443                 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
444                         current_keyframe; )
445                 {
446                         KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
447                         if(current_keyframe->position > 
448                                 current_edit->startproject + current_edit->length ||
449                                 current_keyframe->position < current_edit->startproject)
450                         {
451                                 delete current_keyframe;
452                         }
453                         current_keyframe = previous_keyframe;
454                 }
455         }
456
457 // Insert silence between plugins
458         for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
459         {
460                 if(current->previous)
461                 {
462                         Plugin *previous = (Plugin*)PREVIOUS;
463
464                         if(current->startproject - 
465                                 previous->startproject - 
466                                 previous->length > 0)
467                         {
468                                 Plugin *new_plugin = (Plugin*)create_edit();
469                                 insert_before(current, new_plugin);
470                                 new_plugin->startproject = previous->startproject + 
471                                         previous->length;
472                                 new_plugin->length = current->startproject - 
473                                         previous->startproject - 
474                                         previous->length;
475                         }
476                 }
477                 else
478                 if(current->startproject > 0)
479                 {
480                         Plugin *new_plugin = (Plugin*)create_edit();
481                         insert_before(current, new_plugin);
482                         new_plugin->length = current->startproject;
483                 }
484         }
485
486
487 // delete 0 length plugins
488         while(result)
489         {
490                 result = 0;
491
492                 for(current_edit = (Plugin*)first; 
493                         current_edit && !result; )
494                 {
495                         if(current_edit->length == 0)
496                         {
497                                 Plugin* next = (Plugin*)current_edit->next;
498                                 delete current_edit;
499                                 result = 1;
500                                 current_edit = next;
501                         }
502                         else
503                                 current_edit = (Plugin*)current_edit->next;
504                 }
505
506
507 // merge identical plugins with same keyframes
508                 for(current_edit = (Plugin*)first; 
509                         current_edit && current_edit->next && !result; )
510                 {
511                         Plugin *next_edit = (Plugin*)current_edit->next;
512
513
514 // plugins identical
515                         if(next_edit->identical(current_edit))
516                 {
517                         current_edit->length += next_edit->length;
518 // Merge keyframes
519                                 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
520                                         source;
521                                         source = (KeyFrame*)source->next)
522                                 {
523                                         KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
524                                         *dest = *source;
525                                         current_edit->keyframes->append(dest);
526                                 }
527                         remove(next_edit);
528                         result = 1;
529                 }
530
531                 current_edit = (Plugin*)current_edit->next;
532                 }
533
534 // delete last edit if 0 length or silence
535                 if(last)
536                 {
537                         if(last->silence() || !last->length)
538                         {
539                                 delete last;
540                                 result = 1;
541                         }
542                 }
543         }
544
545         return 0;
546 }
547
548
549
550
551
552 void PluginSet::dump(FILE *fp)
553 {
554         fprintf(fp,"   PLUGIN_SET:\n");
555         for(Plugin *current = (Plugin*)first; current; current =  (Plugin*)NEXT)
556                 current->dump(fp);
557 }