deps: upgrade libuv to a478847
[platform/upstream/nodejs.git] / src / tty_wrap.cc
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #include "node.h"
23 #include "node_buffer.h"
24 #include "req_wrap.h"
25 #include "handle_wrap.h"
26 #include "stream_wrap.h"
27
28 namespace node {
29
30 using v8::Object;
31 using v8::Handle;
32 using v8::Local;
33 using v8::Persistent;
34 using v8::Value;
35 using v8::HandleScope;
36 using v8::FunctionTemplate;
37 using v8::String;
38 using v8::Function;
39 using v8::TryCatch;
40 using v8::Context;
41 using v8::Arguments;
42 using v8::Integer;
43 using v8::Undefined;
44
45 class TTYWrap : StreamWrap {
46  public:
47   static void Initialize(Handle<Object> target) {
48     StreamWrap::Initialize(target);
49
50     HandleScope scope;
51
52     Local<FunctionTemplate> t = FunctionTemplate::New(New);
53     t->SetClassName(String::NewSymbol("TTY"));
54
55     t->InstanceTemplate()->SetInternalFieldCount(1);
56
57     NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
58     NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
59
60     NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
61     NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
62
63     NODE_SET_PROTOTYPE_METHOD(t, "writeBuffer", StreamWrap::WriteBuffer);
64     NODE_SET_PROTOTYPE_METHOD(t, "writeAsciiString", StreamWrap::WriteAsciiString);
65     NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
66     NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
67
68     NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
69     NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
70
71     NODE_SET_METHOD(target, "isTTY", IsTTY);
72     NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
73
74     target->Set(String::NewSymbol("TTY"), t->GetFunction());
75   }
76
77  private:
78   static Handle<Value> GuessHandleType(const Arguments& args) {
79     HandleScope scope;
80     int fd = args[0]->Int32Value();
81     assert(fd >= 0);
82
83     uv_handle_type t = uv_guess_handle(fd);
84
85     switch (t) {
86       case UV_TTY:
87         return scope.Close(String::New("TTY"));
88
89       case UV_NAMED_PIPE:
90         return scope.Close(String::New("PIPE"));
91
92       case UV_FILE:
93         return scope.Close(String::New("FILE"));
94
95       default:
96         assert(0);
97         return v8::Undefined();
98     }
99   }
100
101   static Handle<Value> IsTTY(const Arguments& args) {
102     HandleScope scope;
103     int fd = args[0]->Int32Value();
104     assert(fd >= 0);
105     return uv_guess_handle(fd) == UV_TTY ? v8::True() : v8::False();
106   }
107
108   static Handle<Value> GetWindowSize(const Arguments& args) {
109     HandleScope scope;
110
111     UNWRAP(TTYWrap)
112
113     int width, height;
114     int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
115
116     if (r) {
117       SetErrno(uv_last_error(uv_default_loop()));
118       return v8::Undefined();
119     }
120
121     Local<v8::Array> a = v8::Array::New(2);
122     a->Set(0, Integer::New(width));
123     a->Set(1, Integer::New(height));
124
125     return scope.Close(a);
126   }
127
128   static Handle<Value> SetRawMode(const Arguments& args) {
129     HandleScope scope;
130
131     UNWRAP(TTYWrap)
132
133     int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
134
135     if (r) {
136       SetErrno(uv_last_error(uv_default_loop()));
137     }
138
139     return scope.Close(Integer::New(r));
140   }
141
142   static Handle<Value> New(const Arguments& args) {
143     HandleScope scope;
144
145     // This constructor should not be exposed to public javascript.
146     // Therefore we assert that we are not trying to call this as a
147     // normal function.
148     assert(args.IsConstructCall());
149
150     int fd = args[0]->Int32Value();
151     assert(fd >= 0);
152
153     TTYWrap* wrap = new TTYWrap(args.This(), fd, args[1]->IsTrue());
154     assert(wrap);
155     wrap->UpdateWriteQueueSize();
156
157     return scope.Close(args.This());
158   }
159
160   TTYWrap(Handle<Object> object, int fd, bool readable)
161       : StreamWrap(object, (uv_stream_t*)&handle_) {
162     uv_tty_init(uv_default_loop(), &handle_, fd, readable);
163   }
164
165   uv_tty_t handle_;
166 };
167
168 }  // namespace node
169
170 NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize)