Skip to content
Newer
Older
100644 140 lines (104 sloc) 3.4 KB
c1ae6ea @ry Add TTYWrap
ry authored
1 #include <node.h>
2 #include <node_buffer.h>
3 #include <req_wrap.h>
4 #include <handle_wrap.h>
5 #include <stream_wrap.h>
6
7 namespace node {
8
74b6426 @ry Initial pass at new TTY js layer
ry authored
9 #define UNWRAP \
10 assert(!args.Holder().IsEmpty()); \
11 assert(args.Holder()->InternalFieldCount() > 0); \
12 TTYWrap* wrap = \
13 static_cast<TTYWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
14 if (!wrap) { \
15 SetErrno(UV_EBADF); \
16 return scope.Close(Integer::New(-1)); \
17 }
18
c1ae6ea @ry Add TTYWrap
ry authored
19 using v8::Object;
20 using v8::Handle;
21 using v8::Local;
22 using v8::Persistent;
23 using v8::Value;
24 using v8::HandleScope;
25 using v8::FunctionTemplate;
26 using v8::String;
27 using v8::Function;
28 using v8::TryCatch;
29 using v8::Context;
30 using v8::Arguments;
31 using v8::Integer;
32 using v8::Undefined;
33
34
35 class TTYWrap : StreamWrap {
36 public:
37 static void Initialize(Handle<Object> target) {
38 StreamWrap::Initialize(target);
39
40 HandleScope scope;
41
42 Local<FunctionTemplate> t = FunctionTemplate::New(New);
43 t->SetClassName(String::NewSymbol("TTY"));
44
45 t->InstanceTemplate()->SetInternalFieldCount(1);
46
47 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
48
49 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
50 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
51 NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
52
74b6426 @ry Initial pass at new TTY js layer
ry authored
53 NODE_SET_PROTOTYPE_METHOD(t, "getWindowSize", TTYWrap::GetWindowSize);
54 NODE_SET_PROTOTYPE_METHOD(t, "setRawMode", SetRawMode);
55
c1ae6ea @ry Add TTYWrap
ry authored
56 NODE_SET_METHOD(target, "isTTY", IsTTY);
57
58 target->Set(String::NewSymbol("TTY"), t->GetFunction());
59 }
60
61 private:
62 static Handle<Value> IsTTY(const Arguments& args) {
63 HandleScope scope;
64 int fd = args[0]->Int32Value();
65 assert(fd >= 0);
94bedc6 @ry Upgrade libuv to f20297f
ry authored
66 return uv_guess_handle(fd) == UV_TTY ? v8::True() : v8::False();
c1ae6ea @ry Add TTYWrap
ry authored
67 }
68
74b6426 @ry Initial pass at new TTY js layer
ry authored
69 static Handle<Value> GetWindowSize(const Arguments& args) {
70 HandleScope scope;
71
72 UNWRAP
73
74 int width, height;
75 int r = uv_tty_get_winsize(&wrap->handle_, &width, &height);
76
77 if (r) {
78 SetErrno(uv_last_error(uv_default_loop()).code);
79 return v8::Undefined();
80 }
81
82 Local<v8::Array> a = v8::Array::New(2);
83 a->Set(0, Integer::New(width));
84 a->Set(1, Integer::New(height));
85
86 return scope.Close(a);
87 }
88
89 static Handle<Value> SetRawMode(const Arguments& args) {
90 HandleScope scope;
91
92 assert(!args.Holder().IsEmpty());
93 assert(args.Holder()->InternalFieldCount() > 0);
94 TTYWrap* wrap =
95 static_cast<TTYWrap*>(args.Holder()->GetPointerFromInternalField(0));
96
97 if (!wrap) {
98 SetErrno(UV_EBADF);
99 return scope.Close(Integer::New(-1));
100 }
101
102 int r = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
103
104 if (r) {
105 SetErrno(uv_last_error(uv_default_loop()).code);
106 }
107
108 return scope.Close(Integer::New(r));
109 }
110
c1ae6ea @ry Add TTYWrap
ry authored
111 static Handle<Value> New(const Arguments& args) {
112 HandleScope scope;
113
114 // This constructor should not be exposed to public javascript.
115 // Therefore we assert that we are not trying to call this as a
116 // normal function.
117 assert(args.IsConstructCall());
118
119 int fd = args[0]->Int32Value();
120 assert(fd >= 0);
121
122 TTYWrap* wrap = new TTYWrap(args.This(), fd);
123 assert(wrap);
124 wrap->UpdateWriteQueueSize();
125
126 return scope.Close(args.This());
127 }
128
129 TTYWrap(Handle<Object> object, int fd)
130 : StreamWrap(object, (uv_stream_t*)&handle_) {
131 uv_tty_init(uv_default_loop(), &handle_, fd);
132 }
133
134 uv_tty_t handle_;
135 };
136
137 } // namespace node
138
139 NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize);
Something went wrong with that request. Please try again.