22#include < filesystem>
33
44#include " EDVoiceApp.h"
5- #include " GUI/EDVoiceGUI.h"
5+
6+ #include < conio.h>
7+ #include < windows.h>
8+
9+ void run_failback_cli (
10+ const std::filesystem::path& execPath,
11+ const std::filesystem::path& configFile);
12+
13+ #ifdef GUI_MODE
614
715// ----------------------------------------------------------------------------
816// GUI
917// ----------------------------------------------------------------------------
1018
11- #if 1
12- #include < windows.h >
19+ #include < sstream >
20+ #include " GUI/EDVoiceGUI.h "
1321
1422int WINAPI wWinMain (
1523 _In_ HINSTANCE hInstance,
@@ -24,76 +32,111 @@ int WINAPI wWinMain(
2432 const std::filesystem::path execPath = std::filesystem::path (szArgList[0 ]).parent_path ();
2533 const std::filesystem::path configFile = execPath / " config" / " default.json" ;
2634
27- EDVoiceGUI gui (execPath, configFile, hInstance, nShowCmd);
35+ std::ostringstream local;
36+ std::cout.rdbuf (local.rdbuf ());
37+ std::cerr.rdbuf (local.rdbuf ());
38+
39+ bool failbackMode = false ;
40+
41+ try {
42+ EDVoiceGUI gui (execPath, configFile, hInstance, nShowCmd);
43+ gui.run ();
44+ } catch (const std::exception& e) {
45+ std::cerr << " [FATAL ] Exception: " << e.what () << std::endl;
46+ failbackMode = true ;
47+ } catch (...) {
48+ std::cerr << " [FATAL ] Exception unknown" << std::endl;
49+ failbackMode = true ;
50+ }
51+
52+ if (failbackMode) {
53+ std::ofstream out (" EDVoiceCrash.log" , std::ios::app);
54+ out << local.str ();
55+ out.close ();
56+
57+ // Clear WM_QUIT messages
58+ MSG msg;
59+
60+ while (PeekMessage (&msg, NULL , 0 , 0 , PM_REMOVE)) {
61+ if (msg.message == WM_QUIT) continue ;
62+
63+ TranslateMessage (&msg);
64+ DispatchMessage (&msg);
65+ }
66+
67+ MessageBoxA (NULL , " Could not load the GUI. A Log file EDVoiceCrash.log was created. Please send it for bug review.\n Trying to launch in failback mode...\n o7" , " EDVoice" , MB_OK | MB_ICONERROR);
68+
69+ run_failback_cli (execPath, configFile);
70+ }
2871
29- gui.run ();
30-
3172 return 0 ;
3273}
3374
34- #else
35- #include <windows.h>
36- #include <fcntl.h>
37- #include <io.h>
75+ #else // !GUI_MODE
76+
77+ // ----------------------------------------------------------------------------
78+ // Console mode
79+ // ----------------------------------------------------------------------------
3880
3981int WINAPI wWinMain (
4082 _In_ HINSTANCE hInstance,
4183 _In_opt_ HINSTANCE hPrevInstance,
4284 _In_ LPWSTR lpCmdLine,
4385 _In_ int nShowCmd)
4486{
45- // --- Créer une console ---
46- AllocConsole();
47-
48- // Rediriger stdout, stderr et stdin vers la console
49- FILE* fp;
50- freopen_s(&fp, "CONOUT$", "w", stdout);
51- freopen_s(&fp, "CONOUT$", "w", stderr);
52- freopen_s(&fp, "CONIN$", "r", stdin);
53-
54- std::cout << "Console initialized ✅" << std::endl;
55-
56- // --- Parsing arguments ---
5787 LPWSTR* szArgList;
5888 int argCount;
5989 szArgList = CommandLineToArgvW (GetCommandLine (), &argCount);
6090
6191 const std::filesystem::path execPath = std::filesystem::path (szArgList[0 ]).parent_path ();
6292 const std::filesystem::path configFile = execPath / " config" / " default.json" ;
6393
64- // --- Lancer ton GUI ---
65- EDVoiceGUI gui(execPath, configFile, hInstance, nShowCmd);
66- gui.run();
94+ run_failback_cli (execPath, configFile);
6795
68- // --- Libérer la console à la fermeture ---
69- FreeConsole();
7096 return 0 ;
7197}
7298
73- #endif
99+ #endif
74100
75- // ----------------------------------------------------------------------------
76101
77- #if 0
102+ LRESULT CALLBACK w32WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
103+ {
104+ switch (msg) {
105+ case WM_CLOSE:
106+ ::DestroyWindow (hWnd);
107+ return 0 ;
108+
109+ case WM_DESTROY:
110+ ::PostQuitMessage (0 );
111+ return 0 ;
112+ }
113+
114+ return ::DefWindowProcW (hWnd, msg, wParam, lParam);
115+ }
78116
79- int main(int argc, char* argv[])
117+
118+ void run_failback_cli (
119+ const std::filesystem::path& execPath,
120+ const std::filesystem::path& configFile)
80121{
81- const std::filesystem::path execPath = std::filesystem::path(argv[0]).parent_path();
82- // Load default config or the one specified in command line
83- std::filesystem::path configFile;
122+ AllocConsole ();
84123
85- if (argc < 2) {
86- std::cout << "Using default configuration" << std::endl;
87- configFile = std::filesystem::current_path() / "config" / "default.json";
88- }
89- else {
90- configFile = argv[1];
91- }
124+ FILE* fp;
125+ freopen_s (&fp, " CONOUT$" , " w" , stdout);
126+ freopen_s (&fp, " CONOUT$" , " w" , stderr);
127+ freopen_s (&fp, " CONIN$" , " r" , stdin);
92128
93129 EDVoiceApp app (execPath, configFile);
94130
95- app.run();
131+ MSG msg;
132+ HWND hConsole = GetConsoleWindow ();
96133
97- return 0;
134+ while (IsWindow (hConsole) && GetMessage (&msg, NULL , 0 , 0 )) {
135+ TranslateMessage (&msg);
136+ DispatchMessage (&msg);
137+ hConsole = GetConsoleWindow (); // Update in case the console is closed
138+ }
139+
140+ FreeConsole ();
98141}
99- # endif
142+
0 commit comments