summaryrefslogtreecommitdiffhomepage
path: root/opentrack/work.hpp
blob: a3c0e0141bcddfd0c469a0ef23ffedf55202529a (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
/* Copyright (c) 2014-2015, Stanislaw Halik <sthalik@misaki.pl>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */

#pragma once

#include "opentrack/main-settings.hpp"
#include "opentrack/plugin-support.hpp"
#include "opentrack/tracker.h"
#include "opentrack/shortcuts.h"

#include <QObject>
#include <QFrame>
#include <memory>
#include <vector>
#include <functional>
#include <tuple>

struct Work
{
    main_settings& s;
    SelectedLibraries& libs;
    mem<Tracker> tracker;
    mem<Shortcuts> sc;
    WId handle;
    using fn = std::function<void(void)>;
    using key_opts = Shortcuts::key_opts;
    using tt = std::tuple<key_opts&, fn>;
    std::vector<std::tuple<key_opts&, fn>> keys;
    
    Work(main_settings& s, Mappings& m, SelectedLibraries& libs, WId handle) :
        s(s), libs(libs),
        tracker(std::make_shared<Tracker>(s, m, libs)),
        sc(std::make_shared<Shortcuts>()),
        handle(handle),
        keys {
            tt(sc->s.center, [&]() -> void { tracker->center(); }),
            tt(sc->s.toggle, [&]() -> void { tracker->toggle_enabled(); }),
            tt(sc->s.zero, [&]() -> void { tracker->zero(); }),
        }
    {
        reload_shortcuts();
        tracker->start();   
    }

    void reload_shortcuts()
    {
        sc->reload(keys);
    }

    ~Work()
    {
        sc = nullptr;
        // order matters, otherwise use-after-free -sh
        tracker = nullptr;
        libs = SelectedLibraries();
    }
};