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"
36 using v8::HandleScope;
37 using v8::FunctionTemplate;
47 void TTYWrap::Initialize(Handle<Object> target) {
48 StreamWrap::Initialize(target);
52 Local<FunctionTemplate> t = FunctionTemplate::New(New);
53 t->SetClassName(String::NewSymbol("TTY"));
55 t->InstanceTemplate()->SetInternalFieldCount(1);
57 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
58 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
60 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
61 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
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);
68 NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
69 NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
71 NODE_SET_METHOD(target, "isTTY", IsTTY);
72 NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
74 target->Set(String::NewSymbol("TTY"), t->GetFunction());
78 TTYWrap* TTYWrap::Unwrap(Local<Object> obj) {
79 assert(!obj.IsEmpty());
80 assert(obj->InternalFieldCount() > 0);
81 return static_cast<TTYWrap*>(obj->GetAlignedPointerFromInternalField(0));
85 uv_tty_t* TTYWrap::UVHandle() {
90 Handle<Value> TTYWrap::GuessHandleType(const Arguments& args) {
92 int fd = args[0]->Int32Value();
95 uv_handle_type t = uv_guess_handle(fd);
99 return scope.Close(String::New("TTY"));
102 return scope.Close(String::New("PIPE"));
105 return scope.Close(String::New("FILE"));
107 case UV_UNKNOWN_HANDLE:
108 return scope.Close(String::New("UNKNOWN"));
112 return v8::Undefined(node_isolate);
117 Handle<Value> TTYWrap::IsTTY(const Arguments& args) {
119 int fd = args[0]->Int32Value();
122 if (uv_guess_handle(fd) == UV_TTY) {
123 return v8::True(node_isolate);
126 return v8::False(node_isolate);
130 Handle<Value> TTYWrap::GetWindowSize(const Arguments& args) {
136 int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
139 SetErrno(uv_last_error(uv_default_loop()));
140 return v8::Undefined(node_isolate);
143 Local<v8::Array> a = v8::Array::New(2);
144 a->Set(0, Integer::New(width, node_isolate));
145 a->Set(1, Integer::New(height, node_isolate));
147 return scope.Close(a);
151 Handle<Value> TTYWrap::SetRawMode(const Arguments& args) {
156 int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
159 SetErrno(uv_last_error(uv_default_loop()));
162 return scope.Close(Integer::New(r, node_isolate));
166 Handle<Value> TTYWrap::New(const Arguments& args) {
169 // This constructor should not be exposed to public javascript.
170 // Therefore we assert that we are not trying to call this as a
172 assert(args.IsConstructCall());
174 int fd = args[0]->Int32Value();
177 TTYWrap* wrap = new TTYWrap(args.This(), fd, args[1]->IsTrue());
179 wrap->UpdateWriteQueueSize();
181 return scope.Close(args.This());
185 TTYWrap::TTYWrap(Handle<Object> object, int fd, bool readable)
186 : StreamWrap(object, (uv_stream_t*)&handle_) {
187 uv_tty_init(uv_default_loop(), &handle_, fd, readable);
192 NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize)