Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added first basic version. #1

Merged
merged 3 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# 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.

cmake_minimum_required(VERSION 3.10)

project(yarp-device-openxrheadset
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 <the top level of the
# build tree>/bin and libraries/archives in <top level of the build tree>/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")
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 REQUIRED)
find_package(Threads REQUIRED)
find_package(OpenXR 1.0 REQUIRED)
find_package(glfw3 REQUIRED)
## 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-openxrheadset 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-openxrheadset)

add_subdirectory(src)
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 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.

add_subdirectory(devices)
8 changes: 8 additions & 0 deletions src/devices/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 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.

add_subdirectory(openxrheadset)

69 changes: 69 additions & 0 deletions src/devices/openxrheadset/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.

set(DEPENDS_STRING "OpenXR_FOUND;glfw3_FOUND;OpenGL_FOUND")

if (NOT WIN32)
string(APPEND DEPENDS_STRING ";X11_FOUND")
endif()

yarp_prepare_plugin(openxrheadset
CATEGORY device
TYPE yarp::dev::OpenXrHeadset
INCLUDE OpenXrHeadset.h
DEPENDS ${DEPENDS_STRING}
INTERNAL
QUIET
)

if(NOT ENABLE_openxrheadset)
return()
endif()

yarp_add_plugin(yarp_openxrheadset)

set(yarp_openxrheadset_SRCS
OpenXrHeadset.cpp
OpenXrHeadsetLogComponent.cpp
OpenXrInterface.cpp
)

set(yarp_openxrheadset_HDRS
OpenXrConfig.h
OpenXrHeadset.h
OpenXrHeadsetLogComponent.h
OpenXrInterface.h
)

target_sources(yarp_openxrheadset
PRIVATE
${yarp_openxrheadset_SRCS}
${yarp_openxrheadset_HDRS}
)

target_link_libraries(yarp_openxrheadset
PRIVATE
YARP::YARP_os
YARP::YARP_sig
YARP::YARP_dev
YARP::YARP_math
OpenXR::openxr_loader
glfw
OpenGL::GL
)

if (NOT WIN32)
target_link_libraries(yarp_openxrheadset PRIVATE ${X11_LIBRARIES})
endif()

yarp_install(
TARGETS yarp_openxrheadset
EXPORT yarp-device-openxrheadset
COMPONENT yarp-device-openxrheadset
LIBRARY DESTINATION ${YARP_DYNAMIC_PLUGINS_INSTALL_DIR}
ARCHIVE DESTINATION ${YARP_STATIC_PLUGINS_INSTALL_DIR}
YARP_INI DESTINATION ${YARP_PLUGIN_MANIFESTS_INSTALL_DIR}
)
41 changes: 41 additions & 0 deletions src/devices/openxrheadset/OpenXrConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/

#ifndef YARP_DEV_OPENXRCONFIG_H
#define YARP_DEV_OPENXRCONFIG_H

#define XR_USE_GRAPHICS_API_OPENGL
#define GL_GLEXT_PROTOTYPES
#define GL3_PROTOTYPES

#include <GL/gl.h>
#include <GL/glext.h>

#if defined(_WIN32)
#define XR_USE_PLATFORM_WIN32
#define GLFW_EXPOSE_NATIVE_WIN32
#define GLFW_EXPOSE_NATIVE_WGL
#elif defined(__APPLE__)
#define XR_USE_PLATFORM_XLIB
#define GLFW_EXPOSE_NATIVE_COCOA
#define GLFW_EXPOSE_NATIVE_NSGL
#include <GL/glx.h>
#elif defined(__linux__)
#define XR_USE_PLATFORM_XLIB
#define GLFW_EXPOSE_NATIVE_X11
#define GLFW_EXPOSE_NATIVE_GLX
#include <GL/glx.h>
#endif

#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>

#include <openxr/openxr.h>
#include <openxr/openxr_platform.h>

#endif // YARP_DEV_OPENXRCONFIG_H
114 changes: 114 additions & 0 deletions src/devices/openxrheadset/OpenXrHeadset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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 <yarp/os/LogStream.h>

#include <map>
#include <algorithm>
#include <csignal>

#include "OpenXrHeadset.h"
#include "OpenXrHeadsetLogComponent.h"

typedef bool(yarp::os::Value::*valueIsType)(void) const;


yarp::dev::OpenXrHeadset::OpenXrHeadset()
: yarp::dev::DeviceDriver(),
yarp::os::PeriodicThread(0.011, yarp::os::ShouldUseSystemClock::Yes) // ~90 fps
{
yCTrace(OPENXRHEADSET);

}

yarp::dev::OpenXrHeadset::~OpenXrHeadset()
{
yCTrace(OPENXRHEADSET);
}

bool yarp::dev::OpenXrHeadset::open(yarp::os::Searchable &/*cfg*/)
{
yCTrace(OPENXRHEADSET);

// Start the thread
if (!this->start()) {
yCError(OPENXRHEADSET) << "Thread start failed, aborting.";
this->close();
return false;
}

return true;
}

bool yarp::dev::OpenXrHeadset::close()
{
yCTrace(OPENXRHEADSET);
this->askToStop();
return true;
}

bool yarp::dev::OpenXrHeadset::threadInit()
{
yCTrace(OPENXRHEADSET);
if (!openXrInterface.initialize())
{
yCError(OPENXRHEADSET) << "Failed to initialize OpenXr interface.";
return false;
}

return true;
}

void yarp::dev::OpenXrHeadset::threadRelease()
{
yCTrace(OPENXRHEADSET);
if (closed)
return;

openXrInterface.close();

std::raise(SIGTERM);

closed = true;
}

void yarp::dev::OpenXrHeadset::run()
{
yCTrace(OPENXRHEADSET);

if (openXrInterface.isRunning())
{
openXrInterface.draw();
}
else
{
close();
return;
}
}

bool yarp::dev::OpenXrHeadset::startService()
{
yCTrace(OPENXRHEADSET);
//To let the device driver knowing that it need to poll updateService continuosly
return false;
}

bool yarp::dev::OpenXrHeadset::updateService()
{
yCTrace(OPENXRHEADSET);
//To let the device driver that we are still alive
return !closed;
}

bool yarp::dev::OpenXrHeadset::stopService()
{
yCTrace(OPENXRHEADSET);
return this->close();
}

Loading