~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to tests/mir_test_framework/stub_input_platform.cpp

  • Committer: Package Import Robot
  • Author(s): CI Train Bot
  • Date: 2015-05-12 13:12:55 UTC
  • mto: This revision was merged to the branch mainline in revision 96.
  • Revision ID: package-import@ubuntu.com-20150512131255-y7z12i8n4pbvo70x
Tags: upstream-0.13.0+15.10.20150512
Import upstream version 0.13.0+15.10.20150512

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2014-2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Andreas Pokorny <andreas.pokorny@canonical.com>
 
17
 */
 
18
 
 
19
#include "stub_input_platform.h"
 
20
 
 
21
#include "mir/input/input_device_registry.h"
 
22
#include "mir/dispatch/action_queue.h"
 
23
#include "mir/module_deleter.h"
 
24
 
 
25
#include <algorithm>
 
26
 
 
27
namespace mtf = mir_test_framework;
 
28
namespace mi = mir::input;
 
29
 
 
30
mtf::StubInputPlatform::StubInputPlatform(
 
31
    std::shared_ptr<mi::InputDeviceRegistry> const& input_device_registry)
 
32
    : platform_queue{mir::make_module_ptr<mir::dispatch::ActionQueue>()},
 
33
    registry(input_device_registry)
 
34
{
 
35
    stub_input_platform = this;
 
36
}
 
37
 
 
38
mtf::StubInputPlatform::~StubInputPlatform()
 
39
{
 
40
    device_store.clear();
 
41
    stub_input_platform = nullptr;
 
42
}
 
43
 
 
44
void mtf::StubInputPlatform::start()
 
45
{
 
46
    for (auto const& dev : device_store)
 
47
    {
 
48
        auto device = dev.lock();
 
49
        registry->add_device(device);
 
50
    }
 
51
    device_store.clear();
 
52
}
 
53
 
 
54
std::shared_ptr<mir::dispatch::Dispatchable> mtf::StubInputPlatform::dispatchable()
 
55
{
 
56
    return platform_queue;
 
57
}
 
58
 
 
59
void mtf::StubInputPlatform::stop()
 
60
{
 
61
}
 
62
 
 
63
void mtf::StubInputPlatform::add(std::shared_ptr<mir::input::InputDevice> const& dev)
 
64
{
 
65
    if (!stub_input_platform)
 
66
    {
 
67
        device_store.push_back(dev);
 
68
        return;
 
69
    }
 
70
 
 
71
    stub_input_platform->platform_queue->enqueue(
 
72
        [registry=stub_input_platform->registry,dev]
 
73
        {
 
74
            registry->add_device(dev);
 
75
        });
 
76
}
 
77
 
 
78
void mtf::StubInputPlatform::remove(std::shared_ptr<mir::input::InputDevice> const& dev)
 
79
{
 
80
    if (!stub_input_platform)
 
81
        BOOST_THROW_EXCEPTION(std::runtime_error("No stub input platform available"));
 
82
 
 
83
    stub_input_platform->platform_queue->enqueue(
 
84
        [registry=stub_input_platform->registry,dev]
 
85
        {
 
86
            registry->remove_device(dev);
 
87
        });
 
88
}
 
89
 
 
90
mtf::StubInputPlatform* mtf::StubInputPlatform::stub_input_platform = nullptr;
 
91
std::vector<std::weak_ptr<mir::input::InputDevice>> mtf::StubInputPlatform::device_store;