232 lines
5.8 KiB
C++

#include "include/main.h"
std::unique_ptr<SimConnect> simConnect;
struct germanairlinesva::gaconnector::websocket::data toSend;
germanairlinesva::gaconnector::recorder::Recorder *recorder;
// The Window Procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static UINT s_uTaskbarRestart;
switch (msg) {
case WM_CREATE: {
s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
break;
}
case WM_CLOSE: {
DestroyWindow(hWnd);
break;
}
case WM_DESTROY: {
end(hWnd);
break;
}
case TRAY_MESSAGE: {
switch (lParam) {
case WM_RBUTTONUP:
case WM_CONTEXTMENU: {
createMenu(hWnd);
break;
}
}
break;
}
case SIMCONNECT_MESSAGE: {
if (simConnect != nullptr && simConnect->isConnected()) {
simConnect->handleMessage(
[](int version) { recorder->loadDatabase(version); },
[]() {
germanairlinesva::gaconnector::websocket::data d;
simConnect->getStates();
simConnect->getData(&d);
recorder->setData(d);
});
}
break;
}
default: {
if (msg == s_uTaskbarRestart) {
addNotifyIcon(hWnd);
}
break;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
ZeroMemory(&wc, sizeof(WNDCLASS));
HWND hWnd;
MSG msg;
// Exit if already running
hWnd = FindWindow(WINDOW_CLASS, WINDOW_CLASS);
if (hWnd != NULL) {
toLog("Exiting duplicate");
return 0;
}
// Clear log
std::ofstream clearLog(BASE_DIRECTORY "log.txt");
clearLog.close();
// Registering the Window Class
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = WINDOW_CLASS;
if (!RegisterClass(&wc)) {
toLog("Error window register: " + std::to_string(GetLastError()));
return 0;
}
// Creating the Window
hWnd = CreateWindow(WINDOW_CLASS,
WINDOW_CLASS,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
240,
120,
NULL,
NULL,
hInstance,
NULL);
if (hWnd == NULL) {
toLog("Error window create: " + std::to_string(GetLastError()));
return 0;
}
// Add Notify Icon
if (!addNotifyIcon(hWnd)) {
toLog("Error notifyIcon: " + std::to_string(GetLastError()));
return 0;
}
// Never show window
// ShowWindow(hWnd, SHOW_OPENWINDOW);
recorder = new germanairlinesva::gaconnector::recorder::Recorder(0, toLog);
// Open SimConnect
simConnect =
std::make_unique<SimConnect>(hWnd, toLog, recorder->getConfiguration());
// The Message Loop
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Exit
toLog("Connector stopped");
return msg.wParam;
}
#pragma clang diagnostic pop
WINBOOL addNotifyIcon(HWND hWnd)
{
HICON icon;
NOTIFYICONDATA niData;
ZeroMemory(&niData, sizeof(NOTIFYICONDATA));
icon = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(APP_ICON),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_SHARED);
if (icon == NULL) {
toLog("error icon " + std::to_string(GetLastError()));
}
niData.cbSize = sizeof(NOTIFYICONDATA);
niData.uVersion = NOTIFYICON_VERSION_4;
niData.uID = TRAY_ICON_ID;
niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
strcpy(niData.szTip, "GA Connector");
niData.hIcon = icon;
niData.hWnd = hWnd;
niData.uCallbackMessage = TRAY_MESSAGE;
WINBOOL retVal = Shell_NotifyIcon(NIM_ADD, &niData);
// DestroyIcon(niData.hIcon);
return retVal;
}
WINBOOL removeNotifyIcon(HWND hWnd)
{
NOTIFYICONDATA niData;
ZeroMemory(&niData, sizeof(NOTIFYICONDATA));
niData.cbSize = sizeof(NOTIFYICONDATA);
niData.uID = TRAY_ICON_ID;
niData.hWnd = hWnd;
return Shell_NotifyIcon(NIM_DELETE, &niData);
}
WINBOOL createMenu(HWND hWnd)
{
const int IDM_EXIT = 100;
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_GRAYED, NULL, "Version: " BIT " Bit");
if (simConnect->isConnected()) {
std::string version("Connected to Sim: ");
version.append(SimConnect::resolveVersion(simConnect->getVersion()));
AppendMenu(hMenu, MF_STRING | MF_GRAYED, NULL, version.c_str());
}
AppendMenu(hMenu, MF_SEPARATOR, NULL, NULL);
AppendMenu(hMenu, MF_STRING, IDM_EXIT, "Exit");
SetForegroundWindow(hWnd);
WINBOOL cmd = TrackPopupMenu(hMenu,
TPM_LEFTALIGN | TPM_LEFTBUTTON |
TPM_BOTTOMALIGN | TPM_RETURNCMD,
pt.x,
pt.y,
0,
hWnd,
NULL);
if (cmd == IDM_EXIT) {
PostMessage(hWnd, WM_CLOSE, 0, NULL);
}
return DestroyMenu(hMenu);
}
void end(HWND hWnd)
{
removeNotifyIcon(hWnd);
UnregisterClass(WINDOW_CLASS, GetModuleHandle(NULL));
/* End threads */
recorder->~Recorder();
// End SimConnect
delete simConnect.release();
PostQuitMessage(0);
}
void toLog(const std::string &message)
{
std::time_t utc = std::time(nullptr);
std::tm *time = std::gmtime(&utc);
std::ofstream msg(BASE_DIRECTORY "log.txt", std::ofstream::app);
msg << std::put_time(time, "%Y-%m-%d %H:%M:%S UTC ")
<< "German Airlines VA: " << message << std::endl;
}