LibreOffice Module extensions (master) 1
sane.hxx
Go to the documentation of this file.
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#pragma once
20
21#include <sal/config.h>
22
23#include <string_view>
24
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>
31
32#include <com/sun/star/awt/XBitmap.hpp>
33#include <com/sun/star/uno/Sequence.hxx>
34
35using namespace com::sun::star::uno;
36
37
38class BitmapTransporter: public cppu::WeakImplHelper<css::awt::XBitmap>
39{
41 osl::Mutex m_aProtector;
42
43public:
44
46 virtual ~BitmapTransporter() override;
47
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 >(); }
51
52 // Misc
53 void lock() { m_aProtector.acquire(); }
54 void unlock() { m_aProtector.release(); }
56};
57
58
59class Sane
60{
61private:
62 static int nRefCount;
63 static oslModule pSaneLib;
64
65 static SANE_Status (*p_init)( SANE_Int*,
66 SANE_Auth_Callback );
67 static void (*p_exit)();
68 static SANE_Status (*p_get_devices)( const SANE_Device***,
69 SANE_Bool );
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,
75 SANE_Action, void*,
76 SANE_Int* );
77 static SANE_Status (*p_get_parameters)( SANE_Handle,
78 SANE_Parameters* );
79 static SANE_Status (*p_start)( SANE_Handle );
80 static SANE_Status (*p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
81 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 );
86
87 static SANE_Int nVersion;
88 static SANE_Device** ppDevices;
89 static int nDevices;
90
91 std::unique_ptr<const SANE_Option_Descriptor*[]> mppOptions;
94 SANE_Handle maHandle;
95
97
98 static inline oslGenericFunction
99 LoadSymbol( const char* );
100 static void Init();
101 static void DeInit();
102
103 SANE_Status ControlOption( int, SANE_Action, void* );
104
105 bool CheckConsistency( const char*, bool bInit = false );
106
107public:
108 Sane();
109 ~Sane();
110
111 static bool IsSane()
112 { return pSaneLib != nullptr; }
113 bool IsOpen() const
114 { return maHandle != nullptr; }
115 static int CountDevices()
116 { return nDevices; }
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(); }
125
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[]>& );
142
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* );
149
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 * );
154
155 bool ActivateButtonOption( int );
156
157 int CountOptions() { return mnOptions; }
158 int GetDeviceNumber() const { return mnDevice; }
159
160 bool Open( const char* );
161 bool Open( int );
162 void Close();
163 void ReloadDevices();
164 void ReloadOptions();
165
166 bool Start( BitmapTransporter& );
167
169};
170
171
172inline int Sane::GetOptionElements( int n )
173{
174 if( mppOptions[n]->type == SANE_TYPE_FIXED ||
175 mppOptions[n]->type == SANE_TYPE_INT )
176 {
177 return mppOptions[n]->size/sizeof( SANE_Word );
178 }
179 return 1;
180}
181
182
184{
186 maReloadOptionsLink = rLink;
187 return aRet;
188}
189
190/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::awt::Size SAL_CALL getSize() override
Definition: scanunx.cxx:43
void lock()
Definition: sane.hxx:53
virtual Sequence< sal_Int8 > SAL_CALL getDIB() override
Definition: scanunx.cxx:65
virtual ~BitmapTransporter() override
Definition: scanunx.cxx:37
osl::Mutex m_aProtector
Definition: sane.hxx:41
SvMemoryStream m_aStream
Definition: sane.hxx:40
void unlock()
Definition: sane.hxx:54
SvMemoryStream & getStream()
Definition: sane.hxx:55
virtual Sequence< sal_Int8 > SAL_CALL getMaskDIB() override
Definition: sane.hxx:50
Definition: sane.hxx:60
static OUString GetVendor(int n)
Definition: sane.hxx:119
SANE_Unit GetOptionUnit(int n)
Definition: sane.hxx:132
static SANE_Status(* p_get_devices)(const SANE_Device ***, SANE_Bool)
Definition: sane.hxx:68
int mnDevice
Definition: sane.hxx:93
int mnOptions
Definition: sane.hxx:92
bool CheckConsistency(const char *, bool bInit=false)
Definition: sane.cxx:511
bool GetOptionValue(int, bool &)
Definition: sane.cxx:343
bool IsOpen() const
Definition: sane.hxx:113
static SANE_Status(* p_get_select_fd)(SANE_Handle, SANE_Int *)
Definition: sane.hxx:84
bool ActivateButtonOption(int)
Definition: sane.cxx:990
void ReloadOptions()
Definition: sane.cxx:262
SANE_Constraint_Type GetOptionConstraintType(int n)
Definition: sane.hxx:137
OUString GetOptionUnitName(int n)
Definition: sane.cxx:978
void Close()
Definition: sane.cxx:320
Link< Sane &, void > maReloadOptionsLink
Definition: sane.hxx:96
int GetOptionByName(const char *)
Definition: sane.cxx:331
static SANE_Status(* p_start)(SANE_Handle)
Definition: sane.hxx:79
bool Start(BitmapTransporter &)
Definition: sane.cxx:537
SANE_Int GetOptionCap(int n)
Definition: sane.hxx:135
OUString GetOptionName(int n)
Definition: sane.hxx:126
int GetOptionElements(int n)
Definition: sane.hxx:172
int GetRange(int, std::unique_ptr< double[]> &)
Definition: sane.cxx:902
static SANE_Status(* p_read)(SANE_Handle, SANE_Byte *, SANE_Int, SANE_Int *)
Definition: sane.hxx:80
~Sane()
Definition: sane.cxx:161
static void(* p_exit)()
Definition: sane.hxx:67
static SANE_Status(* p_init)(SANE_Int *, SANE_Auth_Callback)
Definition: sane.hxx:65
SANE_Value_Type GetOptionType(int n)
Definition: sane.hxx:130
static int nRefCount
Definition: sane.hxx:62
static SANE_Device ** ppDevices
Definition: sane.hxx:88
static void(* p_close)(SANE_Handle)
Definition: sane.hxx:71
bool Open(const char *)
Definition: sane.cxx:287
static SANE_Status(* p_control_option)(SANE_Handle, SANE_Int, SANE_Action, void *, SANE_Int *)
Definition: sane.hxx:74
SANE_Handle maHandle
Definition: sane.hxx:94
static void DeInit()
Definition: sane.cxx:242
Link< Sane &, void > SetReloadOptionsHdl(const Link< Sane &, void > &rLink)
Definition: sane.hxx:183
std::unique_ptr< const SANE_Option_Descriptor *[]> mppOptions
Definition: sane.hxx:91
static OUString GetModel(int n)
Definition: sane.hxx:121
static SANE_Int nVersion
Definition: sane.hxx:87
SANE_Status ControlOption(int, SANE_Action, void *)
Definition: sane.cxx:120
static int nDevices
Definition: sane.hxx:89
int CountOptions()
Definition: sane.hxx:157
static OUString GetName(int n)
Definition: sane.hxx:117
static SANE_String_Const(* p_strstatus)(SANE_Status)
Definition: sane.hxx:85
static SANE_Status(* p_set_io_mode)(SANE_Handle, SANE_Bool)
Definition: sane.hxx:83
void ReloadDevices()
Definition: sane.cxx:254
static SANE_Status(* p_get_parameters)(SANE_Handle, SANE_Parameters *)
Definition: sane.hxx:77
static SANE_Status(* p_open)(SANE_String_Const, SANE_Handle)
Definition: sane.hxx:70
static oslGenericFunction LoadSymbol(const char *)
Definition: sane.cxx:108
void SetOptionValue(int, bool)
Definition: sane.cxx:412
static void(* p_cancel)(SANE_Handle)
Definition: sane.hxx:82
static bool IsSane()
Definition: sane.hxx:111
int GetDeviceNumber() const
Definition: sane.hxx:158
static OUString GetType(int n)
Definition: sane.hxx:123
static oslModule pSaneLib
Definition: sane.hxx:63
Sane()
Definition: sane.cxx:151
static int CountDevices()
Definition: sane.hxx:115
static void Init()
Definition: sane.cxx:170
const char ** GetStringConstraint(int n)
Definition: sane.hxx:139
OUString GetOptionTitle(int n)
Definition: sane.hxx:128
const char * name
sal_Int64 n
ResultType type