4 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcdisplayinfo.h"
24 #include "bcsignals.h"
25 #include "attachmentpoint.h"
27 #include "condition.h"
31 #include "edlsession.h"
33 #include "filesystem.h"
35 #include "indexable.h"
37 #include "localsession.h"
41 #include "pluginclient.h"
42 #include "pluginserver.h"
43 #include "preferences.h"
44 #include "renderengine.h"
47 #include "transportque.h"
57 PluginClientFrame::PluginClientFrame()
62 PluginClientFrame::~PluginClientFrame()
67 PluginClientThread::PluginClientThread(PluginClient *client)
70 this->client = client;
72 init_complete = new Condition(0, "PluginClientThread::init_complete");
75 PluginClientThread::~PluginClientThread()
81 void PluginClientThread::run()
85 if(client->window_x < 0) client->window_x = info.get_abs_cursor_x();
86 if(client->window_y < 0) client->window_y = info.get_abs_cursor_y();
88 window = (PluginClientWindow*)client->new_window();
91 window->lock_window("PluginClientThread::run");
92 window->create_objects();
93 VFrame *picon = client->server->get_picon();
94 if( picon ) window->set_icon(picon);
95 window->unlock_window();
97 /* Only set it here so tracking doesn't update it until everything is created. */
98 client->thread = this;
99 init_complete->unlock();
101 result = window->run_window();
102 window->lock_window("PluginClientThread::run");
103 //printf("PluginClientThread::run %p %d\n", this, __LINE__);
104 window->hide_window(1);
105 client->save_defaults_xml(); // needs window lock
106 window->unlock_window();
107 window->done_event(result);
108 /* This is needed when the GUI is closed from itself */
109 if(result) client->client_side_close();
114 client->thread = this;
115 init_complete->unlock();
119 BC_WindowBase* PluginClientThread::get_window()
124 PluginClient* PluginClientThread::get_client()
130 PluginClientWindow::PluginClientWindow(PluginClient *client,
131 int w, int h, int min_w, int min_h, int allow_resize)
132 : BC_Window(client->gui_string,
133 client->window_x /* - w / 2 */, client->window_y /* - h / 2 */,
134 w, h, min_w, min_h, allow_resize, 0, 1)
136 this->client = client;
139 PluginClientWindow::PluginClientWindow(const char *title,
140 int x, int y, int w, int h, int min_w, int min_h, int allow_resize)
141 : BC_Window(title, x, y, w, h, min_w, min_h, allow_resize, 0, 1)
146 PluginClientWindow::~PluginClientWindow()
151 int PluginClientWindow::translation_event()
155 client->window_x = get_x();
156 client->window_y = get_y();
162 int PluginClientWindow::close_event()
164 /* Set result to 1 to indicate a client side close */
169 void PluginClientWindow::param_updated()
171 printf("PluginClientWindow::param_updated %d undefined\n", __LINE__);
175 PluginParam::PluginParam(PluginClient *plugin, PluginClientWindow *gui,
176 int x1, int x2, int x3, int y, int text_w,
177 int *output_i, float *output_f, int *output_q,
178 const char *title, float min, float max)
180 this->output_i = output_i;
181 this->output_f = output_f;
182 this->output_q = output_q;
183 this->title = cstrdup(title);
184 this->plugin = plugin;
189 this->text_w = text_w;
199 PluginParam::~PluginParam()
209 void PluginParam::initialize()
213 (BC_Pot::calculate_h() -
214 BC_Title::calculate_h(gui, _(title), MEDIUMFONT)) / 2;
215 gui->add_tool(title_ = new BC_Title(x1, y2, _(title)));
219 gui->add_tool(fpot = new PluginFPot(this, x2, y));
224 gui->add_tool(ipot = new PluginIPot(this, x2, y));
229 gui->add_tool(qpot = new PluginQPot(this, x2, y));
233 (BC_Pot::calculate_h() -
234 BC_TextBox::calculate_h(gui, MEDIUMFONT, 1, 1)) / 2;
237 gui->add_tool(text = new PluginText(this, x3, y3, *output_i));
241 gui->add_tool(text = new PluginText(this, x3, y3, *output_f));
245 gui->add_tool(text = new PluginText(this, x3, y3, *output_q));
248 set_precision(precision);
251 void PluginParam::update(int skip_text, int skip_pot)
257 text->update((int64_t)*output_i);
261 text->update((int64_t)*output_q);
265 text->update((float)*output_f);
273 ipot->update((int64_t)*output_i);
277 qpot->update((int64_t)*output_q);
281 fpot->update((float)*output_f);
286 void PluginParam::set_precision(int digits)
288 this->precision = digits;
293 text->set_precision(digits);
296 fpot->set_precision(1.0f / pow(10, digits));
301 PluginFPot::PluginFPot(PluginParam *param, int x, int y)
312 int PluginFPot::handle_event()
314 *param->output_f = get_value();
316 param->plugin->send_configure_change();
317 param->gui->param_updated();
321 PluginIPot::PluginIPot(PluginParam *param, int x, int y)
332 int PluginIPot::handle_event()
334 *param->output_i = get_value();
336 param->plugin->send_configure_change();
337 param->gui->param_updated();
342 PluginQPot::PluginQPot(PluginParam *param, int x, int y)
351 int PluginQPot::handle_event()
353 *param->output_q = get_value();
355 param->plugin->send_configure_change();
356 param->gui->param_updated();
360 PluginText::PluginText(PluginParam *param, int x, int y, int value)
372 PluginText::PluginText(PluginParam *param, int x, int y, float value)
385 int PluginText::handle_event()
389 *param->output_i = atoi(get_text());
394 *param->output_f = atof(get_text());
399 *param->output_q = atoi(get_text());
402 param->plugin->send_configure_change();
403 param->gui->param_updated();
408 PluginClient::PluginClient(PluginServer *server)
411 this->server = server;
412 smp = server->preferences->project_smp;
414 update_timer = new Timer;
415 // Virtual functions don't work here.
418 PluginClient::~PluginClient()
426 // Virtual functions don't work here.
427 if(defaults) delete defaults;
431 int PluginClient::reset()
440 realtime_priority = 0;
442 total_in_buffers = 0;
443 total_out_buffers = 0;
447 direction = PLAY_FORWARD;
454 void PluginClient::hide_gui()
456 if(thread && thread->window)
458 thread->window->lock_window("PluginClient::hide_gui");
459 thread->window->set_done(0);
460 thread->window->unlock_window();
464 // For realtime plugins initialize buffers
465 int PluginClient::plugin_init_realtime(int realtime_priority,
466 int total_in_buffers,
470 // Get parameters for all
471 master_gui_on = get_gui_status();
475 // get parameters depending on video or audio
476 init_realtime_parameters();
478 this->realtime_priority = realtime_priority;
479 this->total_in_buffers = this->total_out_buffers = total_in_buffers;
480 this->out_buffer_size = this->in_buffer_size = buffer_size;
484 int PluginClient::plugin_start_loop(int64_t start,
489 //printf("PluginClient::plugin_start_loop %d %ld %ld %ld %d\n",
490 // __LINE__, start, end, buffer_size, total_buffers);
491 this->source_start = start;
492 this->total_len = end - start;
495 this->in_buffer_size = this->out_buffer_size = buffer_size;
496 this->total_in_buffers = this->total_out_buffers = total_buffers;
501 int PluginClient::plugin_process_loop()
503 return process_loop();
506 int PluginClient::plugin_stop_loop()
511 MainProgressBar* PluginClient::start_progress(char *string, int64_t length)
513 return server->start_progress(string, length);
517 // Non realtime parameters
518 int PluginClient::plugin_get_parameters()
520 int result = get_parameters();
521 if(defaults) save_defaults();
525 // ========================= main loop
527 int PluginClient::is_multichannel() { return 0; }
528 int PluginClient::is_synthesis() { return 0; }
529 int PluginClient::is_realtime() { return 0; }
530 int PluginClient::is_fileio() { return 0; }
531 const char* PluginClient::plugin_title() { return _("Untitled"); }
533 Theme* PluginClient::new_theme() { return 0; }
535 int PluginClient::load_configuration()
540 Theme* PluginClient::get_theme()
542 return server->get_theme();
545 int PluginClient::show_gui()
547 load_configuration();
548 thread = new PluginClientThread(this);
550 thread->init_complete->lock("PluginClient::show_gui");
551 // Must wait before sending any hide_gui
552 if( !thread->window ) return 1;
553 thread->window->init_wait();
557 void PluginClient::raise_window()
559 if(thread && thread->window)
561 thread->window->lock_window("PluginClient::raise_window");
562 thread->window->raise_window();
563 thread->window->flush();
564 thread->window->unlock_window();
568 int PluginClient::set_string()
572 thread->window->lock_window("PluginClient::set_string");
573 thread->window->put_title(gui_string);
574 thread->window->unlock_window();
583 PluginClientFrames::PluginClientFrames()
587 PluginClientFrames::~PluginClientFrames()
591 int PluginClientFrames::fwd_cmpr(PluginClientFrame *a, PluginClientFrame *b)
593 double d = a->position - b->position;
594 return d < 0 ? -1 : !d ? 0 : 1;
597 int PluginClientFrames::rev_cmpr(PluginClientFrame *a, PluginClientFrame *b)
599 double d = b->position - a->position;
600 return d < 0 ? -1 : !d ? 0 : 1;
603 void PluginClientFrames::reset()
609 void PluginClientFrames::add_gui_frame(PluginClientFrame *frame)
615 void PluginClientFrames::concatenate(PluginClientFrames *frames)
618 count += frames->count;
622 void PluginClientFrames::sort_position(int dir)
625 if( dir == PLAY_REVERSE )
631 // pop frames until buffer passes position=pos in direction=dir
632 // dir==0, pop frame; pos<0, pop all frames
633 // delete past frames, return last popped frame
634 PluginClientFrame* PluginClientFrames::get_gui_frame(double pos, int dir)
637 while( first != last ) {
638 if( pos >= 0 && dir*(first->next->position - pos) > 0 ) break;
639 delete first; --count;
642 PluginClientFrame *frame = first;
643 if( frame ) { remove_pointer(frame); --count; }
647 PluginClientFrame* PluginClient::get_gui_frame(double pos, int dir)
649 return client_frames.get_gui_frame(pos, dir);
651 PluginClientFrame* PluginClient::next_gui_frame()
653 return client_frames.first;
657 void PluginClient::plugin_update_gui()
662 void PluginClient::update_gui()
666 int PluginClient::pending_gui_frame()
668 PluginClientFrame *frame = client_frames.first;
669 if( !frame ) return 0;
670 double tracking_position = get_tracking_position();
671 int direction = get_tracking_direction();
672 int ret = !(direction == PLAY_REVERSE ?
673 frame->position < tracking_position :
674 frame->position > tracking_position);
678 int PluginClient::pending_gui_frames()
680 PluginClientFrame *frame = client_frames.first;
681 if( !frame ) return 0;
682 double tracking_position = get_tracking_position();
683 int direction = get_tracking_direction();
685 while( frame && !(direction == PLAY_REVERSE ?
686 frame->position < tracking_position :
687 frame->position > tracking_position) ) {
688 ++count; frame=frame->next;
693 void PluginClient::add_gui_frame(PluginClientFrame *frame)
695 client_frames.add_gui_frame(frame);
697 int PluginClient::get_gui_frames()
699 return client_frames.total();
702 double PluginClient::get_tracking_position()
704 return server->mwindow->get_tracking_position();
707 int PluginClient::get_tracking_direction()
709 return server->mwindow->get_tracking_direction();
712 void PluginClient::send_render_gui()
714 server->send_render_gui(&client_frames);
717 void PluginClient::send_render_gui(void *data)
719 server->send_render_gui(data);
722 void PluginClient::send_render_gui(void *data, int size)
724 server->send_render_gui(data, size);
728 void PluginClient::plugin_reset_gui_frames()
730 if( !thread ) return;
731 BC_WindowBase *window = thread->get_window();
732 if( !window ) return;
733 window->lock_window("PluginClient::plugin_reset_gui_frames");
734 client_frames.reset();
735 window->unlock_window();
738 void PluginClient::plugin_render_gui_frames(PluginClientFrames *frames)
740 if( !thread ) return;
741 BC_WindowBase *window = thread->get_window();
742 if( !window ) return;
743 window->lock_window("PluginClient::render_gui");
744 while( client_frames.count > MAX_FRAME_BUFFER )
745 delete get_gui_frame(0, 0);
746 // append client frames to gui client_frames, consumes frames
747 client_frames.concatenate(frames);
748 client_frames.sort_position(get_tracking_direction());
749 update_timer->update();
750 window->unlock_window();
753 void PluginClient::plugin_render_gui(void *data)
758 void PluginClient::plugin_render_gui(void *data, int size)
760 render_gui(data, size);
763 void PluginClient::render_gui(void *data)
765 printf("PluginClient::render_gui %d\n", __LINE__);
768 void PluginClient::render_gui(void *data, int size)
770 printf("PluginClient::render_gui %d\n", __LINE__);
773 void PluginClient::reset_gui_frames()
775 server->reset_gui_frames();
778 int PluginClient::is_audio() { return 0; }
779 int PluginClient::is_video() { return 0; }
780 int PluginClient::is_theme() { return 0; }
781 int PluginClient::uses_gui() { return 1; }
782 int PluginClient::is_transition() { return 0; }
783 int PluginClient::load_defaults()
785 // printf("PluginClient::load_defaults undefined in %s.\n", plugin_title());
789 int PluginClient::save_defaults()
792 // printf("PluginClient::save_defaults undefined in %s.\n", plugin_title());
796 void PluginClient::load_defaults_xml()
798 char path[BCTEXTLEN];
799 server->get_defaults_path(path);
801 fs.complete_path(path);
803 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
808 int fd = open(path, O_RDONLY);
809 if( fd >= 0 && !fstat(fd, &st) ) {
810 int64_t sz = st.st_size;
811 data = new char[sz+1];
812 len = read(fd, data, sz);
815 if( data && len >= 0 ) {
817 // Get window extents
819 for( int state=0; i<len && state>=0; ++i ) {
820 if( !data[i] || data[i] == '<' ) break;
821 if( !isdigit(data[i]) ) continue;
823 window_x = atoi(data+i);
827 window_y = atoi(data+i);
830 while( i<len && isdigit(data[i]) ) ++i;
832 KeyFrame keyframe(data+i, len-i);
833 read_data(&keyframe);
838 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
841 void PluginClient::save_defaults_xml()
843 char path[BCTEXTLEN];
844 server->get_defaults_path(path);
846 fs.complete_path(path);
849 KeyFrame temp_keyframe;
850 save_data(&temp_keyframe);
852 const char *data = temp_keyframe.get_data();
853 int len = strlen(data);
854 FILE *fp = fopen(path, "w");
857 fprintf(fp, "%d\n%d\n", window_x, window_y);
858 if( len > 0 && !fwrite(data, len, 1, fp) ) {
859 fprintf(stderr, "PluginClient::save_defaults_xml %d \"%s\" %d bytes: %s\n",
860 __LINE__, path, len, strerror(errno));
868 int PluginClient::is_defaults()
870 return using_defaults;
873 BC_Hash* PluginClient::get_defaults()
877 PluginClientThread* PluginClient::get_thread()
882 BC_WindowBase* PluginClient::new_window()
884 printf("PluginClient::new_window undefined in %s.\n", plugin_title());
887 int PluginClient::get_parameters() { return 0; }
888 int PluginClient::get_samplerate() { return get_project_samplerate(); }
889 double PluginClient::get_framerate() { return get_project_framerate(); }
890 int PluginClient::init_realtime_parameters() { return 0; }
891 int PluginClient::delete_nonrealtime_parameters() { return 0; }
892 int PluginClient::start_loop() { return 0; };
893 int PluginClient::process_loop() { return 0; };
894 int PluginClient::stop_loop() { return 0; };
896 void PluginClient::set_interactive()
901 int64_t PluginClient::get_in_buffers(int64_t recommended_size)
903 return recommended_size;
906 int64_t PluginClient::get_out_buffers(int64_t recommended_size)
908 return recommended_size;
911 int PluginClient::get_gui_status()
913 return server->get_gui_status();
916 // close event from client side
917 void PluginClient::client_side_close()
919 // Last command executed
920 server->client_side_close();
923 int PluginClient::stop_gui_client()
925 if(!client_gui_on) return 0;
930 int PluginClient::get_project_samplerate()
932 return server->get_project_samplerate();
935 double PluginClient::get_project_framerate()
937 return server->get_project_framerate();
940 const char *PluginClient::get_source_path()
942 EDL *edl = get_edl();
943 Plugin *plugin = edl->tracks->plugin_exists(server->plugin_id);
944 int64_t source_position = plugin->startproject;
945 Edit *edit = plugin->track->edits->editof(source_position,PLAY_FORWARD,0);
946 Indexable *indexable = edit ? edit->get_source() : 0;
947 return indexable ? indexable->path : 0;
951 void PluginClient::update_display_title()
953 server->generate_display_title(gui_string);
957 char* PluginClient::get_gui_string()
963 char* PluginClient::get_path()
968 char* PluginClient::get_plugin_dir()
970 return server->preferences->plugin_dir;
973 int PluginClient::set_string_client(char *string)
975 strcpy(gui_string, string);
981 int PluginClient::get_interpolation_type()
983 return server->get_interpolation_type();
987 float PluginClient::get_red()
989 EDL *edl = get_edl();
990 return edl->local_session->use_max ?
991 edl->local_session->red_max :
992 edl->local_session->red;
995 float PluginClient::get_green()
997 EDL *edl = get_edl();
998 return edl->local_session->use_max ?
999 edl->local_session->green_max :
1000 edl->local_session->green;
1003 float PluginClient::get_blue()
1005 EDL *edl = get_edl();
1006 return edl->local_session->use_max ?
1007 edl->local_session->blue_max :
1008 edl->local_session->blue;
1012 int64_t PluginClient::get_source_position()
1014 return source_position;
1017 int64_t PluginClient::get_source_start()
1019 return source_start;
1022 int64_t PluginClient::get_total_len()
1027 int PluginClient::get_direction()
1032 int64_t PluginClient::local_to_edl(int64_t position)
1037 int64_t PluginClient::edl_to_local(int64_t position)
1042 int PluginClient::get_use_opengl()
1044 return server->get_use_opengl();
1047 int PluginClient::get_total_buffers()
1049 return total_in_buffers;
1052 int PluginClient::get_buffer_size()
1054 return in_buffer_size;
1057 int PluginClient::get_project_smp()
1059 //printf("PluginClient::get_project_smp %d %d\n", __LINE__, smp);
1063 const char* PluginClient::get_defaultdir()
1065 return File::get_plugin_path();
1069 int PluginClient::send_hide_gui()
1071 // Stop the GUI server and delete GUI messages
1076 int PluginClient::send_configure_change()
1079 server->mwindow->undo->update_undo_before(_("tweek"), this);
1080 #ifdef USE_KEYFRAME_SPANNING
1082 save_data(&keyframe);
1083 server->apply_keyframe(&keyframe);
1085 KeyFrame* keyframe = server->get_keyframe();
1086 // Call save routine in plugin
1087 save_data(keyframe);
1090 server->mwindow->undo->update_undo_after(_("tweek"), LOAD_AUTOMATION);
1091 server->sync_parameters();
1096 KeyFrame* PluginClient::get_prev_keyframe(int64_t position, int is_local)
1098 if(is_local) position = local_to_edl(position);
1099 return server->get_prev_keyframe(position);
1102 KeyFrame* PluginClient::get_next_keyframe(int64_t position, int is_local)
1104 if(is_local) position = local_to_edl(position);
1105 return server->get_next_keyframe(position);
1108 void PluginClient::get_camera(float *x, float *y, float *z, int64_t position)
1110 server->get_camera(x, y, z, position, direction);
1113 void PluginClient::get_projector(float *x, float *y, float *z, int64_t position)
1115 server->get_projector(x, y, z, position, direction);
1119 void PluginClient::output_to_track(float ox, float oy, float &tx, float &ty)
1121 float projector_x, projector_y, projector_z;
1122 int64_t position = get_source_position();
1123 get_projector(&projector_x, &projector_y, &projector_z, position);
1124 EDL *edl = get_edl();
1125 projector_x += edl->session->output_w / 2;
1126 projector_y += edl->session->output_h / 2;
1127 Plugin *plugin = edl->tracks->plugin_exists(server->plugin_id);
1128 Track *track = plugin ? plugin->track : 0;
1129 int track_w = track ? track->track_w : edl->session->output_w;
1130 int track_h = track ? track->track_h : edl->session->output_h;
1131 tx = (ox - projector_x) / projector_z + track_w / 2;
1132 ty = (oy - projector_y) / projector_z + track_h / 2;
1135 void PluginClient::track_to_output(float tx, float ty, float &ox, float &oy)
1137 float projector_x, projector_y, projector_z;
1138 int64_t position = get_source_position();
1139 get_projector(&projector_x, &projector_y, &projector_z, position);
1140 EDL *edl = get_edl();
1141 projector_x += edl->session->output_w / 2;
1142 projector_y += edl->session->output_h / 2;
1143 Plugin *plugin = edl->tracks->plugin_exists(server->plugin_id);
1144 Track *track = plugin ? plugin->track : 0;
1145 int track_w = track ? track->track_w : edl->session->output_w;
1146 int track_h = track ? track->track_h : edl->session->output_h;
1147 ox = (tx - track_w / 2) * projector_z + projector_x;
1148 oy = (ty - track_h / 2) * projector_z + projector_y;
1152 EDL *PluginClient::get_edl()
1154 return server->mwindow ? server->mwindow->edl : server->edl;
1157 int PluginClient::gui_open()
1159 return server->gui_open();