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"
26 #include "condition.h"
30 #include "edlsession.h"
32 #include "filesystem.h"
34 #include "indexable.h"
36 #include "localsession.h"
40 #include "pluginclient.h"
41 #include "pluginserver.h"
42 #include "preferences.h"
44 #include "transportque.inc"
55 PluginClientThread::PluginClientThread(PluginClient *client)
58 this->client = client;
60 init_complete = new Condition(0, "PluginClientThread::init_complete");
63 PluginClientThread::~PluginClientThread()
66 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
67 delete window; window = 0;
68 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
72 void PluginClientThread::run()
76 if(client->window_x < 0) client->window_x = info.get_abs_cursor_x();
77 if(client->window_y < 0) client->window_y = info.get_abs_cursor_y();
79 window = (PluginClientWindow*)client->new_window();
82 window->lock_window("PluginClientThread::run");
83 window->create_objects();
84 VFrame *picon = client->server->get_picon();
85 if( picon ) window->set_icon(picon);
86 window->unlock_window();
88 /* Only set it here so tracking doesn't update it until everything is created. */
89 client->thread = this;
90 init_complete->unlock();
92 result = window->run_window();
93 window->lock_window("PluginClientThread::run");
94 //printf("PluginClientThread::run %p %d\n", this, __LINE__);
95 window->hide_window(1);
96 window->unlock_window();
97 window->done_event(result);
98 // Can't save defaults in the destructor because it's not called immediately
100 /* if(client->defaults) */ client->save_defaults_xml();
101 /* This is needed when the GUI is closed from itself */
102 if(result) client->client_side_close();
107 client->thread = this;
108 init_complete->unlock();
112 BC_WindowBase* PluginClientThread::get_window()
117 PluginClient* PluginClientThread::get_client()
127 PluginClientFrame::PluginClientFrame(int data_size,
131 this->data_size = data_size;
133 this->period_n = period_n;
134 this->period_d = period_d;
137 PluginClientFrame::~PluginClientFrame()
146 PluginClientWindow::PluginClientWindow(PluginClient *client,
147 int w, int h, int min_w, int min_h, int allow_resize)
148 : BC_Window(client->gui_string,
149 client->window_x /* - w / 2 */, client->window_y /* - h / 2 */,
150 w, h, min_w, min_h, allow_resize, 0, 1)
152 this->client = client;
155 PluginClientWindow::PluginClientWindow(const char *title,
156 int x, int y, int w, int h, int min_w, int min_h, int allow_resize)
157 : BC_Window(title, x, y, w, h, min_w, min_h, allow_resize, 0, 1)
162 PluginClientWindow::~PluginClientWindow()
167 int PluginClientWindow::translation_event()
171 client->window_x = get_x();
172 client->window_y = get_y();
178 int PluginClientWindow::close_event()
180 /* Set result to 1 to indicate a client side close */
189 PluginClient::PluginClient(PluginServer *server)
192 this->server = server;
193 smp = server->preferences->project_smp;
195 update_timer = new Timer;
196 // Virtual functions don't work here.
199 PluginClient::~PluginClient()
207 // Virtual functions don't work here.
208 if(defaults) delete defaults;
209 frame_buffer.remove_all_objects();
213 int PluginClient::reset()
222 realtime_priority = 0;
224 total_in_buffers = 0;
225 total_out_buffers = 0;
229 direction = PLAY_FORWARD;
236 void PluginClient::hide_gui()
238 if(thread && thread->window)
240 //printf("PluginClient::delete_thread %d\n", __LINE__);
241 /* This is needed when the GUI is closed from elsewhere than itself */
242 /* Since we now use autodelete, this is all that has to be done, thread will take care of itself ... */
243 /* Thread join will wait if this was not called from the thread itself or go on if it was */
244 thread->window->lock_window("PluginClient::hide_gui");
245 thread->window->set_done(0);
246 //printf("PluginClient::hide_gui %d thread->window=%p\n", __LINE__, thread->window);
247 thread->window->unlock_window();
251 // For realtime plugins initialize buffers
252 int PluginClient::plugin_init_realtime(int realtime_priority,
253 int total_in_buffers,
257 // Get parameters for all
258 master_gui_on = get_gui_status();
262 // get parameters depending on video or audio
263 init_realtime_parameters();
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;
271 int PluginClient::plugin_start_loop(int64_t start,
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;
282 this->in_buffer_size = this->out_buffer_size = buffer_size;
283 this->total_in_buffers = this->total_out_buffers = total_buffers;
288 int PluginClient::plugin_process_loop()
290 return process_loop();
293 int PluginClient::plugin_stop_loop()
298 MainProgressBar* PluginClient::start_progress(char *string, int64_t length)
300 return server->start_progress(string, length);
304 // Non realtime parameters
305 int PluginClient::plugin_get_parameters()
307 int result = get_parameters();
308 if(defaults) save_defaults();
312 // ========================= main loop
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"); }
321 Theme* PluginClient::new_theme() { return 0; }
323 int PluginClient::load_configuration()
328 Theme* PluginClient::get_theme()
330 return server->get_theme();
333 int PluginClient::show_gui()
335 load_configuration();
336 thread = new PluginClientThread(this);
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();
345 void PluginClient::raise_window()
347 if(thread && thread->window)
349 thread->window->lock_window("PluginClient::raise_window");
350 thread->window->raise_window();
351 thread->window->flush();
352 thread->window->unlock_window();
356 int PluginClient::set_string()
360 thread->window->lock_window("PluginClient::set_string");
361 thread->window->put_title(gui_string);
362 thread->window->unlock_window();
371 void PluginClient::begin_process_buffer()
373 // Delete all unused GUI frames
374 frame_buffer.remove_all_objects();
378 void PluginClient::end_process_buffer()
380 if(frame_buffer.size())
388 void PluginClient::plugin_update_gui()
393 // Delete unused GUI frames
394 while(frame_buffer.size() > MAX_FRAME_BUFFER)
395 frame_buffer.remove_object_number(0);
399 void PluginClient::update_gui()
403 int PluginClient::get_gui_update_frames()
405 if(frame_buffer.size())
407 PluginClientFrame *frame = frame_buffer.get(0);
408 int total_frames = update_timer->get_difference() *
412 if(total_frames) update_timer->subtract(total_frames *
417 // printf("PluginClient::get_gui_update_frames %d %ld %d %d %d\n",
419 // update_timer->get_difference(),
420 // frame->period_n * 1000 / frame->period_d,
422 // frame_buffer.size());
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);
438 PluginClientFrame* PluginClient::get_gui_frame()
440 if(frame_buffer.size())
442 PluginClientFrame *frame = frame_buffer.get(0);
443 frame_buffer.remove_number(0);
452 void PluginClient::add_gui_frame(PluginClientFrame *frame)
454 frame_buffer.append(frame);
457 void PluginClient::send_render_gui()
459 server->send_render_gui(&frame_buffer);
462 void PluginClient::send_render_gui(void *data)
464 server->send_render_gui(data);
467 void PluginClient::send_render_gui(void *data, int size)
469 server->send_render_gui(data, size);
472 void PluginClient::plugin_render_gui(void *data, int size)
474 render_gui(data, size);
478 void PluginClient::plugin_render_gui(void *data)
483 void PluginClient::render_gui(void *data)
487 thread->get_window()->lock_window("PluginClient::render_gui");
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;
493 ArrayList<PluginClientFrame*> *src =
494 (ArrayList<PluginClientFrame*>*)data;
496 // Shift GUI data to GUI client
499 this->frame_buffer.append(src->get(0));
500 src->remove_number(0);
503 // Start the timer for the current buffer
504 update_timer->update();
505 thread->get_window()->unlock_window();
509 void PluginClient::render_gui(void *data, int size)
511 printf("PluginClient::render_gui %d\n", __LINE__);
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()
528 // printf("PluginClient::load_defaults undefined in %s.\n", plugin_title());
532 int PluginClient::save_defaults()
535 // printf("PluginClient::save_defaults undefined in %s.\n", plugin_title());
539 void PluginClient::load_defaults_xml()
541 char path[BCTEXTLEN];
542 server->get_defaults_path(path);
544 fs.complete_path(path);
546 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
551 int fd = open(path, O_RDONLY);
552 if( fd >= 0 && !fstat(fd, &st) ) {
553 int64_t sz = st.st_size;
554 data = new char[sz+1];
555 len = read(fd, data, sz);
558 if( data && len >= 0 ) {
560 // Get window extents
562 for( int state=0; i<len && state>=0; ++i ) {
563 if( !data[i] || data[i] == '<' ) break;
564 if( !isdigit(data[i]) ) continue;
566 window_x = atoi(data+i);
570 window_y = atoi(data+i);
573 while( i<len && isdigit(data[i]) ) ++i;
575 KeyFrame keyframe(data+i, len-i);
576 read_data(&keyframe);
581 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
584 void PluginClient::save_defaults_xml()
586 char path[BCTEXTLEN];
587 server->get_defaults_path(path);
589 fs.complete_path(path);
592 KeyFrame temp_keyframe;
593 save_data(&temp_keyframe);
595 const char *data = temp_keyframe.get_data();
596 int len = strlen(data);
597 FILE *fp = fopen(path, "w");
600 fprintf(fp, "%d\n%d\n", window_x, window_y);
601 if( len > 0 && !fwrite(data, len, 1, fp) ) {
602 fprintf(stderr, "PluginClient::save_defaults_xml %d \"%s\" %d bytes: %s\n",
603 __LINE__, path, len, strerror(errno));
611 int PluginClient::is_defaults()
613 return using_defaults;
616 BC_Hash* PluginClient::get_defaults()
620 PluginClientThread* PluginClient::get_thread()
625 BC_WindowBase* PluginClient::new_window()
627 printf("PluginClient::new_window undefined in %s.\n", plugin_title());
630 int PluginClient::get_parameters() { return 0; }
631 int PluginClient::get_samplerate() { return get_project_samplerate(); }
632 double PluginClient::get_framerate() { return get_project_framerate(); }
633 int PluginClient::init_realtime_parameters() { return 0; }
634 int PluginClient::delete_nonrealtime_parameters() { return 0; }
635 int PluginClient::start_loop() { return 0; };
636 int PluginClient::process_loop() { return 0; };
637 int PluginClient::stop_loop() { return 0; };
639 void PluginClient::set_interactive()
644 int64_t PluginClient::get_in_buffers(int64_t recommended_size)
646 return recommended_size;
649 int64_t PluginClient::get_out_buffers(int64_t recommended_size)
651 return recommended_size;
654 int PluginClient::get_gui_status()
656 return server->get_gui_status();
659 // close event from client side
660 void PluginClient::client_side_close()
662 // Last command executed
663 server->client_side_close();
666 int PluginClient::stop_gui_client()
668 if(!client_gui_on) return 0;
673 int PluginClient::get_project_samplerate()
675 return server->get_project_samplerate();
678 double PluginClient::get_project_framerate()
680 return server->get_project_framerate();
683 const char *PluginClient::get_source_path()
685 if( server->plugin ) return 0;
686 int64_t source_position = server->plugin->startproject;
687 Edit *edit = server->plugin->track->edits->editof(source_position,PLAY_FORWARD,0);
688 Indexable *indexable = edit ? edit->get_source() : 0;
689 return indexable ? indexable->path : 0;
693 void PluginClient::update_display_title()
695 server->generate_display_title(gui_string);
699 char* PluginClient::get_gui_string()
705 char* PluginClient::get_path()
710 char* PluginClient::get_plugin_dir()
712 return server->preferences->plugin_dir;
715 int PluginClient::set_string_client(char *string)
717 strcpy(gui_string, string);
723 int PluginClient::get_interpolation_type()
725 return server->get_interpolation_type();
729 float PluginClient::get_red()
731 EDL *edl = get_edl();
732 return edl->local_session->use_max ?
733 edl->local_session->red_max :
734 edl->local_session->red;
737 float PluginClient::get_green()
739 EDL *edl = get_edl();
740 return edl->local_session->use_max ?
741 edl->local_session->green_max :
742 edl->local_session->green;
745 float PluginClient::get_blue()
747 EDL *edl = get_edl();
748 return edl->local_session->use_max ?
749 edl->local_session->blue_max :
750 edl->local_session->blue;
754 int64_t PluginClient::get_source_position()
756 return source_position;
759 int64_t PluginClient::get_source_start()
764 int64_t PluginClient::get_total_len()
769 int PluginClient::get_direction()
775 int64_t PluginClient::local_to_edl(int64_t position)
780 int64_t PluginClient::edl_to_local(int64_t position)
785 int PluginClient::get_use_opengl()
787 return server->get_use_opengl();
790 int PluginClient::get_total_buffers()
792 return total_in_buffers;
795 int PluginClient::get_buffer_size()
797 return in_buffer_size;
800 int PluginClient::get_project_smp()
802 //printf("PluginClient::get_project_smp %d %d\n", __LINE__, smp);
806 const char* PluginClient::get_defaultdir()
808 return File::get_plugin_path();
812 int PluginClient::send_hide_gui()
814 // Stop the GUI server and delete GUI messages
819 int PluginClient::send_configure_change()
822 server->mwindow->undo->update_undo_before(_("tweek"), this);
823 #ifdef USE_KEYFRAME_SPANNING
825 save_data(&keyframe);
826 server->apply_keyframe(&keyframe);
828 KeyFrame* keyframe = server->get_keyframe();
829 // Call save routine in plugin
833 server->mwindow->undo->update_undo_after(_("tweek"), LOAD_AUTOMATION);
834 server->sync_parameters();
839 KeyFrame* PluginClient::get_prev_keyframe(int64_t position, int is_local)
841 if(is_local) position = local_to_edl(position);
842 return server->get_prev_keyframe(position);
845 KeyFrame* PluginClient::get_next_keyframe(int64_t position, int is_local)
847 if(is_local) position = local_to_edl(position);
848 return server->get_next_keyframe(position);
851 void PluginClient::get_camera(float *x, float *y, float *z, int64_t position)
853 server->get_camera(x, y, z, position, direction);
856 void PluginClient::get_projector(float *x, float *y, float *z, int64_t position)
858 server->get_projector(x, y, z, position, direction);
862 void PluginClient::output_to_track(float ox, float oy, float &tx, float &ty)
864 float projector_x, projector_y, projector_z;
865 int64_t position = get_source_position();
866 get_projector(&projector_x, &projector_y, &projector_z, position);
867 EDL *edl = get_edl();
868 projector_x += edl->session->output_w / 2;
869 projector_y += edl->session->output_h / 2;
870 Track *track = server->plugin ? server->plugin->track : 0;
871 int track_w = track ? track->track_w : edl->session->output_w;
872 int track_h = track ? track->track_h : edl->session->output_h;
873 tx = (ox - projector_x) / projector_z + track_w / 2;
874 ty = (oy - projector_y) / projector_z + track_h / 2;
877 void PluginClient::track_to_output(float tx, float ty, float &ox, float &oy)
879 float projector_x, projector_y, projector_z;
880 int64_t position = get_source_position();
881 get_projector(&projector_x, &projector_y, &projector_z, position);
882 EDL *edl = get_edl();
883 projector_x += edl->session->output_w / 2;
884 projector_y += edl->session->output_h / 2;
885 Track *track = server->plugin ? server->plugin->track : 0;
886 int track_w = track ? track->track_w : edl->session->output_w;
887 int track_h = track ? track->track_h : edl->session->output_h;
888 ox = (tx - track_w / 2) * projector_z + projector_x;
889 oy = (ty - track_h / 2) * projector_z + projector_y;
893 EDL *PluginClient::get_edl()
895 return server->mwindow ? server->mwindow->edl : server->edl;
898 int PluginClient::gui_open()
900 return server->gui_open();