From 8e2a96fa7901448e223990a043118aee7724643e Mon Sep 17 00:00:00 2001 From: Connor Walker Date: Mon, 10 Jan 2022 00:42:42 +0000 Subject: [PATCH] Fixed undefined behaviour, reworked build process. --- .gitmodules | 2 +- .vscode/tasks.json | 55 +++++++++++++++++++++++++++++---- Build-Debug.cmd | 4 --- Build-Release.cmd | 4 --- Build.ps1 | 76 ++++++++++++++++++++++++++++++++++++++++++++++ ViGEmClient | 2 +- src/main.c | 17 +++++++---- src/stadia.c | 4 +-- src/tray.c | 30 +++++++++--------- 9 files changed, 155 insertions(+), 39 deletions(-) delete mode 100644 Build-Debug.cmd delete mode 100644 Build-Release.cmd create mode 100644 Build.ps1 diff --git a/.gitmodules b/.gitmodules index 996b67b..c27c6c2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "ViGEmClient"] path = ViGEmClient - url = https://github.com/Ryochan7/ViGEmClient.git + url = https://github.com/ViGEm/ViGEmClient.git diff --git a/.vscode/tasks.json b/.vscode/tasks.json index acf51a5..d3b7c98 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -16,8 +16,12 @@ }, { "type": "shell", - "label": "Build debug binary", - "command": "./Build-Debug.cmd", + "label": "Build debug binary (x86)", + "command": "./Build.ps1", + "args": [ + "DEBUG", + "x86" + ], "problemMatcher": [ "$msCompile" ], @@ -28,12 +32,51 @@ }, { "type": "shell", - "label": "Build release binary", - "command": "./Build-Release.cmd", + "label": "Build debug binary (x64)", + "command": "./Build.ps1", + "args": [ + "DEBUG", + "x64" + ], "problemMatcher": [ "$msCompile" ], - "group": "build" - } + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "type": "shell", + "label": "Build release binary (x86)", + "command": "./Build.ps1", + "args": [ + "RELEASE", + "x86" + ], + "problemMatcher": [ + "$msCompile" + ], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "type": "shell", + "label": "Build release binary (x64)", + "command": "./Build.ps1", + "args": [ + "RELEASE", + "x64" + ], + "problemMatcher": [ + "$msCompile" + ], + "group": { + "kind": "build", + "isDefault": true + } + }, ] } \ No newline at end of file diff --git a/Build-Debug.cmd b/Build-Debug.cmd deleted file mode 100644 index 24c33cf..0000000 --- a/Build-Debug.cmd +++ /dev/null @@ -1,4 +0,0 @@ -mkdir bin -mkdir obj -rc.exe /foobj/stadia-vigem.res res/res.rc -cl.exe /Zi /Od /EHsc /DWIN32 /D_UNICODE /DUNICODE /IViGEmClient/include /Foobj/ /Febin/stadia-vigem.exe ViGEmClient/src/*.cpp obj/stadia-vigem.res src/*.c \ No newline at end of file diff --git a/Build-Release.cmd b/Build-Release.cmd deleted file mode 100644 index 2f1b4ed..0000000 --- a/Build-Release.cmd +++ /dev/null @@ -1,4 +0,0 @@ -mkdir bin -mkdir obj -rc.exe /foobj/stadia-vigem.res res/res.rc -cl.exe /GL /Zi /O2 /EHsc /DWIN32 /D_UNICODE /DUNICODE /IViGEmClient/include /Foobj/ /Febin/stadia-vigem.exe ViGEmClient/src/*.cpp obj/stadia-vigem.res src/*.c \ No newline at end of file diff --git a/Build.ps1 b/Build.ps1 new file mode 100644 index 0000000..1232a95 --- /dev/null +++ b/Build.ps1 @@ -0,0 +1,76 @@ +[CmdletBinding()] +param ( + [Parameter()][ValidateSet("DEBUG", "RELEASE")][string]$Configuration = "DEBUG", + [Parameter()][ValidateSet("x86", "x64")][string]$Architecture = "x86" +) + +$script:CommonFlags = @("/Zi", "/W4", "/EHsc", "/DWIN32", "/D_UNICODE", "/DUNICODE") +$script:DebugFlags = @( "/Od" ) +$script:ReleaseFlags = @("/GL", "/O2") + +$script:OutputName = "stadia-vigem-" + +function Import-Prerequisites { + if (Get-Module -ListAvailable -Name WintellectPowerShell) { + Update-Module -Name WintellectPowerShell + } else { + Install-Module -Name WintellectPowerShell -Scope CurrentUser -Force + } + + Import-Module -Name WintellectPowerShell +} + +function Invoke-BuildTools { + param ( + $Architecture + ) + + $latestVsInstallationInfo = Get-VSSetupInstance -All | Sort-Object -Property InstallationVersion -Descending | Select-Object -First 1 + + Invoke-CmdScript "$($latestVsInstallationInfo.InstallationPath)\VC\Auxiliary\Build\vcvarsall.bat" $Architecture +} + +function Invoke-Build { + param ( + $Architecture + ) + + $OutputName = "$script:OutputName$Architecture.exe" + $Flags = If ($Configuration -eq "DEBUG") {$script:DebugFlags} else {$script:ReleaseFlags} + + $StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch + + Write-Host "*** ${OutputName}: Build started ***" + Write-Host + + $StopWatch.Start() + + & "rc.exe" /foobj/stadia-vigem.res res/res.rc + & "cl.exe" $Flags $CommonFlags /IViGEmClient/include /Foobj/ /Febin/$OutputName ViGEmClient/src/*.cpp obj/stadia-vigem.res src/*.c + + $StopWatch.Stop() + + Write-Host + Write-Host "*** ${OutputName}: Build finished in $($StopWatch.Elapsed) ***" +} + +# Entry + +New-Item -Path "bin" -ItemType Directory -Force > $null +New-Item -Path "obj" -ItemType Directory -Force > $null + +Import-Prerequisites + +Write-Host "-- Build started. Configuration: $Configuration, Architecture: $Architecture --" + +if ($Architecture -eq "x86" -Or $Architecture -eq "ALL") { + Invoke-BuildTools -Architecture "x86" + Invoke-Build -Architecture "x86" +} + +if ($Architecture -eq "x64" -Or $Architecture -eq "ALL") { + Invoke-BuildTools -Architecture "x64" + Invoke-Build -Architecture "x64" +} + +Write-Host "-- Build completed. --" \ No newline at end of file diff --git a/ViGEmClient b/ViGEmClient index 843b1c8..e869243 160000 --- a/ViGEmClient +++ b/ViGEmClient @@ -1 +1 @@ -Subproject commit 843b1c83f8046b1ad4e7a59f758bbf8733922f1f +Subproject commit e86924356d4ba2783cc7b450d0cf2ac120820991 diff --git a/src/main.c b/src/main.c index 25d2bb9..4e07fec 100644 --- a/src/main.c +++ b/src/main.c @@ -19,8 +19,7 @@ #endif #define MAX_ACTIVE_DEVICE_COUNT 4 -#define ACTIVE_DEVICE_MENU_TEMPLATE TEXT("%d. Stadia Controller") -#define BATTERY_NA_TEXT TEXT("N/A") +#define DEVICE_COUNT_TEMPLATE TEXT("%d/4 device(s) connected") struct active_device { @@ -36,7 +35,7 @@ static SRWLOCK active_devices_lock = SRWLOCK_INIT; static PVIGEM_CLIENT vigem_client; static BOOL vigem_connected = FALSE; -static struct tray_menu tray_menu_device_count = {.text = TEXT("0/4 device(s) connected") }; +static struct tray_menu tray_menu_device_count; // future declarations static void stadia_controller_update_cb(struct stadia_controller *controller, struct stadia_state *state); @@ -79,7 +78,13 @@ static void rebuild_tray_menu() AcquireSRWLockShared(&active_devices_lock); - _stprintf((&tray_menu_device_count)->text, TEXT("%d/4 device(s) connected"), active_device_count); + LPTSTR old_device_count_text = tray_menu_device_count.text; + + INT tray_text_length = _sctprintf(DEVICE_COUNT_TEMPLATE, active_device_count); + tray_menu_device_count.text = (LPTSTR)malloc((tray_text_length + 1) * sizeof(TCHAR)); + _stprintf(tray_menu_device_count.text, DEVICE_COUNT_TEMPLATE, active_device_count); + + free(old_device_count_text); ReleaseSRWLockShared(&active_devices_lock); @@ -353,7 +358,7 @@ static void quit_cb(struct tray_menu *item) tray_exit(); } -int main() +INT main() { rebuild_tray_menu(); if (tray_init(&tray) < 0) @@ -395,7 +400,7 @@ int main() } AcquireSRWLockExclusive(&active_devices_lock); - for (int i = 0; i < active_device_count; i++) + for (INT i = 0; i < active_device_count; i++) { hid_close_device(active_devices[i]->src_device); hid_free_device(active_devices[i]->src_device); diff --git a/src/stadia.c b/src/stadia.c index 139511d..5fb143f 100644 --- a/src/stadia.c +++ b/src/stadia.c @@ -203,7 +203,7 @@ void stadia_controller_destroy(struct stadia_controller *controller) controller->active = FALSE; SetEvent(controller->stopping_event); - int thread_count = 0; + INT thread_count = 0; HANDLE threads[2]; WaitForMultipleObjects(2, threads, TRUE, INFINITE); @@ -221,7 +221,7 @@ void stadia_controller_destroy(struct stadia_controller *controller) CloseHandle(controller->stopping_event); CloseHandle(controller->output_event); - for (int i = 0; i < thread_count; i++) + for (INT i = 0; i < thread_count; i++) { CloseHandle(threads[i]); } diff --git a/src/tray.c b/src/tray.c index b7405be..5d86345 100644 --- a/src/tray.c +++ b/src/tray.c @@ -19,7 +19,7 @@ static WNDCLASSEX wc; static NOTIFYICONDATA nid; -static HWND hwnd = NULL; +static HWND window_handle = NULL; static HMENU hmenu = NULL; static HANDLE hmutex; static HDEVNOTIFY hdevntf; @@ -45,7 +45,7 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARA POINT p; GetCursorPos(&p); SetForegroundWindow(hwnd); - WORD cmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, + BOOL cmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, p.x, p.y, 0, hwnd, NULL); SendMessage(hwnd, WM_COMMAND, cmd, 0); return 0; @@ -97,12 +97,12 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARA static HMENU _tray_menu(struct tray_menu *m, UINT *id) { - HMENU hmenu = CreatePopupMenu(); + HMENU new_menu = CreatePopupMenu(); for (; m != NULL && m->text != NULL; m++, (*id)++) { if (_tcscmp(m->text, TEXT("-")) == 0) { - InsertMenu(hmenu, *id, MF_SEPARATOR, TRUE, TEXT("")); + InsertMenu(new_menu, *id, MF_SEPARATOR, TRUE, TEXT("")); } else { @@ -129,10 +129,10 @@ static HMENU _tray_menu(struct tray_menu *m, UINT *id) item.dwTypeData = m->text; item.dwItemData = (ULONG_PTR)m; - InsertMenuItem(hmenu, *id, TRUE, &item); + InsertMenuItem(new_menu, *id, TRUE, &item); } } - return hmenu; + return new_menu; } int tray_init(struct tray *tray) @@ -153,16 +153,16 @@ int tray_init(struct tray *tray) return -1; } - hwnd = CreateWindowEx(0, WC_TRAY_CLASS_NAME, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0); - if (hwnd == NULL) + window_handle = CreateWindowEx(0, WC_TRAY_CLASS_NAME, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0); + if (window_handle == NULL) { return -1; } - UpdateWindow(hwnd); + UpdateWindow(window_handle); memset(&nid, 0, sizeof(nid)); nid.cbSize = sizeof(NOTIFYICONDATA); - nid.hWnd = hwnd; + nid.hWnd = window_handle; nid.uID = 0; nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nid.uCallbackMessage = WM_TRAY_CALLBACK_MESSAGE; @@ -197,7 +197,7 @@ void tray_update(struct tray *tray) HMENU prevmenu = hmenu; UINT id = ID_TRAY_FIRST; hmenu = _tray_menu(tray->menu, &id); - SendMessage(hwnd, WM_INITMENUPOPUP, (WPARAM)hmenu, 0); + SendMessage(window_handle, WM_INITMENUPOPUP, (WPARAM)hmenu, 0); HICON hicon = LoadIcon(wc.hInstance, tray->icon); if (nid.hIcon) { @@ -225,7 +225,7 @@ void tray_exit() DestroyMenu(hmenu); } PostQuitMessage(0); - hwnd = NULL; + window_handle = NULL; UnregisterClass(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL)); ReleaseMutex(hmutex); CloseHandle(hmutex); @@ -233,7 +233,7 @@ void tray_exit() void tray_register_device_notification(GUID filter, void (*cb)(UINT, LPTSTR)) { - if (hwnd == NULL) + if (window_handle == NULL) { return; } @@ -244,7 +244,7 @@ void tray_register_device_notification(GUID filter, void (*cb)(UINT, LPTSTR)) dbdi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; dbdi.dbcc_classguid = filter; - hdevntf = RegisterDeviceNotification(hwnd, &dbdi, DEVICE_NOTIFY_WINDOW_HANDLE); + hdevntf = RegisterDeviceNotification(window_handle, &dbdi, DEVICE_NOTIFY_WINDOW_HANDLE); if (hdevntf != NULL) { devntf_cb = cb; @@ -253,7 +253,7 @@ void tray_register_device_notification(GUID filter, void (*cb)(UINT, LPTSTR)) void tray_show_notification(UINT type, LPTSTR title, LPTSTR text) { - if (hwnd == NULL) + if (window_handle == NULL) { return; }