aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/CompositorConfig.cc
blob: 67b29bed9a10a2b81ce7c666c1d35e9a5d65ecad (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/** CompositorConfig.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.


#include "CompositorConfig.hh"

#include "Constants.hh"
#include "Logging.hh"

#ifdef USE_XRENDER_COMPOSITING
#include <X11/extensions/Xrender.h>
#endif  // USE_XRENDER_COMPOSITING

#include <algorithm>
#include <iostream>
#include <sstream>

#include <cstdlib>

using namespace FbCompositor;


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

// Constructor.
CompositorConfig::CompositorConfig(std::vector<FbTk::FbString> args) :
    m_args(args),

#ifdef USE_OPENGL_COMPOSITING
    m_rendering_mode(RM_OpenGL),
#else
#ifdef USE_XRENDER_COMPOSITING
    m_rendering_mode(RM_XRender),
#else
    m_rendering_mode(RM_ServerAuto),
#endif  // USE_XRENDER_COMPOSITING
#endif  // USE_OPENGL_COMPOSITING

#ifdef USE_XRENDER_COMPOSITING
    m_xrender_pict_filter(FilterFast),
#endif  // USE_XRENDER_COMPOSITING

    m_display_name(""),
    m_frames_per_second(60),
    m_plugins(),
    m_show_x_errors(true),
    m_synchronize(false),
    m_user_plugin_dir("~/.fluxbox/fbcompose/plugins") {

    preScanArguments();
    processArguments();
}

// Copy constructor.
CompositorConfig::CompositorConfig(const CompositorConfig &other) :
    m_args(other.m_args),

    m_rendering_mode(other.m_rendering_mode),
#ifdef USE_XRENDER_COMPOSITING
    m_xrender_pict_filter(other.m_xrender_pict_filter),
#endif  // USE_XRENDER_COMPOSITING

    m_display_name(other.m_display_name),
    m_frames_per_second(other.m_frames_per_second),
    m_plugins(other.m_plugins),
    m_show_x_errors(other.m_show_x_errors),
    m_synchronize(other.m_synchronize),
    m_user_plugin_dir(other.m_user_plugin_dir) {
}

// Assignment operator.
CompositorConfig &CompositorConfig::operator=(const CompositorConfig &other) {
    if (this != &other) {
        m_args = other.m_args;

        m_rendering_mode = other.m_rendering_mode;
#ifdef USE_XRENDER_COMPOSITING
        m_xrender_pict_filter = other.m_xrender_pict_filter;
#endif  // USE_XRENDER_COMPOSITING

        m_display_name = other.m_display_name;
        m_frames_per_second = other.m_frames_per_second;
        m_plugins = other.m_plugins;
        m_show_x_errors = other.m_show_x_errors;
        m_synchronize = other.m_synchronize;
        m_user_plugin_dir = other.m_user_plugin_dir;
    }
    return *this;
}

// Destructor.
CompositorConfig::~CompositorConfig() { }


//--- INTERNAL FUNCTIONS -------------------------------------------------------

// Make the first scan of the arguments for special options.
void CompositorConfig::preScanArguments() {
    std::vector<FbTk::FbString>::iterator it = m_args.begin();

    while (it != m_args.end()) {
        if ((*it == "-h") || (*it == "--help")) {
            printFullHelp(std::cout);
            exit(EXIT_SUCCESS);
        } else if ((*it == "-V") || (*it == "--version")) {
            printVersion(std::cout);
            exit(EXIT_SUCCESS);
        }
        ++it;
    }
}

// Properly scan the command line arguments.
void CompositorConfig::processArguments() {
    std::vector<FbTk::FbString>::iterator it = m_args.begin();
    std::stringstream ss;

    bool be_quiet = false;
    int logging_level = 0;

    while (it != m_args.end()) {
        if ((*it == "-d") || (*it == "--display")) {
            m_display_name = getNextOption(it, "No display string specified.");

        } else if ((*it == "-m") || (*it == "--mode")) {
            FbTk::FbString mode = getNextOption(it, "No rendering mode specified.");

#ifdef USE_OPENGL_COMPOSITING
            if (mode == "opengl") {
                m_rendering_mode = RM_OpenGL;
            } else
#endif  // USE_OPENGL_COMPOSITING
                
#ifdef USE_XRENDER_COMPOSITING
            if (mode == "xrender") {
                m_rendering_mode = RM_XRender;
            } else
#endif  // USE_XRENDER_COMPOSITING

            if (mode == "serverauto") {
                m_rendering_mode = RM_ServerAuto;
            } else {
                ss.str("");
                ss << "Unknown rendering mode \"" << mode << "\".";
                throw ConfigException(ss.str());
            }

        } else if (*it == "--no-x-errors") {
            m_show_x_errors = false;

        } else if ((*it == "-p") || (*it == "--plugin")) {
            FbTk::FbString plugin_name = getNextOption(it, "No plugin name specified.");
            m_plugins.push_back(make_pair(plugin_name, std::vector<FbTk::FbString>()));

        } else if ((*it == "-q") || (*it == "--quiet")) {
            be_quiet = true;

        } else if ((*it == "-r") || (*it == "--refresh-rate")) {
            ss.str(getNextOption(it, "No refresh rate specified."));
            ss >> m_frames_per_second;

            if (m_frames_per_second <= 0) {
                ss.str("");
                ss << "Invalid refresh rate given.";
                throw ConfigException(ss.str());
            }

        } else if (*it == "--sync") {
            m_synchronize = true;

        } else if ((*it == "-v") || (*it == "--verbose")) {
            logging_level += 1;

        } else if (*it == "-vv") {
            logging_level += 2;

        } else if (*it == "-vvv") {
            logging_level += 3;

        } else {
            ss.str("");
            ss << "Unknown option \"" << *it << "\".";
            throw ConfigException(ss.str());
        }
        ++it;
    }

    if (be_quiet) {
        Logger::setLoggingLevel(LOG_LEVEL_NONE);
    } else {
        if (logging_level == 0) {
            Logger::setLoggingLevel(LOG_LEVEL_WARN);
        } else if (logging_level == 1) {
            Logger::setLoggingLevel(LOG_LEVEL_INFO);
        } else if (logging_level == 2) {
            Logger::setLoggingLevel(LOG_LEVEL_DEBUG);
        } else if (logging_level >= 3) {
            Logger::setLoggingLevel(LOG_LEVEL_DEBUG_DUMP);
        }
    }
}


// Fetch the value of the next command line argument, advance iterator.
FbTk::FbString CompositorConfig::getNextOption(std::vector<FbTk::FbString>::iterator &it, const char *error_message) {
    ++it;
    if (it == m_args.end()) {
        throw ConfigException(error_message);
    }
    return *it;
}


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

// Output full help message.
void CompositorConfig::printFullHelp(std::ostream &os) {
    static const char modes[] = 
#ifdef USE_OPENGL_COMPOSITING
        "opengl, "
#endif  // USE_OPENGL_COMPOSITING
#ifdef USE_XRENDER_COMPOSITING
        "xrender, "
#endif  // USE_XRENDER_COMPOSITING
        "serverauto";

    // 80 character reference (final column = 92)
    //     ................................................................................
    os << "Usage: fbcompose [OPTION] ..." << std::endl
       << "Options and arguments:" << std::endl
       << "  -d <display>, --display <display>" << std::endl
       << "                    Use the specified display connection." << std::endl
       << "  -h, --help        Print this text and exit." << std::endl
       << "  -m <mode>, --mode <mode>" << std::endl
       << "                    Select the rendering mode." << std::endl
       << "                    <mode> can be " << modes << "." << std::endl
       << "  --no-x-errors     Do not print X errors." << std::endl
       << "  -p <plugin>, --plugin <plugin>" << std::endl
       << "                    Load a specified plugin. Run fbcompose-list_plugins script" << std::endl
       << "                    to see all available plugins." << std::endl
       << "  -q, --quiet       Do not print anything." << std::endl
       << "  -r <rate>, --refresh-rate <rate>" << std::endl
       << "                    Specify the compositor's refresh rate in Hz." << std::endl
       << "  --sync            Synchronize with the X server (useful for debugging)." << std::endl
       << "  -v, --verbose     Print more information. Pass several times for more output." << std::endl
       << "  -V, --version     Print version and exit." << std::endl;
}

// Output short help message.
void CompositorConfig::printShortHelp(std::ostream &os) {
    os << "Usage: fbcompose [OPTION]..." << std::endl
       << "Try `fbcompose --help` for more information." << std::endl;
}

// Output version information.
void CompositorConfig::printVersion(std::ostream &os) {
    os << "Fluxbox compositor " << APP_VERSION << std::endl
       << "Copyright (c) 2011 Gediminas Liktaras" << std::endl;
}