From 25ade76617c2145da9f0972704be2184343e77d7 Mon Sep 17 00:00:00 2001 From: Gaute Hope Date: Wed, 20 Nov 2013 00:01:06 +0100 Subject: [PATCH] return array with return type and ruby string --- examples/form_get_wch.rb | 10 ++++++++-- ncurses_wrap.c | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/examples/form_get_wch.rb b/examples/form_get_wch.rb index 7eb4690..f75362a 100644 --- a/examples/form_get_wch.rb +++ b/examples/form_get_wch.rb @@ -41,7 +41,13 @@ scr.refresh() # Loop through to get user requests - while((ch = Ncurses.get_wch()) != Ncurses::KEY_F1) do + while(true) do + ret = Ncurses.get_wch() + + #ret[1].force_encoding('utf-8') + #puts "got: #{ret.inspect}" + + ch = ret[1] case(ch) when Ncurses::KEY_DOWN # Go to next field @@ -56,7 +62,7 @@ Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE); else # If this is a normal character, it gets Printed - Ncurses::Form.form_driver(form, ch) + Ncurses::Form.form_driver(form, ch.ord) end end diff --git a/ncurses_wrap.c b/ncurses_wrap.c index 2ddfb2f..0d107a7 100755 --- a/ncurses_wrap.c +++ b/ncurses_wrap.c @@ -913,15 +913,21 @@ static int rbncurshelper_nonblocking_wgetch(WINDOW *c_win) { return rbncurshelper_do_wgetch_functor (c_win, &wgetch); } -/* wide char getch */ +/* not thread safe: wide char getch */ static wint_t wget_wch_back; static int my_wget_wch (WINDOW *c_win) { return wget_wch (c_win, &wget_wch_back); } -static int rbncurshelper_nonblocking_wget_wch(WINDOW *c_win) { - rbncurshelper_do_wgetch_functor (c_win, &my_wget_wch); - return wget_wch_back; +/* return array with first element being return key code status, + * and second element the key code */ +static VALUE rbncurshelper_nonblocking_wget_wch(WINDOW *c_win) { + int retcode = rbncurshelper_do_wgetch_functor (c_win, &my_wget_wch); + //char c_str[2]; + //c_str[0] = wget_wch_back; + //c_str[1] = 0; + VALUE r = rb_assoc_new (INT2NUM(retcode), rb_str_new((const char*)&wget_wch_back, 1)); + return r; } static VALUE rbncurs_getch(VALUE dummy) { @@ -929,7 +935,7 @@ static VALUE rbncurs_getch(VALUE dummy) { } static VALUE rbncurs_get_wch(VALUE dummy) { - return LONG2NUM(rbncurshelper_nonblocking_wget_wch(stdscr)); + return rbncurshelper_nonblocking_wget_wch(stdscr); } static VALUE rbncurs_halfdelay(VALUE dummy, VALUE arg1) { @@ -1572,9 +1578,11 @@ static VALUE rbncurs_werase(VALUE dummy, VALUE arg1) { static VALUE rbncurs_wgetch(VALUE dummy, VALUE arg1) { return INT2NUM(rbncurshelper_nonblocking_wgetch(get_window(arg1))); } + static VALUE rbncurs_wget_wch(VALUE dummy, VALUE arg1) { - return INT2NUM(rbncurshelper_nonblocking_wget_wch(get_window(arg1))); + return rbncurshelper_nonblocking_wget_wch(get_window(arg1)); } + static VALUE rbncurs_whline(VALUE dummy, VALUE arg1, VALUE arg2, VALUE arg3) { return INT2NUM(whline(get_window(arg1), (int) NUM2ULONG(arg2), NUM2INT(arg3))); }