aaec69d86e107e92da749a70a46a63b3a7b6d26e
[goodguy/history.git] / cinelerra-5.1 / cinelerra / pluginclient.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-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 "bcdisplayinfo.h"
23 #include "bchash.h"
24 #include "bcsignals.h"
25 #include "clip.h"
26 #include "condition.h"
27 #include "edits.h"
28 #include "edit.h"
29 #include "edl.h"
30 #include "edlsession.h"
31 #include "file.h"
32 #include "filesystem.h"
33 #include "indexable.h"
34 #include "language.h"
35 #include "localsession.h"
36 #include "mainundo.h"
37 #include "mwindow.h"
38 #include "plugin.h"
39 #include "pluginclient.h"
40 #include "pluginserver.h"
41 #include "preferences.h"
42 #include "track.h"
43 #include "transportque.inc"
44
45
46 #include <ctype.h>
47 #include <errno.h>
48 #include <string.h>
49
50
51
52
53
54 PluginClientThread::PluginClientThread(PluginClient *client)
55  : Thread(1, 0, 0)
56 {
57         this->client = client;
58         window = 0;
59         init_complete = new Condition(0, "PluginClientThread::init_complete");
60 }
61
62 PluginClientThread::~PluginClientThread()
63 {
64         join();
65 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
66         delete window;  window = 0;
67 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
68         delete init_complete;
69 }
70
71 void PluginClientThread::run()
72 {
73         BC_DisplayInfo info;
74         int result = 0;
75         if(client->window_x < 0) client->window_x = info.get_abs_cursor_x();
76         if(client->window_y < 0) client->window_y = info.get_abs_cursor_y();
77         if(!window)
78                 window = (PluginClientWindow*)client->new_window();
79
80         if(window) {
81                 window->lock_window("PluginClientThread::run");
82                 window->create_objects();
83                 window->unlock_window();
84
85 /* Only set it here so tracking doesn't update it until everything is created. */
86                 client->thread = this;
87                 init_complete->unlock();
88
89                 result = window->run_window();
90                 window->lock_window("PluginClientThread::run");
91 //printf("PluginClientThread::run %p %d\n", this, __LINE__);
92                 window->hide_window(1);
93                 window->unlock_window();
94                 window->done_event(result);
95 // Can't save defaults in the destructor because it's not called immediately
96 // after closing.
97                 /* if(client->defaults) */ client->save_defaults_xml();
98 /* This is needed when the GUI is closed from itself */
99                 if(result) client->client_side_close();
100         }
101         else
102 // No window
103         {
104                 client->thread = this;
105                 init_complete->unlock();
106         }
107 }
108
109 BC_WindowBase* PluginClientThread::get_window()
110 {
111         return window;
112 }
113
114 PluginClient* PluginClientThread::get_client()
115 {
116         return client;
117 }
118
119
120
121
122
123
124 PluginClientFrame::PluginClientFrame(int data_size,
125         int period_n,
126         int period_d)
127 {
128         this->data_size = data_size;
129         force = 0;
130         this->period_n = period_n;
131         this->period_d = period_d;
132 }
133
134 PluginClientFrame::~PluginClientFrame()
135 {
136
137 }
138
139
140
141
142
143 PluginClientWindow::PluginClientWindow(PluginClient *client,
144         int w,
145         int h,
146         int min_w,
147         int min_h,
148         int allow_resize)
149  : BC_Window(client->gui_string,
150         client->window_x /* - w / 2 */,
151         client->window_y /* - h / 2 */,
152         w, h, min_w, min_h, allow_resize, 0, 1)
153 {
154         this->client = client;
155 }
156
157 PluginClientWindow::PluginClientWindow(const char *title,
158         int x, int y, int w, int h, int min_w, int min_h, int allow_resize)
159  : BC_Window(title, x, y, w, h, min_w, min_h, allow_resize, 0, 1)
160 {
161         this->client = 0;
162 }
163
164 PluginClientWindow::~PluginClientWindow()
165 {
166 }
167
168
169 int PluginClientWindow::translation_event()
170 {
171         if(client)
172         {
173                 client->window_x = get_x();
174                 client->window_y = get_y();
175         }
176
177         return 1;
178 }
179
180 int PluginClientWindow::close_event()
181 {
182 /* Set result to 1 to indicate a client side close */
183         set_done(1);
184         return 1;
185 }
186
187
188
189
190
191 PluginClient::PluginClient(PluginServer *server)
192 {
193         reset();
194         this->server = server;
195         smp = server->preferences->project_smp;
196         defaults = 0;
197         update_timer = new Timer;
198 // Virtual functions don't work here.
199 }
200
201 PluginClient::~PluginClient()
202 {
203 // Delete the GUI thread.  The GUI must be hidden with hide_gui first.
204         delete thread;
205
206 // Virtual functions don't work here.
207         if(defaults) delete defaults;
208         frame_buffer.remove_all_objects();
209         delete update_timer;
210 }
211
212 int PluginClient::reset()
213 {
214         window_x = -1;
215         window_y = -1;
216         interactive = 0;
217         show_initially = 0;
218         wr = rd = 0;
219         master_gui_on = 0;
220         client_gui_on = 0;
221         realtime_priority = 0;
222         gui_string[0] = 0;
223         total_in_buffers = 0;
224         total_out_buffers = 0;
225         source_position = 0;
226         source_start = 0;
227         total_len = 0;
228         direction = PLAY_FORWARD;
229         thread = 0;
230         using_defaults = 0;
231         return 0;
232 }
233
234
235 void PluginClient::hide_gui()
236 {
237         if(thread && thread->window)
238         {
239 //printf("PluginClient::delete_thread %d\n", __LINE__);
240 /* This is needed when the GUI is closed from elsewhere than itself */
241 /* Since we now use autodelete, this is all that has to be done, thread will take care of itself ... */
242 /* Thread join will wait if this was not called from the thread itself or go on if it was */
243                 thread->window->lock_window("PluginClient::hide_gui");
244                 thread->window->set_done(0);
245 //printf("PluginClient::hide_gui %d thread->window=%p\n", __LINE__, thread->window);
246                 thread->window->unlock_window();
247 //printf("PluginClient::delete_thread %d\n", __LINE__);
248         }
249 }
250
251 // For realtime plugins initialize buffers
252 int PluginClient::plugin_init_realtime(int realtime_priority,
253         int total_in_buffers,
254         int buffer_size)
255 {
256
257 // Get parameters for all
258         master_gui_on = get_gui_status();
259
260
261
262 // get parameters depending on video or audio
263         init_realtime_parameters();
264
265         this->realtime_priority = realtime_priority;
266         this->total_in_buffers = this->total_out_buffers = total_in_buffers;
267         this->out_buffer_size = this->in_buffer_size = buffer_size;
268         return 0;
269 }
270
271 int PluginClient::plugin_start_loop(int64_t start,
272         int64_t end,
273         int64_t buffer_size,
274         int total_buffers)
275 {
276 //printf("PluginClient::plugin_start_loop %d %ld %ld %ld %d\n",
277 // __LINE__, start, end, buffer_size, total_buffers);
278         this->source_start = start;
279         this->total_len = end - start;
280         this->start = start;
281         this->end = end;
282         this->in_buffer_size = this->out_buffer_size = buffer_size;
283         this->total_in_buffers = this->total_out_buffers = total_buffers;
284         start_loop();
285         return 0;
286 }
287
288 int PluginClient::plugin_process_loop()
289 {
290         return process_loop();
291 }
292
293 int PluginClient::plugin_stop_loop()
294 {
295         return stop_loop();
296 }
297
298 MainProgressBar* PluginClient::start_progress(char *string, int64_t length)
299 {
300         return server->start_progress(string, length);
301 }
302
303
304 // Non realtime parameters
305 int PluginClient::plugin_get_parameters()
306 {
307         int result = get_parameters();
308         if(defaults) save_defaults();
309         return result;
310 }
311
312 // ========================= main loop
313
314 int PluginClient::is_multichannel() { return 0; }
315 int PluginClient::is_synthesis() { return 0; }
316 int PluginClient::is_realtime() { return 0; }
317 int PluginClient::is_fileio() { return 0; }
318 int PluginClient::delete_buffer_ptrs() { return 0; }
319 const char* PluginClient::plugin_title() { return _("Untitled"); }
320
321 Theme* PluginClient::new_theme() { return 0; }
322
323 int PluginClient::load_configuration()
324 {
325         return 0;
326 }
327
328 Theme* PluginClient::get_theme()
329 {
330         return server->get_theme();
331 }
332
333 int PluginClient::show_gui()
334 {
335         load_configuration();
336         thread = new PluginClientThread(this);
337         thread->start();
338         thread->init_complete->lock("PluginClient::show_gui");
339 // Must wait before sending any hide_gui
340         if( !thread->window ) return 1;
341         thread->window->init_wait();
342         return 0;
343 }
344
345 void PluginClient::raise_window()
346 {
347         if(thread && thread->window)
348         {
349                 thread->window->lock_window("PluginClient::raise_window");
350                 thread->window->raise_window();
351                 thread->window->flush();
352                 thread->window->unlock_window();
353         }
354 }
355
356 int PluginClient::set_string()
357 {
358         if(thread)
359         {
360                 thread->window->lock_window("PluginClient::set_string");
361                 thread->window->put_title(gui_string);
362                 thread->window->unlock_window();
363         }
364         return 0;
365 }
366
367
368
369
370
371 void PluginClient::begin_process_buffer()
372 {
373 // Delete all unused GUI frames
374         frame_buffer.remove_all_objects();
375 }
376
377
378 void PluginClient::end_process_buffer()
379 {
380         if(frame_buffer.size())
381         {
382                 send_render_gui();
383         }
384 }
385
386
387
388 void PluginClient::plugin_update_gui()
389 {
390
391         update_gui();
392
393 // Delete unused GUI frames
394         while(frame_buffer.size() > MAX_FRAME_BUFFER)
395                 frame_buffer.remove_object_number(0);
396
397 }
398
399 void PluginClient::update_gui()
400 {
401 }
402
403 int PluginClient::get_gui_update_frames()
404 {
405         if(frame_buffer.size())
406         {
407                 PluginClientFrame *frame = frame_buffer.get(0);
408                 int total_frames = update_timer->get_difference() *
409                         frame->period_d /
410                         frame->period_n /
411                         1000;
412                 if(total_frames) update_timer->subtract(total_frames *
413                         frame->period_n *
414                         1000 /
415                         frame->period_d);
416
417 // printf("PluginClient::get_gui_update_frames %d %ld %d %d %d\n",
418 // __LINE__,
419 // update_timer->get_difference(),
420 // frame->period_n * 1000 / frame->period_d,
421 // total_frames,
422 // frame_buffer.size());
423
424 // Add forced frames
425                 for(int i = 0; i < frame_buffer.size(); i++)
426                         if(frame_buffer.get(i)->force) total_frames++;
427                 total_frames = MIN(frame_buffer.size(), total_frames);
428
429
430                 return total_frames;
431         }
432         else
433         {
434                 return 0;
435         }
436 }
437
438 PluginClientFrame* PluginClient::get_gui_frame()
439 {
440         if(frame_buffer.size())
441         {
442                 PluginClientFrame *frame = frame_buffer.get(0);
443                 frame_buffer.remove_number(0);
444                 return frame;
445         }
446         else
447         {
448                 return 0;
449         }
450 }
451
452 void PluginClient::add_gui_frame(PluginClientFrame *frame)
453 {
454         frame_buffer.append(frame);
455 }
456
457 void PluginClient::send_render_gui()
458 {
459         server->send_render_gui(&frame_buffer);
460 }
461
462 void PluginClient::send_render_gui(void *data)
463 {
464         server->send_render_gui(data);
465 }
466
467 void PluginClient::send_render_gui(void *data, int size)
468 {
469         server->send_render_gui(data, size);
470 }
471
472 void PluginClient::plugin_render_gui(void *data, int size)
473 {
474         render_gui(data, size);
475 }
476
477
478 void PluginClient::plugin_render_gui(void *data)
479 {
480         render_gui(data);
481 }
482
483 void PluginClient::render_gui(void *data)
484 {
485         if(thread)
486         {
487                 thread->get_window()->lock_window("PluginClient::render_gui");
488
489 // Set all previous frames to draw immediately
490                 for(int i = 0; i < frame_buffer.size(); i++)
491                         frame_buffer.get(i)->force = 1;
492
493                 ArrayList<PluginClientFrame*> *src =
494                         (ArrayList<PluginClientFrame*>*)data;
495
496 // Shift GUI data to GUI client
497                 while(src->size())
498                 {
499                         this->frame_buffer.append(src->get(0));
500                         src->remove_number(0);
501                 }
502
503 // Start the timer for the current buffer
504                 update_timer->update();
505                 thread->get_window()->unlock_window();
506         }
507 }
508
509 void PluginClient::render_gui(void *data, int size)
510 {
511         printf("PluginClient::render_gui %d\n", __LINE__);
512 }
513
514
515
516
517
518
519
520
521 int PluginClient::is_audio() { return 0; }
522 int PluginClient::is_video() { return 0; }
523 int PluginClient::is_theme() { return 0; }
524 int PluginClient::uses_gui() { return 1; }
525 int PluginClient::is_transition() { return 0; }
526 int PluginClient::load_defaults()
527 {
528 //      printf("PluginClient::load_defaults undefined in %s.\n", plugin_title());
529         return 0;
530 }
531
532 int PluginClient::save_defaults()
533 {
534         save_defaults_xml();
535 //      printf("PluginClient::save_defaults undefined in %s.\n", plugin_title());
536         return 0;
537 }
538
539 void PluginClient::load_defaults_xml()
540 {
541         char path[BCTEXTLEN];
542         server->get_defaults_path(path);
543         FileSystem fs;
544         fs.complete_path(path);
545         using_defaults = 1;
546 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
547
548         KeyFrame temp_keyframe;
549         FILE *fd = fopen(path, "r");
550         if(fd)
551         {
552                 char *data = temp_keyframe.get_data();
553                 int data_size = fread(data, 1, MESSAGESIZE-1, fd);
554                 if( data_size < 0 ) data_size = 0;
555                 if( data_size > 0 )
556                 {
557 // Get window extents
558                         int state = 0;
559                         for(int i = 0; i < data_size - 8; i++)
560                         {
561                                 if(data[i] == '<') break;
562                                 if(isdigit(data[i]))
563                                 {
564                                         if(state == 0)
565                                         {
566                                                 window_x = atoi(data + i);
567                                                 state++;
568                                         }
569                                         else
570                                         {
571                                                 window_y = atoi(data + i);
572                                                 break;
573                                         }
574                                         while(i < data_size && isdigit(data[i])) i++;
575                                 }
576                         }
577                         data[data_size] = 0;
578                         read_data(&temp_keyframe);
579                 }
580
581                 fclose(fd);
582         }
583         using_defaults = 0;
584 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
585 }
586
587 void PluginClient::save_defaults_xml()
588 {
589         char path[BCTEXTLEN];
590         server->get_defaults_path(path);
591         FileSystem fs;
592         fs.complete_path(path);
593         using_defaults = 1;
594
595         KeyFrame temp_keyframe;
596         save_data(&temp_keyframe);
597
598         const char *data = temp_keyframe.get_data();
599         int len = strlen(data);
600         FILE *fp = fopen(path, "w");
601
602         if( fp ) {
603                 fprintf(fp, "%d\n%d\n", window_x, window_y);
604                 if( len > 0 && !fwrite(data, len, 1, fp) ) {
605                         fprintf(stderr, "PluginClient::save_defaults_xml %d \"%s\" %d bytes: %s\n",
606                                 __LINE__, path, len, strerror(errno));
607                 }
608                 fclose(fp);
609         }
610
611         using_defaults = 0;
612 }
613
614 int PluginClient::is_defaults()
615 {
616         return using_defaults;
617 }
618
619 BC_Hash* PluginClient::get_defaults()
620 {
621         return defaults;
622 }
623 PluginClientThread* PluginClient::get_thread()
624 {
625         return thread;
626 }
627
628 BC_WindowBase* PluginClient::new_window()
629 {
630         printf("PluginClient::new_window undefined in %s.\n", plugin_title());
631         return 0;
632 }
633 int PluginClient::get_parameters() { return 0; }
634 int PluginClient::get_samplerate() { return get_project_samplerate(); }
635 double PluginClient::get_framerate() { return get_project_framerate(); }
636 int PluginClient::init_realtime_parameters() { return 0; }
637 int PluginClient::delete_nonrealtime_parameters() { return 0; }
638 int PluginClient::start_loop() { return 0; };
639 int PluginClient::process_loop() { return 0; };
640 int PluginClient::stop_loop() { return 0; };
641
642 void PluginClient::set_interactive()
643 {
644         interactive = 1;
645 }
646
647 int64_t PluginClient::get_in_buffers(int64_t recommended_size)
648 {
649         return recommended_size;
650 }
651
652 int64_t PluginClient::get_out_buffers(int64_t recommended_size)
653 {
654         return recommended_size;
655 }
656
657 int PluginClient::get_gui_status()
658 {
659         return server->get_gui_status();
660 }
661
662 // close event from client side
663 void PluginClient::client_side_close()
664 {
665 // Last command executed
666         server->client_side_close();
667 }
668
669 int PluginClient::stop_gui_client()
670 {
671         if(!client_gui_on) return 0;
672         client_gui_on = 0;
673         return 0;
674 }
675
676 int PluginClient::get_project_samplerate()
677 {
678         return server->get_project_samplerate();
679 }
680
681 double PluginClient::get_project_framerate()
682 {
683         return server->get_project_framerate();
684 }
685
686 const char *PluginClient::get_source_path()
687 {
688         int64_t source_position = server->plugin->startproject;
689         Edit *edit = server->plugin->track->edits->editof(source_position,PLAY_FORWARD,0);
690         Indexable *indexable = edit ? edit->get_source() : 0;
691         return indexable ? indexable->path : 0;
692 }
693
694
695 void PluginClient::update_display_title()
696 {
697         server->generate_display_title(gui_string);
698         set_string();
699 }
700
701 char* PluginClient::get_gui_string()
702 {
703         return gui_string;
704 }
705
706
707 char* PluginClient::get_path()
708 {
709         return server->path;
710 }
711
712 char* PluginClient::get_plugin_dir()
713 {
714         return server->preferences->plugin_dir;
715 }
716
717 int PluginClient::set_string_client(char *string)
718 {
719         strcpy(gui_string, string);
720         set_string();
721         return 0;
722 }
723
724
725 int PluginClient::get_interpolation_type()
726 {
727         return server->get_interpolation_type();
728 }
729
730
731 float PluginClient::get_red()
732 {
733         EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
734         return !edl ? 0 : edl->local_session->use_max ?
735                 edl->local_session->red_max :
736                 edl->local_session->red;
737 }
738
739 float PluginClient::get_green()
740 {
741         EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
742         return !edl ? 0 : edl->local_session->use_max ?
743                 edl->local_session->green_max :
744                 edl->local_session->green;
745 }
746
747 float PluginClient::get_blue()
748 {
749         EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
750         return !edl ? 0 : edl->local_session->use_max ?
751                 edl->local_session->blue_max :
752                 edl->local_session->blue;
753 }
754
755
756 int64_t PluginClient::get_source_position()
757 {
758         return source_position;
759 }
760
761 int64_t PluginClient::get_source_start()
762 {
763         return source_start;
764 }
765
766 int64_t PluginClient::get_total_len()
767 {
768         return total_len;
769 }
770
771 int PluginClient::get_direction()
772 {
773         return direction;
774 }
775
776
777 int64_t PluginClient::local_to_edl(int64_t position)
778 {
779         return position;
780 }
781
782 int64_t PluginClient::edl_to_local(int64_t position)
783 {
784         return position;
785 }
786
787 int PluginClient::get_use_opengl()
788 {
789         return server->get_use_opengl();
790 }
791
792 int PluginClient::get_total_buffers()
793 {
794         return total_in_buffers;
795 }
796
797 int PluginClient::get_buffer_size()
798 {
799         return in_buffer_size;
800 }
801
802 int PluginClient::get_project_smp()
803 {
804 //printf("PluginClient::get_project_smp %d %d\n", __LINE__, smp);
805         return smp;
806 }
807
808 const char* PluginClient::get_defaultdir()
809 {
810         return File::get_plugin_path();
811 }
812
813
814 int PluginClient::send_hide_gui()
815 {
816 // Stop the GUI server and delete GUI messages
817         client_gui_on = 0;
818         return 0;
819 }
820
821 int PluginClient::send_configure_change()
822 {
823         if(server->mwindow)
824                 server->mwindow->undo->update_undo_before(_("tweek"), this);
825 #ifdef USE_KEYFRAME_SPANNING
826         KeyFrame keyframe;
827         keyframe.copy_from(server->get_keyframe());
828         save_data(&keyframe);
829         server->apply_keyframe(&keyframe);
830 #else
831         KeyFrame* keyframe = server->get_keyframe();
832 // Call save routine in plugin
833         save_data(keyframe);
834 #endif
835         if(server->mwindow)
836                 server->mwindow->undo->update_undo_after(_("tweek"), LOAD_AUTOMATION);
837         server->sync_parameters();
838         return 0;
839 }
840
841
842 KeyFrame* PluginClient::get_prev_keyframe(int64_t position, int is_local)
843 {
844         if(is_local) position = local_to_edl(position);
845         return server->get_prev_keyframe(position);
846 }
847
848 KeyFrame* PluginClient::get_next_keyframe(int64_t position, int is_local)
849 {
850         if(is_local) position = local_to_edl(position);
851         return server->get_next_keyframe(position);
852 }
853
854 void PluginClient::get_camera(float *x, float *y, float *z, int64_t position)
855 {
856         server->get_camera(x, y, z, position, direction);
857 }
858
859 void PluginClient::get_projector(float *x, float *y, float *z, int64_t position)
860 {
861         server->get_projector(x, y, z, position, direction);
862 }
863
864
865 EDLSession* PluginClient::get_edlsession()
866 {
867         if(server->edl)
868                 return server->edl->session;
869         return 0;
870 }
871
872 int PluginClient::gui_open()
873 {
874         return server->gui_open();
875 }