~andreas-pokorny/mir/key-repeater-on-timer-fd-utility

« back to all changes in this revision

Viewing changes to src/common/time/steady_timer_fd.cpp

  • Committer: Andreas Pokorny
  • Date: 2016-10-12 05:28:11 UTC
  • Revision ID: andreas.pokorny@canonical.com-20161012052811-t7dqmw17kumkzup3
Add a simpler key repeater utility class for stub and evdev input platform

The key repeater is meant to be integrated with the Dispatchable classes and will replace the key repeater that currently abuses the main loop in inject repeated keys inside the mirserver. It uses a new mircommon steady_timer_fd utility that wrapps CLOCK_MONOTONIC timer fds.

This is a preparation step to move the repeat handling inside the input platforms, which will evade another mirserver event type fumbling location.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser 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 "mir/time/steady_timer_fd.h"
 
20
#include <sys/timerfd.h>
 
21
#include <chrono>
 
22
 
 
23
namespace mt = mir::time;
 
24
 
 
25
mt::SteadyTimerFd::SteadyTimerFd()
 
26
  : timer{timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK)}
 
27
{
 
28
}
 
29
 
 
30
mir::Fd const& mt::SteadyTimerFd::get_fd()
 
31
{
 
32
    return timer;
 
33
}
 
34
 
 
35
void mt::SteadyTimerFd::schedule_in(mir::time::Duration delay)
 
36
{
 
37
    using namespace std::chrono;
 
38
    auto const in_seconds = duration_cast<seconds>(delay);
 
39
    itimerspec delay_spec;
 
40
 
 
41
    delay_spec.it_value.tv_sec = in_seconds.count();
 
42
    delay_spec.it_value.tv_nsec = nanoseconds(delay - in_seconds).count();
 
43
    delay_spec.it_interval.tv_sec = 0;
 
44
    delay_spec.it_interval.tv_nsec = 0;
 
45
 
 
46
    const int absolute_timeout = TFD_TIMER_ABSTIME;
 
47
    timerfd_settime(timer, absolute_timeout, &delay_spec, nullptr);
 
48
}
 
49
 
 
50
void mt::SteadyTimerFd::cancel()
 
51
{
 
52
    const itimerspec cancel_timer = {{0,0},{0,0}};
 
53
    timerfd_settime(timer, 0, &cancel_timer, nullptr);
 
54
}