diff --git a/hgdriver/hgdev/hg_scanner.cpp b/hgdriver/hgdev/hg_scanner.cpp index 0364c6c..a6a7a9d 100644 --- a/hgdriver/hgdev/hg_scanner.cpp +++ b/hgdriver/hgdev/hg_scanner.cpp @@ -131,10 +131,10 @@ hg_scanner::hg_scanner(ONLNSCANNER* dev, imgproc_mgr* imgproc, hguser* user, std init(); if (status() == SCANNER_ERR_OK) { - imgproc_ = imgproc; - imgproc_->add_ref(); + //imgproc_ = imgproc; + //imgproc_->add_ref(); + //dev_opts_->add(imgproc_); - dev_opts_->add(imgproc_); if (constopts) { for(auto& v: *constopts) @@ -162,6 +162,9 @@ shared_memory* hg_scanner::create_device_singleton(int vid, int pid, int addr) void hg_scanner::init(void) { + std::string opts(""); + int ret = SCANNER_ERR_OK; + auto on_status = [this](uint32_t statu) ->void { //status_ = statu; @@ -189,18 +192,20 @@ void hg_scanner::init(void) scanner_ = new scanner_handler(); scanner_->set_image_receiver(on_image_received); scanner_->set_status_notifyer(on_status); - - std::string opts(""); - int ret = scanner_->option_get_all(opts); + ret = scanner_->open_usb_scanner(dev_.dev); if (ret == SCANNER_ERR_OK) { + ret = scanner_->option_get_all(opts); set_opt_json_text(&opts[0]); if(user_) dev_opts_ = new device_option(privilege, logmsg); else dev_opts_ = new device_option(privilege_empty, logmsg); dev_opts_->add(this); + + status_ = SCANNER_ERR_OK; + msg_ = "OK"; } else { @@ -212,11 +217,11 @@ void hg_scanner::thread_image_processor(void) imgproc_mgr * processor = imgproc_; img_receiver* raw = nullptr; - processor->add_ref(); + //processor->add_ref(); while (raw_imgs_.take(raw, true)) { } - processor->release(); + //processor->release(); } int hg_scanner::start(void) @@ -266,6 +271,7 @@ int hg_scanner::close(void) } // wait image thread ... + raw_imgs_.trigger(); if (imgpr_thread_.get() && imgpr_thread_->joinable()) imgpr_thread_->join(); diff --git a/hgdriver/hgdev/scanner/async_usb_host.cpp b/hgdriver/hgdev/scanner/async_usb_host.cpp index bab7eeb..2ff715f 100644 --- a/hgdriver/hgdev/scanner/async_usb_host.cpp +++ b/hgdriver/hgdev/scanner/async_usb_host.cpp @@ -364,6 +364,9 @@ void async_usb_host::stop_worker_threads(void) if(t.get() && t->joinable()) \ t->join(); + out_que_.trigger(); + in_que_.trigger(); + WAIT_THREAD(thread_w_); WAIT_THREAD(thread_r_); WAIT_THREAD(thread_p_); diff --git a/hgdriver/hgdev/scanner_manager.cpp b/hgdriver/hgdev/scanner_manager.cpp index 2b17955..2893e6a 100644 --- a/hgdriver/hgdev/scanner_manager.cpp +++ b/hgdriver/hgdev/scanner_manager.cpp @@ -290,7 +290,7 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v char model[40], vendor[40]; SANE_Device_Ex de; - int ev_ui = 0, addr = 0; + int ev_ui = 0; scanner_handle h = NULL; unsigned int len = sizeof(de); std::string name(""), type(""); @@ -303,7 +303,6 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v de.family = NULL; de.vendor = vendor; de.openned = SANE_FALSE; - addr = libusb_get_device_address(device); if (ev == USB_EVENT_DEVICE_ARRIVED) { @@ -367,7 +366,7 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v ols.vid = vid; ols.pid = pid; - ols.addr = addr; + ols.addr = libusb_get_device_address(device); ols.dev = device; ols.scanner = nullptr; ols.family = g_supporting_devices[index].family; diff --git a/hgdriver/wrapper/huagaoxxx_warraper_ex.cpp b/hgdriver/wrapper/huagaoxxx_warraper_ex.cpp index 4e6247d..2ad5a76 100644 --- a/hgdriver/wrapper/huagaoxxx_warraper_ex.cpp +++ b/hgdriver/wrapper/huagaoxxx_warraper_ex.cpp @@ -438,10 +438,9 @@ extern "C" #if defined(WIN32) || defined(_WIN64) + BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { - //static _CrtMemState state; - if (reason == DLL_PROCESS_ATTACH) { //if (g_scanner_path.empty()) @@ -457,6 +456,13 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) //} #ifdef _DEBUG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); + _CrtSetBreakAlloc(-1); +#endif + } + else if (reason == DLL_PROCESS_DETACH) + { +#ifdef _DEBUG + OutputDebugStringA("\r\nscanner module unloading ...\r\n"); #endif } diff --git a/hgsane/entry.cpp b/hgsane/entry.cpp index 34f846c..59bb27d 100644 --- a/hgsane/entry.cpp +++ b/hgsane/entry.cpp @@ -185,14 +185,13 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) //_CrtMemCheckpoint(&state); #ifdef _DEBUG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); - _CrtSetBreakAlloc(632555577); - set_gbjson_life_callback(record_json, nullptr); + _CrtSetBreakAlloc(-1); #endif } else if (reason == DLL_PROCESS_DETACH) { #ifdef _DEBUG - _CrtDumpMemoryLeaks(); + OutputDebugStringA("\r\nsane module unloading ...\r\n"); #endif //_CrtMemDumpAllObjectsSince(&state); } diff --git a/sdk/base/utils.cpp b/sdk/base/utils.cpp index 3f8df4b..32202f4 100644 --- a/sdk/base/utils.cpp +++ b/sdk/base/utils.cpp @@ -54,6 +54,7 @@ int gettimeofday(TIMEV* tv, struct timezone* tz) return 0; } + #else #include @@ -1128,7 +1129,27 @@ namespace utils return 0; } +#if OS_WIN + bool run_get_message(HWND hwnd, UINT filter_min, UINT filter_max, std::function msghandler) + { + MSG msg = { 0 }; + BOOL ret = GetMessageW(&msg, hwnd, filter_min, filter_max); + bool handled = false; + if ((DWORD)ret == -1 || !ret) + return false; + + if (msghandler) + ret = msghandler(&msg, &handled) == true; + if (!handled) + { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + + return ret == TRUE; + } +#endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/sdk/base/utils.h b/sdk/base/utils.h index 7532dfb..98bee20 100644 --- a/sdk/base/utils.h +++ b/sdk/base/utils.h @@ -13,6 +13,7 @@ #include #include #include +#include enum log_type { @@ -66,6 +67,23 @@ namespace utils int copy_log_file_to(const char* dst); int clear_log_file(void); +#if OS_WIN + // Function: pump message recycle (GetMessageW) + // + // Parameters: hwnd - target window + // + // filter_min - the minimal message + // + // filter_max - the maximal message + // + // msghandler - custom message processor + // MSG*, received message by GetMessage + // bool*, return whether user handled the message, no TranslateMessage and DispatchMessage if this was true + // + // Return: whether quit the message recycle + bool run_get_message(HWND hwnd, UINT filter_min = 0, UINT filter_max = 0, std::function msghandler = std::function()); +#endif + template void to_log(int level, const char* fmt, Args ... args) { @@ -141,6 +159,7 @@ struct timezone int tz_dsttime; /* Nonzero if DST is ever in effect. */ }; int gettimeofday(TIMEV* tv, struct timezone* tz); + #else #include diff --git a/sdk/json/gb_json.cpp b/sdk/json/gb_json.cpp index 6ebe5b9..24a73f0 100644 --- a/sdk/json/gb_json.cpp +++ b/sdk/json/gb_json.cpp @@ -276,6 +276,30 @@ int32_t gb_json::release() return ref; } +static void check_cJSON(cJSON* root, std::vector* existing) +{ + if (!root) + return; + + for (auto& v : *existing) + { + if (v == root) + { + printf("cJSON* 0x%08x is repeat!\n", v); + break; + } + } + + existing->push_back(root); + check_cJSON(root->child, existing); + + cJSON* next = root->next; + while (next) + { + check_cJSON(next, existing); + next = next->next; + } +} bool gb_json::attach_text(char* json_txt) { clear(); @@ -283,10 +307,21 @@ bool gb_json::attach_text(char* json_txt) cJSON* jsn = cJSON_Parse(json_txt); if (jsn) { - //char *text = cJSON_Print(jsn); - //if (text) - // free(text); + char *text = cJSON_Print(jsn); + if (text) + { + if (0) + { + FILE* dst = fopen("e:\\test-json.txt", "wb"); + fwrite(text, 1, strlen(text), dst); + fclose(dst); + } + free(text); + } + std::vector repeat; + if(0) + check_cJSON(jsn, &repeat); from_cjson(jsn->child); cJSON_Delete(jsn); diff --git a/twain/ds/scanner.cpp b/twain/ds/scanner.cpp index 2457764..f72f022 100644 --- a/twain/ds/scanner.cpp +++ b/twain/ds/scanner.cpp @@ -902,6 +902,8 @@ COM_API_IMPLEMENT(scanner, int, start(void)) thread_starting_->join(); #ifdef START_SCAN_IN_THREAD thread_starting_.reset(new std::thread(&scanner::thread_start, this)); + get_scanned_images(-1); // block until image arrived or scan finished + ret = err_; #else ret = thread_start(); #endif