Drop references held by any GUI Lua script after execution
[ardour.git] / gtk2_ardour / au_pluginui.mm
index edf3ee5ffbca3074bf42839b8e82fb4bee6c035d..d4003550b5cdf27f66054cf1822fec0f6e2df1b4 100644 (file)
@@ -1,8 +1,29 @@
+/*
+ * Copyright (C) 2008-2014 David Robillard <d@drobilla.net>
+ * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
+ * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
+ *
+ * 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 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
 #undef  Marker
 #define Marker FuckYouAppleAndYourLackOfNameSpaces
 
 #include <sys/time.h>
 #include <gtkmm/button.h>
+#include <gtkmm/comboboxtext.h>
 #include <gdk/gdkquartz.h>
 
 #include "pbd/convert.h"
 #include "gui_thread.h"
 #include "processor_box.h"
 
+// yes, yes we know (see wscript for various available OSX compat modes)
+#if defined (__clang__)
+#      pragma clang diagnostic push
+#      pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#endif
+
 #include "CAAudioUnit.h"
 #include "CAComponent.h"
 
+#if defined (__clang__)
+#      pragma clang diagnostic pop
+#endif
+
 #import <AudioUnit/AUCocoaUIView.h>
 #import <CoreAudioKit/AUGenericView.h>
 #import <objc/runtime.h>
+
+#ifndef __ppc__
 #include <dispatch/dispatch.h>
+#endif
 
 #undef Marker
 
 #include "keyboard.h"
 #include "utils.h"
 #include "public_editor.h"
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 #include "gtk2ardour-config.h"
 
@@ -50,8 +84,10 @@ using namespace std;
 using namespace PBD;
 
 vector<string> AUPluginUI::automation_mode_strings;
-bool AUPluginUI::idle_meter_needed = true;
-int64_t AUPluginUI::last_idle = 0;
+int64_t AUPluginUI::last_timer = 0;
+bool    AUPluginUI::timer_needed = true;
+CFRunLoopTimerRef AUPluginUI::cf_timer;
+sigc::connection AUPluginUI::timer_connection;
 
 static const gchar* _automation_mode_strings[] = {
        X_("Manual"),
@@ -194,15 +230,33 @@ dump_view_tree (NSView* view, int depth, int maxdepth)
  * certainly be possible to make it work for Ardour.
  */
 
+static uint32_t block_plugin_redraws = 0;
+static const uint32_t minimum_redraw_rate = 30; /* frames per second */
+static const uint32_t block_plugin_redraw_count = 15; /* number of combined plugin redraws to block, if blocking */
+
+#ifdef __ppc__
+
+/* PowerPC versions of OS X do not support libdispatch, which we use below when swizzling objective C. But they also don't have Retina
+ * which is the underlying reason for this code. So just skip it on those CPUs.
+ */
+
+
+static void add_plugin_view (id view) {}
+static void remove_plugin_view (id view) {}
+
+#else
+
 static IMP original_nsview_drawIfNeeded;
 static std::vector<id> plugin_views;
-static uint32_t block_plugin_redraws = 0;
-static const uint32_t minimum_redraw_rate = 25; /* frames per second */
-static const uint32_t block_plugin_redraw_count = 100; /* number of combined plugin redraws to block, if blocking */
 
 static void add_plugin_view (id view)
 {
+       if (plugin_views.empty()) {
+               AUPluginUI::start_cf_timer ();
+       }
+
        plugin_views.push_back (view);
+
 }
 
 static void remove_plugin_view (id view)
@@ -211,13 +265,18 @@ static void remove_plugin_view (id view)
        if (x != plugin_views.end()) {
                plugin_views.erase (x);
        }
+       if (plugin_views.empty()) {
+               AUPluginUI::stop_cf_timer ();
+       }
 }
 
 static void interposed_drawIfNeeded (id receiver, SEL selector, NSRect rect)
 {
        if (block_plugin_redraws && (find (plugin_views.begin(), plugin_views.end(), receiver) != plugin_views.end())) {
                block_plugin_redraws--;
+#ifdef AU_DEBUG_PRINT
                std::cerr << "Plugin redraw blocked\n";
+#endif
                /* YOU ... SHALL .... NOT ... DRAW!!!! */
                return;
        }
@@ -242,6 +301,8 @@ static void interposed_drawIfNeeded (id receiver, SEL selector, NSRect rect)
 
 @end
 
+#endif /* __ppc__ */
+
 /* END OF THE PLUGIN REDRAW HACK */
 
 @implementation NotificationObject
@@ -345,7 +406,6 @@ AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
        , cocoa_parent (0)
        , _notify (0)
        , _resize_notify (0)
-       , expose_cnt (0)
 {
        if (automation_mode_strings.empty()) {
                automation_mode_strings = I18N (_automation_mode_strings);
@@ -364,22 +424,24 @@ AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
        HBox* smaller_hbox = manage (new HBox);
 
        smaller_hbox->set_spacing (6);
+       smaller_hbox->pack_start (pin_management_button, false, false, 4);
        smaller_hbox->pack_start (preset_label, false, false, 4);
        smaller_hbox->pack_start (_preset_modified, false, false);
        smaller_hbox->pack_start (_preset_combo, false, false);
        smaller_hbox->pack_start (add_button, false, false);
-#if 0
-       /* Ardour does not currently allow to overwrite existing presets
-        * see save_property_list() in audio_unit.cc
-        */
        smaller_hbox->pack_start (save_button, false, false);
-#endif
+       smaller_hbox->pack_start (delete_button, false, false);
+       if (has_descriptive_presets ()) {
+               smaller_hbox->pack_start (preset_browser_button, false, false);
+       }
 #if 0
        /* one day these might be useful with an AU plugin, but not yet */
        smaller_hbox->pack_start (automation_mode_label, false, false);
        smaller_hbox->pack_start (automation_mode_selector, false, false);
 #endif
-       smaller_hbox->pack_start (reset_button, false, false);
+       if (insert->controls().size() > 0) {
+               smaller_hbox->pack_start (reset_button, false, false);
+       }
        smaller_hbox->pack_start (bypass_button, false, true);
 
        VBox* v1_box = manage (new VBox);
@@ -439,7 +501,6 @@ AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
                low_box.signal_size_allocate ().connect (mem_fun (this, &AUPluginUI::lower_box_size_allocate));
                low_box.signal_map ().connect (mem_fun (this, &AUPluginUI::lower_box_map));
                low_box.signal_unmap ().connect (mem_fun (this, &AUPluginUI::lower_box_unmap));
-               low_box.signal_expose_event ().connect (mem_fun (this, &AUPluginUI::lower_box_expose));
        }
 }
 
@@ -673,28 +734,78 @@ AUPluginUI::update_view_size ()
 }
 
 bool
