1 /* init.cpp
2 
3    Computer Music Toolkit - a library of LADSPA plugins. Copyright (C)
4    2000 Richard W.E. Furse. The author may be contacted at
5    richard@muse.demon.co.uk.
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public Licence as
9    published by the Free Software Foundation; either version 2 of the
10    Licence, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this library; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20    02111-1307, USA. */
21 
22 /*****************************************************************************/
23 
24 #include <ladspa.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 /*****************************************************************************/
29 
30 #include "cmt.h"
31 
32 /*****************************************************************************/
33 
34 void initialise_modules();
35 void finalise_modules();
36 
37 /*****************************************************************************/
38 
39 int
pluginNameComparator(const void * pvDescriptor1,const void * pvDescriptor2)40 pluginNameComparator(const void * pvDescriptor1, const void * pvDescriptor2) {
41 
42   const CMT_Descriptor * psDescriptor1
43     = *(const CMT_Descriptor **)pvDescriptor1;
44   const CMT_Descriptor * psDescriptor2
45     = *(const CMT_Descriptor **)pvDescriptor2;
46 
47   int iResult = strcmp(psDescriptor1->Name, psDescriptor2->Name);
48   if (iResult < 0)
49     return -1;
50   else if (iResult > 0)
51     return 1;
52   else
53     return 0;
54 }
55 
56 /*****************************************************************************/
57 
58 CMT_Descriptor ** g_ppsRegisteredDescriptors = NULL;
59 unsigned long g_lPluginCapacity = 0;
60 unsigned long g_lPluginCount = 0;
61 
62 /*****************************************************************************/
63 
64 #define CAPACITY_STEP 20
65 
66 void
registerNewPluginDescriptor(CMT_Descriptor * psDescriptor)67 registerNewPluginDescriptor(CMT_Descriptor * psDescriptor) {
68   if (g_lPluginCapacity == g_lPluginCount) {
69     /* Full. Enlarge capacity. */
70     CMT_Descriptor ** ppsOldDescriptors
71       = g_ppsRegisteredDescriptors;
72     g_ppsRegisteredDescriptors
73       = new CMT_Descriptor_ptr[g_lPluginCapacity + CAPACITY_STEP];
74     if (g_lPluginCapacity > 0) {
75       memcpy(g_ppsRegisteredDescriptors,
76 	     ppsOldDescriptors,
77 	     g_lPluginCapacity * sizeof(CMT_Descriptor_ptr));
78       delete [] ppsOldDescriptors;
79     }
80     g_lPluginCapacity += CAPACITY_STEP;
81   }
82   g_ppsRegisteredDescriptors[g_lPluginCount++] = psDescriptor;
83 }
84 
85 /*****************************************************************************/
86 
87 /** A global object of this class is used to perform initialisation
88     and shutdown services for the entire library. The constructor is
89     run when the library is loaded and the destructor when it is
90     unloaded. */
91 class StartupShutdownHandler {
92 public:
93 
StartupShutdownHandler()94   StartupShutdownHandler() {
95     initialise_modules();
96     qsort(g_ppsRegisteredDescriptors,
97 	  g_lPluginCount,
98 	  sizeof(CMT_Descriptor_ptr),
99 	  pluginNameComparator);
100   }
101 
~StartupShutdownHandler()102   ~StartupShutdownHandler() {
103     if (g_ppsRegisteredDescriptors != NULL) {
104       for (unsigned long lIndex = 0; lIndex < g_lPluginCount; lIndex++)
105 	delete g_ppsRegisteredDescriptors[lIndex];
106       delete [] g_ppsRegisteredDescriptors;
107     }
108     finalise_modules();
109   }
110 
111 } ;
112 
113 /*****************************************************************************/
114 
115 extern "C"
116 {
117 
118 const LADSPA_Descriptor *
ladspa_descriptor(unsigned long Index)119 ladspa_descriptor(unsigned long Index) {
120 
121 	static StartupShutdownHandler handler;
122 
123   if (Index < g_lPluginCount)
124     return g_ppsRegisteredDescriptors[Index];
125   else
126     return NULL;
127 }
128 
129 };
130 
131 /*****************************************************************************/
132 
133 /* EOF */
134