Skip to content

Commit e712668

Browse files
committed
Add Windows console constants
Expose Windows input constants for console event consumers.
1 parent 7801e85 commit e712668

9 files changed

Lines changed: 127 additions & 4 deletions

File tree

ext/io/console/console.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2406,6 +2406,25 @@ Init_console(void)
24062406
void
24072407
InitVM_console(void)
24082408
{
2409+
/* :nodoc: */
2410+
VALUE mConsole = rb_define_module_under(rb_cIO, "Console");
2411+
#ifdef _WIN32
2412+
/* :nodoc: */
2413+
VALUE mWindows = rb_define_module_under(mConsole, "Windows");
2414+
#define define_win32_const(name) rb_define_const(mWindows, #name, UINT2NUM(name))
2415+
EACH_VK(define_win32_const,;);
2416+
define_win32_const(RIGHT_ALT_PRESSED);
2417+
define_win32_const(LEFT_ALT_PRESSED);
2418+
define_win32_const(RIGHT_CTRL_PRESSED);
2419+
define_win32_const(LEFT_CTRL_PRESSED);
2420+
define_win32_const(SHIFT_PRESSED);
2421+
define_win32_const(NUMLOCK_ON);
2422+
define_win32_const(SCROLLLOCK_ON);
2423+
define_win32_const(CAPSLOCK_ON);
2424+
define_win32_const(ENHANCED_KEY);
2425+
#undef define_win32_const
2426+
#endif
2427+
24092428
rb_define_method(rb_cIO, "raw", console_raw, -1);
24102429
rb_define_method(rb_cIO, "raw!", console_set_raw, -1);
24112430
rb_define_method(rb_cIO, "cooked", console_cooked, 0);
@@ -2466,7 +2485,6 @@ InitVM_console(void)
24662485
}
24672486
{
24682487
/* :nodoc: */
2469-
VALUE mConsole = rb_define_module_under(rb_cIO, "Console");
24702488
VALUE version = rb_obj_freeze(rb_str_new_cstr(IO_CONSOLE_VERSION));
24712489
ID cid, deprecate_constant = rb_intern_const("deprecate_constant");
24722490
rb_define_const(mConsole, "VERSION", version);

ext/io/console/depend

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
win32_vk.inc: win32_vk.list
1+
win32_vk.inc: win32_vk.list $(srcdir)/extract-vk.rb
22

33
.list.inc:
44
( \
5-
$(RUBY) -e "ARGF.read.scan(/^\w+,\s*\KVK_\w+/){|n|puts(%Q[#ifndef #{n}\n# define #{n} UNDEFINED_VK\n#endif])}" \
6-
$< && \
5+
$(RUBY) $(srcdir)/extract-vk.rb $< && \
76
gperf --ignore-case -L ANSI-C -E -C -P -p -j1 -i 1 -g -o -t -K ofs -N console_win32_vk -k* $< \
87
| sed -f $(top_srcdir)/tool/gperf.sed \
98
) > $(@F)

ext/io/console/extract-vk.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
code = [+""]
2+
ARGF.read.scan(/^\w+,\s*\KVK_\w+/) do |n|
3+
puts("#ifndef #{n}\n# define #{n} UNDEFINED_VK\n#endif")
4+
code << +"" if n.size + code.last.size > 60
5+
code.last << " x(#{n})z"
6+
end
7+
puts ["#define EACH_VK(x,z)", code].join(" \\\n "), ""

ext/io/console/win32_vk.inc

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

io-console.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Gem::Specification.new do |s|
4545
lib/ffi/io/console/native_console.rb
4646
lib/ffi/io/console/stty_console.rb
4747
lib/ffi/io/console/stub_console.rb
48+
lib/ffi/io/console/windows_constants.rb
4849
lib/ffi/io/console/version.rb
4950
])
5051
end

lib/ffi/io/console.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Mode
4444
when /linux/
4545
libs << 'linux' << 'stty'
4646
when /mswin|win32|ming/i
47+
require_relative 'console/windows_constants'
4748
# If Windows, stty is not possible, always use the stub version
4849
else
4950
libs << 'stty'

lib/ffi/io/console/common.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Methods common to all backend impls
2+
module IO::Console
3+
end
4+
25
class IO
36
# TODO: Windows version uses "conin$" and "conout$" instead of /dev/tty
47
def self.console(sym = nil, *args)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module IO::Console::Windows
2+
VK_TAB = 0x09
3+
VK_RETURN = 0x0d
4+
VK_SHIFT = 0x10
5+
VK_CONTROL = 0x11
6+
VK_MENU = 0x12
7+
VK_END = 0x23
8+
VK_HOME = 0x24
9+
VK_LEFT = 0x25
10+
VK_UP = 0x26
11+
VK_RIGHT = 0x27
12+
VK_DOWN = 0x28
13+
VK_DELETE = 0x2e
14+
VK_DIVIDE = 0x6f
15+
VK_LMENU = 0xa4
16+
17+
RIGHT_ALT_PRESSED = 0x0001
18+
LEFT_ALT_PRESSED = 0x0002
19+
RIGHT_CTRL_PRESSED = 0x0004
20+
LEFT_CTRL_PRESSED = 0x0008
21+
SHIFT_PRESSED = 0x0010
22+
NUMLOCK_ON = 0x0020
23+
SCROLLLOCK_ON = 0x0040
24+
CAPSLOCK_ON = 0x0080
25+
ENHANCED_KEY = 0x0100
26+
end

test/io/console/test_io_console.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
end
88

99
class TestIO_Console < Test::Unit::TestCase
10+
def test_console_namespace
11+
assert_kind_of(Module, IO::Console)
12+
end unless RUBY_ENGINE == "jruby" && RbConfig::CONFIG["host_os"] !~ /mswin|mingw/
13+
1014
HOST_OS = RbConfig::CONFIG['host_os']
1115

1216
def test_version
@@ -720,6 +724,26 @@ def test_pressed_invalid
720724
end
721725
end
722726

727+
RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ and TestIO_Console.class_eval do
728+
def test_virtual_key_constants
729+
{
730+
VK_TAB: 0x09, VK_RETURN: 0x0d, VK_SHIFT: 0x10,
731+
VK_CONTROL: 0x11, VK_MENU: 0x12, VK_END: 0x23,
732+
VK_HOME: 0x24, VK_LEFT: 0x25, VK_UP: 0x26,
733+
VK_RIGHT: 0x27, VK_DOWN: 0x28, VK_DELETE: 0x2e,
734+
VK_DIVIDE: 0x6f, VK_LMENU: 0xa4,
735+
RIGHT_ALT_PRESSED: 0x0001, LEFT_ALT_PRESSED: 0x0002,
736+
RIGHT_CTRL_PRESSED: 0x0004, LEFT_CTRL_PRESSED: 0x0008,
737+
SHIFT_PRESSED: 0x0010, NUMLOCK_ON: 0x0020,
738+
SCROLLLOCK_ON: 0x0040, CAPSLOCK_ON: 0x0080,
739+
ENHANCED_KEY: 0x0100,
740+
}.each do |name, value|
741+
assert_equal(value, IO::Console::Windows.const_get(name, false))
742+
assert_false(IO::Console.const_defined?(name, false))
743+
end
744+
end
745+
end
746+
723747
RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ and defined?(IO.console) and IO.console and \
724748
TestIO_Console.class_eval do
725749
def test_output_console_mode

0 commit comments

Comments
 (0)