A line of communication is now created between the monitor and visualizer.
[aesalon.git] / visualizer / src / module / Module.cpp
blobb985a6391d5eff5085ddf08f67c2d5773baceb5c
1 #include <sys/stat.h>
2 #include <dlfcn.h>
4 #include <QSettings>
6 #include "Module.h"
8 Module::Module(const char *name) : m_name(name), m_moduleHandle(NULL){
9 QString path = modulePath(QString("lib").append(name).append("Visualizer.so"));
10 if(path.length() == 0) return;
11 m_moduleHandle = dlopen(path.toAscii().constData(), RTLD_LAZY | RTLD_LOCAL);
12 if(m_moduleHandle == NULL) {
13 qWarning("Cannot load module \"%s\". Error: %s", name, dlerror());
14 qWarning("Last path is %s.", path.toAscii().constData());
15 return;
18 void *instantiationHandle = dlsym(m_moduleHandle, "AesalonVisualizerCreateInstance");
20 if(instantiationHandle == NULL) {
21 qWarning("Module \"%s\" does not have instantiation function.", name);
22 return;
25 ModuleInterface *(*instantiateFunction)();
26 *(void **)(&instantiateFunction) = instantiationHandle;
28 m_interface = instantiateFunction();
30 if(m_interface == NULL) {
31 qWarning("Module \"%s\" failed to create ModuleInterface instance.", name);
32 return;
36 Module::~Module() {
40 void Module::processIncoming(DataPacket *packet) {
41 if(m_interface == NULL) return;
42 m_interface->processIncoming(packet);
45 QString Module::modulePath(QString filename) {
46 QSettings settings;
47 QString pathList = settings.value("module-path", "modules/build/").toString();
48 if(filename.length() == 0 || pathList.length() == 0) return "";
50 do {
51 QString path = pathList.left(pathList.indexOf(":"));
53 QString filePath = path;
54 filePath += "/";
55 filePath += filename;
57 struct stat possibleStat;
58 if(stat(filePath.toAscii().constData(), &possibleStat) == 0) return filePath;
60 pathList.remove(0, path.length()+1);
61 } while(pathList.contains(":"));
63 return "";