Enable Menu > Quit to work again after startup on macOS
[ardour.git] / gtk2_ardour / luainstance.h
1 /*
2  * Copyright (C) 2016-2018 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #ifndef __gtkardour_luainstance_h__
20 #define __gtkardour_luainstance_h__
21
22 #include <bitset>
23
24 #include <cairo.h>
25
26 #include "pbd/id.h"
27 #include "pbd/signals.h"
28 #include "pbd/xml++.h"
29
30 #include "ardour/luascripting.h"
31 #include "ardour/lua_script_params.h"
32 #include "ardour/luabindings.h"
33 #include "ardour/session_handle.h"
34
35 #include "lua/luastate.h"
36
37 #include "luasignal.h"
38
39 namespace luabridge {
40         class LuaRef;
41 }
42
43 typedef std::bitset<LuaSignal::LAST_SIGNAL> ActionHook;
44
45 class LuaCallback : public ARDOUR::SessionHandlePtr, public sigc::trackable
46 {
47 public:
48         LuaCallback (ARDOUR::Session*, const std::string&, const std::string&, const ActionHook&, const ARDOUR::LuaScriptParamList&);
49         LuaCallback (ARDOUR::Session*, XMLNode & node);
50         ~LuaCallback ();
51
52         XMLNode& get_state (void);
53         void set_session (ARDOUR::Session *);
54
55         const PBD::ID& id () const { return _id; }
56         const std::string& name () const { return _name; }
57         ActionHook signals () const { return _signals; }
58         bool lua_slot (std::string&, std::string&, ActionHook&, ARDOUR::LuaScriptParamList&);
59         PBD::Signal0<void> drop_callback;
60
61 protected:
62         void session_going_away ();
63
64 private:
65         LuaState lua;
66
67         PBD::ID _id;
68         std::string _name;
69         ActionHook _signals;
70
71         void reconnect ();
72         template <class T> void reconnect_object (T);
73         void init ();
74
75         luabridge::LuaRef * _lua_add;
76         luabridge::LuaRef * _lua_get;
77         luabridge::LuaRef * _lua_call;
78         luabridge::LuaRef * _lua_save;
79         luabridge::LuaRef * _lua_load;
80
81         PBD::ScopedConnectionList _connections;
82
83         template <typename T, typename S> void connect_0 (enum LuaSignal::LuaSignal, T, S*);
84         template <typename T> void proxy_0 (enum LuaSignal::LuaSignal, T);
85
86         template <typename T, typename C1> void connect_1 (enum LuaSignal::LuaSignal, T, PBD::Signal1<void, C1>*);
87         template <typename T, typename C1> void proxy_1 (enum LuaSignal::LuaSignal, T, C1);
88
89         template <typename T, typename C1, typename C2> void connect_2 (enum LuaSignal::LuaSignal, T, PBD::Signal2<void, C1, C2>*);
90         template <typename T, typename C1, typename C2> void proxy_2 (enum LuaSignal::LuaSignal, T, C1, C2);
91
92         template <typename T, typename C1, typename C2, typename C3> void connect_3 (enum LuaSignal::LuaSignal, T, PBD::Signal3<void, C1, C2, C3>*);
93         template <typename T, typename C1, typename C2, typename C3> void proxy_3 (enum LuaSignal::LuaSignal, T, C1, C2, C3);
94 };
95
96 typedef boost::shared_ptr<LuaCallback> LuaCallbackPtr;
97 typedef std::map<PBD::ID, LuaCallbackPtr> LuaCallbackMap;
98
99
100
101 class LuaInstance : public PBD::ScopedConnectionList, public ARDOUR::SessionHandlePtr
102 {
103 public:
104         static LuaInstance* instance();
105         static void destroy_instance();
106         ~LuaInstance();
107
108         static void register_classes (lua_State* L);
109         static void register_hooks (lua_State* L);
110         static void bind_cairo (lua_State* L);
111         static void bind_dialog (lua_State* L);
112
113         static void render_action_icon (cairo_t* cr, int w, int h, uint32_t c, void* i);
114
115         void set_session (ARDOUR::Session* s);
116
117         int set_state (const XMLNode&);
118         XMLNode& get_action_state (void);
119         XMLNode& get_hook_state (void);
120
121         int load_state ();
122         int save_state ();
123
124         bool interactive_add (ARDOUR::LuaScriptInfo::ScriptType, int);
125
126         /* actions */
127         void call_action (const int);
128         void render_icon (int i, cairo_t*, int, int, uint32_t);
129
130         bool set_lua_action (const int, const std::string&, const std::string&, const ARDOUR::LuaScriptParamList&);
131         bool remove_lua_action (const int);
132         bool lua_action_name (const int, std::string&);
133         std::vector<std::string> lua_action_names ();
134         bool lua_action (const int, std::string&, std::string&, ARDOUR::LuaScriptParamList&);
135         bool lua_action_has_icon (const int);
136         sigc::signal<void,int,std::string> ActionChanged;
137
138         /* callbacks */
139         bool register_lua_slot (const std::string&, const std::string&, const ARDOUR::LuaScriptParamList&);
140         bool unregister_lua_slot (const PBD::ID&);
141         std::vector<PBD::ID> lua_slots () const;
142         bool lua_slot_name (const PBD::ID&, std::string&) const;
143         std::vector<std::string> lua_slot_names () const;
144         bool lua_slot (const PBD::ID&, std::string&, std::string&, ActionHook&, ARDOUR::LuaScriptParamList&);
145         sigc::signal<void,PBD::ID,std::string,ActionHook> SlotChanged;
146
147         static PBD::Signal0<void> LuaTimerS; // deci-seconds (Timer every 1s)
148         static PBD::Signal0<void> LuaTimerDS; // deci-seconds (Timer every .1s)
149         static PBD::Signal0<void> SetSession; // emitted when a session is loaded
150
151 private:
152         LuaInstance();
153         static LuaInstance* _instance;
154
155         void init ();
156         void set_dirty ();
157         void session_going_away ();
158         void pre_seed_scripts ();
159
160         LuaState lua;
161
162         luabridge::LuaRef * _lua_call_action;
163         luabridge::LuaRef * _lua_render_icon;
164         luabridge::LuaRef * _lua_add_action;
165         luabridge::LuaRef * _lua_del_action;
166         luabridge::LuaRef * _lua_get_action;
167
168         luabridge::LuaRef * _lua_load;
169         luabridge::LuaRef * _lua_save;
170         luabridge::LuaRef * _lua_clear;
171
172         LuaCallbackMap _callbacks;
173         PBD::ScopedConnectionList _slotcon;
174
175         void every_second ();
176         sigc::connection second_connection;
177
178         void every_point_one_seconds ();
179         sigc::connection point_one_second_connection;
180 };
181
182 #endif