Initial WIN32 Impl

This commit is contained in:
2022-10-02 02:00:52 +02:00
parent bbb0c220e1
commit 997586b8aa
24 changed files with 2087 additions and 20 deletions
+69
View File
@@ -0,0 +1,69 @@
file(GLOB socket CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/websocket/*.cpp)
enable_language(RC)
# Add WIN32 to hide console window
add_executable(germanairlinesva_esp WIN32
${socket}
resources/resources-${BIT}.rc
main.cpp
)
target_include_directories(germanairlinesva_esp PRIVATE
${CMAKE_SOURCE_DIR}/file/include
${CMAKE_SOURCE_DIR}/simdata/include
${CMAKE_SOURCE_DIR}/websocket/include
${CMAKE_SOURCE_DIR}/utilities/include
${CMAKE_SOURCE_DIR}/ixwebsocket/include
${CMAKE_SOURCE_DIR}/nlohmann/include
${CMAKE_SOURCE_DIR}/SimConnect/${BIT}/include
)
target_compile_definitions(germanairlinesva_esp PRIVATE
BIT="${BIT}"
IBM
)
target_compile_options(germanairlinesva_esp PRIVATE
-Wall
-Wextra
-pedantic
-fvisibility=hidden
)
if(DEBUG)
target_compile_options(germanairlinesva_esp PRIVATE
-g
)
target_link_options(germanairlinesva_esp PRIVATE
-g
)
else()
target_compile_options(germanairlinesva_esp PRIVATE
-O2
)
endif()
message("Building for Windows ${BIT} into ${PROJECT_BINARY_DIR}/ESP/${BIT}/${PLUGIN_NAME}")
set_target_properties(germanairlinesva_esp PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/ESP/${BIT}/${PLUGIN_NAME}"
OUTPUT_NAME "GA Connector"
)
if(DEBUG)
target_compile_options(germanairlinesva_esp PRIVATE
-gcodeview
)
target_link_options(germanairlinesva_esp PRIVATE
-Wl,-pdb=
)
endif()
target_link_libraries(germanairlinesva_esp PRIVATE
user32
shell32
version
ixwebsocket
pthread
${CMAKE_SOURCE_DIR}/SimConnect/${BIT}/SimConnect.lib
)
+20
View File
@@ -0,0 +1,20 @@
#ifndef GERMANAIRLINESVA_GACONNECTOR_ESP_MAIN_H
#define GERMANAIRLINESVA_GACONNECTOR_ESP_MAIN_H
#include "constants.h"
#include "windows.h"
#include "SimConnect.h"
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
WINBOOL addNotifyIcon(HWND hWnd);
WINBOOL removeNotifyIcon(HWND hWnd);
void toLog(const std::string &message);
#endif
+203
View File
@@ -0,0 +1,203 @@
#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;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

+24
View File
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity type="win32"
name="germanairlinesva.gaconnector"
version="1.0.0.14"
processorArchitecture='x86'
/>
<dependency>
<dependentAssembly>
<assemblyIdentity type='win32'
name='Microsoft.FlightSimulator.SimConnect'
version='10.0.61259.0'
processorArchitecture='x86'
publicKeyToken='67c7c14424d61b5b' />
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity type="win32"
name="germanairlinesva.gaconnector"
version="1.0.0.14"
processorArchitecture='amd64'
/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
+39
View File
@@ -0,0 +1,39 @@
#define APSTUDIO_READONLY_SYMBOLS
#include "winres.h"
#include "winuser.h"
#undef APSTUDIO_READONLY_SYMBOLS
#include "constants.h"
APP_ICON ICON "gatoolv2_icon.ico"
1 RT_MANIFEST "manifest-32.xml"
VS_VERSION_INFO VERSIONINFO
FILEVERSION VER_FILEVERSION
PRODUCTVERSION VER_PRODUCTVERSION
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
FILEFLAGS VER_DEBUG
FILEOS VOS__WINDOWS32
FILETYPE VFT_APP
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904E4"
BEGIN
VALUE "CompanyName", VER_COMPANY_NAME
VALUE "FileDescription", VER_FILE_DESCRIPTION
VALUE "FileVersion", VER_FILEVERSION_STR
VALUE "InternalName", VER_INTERNAL_NAME
VALUE "LegalCopyright", VER_LEGAL_COPYRIGHT
VALUE "OriginalFilename", VER_ORIGINAL_FILE_NAME
VALUE "ProductName", VER_PRODUCT_NAME " 32"
VALUE "ProductVersion", VER_PRODUCTVERSION_STR
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1252
END
END
+39
View File
@@ -0,0 +1,39 @@
#define APSTUDIO_READONLY_SYMBOLS
#include "winres.h"
#include "winuser.h"
#undef APSTUDIO_READONLY_SYMBOLS
#include "constants.h"
APP_ICON ICON "gatoolv2_icon.ico"
1 RT_MANIFEST "manifest-64.xml"
VS_VERSION_INFO VERSIONINFO
FILEVERSION VER_FILEVERSION
PRODUCTVERSION VER_PRODUCTVERSION
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
FILEFLAGS VER_DEBUG
FILEOS VOS__WINDOWS32
FILETYPE VFT_APP
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904E4"
BEGIN
VALUE "CompanyName", VER_COMPANY_NAME
VALUE "FileDescription", VER_FILE_DESCRIPTION
VALUE "FileVersion", VER_FILEVERSION_STR
VALUE "InternalName", VER_INTERNAL_NAME
VALUE "LegalCopyright", VER_LEGAL_COPYRIGHT
VALUE "OriginalFilename", VER_ORIGINAL_FILE_NAME
VALUE "ProductName", VER_PRODUCT_NAME " 64"
VALUE "ProductVersion", VER_PRODUCTVERSION_STR
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1252
END
END