-AUPluginUI::idle_meter ()
+AUPluginUI::timer_callback ()
+{
+       block_plugin_redraws = 0;
+#ifdef AU_DEBUG_PRINT
+       std::cerr << "Resume redraws after idle\n";
+#endif
+       return false;
+}
+
+void
+au_cf_timer_callback (CFRunLoopTimerRef timer, void* info)
+{
+       reinterpret_cast<AUPluginUI*> (info)->cf_timer_callback ();
+}
+
+void
+AUPluginUI::cf_timer_callback ()
 {
        int64_t now = ARDOUR::get_microseconds ();
 
-       if (!last_idle) {
-               last_idle = now;
-               return true; /* call me again */
+       if (!last_timer || block_plugin_redraws) {
+               last_timer = now;
+               return;
        }
 
-       if ((now - last_idle) > (1000000/minimum_redraw_rate)) {
+       const int64_t usecs_slop = (1400000 / minimum_redraw_rate); // 140%
+
+#ifdef AU_DEBUG_PRINT
+       std::cerr << "Timer elapsed : " << now - last_timer << std::endl;
+#endif
+
+       if ((now - last_timer) > (usecs_slop + (1000000/minimum_redraw_rate))) {
                block_plugin_redraws = block_plugin_redraw_count;
-               std::cerr << "Idle too slow (" << (now - last_idle) << " usecs), block plugin redraws for the next 10 plugin exposes\n";
-       } else {
-               std::cerr << "idle all good: elapsed was " << (now - last_idle) << endl;
+               timer_connection.disconnect ();
+               timer_connection = Glib::signal_timeout().connect (&AUPluginUI::timer_callback, 40);
+#ifdef AU_DEBUG_PRINT
+               std::cerr << "Timer too slow, block plugin redraws\n";
+#endif
        }
 
-       /* We've been called twice. Cancel everything for now. */
+       last_timer = now;
+}
 
-       idle_meter_needed = true;
-       last_idle = 0;
+void
+AUPluginUI::start_cf_timer ()
+{
+       if (!timer_needed) {
+               return;
+       }
 
-       return false;
+       CFTimeInterval interval = 1.0 / (float) minimum_redraw_rate;
+
+       cf_timer = CFRunLoopTimerCreate (kCFAllocatorDefault,
+                                        CFAbsoluteTimeGetCurrent() + interval,
+                                        interval, 0, 0,
+                                        au_cf_timer_callback,
+                                        0);
+
+       CFRunLoopAddTimer (CFRunLoopGetCurrent(), cf_timer, kCFRunLoopCommonModes);
+       timer_needed = false;
+}
+
+void
+AUPluginUI::stop_cf_timer ()
+{
+       if (timer_needed) {
+               return;
+       }
+
+       CFRunLoopRemoveTimer (CFRunLoopGetCurrent(), cf_timer, kCFRunLoopCommonModes);
+       timer_needed = true;
+       last_timer = 0;
 }
 
 void
@@ -726,7 +837,9 @@ AUPluginUI::cocoa_view_resized ()
                 * NSView, resulting in a reentrant call to the FrameDidChange
                 * handler (this method). Ignore this reentrant call.
                 */
+#ifdef AU_DEBUG_PRINT
                std::cerr << plugin->name() << " re-entrant call to cocoa_view_resized, ignored\n";
+#endif
                return;
        }
 
@@ -1136,22 +1249,6 @@ AUPluginUI::lower_box_size_allocate (Gtk::Allocation& allocation)
        update_view_size ();
 }
 
-bool
-AUPluginUI::lower_box_expose (GdkEventExpose* event)
-{
-       ++expose_cnt;
-
-       if (!(expose_cnt % 10)) {
-               /* every 10 exposes, check how frequently idle is being called */
-               if (idle_meter_needed) {
-                       Glib::signal_idle().connect (sigc::ptr_fun (AUPluginUI::idle_meter));
-                       idle_meter_needed = false;
-               }
-       }
-
-       return false;
-}
-
 void
 AUPluginUI::on_window_hide ()
 {