vulp  2.3.0
Keyboard.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2023 Inria
3 
4 #pragma once
5 
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/ioctl.h>
10 #include <sys/select.h>
11 #include <termios.h>
12 #include <unistd.h>
13 
14 #include <chrono>
15 #include <iostream>
16 #include <string>
17 
19 
20 using std::chrono::duration_cast;
21 using std::chrono::milliseconds;
22 using std::chrono::system_clock;
23 
25 constexpr ssize_t kMaxKeyBytes = 3;
26 
28 constexpr int64_t kPollingIntervalMS = 50;
29 
30 // Scan codes for arrow keys
31 constexpr unsigned char UP_BYTES[] = {0x1B, 0x5B, 0x41};
32 constexpr unsigned char DOWN_BYTES[] = {0x1B, 0x5B, 0x42};
33 constexpr unsigned char RIGHT_BYTES[] = {0x1B, 0x5B, 0x43};
34 constexpr unsigned char LEFT_BYTES[] = {0x1B, 0x5B, 0x44};
35 
36 inline bool is_lowercase_alpha(unsigned char c) {
37  return 0x61 <= c && c <= 0x7A;
38 }
39 inline bool is_uppercase_alpha(unsigned char c) {
40  return 0x41 <= c && c <= 0x5A;
41 }
42 inline bool is_printable_ascii(unsigned char c) {
43  return 0x20 <= c && c <= 0x7F;
44 }
45 
47 enum class Key {
48  UP,
49  DOWN,
50  LEFT,
51  RIGHT,
52  W,
53  A,
54  S,
55  D,
56  X,
57  NONE, // No key pressed
58  UNKNOWN // Map everything else to this key
59 };
60 
71 class Keyboard : public Source {
72  public:
76  Keyboard();
77 
79  ~Keyboard() override;
80 
82  inline std::string prefix() const noexcept final { return "keyboard"; }
83 
88  void write(Dictionary& output) final;
89 
90  private:
92  bool read_event();
93 
99  Key map_char_to_key(unsigned char* buf);
100 
102  unsigned char buf_[kMaxKeyBytes];
103 
105  Key key_code_;
106 
108  bool key_pressed_;
109 
111  system_clock::time_point last_key_poll_time_;
112 };
113 
114 } // namespace vulp::observation::sources
bool is_printable_ascii(unsigned char c)
Definition: Keyboard.h:42
constexpr unsigned char LEFT_BYTES[]
Definition: Keyboard.h:34
constexpr unsigned char UP_BYTES[]
Definition: Keyboard.h:31
constexpr unsigned char DOWN_BYTES[]
Definition: Keyboard.h:32
bool is_lowercase_alpha(unsigned char c)
Definition: Keyboard.h:36
bool is_uppercase_alpha(unsigned char c)
Definition: Keyboard.h:39
constexpr ssize_t kMaxKeyBytes
Maximum number of bytes to encode a key.
Definition: Keyboard.h:25
constexpr int64_t kPollingIntervalMS
Polling interval in milliseconds.
Definition: Keyboard.h:28
constexpr unsigned char RIGHT_BYTES[]
Definition: Keyboard.h:33
Base class for sources.
Definition: Source.h:20
Source for reading Keyboard inputs.
Definition: Keyboard.h:71
Keyboard()
Constructor sets up the terminal in non-canonical mode where input is available immediately without w...
Definition: Keyboard.cpp:7
~Keyboard() override
Destructor.
Definition: Keyboard.cpp:20
std::string prefix() const noexcept final
Prefix of output in the observation dictionary.
Definition: Keyboard.h:82
void write(Dictionary &output) final
Write output to a dictionary.
Definition: Keyboard.cpp:83