mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2024-12-29 11:06:18 +00:00
os: Add a minimal hidraw wrapper
This commit is contained in:
parent
d9cdd04b7a
commit
e2bd986bc5
|
@ -10,6 +10,12 @@ set(MATH_SOURCE_FILES
|
|||
math/m_quatexpmap.cpp
|
||||
)
|
||||
|
||||
set(OS_SOURCE_FILES
|
||||
os/os_documentation.h
|
||||
os/os_hid.h
|
||||
os/os_hid_hidraw.c
|
||||
)
|
||||
|
||||
set(UTIL_SOURCE_FILES
|
||||
util/u_misc.c
|
||||
util/u_misc.h
|
||||
|
@ -35,6 +41,10 @@ include_directories(
|
|||
add_library(aux_util OBJECT ${UTIL_SOURCE_FILES})
|
||||
set_property(TARGET aux_util PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
# OS library.
|
||||
add_library(aux_os OBJECT ${OS_SOURCE_FILES})
|
||||
set_property(TARGET aux_os PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
# Math library.
|
||||
# Use OBJECT to not create a archive, since it just gets in the way.
|
||||
add_library(aux_math OBJECT ${MATH_SOURCE_FILES})
|
||||
|
|
25
src/xrt/auxiliary/os/os_documentation.h
Normal file
25
src/xrt/auxiliary/os/os_documentation.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Documentation.
|
||||
* @author Jakob Bornecrantz <jakob@collabora.com>
|
||||
* @ingroup aux_os
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
/*!
|
||||
* @defgroup aux_os OS-Wrappers
|
||||
* @ingroup aux
|
||||
*
|
||||
* @brief Wrapper around OS functions and structs.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @dir auxiliary/os
|
||||
* @ingroup aux
|
||||
*
|
||||
* @brief Wrapper around OS functions and structs.
|
||||
*/
|
71
src/xrt/auxiliary/os/os_hid.h
Normal file
71
src/xrt/auxiliary/os/os_hid.h
Normal file
|
@ -0,0 +1,71 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Wrapper around OS native hid functions.
|
||||
* @author Jakob Bornecrantz <jakob@collabora.com>
|
||||
*
|
||||
* @ingroup aux_os
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "xrt/xrt_config.h"
|
||||
#include "xrt/xrt_compiler.h"
|
||||
|
||||
|
||||
/*!
|
||||
* Representing a single hid interface on a device.
|
||||
*/
|
||||
struct os_hid_device
|
||||
{
|
||||
int (*read)(struct os_hid_device *hid_dev,
|
||||
uint8_t *data,
|
||||
size_t size,
|
||||
int milliseconds);
|
||||
|
||||
int (*write)(struct os_hid_device *hid_dev,
|
||||
const uint8_t *data,
|
||||
size_t size);
|
||||
|
||||
void (*destroy)(struct os_hid_device *hid_dev);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Read from the given hid device, if milliseconds are negative blocks, 0 polls
|
||||
* and positive will block for that amount of seconds.
|
||||
*/
|
||||
XRT_MAYBE_UNUSED static inline int
|
||||
os_hid_read(struct os_hid_device *hid_dev,
|
||||
uint8_t *data,
|
||||
size_t size,
|
||||
int milliseconds)
|
||||
{
|
||||
return hid_dev->read(hid_dev, data, size, milliseconds);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Write to the given device.
|
||||
*/
|
||||
XRT_MAYBE_UNUSED static inline int
|
||||
os_hid_write(struct os_hid_device *hid_dev, const uint8_t *data, size_t size)
|
||||
{
|
||||
return hid_dev->write(hid_dev, data, size);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Close and free the given device.
|
||||
*/
|
||||
XRT_MAYBE_UNUSED static inline void
|
||||
os_hid_destroy(struct os_hid_device *hid_dev)
|
||||
{
|
||||
hid_dev->destroy(hid_dev);
|
||||
}
|
||||
|
||||
#ifdef XRT_OS_LINUX
|
||||
/*!
|
||||
* Open the given path as a hidraw device.
|
||||
*/
|
||||
int
|
||||
os_hid_open_hidraw(const char *path, struct os_hid_device **out_hid);
|
||||
#endif
|
106
src/xrt/auxiliary/os/os_hid_hidraw.c
Normal file
106
src/xrt/auxiliary/os/os_hid_hidraw.c
Normal file
|
@ -0,0 +1,106 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Hid implementation based on hidraw.
|
||||
* @author Jakob Bornecrantz <jakob@collabora.com>
|
||||
* @ingroup aux_os
|
||||
*/
|
||||
|
||||
#include "os_hid.h"
|
||||
|
||||
#ifdef XRT_OS_LINUX
|
||||
|
||||
#include "util/u_misc.h"
|
||||
#include <poll.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <linux/hidraw.h>
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct hid_hidraw
|
||||
{
|
||||
struct os_hid_device base;
|
||||
|
||||
int fd;
|
||||
};
|
||||
|
||||
static int
|
||||
os_hidraw_read(struct os_hid_device *ohdev,
|
||||
uint8_t *data,
|
||||
size_t length,
|
||||
int milliseconds)
|
||||
{
|
||||
struct hid_hidraw *hrdev = (struct hid_hidraw *)ohdev;
|
||||
struct pollfd fds;
|
||||
int ret;
|
||||
|
||||
if (milliseconds >= 0) {
|
||||
fds.fd = hrdev->fd;
|
||||
fds.events = POLLIN;
|
||||
fds.revents = 0;
|
||||
ret = poll(&fds, 1, milliseconds);
|
||||
|
||||
if (ret == -1 || ret == 0) {
|
||||
// Error or timeout.
|
||||
return ret;
|
||||
} else if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
// Device disconnect?
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
ret = read(hrdev->fd, data, length);
|
||||
|
||||
if (ret < 0 && (errno == EAGAIN || errno == EINPROGRESS)) {
|
||||
// Process most likely received a signal.
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
os_hidraw_write(struct os_hid_device *ohdev, const uint8_t *data, size_t length)
|
||||
{
|
||||
struct hid_hidraw *hrdev = (struct hid_hidraw *)ohdev;
|
||||
|
||||
return write(hrdev->fd, data, length);
|
||||
}
|
||||
|
||||
static void
|
||||
os_hidraw_destroy(struct os_hid_device *ohdev)
|
||||
{
|
||||
struct hid_hidraw *hrdev = (struct hid_hidraw *)ohdev;
|
||||
|
||||
close(hrdev->fd);
|
||||
free(hrdev);
|
||||
}
|
||||
|
||||
int
|
||||
os_hid_open_hidraw(const char *path, struct os_hid_device **out_ohdev)
|
||||
{
|
||||
struct hid_hidraw *hrdev = U_TYPED_CALLOC(struct hid_hidraw);
|
||||
|
||||
hrdev->base.read = os_hidraw_read;
|
||||
hrdev->base.write = os_hidraw_write;
|
||||
hrdev->base.destroy = os_hidraw_destroy;
|
||||
hrdev->fd = open(path, O_RDWR);
|
||||
if (hrdev->fd < 0) {
|
||||
free(hrdev);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out_ohdev = &hrdev->base;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -61,6 +61,7 @@ add_library(${RUNTIME_TARGET} SHARED
|
|||
${MANIFEST_DEV_PATH}
|
||||
${MANIFEST_PATH}
|
||||
${SOURCE_FILES}
|
||||
$<TARGET_OBJECTS:aux_os>
|
||||
$<TARGET_OBJECTS:aux_util>
|
||||
$<TARGET_OBJECTS:aux_math>
|
||||
$<TARGET_OBJECTS:comp>
|
||||
|
|
Loading…
Reference in a new issue