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.hh 30 */ 31 32 #ifndef INPUT_DISPATCHER_HH 33 #define INPUT_DISPATCHER_HH 34 35 #include <sys/types.h> 36 37 #include <functional> 38 39 #define KEY_ESCAPE 0x1b 40 #define KEY_CTRL_RBRACKET 0x1d 41 42 class input_dispatcher { 43 public: 44 void new_input(const struct timeval ¤t_time, int ch); 45 46 void poll(const struct timeval ¤t_time); 47 in_escape() const48 bool in_escape() const { 49 return this->id_escape_index > 0; 50 } 51 52 enum class escape_match_t { 53 NONE, 54 PARTIAL, 55 FULL, 56 }; 57 58 std::function<escape_match_t(const char *)> id_escape_matcher; 59 std::function<bool(int)> id_key_handler; 60 std::function<void(const char *)> id_escape_handler; 61 std::function<void()> id_mouse_handler; 62 std::function<void(const char *)> id_unhandled_handler; 63 private: 64 void reset_escape_buffer(int ch, 65 const struct timeval ¤t_time, 66 ssize_t expected_size = -1); 67 void append_to_escape_buffer(int ch); 68 69 char id_escape_buffer[32]; 70 ssize_t id_escape_index{0}; 71 ssize_t id_escape_expected_size{-1}; 72 struct timeval id_escape_start_time{0, 0}; 73 }; 74 75 #endif 76