Permalink
Browse files

C++17

  • Loading branch information...
mlang committed Aug 2, 2017
1 parent 8b837ff commit 02864d6fab654da9ec2841fb85996b9b59754517
Showing with 118 additions and 33 deletions.
  1. +1 −1 CMakeLists.txt
  2. +2 −0 brltest.cpp
  3. +0 −1 cv2midiclock.cpp
  4. +81 −17 lib/brlapi.cpp
  5. +22 −5 lib/brlapi.hpp
  6. +7 −4 lib/jack.cpp
  7. +5 −5 lib/jack.hpp
View
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.7)
project(BrlCV CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
View
@@ -18,7 +18,9 @@ int main() {
TTY.writeText(Text);
auto KeyCode = TTY.readKey();
Text << " and pressed key " << KeyCode.group() << " " << KeyCode.number() << " " << KeyCode.press();
auto Key = BrlAPI::Driver::HandyTech::fromKeyCode(KeyCode);
TTY.writeText(Text);
std::this_thread::sleep_for(5s);
}
}
View
@@ -1,7 +1,6 @@
#include <chrono>
#include <thread>
#include <boost/lockfree/spsc_queue.hpp>
#include <jack.hpp>
View
@@ -1,6 +1,41 @@
#include "brlapi.hpp"
#include <brlapi.h>
#define PACKED
#include <brltty/brldefs-ht.h>
#undef PACKED
#include <netdb.h>
namespace {
struct BrlAPIErrorCategory : std::error_category {
const char* name() const noexcept override { return "BrlAPI"; }
std::string message(int ev) const override {
if (ev >= brlapi_nerr) return "Unknown errror";
return brlapi_errlist[ev];
}
};
const BrlAPIErrorCategory BrlAPICategory {};
void throwSystemError() {
switch (brlapi_errno) {
case BRLAPI_ERROR_LIBCERR:
throw std::system_error(brlapi_libcerrno, std::generic_category());
case BRLAPI_ERROR_GAIERR:
switch (brlapi_gaierrno) {
case EAI_SYSTEM:
throw std::system_error(brlapi_libcerrno, std::generic_category());
default:
throw std::runtime_error("Unresolved GAI error");
}
default:
throw std::system_error(brlapi_errno, BrlAPIErrorCategory());
}
}
} // namespace
namespace BrlAPI {
@@ -35,38 +70,63 @@ std::ostream &operator<<(std::ostream &Out, DisplaySize const &Size) {
} // namespace BrlAPI
namespace BrlAPI::Driver {
HandyTech::Key HandyTech::fromKeyCode(BrlAPI::KeyCode const &Key) {
switch (static_cast<HT_KeyGroup>(Key.group())) {
case HT_GRP_NavigationKeys:
switch (static_cast<HT_NavigationKey>(Key.number())) {
case HT_KEY_B1: return NavigationKey::B1;
case HT_KEY_B2: return NavigationKey::B2;
case HT_KEY_B3: return NavigationKey::B3;
case HT_KEY_B4: return NavigationKey::B4;
case HT_KEY_B5: return NavigationKey::B5;
case HT_KEY_B6: return NavigationKey::B6;
case HT_KEY_B7: return NavigationKey::B7;
case HT_KEY_B8: return NavigationKey::B8;
case HT_KEY_Space: return NavigationKey::LeftSpace;
case HT_KEY_SpaceRight: return NavigationKey::RightSpace;
default: throw std::runtime_error("Unknown HandyTech navigation key " + std::to_string(Key.number()));
}
case HT_GRP_RoutingKeys: return RoutingKey(Key.number());
default: throw std::runtime_error("Unknown HandyTech key group");
}
}
}
class BrlAPI::Connection::Implementation {
std::unique_ptr<gsl::byte[]> HandleStorage;
std::unique_ptr<std::byte[]> HandleStorage;
public:
Implementation() : HandleStorage(new gsl::byte[brlapi_getHandleSize()]) {}
Implementation() : HandleStorage(new std::byte[brlapi_getHandleSize()]) {}
brlapi_handle_t *handle() const {
return reinterpret_cast<brlapi_handle_t *>(HandleStorage.get());
}
};
BrlAPI::TTY::~TTY() {
brlapi__leaveTtyMode(Conn->BrlAPI->handle());
brlapi__leaveTtyMode(Conn.BrlAPI->handle());
}
void BrlAPI::TTY::writeText(std::string Text) {
if (brlapi__writeText(Conn->BrlAPI->handle(), -1, Text.c_str()) == -1) {
throw std::system_error(brlapi_errno, std::generic_category());
if (brlapi__writeText(Conn.BrlAPI->handle(), -1, Text.c_str()) == -1) {
throwSystemError();
}
}
BrlAPI::KeyCode BrlAPI::TTY::readKey() const {
brlapi_keyCode_t Key;
if (brlapi__readKey(Conn->BrlAPI->handle(), 1, &Key) == -1) {
throw std::system_error(brlapi_errno, std::generic_category());
if (brlapi__readKey(Conn.BrlAPI->handle(), 1, &Key) == -1) {
throwSystemError();
}
return { Key };
}
bool BrlAPI::TTY::readKey(KeyCode &KeyCode) const {
brlapi_keyCode_t Key;
auto Result = brlapi__readKey(Conn->BrlAPI->handle(), 0, &Key);
auto Result = brlapi__readKey(Conn.BrlAPI->handle(), 0, &Key);
if (Result == -1) {
throw std::system_error(brlapi_errno, std::generic_category());
throwSystemError();
}
if (Result == 1) {
KeyCode = BrlAPI::KeyCode(Key);
@@ -78,7 +138,7 @@ BrlAPI::Connection::Connection() : BrlAPI(std::make_unique<Implementation>()) {
brlapi_connectionSettings_t Settings = BRLAPI_SETTINGS_INITIALIZER;
auto FileDescriptor = brlapi__openConnection(BrlAPI->handle(), &Settings, &Settings);
if (FileDescriptor == -1) {
throw std::system_error(brlapi_errno, std::generic_category());
throwSystemError();
}
}
@@ -104,15 +164,19 @@ BrlAPI::DisplaySize BrlAPI::Connection::displaySize() const {
BrlAPI::TTY BrlAPI::Connection::tty(int TTY, bool Raw) {
auto Number = brlapi__enterTtyMode(BrlAPI->handle(), TTY, Raw? "HandyTech" : "");
if (Number == -1) {
throw std::system_error(brlapi_errno, std::generic_category());
throwSystemError();
}
brlapi_range_t Ranges[] = {
{ 0, std::numeric_limits<brlapi_keyCode_t>::max() }
{ std::numeric_limits<brlapi_keyCode_t>::min()
, std::numeric_limits<brlapi_keyCode_t>::max()
}
};
brlapi__acceptKeyRanges(
BrlAPI->handle(),
Ranges, std::distance(std::begin(Ranges), std::end(Ranges))
);
return { this, Number };
if (brlapi__acceptKeyRanges(
BrlAPI->handle(),
Ranges, std::distance(std::begin(Ranges), std::end(Ranges))
) == -1) {
throwSystemError();
}
return { *this, Number };
}
View
@@ -3,7 +3,9 @@
#include <ostream>
#include <sstream>
#include <variant>
#include <gsl/gsl>
#include <boost/serialization/strong_typedef.hpp>
namespace BrlAPI {
@@ -25,25 +27,38 @@ class Connection;
class KeyCode {
std::uint8_t Group, Number;
bool Press;
friend class TTY;
KeyCode(std::uint64_t Code)
public:
KeyCode(std::uint8_t Group, std::uint8_t Number, bool Press) noexcept
: Group(Group), Number(Number), Press(Press) {}
KeyCode(std::uint64_t Code) noexcept
: Group((Code >> 8) & 0b11111111)
, Number(Code & 0b11111111)
, Press(Code >> 63) {}
public:
auto group() const noexcept { return Group; }
auto number() const noexcept { return Number; }
auto press() const noexcept { return Press; }
};
namespace Driver {
class HandyTech {
public:
enum class NavigationKey {
B1, B2, B3, B4, B5, B6, B7, B8, LeftSpace, RightSpace
};
BOOST_STRONG_TYPEDEF(std::uint8_t, RoutingKey)
using Key = std::variant<NavigationKey, RoutingKey>;
static Key fromKeyCode(KeyCode const &);
};
} // namespace Driver
class TTY {
Connection *Conn;
Connection &Conn;
int Number;
friend class Connection;
TTY(Connection *Conn, int Number) : Conn(Conn), Number(Number) {}
TTY(Connection &Conn, int Number) : Conn(Conn), Number(Number) {}
public:
~TTY();
@@ -82,6 +97,8 @@ class Connection {
DisplaySize displaySize() const;
TTY tty(int, bool);
using Driver = std::variant<Driver::HandyTech>;
};
} // namespace BrlAPI
View
@@ -137,18 +137,21 @@ void MIDIBuffer::clear() {
jack_midi_clear_buffer(Buffer);
}
gsl::span<gsl::byte> MIDIBuffer::reserve(std::uint32_t FrameOffset, std::uint32_t Size) {
gsl::span<std::byte> MIDIBuffer::reserve (
std::uint32_t FrameOffset, std::uint32_t Size
) {
return {
reinterpret_cast<gsl::byte *>(jack_midi_event_reserve(Buffer, FrameOffset, Size)),
Size
reinterpret_cast<std::byte *>(
jack_midi_event_reserve(Buffer, FrameOffset, Size)
), Size
};
}
MIDIOut::MIDIOut(JACK::Client *Client, std::string Name)
: Port(Client, std::move(Name), JACK_DEFAULT_MIDI_TYPE, false)
{}
MIDIBuffer MIDIOut::buffer(std::int32_t FrameCount) {
MIDIBuffer MIDIOut::buffer(std::uint32_t FrameCount) {
return { jack_port_get_buffer(JACK->Port, FrameCount), FrameCount };
}
View
@@ -65,15 +65,15 @@ class MIDIBuffer {
: Buffer(Buffer), Offset(Offset) {}
public:
Index &operator=(MIDI::SystemRealTimeMessage Message) {
Buffer.reserve<1>(Offset)[0] = static_cast<gsl::byte>(Message);
Buffer.reserve<1>(Offset)[0] = static_cast<std::byte>(Message);
return *this;
}
};
void clear();
gsl::span<gsl::byte> reserve(std::uint32_t FrameOffset, std::uint32_t Size);
gsl::span<std::byte> reserve(std::uint32_t FrameOffset, std::uint32_t Size);
template<std::uint32_t Size>
gsl::span<gsl::byte, Size> reserve(std::uint32_t FrameOffset) {
gsl::span<std::byte, Size> reserve(std::uint32_t FrameOffset) {
return reserve(FrameOffset, Size);
}
Index operator[](std::uint32_t FrameOffset) {
@@ -87,9 +87,9 @@ class MIDIOut : public Port {
MIDIOut(Client *, std::string Name);
public:
using value_type = gsl::byte;
using value_type = std::byte;
MIDIBuffer buffer(std::int32_t FrameCount);
MIDIBuffer buffer(std::uint32_t FrameCount);
};
class Client {

0 comments on commit 02864d6

Please sign in to comment.