1 /**
2 * Copyright (c) 2018, Timothy Stack
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of Timothy Stack nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @file input_dispatcher.cc
30 */
31
32 #include "config.h"
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/time.h>
37 #include <array>
38
39 #if defined HAVE_NCURSESW_CURSES_H
40 # include <ncursesw/curses.h>
41 #elif defined HAVE_NCURSESW_H
42 # include <ncursesw.h>
43 #elif defined HAVE_NCURSES_CURSES_H
44 # include <ncurses/curses.h>
45 #elif defined HAVE_NCURSES_H
46 # include <ncurses.h>
47 #elif defined HAVE_CURSES_H
48 # include <curses.h>
49 #else
50 # error "SysV or X/Open-compatible Curses header file required"
51 #endif
52
53 #include "base/lnav_log.hh"
54 #include "base/time_util.hh"
55 #include "ww898/cp_utf8.hpp"
56 #include "input_dispatcher.hh"
57
58 using namespace ww898;
59
60 template<typename A>
to_key_seq(A & dst,const char * src)61 static void to_key_seq(A &dst, const char *src)
62 {
63 dst[0] = '\0';
64 for (size_t lpc = 0; src[lpc]; lpc++) {
65 snprintf(dst.data() + strlen(dst.data()),
66 dst.size() - strlen(dst.data()),
67 "x%02x",
68 src[lpc] & 0xff);
69 }
70 }
71
new_input(const struct timeval & current_time,int ch)72 void input_dispatcher::new_input(const struct timeval ¤t_time, int ch)
73 {
74 nonstd::optional<bool> handled = nonstd::nullopt;
75 std::array<char, 32 * 3 + 1> keyseq{0};
76
77 switch (ch) {
78 case KEY_ESCAPE:
79 this->reset_escape_buffer(ch, current_time);
80 break;
81 case KEY_MOUSE:
82 this->id_mouse_handler();
83 break;
84 default:
85 if (this->id_escape_index > 0) {
86 this->append_to_escape_buffer(ch);
87
88 if (strcmp("\x1b[", this->id_escape_buffer) == 0) {
89 } else if (strcmp("\x1b[<", this->id_escape_buffer) == 0) {
90 this->id_mouse_handler();
91 this->id_escape_index = 0;
92 } else if (this->id_escape_expected_size == -1 ||
93 this->id_escape_index ==
94 this->id_escape_expected_size) {
95 to_key_seq(keyseq, this->id_escape_buffer);
96 switch (this->id_escape_matcher(keyseq.data())) {
97 case escape_match_t::NONE: {
98 for (int lpc = 0; this->id_escape_buffer[lpc]; lpc++) {
99 handled = this->id_key_handler(
100 this->id_escape_buffer[lpc]);
101 }
102 this->id_escape_index = 0;
103 break;
104 }
105 case escape_match_t::PARTIAL:
106 break;
107 case escape_match_t::FULL:
108 this->id_escape_handler(keyseq.data());
109 this->id_escape_index = 0;
110 break;
111 }
112 }
113 if (this->id_escape_expected_size != -1 &&
114 this->id_escape_index == this->id_escape_expected_size) {
115 this->id_escape_index = 0;
116 }
117 } else {
118 auto seq_size = utf::utf8::char_size([ch]() {
119 return std::make_pair(ch, 16);
120 })
121 .unwrapOr(size_t{1});
122
123 if (seq_size == 1) {
124 snprintf(keyseq.data(), keyseq.size(), "x%02x", ch & 0xff);
125 handled = this->id_key_handler(ch);
126 } else {
127 this->reset_escape_buffer(ch, current_time, seq_size);
128 }
129 }
130 break;
131 }
132
133 if (handled && !handled.value()) {
134 this->id_unhandled_handler(keyseq.data());
135 }
136 }
137
poll(const struct timeval & current_time)138 void input_dispatcher::poll(const struct timeval ¤t_time)
139 {
140 if (this->id_escape_index == 1) {
141 static const struct timeval escape_threshold = { 0, 10000 };
142 struct timeval diff;
143
144 timersub(¤t_time, &this->id_escape_start_time, &diff);
145 if (escape_threshold < diff) {
146 this->id_key_handler(KEY_CTRL_RBRACKET);
147 this->id_escape_index = 0;
148 }
149 }
150 }
151
reset_escape_buffer(int ch,const timeval & current_time,ssize_t expected_size)152 void input_dispatcher::reset_escape_buffer(int ch, const timeval ¤t_time,
153 ssize_t expected_size)
154 {
155 this->id_escape_index = 0;
156 this->append_to_escape_buffer(ch);
157 this->id_escape_expected_size = expected_size;
158 this->id_escape_start_time = current_time;
159 }
160
append_to_escape_buffer(int ch)161 void input_dispatcher::append_to_escape_buffer(int ch)
162 {
163 if (this->id_escape_index < (sizeof(this->id_escape_buffer) - 1)) {
164 this->id_escape_buffer[this->id_escape_index++] = static_cast<char>(ch);
165 this->id_escape_buffer[this->id_escape_index] = '\0';
166 }
167 }
168