~ubuntu-branches/ubuntu/karmic/gmsh/karmic

« back to all changes in this revision

Viewing changes to Plugin/PluginManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme, Christophe Prud'homme, Daniel Leidert
  • Date: 2008-05-18 12:46:05 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20080518124605-716xqbqeo07o497k
Tags: 2.2.0-2
[Christophe Prud'homme]
* Bug fix: "gmsh ships no .desktop", thanks to Vassilis Pandis (Closes:
  #375770). Applied the Ubuntu patch.

[Daniel Leidert]
* debian/control (Vcs-Svn): Fixed.
  (Build-Depends): Use texlive instead of tetex-bin.
* debian/gmsh.doc-base (Section): Fixed accordingly to doc-base (>= 0.8.10).
* debian/rules: Removed some variable declarations, that lead to double
  configuration and seem to be useless.
  (build/gmsh): Try to avoid multiple runs by using a stamp.
  (orig-tarball): Renamed to get-orig-source and changed to use uscan.
* debian/watch: Added.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id: PluginManager.cpp,v 1.7 2008-03-20 11:44:14 geuzaine Exp $
 
2
//
 
3
// Copyright (C) 1997-2008 C. Geuzaine, J.-F. Remacle
 
4
//
 
5
// This program is free software; you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation; either version 2 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program; if not, write to the Free Software
 
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
18
// USA.
 
19
// 
 
20
// Please report all bugs and problems to <gmsh@geuz.org>.
 
21
 
 
22
#if !defined(HAVE_NO_DLL)
 
23
#include <dlfcn.h>
 
24
#endif
 
25
 
 
26
#include <map>
 
27
 
 
28
#if defined(HAVE_FLTK)
 
29
#include <FL/Fl.H>
 
30
#include <FL/filename.H>
 
31
#endif
 
32
 
 
33
#include "Plugin.h"
 
34
#include "PluginManager.h"
 
35
#include "CutMap.h"
 
36
#include "CutGrid.h"
 
37
#include "StreamLines.h"
 
38
#include "CutPlane.h"
 
39
#include "CutParametric.h"
 
40
#include "CutSphere.h"
 
41
#include "Skin.h"
 
42
#include "Extract.h"
 
43
#include "ExtractElements.h"
 
44
#include "ExtractEdges.h"
 
45
#include "HarmonicToTime.h"
 
46
#include "ModulusPhase.h"
 
47
#include "Integrate.h"
 
48
#include "Gradient.h"
 
49
#include "Curl.h"
 
50
#include "Divergence.h"
 
51
#include "Annotate.h"
 
52
#include "Remove.h"
 
53
#include "MakeSimplex.h"
 
54
#include "Smooth.h"
 
55
#include "Transform.h"
 
56
#include "TransformLatLon.h"
 
57
#include "Triangulate.h"
 
58
#include "Warp.h"
 
59
#include "SphericalRaise.h"
 
60
#include "Eigenvectors.h"
 
61
#include "Eigenvalues.h"
 
62
#include "Lambda2.h"
 
63
#include "Evaluate.h"
 
64
#include "Probe.h"
 
65
#include "FieldView.h"
 
66
#include "GSHHS.h"
 
67
#include "Context.h"
 
68
 
 
69
extern Context_T CTX;
 
70
 
 
71
const char *GMSH_PluginEntry = "GMSH_RegisterPlugin";
 
72
 
 
73
GMSH_PluginManager *GMSH_PluginManager::_instance = 0;
 
74
 
 
75
GMSH_PluginManager::GMSH_PluginManager()
 
76
{
 
77
}
 
78
 
 
79
GMSH_PluginManager::~GMSH_PluginManager()
 
80
{
 
81
  for(iter it = allPlugins.begin(); it != allPlugins.end(); ++it)
 
82
    delete(*it).second;
 
83
}
 
84
 
 
85
GMSH_Plugin *GMSH_PluginManager::find(char *pluginName)
 
86
{
 
87
  iter it = allPlugins.find(pluginName);
 
88
  if(it == allPlugins.end())
 
89
    return 0;
 
90
  return (*it).second;
 
91
}
 
92
 
 
93
GMSH_Solve_Plugin *GMSH_PluginManager::findSolverPlugin()
 
94
{
 
95
  iter it  = allPlugins.begin();
 
96
  iter ite = allPlugins.end();
 
97
  for (; it != ite; ++it) {
 
98
    GMSH_Plugin *p = (*it).second;
 
99
    if(p->getType() == GMSH_Plugin::GMSH_SOLVE_PLUGIN) {
 
100
      return (GMSH_Solve_Plugin*)(p);
 
101
    }      
 
102
  }
 
103
  return 0;
 
104
}
 
105
 
 
106
void GMSH_PluginManager::action(char *pluginName, char *action, void *data)
 
107
{
 
108
  GMSH_Plugin *plugin = find(pluginName);
 
109
  if(!plugin)
 
110
    throw "Unknown plugin name";
 
111
 
 
112
  if(!strcmp(action, "Run"))
 
113
    plugin->run();
 
114
  else
 
115
    throw "Unknown plugin action";
 
116
}
 
117
 
 
118
void GMSH_PluginManager::setPluginOption(char *pluginName, char *option,
 
119
                                         char *value)
 
120
{
 
121
  GMSH_Plugin *plugin = find(pluginName);
 
122
 
 
123
  if(!plugin)
 
124
    throw "Unknown plugin name";
 
125
 
 
126
  for(int i = 0; i < plugin->getNbOptionsStr(); i++) {
 
127
    StringXString *sxs;
 
128
    // get the ith option of the plugin
 
129
    sxs = plugin->getOptionStr(i);
 
130
    // look if it's the good option name
 
131
    if(!strcmp(sxs->str, option)) {
 
132
      sxs->def = value;
 
133
      return;
 
134
    }
 
135
  }
 
136
 
 
137
  throw "Unknown plugin option name";
 
138
}
 
139
 
 
140
void GMSH_PluginManager::setPluginOption(char *pluginName, char *option,
 
141
                                         double value)
 
142
{
 
143
  GMSH_Plugin *plugin = find(pluginName);
 
144
 
 
145
  if(!plugin)
 
146
    throw "Unknown plugin name";
 
147
 
 
148
  for(int i = 0; i < plugin->getNbOptions(); i++) {
 
149
    StringXNumber *sxn;
 
150
    // get the ith option of the plugin
 
151
    sxn = plugin->getOption(i);
 
152
    // look if it's the good option name
 
153
    if(!strcmp(sxn->str, option)) {
 
154
      sxn->def = value;
 
155
      return;
 
156
    }
 
157
  }
 
158
  throw "Unknown plugin option name";
 
159
}
 
160
 
 
161
GMSH_PluginManager *GMSH_PluginManager::instance()
 
162
{
 
163
  if(!_instance) {
 
164
    _instance = new GMSH_PluginManager;
 
165
  }
 
166
  return _instance;
 
167
}
 
168
 
 
169
void GMSH_PluginManager::registerDefaultPlugins()
 
170
{
 
171
  if(CTX.solver.plugins){
 
172
    // nothing here yet
 
173
  }
 
174
 
 
175
  if(CTX.post.plugins){
 
176
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
177
                      ("StreamLines", GMSH_RegisterStreamLinesPlugin()));
 
178
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
179
                      ("CutGrid", GMSH_RegisterCutGridPlugin()));
 
180
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
181
                      ("CutMap", GMSH_RegisterCutMapPlugin()));
 
182
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
183
                      ("CutPlane", GMSH_RegisterCutPlanePlugin()));
 
