initial import
This commit is contained in:
commit
4a4769822e
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
*.gem
|
||||
.bundle
|
||||
Gemfile.lock
|
||||
pkg/*
|
4
Gemfile
Normal file
4
Gemfile
Normal file
|
@ -0,0 +1,4 @@
|
|||
source "http://rubygems.org"
|
||||
|
||||
# Specify your gem's dependencies in scanner.gemspec
|
||||
gemspec
|
39
lib/sane.rb
Normal file
39
lib/sane.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
module Sane
|
||||
class << self
|
||||
def init
|
||||
version_code = FFI::MemoryPointer.new(:int)
|
||||
check_status!(API.sane_init(version_code, FFI::Pointer::NULL))
|
||||
version_code.read_int
|
||||
end
|
||||
|
||||
def exit
|
||||
API.sane_exit
|
||||
end
|
||||
|
||||
def get_devices
|
||||
devices_pointer = FFI::MemoryPointer.new(:pointer)
|
||||
check_status!(API.sane_get_devices(devices_pointer, 0))
|
||||
devices = devices_pointer.read_pointer
|
||||
[].tap do |result|
|
||||
until devices.read_pointer.null?
|
||||
result << API::Device.new(devices.read_pointer)
|
||||
devices += FFI.type_size(:pointer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def open(device_name)
|
||||
device_handle_pointer = FFI::MemoryPointer.new(:pointer)
|
||||
check_status!(API.sane_open(device_name.to_s, device_handle_pointer))
|
||||
device_handle_pointer.read_pointer
|
||||
end
|
||||
|
||||
def close(device_handle)
|
||||
API.sane_close(device_handle)
|
||||
end
|
||||
|
||||
def check_status!(status)
|
||||
raise Error.new(API.sane_strstatus(status), status) if status != :good
|
||||
end
|
||||
end
|
||||
end
|
60
lib/sane/api.rb
Normal file
60
lib/sane/api.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
module Sane
|
||||
module API
|
||||
extend FFI::Library
|
||||
|
||||
ffi_lib "sane"
|
||||
|
||||
enum :status, [:good, 0, :unsupported, :cancelled, :device_busy, :inval, :eof, :jammed, :no_docs, :cover_open, :io_error, :no_mem, :access_denied]
|
||||
enum :value_type, [:bool, 0, :int, :fixed, :string, :button, :group]
|
||||
enum :unit, [:none, 0, :pixel, :bit, :mm, :dpi, :percent, :microsecond]
|
||||
enum :action, [:get_value, 0, :set_value, :set_auto]
|
||||
enum :frame, [:gray, :rgb, :red, :green, :blue]
|
||||
|
||||
class Device < FFI::Struct
|
||||
layout :name, :string, :vendor, :string, :model, :string, :type, :string
|
||||
|
||||
def to_s; self[:name] end
|
||||
def inspect; "#<#{self.class.name}:'#{to_s}'>" end
|
||||
end
|
||||
|
||||
class OptionDescriptor < FFI::Struct
|
||||
class ConstraintType < FFI::Union
|
||||
layout :string_list, :pointer, :word_list, :pointer, :range, :pointer
|
||||
end
|
||||
layout :name, :string, :title, :string, :desc, :string, :type, :value_type, :unit, :unit, :size, :int, :cap, :int, :constraint_type, ConstraintType
|
||||
end
|
||||
|
||||
class Parameters < FFI::Struct
|
||||
layout :format, :frame, :last_frame, :int, :bytes_per_line, :int, :pixels_per_line, :int, :lines, :int, :depth, :int
|
||||
end
|
||||
|
||||
# extern SANE_Status sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize);
|
||||
attach_function :sane_init, [:pointer, :pointer], :status
|
||||
# extern void sane_exit (void);
|
||||
attach_function :sane_exit, [], :void
|
||||
# extern SANE_Status sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only);
|
||||
attach_function :sane_get_devices, [:pointer, :int], :status
|
||||
# extern SANE_Status sane_open (SANE_String_Const devicename, SANE_Handle * handle);
|
||||
attach_function :sane_open, [:string, :pointer], :status
|
||||
# extern void sane_close (SANE_Handle handle);
|
||||
attach_function :sane_close, [:pointer], :void
|
||||
# extern const SANE_Option_Descriptor * sane_get_option_descriptor (SANE_Handle handle, SANE_Int option);
|
||||
attach_function :sane_get_option_descriptor, [:pointer, :int], :pointer
|
||||
# extern SANE_Status sane_control_option (SANE_Handle handle, SANE_Int option, SANE_Action action, void *value, SANE_Int * info);
|
||||
attach_function :sane_control_option, [:pointer, :int, :action, :pointer, :pointer], :status
|
||||
# extern SANE_Status sane_get_parameters (SANE_Handle handle, SANE_Parameters * params);
|
||||
attach_function :sane_get_parameters, [:pointer, :pointer], :status
|
||||
# extern SANE_Status sane_start (SANE_Handle handle);
|
||||
attach_function :sane_start, [:pointer], :status
|
||||
# extern SANE_Status sane_read (SANE_Handle handle, SANE_Byte * data, SANE_Int max_length, SANE_Int * length);
|
||||
attach_function :sane_read, [:pointer, :pointer, :int, :pointer], :status
|
||||
# extern void sane_cancel (SANE_Handle handle);
|
||||
attach_function :sane_cancel, [:pointer], :void
|
||||
# extern SANE_Status sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking);
|
||||
attach_function :sane_set_io_mode, [:pointer, :int], :status
|
||||
# extern SANE_Status sane_get_select_fd (SANE_Handle handle, SANE_Int * fd);
|
||||
attach_function :sane_get_select_fd, [:pointer, :pointer], :status
|
||||
# extern SANE_String_Const sane_strstatus (SANE_Status status);
|
||||
attach_function :sane_strstatus, [:status], :string
|
||||
end
|
||||
end
|
11
lib/sane/device.rb
Normal file
11
lib/sane/device.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
module Sane
|
||||
class Device
|
||||
def initialize(handle)
|
||||
@handle = handle
|
||||
end
|
||||
|
||||
def close
|
||||
Sane.close(@handle)
|
||||
end
|
||||
end
|
||||
end
|
10
lib/sane/error.rb
Normal file
10
lib/sane/error.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
module Sane
|
||||
class Error < StandardError
|
||||
attr_reader :status
|
||||
|
||||
def initialize(message, status)
|
||||
super(message)
|
||||
@status = status
|
||||
end
|
||||
end
|
||||
end
|
9
lib/scanner.rb
Normal file
9
lib/scanner.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require "ffi"
|
||||
|
||||
require "sane"
|
||||
require "sane/api"
|
||||
require "sane/error"
|
||||
|
||||
module Scanner
|
||||
|
||||
end
|
5
lib/scanner/sane.rb
Normal file
5
lib/scanner/sane.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
module Scanner
|
||||
module Sane
|
||||
|
||||
end
|
||||
end
|
3
lib/scanner/version.rb
Normal file
3
lib/scanner/version.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
module Scanner
|
||||
VERSION = "0.0.1"
|
||||
end
|
23
scanner.gemspec
Normal file
23
scanner.gemspec
Normal file
|
@ -0,0 +1,23 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
$:.push File.expand_path("../lib", __FILE__)
|
||||
require "scanner/version"
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = "scanner"
|
||||
s.version = Scanner::VERSION
|
||||
s.platform = Gem::Platform::RUBY
|
||||
s.authors = ["Jakub Kuźma"]
|
||||
s.email = ["qoobaa@gmail.com"]
|
||||
s.homepage = ""
|
||||
s.summary = %q{SANE bindings}
|
||||
s.description = %q{Scanner Access now Easier}
|
||||
|
||||
# s.rubyforge_project = "scanner"
|
||||
|
||||
s.add_dependency "ffi"
|
||||
|
||||
s.files = `git ls-files`.split("\n")
|
||||
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
||||
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
||||
s.require_paths = ["lib"]
|
||||
end
|
Loading…
Reference in a new issue