aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/PluginManager.cc
blob: cca1c5c0c4d52a90ef8c309c33c3ccc7151fbce8 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/** PluginManager.cc file for the fluxbox compositor. */

// Copyright (c) 2011 Gediminas Liktaras (gliktaras at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif  // HAVE_CONFIG_H

#include "PluginManager.hh"

#include "BasePlugin.hh"
#include "BaseScreen.hh"
#include "Exceptions.hh"
#include "Logging.hh"
#include "Utility.hh"

#include <algorithm>
#include <dlfcn.h>
#include <sstream>

using namespace FbCompositor;


//--- CONSTRUCTORS AND DESTRUCTORS ---------------------------------------------

// Costructor.
PluginManager::PluginManager(PluginType plugin_type, const BaseScreen &screen,
                             const FbTk::FbString user_plugin_dir) :
    m_screen(screen) {

    m_plugin_type = plugin_type;
    m_user_plugin_dir = user_plugin_dir;
}

// Destructor.
PluginManager::~PluginManager() {
    for (size_t i = 0; i < m_plugin_objects.size(); i++) {
        delete m_plugin_objects[i];
    }

    std::map<FbTk::FbString, PluginLibData>::iterator it = m_plugin_libs.begin();
    while (it != m_plugin_libs.end()) {
        unloadPlugin(it);
        ++it;
    }
}


//--- PLUGIN MANIPULATION ------------------------------------------------------

// Create a plugin object, load the appropriate library if needed.
void PluginManager::createPluginObject(FbTk::FbString name, std::vector<FbTk::FbString> args) {
    if (m_plugin_libs.find(name) == m_plugin_libs.end()) {
        loadPlugin(name);
    }

    CreatePluginFunction create_function = m_plugin_libs.find(name)->second.create_function;
    BasePlugin *new_plugin_object = (*create_function)(m_screen, args);
    m_plugin_objects.push_back(new_plugin_object);
}


//--- INTERNAL PLUGIN MANIPULATION ---------------------------------------------

// Load a plugin.
void PluginManager::loadPlugin(FbTk::FbString name) {
    static union {
        void *void_ptr;
        PluginTypeFunction plugin_type_func;
        CreatePluginFunction create_plugin_func;
    } obj_union;

    std::vector<FbTk::FbString> paths = buildPluginPaths(name);

    // Get the handle to the plugin so object.
    void *handle = NULL;
    for (size_t i = 0; i < paths.size(); i++) {
        handle = dlopen(paths[i].c_str(), RTLD_LAZY | RTLD_LOCAL);
        if (handle) {
            break;
        }
    }
    if (!handle) {
        std::stringstream ss;
        ss << "Could not find/load plugin \"" << name << "\".";
        throw PluginException(ss.str());
    }

    // Check for the correct plugin type.
    obj_union.void_ptr = getLibraryObject(handle, "pluginType", name.c_str(), "type function");
    PluginTypeFunction type_func = obj_union.plugin_type_func;

    if ((*(type_func))() != m_plugin_type) {
        std::stringstream ss;
        ss << "Plugin \"" << name << "\" is of the wrong type.";
        throw PluginException(ss.str());
    }

    // Get the plugin creation function.
    obj_union.void_ptr = getLibraryObject(handle, "createPlugin", name.c_str(), "creation function");
    CreatePluginFunction create_func = obj_union.create_plugin_func;

    // Track the loaded plugin.
    PluginLibData plugin_data = { handle, create_func };
    m_plugin_libs.insert(make_pair(name, plugin_data));
}

// Unload a plugin.
void PluginManager::unloadPlugin(FbTk::FbString name) {
    std::map<FbTk::FbString, PluginLibData>::iterator it = m_plugin_libs.find(name);

    if (it == m_plugin_libs.end()) {
        std::stringstream ss;
        ss << "Plugin \"" << name << "\" is not loaded (unloadPlugin).";
        throw PluginException(ss.str());
    } else {
        unloadPlugin(it);
    }
}

// Unload a plugin (actual worker function).
void PluginManager::unloadPlugin(std::map<FbTk::FbString, PluginLibData>::iterator it) {
    dlclose(it->second.handle);

    it->second.handle = NULL;
    it->second.create_function = NULL;
}


//--- CONVENIENCE FUNCTIONS ----------------------------------------------------

// Build a vector of search paths for a given plugin.
std::vector<FbTk::FbString> PluginManager::buildPluginPaths(const FbTk::FbString &name) {
    std::stringstream ss;
    std::vector<FbTk::FbString> paths;

    FbTk::FbString type_dir = "";
    if (m_plugin_type == Plugin_OpenGL) {
        type_dir = "opengl/";
    } else if (m_plugin_type == Plugin_XRender) {
        type_dir = "xrender/";
    }

    ss << "./plugins/" << type_dir << name << "/.libs/" << name << ".so";
    paths.push_back(ss.str());
    ss.str("");

    ss << m_user_plugin_dir << type_dir << name << ".so";
    paths.push_back(ss.str());
    ss.str("");

#ifdef FBCOMPOSE_PATH
    ss << FBCOMPOSE_PATH << "/plugins/" << type_dir << name << ".so";
    paths.push_back(ss.str());
    ss.str("");
#endif

    paths.push_back(name);

    return paths;
}

// Returns some object from the given library handle.
void *PluginManager::getLibraryObject(void *handle, const char *object_name, const char *plugin_name,
                                      const char *verbose_object_name) {
    dlerror();
    void *raw_object = dlsym(handle, object_name);
    const char *error = dlerror();

    if (error) {
        dlclose(handle);
        std::stringstream ss;
        ss << "Error in loading " << verbose_object_name << " for \"" << plugin_name << "\" plugin: " << error;
        throw PluginException(ss.str());
    } else {
        return raw_object;
    }
}