~alan-griffiths/mir/fix-1654023

« back to all changes in this revision

Viewing changes to tests/mir_test_framework/fake_input_device_impl.cpp

  • Committer: Daniel van Vugt
  • Date: 2015-04-28 07:54:10 UTC
  • mfrom: (2517 development-branch)
  • mto: This revision was merged to the branch mainline in revision 2673.
  • Revision ID: daniel.van.vugt@canonical.com-20150428075410-rwskshfuar7voesp
Merge latest trunk and fix conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 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 "fake_input_device_impl.h"
 
20
#include "stub_input_platform.h"
 
21
 
 
22
#include "mir/input/input_device.h"
 
23
#include "mir/input/input_device_info.h"
 
24
#include "mir/input/input_sink.h"
 
25
#include "mir/dispatch/action_queue.h"
 
26
#include "mir/module_deleter.h"
 
27
 
 
28
#include "boost/throw_exception.hpp"
 
29
#include "linux/input.h"
 
30
 
 
31
#include "mir/events/event_builders.h"
 
32
 
 
33
#include <chrono>
 
34
 
 
35
namespace mi = mir::input;
 
36
namespace md = mir::dispatch;
 
37
namespace mtf = mir_test_framework;
 
38
 
 
39
namespace
 
40
{
 
41
uint32_t to_modifier(int32_t scan_code)
 
42
{
 
43
    switch(scan_code)
 
44
    {
 
45
    case KEY_LEFTALT:
 
46
        return mir_input_event_modifier_alt_left;
 
47
    case KEY_RIGHTALT:
 
48
        return mir_input_event_modifier_alt_right;
 
49
    case KEY_RIGHTCTRL:
 
50
        return mir_input_event_modifier_ctrl_right;
 
51
    case KEY_LEFTCTRL:
 
52
        return mir_input_event_modifier_ctrl_left;
 
53
    case KEY_CAPSLOCK:
 
54
        return mir_input_event_modifier_caps_lock;
 
55
    case KEY_LEFTMETA:
 
56
        return mir_input_event_modifier_meta_left;
 
57
    case KEY_RIGHTMETA:
 
58
        return mir_input_event_modifier_meta_right;
 
59
    case KEY_SCROLLLOCK:
 
60
        return mir_input_event_modifier_scroll_lock;
 
61
    case KEY_NUMLOCK:
 
62
        return mir_input_event_modifier_num_lock;
 
63
    case KEY_LEFTSHIFT:
 
64
        return mir_input_event_modifier_shift_left;
 
65
    case KEY_RIGHTSHIFT:
 
66
        return mir_input_event_modifier_shift_right;
 
67
    default:
 
68
        return mir_input_event_modifier_none;
 
69
    }
 
70
}
 
71
 
 
72
uint32_t expand_modifier(uint32_t modifiers)
 
73
{
 
74
    if ((modifiers&mir_input_event_modifier_alt_left) ||
 
75
        (modifiers&mir_input_event_modifier_alt_right))
 
76
        modifiers |= mir_input_event_modifier_alt;
 
77
 
 
78
    if ((modifiers&mir_input_event_modifier_ctrl_left) ||
 
79
        (modifiers&mir_input_event_modifier_ctrl_right))
 
80
        modifiers |= mir_input_event_modifier_ctrl;
 
81
 
 
82
    if ((modifiers&mir_input_event_modifier_shift_left) ||
 
83
        (modifiers&mir_input_event_modifier_shift_right))
 
84
        modifiers |= mir_input_event_modifier_shift;
 
85
 
 
86
    if ((modifiers&mir_input_event_modifier_meta_left) ||
 
87
        (modifiers&mir_input_event_modifier_meta_right))
 
88
        modifiers |= mir_input_event_modifier_meta;
 
89
 
 
90
    return modifiers;
 
91
}
 
92
 
 
93
}
 
94
 
 
95
mtf::FakeInputDeviceImpl::FakeInputDeviceImpl(mi::InputDeviceInfo const& info)
 
96
    : queue{mir::make_module_ptr<md::ActionQueue>()},
 
97
    device{mir::make_module_ptr<InputDevice>(info, queue)}
 
98
{
 
99
    mtf::StubInputPlatform::add(device);
 
100
}
 
101
 
 
102
void mtf::FakeInputDeviceImpl::emit_event(synthesis::KeyParameters const& key)
 
103
{
 
104
    queue->enqueue([this, key]()
 
105
                   {
 
106
                       device->synthesize_events(key);
 
107
                   });
 
108
}
 
109
 
 
110
mtf::FakeInputDeviceImpl::InputDevice::InputDevice(mi::InputDeviceInfo const& info,
 
111
                                                   std::shared_ptr<mir::dispatch::Dispatchable> const& dispatchable)
 
112
    : info(info), queue{dispatchable}
 
113
{
 
114
}
 
115
 
 
116
void mtf::FakeInputDeviceImpl::InputDevice::synthesize_events(synthesis::KeyParameters const& key_params)
 
117
{
 
118
    int64_t device_id_unknown = 0;
 
119
    xkb_keysym_t key_code = 0;
 
120
    int64_t event_time = std::chrono::duration_cast<std::chrono::nanoseconds>(
 
121
                             std::chrono::system_clock::now().time_since_epoch()).count();
 
122
    auto input_action = (key_params.action == synthesis::EventAction::Down) ? mir_keyboard_action_down:
 
123
                                                                              mir_keyboard_action_up;
 
124
 
 
125
    auto event_modifiers = expand_modifier(modifiers);
 
126
    auto key_event = mir::events::make_event(
 
127
        device_id_unknown, event_time, input_action, key_code, key_params.scancode, event_modifiers);
 
128
 
 
129
    if (key_params.action == synthesis::EventAction::Down)
 
130
        modifiers |= to_modifier(key_params.scancode);
 
131
    else
 
132
        modifiers &= ~to_modifier(key_params.scancode);
 
133
 
 
134
    if (!sink)
 
135
        BOOST_THROW_EXCEPTION(std::runtime_error("Device is not started."));
 
136
    sink->handle_input(*key_event);
 
137
}
 
138
 
 
139
std::shared_ptr<md::Dispatchable> mtf::FakeInputDeviceImpl::InputDevice::dispatchable()
 
140
{
 
141
    return queue;
 
142
}
 
143
 
 
144
void mtf::FakeInputDeviceImpl::InputDevice::start(mi::InputSink* destination)
 
145
{
 
146
    sink = destination;
 
147
}
 
148
 
 
149
void mtf::FakeInputDeviceImpl::InputDevice::stop()
 
150
{
 
151
    sink = nullptr;
 
152
}