xref: /aoo41x/main/extensions/source/scanner/sane.hxx (revision 46dbacee)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _SANE_HXX
24 #define _SANE_HXX
25 
26 #include <osl/thread.h>
27 #include <osl/module.h>
28 #include <tools/string.hxx>
29 #include <vcl/bitmap.hxx>
30 #include <sane/sane.h>
31 #include <scanner.hxx>
32 
33 // ---------------------
34 // - BitmapTransporter -
35 // ---------------------
36 
37 class BitmapTransporter : public OWeakObject, AWT::XBitmap
38 {
39 	SvMemoryStream						m_aStream;
40 	vos::OMutex							m_aProtector;
41 
42 public:
43 
44 										BitmapTransporter();
45 	virtual								~BitmapTransporter();
46 
47 
48 	// XInterface
49 	virtual ANY SAL_CALL				queryInterface( const Type & rType ) throw( RuntimeException );
acquire()50     virtual void SAL_CALL				acquire() throw() { OWeakObject::acquire(); }
release()51     virtual void SAL_CALL				release() throw() { OWeakObject::release(); }
52 
53 	virtual AWT::Size SAL_CALL			getSize() throw();
54 	virtual SEQ( sal_Int8 ) SAL_CALL	getDIB() throw();
SEQ(sal_Int8)55 	virtual SEQ( sal_Int8 ) SAL_CALL	getMaskDIB() throw() { return SEQ( sal_Int8 )(); }
56 
57 	// Misc
lock()58 	void								lock() { m_aProtector.acquire(); }
unlock()59 	void								unlock() { m_aProtector.release(); }
getStream()60 	SvMemoryStream&						getStream() { return m_aStream; }
61 };
62 
63 // --------
64 // - Sane -
65 // --------
66 
67 class Sane
68 {
69 private:
70 	static int				nRefCount;
71 	static oslModule        pSaneLib;
72 
73 	static SANE_Status		(*p_init)( SANE_Int*,
74 									   SANE_Auth_Callback );
75 	static void				(*p_exit)();
76 	static SANE_Status		(*p_get_devices)( const SANE_Device***,
77 											  SANE_Bool );
78 	static SANE_Status		(*p_open)( SANE_String_Const, SANE_Handle );
79 	static void				(*p_close)( SANE_Handle );
80 	static const SANE_Option_Descriptor* (*p_get_option_descriptor)(
81 		SANE_Handle, SANE_Int );
82 	static SANE_Status		(*p_control_option)( SANE_Handle, SANE_Int,
83 												 SANE_Action, void*,
84 												 SANE_Int* );
85 	static SANE_Status		(*p_get_parameters)( SANE_Handle,
86 												 SANE_Parameters* );
87 	static SANE_Status		(*p_start)( SANE_Handle );
88 	static SANE_Status		(*p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
89 									   SANE_Int* );
90 	static void				(*p_cancel)( SANE_Handle );
91 	static SANE_Status		(*p_set_io_mode)( SANE_Handle, SANE_Bool );
92 	static SANE_Status		(*p_get_select_fd)( SANE_Handle, SANE_Int* );
93 	static SANE_String_Const (*p_strstatus)( SANE_Status );
94 
95 	static SANE_Int				nVersion;
96 	static SANE_Device**		ppDevices;
97 	static int					nDevices;
98 
99 	const SANE_Option_Descriptor**	mppOptions;
100 	int								mnOptions;
101 	int								mnDevice;
102 	SANE_Handle						maHandle;
103 
104 	Link						maReloadOptionsLink;
105 
106 	inline oslGenericFunction
107                     LoadSymbol( const char* );
108 	void			Init();
109 	void			DeInit();
110 
111 	SANE_Status ControlOption( int, SANE_Action, void* );
112 
113 	sal_Bool CheckConsistency( const char*, sal_Bool bInit = sal_False );
114 
115 public:
116 	Sane();
117 	~Sane();
118 
IsSane()119 	static sal_Bool			IsSane()
120 		{ return pSaneLib ? sal_True : sal_False; }
IsOpen()121 	sal_Bool			IsOpen()
122 		{ return maHandle ? sal_True : sal_False; }
CountDevices()123 	static int				CountDevices()
124 		{ return nDevices; }
GetName(int n)125 	static String			GetName( int n )
126 		{ return String( ppDevices[n]->name ? ppDevices[n]->name : "", osl_getThreadTextEncoding() ); }
GetVendor(int n)127 	static String			GetVendor( int n )
128 		{ return String( ppDevices[n]->vendor ? ppDevices[n]->vendor : "", osl_getThreadTextEncoding() ); }
GetModel(int n)129 	static String			GetModel( int n )
130 		{ return String( ppDevices[n]->model ? ppDevices[n]->model : "", osl_getThreadTextEncoding() ); }
GetType(int n)131 	static String			GetType( int n )
132 		{ return String( ppDevices[n]->type ? ppDevices[n]->type : "", osl_getThreadTextEncoding() ); }
133 
GetOptionName(int n)134 	String			GetOptionName( int n )
135 		{ return String( mppOptions[n]->name ? (char*)mppOptions[n]->name : "", osl_getThreadTextEncoding() ); }
GetOptionTitle(int n)136 	String			GetOptionTitle( int n )
137 		{ return String( mppOptions[n]->title ? (char*)mppOptions[n]->title : "", osl_getThreadTextEncoding() ); }
GetOptionType(int n)138 	SANE_Value_Type	GetOptionType( int n )
139 		{ return mppOptions[n]->type; }
GetOptionUnit(int n)140 	SANE_Unit		GetOptionUnit( int n )
141 		{ return mppOptions[n]->unit; }
142 	String			GetOptionUnitName( int n );
GetOptionCap(int n)143 	SANE_Int		GetOptionCap( int n )
144 		{ return mppOptions[n]->cap; }
GetOptionConstraintType(int n)145 	SANE_Constraint_Type GetOptionConstraintType( int n )
146 		{ return mppOptions[n]->constraint_type; }
GetStringConstraint(int n)147 	const char**	GetStringConstraint( int n )
148 		{ return (const char**)mppOptions[n]->constraint.string_list; }
149 	int				GetRange( int, double*& );
150 
151 	inline int		GetOptionElements( int n );
152 	int				GetOptionByName( const char* );
153 	sal_Bool			GetOptionValue( int, sal_Bool& );
154 	sal_Bool			GetOptionValue( int, ByteString& );
155 	sal_Bool			GetOptionValue( int, double&, int nElement = 0 );
156 	sal_Bool			GetOptionValue( int, double* );
157 
158 	sal_Bool			SetOptionValue( int, sal_Bool );
159 	sal_Bool			SetOptionValue( int, const String& );
160 	sal_Bool			SetOptionValue( int, double, int nElement = 0 );
161 	sal_Bool			SetOptionValue( int, double* );
162 
163 	sal_Bool			ActivateButtonOption( int );
164 
CountOptions()165 	int				CountOptions() { return mnOptions; }
GetDeviceNumber()166 	int				GetDeviceNumber() { return mnDevice; }
167 
168 	sal_Bool			Open( const char* );
169 	sal_Bool			Open( int );
170 	void			Close();
171 	void			ReloadDevices();
172 	void			ReloadOptions();
173 
174 	sal_Bool			Start( BitmapTransporter& );
175 
176 	inline Link		SetReloadOptionsHdl( const Link& rLink );
177 };
178 
GetOptionElements(int n)179 inline int Sane::GetOptionElements( int n )
180 {
181 	if( mppOptions[n]->type == SANE_TYPE_FIXED ||
182 		mppOptions[n]->type == SANE_TYPE_INT )
183 	{
184 		return mppOptions[n]->size/sizeof( SANE_Word );
185 	}
186 	return 1;
187 }
188 
SetReloadOptionsHdl(const Link & rLink)189 inline Link	Sane::SetReloadOptionsHdl( const Link& rLink )
190 {
191 	Link aRet = maReloadOptionsLink;
192 	maReloadOptionsLink = rLink;
193 	return aRet;
194 }
195 
196 #endif
197