203 lines
5.2 KiB
C++

#include "include/main.h"
bool connectedToSim = false;
// 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: {
toLog("Close");
DestroyWindow(hWnd);
break;
}
case WM_DESTROY: {
toLog("Destroy");
removeNotifyIcon(hWnd);
UnregisterClass(WINDOW_CLASS, GetModuleHandle(NULL));
PostQuitMessage(0);
break;
}
case TRAY_MESSAGE: {
switch (lParam) {
case WM_RBUTTONUP:
case WM_CONTEXTMENU: {
const int IDM_EXIT = 100;
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_GRAYED, NULL, "Version: " BIT);
if (connectedToSim) {
AppendMenu(hMenu, MF_STRING | MF_GRAYED, NULL, "Connected to Sim");
}
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);
}
DestroyMenu(hMenu);
break;
}
}
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)
{
HANDLE simConnect;
HRESULT hr;
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);
hr = SimConnect_Open(&simConnect, "GAConnector", hWnd, NULL, NULL, NULL);
if (hr == E_FAIL) {
toLog("SimConnect_Open failed");
} else {
connectedToSim = true;
}
// The Message Loop
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Exit
SimConnect_Close(&simConnect);
toLog("Bye");
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);
}
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;
}