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()
204 // Delete the GUI thread. The GUI must be hidden with hide_gui first.
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();
248 //printf("PluginClient::delete_thread %d\n", __LINE__);
252 // For realtime plugins initialize buffers
253 int PluginClient::plugin_init_realtime(int realtime_priority,
254 int total_in_buffers,
258 // Get parameters for all
259 master_gui_on = get_gui_status();
263 // get parameters depending on video or audio
264 init_realtime_parameters();
266 this->realtime_priority = realtime_priority;
267 this->total_in_buffers = this->total_out_buffers = total_in_buffers;
268 this->out_buffer_size = this->in_buffer_size = buffer_size;
272 int PluginClient::plugin_start_loop(int64_t start,
277 //printf("PluginClient::plugin_start_loop %d %ld %ld %ld %d\n",
278 // __LINE__, start, end, buffer_size, total_buffers);
279 this->source_start = start;
280 this->total_len = end - start;
283 this->in_buffer_size = this->out_buffer_size = buffer_size;
284 this->total_in_buffers = this->total_out_buffers = total_buffers;
289 int PluginClient::plugin_process_loop()
291 return process_loop();
294 int PluginClient::plugin_stop_loop()
299 MainProgressBar* PluginClient::start_progress(char *string, int64_t length)
301 return server->start_progress(string, length);
305 // Non realtime parameters
306 int PluginClient::plugin_get_parameters()
308 int result = get_parameters();
309 if(defaults) save_defaults();
313 // ========================= main loop
315 int PluginClient::is_multichannel() { return 0; }
316 int PluginClient::is_synthesis() { return 0; }
317 int PluginClient::is_realtime() { return 0; }
318 int PluginClient::is_fileio() { return 0; }
319 int PluginClient::delete_buffer_ptrs() { return 0; }
320 const char* PluginClient::plugin_title() { return _("Untitled"); }
322 Theme* PluginClient::new_theme() { return 0; }
324 int PluginClient::load_configuration()
329 Theme* PluginClient::get_theme()
331 return server->get_theme();
334 int PluginClient::show_gui()
336 load_configuration();
337 thread = new PluginClientThread(this);
339 thread->init_complete->lock("PluginClient::show_gui");
340 // Must wait before sending any hide_gui
341 if( !thread->window ) return 1;
342 thread->window->init_wait();
346 void PluginClient::raise_window()
348 if(thread && thread->window)
350 thread->window->lock_window("PluginClient::raise_window");
351 thread->window->raise_window();
352 thread->window->flush();
353 thread->window->unlock_window();
357 int PluginClient::set_string()
361 thread->window->lock_window("PluginClient::set_string");
362 thread->window->put_title(gui_string);
363 thread->window->unlock_window();
372 void PluginClient::begin_process_buffer()
374 // Delete all unused GUI frames
375 frame_buffer.remove_all_objects();
379 void PluginClient::end_process_buffer()
381 if(frame_buffer.size())
389 void PluginClient::plugin_update_gui()
394 // Delete unused GUI frames
395 while(frame_buffer.size() > MAX_FRAME_BUFFER)
396 frame_buffer.remove_object_number(0);
400 void PluginClient::update_gui()
404 int PluginClient::get_gui_update_frames()
406 if(frame_buffer.size())
408 PluginClientFrame *frame = frame_buffer.get(0);
409 int total_frames = update_timer->get_difference() *
413 if(total_frames) update_timer->subtract(total_frames *
418 // printf("PluginClient::get_gui_update_frames %d %ld %d %d %d\n",
420 // update_timer->get_difference(),
421 // frame->period_n * 1000 / frame->period_d,
423 // frame_buffer.size());
426 for(int i = 0; i < frame_buffer.size(); i++)
427 if(frame_buffer.get(i)->force) total_frames++;
428 total_frames = MIN(frame_buffer.size(), total_frames);
439 PluginClientFrame* PluginClient::get_gui_frame()
441 if(frame_buffer.size())
443 PluginClientFrame *frame = frame_buffer.get(0);
444 frame_buffer.remove_number(0);
453 void PluginClient::add_gui_frame(PluginClientFrame *frame)
455 frame_buffer.append(frame);
458 void PluginClient::send_render_gui()
460 server->send_render_gui(&frame_buffer);
463 void PluginClient::send_render_gui(void *data)
465 server->send_render_gui(data);
468 void PluginClient::send_render_gui(void *data, int size)
470 server->send_render_gui(data, size);
473 void PluginClient::plugin_render_gui(void *data, int size)
475 render_gui(data, size);
479 void PluginClient::plugin_render_gui(void *data)
484 void PluginClient::render_gui(void *data)
488 thread->get_window()->lock_window("PluginClient::render_gui");
490 // Set all previous frames to draw immediately
491 for(int i = 0; i < frame_buffer.size(); i++)
492 frame_buffer.get(i)->force = 1;
494 ArrayList<PluginClientFrame*> *src =
495 (ArrayList<PluginClientFrame*>*)data;
497 // Shift GUI data to GUI client
500 this->frame_buffer.append(src->get(0));
501 src->remove_number(0);
504 // Start the timer for the current buffer
505 update_timer->update();
506 thread->get_window()->unlock_window();
510 void PluginClient::render_gui(void *data, int size)
512 printf("PluginClient::render_gui %d\n", __LINE__);
522 int PluginClient::is_audio() { return 0; }
523 int PluginClient::is_video() { return 0; }
524 int PluginClient::is_theme() { return 0; }
525 int PluginClient::uses_gui() { return 1; }
526 int PluginClient::is_transition() { return 0; }
527 int PluginClient::load_defaults()
529 // printf("PluginClient::load_defaults undefined in %s.\n", plugin_title());
533 int PluginClient::save_defaults()
536 // printf("PluginClient::save_defaults undefined in %s.\n", plugin_title());
540 void PluginClient::load_defaults_xml()
542 char path[BCTEXTLEN];
543 server->get_defaults_path(path);
545 fs.complete_path(path);
547 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
549 KeyFrame temp_keyframe;
550 FILE *fd = fopen(path, "r");
553 char *data = temp_keyframe.get_data();
554 int data_size = fread(data, 1, MESSAGESIZE-1, fd);
555 if( data_size < 0 ) data_size = 0;
558 // Get window extents
560 for(int i = 0; i < data_size - 8; i++)
562 if(data[i] == '<') break;
567 window_x = atoi(data + i);
572 window_y = atoi(data + i);
575 while(i < data_size && isdigit(data[i])) i++;
579 read_data(&temp_keyframe);
585 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
588 void PluginClient::save_defaults_xml()
590 char path[BCTEXTLEN];
591 server->get_defaults_path(path);
593 fs.complete_path(path);
596 KeyFrame temp_keyframe;
597 save_data(&temp_keyframe);
599 const char *data = temp_keyframe.get_data();
600 int len = strlen(data);
601 FILE *fp = fopen(path, "w");
604 fprintf(fp, "%d\n%d\n", window_x, window_y);
605 if( len > 0 && !fwrite(data, len, 1, fp) ) {
606 fprintf(stderr, "PluginClient::save_defaults_xml %d \"%s\" %d bytes: %s\n",
607 __LINE__, path, len, strerror(errno));
615 int PluginClient::is_defaults()
617 return using_defaults;
620 BC_Hash* PluginClient::get_defaults()
624 PluginClientThread* PluginClient::get_thread()
629 BC_WindowBase* PluginClient::new_window()
631 printf("PluginClient::new_window undefined in %s.\n", plugin_title());
634 int PluginClient::get_parameters() { return 0; }
635 int PluginClient::get_samplerate() { return get_project_samplerate(); }
636 double PluginClient::get_framerate() { return get_project_framerate(); }
637 int PluginClient::init_realtime_parameters() { return 0; }
638 int PluginClient::delete_nonrealtime_parameters() { return 0; }
639 int PluginClient::start_loop() { return 0; };
640 int PluginClient::process_loop() { return 0; };
641 int PluginClient::stop_loop() { return 0; };
643 void PluginClient::set_interactive()
648 int64_t PluginClient::get_in_buffers(int64_t recommended_size)
650 return recommended_size;
653 int64_t PluginClient::get_out_buffers(int64_t recommended_size)
655 return recommended_size;
658 int PluginClient::get_gui_status()
660 return server->get_gui_status();
663 // close event from client side
664 void PluginClient::client_side_close()
666 // Last command executed
667 server->client_side_close();
670 int PluginClient::stop_gui_client()
672 if(!client_gui_on) return 0;
677 int PluginClient::get_project_samplerate()
679 return server->get_project_samplerate();
682 double PluginClient::get_project_framerate()
684 return server->get_project_framerate();
687 const char *PluginClient::get_source_path()
689 int64_t source_position = server->plugin->startproject;
690 Edit *edit = server->plugin->track->edits->editof(source_position,PLAY_FORWARD,0);
691 Indexable *indexable = edit ? edit->get_source() : 0;
692 return indexable ? indexable->path : 0;
696 void PluginClient::update_display_title()
698 server->generate_display_title(gui_string);
702 char* PluginClient::get_gui_string()
708 char* PluginClient::get_path()
713 char* PluginClient::get_plugin_dir()
715 return server->preferences->plugin_dir;
718 int PluginClient::set_string_client(char *string)
720 strcpy(gui_string, string);
726 int PluginClient::get_interpolation_type()
728 return server->get_interpolation_type();
732 float PluginClient::get_red()
734 EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
735 return !edl ? 0 : edl->local_session->use_max ?
736 edl->local_session->red_max :
737 edl->local_session->red;
740 float PluginClient::get_green()
742 EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
743 return !edl ? 0 : edl->local_session->use_max ?
744 edl->local_session->green_max :
745 edl->local_session->green;
748 float PluginClient::get_blue()
750 EDL *edl = server->mwindow ? server->mwindow->edl : server->edl;
751 return !edl ? 0 : edl->local_session->use_max ?
752 edl->local_session->blue_max :
753 edl->local_session->blue;
757 int64_t PluginClient::get_source_position()
759 return source_position;
762 int64_t PluginClient::get_source_start()
767 int64_t PluginClient::get_total_len()
772 int PluginClient::get_direction()
778 int64_t PluginClient::local_to_edl(int64_t position)
783 int64_t PluginClient::edl_to_local(int64_t position)
788 int PluginClient::get_use_opengl()
790 return server->get_use_opengl();
793 int PluginClient::get_total_buffers()
795 return total_in_buffers;
798 int PluginClient::get_buffer_size()
800 return in_buffer_size;
803 int PluginClient::get_project_smp()
805 //printf("PluginClient::get_project_smp %d %d\n", __LINE__, smp);
809 const char* PluginClient::get_defaultdir()
811 return File::get_plugin_path();
815 int PluginClient::send_hide_gui()
817 // Stop the GUI server and delete GUI messages
822 int PluginClient::send_configure_change()
825 server->mwindow->undo->update_undo_before(_("tweek"), this);
826 #ifdef USE_KEYFRAME_SPANNING
828 keyframe.copy_from(server->get_keyframe());
829 save_data(&keyframe);
830 server->apply_keyframe(&keyframe);
832 KeyFrame* keyframe = server->get_keyframe();
833 // Call save routine in plugin
837 server->mwindow->undo->update_undo_after(_("tweek"), LOAD_AUTOMATION);
838 server->sync_parameters();
843 KeyFrame* PluginClient::get_prev_keyframe(int64_t position, int is_local)
845 if(is_local) position = local_to_edl(position);
846 return server->get_prev_keyframe(position);
849 KeyFrame* PluginClient::get_next_keyframe(int64_t position, int is_local)
851 if(is_local) position = local_to_edl(position);
852 return server->get_next_keyframe(position);
855 void PluginClient::get_camera(float *x, float *y, float *z, int64_t position)
857 server->get_camera(x, y, z, position, direction);
860 void PluginClient::get_projector(float *x, float *y, float *z, int64_t position)
862 server->get_projector(x, y, z, position, direction);
866 EDLSession* PluginClient::get_edlsession()
869 return server->edl->session;
873 int PluginClient::gui_open()
875 return server->gui_open();