summaryrefslogtreecommitdiff
path: root/src/imaging/TrackerConfig.cpp
blob: be4c7580a0c6e6a2bca3834b2a9ec40fc0fb93be (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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de

#include "TrackerConfig.h"
#include "trackerconfigdtd.h"
#include "DeDistort.h"

#include "../base/XMLHelper.h"
#include "../base/Logger.h"
#include "../base/FileHelper.h"
#include "../base/Exception.h"
#include "../base/StringHelper.h"
#include "../base/ConfigMgr.h"

#include <libxml/parser.h>
#include <libxml/xmlwriter.h>
#include <libxml/xmlstring.h>

#include <cstring>
#include <sstream>
#include <iostream>

using namespace std;

namespace avg {

TrackerConfig::TrackerConfig()
    : m_Doc(0)
{
} 

TrackerConfig::TrackerConfig(const TrackerConfig& other)
{
    m_Doc = 0;
    if (other.m_Doc) {
        m_Doc = xmlCopyDoc(other.m_Doc, true);
        m_sFilename = other.m_sFilename;
        m_pRoot = xmlDocGetRootElement(m_Doc);
    }
}

TrackerConfig::~TrackerConfig()
{
    xmlFreeDoc(m_Doc);
}

void TrackerConfig::loadConfigFile(const string& sFilename)
{
    // TODO: There is duplicated code here and in Player::loadFile which belongs
    // in a lower-level xml handling class.
    registerDTDEntityLoader("trackerconfig.dtd", g_pTrackerConfigDTD);
    xmlDtdPtr dtd;
    string sDTDFName = "trackerconfig.dtd";
    dtd = xmlParseDTD(NULL, (const xmlChar*) sDTDFName.c_str());
    if (!dtd) {
        AVG_LOG_WARNING("DTD not found at " << sDTDFName 
                << ". Not validating trackerconfig files.");
    }

    // xmlParseFile crashes for some reason under Lion.
    string sFileContents;
    readWholeFile(sFilename, sFileContents);
    m_Doc = xmlParseMemory(sFileContents.c_str(), sFileContents.length());
    if (!m_Doc) {
        AVG_LOG_ERROR("Could not open tracker config file " << sFilename <<
                ". Using defaults which will probably not work.");
        return;
    }

    xmlValidCtxtPtr cvp = xmlNewValidCtxt();
    cvp->error = xmlParserValidityError;
    cvp->warning = xmlParserValidityWarning;
    int isValid = xmlValidateDtd(cvp, m_Doc, dtd);  
    xmlFreeValidCtxt(cvp);
    if (!isValid) {
        throw (Exception(AVG_ERR_XML_PARSE, sFilename + " does not validate."));
    }

    m_pRoot = xmlDocGetRootElement(m_Doc);
    xmlFreeDtd(dtd);
    m_sFilename = sFilename;

    AVG_TRACE(Logger::category::CONFIG, Logger::severity::INFO,
            "Reading Tracker config file from " << sFilename);
}

void TrackerConfig::load()
{
    // Give precedence to local configuration
    string sFName = "avgtrackerrc";
    if (fileExists(sFName) || !fileExists(getGlobalConfigDir() + sFName)) {
        loadConfigFile(sFName);
    } else {
        loadConfigFile(getGlobalConfigDir() + sFName);
    }
}

xmlXPathObjectPtr TrackerConfig::findConfigNodes(const string& sXPathExpr) const
{
    string sFullPath = string("/trackerconfig"+sXPathExpr);
    xmlXPathContextPtr xpCtx;
    xmlXPathObjectPtr xpElement;

    xpCtx = xmlXPathNewContext(m_Doc);
    if(!xpCtx) {
        AVG_LOG_ERROR("Unable to create new XPath context");
        return NULL;
    }

    xpElement = xmlXPathEvalExpression(BAD_CAST sFullPath.c_str(), xpCtx);
    if(!xpElement) {
        AVG_LOG_ERROR("Unable to evaluate XPath expression '"
            << sFullPath << "'");
        xmlXPathFreeContext(xpCtx);
        return NULL;
    }
    
    xmlXPathFreeContext(xpCtx);

    return xpElement;
}

void TrackerConfig::setParam(const string& sXPathExpr, const string& sValue)
{
    xmlXPathObjectPtr xpElement = findConfigNodes(sXPathExpr);
    xmlNodeSetPtr nodes = xpElement->nodesetval;
    
    if (!nodes || nodes->nodeNr == 0)
        throw (Exception(AVG_ERR_OPTION_UNKNOWN, 
                    string("setParam(): cannot find requested element ")+sXPathExpr));
    
    for (int i = nodes->nodeNr-1; i >= 0; i--) {
        AVG_ASSERT(nodes->nodeTab[i]);

        xmlNodeSetContent(nodes->nodeTab[i], BAD_CAST sValue.c_str());
        if (nodes->nodeTab[i]->type != XML_NAMESPACE_DECL)
            nodes->nodeTab[i] = NULL;
    }
    
    xmlXPathFreeObject(xpElement);
}

string TrackerConfig::getParam(const string& sXPathExpr) const
{
    xmlXPathObjectPtr xpElement = findConfigNodes(sXPathExpr);
    xmlNodeSetPtr nodes = xpElement->nodesetval;
    
    if (!nodes || nodes->nodeNr == 0) {
        throw (Exception(AVG_ERR_OPTION_UNKNOWN, 
                    string("getParam(): cannot find requested element ")+sXPathExpr));
    } else if (nodes->nodeNr > 1) {
        AVG_LOG_WARNING(
            "getParam(): expression selects more than one node. Returning the first.");
    }

    xmlChar* xsRc = xmlNodeGetContent(nodes->nodeTab[0]);
    string sValue((char *)xsRc);
    
    xmlFree(xsRc);
    xmlXPathFreeObject(xpElement);

    return sValue;
}

bool TrackerConfig::getBoolParam(const std::string& sXPathExpr) const
{
    return stringToBool(getParam(sXPathExpr));
}

int TrackerConfig::getIntParam(const std::string& sXPathExpr) const
{
    return stringToInt(getParam(sXPathExpr));
}

float TrackerConfig::getFloatParam(const std::string& sXPathExpr) const
{
    return stringToFloat(getParam(sXPathExpr));
}

glm::vec2 TrackerConfig::getPointParam(const std::string& sXPathExpr) const
{
    return glm::vec2(getFloatParam(sXPathExpr+"@x"), getFloatParam(sXPathExpr+"@y"));
}

FRect TrackerConfig::getRectParam(const std::string& sXPathExpr) const
{
     glm::vec2 pos1 = glm::vec2(getFloatParam(sXPathExpr+"@x1"), 
            getFloatParam(sXPathExpr+"@y1"));
     glm::vec2 pos2 = glm::vec2(getFloatParam(sXPathExpr+"@x2"),
            getFloatParam(sXPathExpr+"@y2"));
     return FRect(pos1, pos2);
}

xmlNodePtr TrackerConfig::getXmlNode(const std::string& sXPathExpr) const
{
    xmlXPathObjectPtr xpElement = findConfigNodes(sXPathExpr);
    xmlNodeSetPtr nodes = xpElement->nodesetval;
    
    if (!nodes || nodes->nodeNr == 0) {
        throw (Exception(AVG_ERR_OPTION_UNKNOWN, 
                string("getParam(): cannot find requested element ")+sXPathExpr));
    } else if (nodes->nodeNr > 1) {
        AVG_LOG_WARNING(
            "getXmlNode(): expression selects more than one node. Returning the first.");
    }
    return nodes->nodeTab[0];
}

DeDistortPtr TrackerConfig::getTransform() const
{
    glm::vec2 CameraExtents = getPointParam("/camera/size/");
    DeDistortPtr pDD(new DeDistort);
    pDD->load(CameraExtents, *this);
    return pDD;
}

void TrackerConfig::setTransform(DeDistortPtr pDeDistort)
{
    pDeDistort->save(*this);
}

void TrackerConfig::dump() const
{
    string s;
    xmlBufferPtr pBuffer = xmlBufferCreate();
    xmlNodeDump(pBuffer, m_Doc, m_pRoot, 0, 0);
    cerr << xmlBufferContent(pBuffer) << endl;
}

void TrackerConfig::save()
{
    AVG_TRACE(Logger::category::CONFIG, Logger::severity::INFO,
            "Saving tracker configuration to " << m_sFilename << ".");

    if (m_Doc) {
        if (fileExists(m_sFilename)) {
            string sBakFile = m_sFilename + ".bak";
            unlink(sBakFile.c_str());
            if (rename(m_sFilename.c_str(), sBakFile.c_str())) {
                AVG_LOG_WARNING("Cannot create tracker config backup. Backing "
                        "it up on current workdir.");
                copyFile(m_sFilename, "avgtrackerrc.bak");
            }
        }
        xmlSaveFileEnc(m_sFilename.c_str(), m_Doc, "utf-8");
    } else
        throw (Exception(AVG_ERR_FILEIO, 
                    "save(): tracker configuration not initialized"));
}

}