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

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
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored By: Alan Griffiths <alan@octopull.co.uk>
 */

#include "server_example_input_filter.h"

#include "mir/server.h"

#include "mir/compositor/compositor.h"
#include "mir/graphics/display.h"
#include "mir/graphics/display_configuration.h"
#include "mir/input/composite_event_filter.h"
#include "mir/options/option.h"

#include <linux/input.h>

#include <iostream>

namespace me = mir::examples;
namespace mc = mir::compositor;
namespace mg = mir::graphics;
namespace mi = mir::input;

///\example server_example_input_filter.cpp
/// Demonstrate adding a custom input filter to a server
namespace
{
void print_key_event(MirInputEvent const* ev)
{
    auto event_time = mir_input_event_get_event_time(ev);
    auto kev = mir_input_event_get_keyboard_event(ev);
    auto scan_code = mir_keyboard_event_scan_code(kev);
    auto key_code = mir_keyboard_event_key_code(kev);

    std::cout << "Handling key event (time, scancode, keycode): " << event_time << " " <<
              scan_code << " " << key_code << std::endl;
}

void print_touch_event(MirInputEvent const* ev)
{
    auto event_time = mir_input_event_get_event_time(ev);
    auto tev = mir_input_event_get_touch_event(ev);
    auto tc = mir_touch_event_point_count(tev);

    std::cout << "Handline touch event time=" << event_time
              << " touch_count=" << tc << std::endl;
    for (unsigned i = 0; i < tc; i++)
    {
        auto id = mir_touch_event_id(tev, i);
        auto px = mir_touch_event_axis_value(tev, i, mir_touch_axis_x);
        auto py = mir_touch_event_axis_value(tev, i, mir_touch_axis_y);

        std::cout << "  "
            << " id=" << id
            << " pos=(" << px << ", " << py << ")"
            << std::endl;
    }
    std::cout << "----------------" << std::endl << std::endl;
}

struct PrintingEventFilter : public mi::EventFilter
{
    bool handle(MirEvent const& ev) override
    {
        if (mir_event_get_type(&ev) != mir_event_type_input)
            return false;
        auto input_event = mir_event_get_input_event(&ev);

        switch (mir_input_event_get_type(input_event))
        {
        case mir_input_event_type_key:
            print_key_event(input_event);
            break;
        case mir_input_event_type_touch:
            print_touch_event(input_event);
            break;
        default:
            abort();
        }

        return false;
    }
};

struct ScreenRotationFilter : public mi::EventFilter
{
    bool handle(MirEvent const& event) override
    {
        if (mir_event_get_type(&event) != mir_event_type_input)
            return false;

        auto const input_event = mir_event_get_input_event(&event);

        if (mir_input_event_get_type(input_event) != mir_input_event_type_key)
            return false;

        return handle_keyboard_event(mir_input_event_get_keyboard_event(input_event));
    }

    bool handle_keyboard_event(MirKeyboardEvent const* event)
    {
        static const int modifier_mask =
            mir_input_event_modifier_alt |
            mir_input_event_modifier_shift |
            mir_input_event_modifier_sym |
            mir_input_event_modifier_ctrl |
            mir_input_event_modifier_meta;

        auto const action = mir_keyboard_event_action(event);
        auto const scan_code = mir_keyboard_event_scan_code(event);
        auto const modifiers = mir_keyboard_event_modifiers(event) & modifier_mask;

        if (action == mir_keyboard_action_down &&
            modifiers == (mir_input_event_modifier_alt | mir_input_event_modifier_ctrl))
        {
            switch (scan_code)
            {
            case KEY_UP:
                apply_orientation(mir_orientation_normal);
                break;

            case KEY_DOWN:
                apply_orientation(mir_orientation_inverted);
                break;

            case KEY_LEFT:
                apply_orientation(mir_orientation_left);
                break;

            case KEY_RIGHT:
                apply_orientation(mir_orientation_right);
                break;

            default:
                return false;
            }

            return true;
        }

        return false;
    }

    void apply_orientation(MirOrientation orientation)
    {
        // TODO This is too nuts & bolts for the public API.
        // TODO There should be an interface onto MediatingDisplayChanger that
        // TODO provides equivalent functionality wrapped nicely
        compositor->stop();
        auto conf = display->configuration();

        conf->for_each_output([orientation](mg::UserDisplayConfigurationOutput& output)
            {
                output.orientation = orientation;
            });

        display->configure(*conf);
        compositor->start();
    }

    std::shared_ptr<mg::Display> display;
    std::shared_ptr<mc::Compositor> compositor;
};
}

auto me::make_printing_input_filter_for(mir::Server& server)
-> std::shared_ptr<mi::EventFilter>
{
    static const char* const print_input_events = "print-input-events";
    static const char* const print_input_events_descr = "List input events on std::cout";

    server.add_configuration_option(print_input_events, print_input_events_descr, mir::OptionType::null);

    auto const printing_filter = std::make_shared<PrintingEventFilter>();

    server.add_init_callback([printing_filter, &server]
        {
            const auto options = server.get_options();
            if (options->is_set(print_input_events))
                server.the_composite_event_filter()->prepend(printing_filter);
        });

    return printing_filter;
}

auto me::make_screen_rotation_filter_for(mir::Server& server)
-> std::shared_ptr<input::EventFilter>
{
    static const char* const screen_rotation = "screen-rotation";
    static const char* const screen_rotation_descr = "Rotate screen on Ctrl-Alt-<Arrow>";

    server.add_configuration_option(screen_rotation, screen_rotation_descr, mir::OptionType::null);

    auto const screen_rotation_filter = std::make_shared<ScreenRotationFilter>();

    server.add_init_callback([screen_rotation_filter, &server]
        {
            const auto options = server.get_options();
            if (options->is_set(screen_rotation))
            {
                screen_rotation_filter->display = server.the_display();
                screen_rotation_filter->compositor = server.the_compositor();
                server.the_composite_event_filter()->prepend(screen_rotation_filter);
            }
        });

    return screen_rotation_filter;
}