summaryrefslogtreecommitdiffhomepage
path: root/opentrack-api/context.cpp
blob: 100acf5c7e4e5f14c387b6ae44960e72f6d84ec2 (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
#include "opentrack-guts.h"
#include "opentrack.h"

#if defined(__APPLE__)
#   define SONAME "dylib"
#elif defined(_WIN32)
#   define SONAME "dll"
#else
#   define SONAME "so"
#endif

#include <iostream>

#ifdef _MSC_VER
#   define LIB_PREFIX ""
#else
#   define LIB_PREFIX "lib"
#endif

// shamelessly copied from core
static Metadata* get_metadata(DynamicLibrary* lib, QString& longName, QIcon& icon)
{
    Metadata* meta;
    if (!lib->Metadata || ((meta = lib->Metadata()), !meta))
        return NULL;
    meta->getFullName(&longName);
    meta->getIcon(&icon);
    return meta;
}

static QList<opentrack_meta> list_files(QDir& dir, QString filter)
{
    QList<opentrack_meta> ret;
    QStringList filenames = dir.entryList( QStringList() << (LIB_PREFIX + filter + ("*." SONAME)), QDir::Files, QDir::Name );
    for ( int i = 0; i < filenames.size(); i++) {
        QIcon icon;
        QString long_name;
        QString str = filenames.at(i);
        DynamicLibrary* lib = new DynamicLibrary(str);
        qDebug() << "Loading" << str;
        std::cout.flush();
        Metadata* meta;
        if (!(meta = get_metadata(lib, long_name, icon)))
        {
            delete lib;
            continue;
        }
        QString prefix(LIB_PREFIX + filter);
        QString suffix("*." SONAME);
        if (str.size() > prefix.size() + suffix.size() && str.startsWith(prefix) && str.endsWith(suffix))
        {
            auto str2 = str.mid(prefix.size(), str.size() - prefix.size() - suffix.size());
            opentrack_meta item(meta, str2, lib);
            ret.push_back(item);
        }
    }

    return ret;
}

opentrack_ctx::opentrack_ctx(QDir& dir) :
    dir(dir),
    meta_list(list_files(dir, "opentrack-tracker-"))
{
    const int count = meta_list.size();
    list = new char*[count + 1];
    for (int i = 0; i < count; i++)
    {
        QByteArray tmp = meta_list.at(i).path.toUtf8();
        int len = tmp.size();
        auto foo = new char[len+1];
        for (int j = 0; j < len; j++)
            foo[j] = tmp.at(j);
        foo[len] = '\0';
        list[i] = foo;
    }
    list[count] = NULL;
}

opentrack_ctx::~opentrack_ctx()
{
    for (int i = 0; list[i]; i++)
    {
        delete list[i];
    }
    delete list;
}

extern "C"
{

const char** OPENTRACK_EXPORT opentrack_enum_trackers(opentrack ctx)
{
    return const_cast<const char**>(ctx->list);
}

opentrack OPENTRACK_EXPORT opentrack_make_ctx(const char *dir)
{
    QDir d(dir);
    return new opentrack_ctx(d);
}

void OPENTRACK_EXPORT opentrack_finalize_ctx(opentrack foo)
{
    delete foo;
}

}