| 1 | /* |
|---|
| 2 | * Captty |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2008-2009 FURUHASHI Sadayuki |
|---|
| 5 | * |
|---|
| 6 | * This program is free software: you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifndef CAPTTY_H__ |
|---|
| 21 | #define CAPTTY_H__ |
|---|
| 22 | |
|---|
| 23 | #include <iostream> |
|---|
| 24 | #include <stdexcept> |
|---|
| 25 | #include <stdint.h> |
|---|
| 26 | |
|---|
| 27 | namespace Captty { |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | void record(const char* file, char* cmd[] = NULL); |
|---|
| 31 | void play(const char* file); |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | class RecorderIMPL; |
|---|
| 35 | class Recorder { |
|---|
| 36 | public: |
|---|
| 37 | Recorder(std::ostream& output); |
|---|
| 38 | ~Recorder(); |
|---|
| 39 | void write(const char* buf, uint16_t len); |
|---|
| 40 | void set_window_size(short row, short col); |
|---|
| 41 | private: |
|---|
| 42 | RecorderIMPL* impl; |
|---|
| 43 | Recorder(); |
|---|
| 44 | Recorder(const Recorder&); |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | class PlayerIMPL; |
|---|
| 49 | class Player { |
|---|
| 50 | public: |
|---|
| 51 | Player(std::istream& input); |
|---|
| 52 | ~Player(); |
|---|
| 53 | void play(); |
|---|
| 54 | void set_handler(void (*handler)(int, void*), void* data); |
|---|
| 55 | void speed_up(); |
|---|
| 56 | void speed_down(); |
|---|
| 57 | void speed_reset(); |
|---|
| 58 | void speed_set(double speed); |
|---|
| 59 | void pause(); |
|---|
| 60 | void toggle_pause(); |
|---|
| 61 | void skip_back(); |
|---|
| 62 | void skip_forward(); |
|---|
| 63 | void rewind(); |
|---|
| 64 | void quit(); |
|---|
| 65 | private: |
|---|
| 66 | PlayerIMPL* impl; |
|---|
| 67 | Player(); |
|---|
| 68 | Player(const Player&); |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | struct captty_error : public std::runtime_error { |
|---|
| 73 | captty_error(const std::string& msg) : |
|---|
| 74 | std::runtime_error(msg) {} |
|---|
| 75 | captty_error(const std::string& msg, int err) : |
|---|
| 76 | std::runtime_error(msg) {} |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | struct io_error : captty_error { |
|---|
| 80 | io_error(const std::string& msg) : |
|---|
| 81 | captty_error(msg) {} |
|---|
| 82 | io_error(const std::string& msg, int err) : |
|---|
| 83 | captty_error(msg) {} |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | static const size_t MAX_BLOCK_SIZE = 1<<19; |
|---|
| 88 | |
|---|
| 89 | struct tty_size { |
|---|
| 90 | uint16_t row; |
|---|
| 91 | uint16_t col; |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 94 | namespace FRAME { |
|---|
| 95 | static const uint8_t OUTPUT = 0; |
|---|
| 96 | static const uint8_t WINDOW_SIZE = 1; |
|---|
| 97 | static const size_t HEADER_SIZE = sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint16_t); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | namespace BLOCK { |
|---|
| 101 | static const uint8_t UNCOMPRESSED_FRAMES = 0; |
|---|
| 102 | static const uint8_t COMPRESSED_FRAMES = 1; |
|---|
| 103 | static const size_t HEADER_SIZE = sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint16_t); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | inline uint16_t htoles(uint16_t x) { |
|---|
| 108 | #ifdef __BIG_ENDIAN__ |
|---|
| 109 | return ((x << 8) & 0xff00) | ((x >> 8) & 0x00ff); |
|---|
| 110 | #else |
|---|
| 111 | return x; |
|---|
| 112 | #endif |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | inline uint32_t htolel(uint32_t x) { |
|---|
| 116 | #ifdef __BIG_ENDIAN__ |
|---|
| 117 | return ((x << 24) & 0xff000000 ) | |
|---|
| 118 | ((x << 8) & 0x00ff0000 ) | |
|---|
| 119 | ((x >> 8) & 0x0000ff00 ) | |
|---|
| 120 | ((x >> 24) & 0x000000ff ); |
|---|
| 121 | #else |
|---|
| 122 | return x; |
|---|
| 123 | #endif |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | inline uint16_t letohs(uint16_t x) { return htoles(x); } |
|---|
| 127 | inline uint32_t letohl(uint32_t x) { return htolel(x); } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * File format |
|---|
| 133 | * =========== |
|---|
| 134 | * +----+----+----+ |
|---|
| 135 | * | | | | ... |
|---|
| 136 | * +----+----+----+ |
|---|
| 137 | * Block |
|---|
| 138 | * Block |
|---|
| 139 | * Block |
|---|
| 140 | * ... |
|---|
| 141 | * |
|---|
| 142 | * |
|---|
| 143 | * Block format |
|---|
| 144 | * ============ |
|---|
| 145 | * +--+-+----+--------+ |
|---|
| 146 | * |16|8| 32 | | |
|---|
| 147 | * +--+-+----+--------+ |
|---|
| 148 | * time |
|---|
| 149 | * flag |
|---|
| 150 | * length |
|---|
| 151 | * frames |
|---|
| 152 | * |
|---|
| 153 | * time: elapsed time since previous block |
|---|
| 154 | * in seconds |
|---|
| 155 | * |
|---|
| 156 | * flag: BLOCK::COMPRESSED_FRAMES: |
|---|
| 157 | * following data is compressed frames |
|---|
| 158 | * compression algorithm is deflate |
|---|
| 159 | * |
|---|
| 160 | * BLOCK::UNCOMPRESSED_FRAMES: |
|---|
| 161 | * following data is uncompressed frames |
|---|
| 162 | * |
|---|
| 163 | * length: length of following data in bytes |
|---|
| 164 | * maximum is MAX_BLOCK_SIZE |
|---|
| 165 | * |
|---|
| 166 | * |
|---|
| 167 | * Frames format |
|---|
| 168 | * ============= |
|---|
| 169 | * +----+----+----+ |
|---|
| 170 | * | | | | ... |
|---|
| 171 | * +----+----+----+ |
|---|
| 172 | * Frame |
|---|
| 173 | * Frame |
|---|
| 174 | * Frame |
|---|
| 175 | * ... |
|---|
| 176 | * |
|---|
| 177 | * |
|---|
| 178 | * Frame format |
|---|
| 179 | * ============ |
|---|
| 180 | * +----+-+--+--------+ |
|---|
| 181 | * | 32 |8|16| | |
|---|
| 182 | * +----+-+--+--------+ |
|---|
| 183 | * time |
|---|
| 184 | * flag |
|---|
| 185 | * length |
|---|
| 186 | * data |
|---|
| 187 | * |
|---|
| 188 | * time: elapsed time since previous frame |
|---|
| 189 | * in microseconds |
|---|
| 190 | * |
|---|
| 191 | * flag: FRAME::OUTPUT |
|---|
| 192 | * following data is Terminal Output |
|---|
| 193 | * |
|---|
| 194 | * FRAME::WINDOW_SIZE |
|---|
| 195 | * following data is Window Size |
|---|
| 196 | * |
|---|
| 197 | * length: length of following data in bytes |
|---|
| 198 | * maximum is 1<<16 |
|---|
| 199 | * |
|---|
| 200 | * |
|---|
| 201 | * Terminal Output Format |
|---|
| 202 | * ====================== |
|---|
| 203 | * +--------+ |
|---|
| 204 | * | | |
|---|
| 205 | * +--------+ |
|---|
| 206 | * data |
|---|
| 207 | * |
|---|
| 208 | * |
|---|
| 209 | * Window Size Format |
|---|
| 210 | * ================== |
|---|
| 211 | * +--+--+ |
|---|
| 212 | * |16|16| |
|---|
| 213 | * +--+--+ |
|---|
| 214 | * number of rows |
|---|
| 215 | * number of columns |
|---|
| 216 | * |
|---|
| 217 | */ |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | } // namespace Captty |
|---|
| 221 | |
|---|
| 222 | #endif /* captty.h */ |
|---|