require 'fox'

module Fox
  class FXApp

    # Accepted forms are:
    #   addInput(fd, mode, target, selector)
    #   addInput(fd, mode, method(:onTimeout))
    #   addInput(fd, mode) { |sender, sel, ptr| block }

    alias addInputOrig addInput

    def addInput(fd, mode, *args, &block)
      tgt, sel = nil, 0
      if args.length > 0
        if args[0].respond_to? :call
          tgt = FXPseudoTarget.new
	  tgt.pconnect(SEL_IO_READ, args[0], block)
	  tgt.pconnect(SEL_IO_WRITE, args[0], block)
	  tgt.pconnect(SEL_IO_EXCEPT, args[0], block)
        else # it's some other kind of object
          tgt = args[0]
          sel = args[1]
        end
      else
        tgt = FXPseudoTarget.new
	tgt.pconnect(SEL_IO_READ, nil, block)
	tgt.pconnect(SEL_IO_WRITE, nil, block)
	tgt.pconnect(SEL_IO_EXCEPT, nil, block)
      end
      addInputOrig(fd, mode, tgt, sel)
    end
  end
end
