Lines Matching refs:handle
39 #include "handle-inl.h"
74 static int uv__cancel_read_console(uv_tty_t* handle);
106 * of the caret when the first stdout/err handle is created, unless that would
124 * handle signalling SIGWINCH
164 static void uv__determine_vterm_state(HANDLE handle);
193 HANDLE handle;
199 handle = (HANDLE) uv__get_osfhandle(fd);
200 if (handle == INVALID_HANDLE_VALUE)
205 * underlying OS handle and forget about the original fd.
206 * We could also opt to use the original OS handle and just never close it,
211 handle,
213 &handle,
221 readable = GetNumberOfConsoleInputEvents(handle, &NumberOfEvents);
223 /* Obtain the screen buffer info with the output handle. */
224 if (!GetConsoleScreenBufferInfo(handle, &screen_buffer_info)) {
228 /* Obtain the cursor info with the output handle. */
229 if (!GetConsoleCursorInfo(handle, &cursor_info)) {
238 uv__determine_vterm_state(handle);
252 tty->handle = handle;
394 if (!SetConsoleMode(tty->handle, flags)) {
420 if (!GetConsoleScreenBufferInfo(tty->handle, &info)) {
437 uv_tty_t* handle;
444 handle = (uv_tty_t*) req->data;
445 loop = handle->loop;
447 UnregisterWait(handle->tty.rd.read_raw_wait);
448 handle->tty.rd.read_raw_wait = NULL;
455 static void uv__tty_queue_read_raw(uv_loop_t* loop, uv_tty_t* handle) {
459 assert(handle->flags & UV_HANDLE_READING);
460 assert(!(handle->flags & UV_HANDLE_READ_PENDING));
462 assert(handle->handle && handle->handle != INVALID_HANDLE_VALUE);
464 handle->tty.rd.read_line_buffer = uv_null_buf_;
466 req = &handle->read_req;
469 r = RegisterWaitForSingleObject(&handle->tty.rd.read_raw_wait,
470 handle->handle,
476 handle->tty.rd.read_raw_wait = NULL;
481 handle->flags |= UV_HANDLE_READ_PENDING;
482 handle->reqs_pending++;
488 uv_tty_t* handle;
500 handle = (uv_tty_t*) req->data;
501 loop = handle->loop;
503 assert(handle->tty.rd.read_line_buffer.base != NULL);
504 assert(handle->tty.rd.read_line_buffer.len > 0);
506 /* ReadConsole can't handle big buffers. */
507 if (handle->tty.rd.read_line_buffer.len < MAX_INPUT_BUFFER_LENGTH) {
508 bytes = handle->tty.rd.read_line_buffer.len;
526 read_console_success = ReadConsoleW(handle->handle,
537 handle->tty.rd.read_line_buffer.base,
582 static void uv__tty_queue_read_line(uv_loop_t* loop, uv_tty_t* handle) {
586 assert(handle->flags & UV_HANDLE_READING);
587 assert(!(handle->flags & UV_HANDLE_READ_PENDING));
588 assert(handle->handle && handle->handle != INVALID_HANDLE_VALUE);
590 req = &handle->read_req;
593 handle->tty.rd.read_line_buffer = uv_buf_init(NULL, 0);
594 handle->alloc_cb((uv_handle_t*) handle, 8192, &handle->tty.rd.read_line_buffer);
595 if (handle->tty.rd.read_line_buffer.base == NULL ||
596 handle->tty.rd.read_line_buffer.len == 0) {
597 handle->read_cb((uv_stream_t*) handle,
599 &handle->tty.rd.read_line_buffer);
602 assert(handle->tty.rd.read_line_buffer.base != NULL);
617 handle->flags |= UV_HANDLE_READ_PENDING;
618 handle->reqs_pending++;
622 static void uv__tty_queue_read(uv_loop_t* loop, uv_tty_t* handle) {
623 if (handle->flags & UV_HANDLE_TTY_RAW) {
624 uv__tty_queue_read_raw(loop, handle);
626 uv__tty_queue_read_line(loop, handle);
697 void uv_process_tty_read_raw_req(uv_loop_t* loop, uv_tty_t* handle,
699 /* Shortcut for handle->tty.rd.last_input_record.Event.KeyEvent. */
700 #define KEV handle->tty.rd.last_input_record.Event.KeyEvent
706 assert(handle->type == UV_TTY);
707 assert(handle->flags & UV_HANDLE_TTY_READABLE);
708 handle->flags &= ~UV_HANDLE_READ_PENDING;
710 if (!(handle->flags & UV_HANDLE_READING) ||
711 !(handle->flags & UV_HANDLE_TTY_RAW)) {
717 if ((handle->flags & UV_HANDLE_READING)) {
718 handle->flags &= ~UV_HANDLE_READING;
719 handle->read_cb((uv_stream_t*)handle,
727 if (!GetNumberOfConsoleInputEvents(handle->handle, &records_left)) {
728 handle->flags &= ~UV_HANDLE_READING;
729 DECREASE_ACTIVE_COUNT(loop, handle);
730 handle->read_cb((uv_stream_t*)handle,
741 while ((records_left > 0 || handle->tty.rd.last_key_len > 0) &&
742 (handle->flags & UV_HANDLE_READING)) {
743 if (handle->tty.rd.last_key_len == 0) {
745 if (!ReadConsoleInputW(handle->handle,
746 &handle->tty.rd.last_input_record,
749 handle->flags &= ~UV_HANDLE_READING;
750 DECREASE_ACTIVE_COUNT(loop, handle);
751 handle->read_cb((uv_stream_t*) handle,
760 if (handle->tty.rd.last_input_record.EventType == WINDOW_BUFFER_SIZE_EVENT) {
765 if (handle->tty.rd.last_input_record.EventType != KEY_EVENT) {
812 handle->tty.rd.last_utf16_high_surrogate = KEV.uChar.UnicodeChar;
821 handle->tty.rd.last_key[0] = '\033';
831 utf16_buffer[0] = handle->tty.rd.last_utf16_high_surrogate;
837 &handle->tty.rd.last_key[prefix_len],
838 sizeof handle->tty.rd.last_key,
847 &handle->tty.rd.last_key[prefix_len],
848 sizeof handle->tty.rd.last_key,
854 handle->tty.rd.last_utf16_high_surrogate = 0;
859 handle->flags &= ~UV_HANDLE_READING;
860 DECREASE_ACTIVE_COUNT(loop, handle);
861 handle->read_cb((uv_stream_t*) handle,
867 handle->tty.rd.last_key_len = (unsigned char) (prefix_len + char_len);
868 handle->tty.rd.last_key_offset = 0;
890 handle->tty.rd.last_key[0] = '\033';
896 /* Copy the vt100 sequence to the handle buffer. */
897 assert(prefix_len + vt100_len < sizeof handle->tty.rd.last_key);
898 memcpy(&handle->tty.rd.last_key[prefix_len], vt100, vt100_len);
900 handle->tty.rd.last_key_len = (unsigned char) (prefix_len + vt100_len);
901 handle->tty.rd.last_key_offset = 0;
906 if (handle->tty.rd.last_key_offset < handle->tty.rd.last_key_len) {
910 handle->alloc_cb((uv_handle_t*) handle, 1024, &buf);
912 handle->read_cb((uv_stream_t*) handle, UV_ENOBUFS, &buf);
918 buf.base[buf_used++] = handle->tty.rd.last_key[handle->tty.rd.last_key_offset++];
922 handle->read_cb((uv_stream_t*) handle, buf_used, &buf);
932 handle->tty.rd.last_key_offset = 0;
936 handle->tty.rd.last_key_len = 0;
943 handle->read_cb((uv_stream_t*) handle, buf_used, &buf);
948 if ((handle->flags & UV_HANDLE_READING) &&
949 !(handle->flags & UV_HANDLE_READ_PENDING)) {
950 uv__tty_queue_read(loop, handle);
953 DECREASE_PENDING_REQ_COUNT(handle);
960 void uv_process_tty_read_line_req(uv_loop_t* loop, uv_tty_t* handle,
964 assert(handle->type == UV_TTY);
965 assert(handle->flags & UV_HANDLE_TTY_READABLE);
967 buf = handle->tty.rd.read_line_buffer;
969 handle->flags &= ~UV_HANDLE_READ_PENDING;
970 handle->tty.rd.read_line_buffer = uv_null_buf_;
974 if (handle->flags & UV_HANDLE_READING) {
976 handle->flags &= ~UV_HANDLE_READING;
977 DECREASE_ACTIVE_COUNT(loop, handle);
978 handle->read_cb((uv_stream_t*) handle,
983 if (!(handle->flags & UV_HANDLE_CANCELLATION_PENDING) &&
987 handle->read_cb((uv_stream_t*) handle, bytes, &buf);
989 handle->flags &= ~UV_HANDLE_CANCELLATION_PENDING;
993 if ((handle->flags & UV_HANDLE_READING) &&
994 !(handle->flags & UV_HANDLE_READ_PENDING)) {
995 uv__tty_queue_read(loop, handle);
998 DECREASE_PENDING_REQ_COUNT(handle);
1002 void uv__process_tty_read_req(uv_loop_t* loop, uv_tty_t* handle,
1004 assert(handle->type == UV_TTY);
1005 assert(handle->flags & UV_HANDLE_TTY_READABLE);
1010 if (handle->tty.rd.read_line_buffer.len == 0) {
1011 uv_process_tty_read_raw_req(loop, handle, req);
1013 uv_process_tty_read_line_req(loop, handle, req);
1018 int uv__tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb,
1020 uv_loop_t* loop = handle->loop;
1022 if (!(handle->flags & UV_HANDLE_TTY_READABLE)) {
1026 handle->flags |= UV_HANDLE_READING;
1027 INCREASE_ACTIVE_COUNT(loop, handle);
1028 handle->read_cb = read_cb;
1029 handle->alloc_cb = alloc_cb;
1033 if (handle->flags & UV_HANDLE_READ_PENDING) {
1039 if (handle->tty.rd.last_key_len > 0) {
1040 SET_REQ_SUCCESS(&handle->read_req);
1041 uv__insert_pending_req(handle->loop, (uv_req_t*) &handle->read_req);
1043 handle->flags |= UV_HANDLE_READ_PENDING;
1044 handle->reqs_pending++;
1048 uv__tty_queue_read(loop, handle);
1054 int uv__tty_read_stop(uv_tty_t* handle) {
1058 handle->flags &= ~UV_HANDLE_READING;
1059 DECREASE_ACTIVE_COUNT(handle->loop, handle);
1061 if (!(handle->flags & UV_HANDLE_READ_PENDING))
1064 if (handle->flags & UV_HANDLE_TTY_RAW) {
1069 if (!WriteConsoleInputW(handle->handle, &record, 1, &written)) {
1072 } else if (!(handle->flags & UV_HANDLE_CANCELLATION_PENDING)) {
1074 err = uv__cancel_read_console(handle);
1078 handle->flags |= UV_HANDLE_CANCELLATION_PENDING;
1084 static int uv__cancel_read_console(uv_tty_t* handle) {
1091 assert(!(handle->flags & UV_HANDLE_CANCELLATION_PENDING));
1130 if (!WriteConsoleInputW(handle->handle, &record, 1, &written))
1163 static COORD uv__tty_make_real_coord(uv_tty_t* handle,
1200 static int uv__tty_emit_text(uv_tty_t* handle, WCHAR buffer[], DWORD length,
1208 if (!WriteConsoleW(handle->handle,
1221 static int uv__tty_move_caret(uv_tty_t* handle, int x, unsigned char x_relative,
1231 if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1235 pos = uv__tty_make_real_coord(handle, &info, x, x_relative, y, y_relative);
1237 if (!SetConsoleCursorPosition(handle->handle, pos)) {
1251 static int uv__tty_reset(uv_tty_t* handle, DWORD* error) {
1262 if (!SetConsoleTextAttribute(handle->handle, char_attrs)) {
1268 if (!SetConsoleCursorPosition(handle->handle, origin)) {
1275 if (!GetConsoleScreenBufferInfo(handle->handle, &screen_buffer_info)) {
1282 if (!(FillConsoleOutputCharacterW(handle->handle,
1287 FillConsoleOutputAttribute(handle->handle,
1306 if (!SetConsoleCursorInfo(handle->handle, &uv_tty_default_cursor_info)) {
1315 static int uv__tty_clear(uv_tty_t* handle, int dir, char entire_screen,
1362 if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1367 start = uv__tty_make_real_coord(handle, &info, x1, x1r, y1, y1r);
1368 end = uv__tty_make_real_coord(handle, &info, x2, x2r, y2, y2r);
1372 if (!(FillConsoleOutputCharacterW(handle->handle,
1377 FillConsoleOutputAttribute(handle->handle,
1403 static int uv__tty_set_style(uv_tty_t* handle, DWORD* error) {
1404 unsigned short argc = handle->tty.wr.ansi_csi_argc;
1405 unsigned short* argv = handle->tty.wr.ansi_csi_argv;
1499 if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1550 if (!SetConsoleTextAttribute(handle->handle, info.wAttributes)) {
1559 static int uv__tty_save_state(uv_tty_t* handle, unsigned char save_attributes,
1567 if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1574 handle->tty.wr.saved_position.X = info.dwCursorPosition.X;
1575 handle->tty.wr.saved_position.Y = info.dwCursorPosition.Y -
1577 handle->flags |= UV_HANDLE_TTY_SAVED_POSITION;
1580 handle->tty.wr.saved_attributes = info.wAttributes &
1582 handle->flags |= UV_HANDLE_TTY_SAVED_ATTRIBUTES;
1589 static int uv__tty_restore_state(uv_tty_t* handle,
1598 if (handle->flags & UV_HANDLE_TTY_SAVED_POSITION) {
1599 if (uv__tty_move_caret(handle,
1600 handle->tty.wr.saved_position.X,
1602 handle->tty.wr.saved_position.Y,
1610 (handle->flags & UV_HANDLE_TTY_SAVED_ATTRIBUTES)) {
1611 if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1618 new_attributes |= handle->tty.wr.saved_attributes;
1620 if (!SetConsoleTextAttribute(handle->handle, new_attributes)) {
1629 static int uv__tty_set_cursor_visibility(uv_tty_t* handle,
1634 if (!GetConsoleCursorInfo(handle->handle, &cursor_info)) {
1641 if (!SetConsoleCursorInfo(handle->handle, &cursor_info)) {
1649 static int uv__tty_set_cursor_shape(uv_tty_t* handle, int style, DWORD* error) {
1652 if (!GetConsoleCursorInfo(handle->handle, &cursor_info)) {
1665 if (!SetConsoleCursorInfo(handle->handle, &cursor_info)) {
1674 static int uv__tty_write_bufs(uv_tty_t* handle,
1678 /* We can only write 8k characters at a time. Windows can't handle much more
1687 uv__tty_emit_text(handle, utf16_buf, utf16_buf_used, error); \
1698 unsigned char utf8_bytes_left = handle->tty.wr.utf8_bytes_left;
1699 unsigned int utf8_codepoint = handle->tty.wr.utf8_codepoint;
1700 unsigned char previous_eol = handle->tty.wr.previous_eol;
1701 unsigned short ansi_parser_state = handle->tty.wr.ansi_parser_state;
1779 handle->tty.wr.ansi_csi_argc = 0;
1787 handle->tty.wr.ansi_csi_argc = 0;
1806 uv__tty_reset(handle, error);
1813 uv__tty_save_state(handle, 1, error);
1820 uv__tty_restore_state(handle, 1, error);
1849 int style = handle->tty.wr.ansi_csi_argc
1850 ? handle->tty.wr.ansi_csi_argv[0] : 1;
1853 uv__tty_set_cursor_shape(handle, style, error);
1874 if (handle->tty.wr.ansi_csi_argc >=
1875 ARRAY_SIZE(handle->tty.wr.ansi_csi_argv)) {
1880 handle->tty.wr.ansi_csi_argc++;
1881 handle->tty.wr.ansi_csi_argv[handle->tty.wr.ansi_csi_argc - 1] =
1888 handle->tty.wr.ansi_csi_argv[handle->tty.wr.ansi_csi_argc - 1];
1896 handle->tty.wr.ansi_csi_argv[handle->tty.wr.ansi_csi_argc - 1] =
1912 if (handle->tty.wr.ansi_csi_argc >=
1914 ARRAY_SIZE(handle->tty.wr.ansi_csi_argv)) {
1919 handle->tty.wr.ansi_csi_argc++;
1920 handle->tty.wr.ansi_csi_argv[handle->tty.wr.ansi_csi_argc - 1] = 0;
1927 handle->tty.wr.ansi_csi_argc == 0) {
1948 if (handle->tty.wr.ansi_csi_argc == 1 &&
1949 handle->tty.wr.ansi_csi_argv[0] == 25) {
1951 uv__tty_set_cursor_visibility(handle, 0, error);
1957 if (handle->tty.wr.ansi_csi_argc == 1 &&
1958 handle->tty.wr.ansi_csi_argv[0] == 25) {
1960 uv__tty_set_cursor_visibility(handle, 1, error);
1972 y = -(handle->tty.wr.ansi_csi_argc
1973 ? handle->tty.wr.ansi_csi_argv[0] : 1);
1974 uv__tty_move_caret(handle, 0, 1, y, 1, error);
1980 y = handle->tty.wr.ansi_csi_argc
1981 ? handle->tty.wr.ansi_csi_argv[0] : 1;
1982 uv__tty_move_caret(handle, 0, 1, y, 1, error);
1988 x = handle->tty.wr.ansi_csi_argc
1989 ? handle->tty.wr.ansi_csi_argv[0] : 1;
1990 uv__tty_move_caret(handle, x, 1, 0, 1, error);
1996 x = -(handle->tty.wr.ansi_csi_argc
1997 ? handle->tty.wr.ansi_csi_argv[0] : 1);
1998 uv__tty_move_caret(handle, x, 1, 0, 1, error);
2004 y = handle->tty.wr.ansi_csi_argc
2005 ? handle->tty.wr.ansi_csi_argv[0] : 1;
2006 uv__tty_move_caret(handle, 0, 0, y, 1, error);
2012 y = -(handle->tty.wr.ansi_csi_argc
2013 ? handle->tty.wr.ansi_csi_argv[0] : 1);
2014 uv__tty_move_caret(handle, 0, 0, y, 1, error);
2020 x = (handle->tty.wr.ansi_csi_argc >= 1 &&
2021 handle->tty.wr.ansi_csi_argv[0])
2022 ? handle->tty.wr.ansi_csi_argv[0] - 1 : 0;
2023 uv__tty_move_caret(handle, x, 0, 0, 1, error);
2030 y = (handle->tty.wr.ansi_csi_argc >= 1 &&
2031 handle->tty.wr.ansi_csi_argv[0])
2032 ? handle->tty.wr.ansi_csi_argv[0] - 1 : 0;
2033 x = (handle->tty.wr.ansi_csi_argc >= 2 &&
2034 handle->tty.wr.ansi_csi_argv[1])
2035 ? handle->tty.wr.ansi_csi_argv[1] - 1 : 0;
2036 uv__tty_move_caret(handle, x, 0, y, 0, error);
2042 d = handle->tty.wr.ansi_csi_argc
2043 ? handle->tty.wr.ansi_csi_argv[0] : 0;
2045 uv__tty_clear(handle, d, 1, error);
2052 d = handle->tty.wr.ansi_csi_argc
2053 ? handle->tty.wr.ansi_csi_argv[0] : 0;
2055 uv__tty_clear(handle, d, 0, error);
2062 uv__tty_set_style(handle, error);
2068 uv__tty_save_state(handle, 0, error);
2074 uv__tty_restore_state(handle, 0, error);
2166 handle->tty.wr.utf8_bytes_left = utf8_bytes_left;
2167 handle->tty.wr.utf8_codepoint = utf8_codepoint;
2168 handle->tty.wr.previous_eol = previous_eol;
2169 handle->tty.wr.ansi_parser_state = ansi_parser_state;
2185 uv_tty_t* handle,
2192 req->handle = (uv_stream_t*) handle;
2195 handle->reqs_pending++;
2196 handle->stream.conn.write_reqs_pending++;
2197 REGISTER_HANDLE_REQ(loop, handle, req);
2201 if (!uv__tty_write_bufs(handle, bufs, nbufs, &error)) {
2213 int uv__tty_try_write(uv_tty_t* handle,
2218 if (handle->stream.conn.write_reqs_pending > 0)
2221 if (uv__tty_write_bufs(handle, bufs, nbufs, &error))
2228 void uv__process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle,
2232 handle->write_queue_size -= req->u.io.queued_bytes;
2233 UNREGISTER_HANDLE_REQ(loop, handle, req);
2241 handle->stream.conn.write_reqs_pending--;
2242 if (handle->stream.conn.write_reqs_pending == 0)
2243 if (handle->flags & UV_HANDLE_SHUTTING)
2245 handle,
2246 handle->stream.conn.shutdown_req);
2248 DECREASE_PENDING_REQ_COUNT(handle);
2252 void uv__tty_close(uv_tty_t* handle) {
2253 assert(handle->u.fd == -1 || handle->u.fd > 2);
2254 if (handle->flags & UV_HANDLE_READING)
2255 uv__tty_read_stop(handle);
2257 if (handle->u.fd == -1)
2258 CloseHandle(handle->handle);
2260 close(handle->u.fd);
2262 handle->u.fd = -1;
2263 handle->handle = INVALID_HANDLE_VALUE;
2264 handle->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
2265 uv__handle_closing(handle);
2267 if (handle->reqs_pending == 0)
2268 uv__want_endgame(handle->loop, (uv_handle_t*) handle);
2293 void uv__tty_endgame(uv_loop_t* loop, uv_tty_t* handle) {
2294 assert(handle->flags & UV_HANDLE_CLOSING);
2295 assert(handle->reqs_pending == 0);
2297 /* The wait handle used for raw reading should be unregistered when the
2299 assert(!(handle->flags & UV_HANDLE_TTY_READABLE) ||
2300 handle->tty.rd.read_raw_wait == NULL);
2302 assert(!(handle->flags & UV_HANDLE_CLOSED));
2303 uv__handle_close(handle);
2311 void uv__process_tty_accept_req(uv_loop_t* loop, uv_tty_t* handle,
2321 void uv__process_tty_connect_req(uv_loop_t* loop, uv_tty_t* handle,
2336 static void uv__determine_vterm_state(HANDLE handle) {
2340 if (!GetConsoleMode(handle, &dwMode)) {
2345 if (!SetConsoleMode(handle, dwMode)) {