diff --git a/build/windows/HGSaneUser/HGSaneUser.def b/build/windows/HGSaneUser/HGSaneUser.def index c003b6fa..9b48a839 100644 --- a/build/windows/HGSaneUser/HGSaneUser.def +++ b/build/windows/HGSaneUser/HGSaneUser.def @@ -7,7 +7,9 @@ HGSane_DestroyManager HGSane_GetDeviceCount HGSane_GetDeviceName HGSane_OpenDevice +HGSane_OpenSelectedDevice HGSane_CloseDevice HGSane_ShowDeviceSettingDlg HGSane_StartDevice HGSane_StopDevice +HGSane_StartDeviceWithUI diff --git a/modules/base/HGDef.h b/modules/base/HGDef.h index 76d6036a..ca91af08 100644 --- a/modules/base/HGDef.h +++ b/modules/base/HGDef.h @@ -108,6 +108,17 @@ typedef HGUInt HGResult; #define HGAPIV #endif +#if defined(HG_CMP_MSC) + #include + typedef HWND HGWindow; +#else + #ifdef __cplusplus + typedef class QWidget* HGWindow; + #else + typedef void* HGWindow; + #endif +#endif + #define HG_DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name #pragma pack(push) diff --git a/modules/sane_user/HGSane.cpp b/modules/sane_user/HGSane.cpp index 4fd16479..338cc467 100644 --- a/modules/sane_user/HGSane.cpp +++ b/modules/sane_user/HGSane.cpp @@ -79,6 +79,25 @@ HGResult HGAPI HGSane_OpenDevice(HGSaneManager mgr, HGUInt index, HGSaneDevice* return HGBASE_ERR_OK; } +HGResult HGAPI HGSane_OpenSelectedDevice(HGSaneManager mgr, HGWindow parent, HGSaneDevice* dev) +{ + if (NULL == mgr) + { + return HGBASE_ERR_INVALIDARG; + } + + HGSaneManagerImpl* saneManagerImpl = (HGSaneManagerImpl*)mgr; + class HGSaneDeviceImpl* saneDeviceImpl = NULL; + HGResult ret = saneManagerImpl->OpenSelectedDevice(parent, &saneDeviceImpl); + if (HGBASE_ERR_OK != ret) + { + return ret; + } + + *dev = (HGSaneDevice)saneDeviceImpl; + return HGBASE_ERR_OK; +} + HGResult HGAPI HGSane_CloseDevice(HGSaneDevice dev) { if (NULL == dev) @@ -90,11 +109,7 @@ HGResult HGAPI HGSane_CloseDevice(HGSaneDevice dev) return saneDeviceImpl->Close(); } -#ifdef HG_CMP_MSC -HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, HWND parent) -#else -HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, class QWidget* parent) -#endif +HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, HGWindow parent) { if (NULL == dev) { @@ -126,4 +141,15 @@ HGResult HGAPI HGSane_StopDevice(HGSaneDevice dev) HGSaneDeviceImpl* saneDeviceImpl = (HGSaneDeviceImpl*)dev; return saneDeviceImpl->Stop(); +} + +HGResult HGAPI HGSane_StartDeviceWithUI(HGSaneDevice dev, HGWindow parent, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam) +{ + if (NULL == dev) + { + return HGBASE_ERR_INVALIDARG; + } + + HGSaneDeviceImpl* saneDeviceImpl = (HGSaneDeviceImpl*)dev; + return saneDeviceImpl->StartWithUI(parent, imageFunc, imageParam); } \ No newline at end of file diff --git a/modules/sane_user/HGSane.h b/modules/sane_user/HGSane.h index 000ed826..7ec7da98 100644 --- a/modules/sane_user/HGSane.h +++ b/modules/sane_user/HGSane.h @@ -24,17 +24,17 @@ HGEXPORT HGResult HGAPI HGSane_GetDeviceName(HGSaneManager mgr, HGUInt index, HG HGEXPORT HGResult HGAPI HGSane_OpenDevice(HGSaneManager mgr, HGUInt index, HGSaneDevice *dev, HGChar *errInfo, HGUInt errInfoLen); +HGEXPORT HGResult HGAPI HGSane_OpenSelectedDevice(HGSaneManager mgr, HGWindow parent, HGSaneDevice* dev); + HGEXPORT HGResult HGAPI HGSane_CloseDevice(HGSaneDevice dev); -#ifdef HG_CMP_MSC -HGEXPORT HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, HWND parent); -#else -HGEXPORT HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, class QWidget* parent); -#endif +HGEXPORT HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, HGWindow parent); HGEXPORT HGResult HGAPI HGSane_StartDevice(HGSaneDevice dev, HGSane_DeviceEventFunc eventFunc, HGPointer eventParam, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam, HGChar* errInfo, HGUInt errInfoLen); HGEXPORT HGResult HGAPI HGSane_StopDevice(HGSaneDevice dev); +HGEXPORT HGResult HGAPI HGSane_StartDeviceWithUI(HGSaneDevice dev, HGWindow parent, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam); + #endif /* __HGSANE_H__ */ \ No newline at end of file diff --git a/modules/sane_user/HGSaneImpl.cpp b/modules/sane_user/HGSaneImpl.cpp index a75531a3..31398f0b 100644 --- a/modules/sane_user/HGSaneImpl.cpp +++ b/modules/sane_user/HGSaneImpl.cpp @@ -1,10 +1,10 @@ #include "HGSaneImpl.hpp" #include "../base/HGInc.h" +#include HGSaneManagerImpl::HGSaneManagerImpl() { m_dll = NULL; - m_saneDlgDll = NULL; m_f_sane_init = NULL; m_f_sane_exit = NULL; m_f_sane_get_devices = NULL; @@ -18,7 +18,11 @@ HGSaneManagerImpl::HGSaneManagerImpl() m_f_sane_get_parameters = NULL; m_f_sane_get_option_descriptor = NULL; m_f_sane_control_option = NULL; + + m_saneDlgDll = NULL; + m_f_show_devlist_ui = NULL; m_f_show_setting_ui = NULL; + m_f_show_scan_ui = NULL; } HGSaneManagerImpl::~HGSaneManagerImpl() @@ -47,7 +51,9 @@ HGResult HGSaneManagerImpl::Create(const HGChar* sanePath) HGBase_CreateDll("HGSaneDlg.dll", &m_saneDlgDll); if (NULL != m_saneDlgDll) { + HGBase_GetDllProcAddress(m_saneDlgDll, "show_devlist_ui", (HGPointer*)&m_f_show_devlist_ui); HGBase_GetDllProcAddress(m_saneDlgDll, "show_setting_ui", (HGPointer*)&m_f_show_setting_ui); + HGBase_GetDllProcAddress(m_saneDlgDll, "show_scan_ui", (HGPointer*)&m_f_show_scan_ui); } if (SANE_STATUS_GOOD != m_f_sane_init(NULL, NULL)) @@ -164,6 +170,53 @@ HGResult HGSaneManagerImpl::OpenDevice(HGUInt index, HGSaneDeviceImpl** deviceIm return HGBASE_ERR_OK; } +HGResult HGSaneManagerImpl::OpenSelectedDevice(HGWindow parent, class HGSaneDeviceImpl** deviceImpl) +{ + if (NULL == deviceImpl) + { + return HGBASE_ERR_INVALIDARG; + } + + if (NULL == m_f_show_devlist_ui) + { + return HGBASE_ERR_FAIL; + } + + const SANE_Device** device_list; + if (SANE_STATUS_GOOD != m_f_sane_get_devices(&device_list, SANE_TRUE)) + { + return HGSANE_ERR_FAIL; + } + + HGUInt count = 0; + const SANE_Device** p; + for (p = device_list; *p != NULL; ++p) + ++count; + + const char** devNameList = new const char*[count + 1]; + for (HGUInt i = 0; i < count; ++i) + devNameList[i] = device_list[i]->name; + devNameList[count] = 0; + SANE_Handle handle = m_f_show_devlist_ui(devNameList, parent); + delete []devNameList; + if (NULL == handle) + { + return HGSANE_ERR_FAIL; + } + + HGSaneDeviceImpl* newDeviceImpl = new HGSaneDeviceImpl(this); + HGResult ret = newDeviceImpl->Init(handle); + if (HGBASE_ERR_OK != ret) + { + delete newDeviceImpl; + return ret; + } + + m_listDeviceImpl.push_back(newDeviceImpl); + *deviceImpl = newDeviceImpl; + return HGBASE_ERR_OK; +} + HGResult HGSaneManagerImpl::FindFunctions() { HGResult ret = HGBASE_ERR_OK; @@ -262,6 +315,15 @@ HGSaneDeviceImpl::~HGSaneDeviceImpl() } +HGResult HGSaneDeviceImpl::Init(SANE_Handle handle) +{ + assert(NULL == m_devHandle); + + m_mgrImpl->m_f_sane_set_io_mode(handle, SANE_FALSE); + m_devHandle = handle; + return HGBASE_ERR_OK; +} + HGResult HGSaneDeviceImpl::Open(const HGChar* devName, HGChar* errInfo, HGUInt errInfoLen) { assert(NULL == m_devHandle); @@ -303,12 +365,13 @@ HGResult HGSaneDeviceImpl::Close() return HGBASE_ERR_OK; } -#ifdef HG_CMP_MSC -HGResult HGSaneDeviceImpl::ShowSettingDlg(HWND parent) -#else -HGResult HGSaneDeviceImpl::ShowSettingDlg(class QWidget* parent) -#endif +HGResult HGSaneDeviceImpl::ShowSettingDlg(HGWindow parent) { + if (NULL != m_thread) + { + return HGBASE_ERR_FAIL; + } + if (NULL == m_mgrImpl->m_f_show_setting_ui) { return HGBASE_ERR_FAIL; @@ -402,6 +465,25 @@ HGResult HGSaneDeviceImpl::Stop() return HGBASE_ERR_OK; } +HGResult HGSaneDeviceImpl::StartWithUI(HGWindow parent, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam) +{ + if (NULL != m_thread) + { + return HGBASE_ERR_FAIL; + } + + if (NULL == m_mgrImpl->m_f_show_scan_ui) + { + return HGBASE_ERR_FAIL; + } + + m_dpi = GetDpi(); + m_imageFunc = imageFunc; + m_imageParam = imageParam; + m_mgrImpl->m_f_show_scan_ui(m_devHandle, parent, ShowScanImageCallback, this); + return HGBASE_ERR_OK; +} + HGUInt HGSaneDeviceImpl::GetDpi() { HGUInt dpi = 0; @@ -510,4 +592,36 @@ void HGAPI HGSaneDeviceImpl::ThreadFunc(HGThread thread, HGPointer param) } } } +} + +void HGSaneDeviceImpl::ShowScanImageCallback(const SANE_Parameters* imageFormat, const SANE_Byte* imageData, void* callbackParam) +{ + HGSaneDeviceImpl* p = (HGSaneDeviceImpl*)callbackParam; + + if (NULL != p->m_imageFunc) + { + HGUInt imgType = 0; + if (imageFormat->format == SANE_FRAME_GRAY) + { + if (1 == imageFormat->depth) + imgType = HGBASE_IMGTYPE_BINARY; + else if (8 == imageFormat->depth) + imgType = HGBASE_IMGTYPE_GRAY; + } + else if (imageFormat->format == SANE_FRAME_RGB) + { + imgType = HGBASE_IMGTYPE_RGB; + } + + HGImageInfo imgInfo = { (HGUInt)imageFormat->pixels_per_line, (HGUInt)imageFormat->lines, + imgType, (HGUInt)imageFormat->bytes_per_line, HGBASE_IMGORIGIN_TOP }; + HGImage img = NULL; + HGBase_CreateImageWithData((HGByte*)imageData, &imgInfo, &img); + if (NULL != img) + { + HGBase_SetImageDpi(img, p->m_dpi, p->m_dpi); + p->m_imageFunc((HGSaneDevice)p, img, p->m_imageParam); + HGBase_DestroyImage(img); + } + } } \ No newline at end of file diff --git a/modules/sane_user/HGSaneImpl.hpp b/modules/sane_user/HGSaneImpl.hpp index 7bc4bc43..8c2b5374 100644 --- a/modules/sane_user/HGSaneImpl.hpp +++ b/modules/sane_user/HGSaneImpl.hpp @@ -24,11 +24,11 @@ typedef SANE_String_Const (*f_sane_strstatus)(SANE_Status status); typedef SANE_Status (*f_sane_get_parameters)(SANE_Handle handle, SANE_Parameters* params); typedef SANE_Option_Descriptor* (*f_sane_get_option_descriptor)(SANE_Handle handle, SANE_Int option); typedef SANE_Status (*f_sane_control_option)(SANE_Handle handle, SANE_Int option, SANE_Action action, void* value, SANE_Int* info); -#ifdef HG_CMP_MSC -typedef void (*f_show_setting_ui)(SANE_Handle handle, HWND parent); -#else -typedef void (*f_show_setting_ui)(SANE_Handle handle, class QWidget* parent); -#endif + +typedef SANE_Handle (*f_show_devlist_ui)(const char **devNameList, HGWindow parent); +typedef void (*f_show_setting_ui)(SANE_Handle handle, HGWindow parent); +typedef void (*f_show_scan_ui_image_callback)(const SANE_Parameters *imageFormat, const SANE_Byte *imageData, void * callbackParam); +typedef void (*f_show_scan_ui)(SANE_Handle handle, HGWindow parent, f_show_scan_ui_image_callback callback, void *callbackParam); class HGSaneManagerImpl { @@ -42,6 +42,7 @@ public: HGResult GetDeviceCount(HGUInt *count); HGResult GetDeviceName(HGUInt index, HGChar* name, HGUInt maxLen); HGResult OpenDevice(HGUInt index, class HGSaneDeviceImpl **deviceImpl, HGChar* errInfo, HGUInt errInfoLen); + HGResult OpenSelectedDevice(HGWindow parent, class HGSaneDeviceImpl** deviceImpl); private: HGResult FindFunctions(); @@ -49,7 +50,6 @@ private: private: HGDll m_dll; - HGDll m_saneDlgDll; f_sane_init m_f_sane_init; f_sane_exit m_f_sane_exit; f_sane_get_devices m_f_sane_get_devices; @@ -63,7 +63,12 @@ private: f_sane_get_parameters m_f_sane_get_parameters; f_sane_get_option_descriptor m_f_sane_get_option_descriptor; f_sane_control_option m_f_sane_control_option; + + HGDll m_saneDlgDll; + f_show_devlist_ui m_f_show_devlist_ui; f_show_setting_ui m_f_show_setting_ui; + f_show_scan_ui m_f_show_scan_ui; + std::list m_listDeviceImpl; }; @@ -73,20 +78,19 @@ public: HGSaneDeviceImpl(HGSaneManagerImpl* mgrImpl); ~HGSaneDeviceImpl(); + HGResult Init(SANE_Handle handle); HGResult Open(const HGChar *devName, HGChar* errInfo, HGUInt errInfoLen); HGResult Close(); -#ifdef HG_CMP_MSC - HGResult ShowSettingDlg(HWND parent); -#else - HGResult ShowSettingDlg(class QWidget *parent); -#endif + HGResult ShowSettingDlg(HGWindow parent); HGResult Start(HGSane_DeviceEventFunc eventFunc, HGPointer eventParam, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam, HGChar* errInfo, HGUInt errInfoLen); HGResult Stop(); + HGResult StartWithUI(HGWindow parent, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam); private: HGUInt GetDpi(); static void HGAPI ThreadFunc(HGThread thread, HGPointer param); + static void ShowScanImageCallback(const SANE_Parameters* imageFormat, const SANE_Byte* imageData, void* callbackParam); private: HGSaneManagerImpl* m_mgrImpl;