]> git.sesse.net Git - casparcg/blob - shell/windows_specific.cpp
#346 #441 Added remote debugging support in html producer.
[casparcg] / shell / windows_specific.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "stdafx.h"
23
24 #include "platform_specific.h"
25 #include "resource.h"
26
27 #include <common/os/windows/windows.h>
28 #include <common/env.h>
29 #include <common/log.h>
30
31 #include <winnt.h>
32 #include <mmsystem.h>
33 #include <atlbase.h>
34
35 #include <sstream>
36 #include <cstdlib>
37
38 // NOTE: This is needed in order to make CComObject work since this is not a real ATL project.
39 CComModule _AtlModule;
40 extern __declspec(selectany) CAtlModule* _pAtlModule = &_AtlModule;
41
42 namespace caspar {
43
44 LONG WINAPI UserUnhandledExceptionFilter(EXCEPTION_POINTERS* info)
45 {
46         try
47         {
48                 CASPAR_LOG(fatal) << L"#######################\n UNHANDLED EXCEPTION: \n"
49                         << L"Adress:" << info->ExceptionRecord->ExceptionAddress << L"\n"
50                         << L"Code:" << info->ExceptionRecord->ExceptionCode << L"\n"
51                         << L"Flag:" << info->ExceptionRecord->ExceptionFlags << L"\n"
52                         << L"Info:" << info->ExceptionRecord->ExceptionInformation << L"\n"
53                         << L"Continuing execution. \n#######################";
54
55                 CASPAR_LOG_CALL_STACK();
56         }
57         catch (...){}
58
59         return EXCEPTION_CONTINUE_EXECUTION;
60 }
61
62 void setup_prerequisites()
63 {
64         SetUnhandledExceptionFilter(UserUnhandledExceptionFilter);
65
66         // Increase time precision. This will increase accuracy of function like Sleep(1) from 10 ms to 1 ms.
67         static struct inc_prec
68         {
69                 inc_prec(){ timeBeginPeriod(1); }
70                 ~inc_prec(){ timeEndPeriod(1); }
71         } inc_prec;
72 }
73
74 void change_icon(const HICON hNewIcon)
75 {
76         auto hMod = ::LoadLibrary(L"Kernel32.dll");
77         typedef DWORD(__stdcall *SCI)(HICON);
78         auto pfnSetConsoleIcon = reinterpret_cast<SCI>(::GetProcAddress(hMod, "SetConsoleIcon"));
79         pfnSetConsoleIcon(hNewIcon);
80         ::FreeLibrary(hMod);
81 }
82
83 void setup_console_window()
84 {
85         auto hOut = GetStdHandle(STD_OUTPUT_HANDLE);
86
87         // Disable close button in console to avoid shutdown without cleanup.
88         EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_GRAYED);
89         DrawMenuBar(GetConsoleWindow());
90         //SetConsoleCtrlHandler(HandlerRoutine, true);
91
92         // Configure console size and position.
93         auto coord = GetLargestConsoleWindowSize(hOut);
94         coord.X /= 2;
95
96         SetConsoleScreenBufferSize(hOut, coord);
97
98         SMALL_RECT DisplayArea = { 0, 0, 0, 0 };
99         DisplayArea.Right = coord.X - 1;
100         DisplayArea.Bottom = (coord.Y - 1) / 2;
101         SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
102
103         change_icon(::LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(101)));
104
105         // Set console title.
106         std::wstringstream str;
107         str << "CasparCG Server " << env::version() << L" x64 ";
108 #ifdef COMPILE_RELEASE
109         str << " Release";
110 #elif  COMPILE_PROFILE
111         str << " Profile";
112 #elif  COMPILE_DEVELOP
113         str << " Develop";
114 #elif  COMPILE_DEBUG
115         str << " Debug";
116 #endif
117         SetConsoleTitle(str.str().c_str());
118 }
119
120 void increase_process_priority()
121 {
122         SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
123 }
124
125 void wait_for_keypress()
126 {
127         boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
128         std::system("pause");
129 }
130
131 std::shared_ptr<void> setup_debugging_environment()
132 {
133 #ifdef _DEBUG
134         HANDLE hLogFile;
135         hLogFile = CreateFile(L"crt_log.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
136         std::shared_ptr<void> crt_log(nullptr, [](HANDLE h){::CloseHandle(h); });
137
138         _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
139         _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
140         _CrtSetReportFile(_CRT_WARN, hLogFile);
141         _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
142         _CrtSetReportFile(_CRT_ERROR, hLogFile);
143         _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
144         _CrtSetReportFile(_CRT_ASSERT, hLogFile);
145
146         return crt_log;
147 #else
148         return nullptr;
149 #endif
150 }
151
152 void wait_for_remote_debugging()
153 {
154 #ifdef _DEBUG
155         MessageBox(nullptr, L"Now is the time to connect for remote debugging...", L"Debug", MB_OK | MB_TOPMOST);
156 #endif   
157 }
158
159 }