[sane-devel] sane_init(), sane_get_devices() and SANE_Authorization_Callback()

Kåre Särs kare.sars at iki.fi
Wed May 26 19:08:27 UTC 2010

On Wednesday 26 May 2010 15:48:08 m. allan noah wrote:
> 2010/5/26 Kåre Särs <kare.sars at iki.fi>:
> > Hi,
> > 
> > I have a couple of somewhat related problems regarding
> >  sane_get_devices(), sane_init() and SANE_Authorization_Callback().
> > 
> > 1) sane_get_devices() does not update the available devices list without
> > calling sane_init(). I asked about this some time ago and got the answer
> > that it was a bug. With the version in (K)Ubuntu Lucid and the
> > pashazz-sane PPA, I still have the problem that the scanner is not found
> > if turned on after sane_init(). I use the epson2 and v4l backends. With
> > epson2 I get a crash if the scanner is on the first time but is turned
> > off before the second sane_get_devices().
> > 
> > Do you have any hints on where to start digging?
> Many backends do not properly re-scan for devices. Those backends
> should be fixed. Which version of sane-backends is this?
This was sane-backends 1.0.21 from the ubunntu ppa mentioned in a couple of 
mails back. The backends I use are epson2 and v4l

> > 2) With  sane_get_devices() I can get the vendor, model, type and backend
> > names for the available scanners. Is there a way to get the same
> > information about the currently opened device? I do not find an API for
> > that.  Have I missed something? The problem is that I can specify a
> > backend like "test" and sane_open() will open either "test:0" or
> > "test:1", but I do not know which one and I do not get the vendor, model
> > and type strings.
> Correct. This is a deficiency in the sane API. You have to keep the
> device list, and open a device by the given name. If you ask a backend
> for the NULL device, it is impossible to know which one you will get.
> > 3) libksane can be used in multi threaded applications and at the moment
> > I'm making sure that sane_exit() is only called when the last instance
> > exits, as sane_exit() will invalidate the other running instances.
> > Should I do the same for sane_init()?. So far it has not resulted in
> > problems, but when I tried a workaround for the  sane_get_devices()
> > problem I noticed that I got unwanted behavior.
> sane-backends is not designed to be thread safe, as only one function
> (sane_cancel) is explicitly required to be reentrant. So, arrange your
> app to only call sane from one thread. But, more to your question- you
> should only call sane_init once. If you call it again (particularly
> without calling sane_exit first), I would not be surprised to hear of
> bad things happening.

OK, I'll ensure that sane_init is called only once. Does this also mean that 
the sane callbacks that take a sane handle can have local variables that would 

Since libksane provides a "widget" it always runs in the main event loop and 
will not reenter the same function twice. Only sane_start and sane_read are 
called from real threads.

At the moment I have sane_get_devices() in a singleton with separate thread to 
enable updating the GUI. Should I here also ensure that no other sane 
functions are called at the same time?

One workaround I can think of would be to start a separate process that would 
read the available devices...

> > 4) Again in a multi threaded application there might be two scanners that
> > require authentication. The resource string that is provided to
> > SANE_Authorization_Callback() contains the backend name and a md5 hash,
> > but it does not contain the same ":libusb:001:004" extra info type of
> > sting that sane_get_devices() returns for the backend.
> Which backend?
At least "epson2" and "test" through the net backend.

> > How should I identify the different scanners with the same backend? Or is
> > the authorization restricted to the backend with the same authentication
> > for all the scanners with the same backend?
> Personally, I'd probably skip the auth callback...
It is there already, but it only works if invoked from sane_open :)

> allan

Thanks for the answers,

More information about the sane-devel mailing list