~alan-griffiths/mir/knee-jerk-mir_surface_state_automatic

« back to all changes in this revision

Viewing changes to src/input/evemu/device.cpp

  • Committer: Kevin DuBois
  • Date: 2012-11-13 01:36:29 UTC
  • mfrom: (245 trunk)
  • mto: This revision was merged to the branch mainline in revision 246.
  • Revision ID: kevin.dubois@canonical.com-20121113013629-q4496w4mp5e33auk
merge in base branch. move the demo clients to a new directory, examples/

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License version 3 as
6
 
 * 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: Chase Douglas <chase.douglas@canonical.com>
17
 
 */
18
 
 
19
 
#include "mir/input/evemu/device.h"
20
 
 
21
 
#include <boost/filesystem.hpp>
22
 
 
23
 
#include <evemu.h>
24
 
 
25
 
#include <cstdio>
26
 
#include <stdexcept>
27
 
 
28
 
#include <sys/types.h>
29
 
#include <sys/stat.h>
30
 
#include <fcntl.h>
31
 
 
32
 
namespace mi = mir::input;
33
 
 
34
 
namespace {
35
 
 
36
 
struct EvemuDeleter
37
 
{
38
 
    void operator() (struct evemu_device* device)
39
 
    {
40
 
        evemu_delete(device);
41
 
    }
42
 
};
43
 
 
44
 
}
45
 
 
46
 
mi::evemu::EvemuDevice::EvemuDevice(
47
 
    const std::string& path,
48
 
    EventHandler* event_handler) : LogicalDevice(event_handler),
49
 
                                   simultaneous_instances{1},
50
 
    buttons(static_cast<size_t>(KEY_MAX), false),
51
 
    position_info{Mode::none}
52
 
{
53
 
    std::unique_ptr<evemu_device, EvemuDeleter> evemu{evemu_new(NULL), EvemuDeleter()};
54
 
 
55
 
    boost::filesystem::file_status const status{boost::filesystem::status(path)};
56
 
    if (status.type() == boost::filesystem::regular_file)
57
 
    {
58
 
        std::FILE *file = fopen(path.c_str(), "r");
59
 
 
60
 
        if (!file)
61
 
            throw std::runtime_error("Failed to open evemu file");
62
 
 
63
 
        int const evenmu_error{evemu_read(evemu.get(), file)};
64
 
        fclose(file);
65
 
 
66
 
        if (evenmu_error < 0)
67
 
            throw std::runtime_error("Failed to read evemu parameters from file");
68
 
    }
69
 
    /* FIXME: Need test for evdev device nodes before uncommenting */
70
 
    /*else if (status.type() == boost::filesystem::character_file)
71
 
      {
72
 
      int fd = open(path.c_str(), O_RDONLY);
73
 
      if (fd < 0)
74
 
      throw std::runtime_error("Failed to open evdev node");
75
 
 
76
 
      if (evemu_extract(evemu.get(), fd) < 0)
77
 
      throw std::runtime_error("Failed to extract evdev node evemu parameters");
78
 
      }*/ else
79
 
        throw std::runtime_error("Device path is not a file nor a character device");
80
 
 
81
 
    name = evemu_get_name(evemu.get());
82
 
 
83
 
    if (evemu_has_event(evemu.get(), EV_ABS, ABS_MT_SLOT))
84
 
    {
85
 
        simultaneous_instances =
86
 
                evemu_get_abs_maximum(evemu.get(), ABS_MT_SLOT) - evemu_get_abs_minimum(evemu.get(), ABS_MT_SLOT) + 1;
87
 
    }
88
 
 
89
 
    for (int b = 0; b <= KEY_MAX; ++b)
90
 
    {
91
 
        switch (b)
92
 
        {
93
 
            case BTN_TOUCH:
94
 
                continue;
95
 
            default:
96
 
                break;
97
 
        }
98
 
 
99
 
        if (evemu_has_event(evemu.get(), EV_KEY, b))
100
 
            buttons[b] = 1;
101
 
    }
102
 
 
103
 
    if (evemu_has_event(evemu.get(), EV_ABS, ABS_MT_POSITION_X) || evemu_has_event(evemu.get(), EV_ABS, ABS_X))
104
 
        position_info.mode = Mode::absolute;
105
 
    else if (evemu_has_event(evemu.get(), EV_REL, REL_X))
106
 
        position_info.mode = Mode::relative;
107
 
}
108
 
 
109
 
mi::EventProducer::State mi::evemu::EvemuDevice::current_state() const
110
 
{
111
 
    return mi::EventProducer::State::stopped;
112
 
}
113
 
 
114
 
void mi::evemu::EvemuDevice::start()
115
 
{
116
 
    // TODO
117
 
}
118
 
 
119
 
void mi::evemu::EvemuDevice::stop()
120
 
{
121
 
    // TODO
122
 
}
123
 
 
124
 
const std::string& mi::evemu::EvemuDevice::get_name() const
125
 
{
126
 
    return name;
127
 
}
128
 
 
129
 
int mi::evemu::EvemuDevice::get_simultaneous_instances() const
130
 
{
131
 
    return simultaneous_instances;
132
 
}
133
 
 
134
 
/* FIXME: Reenable once under test.
135
 
bool mi::evemu::EvemuDevice::is_button_supported(const Button& button) const
136
 
{
137
 
    return buttons[button];
138
 
}
139
 
*/
140
 
 
141
 
const mi::PositionInfo& mi::evemu::EvemuDevice::get_position_info() const
142
 
{
143
 
    return position_info;
144
 
}
145
 
 
146
 
const std::map<mi::AxisType, mi::Axis>& mi::evemu::EvemuDevice::get_axes() const
147
 
{
148
 
    return axes;
149
 
}