io-console 0.8.2 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,12 +4,17 @@
4
4
  */
5
5
 
6
6
  static const char *const
7
- IO_CONSOLE_VERSION = "0.8.2";
7
+ IO_CONSOLE_VERSION = "0.9.5";
8
8
 
9
9
  #include "ruby.h"
10
10
  #include "ruby/io.h"
11
11
  #include "ruby/thread.h"
12
12
 
13
+ #ifndef HAVE_RB_LEN_T
14
+ typedef long rb_len_t;
15
+ # define PRIdLEN "ld"
16
+ #endif
17
+
13
18
  #ifdef HAVE_UNISTD_H
14
19
  #include <unistd.h>
15
20
  #endif
@@ -56,6 +61,13 @@ typedef struct sgttyb conmode;
56
61
  #include <conio.h>
57
62
  typedef DWORD conmode;
58
63
 
64
+ #ifndef ENABLE_WRAP_AT_EOL_OUTPUT
65
+ # define ENABLE_WRAP_AT_EOL_OUTPUT 0x0002
66
+ #endif
67
+ #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
68
+ # define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
69
+ #endif
70
+
59
71
  #define LAST_ERROR rb_w32_map_errno(GetLastError())
60
72
  #define SET_LAST_ERROR (errno = LAST_ERROR, 0)
61
73
 
@@ -81,7 +93,7 @@ getattr(int fd, conmode *t)
81
93
 
82
94
  #define CSI "\x1b\x5b"
83
95
 
84
- static ID id_getc, id_close;
96
+ static ID id_getc, id_close, id_timeout;
85
97
  static ID id_gets, id_flush, id_chomp_bang;
86
98
 
87
99
  #ifndef HAVE_RB_INTERNED_STR_CSTR
@@ -89,6 +101,10 @@ static ID id_gets, id_flush, id_chomp_bang;
89
101
  # define rb_interned_str_cstr(str) rb_str_freeze(rb_usascii_str_new_cstr(str))
90
102
  #endif
91
103
 
104
+ #if !defined(HAVE_RB_CATEGORY_WARN) || !defined(HAVE_CONST_RB_WARN_CATEGORY_DEPRECATED)
105
+ # define rb_category_warn(category, ...) rb_warn(__VA_ARGS__)
106
+ #endif
107
+
92
108
  #if defined HAVE_RUBY_FIBER_SCHEDULER_H
93
109
  # include "ruby/fiber/scheduler.h"
94
110
  #elif defined HAVE_RB_SCHEDULER_TIMEOUT
@@ -118,22 +134,6 @@ io_path_fallback(VALUE io)
118
134
  #define rb_io_path io_path_fallback
119
135
  #endif
120
136
 
121
- #ifndef HAVE_RB_IO_GET_WRITE_IO
122
- static VALUE
123
- io_get_write_io_fallback(VALUE io)
124
- {
125
- rb_io_t *fptr;
126
- GetOpenFile(io, fptr);
127
- VALUE wio = fptr->tied_io_for_writing;
128
- return wio ? wio : io;
129
- }
130
- #define rb_io_get_write_io io_get_write_io_fallback
131
- #endif
132
-
133
- #ifndef DHAVE_RB_SYSERR_FAIL_STR
134
- # define rb_syserr_fail_str(e, mesg) rb_exc_raise(rb_syserr_new_str(e, mesg))
135
- #endif
136
-
137
137
  #define sys_fail(io) do { \
138
138
  int err = errno; \
139
139
  rb_syserr_fail_str(err, rb_io_path(io)); \
