From 383fc01ed08803c07081354071dac624afd6e327 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Thu, 25 May 2023 15:37:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E9=97=AD=E8=AE=BE=E7=BD=AE=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E5=90=8E=EF=BC=8C=E5=90=AF=E5=8A=A8=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=BB=B6=E6=97=B61s=EF=BC=8C=E5=A6=82?= =?UTF-8?q?=E6=9E=9CeventProcess=E6=B2=A1=E6=9C=89=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E5=88=99=E5=86=8D=E8=B0=83=E7=94=A8notifyClose;=20?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=B7=BB=E5=8A=A0=E8=BF=9B=E7=A8=8BPID?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sane/scanner.cpp | 2 +- twain/twain/huagaods.cpp | 110 +++++++++++++++++++-------------------- twain/twain/huagaods.hpp | 10 +++- 3 files changed, 63 insertions(+), 59 deletions(-) diff --git a/sane/scanner.cpp b/sane/scanner.cpp index b36aa38..179f285 100644 --- a/sane/scanner.cpp +++ b/sane/scanner.cpp @@ -3408,7 +3408,7 @@ void init_log(void) hg_get_current_time_w(now); g_log_level = get_log_level(g_path_file_.c_str()); - swprintf_s(ts, _countof(ts) - 1, L"==================%s - %d==================\r\n", now, g_log_level); + swprintf_s(ts, _countof(ts) - 1, L"==================%s - %d, Proc %d==================\r\n", now, g_log_level, GetCurrentProcessId()); fwrite(ts, 2, lstrlenW(ts), g_file_); } diff --git a/twain/twain/huagaods.cpp b/twain/twain/huagaods.cpp index 11f7053..a957529 100644 --- a/twain/twain/huagaods.cpp +++ b/twain/twain/huagaods.cpp @@ -776,7 +776,7 @@ static const SCANNERID scanner_guid = MAKE_SCANNER_ID(PRODUCT_PID, PRODUCT_VID); static std::once_flag oc; huagao_ds::huagao_ds() : cur_head_(NULL), dpi_(200), xfer_ready_failed_(false), log_all_triple_(false), scanner_status_(SCANNER_STATUS_NOT_INIT) - ,count_(-1), bUiOnly_(false), is_getting_count_(false), show_setting_(false) + ,count_(-1), bUiOnly_(false), show_setting_(false) { //std::call_once(oc, [&]() { log4cplus::Initializer(); }); } @@ -923,12 +923,11 @@ Result huagao_ds::eventProcess(const Identity&, Event& event) notifyXferReady(); xfer_ready_ = false; } - else*/ if (notfify_close_) + else*/ if (take_and_reset_notify_close_flag()) { notifyCloseCancel(); - notfify_close_ = false; } - has_event_called_ = true; + event.setMessage(Msg::Null); @@ -951,7 +950,6 @@ Result huagao_ds::identityOpenDs(const Identity& id) // // return { ReturnCode::Failure, ConditionCode::CapBadOperation }; //} - is_getting_count_ = false; if (!load_sane_util::is_ok()) load_sane_util::initialize(me_); if (!load_sane_util::is_ok()) @@ -987,13 +985,13 @@ Result huagao_ds::identityOpenDs(const Identity& id) err = 0; scanner_.reset(load_sane_util::open(scanner_guid, &err, attempt >= max_try)); } + if(attempt > 1) + load_sane_util::to_log(2, L"OpenDS: wait and try %d times.\r\n", attempt); if (!scanner_.get()) { //CloseHandle(singleton_); //singleton_ = NULL; - wchar_t info[128] = { 0 }; - swprintf_s(info, _countof(info) - 1, L"OpenDS error: %d\r\n", err); - load_sane_util::log_info(info, 2); + load_sane_util::to_log(3, L"OpenDS(%04x) error: %d\r\n", PRODUCT_PID, err); load_sane_util::uninitialize(); return { ReturnCode::Failure, huagao_ds::condition_code_from_hg_error(err) }; @@ -1006,6 +1004,7 @@ Result huagao_ds::identityOpenDs(const Identity& id) memset(cur_head_, 0, sizeof(SANE_Parameters)); } log_all_triple_ = get_config_number(L"twain-app", L"log-all-triple") == 1; + notify_close_ = get_config_number(L"twain-app", L"notify-close") == 1; m_compression = Compression::None; init_support_caps(); @@ -1016,6 +1015,9 @@ Result huagao_ds::identityOpenDs(const Identity& id) } Result huagao_ds::identityCloseDs(const Identity&) { + if (notify_close_thread_.get() && notify_close_thread_->joinable()) + notify_close_thread_->join(); + // ui_.reset(); if (scanner_.get()) { @@ -1116,7 +1118,6 @@ Result huagao_ds::userInterfaceEnable(const Identity&, UserInterface& ui) notfify_close_ = false; bUiOnly_ = false; show_setting_ = false; - has_event_called_ = false; if (!ui.showUi()) { scanner_->ui_show_progress((HWND)ui.parent().raw(), m_bIndicator); @@ -1596,47 +1597,6 @@ Twpp::Result huagao_ds::setupFileXferReset(const Twpp::Identity& origin, Twpp::S return badProtocol(); } Result huagao_ds::call(const Identity& origin, DataGroup dg, Dat dat, Msg msg, void* data) { - static bool in_calling = false; - static unsigned nested_events = 0; - - bool calling = in_calling; - //if (calling) - //{ - // wchar_t buf[128] = { 0 }, dgs[20] = { 0 }, dts[20] = { 0 }, ms[20] = { 0 }, ss[20] = { 0 }, rcs[20] = { 0 }, cs[20] = { 0 }; - // - // if (dg == DataGroup::Control && dat == Dat::Event && msg == Msg::ProcessEvent) - // { - // if (is_getting_count_ && scanner_status_ == SCANNER_STATUS_SCAN_1) - // { - // // let it go ... - // } - // else - // { - // if(nested_events == 0) - // load_sane_util::log_info(L"Nested Call: discard 'Msg::ProcessEvent'!\r\n", 0); - // nested_events++; - // return { ReturnCode::NotDsEvent, ConditionCode::Success }; - // } - // } - // else - // { - // swprintf_s(buf, _countof(buf) - 1, L"Nested Call: [%x - %s]DSEntry(%s, %s, %s) called in another operation! \r\n", GetCurrentThreadId(), desc_state(state(), ss), - // desc_data_group(dg, dgs), desc_data(dat, dts), desc_msg(msg, ms)); - // load_sane_util::log_info(buf, 0); - // } - //} - //else - //{ - // in_calling = true; - //} - //if (nested_events > 1) - //{ - // wchar_t buf[256] = { 0 }; - // swprintf_s(buf, _countof(buf) - 1, L"Discard %u nested Msg::ProcessEvent messages before.\r\n", nested_events); - // load_sane_util::log_info(buf, 0); - //} - nested_events = 0; - try { // we can override almost anything from SourceFromThis, even the top-most source instance call //FileTools::write_log("D:\\1.txt", "call:datagroup-"+to_string((int)dg)+"dat-"+to_string(int(dat))+"msg-"+to_string(int(msg))); @@ -1684,13 +1644,11 @@ Result huagao_ds::call(const Identity& origin, DataGroup dg, Dat dat, Msg msg, v } } - in_calling = calling; return rt; } catch (const CapabilityException& e) { //FileTools::writelog(log_ERROR, e.what()); UNREFERENCED_PARAMETER(e); - in_calling = calling; return badValue(); } } @@ -3635,10 +3593,28 @@ int huagao_ds::handle_scanner_event(int ev, bool from_event_proc) break; case SANE_EVENT_UI_CLOSE_SETTING: scanner_->ui_hide(); - if(has_event_called_) + if (notify_close_ == NOTIFY_AUTO) + { + load_sane_util::to_log(1, L"close setting ui and notify close in auto mode.\r\n"); + //if (has_event_called_) + // notfify_close_ = true; + //else + // rc = notifyCloseCancel(); notfify_close_ = true; - else + if (notify_close_thread_.get() && notify_close_thread_->joinable()) + notify_close_thread_->join(); + notify_close_thread_.reset(new std::thread(&huagao_ds::notify_close_thread, this)); + } + else if (notify_close_ == NOTIFY_ALWAYS) + { + load_sane_util::to_log(1, L"close setting ui and notify close immediately.\r\n"); rc = notifyCloseCancel(); + } + else if (notify_close_ == NOTIFY_NONE) + { + load_sane_util::to_log(1, L"close setting ui and notify close passively (wait eventProcess to do).\r\n"); + notfify_close_ = true; + } //if(main_thread_id_ == GetCurrentThreadId()) // rc = notifyCloseCancel(); //else @@ -3717,7 +3693,6 @@ int huagao_ds::handle_scanner_event(int ev, bool from_event_proc) } int huagao_ds::get_scanned_image_count(DWORD timeout) { - is_getting_count_ = true; int cnt = scanner_->get_scanned_images(timeout); //if (cnt == -1) @@ -3727,7 +3702,6 @@ int huagao_ds::get_scanned_image_count(DWORD timeout) // scanner_->ui_hide(); // notifyCloseCancel(); //} - is_getting_count_ = false; return cnt; } @@ -3789,3 +3763,27 @@ void huagao_ds::trigger_ProcessEvent(Twpp::DataGroup dg, Twpp::Dat dat, Twpp::Ms } } +bool huagao_ds::take_and_reset_notify_close_flag(void) +{ + std::lock_guard lock(notify_close_lock_); + bool notify = notfify_close_; + + if (notfify_close_) + notfify_close_ = false; + + return notify; +} +void huagao_ds::notify_close_thread(void) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + if (take_and_reset_notify_close_flag()) + { + notifyCloseCancel(); + load_sane_util::to_log(1, L"Trigger notifyCloseCancel in custom thread!\r\n"); + } + else + { + load_sane_util::to_log(1, L"Good! Trigger notifyCloseCancel in eventProcess ^_^\r\n"); + } +} diff --git a/twain/twain/huagaods.hpp b/twain/twain/huagaods.hpp index 702b19f..cf83b6b 100644 --- a/twain/twain/huagaods.hpp +++ b/twain/twain/huagaods.hpp @@ -57,9 +57,15 @@ class huagao_ds : public Twpp::SourceFromThis { bool bUiOnly_; bool show_setting_; int count_; - volatile bool is_getting_count_; + volatile bool notfify_close_ = false; - volatile bool has_event_called_ = false; + enum {NOTIFY_AUTO = 0, NOTIFY_ALWAYS, NOTIFY_NONE}; + volatile unsigned notify_close_ = 0; // 0 - auto; 1 - notify always; 2 - no notify + std::mutex notify_close_lock_; + std::unique_ptr notify_close_thread_; + bool take_and_reset_notify_close_flag(void); + void notify_close_thread(void); + static std::string get_hidedlg_path(void); static void showmsg(const char* msg, int err); static int __stdcall on_scanner_event(int ev, void* param);