libvisual  0.5.0
lv_plugin.cpp
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012-2013 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include "config.h"
25 #include "lv_plugin.h"
26 #include "lv_common.h"
27 #include "lv_libvisual.h"
28 #include "lv_util.h"
29 #include "lv_plugin_registry.h"
30 #include <cstring>
31 
32 namespace LV {
33 
34  class PluginData
35  {
36  public:
37 
38  VisPluginInfo const *info;
39 
40  EventQueue eventqueue;
41  ParamList params;
42  int plugflags;
43  RandomContext random;
44  bool realized;
45  void *priv;
46 
47  PluginData ();
48  };
49 
50  const char *plugin_get_next_by_name (PluginList const& list, const char *name)
51  {
52  for (unsigned int i = 0; i < list.size (); i++)
53  {
54  if (std::strcmp (list[i].info->plugname, name) == 0)
55  {
56  unsigned int next_i = (i + 1) % list.size ();
57  return list[next_i].info->plugname;
58  }
59  }
60 
61  return nullptr;
62  }
63 
64  const char *plugin_get_prev_by_name (PluginList const& list, const char *name)
65  {
66  for (unsigned int i = 0; i < list.size (); i++)
67  {
68  if (std::strcmp (list[i].info->plugname, name) == 0)
69  {
70  unsigned int prev_i = (i + list.size () - 1) % list.size ();
71  return list[prev_i].info->plugname;
72  }
73  }
74 
75  return nullptr;
76  }
77 
78  PluginData::PluginData ()
79  : info {nullptr}
80  , plugflags {0}
81  , random {LV::rand ()}
82  , realized {false}
83  , priv {nullptr}
84  {
85  // nothing
86  }
87 
88 } // LV namespace
89 
90 void visual_plugin_events_pump (VisPluginData *plugin)
91 {
92  visual_return_if_fail (plugin != nullptr);
93 
94  if (plugin->info->events) {
95  plugin->info->events (plugin, &plugin->eventqueue);
96  }
97 }
98 
100 {
101  visual_return_val_if_fail (plugin != nullptr, nullptr);
102 
103  return &plugin->eventqueue;
104 }
105 
106 VisPluginInfo const* visual_plugin_get_info (VisPluginData *plugin)
107 {
108  visual_return_val_if_fail (plugin != nullptr, nullptr);
109 
110  return plugin->info;
111 }
112 
113 VisParamList *visual_plugin_get_params (VisPluginData *plugin)
114 {
115  visual_return_val_if_fail (plugin != nullptr, nullptr);
116 
117  return &plugin->params;
118 }
119 
121 {
122  visual_return_val_if_fail (plugin != nullptr, nullptr);
123 
124  return &plugin->random;
125 }
126 
127 void *visual_plugin_get_specific (VisPluginData *plugin)
128 {
129  visual_return_val_if_fail (plugin != nullptr, nullptr);
130 
131  auto pluginfo = visual_plugin_get_info (plugin);
132  visual_return_val_if_fail (pluginfo != nullptr, nullptr);
133 
134  return pluginfo->plugin;
135 }
136 
137 static VisPluginData *visual_plugin_new ()
138 {
139  return new VisPluginData;
140 }
141 
142 static void visual_plugin_free (VisPluginData *plugin)
143 {
144  delete plugin;
145 }
146 
147 void visual_plugin_unload (VisPluginData *plugin)
148 {
149  visual_return_if_fail (plugin != nullptr);
150 
151  if (plugin->realized) {
152  plugin->info->cleanup (plugin);
153  }
154 
155  visual_plugin_free (plugin);
156 }
157 
158 VisPluginData *visual_plugin_load (VisPluginType type, const char *name)
159 {
160  // FIXME: Check if plugin has already been loaded
161 
162  auto info = LV::PluginRegistry::instance()->get_plugin_info (type, name);
163  if (!info) {
164  return nullptr;
165  }
166 
167  auto plugin = visual_plugin_new ();
168  plugin->info = info;
169 
170  return plugin;
171 }
172 
173 int visual_plugin_realize (VisPluginData *plugin)
174 {
175  visual_return_val_if_fail (plugin != nullptr, FALSE);
176 
177  if (plugin->realized) {
178  return TRUE;
179  }
180 
181  visual_log (VISUAL_LOG_DEBUG, "Activating plugin '%s'", plugin->info->plugname);
182 
183  auto params = visual_plugin_get_params (plugin);
184  params->set_event_queue (plugin->eventqueue);
185 
186  if (!plugin->info->init (plugin)) {
187  visual_log (VISUAL_LOG_ERROR, "Failed to initialise plugin");
188  return FALSE;
189  }
190 
191  plugin->realized = TRUE;
192 
193  return TRUE;
194 }
195 
196 int visual_plugin_is_realized (VisPluginData *plugin)
197 {
198  visual_return_val_if_fail (plugin != nullptr, FALSE);
199 
200  return plugin->realized;
201 }
202 
203 void visual_plugin_set_private (VisPluginData *plugin, void *priv)
204 {
205  visual_return_if_fail (plugin != nullptr);
206 
207  plugin->priv = priv;
208 }
209 
210 void *visual_plugin_get_private (VisPluginData *plugin)
211 {
212  visual_return_val_if_fail (plugin != nullptr, nullptr);
213 
214  return plugin->priv;
215 }
216 
218 {
220 }