Skip to content

Commit

Permalink
Fixed undefined behaviour, reworked build process.
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Walker committed Jan 10, 2022
1 parent 5bcb5b4 commit 8e2a96f
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
@@ -1,3 +1,3 @@
[submodule "ViGEmClient"]
path = ViGEmClient
url = https://github.com/Ryochan7/ViGEmClient.git
url = https://github.com/ViGEm/ViGEmClient.git
55 changes: 49 additions & 6 deletions .vscode/tasks.json
Expand Up @@ -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"
],
Expand All @@ -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
}
},
]
}
4 changes: 0 additions & 4 deletions Build-Debug.cmd

This file was deleted.

4 changes: 0 additions & 4 deletions Build-Release.cmd

This file was deleted.

76 changes: 76 additions & 0 deletions 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. --"
2 changes: 1 addition & 1 deletion ViGEmClient
17 changes: 11 additions & 6 deletions src/main.c
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/stadia.c
Expand Up @@ -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);
Expand All @@ -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]);
}
Expand Down
30 changes: 15 additions & 15 deletions src/tray.c
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -225,15 +225,15 @@ void tray_exit()
DestroyMenu(hmenu);
}
PostQuitMessage(0);
hwnd = NULL;
window_handle = NULL;
UnregisterClass(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
ReleaseMutex(hmutex);
CloseHandle(hmutex);
}

void tray_register_device_notification(GUID filter, void (*cb)(UINT, LPTSTR))
{
if (hwnd == NULL)
if (window_handle == NULL)
{
return;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 8e2a96f

Please sign in to comment.