Add joystick plugin (#39)

https://github.com/sony/flutter-elinux-plugins/issues/35
This commit is contained in:
Hidenori Matsubayashi
2021-08-30 15:58:08 +09:00
committed by GitHub
parent bb10db6bdb
commit fc39cb676c
27 changed files with 1320 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
flutter/
+25
View File
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.10)
set(PROJECT_NAME "joystick")
project(${PROJECT_NAME} LANGUAGES CXX)
# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "joystick_plugin")
add_library(${PLUGIN_NAME} SHARED
"joystick_plugin.cc"
"linux_joystick.cc"
)
apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES
CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)
# List of absolute paths to libraries that should be bundled with the plugin
set(joystick_bundled_libraries
""
PARENT_SCOPE
)
@@ -0,0 +1,23 @@
#ifndef FLUTTER_PLUGIN_JOYSTICK_PLUGIN_H_
#define FLUTTER_PLUGIN_JOYSTICK_PLUGIN_H_
#include <flutter_plugin_registrar.h>
#ifdef FLUTTER_PLUGIN_IMPL
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
#else
#define FLUTTER_PLUGIN_EXPORT
#endif
#if defined(__cplusplus)
extern "C" {
#endif
FLUTTER_PLUGIN_EXPORT void JoystickPluginRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar);
#if defined(__cplusplus)
} // extern "C"
#endif
#endif // FLUTTER_PLUGIN_JOYSTICK_PLUGIN_H_
@@ -0,0 +1,41 @@
// Copyright 2021 Sony Group Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "include/joystick/joystick_plugin.h"
#include <flutter/plugin_registrar.h>
#include <memory>
#include <sstream>
namespace {
class JoystickPlugin : public flutter::Plugin {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar);
JoystickPlugin();
virtual ~JoystickPlugin();
};
// static
void JoystickPlugin::RegisterWithRegistrar(
flutter::PluginRegistrar *registrar) {
auto plugin = std::make_unique<JoystickPlugin>();
registrar->AddPlugin(std::move(plugin));
}
JoystickPlugin::JoystickPlugin() {}
JoystickPlugin::~JoystickPlugin() {}
} // namespace
void JoystickPluginRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar) {
JoystickPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarManager::GetInstance()
->GetRegistrar<flutter::PluginRegistrar>(registrar));
}
@@ -0,0 +1,27 @@
// Copyright 2021 Sony Group Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <errno.h>
#include <fcntl.h>
#include <linux/joystick.h>
#include <stdio.h>
#include <unistd.h>
extern "C" __attribute__((visibility("default"))) int joystick_open(
const char* device) {
int fd = open(device, O_NONBLOCK);
if (fd < 0) {
fprintf(stderr, "Failed to open %s (%d)\n", device, errno);
}
return fd;
}
extern "C" __attribute__((visibility("default"))) int joystick_read(
int fd, js_event* ev) {
int bytes = read(fd, ev, sizeof(*ev));
if (bytes < 0) {
return -1;
}
return bytes == sizeof(*ev);
}