]> git.tdb.fi Git - libs/core.git/blob - source/io/unix/console.cpp
Use vectors for storage in Poller
[libs/core.git] / source / io / unix / console.cpp
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <termios.h>
4 #include <sys/ioctl.h>
5 #include <msp/core/systemerror.h>
6 #include "console.h"
7 #include "handle_private.h"
8
9 namespace {
10
11 termios orig_attr;
12
13 }
14
15
16 namespace Msp {
17 namespace IO {
18
19 void Console::platform_init()
20 {
21         *handle = stream;
22
23         if(stream==CIN)
24                 tcgetattr(*handle, &orig_attr);
25 }
26
27 Console::~Console()
28 {
29         if(stream==CIN)
30                 tcsetattr(*handle, TCSADRAIN, &orig_attr);
31 }
32
33 void Console::set_local_echo(bool e)
34 {
35         check_access(M_READ);
36
37         termios t;
38         tcgetattr(*handle, &t);
39         t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0);
40         tcsetattr(*handle, TCSADRAIN, &t);
41 }
42
43 void Console::set_line_buffer(bool l)
44 {
45         check_access(M_READ);
46
47         termios t;
48         if(tcgetattr(*handle, &t)==-1)
49                 throw system_error("tcgetattr");
50         t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
51         // man termios warns that VMIN and VTIME may share indices with VEOF and VEOL
52         if(l)
53         {
54                 t.c_cc[VEOF] = orig_attr.c_cc[VEOF];
55                 t.c_cc[VEOL] = orig_attr.c_cc[VEOL];
56         }
57         else
58         {
59                 t.c_cc[VMIN] = 1;
60                 t.c_cc[VTIME] = 0;
61         }
62         if(tcsetattr(*handle, TCSADRAIN, &t)==-1)
63                 throw system_error("tcsetattr");
64 }
65
66 void Console::get_size(unsigned &rows, unsigned &cols)
67 {
68         check_access(M_WRITE);
69
70         struct winsize wsz;
71         if(ioctl(*handle, TIOCGWINSZ, &wsz)==-1)
72                 throw system_error("ioctl TIOCGWINSZ");
73         rows = wsz.ws_row;
74         cols = wsz.ws_col;
75 }
76
77 void Console::redirect(Base &other)
78 {
79         dup2(*other.get_handle(mode), *handle);
80 }
81
82 } // namespace IO
83 } // namespace Msp