File: terminal.hpp

package info (click to toggle)
lager 0.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,916 kB
  • sloc: cpp: 10,967; javascript: 10,433; makefile: 214; python: 100; sh: 98
file content (84 lines) | stat: -rw-r--r-- 1,695 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//

#pragma once

#include <boost/asio/io_context.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <boost/asio/signal_set.hpp>

#include <functional>
#include <variant>

extern "C"
{
// this is required to avoid naming conflicts with boost::asio
#ifndef NCURSES_NOMACROS
#define NCURSES_NOMACROS 1
#endif
// this is required on Apple for ::get_wch
#ifndef _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED 1
#endif
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 1
#endif
#include <ncurses.h>
}

namespace ncurses {

struct coord
{
    int row = {};
    int col = {};
};

using key_code = std::tuple<int, wint_t>;

struct key_event
{
    key_code key;
};
struct resize_event
{
    coord size;
};
using event = std::variant<key_event, resize_event>;

struct terminal
{
    using event_handler = std::function<void(event)>;

    terminal(boost::asio::io_context& serv);

    coord size();

    void start(event_handler ev);
    void stop();

private:
    struct cleanup_fn
    {
        void operator()(_win_st* win) const;
    };

    void next_key_();
    void next_resize_();

    std::unique_ptr<_win_st, cleanup_fn> win_;
    boost::asio::posix::stream_descriptor input_;
    boost::asio::signal_set signal_;
    event_handler handler_;
};

} // namespace ncurses