consistent step+page sizes for fader+spinbox
[ardour.git] / gtk2_ardour / linux_vst_gui_support.cc
index 161b6495c140c166c36efab4e75239efa148c762..e438ed6f926f98c83a28d6c96b199b3aa7b38e64 100644 (file)
@@ -1,21 +1,36 @@
-/******************************************************************/
-/** VSTFX - An engine based on FST for handling linuxVST plugins **/
-/******************************************************************/
+/*
+    Copyright (C) 2012 Paul Davis 
+    Based on code by Paul Davis, Torben Hohn as part of FST
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
 
-/*This is derived from the original FST (C code) with some tweaks*/
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
 
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-/** EDITOR tab stops at 4 **/
+*/
+
+/******************************************************************/
+/** VSTFX - An engine based on FST for handling linuxVST plugins **/
+/******************************************************************/
 
 #include <stdlib.h>
 #include <stdio.h>
-#include <jack/jack.h>
-#include <jack/thread.h>
 #include <libgen.h>
+#include <assert.h>
 
 #include <pthread.h>
 #include <signal.h>
 #include <glib.h>
+#include <glibmm/timer.h>
 
 #include "ardour/linux_vst_support.h"
 
@@ -26,6 +41,7 @@
 #include <time.h>
 #include <unistd.h>
 #include <pthread.h>
+#include <sys/time.h>
 
 struct ERect{
     short top;
@@ -317,10 +333,11 @@ windows, that is if they don't manage their own UIs **/
 void* gui_event_loop (void* ptr)
 {
        VSTState* vstfx;
-       int LXVST_sched_event_timer = 0;
-       int LXVST_sched_timer_interval = 50; //ms
+       int LXVST_sched_timer_interval = 40; //ms, 25fps
        XEvent event;
+       uint64_t clock1, clock2;
        
+       clock1 = g_get_monotonic_time();
        /*The 'Forever' loop - runs the plugin UIs etc - based on the FST gui event loop*/
        
        while (!gui_quit)
@@ -360,16 +377,16 @@ void* gui_event_loop (void* ptr)
 
                /*We don't want to use all the CPU.. */
 
-               usleep(1000);
+               Glib::usleep(1000);
                
-               LXVST_sched_event_timer++;
-               
-               LXVST_sched_event_timer = LXVST_sched_event_timer & 0x00FFFFFF;
-
                /*See if its time for us to do a scheduled event pass on all the plugins*/
 
-               if((LXVST_sched_timer_interval!=0) && (!(LXVST_sched_event_timer% LXVST_sched_timer_interval)))
+               clock2 = g_get_monotonic_time();
+               const int64_t elapsed_time_ms = (clock2 - clock1) / 1000;
+
+               if((LXVST_sched_timer_interval != 0) && elapsed_time_ms >= LXVST_sched_timer_interval)
                {
+                       //printf("elapsed %d ms ^= %.2f Hz\n", elapsed_time_ms, 1000.0/(double)elapsed_time_ms); // DEBUG
                        pthread_mutex_lock (&plugin_mutex);
                    
 again:
@@ -444,6 +461,8 @@ again:
                                pthread_mutex_unlock (&vstfx->lock);
                        }
                        pthread_mutex_unlock (&plugin_mutex);
+
+                       clock1 = g_get_monotonic_time();
                }
        }
 
@@ -689,7 +708,7 @@ vstfx_launch_editor (VSTState* vstfx)
        /*QUIRK - some plugins need a slight delay after opening the editor before you can
        ask the window size or they might return zero - specifically discoDSP */
        
-       usleep(100000);
+       Glib::usleep(100000);
        
        /*Now we can find out how big the parent window should be (and try) to resize it*/
        
@@ -752,6 +771,10 @@ vstfx_event_loop_remove_plugin (VSTState* vstfx)
                }
        }
 
+       // if this function is called, there must be
+       // at least one plugin in the linked list
+       assert(vstfx_first);
+
        if (vstfx_first == vstfx) {
                vstfx_first = vstfx_first->next;
        }