aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/PluginManager.hh
blob: 904f9ad03017ee6323ae4bd11c5e9cc71e2d4d74 (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
/** PluginManager.hh 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.


#ifndef FBCOMPOSITOR_PLUGINMANAGER_HH
#define FBCOMPOSITOR_PLUGINMANAGER_HH

#include "Enumerations.hh"

#include "FbTk/FbString.hh"

#include <map>
#include <vector>


namespace FbCompositor {

    class BasePlugin;
    class BaseScreen;


    //--- TYPEDEFS -------------------------------------------------------------

    /** A pointer to a function that creates a plugin class instance. */
    typedef BasePlugin* (*CreatePluginFunction)(const BaseScreen&, const std::vector<FbTk::FbString>&);

    /** A pointer to a function that returns the rendering mode the plugin operates in. */
    typedef PluginType (*PluginTypeFunction)();


    /**
     * Responsible for plugin loading, unloading and availibility.
     */
    class PluginManager {
        struct PluginLibData;

    public :
        //--- CONSTRUCTORS AND DESTRUCTORS -------------------------------------

        /** Constructor. */
        PluginManager(PluginType plugin_type, const BaseScreen &screen, const FbTk::FbString user_plugin_dir);

        /** Destructor. */
        ~PluginManager();


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

        /** Create a plugin object, load the appropriate library if needed. */
        void createPluginObject(FbTk::FbString name, std::vector<FbTk::FbString> args = std::vector<FbTk::FbString>());

        /** \returns a reference to a vector with plugin objects. */
        std::vector<BasePlugin*> &plugins();

        /** \returns a reference to a vector with plugin objects (const version). */
        const std::vector<BasePlugin*> &plugins() const;


    private :
        //--- CONSTRUCTORS -----------------------------------------------------

        /** Copy constructor. */
        PluginManager(const PluginManager&);

        /** Assignment operator. */
        PluginManager &operator=(const PluginManager&);


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

        /** Load a plugin. */
        void loadPlugin(FbTk::FbString name);

        /** Unload a plugin. */
        void unloadPlugin(FbTk::FbString name);

        /** Unload a plugin (actual worker function). */
        void unloadPlugin(std::map<FbTk::FbString, PluginLibData>::iterator it);


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

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

        /** \returns some object from the given library handle. */
        void *getLibraryObject(void *handle, const char *object_name, const char *plugin_name,
                               const char *verbose_object_name);


        //--- PLUGINS AND METADATA ---------------------------------------------

        /** Specific plugin-related data. */
        struct PluginLibData {
            void *handle;                           ///< Handle to the loaded library.
            CreatePluginFunction create_function;   ///< Plugin creation function.
        };

        /** A map, containing all loaded plugins. */
        std::map<FbTk::FbString, PluginLibData> m_plugin_libs;

        /** A vector with active plugin objects. */
        std::vector<BasePlugin*> m_plugin_objects;

        /** Type of the plugins this object manages. */
        PluginType m_plugin_type;

        /** The screen this manager operates on. */
        const BaseScreen &m_screen;

        /** User plugin directory. */
        FbTk::FbString m_user_plugin_dir;
    };


    //--- INLINE FUNCTIONS -----------------------------------------------------

    // Returns a reference to a vector with plugin objects.
    inline std::vector<BasePlugin*> &PluginManager::plugins() {
        return m_plugin_objects;
    }

    // Returns a reference to a vector with plugin objects (const version).
    inline const std::vector<BasePlugin*> &PluginManager::plugins() const {
        return m_plugin_objects;
    }
}

#endif  // FBCOMPOSITOR_PLUGINMANAGER_HH