Mir
server_example_input_filter.cpp

Demonstrate adding a custom input filter to a server

/*
* 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 "mir/server.h"
#include <linux/input.h>
#include <iostream>
namespace me = mir::examples;
namespace mc = mir::compositor;
namespace mg = mir::graphics;
namespace mi = mir::input;
namespace
{
void print_key_event(MirInputEvent const* ev)
{
auto event_time = mir_input_event_get_event_time(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);
std::cout << "Handling 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);
std::cout << " "
<< " id=" << id
<< " pos=(" << px << ", " << py << ")"
<< std::endl;
}
std::cout << "----------------" << std::endl << std::endl;
}
void print_pointer_event(MirInputEvent const* ev)
{
auto event_time = mir_input_event_get_event_time(ev);
auto action = mir_pointer_event_action(pev);
std::cout << "Handling pointer event time=" << event_time
<< " action=";
switch(action)
{
std::cout << "motion";
break;
std::cout << "up";
break;
std::cout << "down";
break;
default:
break;
}
std::cout << " "
<< " pos=(" << mir_pointer_event_axis_value(pev, mir_pointer_axis_x) << ", "
std::cout << "----------------" << std::endl << std::endl;
}
struct PrintingEventFilter : public mi::EventFilter
{
bool handle(MirEvent const& ev) override
{
return false;
auto input_event = mir_event_get_input_event(&ev);
switch (mir_input_event_get_type(input_event))
{
print_key_event(input_event);
break;
print_touch_event(input_event);
break;
print_pointer_event(input_event);
break;
default:
std::cout << "unkown input event type: " << mir_input_event_get_type(input_event) << std::endl;
break;
}
return false;
}
};
struct ScreenRotationFilter : public mi::EventFilter
{
bool handle(MirEvent const& event) override
{
return false;
auto const input_event = mir_event_get_input_event(&event);
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 =
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 &&
{
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;
};
}
-> 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;
}
-> 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;
}

Copyright © 2012-2016 Canonical Ltd.
Generated on Mon Jun 5 11:07:25 UTC 2017