1 // -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 // (c) 2016 Henner Zeller <h.zeller@acm.org>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation version 2.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://gnu.org/licenses/gpl-2.0.txt>
15 
16 #ifndef TERMINAL_CANVAS_H_
17 #define TERMINAL_CANVAS_H_
18 
19 #include "framebuffer.h"
20 #include "buffered-write-sequencer.h"
21 
22 #include <stddef.h>
23 #include <sys/types.h>
24 
25 #include <string>
26 
27 namespace timg {
28 
29 // Canvas that can send a framebuffer to a terminal.
30 class TerminalCanvas {
31 public:
32     // Create a terminal canvas, sending to given file-descriptor.
33     TerminalCanvas(BufferedWriteSequencer *write_sequencer);
34     TerminalCanvas(const TerminalCanvas &) = delete;
35     virtual ~TerminalCanvas();
36 
37     // Send frame to terminal. Move to xposition (relative to the left
38     // of the screen, and delta y (relative to the current position) first.
39     virtual void Send(int x, int dy, const Framebuffer &framebuffer,
40                       SeqType sequence_type, Duration end_of_frame) = 0;
41 
42     // The following methods add content that is emitted before the next Send()
43 
44     void AddPrefixNextSend(const char *data, int len);
45 
46     void ClearScreen();
47     void CursorOff();
48     void CursorOn();
49 
50     void MoveCursorDY(int rows);  // -: up^, +: downV
51     void MoveCursorDX(int cols);  // -: <-left, +: right->
52 
53 protected:
54     char *AppendPrefixToBuffer(char *buffer);
55 
56     BufferedWriteSequencer *const write_sequencer_;  // not owned
57 
58 private:
59     std::string prefix_send_;
60 };
61 }  // namespace timg
62 
63 #endif // TERMINAL_CANVAS_H_
64