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()
58 //printf("PluginClientThread::~PluginClientThread %p %d\n", this, __LINE__);
60 //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();
71 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();
90 // Can't save defaults in the destructor because it's not called immediately
92 /* if(client->defaults) */ client->save_defaults_xml();
93 /* This is needed when the GUI is closed from itself */
94 if(result) client->client_side_close();
99 client->thread = this;
100 init_complete->unlock();
104 BC_WindowBase* PluginClientThread::get_window()
109 PluginClient* PluginClientThread::get_client()
119 PluginClientFrame::PluginClientFrame(int data_size,
123 this->data_size = data_size;
125 this->period_n = period_n;
126 this->period_d = period_d;
129 PluginClientFrame::~PluginClientFrame()
138 PluginClientWindow::PluginClientWindow(PluginClient *client,
144 : BC_Window(client->gui_string,
145 client->window_x /* - w / 2 */,
146 client->window_y /* - h / 2 */,
155 this->client = client;
158 PluginClientWindow::PluginClientWindow(const char *title,
180 PluginClientWindow::~PluginClientWindow()
185 int PluginClientWindow::translation_event()
189 client->window_x = get_x();
190 client->window_y = get_y();
196 int PluginClientWindow::close_event()
198 /* Set result to 1 to indicate a client side close */
207 PluginClient::PluginClient(PluginServer *server)
210 this->server = server;
211 smp = server->preferences->processors;
213 update_timer = new Timer;
214 // Virtual functions don't work here.
217 PluginClient::~PluginClient()
219 // Delete the GUI thread. The GUI must be hidden with hide_gui first.
226 // Virtual functions don't work here.
227 if(defaults) delete defaults;
228 frame_buffer.remove_all_objects();
232 int PluginClient::reset()
241 realtime_priority = 0;
243 total_in_buffers = 0;
244 total_out_buffers = 0;
248 direction = PLAY_FORWARD;
255 void PluginClient::hide_gui()
257 if(thread && thread->window)
259 //printf("PluginClient::delete_thread %d\n", __LINE__);
260 /* This is needed when the GUI is closed from elsewhere than itself */
261 /* Since we now use autodelete, this is all that has to be done, thread will take care of itself ... */
262 /* Thread join will wait if this was not called from the thread itself or go on if it was */
263 thread->window->lock_window("PluginClient::hide_gui");
264 thread->window->set_done(0);
265 //printf("PluginClient::hide_gui %d thread->window=%p\n", __LINE__, thread->window);
266 thread->window->unlock_window();
267 //printf("PluginClient::delete_thread %d\n", __LINE__);
271 // For realtime plugins initialize buffers
272 int PluginClient::plugin_init_realtime(int realtime_priority,
273 int total_in_buffers,
277 // Get parameters for all
278 master_gui_on = get_gui_status();
282 // get parameters depending on video or audio
283 init_realtime_parameters();
285 this->realtime_priority = realtime_priority;
286 this->total_in_buffers = this->total_out_buffers = total_in_buffers;
287 this->out_buffer_size = this->in_buffer_size = buffer_size;
291 int PluginClient::plugin_start_loop(int64_t start,
296 //printf("PluginClient::plugin_start_loop %d %ld %ld %ld %d\n",
297 // __LINE__, start, end, buffer_size, total_buffers);
298 this->source_start = start;
299 this->total_len = end - start;
302 this->in_buffer_size = this->out_buffer_size = buffer_size;
303 this->total_in_buffers = this->total_out_buffers = total_buffers;
308 int PluginClient::plugin_process_loop()
310 return process_loop();
313 int PluginClient::plugin_stop_loop()
318 MainProgressBar* PluginClient::start_progress(char *string, int64_t length)
320 return server->start_progress(string, length);
324 // Non realtime parameters
325 int PluginClient::plugin_get_parameters()
327 int result = get_parameters();
328 if(defaults) save_defaults();
332 // ========================= main loop
334 int PluginClient::is_multichannel() { return 0; }
335 int PluginClient::is_synthesis() { return 0; }
336 int PluginClient::is_realtime() { return 0; }
337 int PluginClient::is_fileio() { return 0; }
338 int PluginClient::delete_buffer_ptrs() { return 0; }
339 const char* PluginClient::plugin_title() { return _("Untitled"); }
341 Theme* PluginClient::new_theme() { return 0; }
343 int PluginClient::load_configuration()
348 Theme* PluginClient::get_theme()
350 return server->get_theme();
353 int PluginClient::show_gui()
355 load_configuration();
356 thread = new PluginClientThread(this);
358 thread->init_complete->lock("PluginClient::show_gui");
359 // Must wait before sending any hide_gui
362 thread->window->init_wait();
371 void PluginClient::raise_window()
373 if(thread && thread->window)
375 thread->window->lock_window("PluginClient::raise_window");
376 thread->window->raise_window();
377 thread->window->flush();
378 thread->window->unlock_window();
382 int PluginClient::set_string()
386 thread->window->lock_window("PluginClient::set_string");
387 thread->window->set_title(gui_string);
388 thread->window->unlock_window();
397 void PluginClient::begin_process_buffer()
399 // Delete all unused GUI frames
400 frame_buffer.remove_all_objects();
404 void PluginClient::end_process_buffer()
406 if(frame_buffer.size())
414 void PluginClient::plugin_update_gui()
419 // Delete unused GUI frames
420 while(frame_buffer.size() > MAX_FRAME_BUFFER)
421 frame_buffer.remove_object_number(0);
425 void PluginClient::update_gui()
429 int PluginClient::get_gui_update_frames()
431 if(frame_buffer.size())
433 PluginClientFrame *frame = frame_buffer.get(0);
434 int total_frames = update_timer->get_difference() *
438 if(total_frames) update_timer->subtract(total_frames *
443 // printf("PluginClient::get_gui_update_frames %d %ld %d %d %d\n",
445 // update_timer->get_difference(),
446 // frame->period_n * 1000 / frame->period_d,
448 // frame_buffer.size());
451 for(int i = 0; i < frame_buffer.size(); i++)
452 if(frame_buffer.get(i)->force) total_frames++;
453 total_frames = MIN(frame_buffer.size(), total_frames);
464 PluginClientFrame* PluginClient::get_gui_frame()
466 if(frame_buffer.size())
468 PluginClientFrame *frame = frame_buffer.get(0);
469 frame_buffer.remove_number(0);
478 void PluginClient::add_gui_frame(PluginClientFrame *frame)
480 frame_buffer.append(frame);
483 void PluginClient::send_render_gui()
485 server->send_render_gui(&frame_buffer);
488 void PluginClient::send_render_gui(void *data)
490 server->send_render_gui(data);
493 void PluginClient::send_render_gui(void *data, int size)
495 server->send_render_gui(data, size);
498 void PluginClient::plugin_render_gui(void *data, int size)
500 render_gui(data, size);
504 void PluginClient::plugin_render_gui(void *data)
509 void PluginClient::render_gui(void *data)
513 thread->get_window()->lock_window("PluginClient::render_gui");
515 // Set all previous frames to draw immediately
516 for(int i = 0; i < frame_buffer.size(); i++)
517 frame_buffer.get(i)->force = 1;
519 ArrayList<PluginClientFrame*> *src =
520 (ArrayList<PluginClientFrame*>*)data;
522 // Shift GUI data to GUI client
525 this->frame_buffer.append(src->get(0));
526 src->remove_number(0);
529 // Start the timer for the current buffer
530 update_timer->update();
531 thread->get_window()->unlock_window();
535 void PluginClient::render_gui(void *data, int size)
537 printf("PluginClient::render_gui %d\n", __LINE__);
547 int PluginClient::is_audio() { return 0; }
548 int PluginClient::is_video() { return 0; }
549 int PluginClient::is_theme() { return 0; }
550 int PluginClient::uses_gui() { return 1; }
551 int PluginClient::is_transition() { return 0; }
552 int PluginClient::load_defaults()
554 // printf("PluginClient::load_defaults undefined in %s.\n", plugin_title());
558 int PluginClient::save_defaults()
561 // printf("PluginClient::save_defaults undefined in %s.\n", plugin_title());
565 void PluginClient::load_defaults_xml()
567 char path[BCTEXTLEN];
568 server->get_defaults_path(path);
570 fs.complete_path(path);
572 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
574 KeyFrame temp_keyframe;
575 FILE *fd = fopen(path, "r");
578 char *data = temp_keyframe.get_data();
579 int data_size = fread(data, 1, MESSAGESIZE-1, fd);
580 if( data_size < 0 ) data_size = 0;
583 // Get window extents
585 for(int i = 0; i < data_size - 8; i++)
587 if(data[i] == '<') break;
592 window_x = atoi(data + i);
597 window_y = atoi(data + i);
600 while(i < data_size && isdigit(data[i])) i++;
604 read_data(&temp_keyframe);
610 //printf("PluginClient::load_defaults_xml %d %s\n", __LINE__, path);
613 void PluginClient::save_defaults_xml()
615 char path[BCTEXTLEN];
616 server->get_defaults_path(path);
618 fs.complete_path(path);
621 KeyFrame temp_keyframe;
623 save_data(&temp_keyframe);
624 FILE *fd = fopen(path, "w");
627 fprintf(fd, "%d\n%d\n", window_x, window_y);
628 if(!fwrite(temp_keyframe.get_data(), strlen(temp_keyframe.get_data()), 1, fd))
630 fprintf(stderr, "PluginClient::save_defaults_xml %d \"%s\" %d bytes: %s\n",
633 (int)strlen(temp_keyframe.get_data()),
642 int PluginClient::is_defaults()
644 return using_defaults;
647 BC_Hash* PluginClient::get_defaults()
651 PluginClientThread* PluginClient::get_thread()
656 BC_WindowBase* PluginClient::new_window()
658 printf("PluginClient::new_window undefined in %s.\n", plugin_title());
661 int PluginClient::get_parameters() { return 0; }
662 int PluginClient::get_samplerate() { return get_project_samplerate(); }
663 double PluginClient::get_framerate() { return get_project_framerate(); }
664 int PluginClient::init_realtime_parameters() { return 0; }
665 int PluginClient::delete_nonrealtime_parameters() { return 0; }
666 int PluginClient::start_loop() { return 0; };
667 int PluginClient::process_loop() { return 0; };
668 int PluginClient::stop_loop() { return 0; };
670 void PluginClient::set_interactive()
675 int64_t PluginClient::get_in_buffers(int64_t recommended_size)
677 return recommended_size;
680 int64_t PluginClient::get_out_buffers(int64_t recommended_size)
682 return recommended_size;
685 int PluginClient::get_gui_status()
687 return server->get_gui_status();
690 int PluginClient::start_plugin()
692 printf(_("No processing defined for this plugin.\n"));
696 // close event from client side
697 void PluginClient::client_side_close()
699 // Last command executed
700 server->client_side_close();
703 int PluginClient::stop_gui_client()
705 if(!client_gui_on) return 0;
710 int PluginClient::get_project_samplerate()
712 return server->get_project_samplerate();
715 double PluginClient::get_project_framerate()
717 return server->get_project_framerate();
721 void PluginClient::update_display_title()
723 server->generate_display_title(gui_string);
727 char* PluginClient::get_gui_string()
733 char* PluginClient::get_path()
738 char* PluginClient::get_plugin_dir()
740 return server->preferences->plugin_dir;
743 int PluginClient::set_string_client(char *string)
745 strcpy(gui_string, string);
751 int PluginClient::get_interpolation_type()
753 return server->get_interpolation_type();
757 float PluginClient::get_red()
760 return server->mwindow->edl->local_session->red;
763 return server->edl->local_session->red;
768 float PluginClient::get_green()
771 return server->mwindow->edl->local_session->green;
774 return server->edl->local_session->green;
779 float PluginClient::get_blue()
782 return server->mwindow->edl->local_session->blue;
785 return server->edl->local_session->blue;
792 int64_t PluginClient::get_source_position()
794 return source_position;
797 int64_t PluginClient::get_source_start()
802 int64_t PluginClient::get_total_len()
807 int PluginClient::get_direction()
813 int64_t PluginClient::local_to_edl(int64_t position)
818 int64_t PluginClient::edl_to_local(int64_t position)
823 int PluginClient::get_use_opengl()
825 return server->get_use_opengl();
828 int PluginClient::get_total_buffers()
830 return total_in_buffers;
833 int PluginClient::get_buffer_size()
835 return in_buffer_size;
838 int PluginClient::get_project_smp()
840 //printf("PluginClient::get_project_smp %d %d\n", __LINE__, smp);
844 const char* PluginClient::get_defaultdir()
850 int PluginClient::send_hide_gui()
852 // Stop the GUI server and delete GUI messages
857 int PluginClient::send_configure_change()
860 server->mwindow->undo->update_undo_before(_("tweek"), this);
861 #ifdef USE_KEYFRAME_SPANNING
863 keyframe.copy_from(server->get_keyframe());
864 save_data(&keyframe);
865 server->apply_keyframe(&keyframe);
867 KeyFrame* keyframe = server->get_keyframe();
868 // Call save routine in plugin
872 server->mwindow->undo->update_undo_after(_("tweek"), LOAD_AUTOMATION);
873 server->sync_parameters();
878 KeyFrame* PluginClient::get_prev_keyframe(int64_t position, int is_local)
880 if(is_local) position = local_to_edl(position);
881 return server->get_prev_keyframe(position);
884 KeyFrame* PluginClient::get_next_keyframe(int64_t position, int is_local)
886 if(is_local) position = local_to_edl(position);
887 return server->get_next_keyframe(position);
890 void PluginClient::get_camera(float *x, float *y, float *z, int64_t position)
892 server->get_camera(x, y, z, position, direction);
895 void PluginClient::get_projector(float *x, float *y, float *z, int64_t position)
897 server->get_projector(x, y, z, position, direction);
901 EDLSession* PluginClient::get_edlsession()
904 return server->edl->session;
908 int PluginClient::gui_open()
910 return server->gui_open();