-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicMute.cpp
257 lines (217 loc) · 6.27 KB
/
MicMute.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <Windows.h>
#include <TlHelp32.h>
#include <tchar.h>
#include <iostream>
#include <stdio.h>
#include <thread>
#include <chrono>
#include <iphlpapi.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include "Defs.h"
#include "resource.h"
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Winmm.lib")
HANDLE hKeyboardHook;
IMMDeviceEnumerator* pDeviceEnumerator;
IMMDevice* pMicDevice;
IAudioEndpointVolume* pMicVolume;
CHAR buffer[256];
SOCKET client;
sockaddr_in addr;
BOOL bAbortThreads = FALSE;
BOOL bLastMute;
DWORD WaitForServerStart(PUSHORT pLocalPort) {
DWORD dwPID = -1;
while (dwPID == -1) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return SNAPSHOT_FAILED;
}
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(PROCESSENTRY32);
BOOL bResult = Process32FirstW(hSnapshot, &procEntry);
while (bResult) {
if (_tcscmp(SERVER_PROC_NAME, procEntry.szExeFile) == 0) {
dwPID = procEntry.th32ProcessID;
break;
}
bResult = Process32Next(hSnapshot, &procEntry);
}
CloseHandle(hSnapshot);
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
}
std::cout << "Server PID: " << dwPID << "\n";
DWORD dwSize = 0;
DWORD dwRetVal = GetExtendedTcpTable(NULL, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
PMIB_TCPTABLE_OWNER_PID pTcpTable = reinterpret_cast<PMIB_TCPTABLE_OWNER_PID>(new char[dwSize]);
dwRetVal = GetExtendedTcpTable(pTcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
USHORT uLocalPort = 0;
if (dwRetVal == NO_ERROR) {
for (int i = 0; i < (INT)pTcpTable->dwNumEntries; i++) {
if (pTcpTable->table[i].dwOwningPid == dwPID) {
USHORT localPort = ntohs((USHORT) pTcpTable->table[i].dwLocalPort);
uLocalPort = localPort;
break;
}
}
}
if (pTcpTable != NULL) {
delete[] pTcpTable;
pTcpTable = NULL;
}
if (!uLocalPort) {
return PORT_FIND_FAILED;
}
*pLocalPort = uLocalPort;
return SUCCESSFUL;
}
VOID SendCommand(PCCH szCommand) {
strcpy_s(buffer, szCommand);
std::cout << "Trying to send command \"" << buffer << "\"..\n";
if (send(client, buffer, strlen(buffer), 0) == SOCKET_ERROR) {
std::cout << "Sending message to server failed with exit code: " << std::hex << std::uppercase << WSAGetLastError() << "!\n";
}
else {
std::cout << "Success!\n";
}
}
VOID StartKeepalive() {
std::cout << "Starting keepalive messaging..\n";
std::thread([]() {
while (!bAbortThreads) {
auto x = std::chrono::steady_clock::now() + std::chrono::milliseconds(5000);
SendCommand(KEEPALIVE_MSG);
std::this_thread::sleep_until(x);
}
}).detach();
}
VOID StartMuteWatchdog() {
if (!pMicVolume) {
return;
}
std::cout << "Starting mute watchdog..\n";
std::thread([]() {
while (!bAbortThreads) {
auto x = std::chrono::steady_clock::now() + std::chrono::milliseconds(100);
BOOL bMute;
pMicVolume->GetMute(&bMute);
if (bMute != bLastMute) {
bLastMute = bMute;
if (bMute) {
std::cout << "Changed to Mute state.\n";
SendCommand(MODE_COLOR_MSG);
SendCommand(COLOR_RED_MSG);
} else {
std::cout << "Changed to Unmute state.\n";
SendCommand(MODE_AUDIO_MSG);
}
}
std::this_thread::sleep_until(x);
}
}).detach();
}
DWORD StartFocusriteIntegration() {
USHORT uLocalPort;
DWORD dwError = WaitForServerStart(&uLocalPort);
if (dwError != SUCCESSFUL) {
return dwError;
}
std::cout << "Local port: " << uLocalPort << "\n";
WSADATA wsaData;
INT result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
return WSA_START_FAILED;
}
client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client == INVALID_SOCKET) {
WSACleanup();
return WSA_INVALID_SOCKET;
}
addr = {
.sin_family = AF_INET,
.sin_port = htons(uLocalPort)
};
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
result = connect(client, (SOCKADDR*)&addr, sizeof(addr));
if (result == SOCKET_ERROR) {
closesocket(client);
WSACleanup();
return WSA_SOCKET_ERROR;
}
SendCommand(INIT_MSG);
StartKeepalive();
StartMuteWatchdog();
return SUCCESSFUL;
}
LRESULT CALLBACK KeyboardHookCallback(int code, WPARAM wParam, LPARAM lParam) {
if (code != 0 || lParam == 0 || (wParam != WM_KEYDOWN && wParam != WM_KEYUP)) {
return CallNextHookEx(0, code, wParam, lParam);
}
KBDLLHOOKSTRUCT* pHookStruct = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
if (pHookStruct->vkCode == FUNCTION_KEY && wParam == WM_KEYUP) {
BOOL bMuted;
pMicVolume->GetMute(&bMuted);
if (bMuted) {
pMicVolume->SetMute(FALSE, NULL);
#ifdef USE_SOUND
PlaySound((PWCH) IDR_WAVE1, NULL, SND_RESOURCE | SND_ASYNC);
#endif
} else {
pMicVolume->SetMute(TRUE, NULL);
#ifdef USE_SOUND
PlaySound((PWCH)IDR_WAVE2, NULL, SND_RESOURCE | SND_ASYNC);
#endif
}
}
return CallNextHookEx(NULL, code, wParam, lParam);
}
DWORD StartMicIntegration() {
HRESULT hResult = CoInitialize(NULL);
if (hResult != S_OK) {
return COM_INIT_FAILED;
}
if (CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (PVOID*)&pDeviceEnumerator) != S_OK) {
return COCREATE_FAILED;
}
pDeviceEnumerator->GetDefaultAudioEndpoint(EDataFlow::eCapture, ERole::eCommunications, &pMicDevice);
pMicDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (PVOID*)&pMicVolume);
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookCallback, NULL, 0);
return SUCCESSFUL;
}
INT APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ INT nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
#ifdef USE_CONSOLE
AllocConsole();
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
#endif
CreateMutex(NULL, FALSE, MUTEX_NAME);
if (GetLastError() == ERROR_ALREADY_EXISTS) {
return ALREADY_RUNNING;
}
DWORD dwError = StartMicIntegration();
if (dwError) {
return dwError;
}
#ifdef USE_FOCUSRITE
dwError = StartFocusriteIntegration();
if (dwError) {
return dwError;
}
#endif
BOOL bRet;
MSG msg;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
closesocket(client);
WSACleanup();
bAbortThreads = TRUE;
return SUCCESSFUL;
}