summaryrefslogtreecommitdiff
path: root/plugin.c
blob: 7835e998c9275246f413043bada19a3aa199fda7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * plugin.c
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <dlfcn.h>

#include "epgd.h"

//***************************************************************************
// Class Plugin Loader
//***************************************************************************

PluginLoader::PluginLoader(const char* name, Plugin* p)
{
   fileName = strdup(name);
   plugin = p;    // normally 0, set for testing only
   handle = 0;
}

PluginLoader::~PluginLoader()
{
  delete plugin;

  if (handle)
     dlclose(handle);

  free(fileName);
}

int PluginLoader::load()
{
   const char* error;

   tell(0, "Loading plugin: %s", fileName);

   if (handle) 
   {
      tell(0, "Warning: Attempt to load plugin '%s' twice!", fileName);
      return fail;
   }

   plugin = 0;
   handle = dlopen(fileName, RTLD_NOW);
   error = dlerror();

   if (!error) 
   {
      void *(*creator)(void);

      *(void**)(&creator) = dlsym(handle, "EPGPluginCreator");

      error = dlerror();

      if (!error)
         plugin = (Plugin*)creator();
   }

   if (error)
      tell(0, "Error: %s", error);
   
   return plugin ? success : fail;
}