From ff30f71c13b5207a8d00ae39f30ae6d3f8c3c55e Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Tue, 26 Nov 2013 23:00:33 +0100 Subject: [PATCH] Refactored finalization code, so each "Global..." file can have its own finalization function. --- LameXP_VS2013.vcxproj | 2 +- LameXP_VS2013.vcxproj.filters | 6 +- src/Global.h | 11 +- src/Global_IPC.cpp | 31 +++-- src/Global_Tools.cpp | 73 +++++------ src/Global_Utils.cpp | 41 ++++-- src/Global_Version.cpp | 9 ++ src/Global_Win32.cpp | 171 ++++++++++++------------- src/{Global_EntryPoint.cpp => Global_Zero.cpp} | 61 ++++++--- 9 files changed, 230 insertions(+), 175 deletions(-) rename src/{Global_EntryPoint.cpp => Global_Zero.cpp} (65%) diff --git a/LameXP_VS2013.vcxproj b/LameXP_VS2013.vcxproj index c992b772..8d44c85a 100644 --- a/LameXP_VS2013.vcxproj +++ b/LameXP_VS2013.vcxproj @@ -327,7 +327,7 @@ del "$(TargetDir)imageformats\q???d4.dll" - + diff --git a/LameXP_VS2013.vcxproj.filters b/LameXP_VS2013.vcxproj.filters index 5377dab5..9e68adcc 100644 --- a/LameXP_VS2013.vcxproj.filters +++ b/LameXP_VS2013.vcxproj.filters @@ -406,9 +406,6 @@ Source Files - - Source Files - Source Files @@ -421,6 +418,9 @@ Generated Files\MOC + + Source Files + diff --git a/src/Global.h b/src/Global.h index 8aa8dbfc..06de9726 100644 --- a/src/Global.h +++ b/src/Global.h @@ -135,6 +135,9 @@ typedef enum } lamexp_syscolor_t; +//Icon type +class lamexp_icon_t; + /////////////////////////////////////////////////////////////////////////////// // GLOBAL FUNCTIONS /////////////////////////////////////////////////////////////////////////////// @@ -156,8 +159,6 @@ bool lamexp_check_sysmenu_msg(void *message, const unsigned int identifier); bool lamexp_check_tool(const QString &toolName); const QString lamexp_clean_filename(const QString &str); const QString lamexp_clean_filepath(const QString &str); -void lamexp_clean_all_tools(void); -void lamexp_clean_all_translations(void); bool lamexp_clean_folder(const QString &folderPath); QDate lamexp_current_date_safe(void); unsigned __int64 lamexp_current_file_time(void); @@ -171,6 +172,7 @@ bool lamexp_exec_shell(const QWidget *win, const QString &url, const QString &pa void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage = NULL); void lamexp_finalization(void); unsigned __int64 lamexp_free_diskspace(const QString &path, bool *ok = NULL); +void lamexp_free_window_icon(lamexp_icon_t *icon); const lamexp_os_version_t &lamexp_get_os_version(void); void lamexp_init_console(const QStringList &argv); void lamexp_init_error_handlers(void); @@ -180,7 +182,6 @@ bool lamexp_init_qt(int argc, char* argv[]); bool lamexp_install_translator(const QString &language); bool lamexp_install_translator_from_file(const QString &qmFile); void lamexp_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t); -void lamexp_ipc_exit(void); void lamexp_ipc_read(unsigned int *command, char* message, size_t buffSize); void lamexp_ipc_send(unsigned int command, const char* message); bool lamexp_is_hibernation_supported(void); @@ -204,15 +205,15 @@ QString lamexp_rand_str(const bool bLong = false); void lamexp_register_tool(const QString &toolName, LockedFile *file, unsigned int version = 0, const QString *tag = NULL); bool lamexp_remove_file(const QString &filename); void lamexp_seed_rand(void); -bool lamexp_set_window_icon(QWidget *window, const QIcon &icon, const bool bIsBigIcon); +lamexp_icon_t *lamexp_set_window_icon(QWidget *window, const QIcon &icon, const bool bIsBigIcon); bool lamexp_sheet_of_glass(QWidget *window); bool lamexp_sheet_of_glass_update(QWidget *window); bool lamexp_shutdown_computer(const QString &message, const unsigned long timeout = 30, const bool forceShutdown = true, const bool hibernate = false); void lamexp_sleep(const unsigned int delay); QColor lamexp_system_color(const int color_id); +int lamexp_system_message(const wchar_t *text, int beepType); const char *lamexp_support_url(void); const QString &lamexp_temp_folder2(void); -void lamexp_temp_folder_clear(void); bool lamexp_themes_enabled(void); unsigned int lamexp_tool_version(const QString &toolName, QString *tag = NULL); unsigned int lamexp_toolver_coreaudio(void); diff --git a/src/Global_IPC.cpp b/src/Global_IPC.cpp index 5a5737d1..4b130329 100644 --- a/src/Global_IPC.cpp +++ b/src/Global_IPC.cpp @@ -255,19 +255,6 @@ void lamexp_ipc_read(unsigned int *command, char* message, size_t buffSize) } } -/* - * Exit and clean-up IPC - */ -void lamexp_ipc_exit(void) -{ - if(g_lamexp_ipc_ptr.sharedmem) g_lamexp_ipc_ptr.sharedmem->detach(); - LAMEXP_DELETE(g_lamexp_ipc_ptr.sharedmem); - LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read); - LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write); - LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read_mutex); - LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write_mutex); -} - /////////////////////////////////////////////////////////////////////////////// // INITIALIZATION /////////////////////////////////////////////////////////////////////////////// @@ -276,3 +263,21 @@ extern "C" void _lamexp_global_init_ipcom(void) { LAMEXP_ZERO_MEMORY(g_lamexp_ipc_ptr); } + +/////////////////////////////////////////////////////////////////////////////// +// FINALIZATION +/////////////////////////////////////////////////////////////////////////////// + +extern "C" void _lamexp_global_free_ipcom(void) +{ + if(g_lamexp_ipc_ptr.sharedmem) + { + g_lamexp_ipc_ptr.sharedmem->detach(); + } + + LAMEXP_DELETE(g_lamexp_ipc_ptr.sharedmem); + LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read); + LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write); + LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_read_mutex); + LAMEXP_DELETE(g_lamexp_ipc_ptr.semaphore_write_mutex); +} diff --git a/src/Global_Tools.cpp b/src/Global_Tools.cpp index 0c9f5dd6..0b4dee71 100644 --- a/src/Global_Tools.cpp +++ b/src/Global_Tools.cpp @@ -193,29 +193,6 @@ const QString lamexp_version2string(const QString &pattern, unsigned int version } /* - * Free all registered tools (final clean-up) - */ -void lamexp_clean_all_tools(void) -{ - if(g_lamexp_tools.registry) - { - QStringList keys = g_lamexp_tools.registry->keys(); - for(int i = 0; i < keys.count(); i++) - { - LockedFile *lf = g_lamexp_tools.registry->take(keys.at(i)); - LAMEXP_DELETE(lf); - } - g_lamexp_tools.registry->clear(); - g_lamexp_tools.versions->clear(); - g_lamexp_tools.tags->clear(); - } - - LAMEXP_DELETE(g_lamexp_tools.registry); - LAMEXP_DELETE(g_lamexp_tools.versions); - LAMEXP_DELETE(g_lamexp_tools.tags); -} - -/* * Initialize translations and add default language */ bool lamexp_translation_init(void) @@ -364,35 +341,49 @@ bool lamexp_install_translator_from_file(const QString &qmFile) return success; } -/* - * Free all registered translations (final clean-up) - */ -void lamexp_clean_all_translations(void) +/////////////////////////////////////////////////////////////////////////////// +// INITIALIZATION +/////////////////////////////////////////////////////////////////////////////// + +extern "C" void _lamexp_global_init_tools(void) { - QWriteLocker writeLockTranslator(&g_lamexp_currentTranslator.lock); + LAMEXP_ZERO_MEMORY(g_lamexp_tools); + LAMEXP_ZERO_MEMORY(g_lamexp_currentTranslator); + LAMEXP_ZERO_MEMORY(g_lamexp_translation); +} +/////////////////////////////////////////////////////////////////////////////// +// FINALIZATION +/////////////////////////////////////////////////////////////////////////////// + +extern "C" void _lamexp_global_free_tools(void) +{ + //Free *all* registered translations if(g_lamexp_currentTranslator.instance) { QApplication::removeTranslator(g_lamexp_currentTranslator.instance); LAMEXP_DELETE(g_lamexp_currentTranslator.instance); } - - writeLockTranslator.unlock(); - QWriteLocker writeLockTranslations(&g_lamexp_translation.lock); - LAMEXP_DELETE(g_lamexp_translation.files); LAMEXP_DELETE(g_lamexp_translation.names); LAMEXP_DELETE(g_lamexp_translation.cntry); LAMEXP_DELETE(g_lamexp_translation.sysid); -} -/////////////////////////////////////////////////////////////////////////////// -// INITIALIZATION -/////////////////////////////////////////////////////////////////////////////// + //Free *all* registered tools + if(g_lamexp_tools.registry) + { + QStringList keys = g_lamexp_tools.registry->keys(); + for(int i = 0; i < keys.count(); i++) + { + LockedFile *lf = g_lamexp_tools.registry->take(keys.at(i)); + LAMEXP_DELETE(lf); + } + g_lamexp_tools.registry->clear(); + g_lamexp_tools.versions->clear(); + g_lamexp_tools.tags->clear(); + } + LAMEXP_DELETE(g_lamexp_tools.registry); + LAMEXP_DELETE(g_lamexp_tools.versions); + LAMEXP_DELETE(g_lamexp_tools.tags); -extern "C" void _lamexp_global_init_tools(void) -{ - LAMEXP_ZERO_MEMORY(g_lamexp_tools); - LAMEXP_ZERO_MEMORY(g_lamexp_currentTranslator); - LAMEXP_ZERO_MEMORY(g_lamexp_translation); } diff --git a/src/Global_Utils.cpp b/src/Global_Utils.cpp index 06f1cdd0..f5e018f7 100644 --- a/src/Global_Utils.cpp +++ b/src/Global_Utils.cpp @@ -188,15 +188,6 @@ const QString &lamexp_temp_folder2(void) } /* - * Clear LameXP temp folder cache - */ -void lamexp_temp_folder_clear(void) -{ - QWriteLocker writeLock(&g_lamexp_temp_folder.lock); - LAMEXP_DELETE(g_lamexp_temp_folder.path); -} - -/* * Setup QPorcess object */ void lamexp_init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir) @@ -614,3 +605,35 @@ extern "C" void _lamexp_global_init_utils(void) LAMEXP_ZERO_MEMORY(g_lamexp_temp_folder); LAMEXP_ZERO_MEMORY(g_lamexp_app_icon); } + +/////////////////////////////////////////////////////////////////////////////// +// FINALIZATION +/////////////////////////////////////////////////////////////////////////////// + +extern "C" void _lamexp_global_free_utils(void) +{ + //Delete temporary files + const QString &tempFolder = lamexp_temp_folder2(); + if(!tempFolder.isEmpty()) + { + bool success = false; + for(int i = 0; i < 100; i++) + { + if(lamexp_clean_folder(tempFolder)) + { + success = true; + break; + } + lamexp_sleep(100); + } + if(!success) + { + lamexp_system_message(L"Sorry, LameXP was unable to clean up all temporary files. Some residual files in your TEMP directory may require manual deletion!", lamexp_beep_warning); + lamexp_exec_shell(NULL, tempFolder, QString(), QString(), true); + } + } + + //Free memory + LAMEXP_DELETE(g_lamexp_temp_folder.path); + LAMEXP_DELETE(g_lamexp_app_icon.appIcon); +} diff --git a/src/Global_Version.cpp b/src/Global_Version.cpp index f5aeaf88..24eeadf3 100644 --- a/src/Global_Version.cpp +++ b/src/Global_Version.cpp @@ -325,3 +325,12 @@ extern "C" void _lamexp_global_init_versn(void) { LAMEXP_ZERO_MEMORY(g_lamexp_portable); } + +/////////////////////////////////////////////////////////////////////////////// +// FINALIZATION +/////////////////////////////////////////////////////////////////////////////// + +extern "C" void _lamexp_global_free_versn(void) +{ + /*nothing to do here*/ +} diff --git a/src/Global_Win32.cpp b/src/Global_Win32.cpp index c5654754..bfd20ba4 100644 --- a/src/Global_Win32.cpp +++ b/src/Global_Win32.cpp @@ -1492,6 +1492,23 @@ QDate lamexp_current_date_safe(void) } /* + * Show system message box + */ +int lamexp_system_message(const wchar_t *text, int beepType) +{ + UINT flags = MB_OK | MB_TOPMOST; + + switch(beepType) + { + case lamexp_beep_info: flags = flags | MB_ICONASTERISK; + case lamexp_beep_warning: flags = flags | MB_ICONEXCLAMATION; + case lamexp_beep_error: flags = flags | MB_ICONHAND; + } + + return MessageBoxW(NULL, text, L"LameXP", flags); +} + +/* * Suspend calling thread for N milliseconds */ inline void lamexp_sleep(const unsigned int delay) @@ -1735,10 +1752,16 @@ unsigned __int64 lamexp_current_file_time(void) /* * Bring the specifed window to the front */ -bool lamexp_bring_to_front(const QWidget *win) +bool lamexp_bring_to_front(const QWidget *window) { - const bool ret = (SetForegroundWindow(win->winId()) == TRUE); - SwitchToThisWindow(win->winId(), TRUE); + bool ret = false; + + if(window) + { + ret = (SetForegroundWindow(window->winId()) == TRUE); + SwitchToThisWindow(window->winId(), TRUE); + } + return ret; } @@ -2027,7 +2050,7 @@ bool lamexp_sheet_of_glass_update(QWidget *window) /* * Update the window icon */ -bool lamexp_set_window_icon(QWidget *window, const QIcon &icon, const bool bIsBigIcon) +lamexp_icon_t *lamexp_set_window_icon(QWidget *window, const QIcon &icon, const bool bIsBigIcon) { if(!icon.isNull()) { @@ -2035,11 +2058,21 @@ bool lamexp_set_window_icon(QWidget *window, const QIcon &icon, const bool bIsBi if(HICON hIcon = lamexp_qicon2hicon(icon, extend, extend)) { SendMessage(window->winId(), WM_SETICON, (bIsBigIcon ? ICON_BIG : ICON_SMALL), LPARAM(hIcon)); - //DestroyIcon(hIcon); /*FIXME: Destroying the icon here will remove it from the window*/ + return reinterpret_cast(hIcon); } - return true; } - return false; + return NULL; +} + +/* + * Free window icon + */ +void lamexp_free_window_icon(lamexp_icon_t *icon) +{ + if(HICON hIcon = reinterpret_cast(icon)) + { + DestroyIcon(hIcon); + } } /* @@ -2103,85 +2136,6 @@ void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessag } /* - * Finalization function (final clean-up) - */ -void lamexp_finalization(void) -{ - qDebug("lamexp_finalization()"); - - //Free all tools - lamexp_clean_all_tools(); - - //Delete temporary files - const QString &tempFolder = lamexp_temp_folder2(); - if(!tempFolder.isEmpty()) - { - bool success = false; - for(int i = 0; i < 100; i++) - { - if(lamexp_clean_folder(tempFolder)) - { - success = true; - break; - } - lamexp_sleep(100); - } - if(!success) - { - MessageBoxW(NULL, L"Sorry, LameXP was unable to clean up all temporary files. Some residual files in your TEMP directory may require manual deletion!", L"LameXP", MB_ICONEXCLAMATION|MB_TOPMOST); - lamexp_exec_shell(NULL, tempFolder, QString(), QString(), true); - } - } - - //Clear folder cache - LAMEXP_DELETE(g_lamexp_known_folder.knownFolders); - - //Clear languages - lamexp_clean_all_translations(); - - //Destroy Qt application object - QApplication *application = dynamic_cast(QApplication::instance()); - LAMEXP_DELETE(application); - - //Release DWM API - g_lamexp_dwmapi.dwmIsCompositionEnabled = NULL; - g_lamexp_dwmapi.dwmExtendFrameIntoClientArea = NULL; - g_lamexp_dwmapi.dwmEnableBlurBehindWindow = NULL; - LAMEXP_DELETE(g_lamexp_dwmapi.dwmapi_dll); - - //Detach from shared memory - lamexp_ipc_exit(); - - //Free STDOUT and STDERR buffers - if(g_lamexp_console_attached) - { - if(std::filebuf *tmp = dynamic_cast(std::cout.rdbuf())) - { - std::cout.rdbuf(NULL); - LAMEXP_DELETE(tmp); - } - if(std::filebuf *tmp = dynamic_cast(std::cerr.rdbuf())) - { - std::cerr.rdbuf(NULL); - LAMEXP_DELETE(tmp); - } - } - - //Close log file - if(g_lamexp_log_file) - { - fclose(g_lamexp_log_file); - g_lamexp_log_file = NULL; - } - - //Free CLI Arguments - LAMEXP_DELETE(g_lamexp_argv.list); - - //Free TEMP folder - lamexp_temp_folder_clear(); -} - -/* * Initialize debug thread */ static const HANDLE g_debug_thread1 = LAMEXP_DEBUG ? NULL : lamexp_debug_thread_init(); @@ -2238,3 +2192,48 @@ extern "C" void _lamexp_global_init_win32(void) LAMEXP_ZERO_MEMORY(g_lamexp_themes_enabled); LAMEXP_ZERO_MEMORY(g_lamexp_dwmapi); } + +/////////////////////////////////////////////////////////////////////////////// +// FINALIZATION +/////////////////////////////////////////////////////////////////////////////// + +extern "C" void _lamexp_global_free_win32(void) +{ + //Clear folder cache + LAMEXP_DELETE(g_lamexp_known_folder.knownFolders); + + //Destroy Qt application object + QApplication *application = dynamic_cast(QApplication::instance()); + LAMEXP_DELETE(application); + + //Release DWM API + g_lamexp_dwmapi.dwmIsCompositionEnabled = NULL; + g_lamexp_dwmapi.dwmExtendFrameIntoClientArea = NULL; + g_lamexp_dwmapi.dwmEnableBlurBehindWindow = NULL; + LAMEXP_DELETE(g_lamexp_dwmapi.dwmapi_dll); + + //Free STDOUT and STDERR buffers + if(g_lamexp_console_attached) + { + if(std::filebuf *tmp = dynamic_cast(std::cout.rdbuf())) + { + std::cout.rdbuf(NULL); + LAMEXP_DELETE(tmp); + } + if(std::filebuf *tmp = dynamic_cast(std::cerr.rdbuf())) + { + std::cerr.rdbuf(NULL); + LAMEXP_DELETE(tmp); + } + } + + //Close log file + if(g_lamexp_log_file) + { + fclose(g_lamexp_log_file); + g_lamexp_log_file = NULL; + } + + //Free CLI Arguments + LAMEXP_DELETE(g_lamexp_argv.list); +} diff --git a/src/Global_EntryPoint.cpp b/src/Global_Zero.cpp similarity index 65% rename from src/Global_EntryPoint.cpp rename to src/Global_Zero.cpp index 29905233..23590256 100644 --- a/src/Global_EntryPoint.cpp +++ b/src/Global_Zero.cpp @@ -22,6 +22,8 @@ #include "Global.h" +#include + /////////////////////////////////////////////////////////////////////////////// // GLOBAL FUNCTIONS /////////////////////////////////////////////////////////////////////////////// @@ -44,7 +46,7 @@ static size_t lamexp_entry_check(void) } /* - * Application entry point (runs before static initializers) + * Function declarations */ extern "C" { @@ -56,24 +58,49 @@ extern "C" void _lamexp_global_init_ipcom(void); void _lamexp_global_init_utils(void); - int lamexp_entry_point(void) + void _lamexp_global_free_win32(void); + void _lamexp_global_free_versn(void); + void _lamexp_global_free_tools(void); + void _lamexp_global_free_ipcom(void); + void _lamexp_global_free_utils(void); +} + +/* + * Application entry point (runs before static initializers) + */ + +extern "C" int lamexp_entry_point(void) +{ + if(g_lamexp_entry_check_flag != 0x789E09B2) { - if(g_lamexp_entry_check_flag != 0x789E09B2) - { - lamexp_fatal_exit(L"Application initialization has failed, take care!"); - } + lamexp_fatal_exit(L"Application initialization has failed, take care!"); + } - //Call global initialization functions - _lamexp_global_init_win32(); - _lamexp_global_init_versn(); - _lamexp_global_init_tools(); - _lamexp_global_init_ipcom(); - _lamexp_global_init_utils(); + //Call global initialization functions + _lamexp_global_init_win32(); + _lamexp_global_init_versn(); + _lamexp_global_init_tools(); + _lamexp_global_init_ipcom(); + _lamexp_global_init_utils(); - //Make sure we will pass the check - g_lamexp_entry_check_flag = (~g_lamexp_entry_check_flag); + //Make sure we will pass the check + g_lamexp_entry_check_flag = (~g_lamexp_entry_check_flag); - //Now initialize the C Runtime library! - return WinMainCRTStartup(); - } + //Now initialize the C Runtime library! + return WinMainCRTStartup(); +} + +/* + * Application finalization function + */ +void lamexp_finalization(void) +{ + qDebug("lamexp_finalization()"); + + //Call global finalization functions, in proper order + _lamexp_global_free_versn(); + _lamexp_global_free_tools(); + _lamexp_global_free_ipcom(); + _lamexp_global_free_utils(); + _lamexp_global_free_win32(); } -- 2.11.0