1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
23 #include <node_buffer.h>
25 #include <handle_wrap.h>
26 #include <stream_wrap.h>
35 using v8::HandleScope;
36 using v8::FunctionTemplate;
46 assert(!args.Holder().IsEmpty()); \
47 assert(args.Holder()->InternalFieldCount() > 0); \
49 static_cast<TTYWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
52 err.code = UV_EBADF; \
54 return scope.Close(Integer::New(-1)); \
58 class TTYWrap : StreamWrap {
60 static void Initialize(Handle<Object> target) {
61 StreamWrap::Initialize(target);
65 Local<FunctionTemplate> t = FunctionTemplate::New(New);
66 t->SetClassName(String::NewSymbol("TTY"));
68 t->InstanceTemplate()->SetInternalFieldCount(1);
70 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
71 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
73 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
74 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
75 NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
77 NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
78 NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
80 NODE_SET_METHOD(target, "isTTY", IsTTY);
81 NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
83 target->Set(String::NewSymbol("TTY"), t->GetFunction());
87 static Handle<Value> GuessHandleType(const Arguments& args) {
89 int fd = args[0]->Int32Value();
92 uv_handle_type t = uv_guess_handle(fd);
96 return scope.Close(String::New("TTY"));
99 return scope.Close(String::New("PIPE"));
102 return scope.Close(String::New("FILE"));
106 return v8::Undefined();
110 static Handle<Value> IsTTY(const Arguments& args) {
112 int fd = args[0]->Int32Value();
114 return uv_guess_handle(fd) == UV_TTY ? v8::True() : v8::False();
117 static Handle<Value> GetWindowSize(const Arguments& args) {
123 int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
126 SetErrno(uv_last_error(uv_default_loop()));
127 return v8::Undefined();
130 Local<v8::Array> a = v8::Array::New(2);
131 a->Set(0, Integer::New(width));
132 a->Set(1, Integer::New(height));
134 return scope.Close(a);
137 static Handle<Value> SetRawMode(const Arguments& args) {
142 int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
145 SetErrno(uv_last_error(uv_default_loop()));
148 return scope.Close(Integer::New(r));
151 static Handle<Value> New(const Arguments& args) {
154 // This constructor should not be exposed to public javascript.
155 // Therefore we assert that we are not trying to call this as a
157 assert(args.IsConstructCall());
159 int fd = args[0]->Int32Value();
162 TTYWrap* wrap = new TTYWrap(args.This(), fd, args[1]->IsTrue());
164 wrap->UpdateWriteQueueSize();
166 return scope.Close(args.This());
169 TTYWrap(Handle<Object> object, int fd, bool readable)
170 : StreamWrap(object, (uv_stream_t*)&handle_) {
171 uv_tty_init(uv_default_loop(), &handle_, fd, readable);
179 NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize)