1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "AppParamParser.h"
#include "settings/AdvancedSettings.h"
#include "utils/CharsetConverter.h"
#include "utils/log.h"
#include "utils/SystemInfo.h"
#include "threads/platform/win/Win32Exception.h"
#include "shellapi.h"
#include "dbghelp.h"
#include "XBDateTime.h"
#include "threads/Thread.h"
#include "Application.h"
#include "XbmcContext.h"
#include "GUIInfoManager.h"
#include "utils/StringUtils.h"
#include "utils/CPUInfo.h"
#include <mmdeviceapi.h>
#include "win32/IMMNotificationClient.h"
#ifndef _DEBUG
#define XBMC_TRACK_EXCEPTIONS
#endif
// Minidump creation function
LONG WINAPI CreateMiniDump( EXCEPTION_POINTERS* pEp )
{
win32_exception::write_stacktrace(pEp);
win32_exception::write_minidump(pEp);
return pEp->ExceptionRecord->ExceptionCode;;
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR commandLine, INT )
{
// set up some xbmc specific relationships
XBMC::Context context;
//this can't be set from CAdvancedSettings::Initialize() because it will overwrite
//the loglevel set with the --debug flag
#ifdef _DEBUG
g_advancedSettings.m_logLevel = LOG_LEVEL_DEBUG;
g_advancedSettings.m_logLevelHint = LOG_LEVEL_DEBUG;
#else
g_advancedSettings.m_logLevel = LOG_LEVEL_NORMAL;
g_advancedSettings.m_logLevelHint = LOG_LEVEL_NORMAL;
#endif
CLog::SetLogLevel(g_advancedSettings.m_logLevel);
// Initializes CreateMiniDump to handle exceptions.
win32_exception::set_version(g_infoManager.GetVersion());
SetUnhandledExceptionFilter( CreateMiniDump );
// check if Kodi is already running
std::string appName = CSysInfo::GetAppName();
CreateMutex(NULL, FALSE, (appName + " Media Center").c_str());
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
HWND m_hwnd = FindWindow(appName.c_str(), appName.c_str());
if(m_hwnd != NULL)
{
// switch to the running instance
ShowWindow(m_hwnd,SW_RESTORE);
SetForegroundWindow(m_hwnd);
}
return 0;
}
#ifndef HAS_DX
if(CWIN32Util::GetDesktopColorDepth() < 32)
{
//FIXME: replace it by a SDL window for all ports
MessageBox(NULL, "Desktop Color Depth isn't 32Bit", (appName + ": Fatal Error").c_str(), MB_OK|MB_ICONERROR);
return 0;
}
#endif
if((g_cpuInfo.GetCPUFeatures() & CPU_FEATURE_SSE2) == 0)
{
MessageBox(NULL, "No SSE2 support detected", (appName + ": Fatal Error").c_str(), MB_OK|MB_ICONERROR);
return 0;
}
//Initialize COM
CoInitializeEx(NULL, COINIT_MULTITHREADED);
// Handle numeric values using the default/POSIX standard
setlocale(LC_NUMERIC, "C");
// If the command line passed to WinMain, commandLine, is not "" we need
// to process the command line arguments.
// Note that commandLine does not include the program name and can be
// equal to "" if no arguments were supplied. By contrast GetCommandLineW()
// does include the program name and is never equal to "".
g_advancedSettings.Initialize();
if (strlen(commandLine) != 0)
{
int argc;
LPWSTR* argvW = CommandLineToArgvW(GetCommandLineW(), &argc);
std::vector<std::string> strargvA;
strargvA.resize(argc);
const char** argv = (const char**) LocalAlloc(LMEM_FIXED, argc*sizeof(char*));
if (!argv)
return 20;
for (int i = 0; i < argc; i++)
{
g_charsetConverter.wToUTF8(argvW[i], strargvA[i]);
argv[i] = strargvA[i].c_str();
}
// Parse the arguments
CAppParamParser appParamParser;
appParamParser.Parse(argv, argc);
// Clean up the storage we've used
LocalFree(argvW);
LocalFree(argv);
}
// Initialise Winsock
WSADATA wd;
WSAStartup(MAKEWORD(2,2), &wd);
// use 1 ms timer precision - like SDL initialization used to do
timeBeginPeriod(1);
#ifdef XBMC_TRACK_EXCEPTIONS
try
{
#endif
// Create and run the app
if(!g_application.Create())
{
MessageBox(NULL, "ERROR: Unable to create application. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
#ifdef XBMC_TRACK_EXCEPTIONS
}
catch (const XbmcCommons::UncheckedException &e)
{
e.LogThrowMessage("CApplication::Create()");
MessageBox(NULL, "ERROR: Unable to create application. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
catch (...)
{
CLog::Log(LOGERROR, "exception in CApplication::Create()");
MessageBox(NULL, "ERROR: Unable to create application. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
#endif
#ifndef _DEBUG
// we don't want to see the "no disc in drive" windows message box
SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
#endif
#ifdef XBMC_TRACK_EXCEPTIONS
try
{
#endif
if (!g_application.CreateGUI())
{
MessageBox(NULL, "ERROR: Unable to create GUI. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
#ifdef XBMC_TRACK_EXCEPTIONS
}
catch (const XbmcCommons::UncheckedException &e)
{
e.LogThrowMessage("CApplication::CreateGUI()");
MessageBox(NULL, "ERROR: Unable to create GUI. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
catch (...)
{
CLog::Log(LOGERROR, "exception in CApplication::CreateGUI()");
MessageBox(NULL, "ERROR: Unable to create GUI. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
#endif
#ifdef XBMC_TRACK_EXCEPTIONS
try
{
#endif
if (!g_application.Initialize())
{
MessageBox(NULL, "ERROR: Unable to Initialize. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
#ifdef XBMC_TRACK_EXCEPTIONS
}
catch (const XbmcCommons::UncheckedException &e)
{
e.LogThrowMessage("CApplication::Initialize()");
MessageBox(NULL, "ERROR: Unable to Initialize. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
catch (...)
{
CLog::Log(LOGERROR, "exception in CApplication::Initialize()");
MessageBox(NULL, "ERROR: Unable to Initialize. Exiting.", (appName + ": Error").c_str(), MB_OK|MB_ICONERROR);
return 1;
}
#endif
HRESULT hr = E_FAIL;
IMMDeviceEnumerator *pEnumerator = NULL;
CMMNotificationClient cMMNC;
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator);
if(SUCCEEDED(hr))
{
pEnumerator->RegisterEndpointNotificationCallback(&cMMNC);
SAFE_RELEASE(pEnumerator);
}
g_application.Run();
// clear previously set timer resolution
timeEndPeriod(1);
// the end
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator);
if(SUCCEEDED(hr))
{
pEnumerator->UnregisterEndpointNotificationCallback(&cMMNC);
SAFE_RELEASE(pEnumerator);
}
WSACleanup();
CoUninitialize();
return 0;
}
|