1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 #ifndef INCLUDED_EXTENSIONS_SOURCE_SCANNER_SANE_HXX
20 #define INCLUDED_EXTENSIONS_SOURCE_SCANNER_SANE_HXX
21 
22 #include <osl/thread.h>
23 #include <osl/module.h>
24 #include <tools/stream.hxx>
25 #include <tools/link.hxx>
26 #include <sane/sane.h>
27 #include "scanner.hxx"
28 
29 
30 class BitmapTransporter: public cppu::WeakImplHelper<css::awt::XBitmap>
31 {
32     SvMemoryStream                      m_aStream;
33     osl::Mutex                          m_aProtector;
34 
35 public:
36 
37                                         BitmapTransporter();
38     virtual                             ~BitmapTransporter() override;
39 
40     virtual css::awt::Size SAL_CALL          getSize() override;
41     virtual Sequence< sal_Int8 > SAL_CALL    getDIB() override;
getMaskDIB()42     virtual Sequence< sal_Int8 > SAL_CALL    getMaskDIB() override { return Sequence< sal_Int8 >(); }
43 
44     // Misc
lock()45     void                                lock() { m_aProtector.acquire(); }
unlock()46     void                                unlock() { m_aProtector.release(); }
getStream()47     SvMemoryStream&                     getStream() { return m_aStream; }
48 };
49 
50 
51 class Sane
52 {
53 private:
54     static int              nRefCount;
55     static oslModule        pSaneLib;
56 
57     static SANE_Status      (*p_init)( SANE_Int*,
58                                        SANE_Auth_Callback );
59     static void             (*p_exit)();
60     static SANE_Status      (*p_get_devices)( const SANE_Device***,
61                                               SANE_Bool );
62     static SANE_Status      (*p_open)( SANE_String_Const, SANE_Handle );
63     static void             (*p_close)( SANE_Handle );
64     static const SANE_Option_Descriptor* (*p_get_option_descriptor)(
65         SANE_Handle, SANE_Int );
66     static SANE_Status      (*p_control_option)( SANE_Handle, SANE_Int,
67                                                  SANE_Action, void*,
68                                                  SANE_Int* );
69     static SANE_Status      (*p_get_parameters)( SANE_Handle,
70                                                  SANE_Parameters* );
71     static SANE_Status      (*p_start)( SANE_Handle );
72     static SANE_Status      (*p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
73                                        SANE_Int* );
74     static void             (*p_cancel)( SANE_Handle );
75     static SANE_Status      (*p_set_io_mode)( SANE_Handle, SANE_Bool );
76     static SANE_Status      (*p_get_select_fd)( SANE_Handle, SANE_Int* );
77     static SANE_String_Const (*p_strstatus)( SANE_Status );
78 
79     static SANE_Int             nVersion;
80     static SANE_Device**        ppDevices;
81     static int                  nDevices;
82 
83     std::unique_ptr<const SANE_Option_Descriptor*[]> mppOptions;
84     int                             mnOptions;
85     int                             mnDevice;
86     SANE_Handle                     maHandle;
87 
88     Link<Sane&,void>                maReloadOptionsLink;
89 
90     static inline oslGenericFunction
91                     LoadSymbol( const char* );
92     static void     Init();
93     static void     DeInit();
94 
95     SANE_Status ControlOption( int, SANE_Action, void* );
96 
97     bool CheckConsistency( const char*, bool bInit = false );
98 
99 public:
100     Sane();
101     ~Sane();
102 
IsSane()103     static bool         IsSane()
104         { return pSaneLib != nullptr; }
IsOpen() const105     bool            IsOpen() const
106         { return maHandle != nullptr; }
CountDevices()107     static int              CountDevices()
108         { return nDevices; }
GetName(int n)109     static OUString         GetName( int n )
110         { return ppDevices[n]->name ? OUString( ppDevices[n]->name, strlen(ppDevices[n]->name),  osl_getThreadTextEncoding() ) : OUString(); }
GetVendor(int n)111     static OUString         GetVendor( int n )
112         { return ppDevices[n]->vendor ? OUString( ppDevices[n]->vendor, strlen(ppDevices[n]->vendor), osl_getThreadTextEncoding() ) : OUString(); }
GetModel(int n)113     static OUString         GetModel( int n )
114         { return ppDevices[n]->model ? OUString( ppDevices[n]->model, strlen(ppDevices[n]->model), osl_getThreadTextEncoding() ) : OUString(); }
GetType(int n)115     static OUString         GetType( int n )
116         { return ppDevices[n]->type ? OUString( ppDevices[n]->type, strlen(ppDevices[n]->type), osl_getThreadTextEncoding() ) : OUString(); }
117 
GetOptionName(int n)118     OUString        GetOptionName( int n )
119         { return mppOptions[n]->name ? OUString( mppOptions[n]->name, strlen(mppOptions[n]->name), osl_getThreadTextEncoding() ) : OUString(); }
GetOptionTitle(int n)120     OUString        GetOptionTitle( int n )
121         { return mppOptions[n]->title ? OUString( mppOptions[n]->title, strlen(mppOptions[n]->title), osl_getThreadTextEncoding() ) : OUString(); }
GetOptionType(int n)122     SANE_Value_Type GetOptionType( int n )
123         { return mppOptions[n]->type; }
GetOptionUnit(int n)124     SANE_Unit       GetOptionUnit( int n )
125         { return mppOptions[n]->unit; }
126     OUString        GetOptionUnitName( int n );
GetOptionCap(int n)127     SANE_Int        GetOptionCap( int n )
128         { return mppOptions[n]->cap; }
GetOptionConstraintType(int n)129     SANE_Constraint_Type GetOptionConstraintType( int n )
130         { return mppOptions[n]->constraint_type; }
GetStringConstraint(int n)131     const char**    GetStringConstraint( int n )
132         { return const_cast<const char**>(mppOptions[n]->constraint.string_list); }
133     int             GetRange( int, std::unique_ptr<double[]>& );
134 
135     inline int      GetOptionElements( int n );
136     int             GetOptionByName( const char* );
137     bool            GetOptionValue( int, bool& );
138     bool            GetOptionValue( int, OString& );
139     bool            GetOptionValue( int, double&, int nElement = 0 );
140     bool            GetOptionValue( int, double* );
141 
142     void            SetOptionValue( int, bool );
143     void            SetOptionValue( int, const OUString& );
144     void            SetOptionValue( int, double, int nElement = 0 );
145     void            SetOptionValue( int, double const * );
146 
147     bool            ActivateButtonOption( int );
148 
CountOptions()149     int             CountOptions() { return mnOptions; }
GetDeviceNumber() const150     int             GetDeviceNumber() const { return mnDevice; }
151 
152     bool            Open( const char* );
153     bool            Open( int );
154     void            Close();
155     void            ReloadDevices();
156     void            ReloadOptions();
157 
158     bool            Start( BitmapTransporter& );
159 
160     inline Link<Sane&,void>   SetReloadOptionsHdl( const Link<Sane&,void>& rLink );
161 };
162 
163 
GetOptionElements(int n)164 inline int Sane::GetOptionElements( int n )
165 {
166     if( mppOptions[n]->type == SANE_TYPE_FIXED ||
167         mppOptions[n]->type == SANE_TYPE_INT )
168     {
169         return mppOptions[n]->size/sizeof( SANE_Word );
170     }
171     return 1;
172 }
173 
174 
SetReloadOptionsHdl(const Link<Sane &,void> & rLink)175 inline Link<Sane&,void> Sane::SetReloadOptionsHdl( const Link<Sane&,void>& rLink )
176 {
177     Link<Sane&,void> aRet = maReloadOptionsLink;
178     maReloadOptionsLink = rLink;
179     return aRet;
180 }
181 
182 #endif // INCLUDED_EXTENSIONS_SOURCE_SCANNER_SANE_HXX
183 
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
185