summaryrefslogtreecommitdiffstats
path: root/extensions/source/scanner/scanunx.cxx
blob: b06e30a2794a47e5386e9109eaef3718553c13db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "scanner.hxx"
#include "sanedlg.hxx"
#include <o3tl/safeint.hxx>
#include <osl/thread.hxx>
#include <sal/log.hxx>
#include <vcl/svapp.hxx>
#include <memory>

#include <com/sun/star/scanner/ScannerException.hpp>

BitmapTransporter::BitmapTransporter()
{
    SAL_INFO("extensions.scanner", "BitmapTransporter");
}


BitmapTransporter::~BitmapTransporter()
{
    SAL_INFO("extensions.scanner", "~BitmapTransporter");
}


css::awt::Size BitmapTransporter::getSize()
{
    osl::MutexGuard aGuard( m_aProtector );
    css::awt::Size   aRet;

    // ensure that there is at least a header
    int nLen = m_aStream.TellEnd();
    if( nLen > 15 )
    {
        int nPreviousPos = m_aStream.Tell();
        m_aStream.Seek( 4 );
        m_aStream.ReadInt32( aRet.Width ).ReadInt32( aRet.Height );
        m_aStream.Seek( nPreviousPos );
    }
    else
        aRet.Width = aRet.Height = 0;


    return aRet;
}


Sequence< sal_Int8 > BitmapTransporter::getDIB()
{
    osl::MutexGuard aGuard( m_aProtector );
    int         nPreviousPos = m_aStream.Tell();

    // create return value
    int nBytes = m_aStream.TellEnd();
    m_aStream.Seek( 0 );

    Sequence< sal_Int8 > aValue( nBytes );
    m_aStream.ReadBytes( aValue.getArray(), nBytes );
    m_aStream.Seek( nPreviousPos );

    return aValue;
}

namespace {

struct SaneHolder
{
    Sane                m_aSane;
    Reference< css::awt::XBitmap > m_xBitmap;
    osl::Mutex          m_aProtector;
    ScanError           m_nError;
    bool                m_bBusy;

    SaneHolder() : m_nError(ScanError_ScanErrorNone), m_bBusy(false) {}
};

    typedef std::vector< std::shared_ptr<SaneHolder> > sanevec;
    class allSanes
    {
    private:
        int mnRefCount;
    public:
        sanevec m_aSanes;
        allSanes() : mnRefCount(0) {}
        void acquire();
        void release();
    };

    void allSanes::acquire()
    {
        ++mnRefCount;
    }

    void allSanes::release()
    {
        // was unused, now because of i99835: "Scanning interface not SANE API
        // compliant" destroy all SaneHolder to get Sane Dtor called
        --mnRefCount;
        if (!mnRefCount)
            m_aSanes.clear();
    }

    struct theSaneProtector : public rtl::Static<osl::Mutex, theSaneProtector> {};
    struct theSanes : public rtl::Static<allSanes, theSanes> {};

class ScannerThread : public osl::Thread
{
    std::shared_ptr<SaneHolder>               m_pHolder;
    Reference< css::lang::XEventListener >    m_xListener;
    ScannerManager*                           m_pManager; // just for the disposing call

public:
    virtual void SAL_CALL run() override;
    virtual void SAL_CALL onTerminated() override { delete this; }
public:
    ScannerThread( const std::shared_ptr<SaneHolder>& pHolder,
                   const Reference< css::lang::XEventListener >& listener,
                   ScannerManager* pManager );
    virtual ~ScannerThread() override;
};

}

ScannerThread::ScannerThread(const std::shared_ptr<SaneHolder>& pHolder,
                             const Reference< css::lang::XEventListener >& listener,
                             ScannerManager* pManager)
        : m_pHolder( pHolder ), m_xListener( listener ), m_pManager( pManager )
{
    SAL_INFO("extensions.scanner", "ScannerThread");
}


ScannerThread::~ScannerThread()
{
    SAL_INFO("extensions.scanner", "~ScannerThread");
}


void ScannerThread::run()
{
    osl_setThreadName("ScannerThread");

    osl::MutexGuard         aGuard( m_pHolder->m_aProtector );
    rtl::Reference<BitmapTransporter> pTransporter = new BitmapTransporter;

    m_pHolder->m_xBitmap = pTransporter;

    m_pHolder->m_bBusy = true;
    if( m_pHolder->m_aSane.IsOpen() )
    {
        int nOption = m_pHolder->m_aSane.GetOptionByName( "preview" );
        if( nOption != -1 )
            m_pHolder->m_aSane.SetOptionValue( nOption, false );

        m_pHolder->m_nError =
            m_pHolder->m_aSane.Start( *pTransporter ) ?
            ScanError_ScanErrorNone : ScanError_ScanCanceled;
    }
    else
        m_pHolder->m_nError = ScanError_ScannerNotAvailable;


    Reference< XInterface > xXInterface( static_cast< OWeakObject* >( m_pManager ) );
    m_xListener->disposing( css::lang::EventObject(xXInterface) );
    m_pHolder->m_bBusy = false;
}