184
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
185
                      ("CutSphere", GMSH_RegisterCutSpherePlugin()));
 
186
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
187
                      ("Skin", GMSH_RegisterSkinPlugin()));
 
188
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
189
                      ("Extract", GMSH_RegisterExtractPlugin()));
 
190
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
191
                      ("ExtractElements", GMSH_RegisterExtractElementsPlugin()));
 
192
#if 0 // waiting for BDS rewrite
 
193
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
194
                      ("ExtractEdges", GMSH_RegisterExtractEdgesPlugin()));
 
195
#endif
 
196
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
197
                      ("MakeSimplex", GMSH_RegisterMakeSimplexPlugin()));
 
198
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
199
                      ("Smooth", GMSH_RegisterSmoothPlugin()));
 
200
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
201
                      ("Transform", GMSH_RegisterTransformPlugin()));
 
202
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
203
                      ("TransformLatLon", GMSH_RegisterTransformLatLonPlugin()));
 
204
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
205
                      ("Warp", GMSH_RegisterWarpPlugin()));
 
206
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
207
                      ("SphericalRaise", GMSH_RegisterSphericalRaisePlugin()));
 
208
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
209
                      ("HarmonicToTime", GMSH_RegisterHarmonicToTimePlugin()));
 
210
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
211
                      ("ModulusPhase", GMSH_RegisterModulusPhasePlugin()));
 
212
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
213
                      ("Integrate", GMSH_RegisterIntegratePlugin()));
 
214
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
215
                      ("Gradient", GMSH_RegisterGradientPlugin()));
 
