xref: /trunk/main/extensions/source/scanner/sane.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb) !
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _SANE_HXX
28 #define _SANE_HXX
29 
30 #include <osl/thread.h>
31 #include <osl/module.h>
32 #include <tools/string.hxx>
33 #include <vcl/bitmap.hxx>
34 #include <sane/sane.h>
35 #include <scanner.hxx>
36 
37 // ---------------------
38 // - BitmapTransporter -
39 // ---------------------
40 
41 class BitmapTransporter : public OWeakObject, AWT::XBitmap
42 {
43     SvMemoryStream                      m_aStream;
44     vos::OMutex                         m_aProtector;
45 
46 public:
47 
48                                         BitmapTransporter();
49     virtual                             ~BitmapTransporter();
50 
51 
52     // XInterface
53     virtual ANY SAL_CALL                queryInterface( const Type & rType ) throw( RuntimeException );
54     virtual void SAL_CALL               acquire() throw() { OWeakObject::acquire(); }
55     virtual void SAL_CALL               release() throw() { OWeakObject::release(); }
56 
57     virtual AWT::Size SAL_CALL          getSize() throw();
58     virtual SEQ( sal_Int8 ) SAL_CALL    getDIB() throw();
59     virtual SEQ( sal_Int8 ) SAL_CALL    getMaskDIB() throw() { return SEQ( sal_Int8 )(); }
60 
61     // Misc
62     void                                lock() { m_aProtector.acquire(); }
63     void                                unlock() { m_aProtector.release(); }
64     SvMemoryStream&                     getStream() { return m_aStream; }
65 };
66 
67 // --------
68 // - Sane -
69 // --------
70 
71 class Sane
72 {
73 private:
74     static int              nRefCount;
75     static oslModule        pSaneLib;
76 
77     static SANE_Status      (*p_init)( SANE_Int*,
78                                        SANE_Auth_Callback );
79     static void             (*p_exit)();
80     static SANE_Status      (*p_get_devices)( const SANE_Device***,
81                                               SANE_Bool );
82     static SANE_Status      (*p_open)( SANE_String_Const, SANE_Handle );
83     static void             (*p_close)( SANE_Handle );
84     static const SANE_Option_Descriptor* (*p_get_option_descriptor)(
85         SANE_Handle, SANE_Int );
86     static SANE_Status      (*p_control_option)( SANE_Handle, SANE_Int,
87                                                  SANE_Action, void*,
88                                                  SANE_Int* );
89     static SANE_Status      (*p_get_parameters)( SANE_Handle,
90                                                  SANE_Parameters* );
91     static SANE_Status      (*p_start)( SANE_Handle );
92     static SANE_Status      (*p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
93                                        SANE_Int* );
94     static void             (*p_cancel)( SANE_Handle );
95     static SANE_Status      (*p_set_io_mode)( SANE_Handle, SANE_Bool );
96     static SANE_Status      (*p_get_select_fd)( SANE_Handle, SANE_Int* );
97     static SANE_String_Const (*p_strstatus)( SANE_Status );
98 
99     static SANE_Int             nVersion;
100     static SANE_Device**        ppDevices;
101     static int                  nDevices;
102 
103     const SANE_Option_Descriptor**  mppOptions;
104     int                             mnOptions;
105     int                             mnDevice;
106     SANE_Handle                     maHandle;
107 
108     Link                        maReloadOptionsLink;
109 
110     inline oslGenericFunction
111                     LoadSymbol( const char* );
112     void            Init();
113     void            DeInit();
114 
115     SANE_Status ControlOption( int, SANE_Action, void* );
116 
117     sal_Bool CheckConsistency( const char*, sal_Bool bInit = sal_False );
118 
119 public:
120     Sane();
121     ~Sane();
122 
123     static sal_Bool         IsSane()
124         { return pSaneLib ? sal_True : sal_False; }
125     sal_Bool            IsOpen()
126         { return maHandle ? sal_True : sal_False; }
127     static int              CountDevices()
128         { return nDevices; }
129     static String           GetName( int n )
130         { return String( ppDevices[n]->name ? ppDevices[n]->name : "", osl_getThreadTextEncoding() ); }
131     static String           GetVendor( int n )
132         { return String( ppDevices[n]->vendor ? ppDevices[n]->vendor : "", osl_getThreadTextEncoding() ); }
133     static String           GetModel( int n )
134         { return String( ppDevices[n]->model ? ppDevices[n]->model : "", osl_getThreadTextEncoding() ); }
135     static String           GetType( int n )
136         { return String( ppDevices[n]->type ? ppDevices[n]->type : "", osl_getThreadTextEncoding() ); }
137 
138     String          GetOptionName( int n )
139         { return String( mppOptions[n]->name ? (char*)mppOptions[n]->name : "", osl_getThreadTextEncoding() ); }
140     String          GetOptionTitle( int n )
141         { return String( mppOptions[n]->title ? (char*)mppOptions[n]->title : "", osl_getThreadTextEncoding() ); }
142     SANE_Value_Type GetOptionType( int n )
143         { return mppOptions[n]->type; }
144     SANE_Unit       GetOptionUnit( int n )
145         { return mppOptions[n]->unit; }
146     String          GetOptionUnitName( int n );
147     SANE_Int        GetOptionCap( int n )
148         { return mppOptions[n]->cap; }
149     SANE_Constraint_Type GetOptionConstraintType( int n )
150         { return mppOptions[n]->constraint_type; }
151     const char**    GetStringConstraint( int n )
152         { return (const char**)mppOptions[n]->constraint.string_list; }
153     int             GetRange( int, double*& );
154 
155     inline int      GetOptionElements( int n );
156     int             GetOptionByName( const char* );
157     sal_Bool            GetOptionValue( int, sal_Bool& );
158     sal_Bool            GetOptionValue( int, ByteString& );
159     sal_Bool            GetOptionValue( int, double&, int nElement = 0 );
160     sal_Bool            GetOptionValue( int, double* );
161 
162     sal_Bool            SetOptionValue( int, sal_Bool );
163     sal_Bool            SetOptionValue( int, const String& );
164     sal_Bool            SetOptionValue( int, double, int nElement = 0 );
165     sal_Bool            SetOptionValue( int, double* );
166 
167     sal_Bool            ActivateButtonOption( int );
168 
169     int             CountOptions() { return mnOptions; }
170     int             GetDeviceNumber() { return mnDevice; }
171 
172     sal_Bool            Open( const char* );
173     sal_Bool            Open( int );
174     void            Close();
175     void            ReloadDevices();
176     void            ReloadOptions();
177 
178     sal_Bool            Start( BitmapTransporter& );
179 
180     inline Link     SetReloadOptionsHdl( const Link& rLink );
181 };
182 
183 inline int Sane::GetOptionElements( int n )
184 {
185     if( mppOptions[n]->type == SANE_TYPE_FIXED ||
186         mppOptions[n]->type == SANE_TYPE_INT )
187     {
188         return mppOptions[n]->size/sizeof( SANE_Word );
189     }
190     return 1;
191 }
192 
193 inline Link Sane::SetReloadOptionsHdl( const Link& rLink )
194 {
195     Link aRet = maReloadOptionsLink;
196     maReloadOptionsLink = rLink;
197     return aRet;
198 }
199 
200 #endif
201