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"
33 #include "indexable.h"
35 #include "localsession.h"
39 #include "pluginclient.h"
40 #include "pluginserver.h"
41 #include "preferences.h"
43 #include "transportque.inc"
54 PluginClientThread::PluginClientThread(PluginClient *client)
57 this->client = client;
59 init_complete = new Condition(0, "PluginClientThread::init_complete");
62 PluginClientThread::~PluginClientThread()
65 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
66 delete window; window = 0;
67 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
71 void PluginClientThread::run()
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();
78 window = (PluginClientWindow*)client->new_window();
81 window->lock_window("PluginClientThread::run");
82 window->create_objects();
83 window->unlock_window();
85 /* Only set it here so tracking doesn't update it until everything is created. */
86 client->thread = this;
87 init_complete->unlock();
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
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();
104 client->thread = this;
105 init_complete->unlock();
109 BC_WindowBase* PluginClientThread::get_window()
114 PluginClient* PluginClientThread::get_client()
124 PluginClientFrame::PluginClientFrame(int data_size,
128 this->data_size = data_size;
130 this->period_n = period_n;
131 this->period_d = period_d;
134 PluginClientFrame::~PluginClientFrame()
143 PluginClientWindow::PluginClientWindow(PluginClient *client,
144 int w, int h, int min_w, int min_h, int allow_resize)
145 : BC_Window(client->gui_string,
146 client->window_x /* - w / 2 */,
147 client->window_y /* - h / 2 */,
148 (int)(w*get_resources()->font_scale+0.5), (int)(h*get_resources()->font_scale+0.5),
149 (int)(min_w*get_resources()->font_scale+0.5), (int)(min_h*get_resources()->font_scale+0.5),
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,
158 (int)(w*get_resources()->font_scale+0.5), (int)(h*get_resources()->font_scale+0.5),
159 (int)(min_w*get_resources()->font_scale+0.5), (int)(min_h*get_resources()->font_scale+0.5),
165 PluginClientWindow::~PluginClientWindow()
170 int PluginClientWindow::translation_event()
174 client->window_x = get_x();
175 client->window_y = get_y();
181 int PluginClientWindow::close_event()
183 /* Set result to 1 to indicate a client side close */
192 PluginClient::PluginClient(PluginServer *server)
195 this->server = server;
196 smp = server->preferences->project_smp;
198 update_timer = new Timer;
199 // Virtual functions don't work here.
202 PluginClient::~PluginClient()
210 // Virtual functions don't work here.
211 if(defaults) delete defaults;
212 frame_buffer.remove_all_objects();
216 int PluginClient::reset()
225 realtime_priority = 0;
227 total_in_buffers = 0;
228 total_out_buffers = 0;
232 direction = PLAY_FORWARD;
239 void PluginClient::hide_gui()
241 if(thread && thread->window)
243 //printf("PluginClient::delete_thread %d\n", __LINE__);
244 /* This is needed when the GUI is closed from elsewhere than itself */
245 /* Since we now use autodelete, this is all that has to be done, thread will take care of itself ... */
246 /* Thread join will wait if this was not called from the thread itself or go on if it was */
247 thread->window->lock_window("PluginClient::hide_gui");
248 thread->window->set_done(0);
249 //printf("PluginClient::hide_gui %d thread->window=%p\n", __LINE__, thread->window);
250 thread->window->unlock_window();
254 // For realtime plugins initialize buffers
255 int PluginClient::plugin_init_realtime(int realtime_priority,
256 int total_in_buffers,
260 // Get parameters for all
261 master_gui_on = get_gui_status();
265 // get parameters depending on video or audio
266 init_realtime_parameters();
268 this->realtime_priority = realtime_priority;
269 this->total_in_buffers = this->total_out_buffers = total_in_buffers;
270 this->out_buffer_size = this->in_buffer_size = buffer_size;
274 int PluginClient::plugin_start_loop(int64_t start,
279 //printf("PluginClient::plugin_start_loop %d %ld %ld %ld %d\n",
280 // __LINE__, start, end, buffer_size, total_buffers);
281 this->source_start = start;
282 this->total_len = end - start;
285 this->in_buffer_size = this->out_buffer_size = buffer_size;
286 this->total_in_buffers = this->total_out_buffers = total_buffers;
291 int PluginClient::plugin_process_loop()
293 return process_loop();
296 int PluginClient::plugin_stop_loop()
301 MainProgressBar* PluginClient::start_progress(char *string, int64_t length)
303 return server->start_progress(string, length);
307 // Non realtime parameters
308 int PluginClient::plugin_get_parameters()
310 int result = get_parameters();
311 if(defaults) save_defaults();
315 // ========================= main loop
317 int PluginClient::is_multichannel() { return 0; }
318 int PluginClient::is_synthesis() { return 0; }
319 int PluginClient::is_realtime() { return 0; }
320 int PluginClient::is_fileio() { return 0; }
321 int PluginClient::delete_buffer_ptrs() { return 0; }
322 const char* PluginClient::plugin_title() { return _("Untitled"); }
324 Theme* PluginClient::new_theme() { return 0; }
326 int PluginClient::load_configuration()
331 Theme* PluginClient::get_theme()
333 return server->get_theme();
336 int PluginClient::show_gui()
338 load_configuration();
339 thread = new PluginClientThread(this);
341 thread->init_complete->lock("PluginClient::show_gui");
342 // Must wait before sending any hide_gui
343 if( !thread->window ) return 1;
344 thread->window->init_wait();
348 void PluginClient::raise_window()
350 if(thread && thread->window)
352 thread->window->lock_window("PluginClient::raise_window");
353 thread->window->raise_window();
354 thread->window->flush();
355 thread->window->unlock_window();
359 int PluginClient::set_string()
363 thread->window->lock_window("PluginClient::set_string");
364 thread->window->put_title(gui_string);
365 thread->window->unlock_window();
374 void PluginClient::begin_process_buffer()
376 // Delete all unused GUI frames
377 frame_buffer.remove_all_objects();
381 void PluginClient::end_process_buffer()
383 if(frame_buffer.size())
391 void PluginClient::plugin_update_gui()
396 // Delete unused GUI frames
397 while(frame_buffer.size() > MAX_FRAME_BUFFER)
398 frame_buffer.remove_object_number(0);
402 void PluginClient::update_gui()
406 int PluginClient::get_gui_update_frames()
408 if(frame_buffer.size())
410 PluginClientFrame *frame = frame_buffer.get(0);
411 int total_frames = update_timer->get_difference() *
415 if(total_frames) update_timer->subtract(total_frames *
420 // printf("PluginClient::get_gui_update_frames %d %ld %d %d %d\n",
422 // update_timer->get_difference(),
423 // frame->period_n * 1000 / frame->period_d,
425 // frame_buffer.size());
428 for(int i = 0; i < frame_buffer.size(); i++)
429 if(frame_buffer.get(i)->force) total_frames++;
430 total_frames = MIN(frame_buffer.size(), total_frames);
441 PluginClientFrame* PluginClient::get_gui_frame()
443 if(frame_buffer.size())
445 PluginClientFrame *frame = frame_buffer.get(0);
446 frame_buffer.remove_number(0);
455 void PluginClient::add_gui_frame(PluginClientFrame *frame)
457 frame_buffer.append(frame);
460 void PluginClient::send_render_gui()
462 server->send_render_gui(&frame_buffer);
465 void PluginClient::send_render_gui(void *data)
467 server->send_render_gui(data);
470 void PluginClient::send_render_gui(void *data, int size)
472 server->send_render_gui(data, size);
475 void PluginClient::plugin_render_gui(void *data, int size)
477 render_gui(data, size);
481 void PluginClient::plugin_render_gui(void *data)
486 void PluginClient::render_gui(void *data)
490 thread->get_window()->lock_window("PluginClient::render_gui");
492 // Set all previous frames to draw immediately
493 for(int i = 0; i < frame_buffer.size(); i++)
494 frame_buffer.get(i)->force = 1;
496 ArrayList<PluginClientFrame*> *src =
497 (ArrayList<PluginClientFrame*>*)data;
499 // Shift GUI data to GUI client
502 this->frame_buffer.append(src->get(0));
503 src->remove_number(0);
506 // Start the timer for the current buffer
507 update_timer->update();
508 thread->get_window()->unlock_window();
512 void PluginClient::render_gui(void *data, int size)
514 printf("PluginClient::render_gui %d\n", __LINE__);
524 int PluginClient::is_audio() { return 0; }
525 int PluginClient::is_video() { return 0; }
526 int PluginClient::is_theme() { return 0; }
527 int PluginClient::uses_gui() { return 1; }
528 int PluginClient::is_transition() { return 0; }
529 int PluginClient::load_defaults()
531 // printf("PluginClient::load_defaults undefined in %s.\n", plugin_title());
535 int PluginClient::save_defaults()
538 // printf("PluginClient::save_defaults undefined in %s.\n", plugin_title());
542 void PluginClient::load_defaults_xml()
544 char path[BCTEXTLEN];
545 server->get_defaults_path(path);
547 fs.complete_path(path);
549 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
551 KeyFrame temp_keyframe;
552 FILE *fp = fopen(path, "r");
554 struct stat st; int fd = fileno(fp);
555 int64_t sz = !fstat(fd, &st) ? st.st_size : BCTEXTLEN;
556 char *data = temp_keyframe.get_data(sz+1);
557 int data_size = fread(data, 1, sz, fp);
558 if( data_size < 0 ) data_size = 0;
559 if( data_size > 0 ) {
560 // Get window extents
562 for( int i=0; i<(data_size-8) && data[i]!='<'; ++i ) {
563 if( !isdigit(data[i]) ) continue;
565 window_x = atoi(data + i);
569 window_y = atoi(data + i);
572 while( i<data_size && isdigit(data[i]) ) ++i;
575 read_data(&temp_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 int64_t source_position = server->plugin->startproject;
686 Edit *edit = server->plugin->track->edits->editof(source_position,PLAY_FORWARD,0);
687 Indexable *indexable = edit ? edit->get_source() : 0;
688 return indexable ? indexable->path : 0;
692 void PluginClient::update_display_title()
694 server->generate_display_title(gui_string);
698 char* PluginClient::get_gui_string()
704 char* PluginClient::get_path()
709 char* PluginClient::get_plugin_dir()
711 return server->preferences->plugin_dir;
714 int PluginClient::set_string_client(char *string)
716 strcpy(gui_string, string);
722 int PluginClient::get_interpolation_type()
724 return server->get_interpolation_type();
728 float PluginClient::get_red()
730 EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
731 return !edl ? 0 : edl->local_session->use_max ?
732 edl->local_session->red_max :
733 edl->local_session->red;
736 float PluginClient::get_green()
738 EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
739 return !edl ? 0 : edl->local_session->use_max ?
740 edl->local_session->green_max :
741 edl->local_session->green;
744 float PluginClient::get_blue()
746 EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
747 return !edl ? 0 : edl->local_session->use_max ?
748 edl->local_session->blue_max :
749 edl->local_session->blue;
753 int64_t PluginClient::get_source_position()
755 return source_position;
758 int64_t PluginClient::get_source_start()
763 int64_t PluginClient::get_total_len()
768 int PluginClient::get_direction()
774 int64_t PluginClient::local_to_edl(int64_t position)
779 int64_t PluginClient::edl_to_local(int64_t position)
784 int PluginClient::get_use_opengl()
786 return server->get_use_opengl();
789 int PluginClient::get_total_buffers()
791 return total_in_buffers;
794 int PluginClient::get_buffer_size()
796 return in_buffer_size;
799 int PluginClient::get_project_smp()
801 //printf("PluginClient::get_project_smp %d %d\n", __LINE__, smp);
805 const char* PluginClient::get_defaultdir()
807 return File::get_plugin_path();
811 int PluginClient::send_hide_gui()
813 // Stop the GUI server and delete GUI messages
818 int PluginClient::send_configure_change()
821 server->mwindow->undo->update_undo_before(_("tweek"), this);
822 #ifdef USE_KEYFRAME_SPANNING
824 save_data(&keyframe);
825 server->apply_keyframe(&keyframe);
827 KeyFrame* keyframe = server->get_keyframe();
828 // Call save routine in plugin
832 server->mwindow->undo->update_undo_after(_("tweek"), LOAD_AUTOMATION);
833 server->sync_parameters();
838 KeyFrame* PluginClient::get_prev_keyframe(int64_t position, int is_local)
840 if(is_local) position = local_to_edl(position);
841 return server->get_prev_keyframe(position);
844 KeyFrame* PluginClient::get_next_keyframe(int64_t position, int is_local)
846 if(is_local) position = local_to_edl(position);
847 return server->get_next_keyframe(position);
850 void PluginClient::get_camera(float *x, float *y, float *z, int64_t position)
852 server->get_camera(x, y, z, position, direction);
855 void PluginClient::get_projector(float *x, float *y, float *z, int64_t position)
857 server->get_projector(x, y, z, position, direction);
861 EDLSession* PluginClient::get_edlsession()
864 return server->edl->session;
868 int PluginClient::gui_open()
870 return server->gui_open();