216
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
217
                      ("Curl", GMSH_RegisterCurlPlugin()));
 
218
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
219
                      ("Divergence", GMSH_RegisterDivergencePlugin()));
 
220
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
221
                      ("Annotate", GMSH_RegisterAnnotatePlugin()));
 
222
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
223
                      ("Remove", GMSH_RegisterRemovePlugin()));
 
224
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
225
                      ("Eigenvectors", GMSH_RegisterEigenvectorsPlugin()));
 
226
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
227
                      ("Eigenvalues", GMSH_RegisterEigenvaluesPlugin()));
 
228
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
229
                      ("Lambda2", GMSH_RegisterLambda2Plugin()));
 
230
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
231
                      ("Probe", GMSH_RegisterProbePlugin()));
 
232
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
233
                      ("FieldView", GMSH_RegisterFieldViewPlugin()));
 
234
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
235
                      ("Triangulate", GMSH_RegisterTriangulatePlugin()));
 
236
#if defined(HAVE_MATH_EVAL)
 
237
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
238
                      ("Evaluate", GMSH_RegisterEvaluatePlugin()));
 
239
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
240
                      ("CutParametric", GMSH_RegisterCutParametricPlugin()));
 
241
    allPlugins.insert(std::pair<const char*, GMSH_Plugin*>
 
242
                      ("GSHHS", GMSH_RegisterGSHHSPlugin()));
 
243
#endif
 
244
  }
 
245
 
 
246
#if defined(HAVE_FLTK)
 
247
  char *homeplugins = getenv("GMSHPLUGINSHOME");
 
248
  if(!homeplugins) return;
 
249
  struct dirent **list;
 
250
  int nbFiles = fl_filename_list(homeplugins, &list);
 
251
  if(nbFiles <= 0)
 
252
    return;
 
253
  for(int i = 0; i < nbFiles; i++) {
 
254
    char *name = list[i]->d_name;
 
255
    if(strlen(name) > 3) {
 
256
      char ext[6];
 
257
      strcpy(ext, name + (strlen(name) - 3));
 
258
      if(!strcmp(ext, ".so") || !strcmp(ext, "dll")) {
 
259
        addPlugin(homeplugins, name);
 
260
      }
 
261
    }
 
262
  }
 
263
  for(int i = 0; i < nbFiles; i++)
 
264
    free(list[i]);
 
265
  free(list);
 
266
#endif
 
267
}
 
268
 
 
269
void GMSH_PluginManager::addPlugin(char *dirName, char *pluginName)
 
270
{
 
271
#if defined(HAVE_NO_DLL) || !defined(HAVE_FLTK)
 
272
  Msg(WARNING, "No dynamic plugin loading on this platform");
 
273
#else
 
274
  char dynamic_lib[1024];
 
275
  char plugin_name[256];
 
276
  char plugin_author[256];
 
277
  char plugin_copyright[256];
 
278
  char plugin_help[1024];
 
279
  class GMSH_Plugin *(*registerPlugin) (void);
 
280
  sprintf(dynamic_lib, "%s/%s", dirName, pluginName);
 
281
  Msg(INFO, "Opening Plugin '%s'", dynamic_lib);
 
282
  void *hlib = dlopen(dynamic_lib, RTLD_NOW);
 
283
  const char *err = dlerror();
 
284
  if(!hlib){
 
285
    Msg(WARNING, "Error in opening %s (dlerror = %s)", dynamic_lib, err);
 
286
    return;
 
287
  }
 
288
  registerPlugin = (class GMSH_Plugin * (*)(void))dlsym(hlib, GMSH_PluginEntry);
 
289
  err = dlerror();
 
290
  if(err){
 
291
    Msg(WARNING, "Symbol '%s' missing in plugin '%s' (dlerror = %s)",
 
292
        GMSH_PluginEntry, pluginName, err);
 
293
    return;
 
294
  }
 
295
 
 
296
  GMSH_Plugin *p = registerPlugin();
 
297
  p->hlib = hlib;
 
298
  p->getName(plugin_name);
 
299
  p->getInfos(plugin_author, plugin_copyright, plugin_help);
 
300
  if(allPlugins.find(plugin_name) != allPlugins.end()) {
 
301
    Msg(WARNING, "Plugin '%s' multiply defined", pluginName);
 
302
    return;
 
303
  }
 
304
  allPlugins.insert(std::pair<const char*, GMSH_Plugin*>(plugin_name, p));
 
305
  Msg(INFO, "Loaded Plugin '%s' (%s)", plugin_name, plugin_author);
 
306
#endif
 
307
}