@@ -183,6 +183,22 @@ typedef struct {
183
183
  # define NIL_OR_UNDEF_P(obj) (NIL_P(obj) || UNDEF_P(obj))
184
184
  #endif
185
185
 
186
+ static int
187
+ to_vtime(VALUE vtime)
188
+ {
189
+ VALUE v10 = INT2FIX(10);
190
+ vtime = rb_funcall3(vtime, '*', 1, &v10);
191
+ return NUM2INT(vtime);
192
+ }
193
+
194
+ static unsigned char
195
+ clamp_uchar(int n)
196
+ {
197
+ if ((unsigned int)n >= UCHAR_MAX) n = UCHAR_MAX;
198
+ else if (n < 0) n = 0;
199
+ return (unsigned char)n;
200
+ }
201
+
186
202
  static rawmode_arg_t *
187
203
  rawmode_opt(int *argcp, VALUE *argv, int min_argc, int max_argc, rawmode_arg_t *opts)
188
204
  {
@@ -214,13 +230,11 @@ rawmode_opt(int *argcp, VALUE *argv, int min_argc, int max_argc, rawmode_arg_t *
214
230
  opts->vtime = 0;
215
231
  opts->intr = 0;
216
232
  if (!NIL_OR_UNDEF_P(vmin)) {
217
- opts->vmin = NUM2INT(vmin);
233
+ opts->vmin = clamp_uchar(NUM2INT(vmin));
218
234
  optp = opts;
219
235
  }
220
236
  if (!NIL_OR_UNDEF_P(vtime)) {
221
- VALUE v10 = INT2FIX(10);
222
- vtime = rb_funcall3(vtime, '*', 1, &v10);
223
- opts->vtime = NUM2INT(vtime);
237
+ opts->vtime = clamp_uchar(to_vtime(vtime));
224
238
  optp = opts;
225
239
  }
226
240
  switch (intr) {
@@ -550,7 +564,7 @@ nogvl_getch(void *p)
550
564
 
551
565
  /*
552
566
  * call-seq:
553
- * io.getch(min: nil, time: nil, intr: nil) -> char
567
+ * io.getch(min: nil, time: nil, intr: nil) -> char or nil
554
568
  *
555
569
  * Reads and returns a character in raw mode.
556
570
  *
@@ -635,6 +649,43 @@ console_getch(int argc, VALUE *argv, VALUE io)
635
649
  #endif
636
650
  }
637
651
 
652
+ /*
653
+ * call-seq:
654
+ * io.input_pending? -> true or false
655
+ *
656
+ * Returns whether input can be read without blocking.
657
+ *
658
+ * You must require 'io/console' to use this method.
659
+ */
660
+ static VALUE
661
+ console_input_pending_p(VALUE io)
662
+ {
663
+ rb_io_t *fptr;
664
+
665
+ GetOpenFile(io, fptr);
666
+ if (rb_io_read_pending(fptr)) return Qtrue;
667
+ #ifdef _WIN32
668
+ {
669
+ DWORD mode;
670
+ HANDLE h = (HANDLE)rb_w32_get_osfhandle(GetReadFD(io));
671
+
672
+ if (GetConsoleMode(h, &mode)) return _kbhit() ? Qtrue : Qfalse;
673
+ }
674
+ #endif
675
+ #if defined HAVE_RB_IO_WAIT
676
+ return RTEST(rb_io_wait(io, RB_INT2NUM(RUBY_IO_READABLE), INT2FIX(0))) ? Qtrue : Qfalse;
677
+ #else
678
+ {
679
+ struct timeval timeout = {0, 0};
680
+ int result;
681
+
682
+ result = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, &timeout);
683
+ if (result < 0) sys_fail(io);
684
+ return (result & RB_WAITFD_IN) ? Qtrue : Qfalse;
685
+ }
686
+ #endif
687
+ }
688
+
638
689
  /*
639
690
  * call-seq:
640
691
  * io.noecho {|io| }
@@ -730,6 +781,13 @@ conmode_init_copy(VALUE obj, VALUE obj2)
730
781
  return obj;
731
782
  }
732
783
 
784
+ static VALUE
785
+ conmode_get_echo(VALUE obj)
786
+ {
787
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
788
+ return echo_p(t) ? Qtrue : Qfalse;
789
+ }
790
+
733
791
  static VALUE
734
792
  conmode_set_echo(VALUE obj, VALUE f)
735
793
  {
@@ -762,6 +820,122 @@ conmode_raw_new(int argc, VALUE *argv, VALUE obj)
762
820
  return conmode_new(rb_obj_class(obj), &t);
763
821
  }
764
822
 
823
+ static VALUE
824
+ conmode_get_min(VALUE obj)
825
+ {
826
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
827
+ #ifdef VMIN
828
+ return INT2FIX(t->c_cc[VMIN]);
829
+ #else
830
+ (void)t;
831
+ return Qnil;
832
+ #endif
833
+ }
834
+
835
+ static VALUE
836
+ conmode_set_min(VALUE obj, VALUE min)
837
+ {
838
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
839
+ int vmin = NIL_P(min) ? 1 : clamp_uchar(NUM2INT(min));
840
+ #ifdef VMIN
841
+ t->c_cc[VMIN] = vmin;
842
+ #else
843
+ (void)t;
844
+ (void)vmin;
845
+ #endif
846
+ return min;
847
+ }
848
+
849
+ static VALUE
850
+ conmode_get_time(VALUE obj)
851
+ {
852
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
853
+ #ifdef VTIME
854
+ return rb_rational_new(INT2FIX(t->c_cc[VTIME]), INT2FIX(10));
855
+ #else
856
+ (void)t;
857
+ return Qnil;
858
+ #endif
859
+ }
860
+
861
+ static VALUE
862
+ conmode_set_time(VALUE obj, VALUE time)
863
+ {
864
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
865
+ int vtime = NIL_P(time) ? 0 : clamp_uchar(to_vtime(time));
866
+ #ifdef VTIME
867
+ t->c_cc[VTIME] = vtime;
868
+ #else
869
+ (void)t;
870
+ (void)vtime;
871
+ #endif
872
+ return time;
873
+ }
874
+
875
+ #ifdef _WIN32
876
+ /*
877
+ * call-seq:
878
+ * mode.virtual_terminal_processing? -> true or false
879
+ *
880
+ * Returns whether virtual terminal sequences are processed on output.
881
+ */
882
+ static VALUE
883
+ conmode_virtual_terminal_processing_p(VALUE obj)
884
+ {
885
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
886
+ return (*t & ENABLE_VIRTUAL_TERMINAL_PROCESSING) ? Qtrue : Qfalse;
887
+ }
888
+
889
+ /*
890
+ * call-seq:
891
+ * mode.virtual_terminal_processing = enabled
892
+ *
893
+ * Enables or disables virtual terminal sequence processing in +mode+.
894
+ * Assign +mode+ to IO#console_mode= to apply the change.
895
+ */
896
+ static VALUE
897
+ conmode_set_virtual_terminal_processing(VALUE obj, VALUE enabled)
898
+ {
899
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
900
+ if (RTEST(enabled))
901
+ *t |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
902
+ else
903
+ *t &= ~ENABLE_VIRTUAL_TERMINAL_PROCESSING;
904
+ return obj;
905
+ }
906
+
907
+ /*
908
+ * call-seq:
909
+ * mode.wrap_at_eol_output? -> true or false
910
+ *
911
+ * Returns whether output wraps at the end of a line.
912
+ */
913
+ static VALUE
914
+ conmode_wrap_at_eol_output_p(VALUE obj)
915
+ {
916
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
917
+ return (*t & ENABLE_WRAP_AT_EOL_OUTPUT) ? Qtrue : Qfalse;
918
+ }
919
+
920
+ /*
921
+ * call-seq:
922
+ * mode.wrap_at_eol_output = enabled
923
+ *
924
+ * Enables or disables wrapping at the end of a line in +mode+.
925
+ * Assign +mode+ to IO#console_mode= to apply the change.
926
+ */
927
+ static VALUE
928
+ conmode_set_wrap_at_eol_output(VALUE obj, VALUE enabled)
929
+ {
930
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
931
+ if (RTEST(enabled))
932
+ *t |= ENABLE_WRAP_AT_EOL_OUTPUT;
933
+ else
934
+ *t &= ~ENABLE_WRAP_AT_EOL_OUTPUT;
935
+ return obj;
936
+ }
937
+ #endif
938
+
765
939
  /*
766
940
  * call-seq:
767
941
  * io.console_mode -> mode
@@ -861,16 +1035,15 @@ console_set_winsize(VALUE io, VALUE size)
861
1035
  int newrow, newcol;
862
1036
  COORD oldsize;
863
1037
  SMALL_RECT oldwindow;
864
- BOOL ret;
865
1038
  #endif
866
1039
  VALUE row, col, xpixel, ypixel;
867
1040
  const VALUE *sz;
868
- long sizelen;
1041
+ rb_len_t sizelen;
869
1042
  int fd;
870
1043
 
871
1044
  size = rb_Array(size);
872
1045
  if ((sizelen = RARRAY_LEN(size)) != 2 && sizelen != 4) {
873
- rb_raise(rb_eArgError, "wrong number of arguments (given %ld, expected 2 or 4)", sizelen);
1046
+ rb_raise(rb_eArgError, "wrong number of arguments (given %"PRIdLEN", expected 2 or 4)", sizelen);
874
1047
  }
875
1048
  sz = RARRAY_CONST_PTR(size);
876
1049
  row = sz[0], col = sz[1], xpixel = ypixel = Qnil;
@@ -963,12 +1136,207 @@ console_set_winsize(VALUE io, VALUE size)
963
1136
  #endif
964
1137
 
965
1138
  #ifdef _WIN32
1139
+ enum console_input_handle_index {
1140
+ console_input_handle,
1141
+ console_input_wakeup,
1142
+ console_input_handle_count
1143
+ };
1144
+
1145
+ typedef struct {
1146
+ HANDLE handles[console_input_handle_count];
1147
+ INPUT_RECORD *records;
1148
+ DWORD length;
1149
+ DWORD count;
1150
+ DWORD timeout;
1151
+ DWORD wait_result;
1152
+ DWORD error;
1153
+ BOOL result;
1154
+ } read_console_input_args_t;
1155
+
1156
+ static void *
1157
+ nogvl_read_console_input(void *ptr)
1158
+ {
1159
+ read_console_input_args_t *args = ptr;
1160
+
1161
+ args->wait_result = WaitForMultipleObjects(console_input_handle_count,
1162
+ args->handles, FALSE, args->timeout);
1163
+ if (args->wait_result == WAIT_OBJECT_0 + console_input_handle) {
1164
+ args->result = ReadConsoleInputW(args->handles[console_input_handle],
1165
+ args->records, args->length, &args->count);
1166
+ if (!args->result) args->error = GetLastError();
1167
+ }
1168
+ else if (args->wait_result == WAIT_FAILED) {
1169
+ args->error = GetLastError();
1170
+ }
1171
+ return 0;
1172
+ }
1173
+
1174
+ static void
1175
+ ubf_console_input(void *ptr)
1176
+ {
1177
+ read_console_input_args_t *args = ptr;
1178
+ SetEvent(args->handles[console_input_wakeup]);
1179
+ }
1180
+
1181
+ static void
1182
+ console_input_event_set(VALUE event, const char *name, VALUE value)
1183
+ {
1184
+ rb_hash_aset(event, ID2SYM(rb_intern(name)), value);
1185
+ }
1186
+
1187
+ static VALUE
1188
+ console_input_event(const INPUT_RECORD *record)
1189
+ {
1190
+ VALUE event = rb_hash_new();
1191
+
1192
+ switch (record->EventType) {
1193
+ case KEY_EVENT:
1194
+ console_input_event_set(event, "type", ID2SYM(rb_intern("key")));
1195
+ console_input_event_set(event, "key_down", record->Event.KeyEvent.bKeyDown ? Qtrue : Qfalse);
1196
+ console_input_event_set(event, "repeat_count", UINT2NUM(record->Event.KeyEvent.wRepeatCount));
1197
+ console_input_event_set(event, "virtual_key_code", UINT2NUM(record->Event.KeyEvent.wVirtualKeyCode));
1198
+ console_input_event_set(event, "virtual_scan_code", UINT2NUM(record->Event.KeyEvent.wVirtualScanCode));
1199
+ console_input_event_set(event, "unicode_char", UINT2NUM(record->Event.KeyEvent.uChar.UnicodeChar));
1200
+ console_input_event_set(event, "control_key_state", UINT2NUM(record->Event.KeyEvent.dwControlKeyState));
1201
+ break;
1202
+ case MOUSE_EVENT:
1203
+ console_input_event_set(event, "type", ID2SYM(rb_intern("mouse")));
1204
+ console_input_event_set(event, "position", rb_assoc_new(
1205
+ INT2NUM(record->Event.MouseEvent.dwMousePosition.Y),
1206
+ INT2NUM(record->Event.MouseEvent.dwMousePosition.X)));
1207
+ console_input_event_set(event, "button_state", UINT2NUM(record->Event.MouseEvent.dwButtonState));
1208
+ console_input_event_set(event, "control_key_state", UINT2NUM(record->Event.MouseEvent.dwControlKeyState));
1209
+ console_input_event_set(event, "event_flags", UINT2NUM(record->Event.MouseEvent.dwEventFlags));
1210
+ break;
1211
+ case WINDOW_BUFFER_SIZE_EVENT:
1212
+ console_input_event_set(event, "type", ID2SYM(rb_intern("window_buffer_size")));
1213
+ console_input_event_set(event, "size", rb_assoc_new(
1214
+ INT2NUM(record->Event.WindowBufferSizeEvent.dwSize.Y),
1215
+ INT2NUM(record->Event.WindowBufferSizeEvent.dwSize.X)));
1216
+ break;
1217
+ case MENU_EVENT:
1218
+ console_input_event_set(event, "type", ID2SYM(rb_intern("menu")));
1219
+ console_input_event_set(event, "command_id", UINT2NUM(record->Event.MenuEvent.dwCommandId));
1220
+ break;
1221
+ case FOCUS_EVENT:
1222
+ console_input_event_set(event, "type", ID2SYM(rb_intern("focus")));
1223
+ console_input_event_set(event, "set_focus", record->Event.FocusEvent.bSetFocus ? Qtrue : Qfalse);
1224
+ break;
1225
+ default:
1226
+ console_input_event_set(event, "type", UINT2NUM(record->EventType));
1227
+ break;
1228
+ }
1229
+
1230
+ return event;
1231
+ }
1232
+
1233
+ static VALUE
1234
+ console_input_events_read(VALUE vargs)
1235
+ {
1236
+ read_console_input_args_t *args = (read_console_input_args_t *)vargs;
1237
+ VALUE events;
1238
+ DWORD i;
1239
+
1240
+ rb_thread_call_without_gvl(nogvl_read_console_input, args,
1241
+ ubf_console_input, args);
1242
+ if (args->wait_result == WAIT_TIMEOUT) return rb_ary_new();
1243
+ if (args->wait_result != WAIT_OBJECT_0 + console_input_handle ||
1244
+ !args->result) {
1245
+ rb_syserr_fail(rb_w32_map_errno(args->error), 0);
1246
+ }
1247
+
1248
+ events = rb_ary_new_capa(args->count);
1249
+ for (i = 0; i < args->count; ++i) {
1250
+ rb_ary_push(events, console_input_event(&args->records[i]));
1251
+ }
1252
+ return events;
1253
+ }
1254
+
1255
+ static VALUE
1256
+ console_input_events_ensure(VALUE vargs)
1257
+ {
1258
+ read_console_input_args_t *args = (read_console_input_args_t *)vargs;
1259
+
1260
+ CloseHandle(args->handles[console_input_wakeup]);
1261
+ xfree(args->records);
1262
+ return Qnil;
1263
+ }
1264
+
1265
+ /*
1266
+ * call-seq:
1267
+ * io.console_input_events([max_events], timeout: nil) -> array
1268
+ *
1269
+ * Reads up to +max_events+ console input events, preserving their order.
1270
+ * The default is one event. Blocks until at least one event is available,
1271
+ * or for +timeout+ seconds if specified. Returns an empty Array on timeout.
1272
+ *
1273
+ * Each event is returned as a Hash. The +:type+ and remaining keys are:
1274
+ *
1275
+ * - +:key+ : +:key_down+, +:repeat_count+, +:virtual_key_code+,
1276
+ * +:virtual_scan_code+, +:unicode_char+, and +:control_key_state+.
1277
+ * - +:mouse+ : +:position+ ([row, column]), +:button_state+,
1278
+ * +:control_key_state+, and +:event_flags+.
1279
+ * - +:window_buffer_size+ : +:size+ ([rows, columns]).
1280
+ * - +:menu+ : +:command_id+.
1281
+ * - +:focus+ : +:set_focus+.
1282
+ *
1283
+ * This method is Windows only.
1284
+ *
1285
+ * You must require 'io/console' to use this method.
1286
+ */
1287
+ static VALUE
1288
+ console_input_events(int argc, VALUE *argv, VALUE io)
1289
+ {
1290
+ VALUE vmax = Qnil, vopts = Qnil, vtimeout = Qundef;
1291
+ VALUE values[1];
1292
+ ID keywords[1] = {id_timeout};
1293
+ DWORD max_events = 1;
1294
+ read_console_input_args_t args;
1295
+
1296
+ rb_scan_args(argc, argv, "01:", &vmax, &vopts);
1297
+ if (rb_get_kwargs(vopts, keywords, 0, 1, values)) {
1298
+ vtimeout = values[0];
1299
+ }
1300
+ if (!NIL_P(vmax)) {
1301
+ max_events = NUM2UINT(vmax);
1302
+ if (max_events == 0) rb_raise(rb_eArgError, "max_events must be positive");
1303
+ }
1304
+
1305
+ args.timeout = INFINITE;
1306
+ if (!NIL_OR_UNDEF_P(vtimeout)) {
1307
+ struct timeval timeout = rb_time_interval(vtimeout);
1308
+ uint64_t milliseconds = (uint64_t)timeout.tv_sec * 1000;
1309
+ milliseconds += ((uint64_t)timeout.tv_usec + 999) / 1000;
1310
+ args.timeout = milliseconds < INFINITE ? (DWORD)milliseconds : INFINITE - 1;
1311
+ }
1312
+
1313
+ args.handles[console_input_handle] =
1314
+ (HANDLE)rb_w32_get_osfhandle(GetReadFD(io));
1315
+ args.records = ALLOC_N(INPUT_RECORD, max_events);
1316
+ args.handles[console_input_wakeup] = CreateEvent(NULL, FALSE, FALSE, NULL);
1317
+ if (!args.handles[console_input_wakeup]) {
1318
+ int error = LAST_ERROR;
1319
+ xfree(args.records);
1320
+ rb_syserr_fail(error, 0);
1321
+ }
1322
+ args.length = max_events;
1323
+ args.count = 0;
1324
+ args.wait_result = WAIT_FAILED;
1325
+ args.error = ERROR_SUCCESS;
1326
+ args.result = FALSE;
1327
+ return rb_ensure(console_input_events_read, (VALUE)&args,
1328
+ console_input_events_ensure, (VALUE)&args);
1329
+ }
1330
+
966
1331
  /*
967
1332
  * call-seq:
968
1333
  * io.check_winsize_changed { ... } -> io
969
1334
  *
970
1335
  * Yields while console input events are queued.
971
1336
  *
1337
+ * Deprecated because it discards queued input events other than window buffer
1338
+ * size changes. Use IO#console_input_events instead to preserve all events.
1339
+ *
972
1340
  * This method is Windows only.
973
1341
  *
974
1342
  * You must require 'io/console' to use this method.
@@ -979,6 +1347,9 @@ console_check_winsize_changed(VALUE io)
979
1347
  HANDLE h;
980
1348
  DWORD num;
981
1349
 
1350
+ rb_category_warn(RB_WARN_CATEGORY_DEPRECATED,
1351
+ "IO#check_winsize_changed is deprecated; "
1352
+ "use IO#console_input_events instead");
982
1353
  h = (HANDLE)rb_w32_get_osfhandle(GetReadFD(io));
983
1354
  while (GetNumberOfConsoleInputEvents(h, &num) && num > 0) {
984
1355
  INPUT_RECORD rec;
@@ -991,6 +1362,7 @@ console_check_winsize_changed(VALUE io)
991
1362
  return io;
992
1363
  }
993
1364
  #else
1365
+ #define console_input_events rb_f_notimplement
994
1366
  #define console_check_winsize_changed rb_f_notimplement
995
1367
  #endif
996
1368
 
@@ -1291,6 +1663,54 @@ console_cursor_pos(VALUE io)
1291
1663
  #endif
1292
1664
  }
1293
1665
 
1666
+ static VALUE
1667
+ console_cursor_visibility(VALUE io, int visible)
1668
+ {
1669
+ #ifdef _WIN32
1670
+ HANDLE h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(io));
1671
+ CONSOLE_CURSOR_INFO info;
1672
+
1673
+ if (!GetConsoleCursorInfo(h, &info)) {
1674
+ rb_syserr_fail(LAST_ERROR, 0);
1675
+ }
1676
+ info.bVisible = visible;
1677
+ if (!SetConsoleCursorInfo(h, &info)) {
1678
+ rb_syserr_fail(LAST_ERROR, 0);
1679
+ }
1680
+ #else
1681
+ rb_io_write(io, rb_str_new_cstr(visible ? CSI "?25h" : CSI "?25l"));
1682
+ #endif
1683
+ return io;
1684
+ }
1685
+
1686
+ /*
1687
+ * call-seq:
1688
+ * io.hide_cursor -> io
1689
+ *
1690
+ * Hides the cursor.
1691
+ *
1692
+ * You must require 'io/console' to use this method.
1693
+ */
1694
+ static VALUE
1695
+ console_hide_cursor(VALUE io)
1696
+ {
1697
+ return console_cursor_visibility(io, 0);
1698
+ }
1699
+
1700
+ /*
1701
+ * call-seq:
1702
+ * io.show_cursor -> io
1703
+ *
1704
+ * Shows the cursor.
1705
+ *
1706
+ * You must require 'io/console' to use this method.
1707
+ */
1708
+ static VALUE
1709
+ console_show_cursor(VALUE io)
1710
+ {
1711
+ return console_cursor_visibility(io, 1);
1712
+ }
1713
+
1294
1714
  /*
1295
1715
  * call-seq:
1296
1716
  * io.goto(line, column) -> io
@@ -1710,7 +2130,7 @@ console_dev(int argc, VALUE *argv, VALUE klass)
1710
2130
  Check_Type(sym = argv[0], T_SYMBOL);
1711
2131
  }
1712
2132
 
1713
- // Force the class to be File.
2133
+ /* Force the class to be File. */
1714
2134
  if (klass == rb_cIO) klass = rb_cFile;
1715
2135
 
1716
2136
  if (console_dev_get(klass, &con)) {
@@ -1777,7 +2197,7 @@ console_dev(int argc, VALUE *argv, VALUE klass)
1777
2197
 
1778
2198
  /*
1779
2199
  * call-seq:
1780
- * io.getch(min: nil, time: nil, intr: nil) -> char
2200
+ * io.getch(min: nil, time: nil, intr: nil) -> char or nil
1781
2201
  *
1782
2202
  * See IO#getch.
1783
2203
  */
@@ -1793,10 +2213,17 @@ puts_call(VALUE io)
1793
2213
  return rb_io_write(io, rb_default_rs);
1794
2214
  }
1795
2215
 
2216
+ static VALUE
2217
+ funcall_with_rs(VALUE obj, ID mid)
2218
+ {
2219
+ const VALUE rs = rb_default_rs; /* rvalue in TruffleRuby */
2220
+ return rb_funcallv(obj, mid, 1, &rs);
2221
+ }
2222
+
1796
2223
  static VALUE
1797
2224
  gets_call(VALUE io)
1798
2225
  {
1799
- return rb_funcallv(io, id_gets, 0, 0);
2226
+ return funcall_with_rs(io, id_gets);
1800
2227
  }
1801
2228
 
1802
2229
  static VALUE
@@ -1819,8 +2246,7 @@ static VALUE
1819
2246
  str_chomp(VALUE str)
1820
2247
  {
1821
2248
  if (!NIL_P(str)) {
1822
- const VALUE rs = rb_default_rs; /* rvalue in TruffleRuby */
1823
- rb_funcallv(str, id_chomp_bang, 1, &rs);
2249
+ funcall_with_rs(str, id_chomp_bang);
1824
2250
  }
1825
2251
  return str;
1826
2252
  }
@@ -1881,7 +2307,7 @@ io_getpass(int argc, VALUE *argv, VALUE io)
1881
2307
  * call-seq:
1882
2308
  * io.ttyname -> string or nil
1883
2309
  *
1884
- * Returns name of associated terminal (tty) if +io+ is not a tty.
2310
+ * Returns name of associated terminal (tty) if +io+ is a tty.
1885
2311
  * Returns +nil+ otherwise.
1886
2312
  */
1887
2313
  static VALUE
@@ -1893,20 +2319,23 @@ console_ttyname(VALUE io)
1893
2319
  return rb_usascii_str_new_lit("con");
1894
2320
  # elif defined HAVE_TTYNAME_R
1895
2321
  {
2322
+ # ifdef TTYNAME_R_RETURNS_CHAR_P
2323
+ # define ttyname_r(fd, name, size) (ttyname_r(fd, name, size) == NULL ? errno : 0)
2324
+ # endif
1896
2325
  char termname[1024], *tn = termname;
1897
2326
  size_t size = sizeof(termname);
1898
2327
  int e;
1899
- if (ttyname_r(fd, tn, size) == 0)
2328
+ if ((e = ttyname_r(fd, tn, size)) == 0)
1900
2329
  return rb_interned_str_cstr(tn);
1901
- if ((e = errno) == ERANGE) {
2330
+ if (e == ERANGE) {
1902
2331
  VALUE s = rb_str_new(0, size);
1903
2332
  while (1) {
1904
2333
  tn = RSTRING_PTR(s);
1905
2334
  size = rb_str_capacity(s);
1906
- if (ttyname_r(fd, tn, size) == 0) {
2335
+ if ((e = ttyname_r(fd, tn, size)) == 0) {
1907
2336
  return rb_str_to_interned_str(rb_str_resize(s, strlen(tn)));
1908
2337
  }
1909
- if ((e = errno) != ERANGE) break;
2338
+ if (e != ERANGE) break;
1910
2339
  if ((size *= 2) >= INT_MAX/2) break;
1911
2340
  rb_str_resize(s, size);
1912
2341
  }
@@ -1931,6 +2360,103 @@ console_ttyname(VALUE io)
1931
2360
  # define console_ttyname rb_f_notimplement
1932
2361
  #endif
1933
2362
 
2363
+ typedef enum {
2364
+ platform_default,
2365
+ #if defined _WIN32
2366
+ platform_cygwin,
2367
+ platform_msys,
2368
+ #endif
2369
+ platform_any,
2370
+
2371
+ platform_default_bit = 1U << platform_default,
2372
+ #if defined _WIN32
2373
+ platform_cygwin_bit = 1U << platform_cygwin,
2374
+ platform_msys_bit = 1U << platform_msys,
2375
+ #endif
2376
+ platform_any_bit = (1U << platform_any) - 1 /* all bits */
2377
+ } console_platform_t;
2378
+
2379
+ /*
2380
+ * call-seq:
2381
+ * io.tty?([mode, ...]) -> true or false
2382
+ *
2383
+ * Returns +true+ if the stream is associated with a terminal device (tty),
2384
+ * +false+ otherwise.
2385
+ *
2386
+ * If one or more +type+s are given, returns +true+ if the stream is
2387
+ * associated with any of the specified tty types.
2388
+ *
2389
+ * - +nil+ : Returns the result of the default tty check, as if no type were
2390
+ * given. It can be combined with other types.
2391
+ * - +:any+ : Returns +true+ for any known kind of tty, including the
2392
+ * default tty.
2393
+ * - +:cygwin+ : Returns +true+ for cygwin tty, on Windows.
2394
+ * - +:msys+ : Returns +true+ for msys2 tty, on Windows.
2395
+ */
2396
+ static VALUE
2397
+ console_platform_tty_p(int argc, VALUE *argv, VALUE io)
2398
+ {
2399
+ VALUE ret = Qfalse;
2400
+ int mode = 0;
2401
+
2402
+ if (argc > 0) {
2403
+ int i;
2404
+ for (i = 0; i < argc; ++i) {
2405
+ VALUE m = argv[i];
2406
+ if (NIL_P(m)) {
2407
+ mode |= platform_default_bit;
2408
+ continue;
2409
+ }
2410
+ Check_Type(m, T_SYMBOL);
2411
+ if (m == ID2SYM(rb_intern("any"))) {
2412
+ mode |= platform_any_bit;
2413
+ }
2414
+ #if defined _WIN32
2415
+ else if (m == ID2SYM(rb_intern("cygwin"))) {
2416
+ mode |= platform_cygwin_bit;
2417
+ }
2418
+ else if (m == ID2SYM(rb_intern("msys"))) {
2419
+ mode |= platform_msys_bit;
2420
+ }
2421
+ #endif
2422
+ else {
2423
+ rb_raise(rb_eArgError, "unknown tty type: %+" PRIsVALUE, m);
2424
+ }
2425
+ }
2426
+ }
2427
+ if ((mode & platform_default_bit) || (mode == 0)) {
2428
+ ret = rb_call_super(0, 0);
2429
+ }
2430
+ if ((mode & ~platform_default_bit) && !RTEST(ret)) {
2431
+ #if defined _WIN32
2432
+ if (mode & (platform_cygwin_bit | platform_msys_bit)) {
2433
+ struct {
2434
+ FILE_NAME_INFO info;
2435
+ WCHAR rest[MAX_PATH];
2436
+ } buffer;
2437
+
2438
+ HANDLE h = (HANDLE)rb_w32_get_osfhandle(GetReadFD(io));
2439
+ if ((GetFileType(h) == FILE_TYPE_PIPE) &&
2440
+ GetFileInformationByHandleEx(h, FileNameInfo, &buffer, sizeof(buffer))) {
2441
+ WCHAR *const name = buffer.info.FileName;
2442
+ DWORD len = buffer.info.FileNameLength / sizeof(WCHAR);
2443
+ name[len] = L'\0';
2444
+ # define tty_pipe_p(type) \
2445
+ (memcmp(name, L"\\" #type "-", sizeof(L"\\" #type)) == 0 && \
2446
+ wcsstr(&name[rb_strlen_lit("\\" #type "-")], L"-pty") != NULL)
2447
+ if (!ret && (mode & platform_cygwin_bit)) {
2448
+ ret = tty_pipe_p(cygwin);
2449
+ }
2450
+ if (!ret && (mode & platform_msys_bit)) {
2451
+ ret = tty_pipe_p(msys);
2452
+ }
2453
+ }
2454
+ }
2455
+ #endif
2456
+ }
2457
+ return ret;
2458
+ }
2459
+
1934
2460
  /*
1935
2461
  * IO console methods
1936
2462
  */
@@ -1952,6 +2478,7 @@ Init_console(void)
1952
2478
  id_flush = rb_intern("flush");
1953
2479
  id_chomp_bang = rb_intern("chomp!");
1954
2480
  id_close = rb_intern("close");
2481
+ id_timeout = rb_intern("timeout");
1955
2482
  #define init_rawmode_opt_id(name) \
1956
2483
  rawmode_opt_ids[kwd_##name] = rb_intern(#name)
1957
2484
  init_rawmode_opt_id(min);
@@ -1966,11 +2493,31 @@ Init_console(void)
1966
2493
  void
1967
2494
  InitVM_console(void)
1968
2495
  {
2496
+ /* :nodoc: */
2497
+ VALUE mConsole = rb_define_module_under(rb_cIO, "Console");
2498
+ #ifdef _WIN32
2499
+ /* :nodoc: */
2500
+ VALUE mWindows = rb_define_module_under(mConsole, "Windows");
2501
+ #define define_win32_const(name) rb_define_const(mWindows, #name, UINT2NUM(name))
2502
+ EACH_VK(define_win32_const,;);
2503
+ define_win32_const(RIGHT_ALT_PRESSED);
2504
+ define_win32_const(LEFT_ALT_PRESSED);
2505
+ define_win32_const(RIGHT_CTRL_PRESSED);
2506
+ define_win32_const(LEFT_CTRL_PRESSED);
2507
+ define_win32_const(SHIFT_PRESSED);
2508
+ define_win32_const(NUMLOCK_ON);
2509
+ define_win32_const(SCROLLLOCK_ON);
2510
+ define_win32_const(CAPSLOCK_ON);
2511
+ define_win32_const(ENHANCED_KEY);
2512
+ #undef define_win32_const
2513
+ #endif
2514
+
1969
2515
  rb_define_method(rb_cIO, "raw", console_raw, -1);
1970
2516
  rb_define_method(rb_cIO, "raw!", console_set_raw, -1);
1971
2517
  rb_define_method(rb_cIO, "cooked", console_cooked, 0);
1972
2518
  rb_define_method(rb_cIO, "cooked!", console_set_cooked, 0);
1973
2519
  rb_define_method(rb_cIO, "getch", console_getch, -1);
2520
+ rb_define_method(rb_cIO, "input_pending?", console_input_pending_p, 0);
1974
2521
  rb_define_method(rb_cIO, "echo=", console_set_echo, 1);
1975
2522
  rb_define_method(rb_cIO, "echo?", console_echo_p, 0);
1976
2523
  rb_define_method(rb_cIO, "console_mode", console_conmode_get, 0);
@@ -1985,6 +2532,8 @@ InitVM_console(void)
1985
2532
  rb_define_method(rb_cIO, "goto", console_goto, 2);
1986
2533
  rb_define_method(rb_cIO, "cursor", console_cursor_pos, 0);
1987
2534
  rb_define_method(rb_cIO, "cursor=", console_cursor_set, 1);
2535
+ rb_define_method(rb_cIO, "hide_cursor", console_hide_cursor, 0);
2536
+ rb_define_method(rb_cIO, "show_cursor", console_show_cursor, 0);
1988
2537
  rb_define_method(rb_cIO, "cursor_up", console_cursor_up, 1);
1989
2538
  rb_define_method(rb_cIO, "cursor_down", console_cursor_down, 1);
1990
2539
  rb_define_method(rb_cIO, "cursor_left", console_cursor_left, 1);
@@ -1996,27 +2545,64 @@ InitVM_console(void)
1996
2545
  rb_define_method(rb_cIO, "scroll_backward", console_scroll_backward, 1);
1997
2546
  rb_define_method(rb_cIO, "clear_screen", console_clear_screen, 0);
1998
2547
  rb_define_method(rb_cIO, "pressed?", console_key_pressed_p, 1);
2548
+ rb_define_method(rb_cIO, "console_input_events", console_input_events, -1);
1999
2549
  rb_define_method(rb_cIO, "check_winsize_changed", console_check_winsize_changed, 0);
2000
2550
  rb_define_method(rb_cIO, "getpass", console_getpass, -1);
2001
2551
  rb_define_method(rb_cIO, "ttyname", console_ttyname, 0);
2552
+ {
2553
+ /* :nodoc: */
2554
+ VALUE platform = rb_define_module_under(rb_cIO, "platform_tty");
2555
+ {
2556
+ VALUE rb_cIO = platform;
2557
+ rb_define_method(rb_cIO, "tty?", console_platform_tty_p, -1);
2558
+ rb_define_method(rb_cIO, "isatty", console_platform_tty_p, -1);
2559
+ }
2560
+ #ifdef HAVE_RB_PREPEND_MODULE
2561
+ rb_prepend_module(rb_cIO, platform);
2562
+ #else
2563
+ rb_funcall(rb_cIO, rb_intern_const("prepend"), 1, platform);
2564
+ #endif
2565
+ }
2002
2566
  rb_define_singleton_method(rb_cIO, "console", console_dev, -1);
2003
2567
  {
2004
- /* :stopdoc: */
2568
+ /* :nodoc: */
2005
2569
  VALUE mReadable = rb_define_module_under(rb_cIO, "generic_readable");
2006
- /* :startdoc: */
2007
2570
  rb_define_method(mReadable, "getch", io_getch, -1);
2008
2571
  rb_define_method(mReadable, "getpass", io_getpass, -1);
2009
2572
  }
2010
2573
  {
2011
- /* :stopdoc: */
2012
- cConmode = rb_define_class_under(rb_cIO, "ConsoleMode", rb_cObject);
2013
- rb_define_const(cConmode, "VERSION", rb_obj_freeze(rb_str_new_cstr(IO_CONSOLE_VERSION)));
2574
+ /* :nodoc: */
2575
+ VALUE version = rb_obj_freeze(rb_str_new_cstr(IO_CONSOLE_VERSION));
2576
+ ID cid, deprecate_constant = rb_intern_const("deprecate_constant");
2577
+ rb_define_const(mConsole, "VERSION", version);
2578
+ /* :nodoc: */
2579
+ cConmode = rb_define_class_under(mConsole, "Mode", rb_cObject);
2580
+
2581
+ /* old internal names; do not use */
2582
+ cid = rb_intern_const("ConsoleMode");
2583
+ rb_const_set(rb_cIO, cid, cConmode);
2584
+ rb_funcall(rb_cIO, deprecate_constant, 1, ID2SYM(cid));
2585
+ cid = rb_intern_const("VERSION");
2586
+ rb_const_set(cConmode, cid, version);
2587
+ rb_funcall(cConmode, deprecate_constant, 1, ID2SYM(cid));
2588
+
2014
2589
  rb_define_alloc_func(cConmode, conmode_alloc);
2015
2590
  rb_undef_method(cConmode, "initialize");
2016
2591
  rb_define_method(cConmode, "initialize_copy", conmode_init_copy, 1);
2592
+ rb_define_method(cConmode, "echo?", conmode_get_echo, 0);
2593
+ rb_define_method(cConmode, "echo", conmode_get_echo, 0);
2017
2594
  rb_define_method(cConmode, "echo=", conmode_set_echo, 1);
2018
2595
  rb_define_method(cConmode, "raw!", conmode_set_raw, -1);
2019
2596
  rb_define_method(cConmode, "raw", conmode_raw_new, -1);
2020
- /* :startdoc: */
2597
+ rb_define_method(cConmode, "min", conmode_get_min, 0);
2598
+ rb_define_method(cConmode, "min=", conmode_set_min, 1);
2599
+ rb_define_method(cConmode, "time", conmode_get_time, 0);
2600
+ rb_define_method(cConmode, "time=", conmode_set_time, 1);
2601
+ #ifdef _WIN32
2602
+ rb_define_method(cConmode, "virtual_terminal_processing?", conmode_virtual_terminal_processing_p, 0);
2603
+ rb_define_method(cConmode, "virtual_terminal_processing=", conmode_set_virtual_terminal_processing, 1);
2604
+ rb_define_method(cConmode, "wrap_at_eol_output?", conmode_wrap_at_eol_output_p, 0);
2605
+ rb_define_method(cConmode, "wrap_at_eol_output=", conmode_set_wrap_at_eol_output, 1);
2606
+ #endif
2021
2607
  }
2022
2608
  }