fb32f559784388f7e8f52289fb30bbabe767aa4c
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / input / manager.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
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, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #include "anbox/input/manager.h"
19 #include "anbox/system_configuration.h"
20 #include "anbox/input/device.h"
21 #include "anbox/runtime.h"
22 #include "anbox/utils.h"
23
24 #include <boost/format.hpp>
25
26 namespace anbox {
27 namespace input {
28 Manager::Manager(const std::shared_ptr<Runtime> &runtime) : runtime_(runtime) {
29   const auto dir = SystemConfiguration::instance().input_device_dir();
30   utils::ensure_paths({dir});
31
32   // The directory is bind-mounted into the container but might have user
33   // permissions only (rwx------). Make sure it is accessible.
34   ::chmod(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
35 }
36
37 Manager::~Manager() {}
38
39 std::shared_ptr<Device> Manager::create_device() {
40   const auto id = next_id();
41   const auto path = build_device_path(id);
42   auto device = Device::create(path, runtime_);
43   devices_.insert({id, device});
44   return device;
45 }
46
47 std::uint32_t Manager::next_id() {
48   static std::uint32_t next_id = 0;
49   return next_id++;
50 }
51
52 std::string Manager::build_device_path(const std::uint32_t &id) {
53   return (boost::format("%1%/event%2%") % SystemConfiguration::instance().input_device_dir() % id).str();
54 }
55
56 }  // namespace input
57 }  // namespace anbox