1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <sal/config.h>
23 #include <string_view>
25 #include <cppuhelper/implbase.hxx>
26 #include <osl/thread.h>
27 #include <osl/module.h>
28 #include <tools/stream.hxx>
29 #include <tools/link.hxx>
30 #include <sane/sane.h>
32 #include <com/sun/star/awt/XBitmap.hpp>
33 #include <com/sun/star/uno/Sequence.hxx>
35 using namespace com::sun::star::uno
;
38 class BitmapTransporter
: public cppu::WeakImplHelper
<css::awt::XBitmap
>
40 SvMemoryStream m_aStream
;
41 osl::Mutex m_aProtector
;
46 virtual ~BitmapTransporter() override
;
48 virtual css::awt::Size SAL_CALL
getSize() override
;
49 virtual Sequence
< sal_Int8
> SAL_CALL
getDIB() override
;
50 virtual Sequence
< sal_Int8
> SAL_CALL
getMaskDIB() override
{ return Sequence
< sal_Int8
>(); }
53 void lock() { m_aProtector
.acquire(); }
54 void unlock() { m_aProtector
.release(); }
55 SvMemoryStream
& getStream() { return m_aStream
; }
63 static oslModule pSaneLib
;
65 static SANE_Status (*p_init
)( SANE_Int
*,
67 static void (*p_exit
)();
68 static SANE_Status (*p_get_devices
)( const SANE_Device
***,
70 static SANE_Status (*p_open
)( SANE_String_Const
, SANE_Handle
);
71 static void (*p_close
)( SANE_Handle
);
72 static const SANE_Option_Descriptor
* (*p_get_option_descriptor
)(
73 SANE_Handle
, SANE_Int
);
74 static SANE_Status (*p_control_option
)( SANE_Handle
, SANE_Int
,
77 static SANE_Status (*p_get_parameters
)( SANE_Handle
,
79 static SANE_Status (*p_start
)( SANE_Handle
);
80 static SANE_Status (*p_read
)( SANE_Handle
, SANE_Byte
*, SANE_Int
,
82 static void (*p_cancel
)( SANE_Handle
);
83 static SANE_Status (*p_set_io_mode
)( SANE_Handle
, SANE_Bool
);
84 static SANE_Status (*p_get_select_fd
)( SANE_Handle
, SANE_Int
* );
85 static SANE_String_Const (*p_strstatus
)( SANE_Status
);
87 static SANE_Int nVersion
;
88 static SANE_Device
** ppDevices
;
91 std::unique_ptr
<const SANE_Option_Descriptor
*[]> mppOptions
;
96 Link
<Sane
&,void> maReloadOptionsLink
;
98 static inline oslGenericFunction
99 LoadSymbol( const char* );
101 static void DeInit();
103 SANE_Status
ControlOption( int, SANE_Action
, void* );
105 bool CheckConsistency( const char*, bool bInit
= false );
112 { return pSaneLib
!= nullptr; }
114 { return maHandle
!= nullptr; }
115 static int CountDevices()
117 static OUString
GetName( int n
)
118 { return ppDevices
[n
]->name
? OUString( ppDevices
[n
]->name
, strlen(ppDevices
[n
]->name
), osl_getThreadTextEncoding() ) : OUString(); }
119 static OUString
GetVendor( int n
)
120 { return ppDevices
[n
]->vendor
? OUString( ppDevices
[n
]->vendor
, strlen(ppDevices
[n
]->vendor
), osl_getThreadTextEncoding() ) : OUString(); }
121 static OUString
GetModel( int n
)
122 { return ppDevices
[n
]->model
? OUString( ppDevices
[n
]->model
, strlen(ppDevices
[n
]->model
), osl_getThreadTextEncoding() ) : OUString(); }
123 static OUString
GetType( int n
)
124 { return ppDevices
[n
]->type
? OUString( ppDevices
[n
]->type
, strlen(ppDevices
[n
]->type
), osl_getThreadTextEncoding() ) : OUString(); }
126 OUString
GetOptionName( int n
)
127 { return mppOptions
[n
]->name
? OUString( mppOptions
[n
]->name
, strlen(mppOptions
[n
]->name
), osl_getThreadTextEncoding() ) : OUString(); }
128 OUString
GetOptionTitle( int n
)
129 { return mppOptions
[n
]->title
? OUString( mppOptions
[n
]->title
, strlen(mppOptions
[n
]->title
), osl_getThreadTextEncoding() ) : OUString(); }
130 SANE_Value_Type
GetOptionType( int n
)
131 { return mppOptions
[n
]->type
; }
132 SANE_Unit
GetOptionUnit( int n
)
133 { return mppOptions
[n
]->unit
; }
134 OUString
GetOptionUnitName( int n
);
135 SANE_Int
GetOptionCap( int n
)
136 { return mppOptions
[n
]->cap
; }
137 SANE_Constraint_Type
GetOptionConstraintType( int n
)
138 { return mppOptions
[n
]->constraint_type
; }
139 const char** GetStringConstraint( int n
)
140 { return const_cast<const char**>(mppOptions
[n
]->constraint
.string_list
); }
141 int GetRange( int, std::unique_ptr
<double[]>& );
143 inline int GetOptionElements( int n
);
144 int GetOptionByName( const char* );
145 bool GetOptionValue( int, bool& );
146 bool GetOptionValue( int, OString
& );
147 bool GetOptionValue( int, double&, int nElement
= 0 );
148 bool GetOptionValue( int, double* );
150 void SetOptionValue( int, bool );
151 void SetOptionValue( int, std::u16string_view
);
152 void SetOptionValue( int, double, int nElement
= 0 );
153 void SetOptionValue( int, double const * );
155 bool ActivateButtonOption( int );
157 int CountOptions() { return mnOptions
; }
158 int GetDeviceNumber() const { return mnDevice
; }
160 bool Open( const char* );
163 void ReloadDevices();
164 void ReloadOptions();
166 bool Start( BitmapTransporter
& );
168 inline Link
<Sane
&,void> SetReloadOptionsHdl( const Link
<Sane
&,void>& rLink
);
172 inline int Sane::GetOptionElements( int n
)
174 if( mppOptions
[n
]->type
== SANE_TYPE_FIXED
||
175 mppOptions
[n
]->type
== SANE_TYPE_INT
)
177 return mppOptions
[n
]->size
/sizeof( SANE_Word
);
183 inline Link
<Sane
&,void> Sane::SetReloadOptionsHdl( const Link
<Sane
&,void>& rLink
)
185 Link
<Sane
&,void> aRet
= maReloadOptionsLink
;
186 maReloadOptionsLink
= rLink
;
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */