diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..985139e --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @S-Dafarra diff --git a/.github/workflows/conda-forge-ci.yml b/.github/workflows/conda-forge-ci.yml new file mode 100644 index 0000000..cd63c09 --- /dev/null +++ b/.github/workflows/conda-forge-ci.yml @@ -0,0 +1,67 @@ +name: C++ CI Workflow with conda-forge dependencies + +on: + push: + pull_request: + schedule: + # * is a special character in YAML so you have to quote this string + # Execute a "nightly" build at 2 AM UTC + - cron: '0 2 * * *' + +jobs: + build: + name: '[${{ matrix.os }}@${{ matrix.build_type }}@conda]' + runs-on: ${{ matrix.os }} + strategy: + matrix: + build_type: [Release] + os: [ubuntu-latest, windows-2019] + fail-fast: false + + steps: + - uses: actions/checkout@v2 + + - uses: conda-incubator/setup-miniconda@v2 + with: + miniforge-variant: Mambaforge + miniforge-version: latest + + - name: Dependencies + shell: bash -l {0} + run: | + # Compilation related dependencies + mamba install cmake compilers make ninja pkg-config + # Actual dependencies + mamba install glew glfw eigen yarp imgui + + - name: OpenGL [Linux] + if: contains(matrix.os, 'ubuntu') + shell: bash -l {0} + run: | + # OpenGL is not found on Ubuntu when using conda. Related issue https://github.com/robotology/robotology-superbuild/issues/929 + # See https://github.com/robotology/robotology-superbuild/issues/477 + # See https://github.com/robotology/robotology-superbuild/pull/1606 + mamba install expat freeglut libselinux-cos7-x86_64 xorg-libxau libxcb xorg-libxdamage xorg-libxext xorg-libxfixes xorg-libxxf86vm xorg-libxrandr mesa-libgl-cos7-x86_64 mesa-libgl-devel-cos7-x86_64 + + - name: Configure [Linux] + if: contains(matrix.os, 'ubuntu') + shell: bash -l {0} + run: | + mkdir -p build + cd build + cmake -GNinja -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} .. + + - name: Configure [Windows] + if: contains(matrix.os, 'windows') + shell: bash -l {0} + run: | + mkdir -p build + cd build + cmake -G"Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} .. + + - name: Build + shell: bash -l {0} + run: | + cd build + cmake --build . --config ${{ matrix.build_type }} + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6baefa4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# build folder +build/* + +# emacs +*~ +\#*\# + +# Qtcreator +*.user* +*.autosave + +# Visual Studio +.vs/ +CmakeSettings.json diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..518f586 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,121 @@ +# Copyright (C) 2024 Istituto Italiano di Tecnologia (IIT) +# All rights reserved. +# +# This software may be modified and distributed under the terms of the +# BSD-2-Clause license. See the accompanying LICENSE file for details. + +cmake_minimum_required(VERSION 3.12) + +project(yarp-device-keyboard-joypad + LANGUAGES C CXX + VERSION 0.0.1) + +# Defines the CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_BINDIR and many other useful macros. +# See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html +include(GNUInstallDirs) + +# Control where libraries and executables are placed during the build. +# With the following settings executables are placed in /bin and libraries/archives in /lib. +# This is particularly useful to run ctests on libraries built on Windows +# machines: tests, which are executables, are placed in the same folders of +# dlls, which are treated as executables as well, so that they can properly +# find the libraries to run. This is a because of missing RPATH on Windows. +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}") +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}") + +# To build shared libraries in Windows, we set CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS to TRUE. +# See https://cmake.org/cmake/help/v3.4/variable/CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS.html +# See https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/ +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + +# Under MSVC, we set CMAKE_DEBUG_POSTFIX to "d" to add a trailing "d" to library +# built in debug mode. In this Windows user can compile, build and install the +# library in both Release and Debug configuration avoiding naming clashes in the +# installation directories. +if(MSVC) + set(CMAKE_DEBUG_POSTFIX "d") + #_USE_MATH_DEFINES is to have constants like M_PI defined also on Windows + add_definitions(-D_USE_MATH_DEFINES) + #NOMINMAX is to avoid windows.h defining its own versions of min and max + add_definitions(-DNOMINMAX) +endif() + +# Build position independent code. +# Position Independent Code (PIC) is commonly used for shared libraries so that +# the same shared library code can be loaded in each program address space in a +# location where it will not overlap with any other uses of such memory. +# In particular, this option avoids problems occurring when a process wants to +# load more than one shared library at the same virtual address. +# Since shared libraries cannot predict where other shared libraries could be +# loaded, this is an unavoidable problem with the traditional shared library +# concept. +# Generating position-independent code is often the default behavior for most +# modern compilers. +# Moreover linking a static library that is not built with PIC from a shared +# library will fail on some compiler/architecture combinations. +# Further details on PIC can be found here: +# https://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/ +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# Disable C and C++ compiler extensions. +# C/CXX_EXTENSIONS are ON by default to allow the compilers to use extended +# variants of the C/CXX language. +# However, this could expose cross-platform bugs in user code or in the headers +# of third-party dependencies and thus it is strongly suggested to turn +# extensions off. +set(CMAKE_C_EXTENSIONS OFF) +set(CMAKE_CXX_EXTENSIONS OFF) + + +### Dependencies +find_package(YCM REQUIRED) +find_package(YARP 3.4 COMPONENTS os sig dev math idl_tools REQUIRED) +find_package(Threads REQUIRED) +find_package(glfw3 REQUIRED NO_MODULE) +find_package(GLEW REQUIRED) #Helps with the OpenGL configuration on Windows + +find_package(imgui QUIET) +if(NOT imgui_FOUND) + message(STATUS "imgui not found, using vendored version") + set(USE_VENDORED_IMGUI ON) +endif() + +## This is to select the newest version of OpenGL +if (POLICY CMP0072) + cmake_policy (SET CMP0072 NEW) +endif(POLICY CMP0072) + +find_package(OpenGL REQUIRED) + +if (NOT WIN32) + find_package(X11 REQUIRED) +endif() + +# Encourage user to specify a build type (e.g. Release, Debug, etc.), otherwise set it to Release. +if(NOT CMAKE_CONFIGURATION_TYPES) + if(NOT CMAKE_BUILD_TYPE) + message(STATUS "Setting build type to 'Release' as none was specified.") + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY VALUE "Release") + endif() +endif() + +if (NOT "${CMAKE_SIZEOF_VOID_P}" EQUAL "8") + message( FATAL_ERROR "Only 64 bit builds supported." ) +endif() + +set(YARP_FORCE_DYNAMIC_PLUGINS TRUE CACHE INTERNAL "yarp-device-keyboard-joypad is always built with dynamic plugins") +set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Build libraries as shared as opposed to static") + +include(AddInstallRPATHSupport) +add_install_rpath_support(BIN_DIRS "${CMAKE_INSTALL_FULL_BINDIR}" + LIB_DIRS "${CMAKE_INSTALL_FULL_LIBDIR}" + INSTALL_NAME_DIR "${CMAKE_INSTALL_FULL_LIBDIR}" + USE_LINK_PATH) + +include(AddUninstallTarget) + +yarp_configure_plugins_installation(yarp-device-keyboard-joypad) + +add_subdirectory(src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..124d714 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (C) 2024 Istituto Italiano di Tecnologia (IIT) +# All rights reserved. +# +# This software may be modified and distributed under the terms of the +# BSD-2-Clause license. See the accompanying LICENSE file for details. + +add_subdirectory(vendor) +add_subdirectory(devices) diff --git a/src/devices/CMakeLists.txt b/src/devices/CMakeLists.txt new file mode 100644 index 0000000..507367f --- /dev/null +++ b/src/devices/CMakeLists.txt @@ -0,0 +1,7 @@ +# Copyright (C) 2024 Istituto Italiano di Tecnologia (IIT) +# All rights reserved. +# +# This software may be modified and distributed under the terms of the +# BSD-2-Clause license. See the accompanying LICENSE file for details. + +add_subdirectory(keyboard-joypad) diff --git a/src/devices/keyboard-joypad/CMakeLists.txt b/src/devices/keyboard-joypad/CMakeLists.txt new file mode 100644 index 0000000..afeb353 --- /dev/null +++ b/src/devices/keyboard-joypad/CMakeLists.txt @@ -0,0 +1,72 @@ +# Copyright (C) 2024 Istituto Italiano di Tecnologia (IIT) +# All rights reserved. +# +# This software may be modified and distributed under the terms of the +# BSD-2-Clause license. See the accompanying LICENSE file for details. + +set(DEPENDS_STRING "glfw3_FOUND;GLEW_FOUND;OpenGL_FOUND") + +if (NOT WIN32) + string(APPEND DEPENDS_STRING ";X11_FOUND") +endif() + +yarp_prepare_plugin(keyboardJoypad + CATEGORY device + TYPE yarp::dev::KeyboardJoypad + INCLUDE KeyboardJoypad.h + DEPENDS ${DEPENDS_STRING} + INTERNAL + QUIET +) + +if(NOT ENABLE_keyboard-joypad) + return() +endif() + +set(yarp_keyboard-joypad_SRCS + KeyboardJoypad.cpp +) + +set(yarp_keyboard-joypad_HDRS + KeyboardJoypad.h + KeyboardJoypadLogComponent.h +) + + +yarp_add_plugin(yarp_keyboard-joypad) + + +target_sources(yarp_keyboard-joypad + PRIVATE + ${yarp_keyboard-joypad_SRCS} + ${yarp_keyboard-joypad_HDRS} +) + +target_link_libraries(yarp_keyboard-joypad + PRIVATE + YARP::YARP_os + YARP::YARP_sig + YARP::YARP_dev + YARP::YARP_math + glfw + GLEW::GLEW + OpenGL::GL + imgui::imgui +) + +if (NOT WIN32) + target_link_libraries(yarp_keyboard-joypad PRIVATE ${X11_LIBRARIES}) +endif() + +target_include_directories(yarp_keyboard-joypad PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +target_compile_features(yarp_keyboard-joypad PUBLIC cxx_std_20) #C++20 is used for the designated initialization of structs + +yarp_install( + TARGETS yarp_keyboard-joypad + EXPORT yarp-device-keyboard-joypad + COMPONENT yarp-device-keyboard-joypad + LIBRARY DESTINATION ${YARP_DYNAMIC_PLUGINS_INSTALL_DIR} + ARCHIVE DESTINATION ${YARP_STATIC_PLUGINS_INSTALL_DIR} + YARP_INI DESTINATION ${YARP_PLUGIN_MANIFESTS_INSTALL_DIR} +) diff --git a/src/devices/keyboard-joypad/KeyboardJoypad.cpp b/src/devices/keyboard-joypad/KeyboardJoypad.cpp new file mode 100644 index 0000000..187716e --- /dev/null +++ b/src/devices/keyboard-joypad/KeyboardJoypad.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2021 Istituto Italiano di Tecnologia (IIT) + * All rights reserved. + * + * This software may be modified and distributed under the terms of the + * BSD-2-Clause license. See the accompanying LICENSE file for details. + */ + +#include + +#include +#include + +yarp::dev::KeyboardJoypad::KeyboardJoypad() + : yarp::dev::DeviceDriver(), + yarp::os::PeriodicThread(0.01, yarp::os::ShouldUseSystemClock::Yes) +{ +} + +yarp::dev::KeyboardJoypad::~KeyboardJoypad() +{ +} + +bool yarp::dev::KeyboardJoypad::open(yarp::os::Searchable& cfg) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::close() +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::threadInit() +{ + return false; +} + +void yarp::dev::KeyboardJoypad::threadRelease() +{ +} + +void yarp::dev::KeyboardJoypad::run() +{ +} + +bool yarp::dev::KeyboardJoypad::startService() +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::updateService() +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::stopService() +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getAxisCount(unsigned int& axis_count) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getButtonCount(unsigned int& button_count) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getTrackballCount(unsigned int& trackball_count) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getHatCount(unsigned int& hat_count) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getTouchSurfaceCount(unsigned int& touch_count) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getStickCount(unsigned int& stick_count) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getStickDoF(unsigned int stick_id, unsigned int& dof) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getButton(unsigned int button_id, float& value) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getTrackball(unsigned int trackball_id, yarp::sig::Vector& value) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getHat(unsigned int hat_id, unsigned char& value) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getAxis(unsigned int axis_id, double& value) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) +{ + return false; +} + +bool yarp::dev::KeyboardJoypad::getTouch(unsigned int touch_id, yarp::sig::Vector& value) +{ + return false; +} diff --git a/src/devices/keyboard-joypad/KeyboardJoypad.h b/src/devices/keyboard-joypad/KeyboardJoypad.h new file mode 100644 index 0000000..df0bc68 --- /dev/null +++ b/src/devices/keyboard-joypad/KeyboardJoypad.h @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2024 Istituto Italiano di Tecnologia (IIT) + * All rights reserved. + * + * This software may be modified and distributed under the terms of the + * BSD-2-Clause license. See the accompanying LICENSE file for details. + */ + +#ifndef YARP_DEV_KEYBOARDJOYPAD_H +#define YARP_DEV_KEYBOARDJOYPAD_H + +#include +#include + +#include +#include +#include +#include + +namespace yarp { + namespace dev { + class KeyboardJoypad; + } +} + +class yarp::dev::KeyboardJoypad : public yarp::dev::DeviceDriver, + public yarp::os::PeriodicThread, + public yarp::dev::IService, + public yarp::dev::IJoypadController +{ +public: + KeyboardJoypad(); + + virtual ~KeyboardJoypad(); + + // yarp::dev::DeviceDriver methods + virtual bool open(yarp::os::Searchable& cfg) override; + virtual bool close() override; + + // yarp::os::RateThread methods + virtual bool threadInit() override; + virtual void threadRelease() override; + virtual void run() override; + + // yarp::dev::IService methods + virtual bool startService() override; + virtual bool updateService() override; + virtual bool stopService() override; + + // yarp::dev::IJoypadController methods + virtual bool getAxisCount(unsigned int& axis_count) override; + virtual bool getButtonCount(unsigned int& button_count) override; + virtual bool getTrackballCount(unsigned int& trackball_count) override; + virtual bool getHatCount(unsigned int& hat_count) override; + virtual bool getTouchSurfaceCount(unsigned int& touch_count) override; + virtual bool getStickCount(unsigned int& stick_count) override; + virtual bool getStickDoF(unsigned int stick_id, unsigned int& dof) override; + virtual bool getButton(unsigned int button_id, float& value) override; + virtual bool getTrackball(unsigned int trackball_id, yarp::sig::Vector& value) override; + virtual bool getHat(unsigned int hat_id, unsigned char& value) override; + virtual bool getAxis(unsigned int axis_id, double& value) override; + virtual bool getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) override; + virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector& value) override; + +private: + + std::atomic_bool m_closed{ false }; + + std::mutex m_mutex; + +}; + + +#endif // YARP_DEV_KEYBOARDJOYPAD_H diff --git a/src/devices/keyboard-joypad/KeyboardJoypadLogComponent.h b/src/devices/keyboard-joypad/KeyboardJoypadLogComponent.h new file mode 100644 index 0000000..7fde099 --- /dev/null +++ b/src/devices/keyboard-joypad/KeyboardJoypadLogComponent.h @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2024 Istituto Italiano di Tecnologia (IIT) + * All rights reserved. + * + * This software may be modified and distributed under the terms of the + * BSD-2-Clause license. See the accompanying LICENSE file for details. + */ + +#ifndef YARP_DEV_KEYBOARDJOYPADLOGCOMPONENT_H +#define YARP_DEV_KEYBOARDJOYPADLOGCOMPONENT_H + +#include + +YARP_DECLARE_LOG_COMPONENT(KEYBOARDJOYPAD) + +#endif // YARP_DEV_KEYBOARDJOYPADLOGCOMPONENT_H \ No newline at end of file diff --git a/src/vendor/CMakeLists.txt b/src/vendor/CMakeLists.txt new file mode 100644 index 0000000..77b5d61 --- /dev/null +++ b/src/vendor/CMakeLists.txt @@ -0,0 +1,7 @@ +# Copyright (C) 2024 Istituto Italiano di Tecnologia (IIT) +# All rights reserved. +# +# This software may be modified and distributed under the terms of the +# BSD-2-Clause license. See the accompanying LICENSE file for details. + +add_subdirectory(dearimgui) diff --git a/src/vendor/dearimgui/CMakeLists.txt b/src/vendor/dearimgui/CMakeLists.txt new file mode 100644 index 0000000..a4970cd --- /dev/null +++ b/src/vendor/dearimgui/CMakeLists.txt @@ -0,0 +1,34 @@ +# Copyright (C) 2024 Istituto Italiano di Tecnologia (IIT) +# All rights reserved. +# +# This software may be modified and distributed under the terms of the +# BSD-2-Clause license. See the accompanying LICENSE file for details. + +if (NOT USE_VENDORED_IMGUI) + return() +endif() + +include(FetchContent) + +set(imgui_TAG v1.90.4) +set(IMGUI_BUILD_GLFW_BINDING ON) +set(imgui-feedstock_TAG 7f402e0ac895620420310a7e4791860a2dc6e79d) + +FetchContent_Declare( + imgui + GIT_REPOSITORY https://github.com/ocornut/imgui + GIT_TAG ${imgui_TAG} + GIT_SHALLOW 1 +) + +FetchContent_GetProperties(imgui) +if(NOT imgui_POPULATED) + message(STATUS "Fetching imgui(${imgui_TAG})...") + FetchContent_Populate(imgui) + set(CMAKELIST_URL "https://raw.githubusercontent.com/conda-forge/imgui-feedstock/${imgui-feedstock_TAG}/recipe") + message (STATUS "Downloading ${CMAKELIST_URL}/CMakeLists.txt to ${imgui_SOURCE_DIR}/CMakeLists.txt") + file(DOWNLOAD "${CMAKELIST_URL}/CMakeLists.txt" "${imgui_SOURCE_DIR}/CMakeLists.txt") + message (STATUS "Downloading ${CMAKELIST_URL}/imgui-config.cmake.in to ${imgui_SOURCE_DIR}/imgui-config.cmake.in") + file(DOWNLOAD "${CMAKELIST_URL}/imgui-config.cmake.in" "${imgui_SOURCE_DIR}/imgui-config.cmake.in") + add_subdirectory(${imgui_SOURCE_DIR} ${imgui_BINARY_DIR}) +endif() \ No newline at end of file