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"
28 #include "edlsession.h"
29 #include "filesystem.h"
31 #include "localsession.h"
34 #include "pluginclient.h"
35 #include "pluginserver.h"
36 #include "preferences.h"
37 #include "transportque.inc"
48 PluginClientThread::PluginClientThread(PluginClient *client)
51 this->client = client;
53 init_complete = new Condition(0, "PluginClientThread::init_complete");
56 PluginClientThread::~PluginClientThread()
59 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
60 delete window; window = 0;
61 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
65 void PluginClientThread::run()
69 if(client->window_x < 0) client->window_x = info.get_abs_cursor_x();
70 if(client->window_y < 0) client->window_y = info.get_abs_cursor_y();
72 window = client->new_window();
75 window->lock_window("PluginClientThread::run");
76 window->create_objects();
77 window->unlock_window();
79 /* Only set it here so tracking doesn't update it until everything is created. */
80 client->thread = this;
81 init_complete->unlock();
83 result = window->run_window();
84 window->lock_window("PluginClientThread::run");
85 //printf("PluginClientThread::run %p %d\n", this, __LINE__);
86 window->hide_window(1);
87 window->unlock_window();
88 // Can't save defaults in the destructor because it's not called immediately
90 /* if(client->defaults) */ client->save_defaults_xml();
91 /* This is needed when the GUI is closed from itself */
92 if(result) client->client_side_close();
97 client->thread = this;
98 init_complete->unlock();
102 BC_WindowBase* PluginClientThread::get_window()
107 PluginClient* PluginClientThread::get_client()
117 PluginClientFrame::PluginClientFrame(int data_size,
121 this->data_size = data_size;
123 this->period_n = period_n;
124 this->period_d = period_d;
127 PluginClientFrame::~PluginClientFrame()
136 PluginClientWindow::PluginClientWindow(PluginClient *client,
142 : BC_Window(client->gui_string,
143 client->window_x /* - w / 2 */,
144 client->window_y /* - h / 2 */,
145 w, h, min_w, min_h, allow_resize, 0, 1)
147 this->client = client;
150 PluginClientWindow::PluginClientWindow(const char *title,
151 int x, int y, int w, int h, int min_w, int min_h, int allow_resize)
152 : BC_Window(title, x, y, w, h, min_w, min_h, allow_resize, 0, 1)
157 PluginClientWindow::~PluginClientWindow()
162 int PluginClientWindow::translation_event()
166 client->window_x = get_x();
167 client->window_y = get_y();
173 int PluginClientWindow::close_event()
175 /* Set result to 1 to indicate a client side close */
184 PluginClient::PluginClient(PluginServer *server)
187 this->server = server;
188 smp = server->preferences->processors;
190 update_timer = new Timer;
191 // Virtual functions don't work here.
194 PluginClient::~PluginClient()
196 // Delete the GUI thread. The GUI must be hidden with hide_gui first.
199 // Virtual functions don't work here.
200 if(defaults) delete defaults;
201 frame_buffer.remove_all_objects();
205 int PluginClient::reset()
214 realtime_priority = 0;
216 total_in_buffers = 0;
217 total_out_buffers = 0;
221 direction = PLAY_FORWARD;
228 void PluginClient::hide_gui()
230 if(thread && thread->window)
232 //printf("PluginClient::delete_thread %d\n", __LINE__);
233 /* This is needed when the GUI is closed from elsewhere than itself */
234 /* Since we now use autodelete, this is all that has to be done, thread will take care of itself ... */
235 /* Thread join will wait if this was not called from the thread itself or go on if it was */
236 thread->window->lock_window("PluginClient::hide_gui");
237 thread->window->set_done(0);
238 //printf("PluginClient::hide_gui %d thread->window=%p\n", __LINE__, thread->window);
239 thread->window->unlock_window();
240 //printf("PluginClient::delete_thread %d\n", __LINE__);
244 // For realtime plugins initialize buffers
245 int PluginClient::plugin_init_realtime(int realtime_priority,
246 int total_in_buffers,
250 // Get parameters for all
251 master_gui_on = get_gui_status();
255 // get parameters depending on video or audio
256 init_realtime_parameters();
258 this->realtime_priority = realtime_priority;
259 this->total_in_buffers = this->total_out_buffers = total_in_buffers;
260 this->out_buffer_size = this->in_buffer_size = buffer_size;
264 int PluginClient::plugin_start_loop(int64_t start,
269 //printf("PluginClient::plugin_start_loop %d %ld %ld %ld %d\n",
270 // __LINE__, start, end, buffer_size, total_buffers);
271 this->source_start = start;
272 this->total_len = end - start;
275 this->in_buffer_size = this->out_buffer_size = buffer_size;
276 this->total_in_buffers = this->total_out_buffers = total_buffers;
281 int PluginClient::plugin_process_loop()
283 return process_loop();
286 int PluginClient::plugin_stop_loop()
291 MainProgressBar* PluginClient::start_progress(char *string, int64_t length)
293 return server->start_progress(string, length);
297 // Non realtime parameters
298 int PluginClient::plugin_get_parameters()
300 int result = get_parameters();
301 if(defaults) save_defaults();
305 // ========================= main loop
307 int PluginClient::is_multichannel() { return 0; }
308 int PluginClient::is_synthesis() { return 0; }
309 int PluginClient::is_realtime() { return 0; }
310 int PluginClient::is_fileio() { return 0; }
311 int PluginClient::delete_buffer_ptrs() { return 0; }
312 const char* PluginClient::plugin_title() { return _("Untitled"); }
314 Theme* PluginClient::new_theme() { return 0; }
316 int PluginClient::load_configuration()
321 Theme* PluginClient::get_theme()
323 return server->get_theme();
326 int PluginClient::show_gui()
328 load_configuration();
329 thread = new PluginClientThread(this);
331 thread->init_complete->lock("PluginClient::show_gui");
332 // Must wait before sending any hide_gui
335 thread->window->init_wait();
344 void PluginClient::raise_window()
346 if(thread && thread->window)
348 thread->window->lock_window("PluginClient::raise_window");
349 thread->window->raise_window();
350 thread->window->flush();
351 thread->window->unlock_window();
355 int PluginClient::set_string()
359 thread->window->lock_window("PluginClient::set_string");
360 thread->window->put_title(gui_string);
361 thread->window->unlock_window();
370 void PluginClient::begin_process_buffer()
372 // Delete all unused GUI frames
373 frame_buffer.remove_all_objects();
377 void PluginClient::end_process_buffer()
379 if(frame_buffer.size())
387 void PluginClient::plugin_update_gui()
392 // Delete unused GUI frames
393 while(frame_buffer.size() > MAX_FRAME_BUFFER)
394 frame_buffer.remove_object_number(0);
398 void PluginClient::update_gui()
402 int PluginClient::get_gui_update_frames()
404 if(frame_buffer.size())
406 PluginClientFrame *frame = frame_buffer.get(0);
407 int total_frames = update_timer->get_difference() *
411 if(total_frames) update_timer->subtract(total_frames *
416 // printf("PluginClient::get_gui_update_frames %d %ld %d %d %d\n",
418 // update_timer->get_difference(),
419 // frame->period_n * 1000 / frame->period_d,
421 // frame_buffer.size());
424 for(int i = 0; i < frame_buffer.size(); i++)
425 if(frame_buffer.get(i)->force) total_frames++;
426 total_frames = MIN(frame_buffer.size(), total_frames);
437 PluginClientFrame* PluginClient::get_gui_frame()
439 if(frame_buffer.size())
441 PluginClientFrame *frame = frame_buffer.get(0);
442 frame_buffer.remove_number(0);
451 void PluginClient::add_gui_frame(PluginClientFrame *frame)
453 frame_buffer.append(frame);
456 void PluginClient::send_render_gui()
458 server->send_render_gui(&frame_buffer);
461 void PluginClient::send_render_gui(void *data)
463 server->send_render_gui(data);
466 void PluginClient::send_render_gui(void *data, int size)
468 server->send_render_gui(data, size);
471 void PluginClient::plugin_render_gui(void *data, int size)
473 render_gui(data, size);
477 void PluginClient::plugin_render_gui(void *data)
482 void PluginClient::render_gui(void *data)
486 thread->get_window()->lock_window("PluginClient::render_gui");
488 // Set all previous frames to draw immediately
489 for(int i = 0; i < frame_buffer.size(); i++)
490 frame_buffer.get(i)->force = 1;
492 ArrayList<PluginClientFrame*> *src =
493 (ArrayList<PluginClientFrame*>*)data;
495 // Shift GUI data to GUI client
498 this->frame_buffer.append(src->get(0));
499 src->remove_number(0);
502 // Start the timer for the current buffer
503 update_timer->update();
504 thread->get_window()->unlock_window();
508 void PluginClient::render_gui(void *data, int size)
510 printf("PluginClient::render_gui %d\n", __LINE__);
520 int PluginClient::is_audio() { return 0; }
521 int PluginClient::is_video() { return 0; }
522 int PluginClient::is_theme() { return 0; }
523 int PluginClient::uses_gui() { return 1; }
524 int PluginClient::is_transition() { return 0; }
525 int PluginClient::load_defaults()
527 // printf("PluginClient::load_defaults undefined in %s.\n", plugin_title());
531 int PluginClient::save_defaults()
534 // printf("PluginClient::save_defaults undefined in %s.\n", plugin_title());
538 void PluginClient::load_defaults_xml()
540 char path[BCTEXTLEN];
541 server->get_defaults_path(path);
543 fs.complete_path(path);
545 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
547 KeyFrame temp_keyframe;
548 FILE *fd = fopen(path, "r");
551 char *data = temp_keyframe.get_data();
552 int data_size = fread(data, 1, MESSAGESIZE-1, fd);
553 if( data_size < 0 ) data_size = 0;
556 // Get window extents
558 for(int i = 0; i < data_size - 8; i++)
560 if(data[i] == '<') break;
565 window_x = atoi(data + i);
570 window_y = atoi(data + i);
573 while(i < data_size && isdigit(data[i])) i++;
577 read_data(&temp_keyframe);
583 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
586 void PluginClient::save_defaults_xml()
588 char path[BCTEXTLEN];
589 server->get_defaults_path(path);
591 fs.complete_path(path);
594 KeyFrame temp_keyframe;
595 save_data(&temp_keyframe);
597 const char *data = temp_keyframe.get_data();
598 int len = strlen(data);
599 FILE *fp = fopen(path, "w");
602 fprintf(fp, "%d\n%d\n", window_x, window_y);
603 if( len > 0 && !fwrite(data, len, 1, fp) ) {
604 fprintf(stderr, "PluginClient::save_defaults_xml %d \"%s\" %d bytes: %s\n",
605 __LINE__, path, len, strerror(errno));
613 int PluginClient::is_defaults()
615 return using_defaults;
618 BC_Hash* PluginClient::get_defaults()
622 PluginClientThread* PluginClient::get_thread()
627 BC_WindowBase* PluginClient::new_window()
629 printf("PluginClient::new_window undefined in %s.\n", plugin_title());
632 int PluginClient::get_parameters() { return 0; }
633 int PluginClient::get_samplerate() { return get_project_samplerate(); }
634 double PluginClient::get_framerate() { return get_project_framerate(); }
635 int PluginClient::init_realtime_parameters() { return 0; }
636 int PluginClient::delete_nonrealtime_parameters() { return 0; }
637 int PluginClient::start_loop() { return 0; };
638 int PluginClient::process_loop() { return 0; };
639 int PluginClient::stop_loop() { return 0; };
641 void PluginClient::set_interactive()
646 int64_t PluginClient::get_in_buffers(int64_t recommended_size)
648 return recommended_size;
651 int64_t PluginClient::get_out_buffers(int64_t recommended_size)
653 return recommended_size;
656 int PluginClient::get_gui_status()
658 return server->get_gui_status();
661 // close event from client side
662 void PluginClient::client_side_close()
664 // Last command executed
665 server->client_side_close();
668 int PluginClient::stop_gui_client()
670 if(!client_gui_on) return 0;
675 int PluginClient::get_project_samplerate()
677 return server->get_project_samplerate();
680 double PluginClient::get_project_framerate()
682 return server->get_project_framerate();
686 void PluginClient::update_display_title()
688 server->generate_display_title(gui_string);
692 char* PluginClient::get_gui_string()
698 char* PluginClient::get_path()
703 char* PluginClient::get_plugin_dir()
705 return server->preferences->plugin_dir;
708 int PluginClient::set_string_client(char *string)
710 strcpy(gui_string, string);
716 int PluginClient::get_interpolation_type()
718 return server->get_interpolation_type();
722 float PluginClient::get_red()
725 return server->mwindow->edl->local_session->red;
728 return server->edl->local_session->red;
733 float PluginClient::get_green()
736 return server->mwindow->edl->local_session->green;
739 return server->edl->local_session->green;
744 float PluginClient::get_blue()
747 return server->mwindow->edl->local_session->blue;
750 return server->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()
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();