void ScannerManager::AcquireData()
{
    osl::MutexGuard aGuard( theSaneProtector::get() );
    theSanes::get().acquire();
}


void ScannerManager::ReleaseData()
{
    osl::MutexGuard aGuard( theSaneProtector::get() );
    theSanes::get().release();
}


css::awt::Size ScannerManager::getSize()
{
    css::awt::Size aRet;
    aRet.Width = aRet.Height = 0;
    return aRet;
}


Sequence< sal_Int8 > ScannerManager::getDIB()
{
    return Sequence< sal_Int8 >();
}


Sequence< ScannerContext > ScannerManager::getAvailableScanners()
{
    osl::MutexGuard aGuard( theSaneProtector::get() );
    sanevec &rSanes = theSanes::get().m_aSanes;

    if( rSanes.empty() )
    {
        auto pSaneHolder = std::make_shared<SaneHolder>();
        if( Sane::IsSane() )
            rSanes.push_back( pSaneHolder );
    }

    if( Sane::IsSane() )
    {
        Sequence< ScannerContext > aRet{ { /* ScannerName */ "SANE", /* InternalData */ 0 } };
        return aRet;
    }

    return Sequence< ScannerContext >();
}


sal_Bool ScannerManager::configureScannerAndScan( ScannerContext& scanner_context,
                                                  const Reference< css::lang::XEventListener >& listener )
{
    bool bRet;
    bool bScan;
    {
        osl::MutexGuard aGuard( theSaneProtector::get() );
        sanevec &rSanes = theSanes::get().m_aSanes;

        SAL_INFO("extensions.scanner", "ScannerManager::configureScanner");

        if( scanner_context.InternalData < 0 || o3tl::make_unsigned(scanner_context.InternalData) >= rSanes.size() )
            throw ScannerException(
                "Scanner does not exist",
                Reference< XScannerManager >( this ),
                ScanError_InvalidContext
            );

        std::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
        if( pHolder->m_bBusy )
            throw ScannerException(
                "Scanner is busy",
                Reference< XScannerManager >( this ),
                ScanError_ScanInProgress
            );

        pHolder->m_bBusy = true;
        SaneDlg aDlg(Application::GetFrameWeld(mxDialogParent), pHolder->m_aSane, listener.is());
        bRet = aDlg.run();
        bScan = aDlg.getDoScan();
        pHolder->m_bBusy = false;
    }
    if ( bScan )
        startScan( scanner_context, listener );

    return bRet;
}


void ScannerManager::startScan( const ScannerContext& scanner_context,
                                const Reference< css::lang::XEventListener >& listener )
{
    osl::MutexGuard aGuard( theSaneProtector::get() );
    sanevec &rSanes = theSanes::get().m_aSanes;

    SAL_INFO("extensions.scanner", "ScannerManager::startScan");

    if( scanner_context.InternalData < 0 || o3tl::make_unsigned(scanner_context.InternalData) >= rSanes.size() )
        throw ScannerException(
            "Scanner does not exist",
            Reference< XScannerManager >( this ),
            ScanError_InvalidContext
            );
    std::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
    if( pHolder->m_bBusy )
        throw ScannerException(
            "Scanner is busy",
            Reference< XScannerManager >( this ),
            ScanError_ScanInProgress
            );
    pHolder->m_bBusy = true;

    ScannerThread* pThread = new ScannerThread( pHolder, listener, this );
    pThread->create();
}


ScanError ScannerManager::getError( const ScannerContext& scanner_context )
{
    osl::MutexGuard aGuard( theSaneProtector::get() );
    sanevec &rSanes = theSanes::get().m_aSanes;

    if( scanner_context.InternalData < 0 || o3tl::make_unsigned(scanner_context.InternalData) >= rSanes.size() )
        throw ScannerException(
            "Scanner does not exist",
            Reference< XScannerManager >( this ),
            ScanError_InvalidContext
            );

    std::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];

    return pHolder->m_nError;
}


Reference< css::awt::XBitmap > ScannerManager::getBitmap( const ScannerContext& scanner_context )
{
    osl::MutexGuard aGuard( theSaneProtector::get() );
    sanevec &rSanes = theSanes::get().m_aSanes;

    if( scanner_context.InternalData < 0 || o3tl::make_unsigned(scanner_context.InternalData) >= rSanes.size() )
        throw ScannerException(
            "Scanner does not exist",
            Reference< XScannerManager >( this ),
            ScanError_InvalidContext
            );
    std::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];

    osl::MutexGuard aProtGuard( pHolder->m_aProtector );

    Reference< css::awt::XBitmap > xRet( pHolder->m_xBitmap );
    pHolder->m_xBitmap.clear();

    return xRet;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */