From: Kouhei S. <nu...@co...> - 2017-10-26 15:35:54
|
Kouhei Sutou 2017-10-27 00:34:43 +0900 (Fri, 27 Oct 2017) New Revision: 4a32a9eb412fb867014afa38f152a2597a00cdfe https://github.com/ruby-gnome2/ruby-gnome2/commit/4a32a9eb412fb867014afa38f152a2597a00cdfe Message: glib2: support GLib::IOChanne#create_watch GitHub: #1106 Reported by kspt-johs. Thanks!!! Modified files: glib2/ext/glib2/rbglib_iochannel.c glib2/test/test_iochannel.rb Modified: glib2/ext/glib2/rbglib_iochannel.c (+34 -3) =================================================================== --- glib2/ext/glib2/rbglib_iochannel.c 2017-10-17 00:03:12 +0900 (bf2dbd536) +++ glib2/ext/glib2/rbglib_iochannel.c 2017-10-27 00:34:43 +0900 (8d0618732) @@ -1,6 +1,6 @@ /* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */ /* - * Copyright (C) 2011 Ruby-GNOME2 Project Team + * Copyright (C) 2011-2017 Ruby-GNOME2 Project Team * Copyright (C) 2005 Masao Mutoh * * This library is free software; you can redistribute it and/or @@ -27,6 +27,8 @@ static ID id_unpack; static VALUE default_rs; +static VALUE rb_mIOChannelSource; + #define RG_TARGET_NAMESPACE cIOChannel #define _SELF(s) ((GIOChannel*)RVAL2BOXED(s, G_TYPE_IO_CHANNEL)) @@ -499,8 +501,17 @@ rg_close(gint argc, VALUE *argv, VALUE self) static VALUE rg_create_watch(VALUE self, VALUE condition) { - return BOXED2RVAL(g_io_create_watch(_SELF(self), NUM2INT(condition)), - G_TYPE_SOURCE); + VALUE rb_source; + + rb_source = BOXED2RVAL(g_io_create_watch(_SELF(self), NUM2INT(condition)), + G_TYPE_SOURCE); + rb_extend_object(rb_source, rb_mIOChannelSource); + if (rb_block_given_p()) { + ID id_set_callback; + CONST_ID(id_set_callback, "set_callback"); + rb_funcall(rb_source, id_set_callback, 0); + } + return rb_source; } static gboolean @@ -530,6 +541,20 @@ guint g_io_add_watch_full (GIOChannel *channel, */ static VALUE +rg_io_channel_source_set_callback(VALUE self) +{ + VALUE callback; + + callback = rb_block_proc(); + G_RELATIVE(self, callback); + g_source_set_callback(RVAL2BOXED(self, G_TYPE_SOURCE), + (GSourceFunc)io_func, + (gpointer)callback, + (GDestroyNotify)NULL); + return self; +} + +static VALUE rg_buffer_size(VALUE self) { return UINT2NUM(g_io_channel_get_buffer_size(_SELF(self))); @@ -818,4 +843,10 @@ Init_glib_io_channel(void) rb_define_const(RG_TARGET_NAMESPACE, "FLAG_MASK", INT2NUM(G_IO_FLAG_MASK)); rb_define_const(RG_TARGET_NAMESPACE, "FLAG_GET_MASK", INT2NUM(G_IO_FLAG_GET_MASK)); rb_define_const(RG_TARGET_NAMESPACE, "FLAG_SET_MASK", INT2NUM(G_IO_FLAG_SET_MASK)); + + rb_mIOChannelSource = rb_define_module_under(mGLib, "IOChannelSource"); + rb_define_method(rb_mIOChannelSource, + "set_callback", + rg_io_channel_source_set_callback, + 0); } Modified: glib2/test/test_iochannel.rb (+38 -2) =================================================================== --- glib2/test/test_iochannel.rb 2017-10-17 00:03:12 +0900 (d823d03a8) +++ glib2/test/test_iochannel.rb 2017-10-27 00:34:43 +0900 (1fe97a9f3) @@ -1,4 +1,4 @@ -# Copyright (C) 2015 Ruby-GNOME2 Project Team +# Copyright (C) 2015-2017 Ruby-GNOME2 Project Team # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -13,7 +13,7 @@ # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - + require 'test/unit' require 'glib2' @@ -286,4 +286,40 @@ class TestGIOChannel < Test::Unit::TestCase GLib::IOChannel.new("foo") } end + + sub_test_case("#create_watch") do + def setup + super + @context = GLib::MainContext.new + end + + def test_with_block + GLib::IOChannel.open(@file.path) do |channel| + received_condition = nil + source = channel.create_watch(GLib::IOChannel::IN) do |_, condition| + received_condition = condition + end + source.attach(@context) + 10.times do + @context.iteration(false) + end + assert_equal(GLib::IOCondition::IN, received_condition) + end + end + + def test_set_callback + GLib::IOChannel.open(@file.path) do |channel| + received_condition = nil + source = channel.create_watch(GLib::IOChannel::IN) + source.set_callback do |_, condition| + received_condition = condition + end + source.attach(@context) + 10.times do + @context.iteration(false) + end + assert_equal(GLib::IOCondition::IN, received_condition) + end + end + end end |