22#include <com/sun/star/uno/Reference.hxx>
23#include <com/sun/star/frame/XFrame.hpp>
24#include <com/sun/star/frame/Desktop.hpp>
25#include <com/sun/star/scanner/ScannerException.hpp>
31#include <config_folders.h>
33#include <osl/conditn.hxx>
34#include <osl/file.hxx>
35#include <osl/mutex.hxx>
36#include <rtl/bootstrap.hxx>
50 TWAIN_STATE_SCANNING = 1,
52 TWAIN_STATE_CANCELED = 3
57 using pointer = HANDLE;
58 void operator()(HANDLE h) { CloseHandle(h); }
61using ScopedHANDLE = std::unique_ptr<HANDLE, HANDLEDeleter>;
71 const css::uno::Reference<css::lang::XEventListener>& rxListener,
73 void WaitReadyForNextTask();
75 TwainState
GetState()
const {
return meState; }
78 friend class ShimListenerThread;
83 ~ShimListenerThread()
override;
84 void execute()
override;
85 const OUString& getError() {
return msErrorReported; }
88 bool WaitInitialization();
89 bool WaitRequestResult();
90 void DontNotify() { mbDontNotify =
true; }
91 void RequestDestroy();
92 bool RequestSelectSource();
93 bool RequestInitXfer();
97 bool mbDontNotify =
false;
98 HWND mhWndShim =
nullptr;
99 OUString msErrorReported;
100 osl::Condition mcInitCompleted;
101 bool mbInitSucceeded =
false;
102 osl::Condition mcGotRequestResult;
103 bool mbRequestResult =
false;
105 void SendShimRequest(WPARAM nRequest);
106 bool SendShimRequestWithResult(WPARAM nRequest);
107 void NotificationHdl(WPARAM nEvent, LPARAM lParam);
108 void NotifyOwner(WPARAM nEvent);
109 void NotifyXFerOwner(LPARAM nHandle);
111 css::uno::Reference<css::lang::XEventListener>
mxListener;
112 css::uno::Reference<css::scanner::XScannerManager> mxMgr;
114 TwainState meState = TWAIN_STATE_NONE;
119 DECL_LINK(ImpNotifyXferHdl,
void*,
void);
120 void Notify(WPARAM nEvent);
121 void NotifyXFer(LPARAM nHandle);
131 :
salhelper::Thread(
"TWAINShimListenerThread")
132 , mxTopWindow(xTopWindow)
136 mxTopWindow->IncModalCount();
140Twain::ShimListenerThread::~ShimListenerThread()
144 mxTopWindow->DecModalCount();
148bool Twain::ShimListenerThread::WaitInitialization()
150 mcInitCompleted.wait();
151 return mbInitSucceeded;
154bool Twain::ShimListenerThread::WaitRequestResult()
156 mcGotRequestResult.wait();
157 return mbRequestResult;
160void Twain::ShimListenerThread::SendShimRequest(WPARAM nRequest)
166bool Twain::ShimListenerThread::SendShimRequestWithResult(WPARAM nRequest)
168 mcGotRequestResult.reset();
169 mbRequestResult =
false;
170 SendShimRequest(nRequest);
171 return WaitRequestResult();
174void Twain::ShimListenerThread::RequestDestroy() { SendShimRequest(
TWAIN_REQUEST_QUIT); }
176bool Twain::ShimListenerThread::RequestSelectSource()
178 assert(mbInitSucceeded);
182bool Twain::ShimListenerThread::RequestInitXfer()
184 assert(mbInitSucceeded);
188void Twain::ShimListenerThread::NotifyOwner(WPARAM nEvent)
191 aTwain.Notify(nEvent);
194void Twain::ShimListenerThread::NotifyXFerOwner(LPARAM nHandle)
197 aTwain.NotifyXFer(nHandle);
201void Twain::ShimListenerThread::NotificationHdl(WPARAM nEvent, LPARAM lParam)
206 if (!mcInitCompleted.check())
210 mhWndShim =
reinterpret_cast<HWND
>(lParam);
212 mbInitSucceeded = lParam != 0;
213 mcInitCompleted.set();
220 NotifyXFerOwner(lParam);
223 mbRequestResult = lParam;
224 mcGotRequestResult.set();
232void Twain::ShimListenerThread::execute()
236 PeekMessageW(&msg,
nullptr, 0, 0, PM_NOREMOVE);
240 ScopedHANDLE hShimProcess;
243 OUString shimURL(
"$BRAND_BASE_DIR/" LIBO_BIN_FOLDER
"/twain32shim.exe");
244 rtl::Bootstrap::expandMacros(shimURL);
247 if (osl::FileBase::getSystemPathFromFileURL(shimURL, sCmdLine) != osl::FileBase::E_None)
248 throw std::exception(
"getSystemPathFromFileURL failed!");
251 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(),
252 &hDup, SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION, TRUE, 0))
255 ScopedHANDLE hScopedDup(hDup);
256 DWORD nDup =
static_cast<DWORD
>(
reinterpret_cast<sal_uIntPtr
>(hDup));
257 if (
reinterpret_cast<HANDLE
>(nDup) != hDup)
258 throw std::exception(
"HANDLE does not fit to 32 bit - cannot pass to shim!");
261 sCmdLine =
"\"" + sCmdLine +
"\" " + OUString::number(nDup);
268 PROCESS_INFORMATION pi;
270 if (!CreateProcessW(
nullptr,
const_cast<LPWSTR
>(o3tl::toW(sCmdLine.getStr())),
nullptr,
271 nullptr, TRUE, CREATE_NO_WINDOW,
nullptr,
nullptr, &si, &pi))
274 CloseHandle(pi.hThread);
275 hShimProcess.reset(pi.hProcess);
277 HANDLE
h = hShimProcess.get();
280 DWORD nWaitResult = MsgWaitForMultipleObjects(1, &h, FALSE, INFINITE, QS_POSTMESSAGE);
283 while (PeekMessageW(&msg,
nullptr, 0, 0, PM_REMOVE))
288 NotificationHdl(msg.wParam, msg.lParam);
291 if (nWaitResult == WAIT_OBJECT_0)
296 if (nWaitResult == WAIT_FAILED)
303 catch (
const std::exception& e)
305 msErrorReported = OUString(e.what(), strlen(e.what()), RTL_TEXTENCODING_UTF8);
307 mcInitCompleted.set();
310 mcGotRequestResult.set();
318 osl::MutexGuard aGuard(maMutex);
321 mpThread->DontNotify();
322 mpThread->RequestDestroy();
331 if (!mpThread->getError().isEmpty())
332 SAL_WARN(
"extensions.scanner", mpThread->getError());
340 osl::MutexGuard aGuard(maMutex);
345 mxMgr = mpCurMgr = &rMgr;
347 mpThread.set(
new ShimListenerThread(xTopWindow));
349 const bool bSuccess = mpThread->WaitInitialization();
356void Twain::Notify(WPARAM nEvent)
361void Twain::NotifyXFer(LPARAM nHandle)
364 reinterpret_cast<void*
>(nHandle));
369 osl::MutexGuard aGuard(maMutex);
372 if (InitializeNewShim(rMgr, xTopWindow))
374 meState = TWAIN_STATE_NONE;
375 bRet = mpThread->RequestSelectSource();
382 const css::uno::Reference<css::lang::XEventListener>& rxListener,
385 osl::MutexGuard aGuard(maMutex);
388 if (InitializeNewShim(rMgr, xTopWindow))
391 meState = TWAIN_STATE_NONE;
392 bRet = mpThread->RequestInitXfer();
398void Twain::WaitReadyForNextTask()
401 osl::MutexGuard aGuard(maMutex);
402 return bool(mpThread);
409IMPL_LINK(Twain, ImpNotifyHdl,
void*, pParam,
void)
411 osl::MutexGuard aGuard(maMutex);
412 WPARAM nEvent =
reinterpret_cast<WPARAM
>(pParam);
416 meState = TWAIN_STATE_SCANNING;
421 if (meState != TWAIN_STATE_DONE)
422 meState = TWAIN_STATE_CANCELED;
424 css::lang::EventObject event(mxMgr);
442IMPL_LINK(Twain, ImpNotifyXferHdl,
void*, pParam,
void)
444 osl::MutexGuard aGuard(maMutex);
447 mpCurMgr->SetData(pParam);
448 meState = pParam ? TWAIN_STATE_DONE : TWAIN_STATE_CANCELED;
450 css::lang::EventObject event(mxMgr);
455 mxListener->disposing(css::lang::EventObject(mxMgr));
466 css::uno::Reference<css::frame::XDesktop2> xDesktop
468 if (css::uno::Reference<css::frame::XFrame> xActiveFrame = xDesktop->getActiveFrame())
471 catch (
const css::uno::Exception&)
474 SAL_WARN(
"extensions.scanner",
"ImplGetActiveFrame: Could not determine active frame!");
486 CloseHandle(
static_cast<HANDLE
>(
mpData));
497 HANDLE hMap =
static_cast<HANDLE
>(
mpData);
499 const sal_Int8* pMap =
static_cast<sal_Int8*
>(MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0));
502 const BITMAPINFOHEADER* pBIH =
reinterpret_cast<const BITMAPINFOHEADER*
>(pMap + 4);
503 aRet.Width = pBIH->biWidth;
504 aRet.Height = pBIH->biHeight;
506 UnmapViewOfFile(pMap);
515 css::uno::Sequence<sal_Int8> aRet;
519 HANDLE hMap =
static_cast<HANDLE
>(
mpData);
521 const sal_Int8* pMap =
static_cast<sal_Int8*
>(MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0));
525 memcpy(&nDIBSize, pMap, 4);
527 const BITMAPINFOHEADER* pBIH =
reinterpret_cast<const BITMAPINFOHEADER*
>(pMap + 4);
529 sal_uInt32 nColEntries = 0;
531 switch (pBIH->biBitCount)
536 nColEntries = pBIH->biClrUsed ? pBIH->biClrUsed : (1 << pBIH->biBitCount);
540 nColEntries = pBIH->biClrUsed ? pBIH->biClrUsed : 0;
545 nColEntries = pBIH->biClrUsed;
546 if (pBIH->biCompression == BI_BITFIELDS)
551 aRet = css::uno::Sequence<sal_Int8>(
sizeof(BITMAPFILEHEADER) + nDIBSize);
555 =
new SvMemoryStream(pBuf,
sizeof(BITMAPFILEHEADER), StreamMode::WRITE);
558 pMemStm->
WriteUInt32(
sizeof(BITMAPFILEHEADER) + pBIH->biSize
559 + (nColEntries *
sizeof(RGBQUAD)));
562 memcpy(pBuf +
sizeof(BITMAPFILEHEADER), pBIH, nDIBSize);
564 UnmapViewOfFile(pMap);
576 css::uno::Sequence<ScannerContext> aRet(1);
578 aRet.getArray()[0].ScannerName =
"TWAIN";
579 aRet.getArray()[0].InternalData = 0;
585 ScannerContext& rContext,
const css::uno::Reference<css::lang::XEventListener>& rxListener)
588 css::uno::Reference<XScannerManager> xThis(
this);
590 if (rContext.InternalData != 0 || rContext.ScannerName !=
"TWAIN")
591 throw ScannerException(
"Scanner does not exist", xThis, ScanError_InvalidContext);
601 xTopWindow->DecModalCount();
604 const bool bSelected = aTwain.SelectSource(*
this, xTopWindow);
607 aTwain.WaitReadyForNextTask();
608 aTwain.PerformTransfer(*
this, rxListener, xTopWindow);
615 const css::uno::Reference<css::lang::XEventListener>& rxListener)
618 css::uno::Reference<XScannerManager> xThis(
this);
620 if (rContext.InternalData != 0 || rContext.ScannerName !=
"TWAIN")
621 throw ScannerException(
"Scanner does not exist", xThis, ScanError_InvalidContext);
624 aTwain.PerformTransfer(*
this, rxListener, ImplGetActiveFrameWindow());
630 css::uno::Reference<XScannerManager> xThis(
this);
632 if (rContext.InternalData != 0 || rContext.ScannerName !=
"TWAIN")
633 throw ScannerException(
"Scanner does not exist", xThis, ScanError_InvalidContext);
635 return ((aTwain.GetState() == TWAIN_STATE_CANCELED) ? ScanError_ScanCanceled
636 : ScanError_ScanErrorNone);
639css::uno::Reference<css::awt::XBitmap>
643 return css::uno::Reference<css::awt::XBitmap>(
this);
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static bool Reschedule(bool bHandleAllCurrentEvents=false)
virtual Sequence< sal_Int8 > SAL_CALL getDIB() override
virtual css::awt::Size SAL_CALL getSize() override
virtual Sequence< ScannerContext > SAL_CALL getAvailableScanners() override
static void AcquireData()
virtual ScanError SAL_CALL getError(const ScannerContext &scanner_context) override
virtual void SAL_CALL startScan(const ScannerContext &scanner_context, const Reference< css::lang::XEventListener > &rxListener) override
virtual Reference< css::awt::XBitmap > SAL_CALL getBitmap(const ScannerContext &scanner_context) override
virtual sal_Bool SAL_CALL configureScannerAndScan(ScannerContext &scanner_context, const Reference< css::lang::XEventListener > &rxListener) override
SvStream & WriteUInt32(sal_uInt32 nUInt32)
SvStream & WriteChar(char nChar)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
Reference< script::XScriptListener > mxListener
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
#define LINK(Instance, Class, Member)
#define SAL_WARN(area, stream)
IMPL_LINK(TypeSelectionPage, OnTypeSelected, weld::Toggleable &, rButton, void)
Reference< XComponentContext > getProcessComponentContext()
#define TWAIN_REQUEST_SELECTSOURCE
void ThrowLastError(const char *sFunc)
#define TWAIN_EVENT_REQUESTRESULT
#define TWAIN_REQUEST_INITXFER
#define TWAIN_EVENT_NOTIFYHWND
#define TWAIN_REQUEST_QUIT
#define TWAIN_EVENT_SCANNING