Program Listing for File keyboard_handler_unix_impl.hpp
↰ Return to documentation for file (/tmp/ws/src/keyboard_handler/keyboard_handler/include/keyboard_handler/keyboard_handler_unix_impl.hpp
)
// Copyright 2021 Apex.AI, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef KEYBOARD_HANDLER__KEYBOARD_HANDLER_UNIX_IMPL_HPP_
#define KEYBOARD_HANDLER__KEYBOARD_HANDLER_UNIX_IMPL_HPP_
#ifndef _WIN32
#include <termios.h>
#include <string>
#include <unordered_map>
#include <atomic>
#include <thread>
#include <tuple>
#include <stdexcept>
#include "keyboard_handler/visibility_control.hpp"
#include "keyboard_handler_base.hpp"
class KeyboardHandlerUnixImpl : public KeyboardHandlerBase
{
public:
using isattyFunction = std::function<int (int)>;
using tcgetattrFunction = std::function<int (int, struct termios *)>;
using tcsetattrFunction = std::function<int (int, int, const struct termios *)>;
using readFunction = std::function<ssize_t(int, void *, size_t)>;
using signal_handler_type = void (*)(int);
KEYBOARD_HANDLER_PUBLIC
KeyboardHandlerUnixImpl();
KEYBOARD_HANDLER_PUBLIC
explicit KeyboardHandlerUnixImpl(bool install_signal_handler);
KEYBOARD_HANDLER_PUBLIC
virtual ~KeyboardHandlerUnixImpl();
KEYBOARD_HANDLER_PUBLIC
std::string get_terminal_sequence(KeyboardHandlerUnixImpl::KeyCode key_code);
KEYBOARD_HANDLER_PUBLIC
static bool restore_buffer_mode_for_stdin();
KEYBOARD_HANDLER_PUBLIC
static signal_handler_type get_old_sigint_handler();
protected:
KEYBOARD_HANDLER_PUBLIC
KeyboardHandlerUnixImpl(
const readFunction & read_fn,
const isattyFunction & isatty_fn,
const tcgetattrFunction & tcgetattr_fn,
const tcsetattrFunction & tcsetattr_fn,
bool install_signal_handler = true);
std::tuple<KeyCode, KeyModifiers> parse_input(const char * buff, ssize_t read_bytes);
struct KeyMap
{
KeyCode inner_code;
const char * terminal_sequence;
};
static const KeyMap DEFAULT_STATIC_KEY_MAP[];
static const size_t STATIC_KEY_MAP_LENGTH;
private:
static void on_signal(int signal_number);
static struct termios old_term_settings_;
static tcsetattrFunction tcsetattr_fn_;
static signal_handler_type old_sigint_handler_;
bool install_signal_handler_ = false;
std::thread key_handler_thread_;
static std::atomic_bool exit_;
const int stdin_fd_;
std::unordered_map<std::string, KeyCode> key_codes_map_;
std::exception_ptr thread_exception_ptr{nullptr};
};
#endif // #ifndef _WIN32
#endif // KEYBOARD_HANDLER__KEYBOARD_HANDLER_UNIX_IMPL_HPP_