From a8718596c2eb71dcb46f845d44146b37e23883c8 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Tue, 23 May 2023 09:13:55 +0800 Subject: [PATCH] move notifyCloseCancel invoking to eventProcess - we trust that the eventProcess always called when show setting UI --- twain/load_sane.h | 4 ++-- twain/twain/huagaods.cpp | 41 ++++++++++++++++++++++++++++++++++++++-- twain/twain/huagaods.hpp | 1 + 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/twain/load_sane.h b/twain/load_sane.h index cd3c9be..276842e 100644 --- a/twain/load_sane.h +++ b/twain/load_sane.h @@ -22,13 +22,13 @@ namespace load_sane_util int move_file(const wchar_t* from, const wchar_t* to); template - void to_log(const wchar_t* fmt, Args ... args) + void to_log(int level, const wchar_t* fmt, Args ... args) { size_t size = swprintf(nullptr, 0, fmt, args ...) + 2; std::unique_ptr buf(new wchar_t[size]); swprintf(buf.get(), size, fmt, args ...); - log_info(buf.get(), 0); + log_info(buf.get(), level); } }; diff --git a/twain/twain/huagaods.cpp b/twain/twain/huagaods.cpp index 24a9406..34f91df 100644 --- a/twain/twain/huagaods.cpp +++ b/twain/twain/huagaods.cpp @@ -918,6 +918,16 @@ Result huagao_ds::eventProcess(const Identity&, Event& event) // if(ev) // handle_scanner_event(ev); //} + /*if (xfer_ready_) + { + notifyXferReady(); + xfer_ready_ = false; + } + else*/ if (notfify_close_) + { + notifyCloseCancel(); + notfify_close_ = false; + } event.setMessage(Msg::Null); @@ -1102,6 +1112,7 @@ Result huagao_ds::userInterfaceDisable(const Identity&, UserInterface& ui) } Result huagao_ds::userInterfaceEnable(const Identity&, UserInterface& ui) { + notfify_close_ = false; bUiOnly_ = false; show_setting_ = false; if (!ui.showUi()) @@ -1377,8 +1388,10 @@ Result huagao_ds::imageNativeXferGet(const Identity& id, ImageNativeXfer& data) { if (!scanner_.get()) return seqError(); - else if(get_scanned_image_count(-1) == 0) + else if (get_scanned_image_count(-1) == 0) + { return { ReturnCode::Failure, condition_code_from_hg_error(scanner_->last_error()) }; + } IScanImg* img = scanner_->take_first_image(TWAIN_XFER_Native); @@ -1638,21 +1651,29 @@ Result huagao_ds::call(const Identity& origin, DataGroup dg, Dat dat, Msg msg, v load_sane_util::log_info(buf, 7); if ((int)rt.returnCode() && rt.returnCode() != ReturnCode::NotDsEvent) { + bool changed = false; if (dat == Dat::PendingXfers && Msg::Reset == msg && scanner_status_ == SCANNER_STATUS_STOPPED) // PurvarScannerForHomework.exe - move modifying from EndXfer to xfer::Reset + { + changed = true; rt = success(); + } else if (rt.status() == ConditionCode::SeqError) { if (dat == Dat::ImageInfo && Msg::Get == msg && scanner_status_ == SCANNER_STATUS_STOPPED) // for demo.exe { + changed = true; rt = { ReturnCode::Success, ConditionCode::Bummer }; } else if (dat == Dat::PendingXfers && Msg::EndXfer == msg && scanner_status_ == SCANNER_STATUS_STOPPED) // for photoshop.exe { // PendingXfers& data (static_cast(data))->setCount(0); + changed = true; rt = { ReturnCode::Success, ConditionCode::Success }; } } + if(changed) + load_sane_util::to_log(7, L" modify result to {%s, %s}.\r\n", desc_return_code(rt, rcs), desc_condition_code((ConditionCode)(Status)rt, cs)); } } @@ -3607,7 +3628,23 @@ int huagao_ds::handle_scanner_event(int ev, bool from_event_proc) break; case SANE_EVENT_UI_CLOSE_SETTING: scanner_->ui_hide(); - rc = notifyCloseCancel(); + notfify_close_ = true; + //if(main_thread_id_ == GetCurrentThreadId()) + //rc = notifyCloseCancel(); + //else + //{ + // HANDLE thrd = OpenThread(THREAD_ALL_ACCESS, FALSE, main_thread_id_); + // int err = -1; + // if (thrd) + // { + // if (QueueUserAPC(&huagao_ds::notify_close, thrd, (ULONG_PTR)this)) + // err = 0; + // else + // err = GetLastError(); + // CloseHandle(thrd); + // } + // load_sane_util::to_log(3, L"Notify close setting UI in different thread(%x), invoke 'notifyCloseCancel' in enable-thread %x(%x), result %d\r\n", GetCurrentThreadId(), main_thread_id_, thrd, err); + //} //if (!Twpp::success(rc)) //{ // rc = notifyXferReady(); // 好分数需要再通知 FAINT :( - modified on 2022-10-20 diff --git a/twain/twain/huagaods.hpp b/twain/twain/huagaods.hpp index 97363f7..0306c8c 100644 --- a/twain/twain/huagaods.hpp +++ b/twain/twain/huagaods.hpp @@ -58,6 +58,7 @@ class huagao_ds : public Twpp::SourceFromThis { bool show_setting_; int count_; volatile bool is_getting_count_; + volatile bool notfify_close_ = false; 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);