-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (139 loc) · 4.15 KB
/
main.cpp
File metadata and controls
167 lines (139 loc) · 4.15 KB
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
#include "logger.h"
#include "usb_monitor.h"
#include "audio_monitor.h"
#include "usb_power.h"
#include <Windows.h>
#include <atomic>
#include <string>
// Global monitors
USBMonitor g_usbMonitor;
AudioMonitor g_audioMonitor;
USBPowerMonitor g_powerMonitor;
std::atomic<bool> g_running{true};
// Hidden window class name
const wchar_t* WINDOW_CLASS_NAME = L"DeviceMonitorWindow";
// Window procedure for receiving device change messages
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DEVICECHANGE:
g_usbMonitor.processDeviceChange(wParam, lParam);
return TRUE;
case WM_CLOSE:
g_running = false;
PostQuitMessage(0);
return 0;
default:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
}
// Console control handler for Ctrl+C
BOOL WINAPI ConsoleHandler(DWORD ctrlType) {
switch (ctrlType) {
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
LOG_INFO(L"Shutting down...");
g_running = false;
return TRUE;
default:
return FALSE;
}
}
HWND createHiddenWindow() {
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandleW(nullptr);
wc.lpszClassName = WINDOW_CLASS_NAME;
if (!RegisterClassExW(&wc)) {
return nullptr;
}
return CreateWindowExW(
0,
WINDOW_CLASS_NAME,
L"Device Monitor",
0,
0, 0, 0, 0,
HWND_MESSAGE, // Message-only window
nullptr,
GetModuleHandleW(nullptr),
nullptr);
}
void writeConsole(const std::wstring& text) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD written = 0;
WriteConsoleW(hConsole, text.c_str(), static_cast<DWORD>(text.length()), &written, nullptr);
}
void printBanner() {
writeConsole(L"\n");
writeConsole(L" ========================================\n");
writeConsole(L" USB & Audio Device Monitor\n");
writeConsole(L" Press Ctrl+C to exit\n");
writeConsole(L" ========================================\n");
writeConsole(L"\n");
}
int main() {
// Initialize COM for audio monitoring
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr)) {
writeConsole(L"Failed to initialize COM\n");
return 1;
}
// Initialize logger
Logger::instance().init();
printBanner();
// Create hidden window for USB notifications
HWND hwnd = createHiddenWindow();
if (!hwnd) {
LOG_ERROR(L"Failed to create message window");
CoUninitialize();
return 1;
}
// Setup console control handler
SetConsoleCtrlHandler(ConsoleHandler, TRUE);
// Start USB monitoring
if (!g_usbMonitor.start(hwnd)) {
LOG_ERROR(L"Failed to start USB monitor");
}
// Start audio monitoring
if (!g_audioMonitor.start()) {
LOG_ERROR(L"Failed to start audio monitor");
}
// List current devices
g_usbMonitor.listCurrentDevices();
writeConsole(L"\n");
// Show USB power topology
g_powerMonitor.printTopology();
writeConsole(L"\n");
g_powerMonitor.printPowerSummary();
writeConsole(L"\n");
g_audioMonitor.listCurrentDevices();
writeConsole(L"\n");
LOG_SECTION(L"MONITORING STARTED");
LOG_INFO(L"Waiting for device changes...");
writeConsole(L"\n");
// Message loop
MSG msg;
while (g_running) {
// Use PeekMessage to allow checking g_running
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
g_running = false;
break;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
// Small sleep to reduce CPU usage
Sleep(10);
}
// Cleanup
LOG_INFO(L"Stopping monitors...");
g_usbMonitor.stop();
g_audioMonitor.stop();
DestroyWindow(hwnd);
UnregisterClassW(WINDOW_CLASS_NAME, GetModuleHandleW(nullptr));
Logger::instance().shutdown();
CoUninitialize();
return 0;
}