Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
363 lines (319 loc) · 12.9 KB
/
Copy pathmain.cpp
File metadata and controls
363 lines (319 loc) · 12.9 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <chrono>
#include <iostream>
#include <memory>
#include <windows.h>
#include <string>
#include <thread>
#include <unordered_map>
#include "ButtonManager.h"
#include "HelperFunctions.h"
#include "ListViewManager.h"
#include "ProcessInfo.h"
#include "ProcessMonitor.h"
#include "resource.h"
#include "SystemInfoPanel.h"
#include "TasksIDDef.h"
#include "Concurrency/Scheduler.h"
#include "Concurrency/TaskManager.h"
#include "Concurrency/ThreadPool.h"
using namespace std::chrono_literals;
class MainWindow {
public:
explicit MainWindow(const HINSTANCE hInstance) : hInstance(hInstance), hwnd(nullptr) {
initializeMessageHandlers();
}
~MainWindow() {
process_monitor_->stopMonitoring();
task_manager_->stopAllTasks();
}
bool Create(const std::wstring &title, const int width = 800, const int height = 600) {
constexpr wchar_t CLASS_NAME[] = L"MainWindowClass";
WNDCLASSW wc = {};
wc.lpfnWndProc = &MainWindow::WindowProcSetup;
wc.hInstance = hInstance;
wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.lpszClassName = CLASS_NAME;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APP_ICON));
RegisterClassW(&wc);
hwnd = CreateWindowExW(
0,
CLASS_NAME,
title.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
nullptr, nullptr, hInstance, this // pass `this` as lpParam
);
if (!hwnd) {
return false;
}
return true;
}
void Show(const int nCmdShow) const {
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
}
[[nodiscard]] HWND GetHwnd() const { return hwnd; }
private:
HINSTANCE hInstance;
HWND hwnd;
std::unique_ptr<ListViewManager> list_view_manager_;
std::unique_ptr<ProcessMonitor> process_monitor_;
std::unique_ptr<ButtonManager> button_manager_;
std::unique_ptr<SystemInfoPanel> system_info_panel_;
std::unique_ptr<TaskManager> task_manager_;
std::unique_ptr<ThreadPool> thread_pool_;
std::unique_ptr<Scheduler> scheduler_;
using MessageHandler = std::function<LRESULT(HWND, WPARAM, LPARAM)>;
std::unordered_map<UINT, MessageHandler> message_handlers_;
// Initial WndProc setup to attach instance pointer
static LRESULT CALLBACK WindowProcSetup(
const HWND hwnd,
const UINT msg,
const WPARAM wParam,
const LPARAM lParam
) {
if (msg == WM_NCCREATE) {
const auto createStruct = reinterpret_cast<CREATESTRUCT *>(lParam);
auto instance = static_cast<MainWindow *>(createStruct->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(instance));
SetWindowLongPtrW(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(
&MainWindow::WindowProcThunk
)
);
return instance->WndProc(hwnd, msg, wParam, lParam);
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
// Redirect messages to the member WndProc
static LRESULT CALLBACK WindowProcThunk(
const HWND hwnd,
const UINT msg,
const WPARAM wParam,
const LPARAM lParam
) {
const auto instance = reinterpret_cast<MainWindow *>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
return instance ? instance->WndProc(hwnd, msg, wParam, lParam) : DefWindowProcW(hwnd, msg, wParam, lParam);
}
void fetchSystemInfo(HWND hwnd) const {
const std::wstring osVersion = GetWindowsVersionString();
PostMessage(hwnd, WM_APP + 100, 0, reinterpret_cast<LPARAM>(new std::wstring(osVersion)));
// Fetch CPU Info (Example)
const std::wstring cpuInfo = GetSystemInfoString();
PostMessage(hwnd, WM_APP + 101, 0, reinterpret_cast<LPARAM>(new std::wstring(cpuInfo)));
const std::wstring totalRam = GetTotalPhysicalMemory();
PostMessage(hwnd, WM_APP + 102, 0, reinterpret_cast<LPARAM>(new std::wstring(totalRam)));
try {
task_manager_->addTask(
systemInfoScheduledTaskID,
"System Info Update", // Task name
[this, hwnd](const std::stop_token &st) {
if (!st.stop_requested()) {
const std::wstring availRam = GetMemoryLoadPercentage();
// Post messages to update RAM
PostMessage(hwnd, WM_APP + 103, 0, reinterpret_cast<LPARAM>(new std::wstring(availRam)));
}
},
1000ms
);
} catch (...) {
std::cerr << "Something went wrong when creating new task" << std::endl;
}
}
// Initialize message handlers
void initializeMessageHandlers() {
message_handlers_[WM_PAINT] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handlePaint(hwnd, wp, lp);
};
message_handlers_[WM_CREATE] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleCreate(hwnd, wp, lp);
};
message_handlers_[WM_SIZE] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleSize(hwnd, wp, lp);
};
message_handlers_[WM_CLOSE] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleClose(hwnd, wp, lp);
};
message_handlers_[WM_DESTROY] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleDestroy(hwnd, wp, lp);
};
message_handlers_[WM_COMMAND] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleCommand(hwnd, wp, lp);
};
message_handlers_[WM_APP + 100] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleOsVersionUpdate(hwnd, wp, lp);
};
message_handlers_[WM_APP + 101] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleCpuInfoUpdate(hwnd, wp, lp);
};
message_handlers_[WM_APP + 102] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleTotalRamUpdate(hwnd, wp, lp);
};
message_handlers_[WM_APP + 103] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleAvailableRamUpdate(hwnd, wp, lp);
};
message_handlers_[WM_APP + 104] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleProcessListUpdate(hwnd, wp, lp);
};
message_handlers_[WM_NOTIFY] = [this](const HWND hwnd, const WPARAM wp, const LPARAM lp) {
return handleWMNotify(hwnd, wp, lp);
};
}
// Message handlers
static LRESULT handlePaint(const HWND hwnd, WPARAM, LPARAM) {
PAINTSTRUCT ps;
const HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1));
EndPaint(hwnd, &ps);
return 0;
}
LRESULT handleCreate(HWND hwnd, WPARAM, LPARAM) {
// Initialize controls
thread_pool_ = std::make_unique<ThreadPool>(2);
task_manager_ = std::make_unique<TaskManager>();
scheduler_ = std::make_unique<Scheduler>(*task_manager_, *thread_pool_, 100ms);
list_view_manager_ = std::make_unique<ListViewManager>(hwnd);
button_manager_ = std::make_unique<ButtonManager>(hwnd);
system_info_panel_ = std::make_unique<SystemInfoPanel>(hwnd);
if (
!list_view_manager_->Create() ||
!button_manager_->Create() ||
!system_info_panel_->Create(0, 300, 200, 50)
) {
MessageBoxW(hwnd, L"Something went wrong!", L"ERROR", MB_ICONERROR);
PostQuitMessage(0);
}
fetchSystemInfo(hwnd);
process_monitor_ = std::make_unique<ProcessMonitor>(*task_manager_, hwnd, list_view_manager_->getHWND());
return 0;
}
LRESULT handleSize(HWND, WPARAM, const LPARAM lParam) const {
if (list_view_manager_) {
list_view_manager_->Resize(LOWORD(lParam), HIWORD(lParam));
}
if (button_manager_) {
button_manager_->Resize(LOWORD(lParam), HIWORD(lParam));
}
if (system_info_panel_) {
system_info_panel_->Resize(LOWORD(lParam), HIWORD(lParam));
}
return 0;
}
static LRESULT handleClose(const HWND hwnd, WPARAM, LPARAM) {
DestroyWindow(hwnd);
return 0;
}
static LRESULT handleDestroy(HWND, WPARAM, LPARAM) {
PostQuitMessage(0);
return 0;
}
LRESULT handleCommand(HWND, const WPARAM wParam, LPARAM) const {
this->onCommand(wParam);
return 0;
}
LRESULT handleOsVersionUpdate(HWND, WPARAM, const LPARAM lParam) const {
if (system_info_panel_) {
const std::unique_ptr<std::wstring> data(reinterpret_cast<std::wstring *>(lParam));
system_info_panel_->SetOsVersion(*data);
}
return 0;
}
LRESULT handleCpuInfoUpdate(HWND, WPARAM, const LPARAM lParam) const {
if (system_info_panel_) {
const std::unique_ptr<std::wstring> data(reinterpret_cast<std::wstring *>(lParam));
system_info_panel_->SetCpuInfo(*data);
}
return 0;
}
LRESULT handleTotalRamUpdate(HWND, WPARAM, const LPARAM lParam) const {
if (system_info_panel_) {
const std::unique_ptr<std::wstring> data(reinterpret_cast<std::wstring *>(lParam));
system_info_panel_->SetTotalRam(*data);
}
return 0;
}
LRESULT handleAvailableRamUpdate(HWND, WPARAM, const LPARAM lParam) const {
if (system_info_panel_) {
const std::unique_ptr<std::wstring> data(reinterpret_cast<std::wstring *>(lParam));
system_info_panel_->SetAvailableRam(*data);
}
return 0;
}
LRESULT handleProcessListUpdate(HWND, WPARAM, const LPARAM lParam) const {
if (list_view_manager_) {
const auto data(reinterpret_cast<ProcessUpdateData *>(lParam));
list_view_manager_->applyListViewUpdates(data);
}
return 0;
}
LRESULT handleWMNotify(HWND, WPARAM, const LPARAM lParam) const {
if (
const auto nm_hdr = reinterpret_cast<LPNMHDR>(lParam);
list_view_manager_ != nullptr &&
nm_hdr->hwndFrom == list_view_manager_->getHWND() &&
nm_hdr->code == LVN_ITEMCHANGED
) {
// Only react if the item is selected
if (const auto *pListView = reinterpret_cast<NMLISTVIEW *>(lParam); pListView->uChanged & LVIF_STATE) {
if (pListView->uNewState & LVIS_SELECTED) {
// Retrieve lParam which holds the PID
LVITEMW item{};
item.mask = LVIF_PARAM;
item.iItem = pListView->iItem;
if (ListView_GetItem(nm_hdr->hwndFrom, &item)) {
const auto pid = static_cast<DWORD>(item.lParam);
std::wcout << L"Selected PID: " << pid << std::endl;
}
}
}
}
return 0;
}
// WndProc implementation
LRESULT WndProc(const HWND hwnd, const UINT msg, const WPARAM wParam, const LPARAM lParam) {
if (const auto it = message_handlers_.find(msg); it != message_handlers_.end()) {
// Call the corresponding handler
return it->second(hwnd, wParam, lParam);
}
// Default handling
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
void onCommand(const WPARAM wParam) const {
switch (LOWORD(wParam)) {
case 1:
MessageBoxW(hwnd, L"Button 1 clicked!", L"Info", MB_OK);
break;
case 2:
if (list_view_manager_ != nullptr) {
if (const DWORD pid = list_view_manager_->getSelectedPID(); pid != 0) {
KillProcessByPID(hwnd, pid);
}
}
break;
case 3:
PostQuitMessage(0); // Close the application
break;
default:
MessageBoxW(hwnd, L"Invalid Command", L"ERROR", MB_ICONERROR);
}
}
};
int WINAPI wWinMain(const HINSTANCE hInstance, HINSTANCE, PWSTR, const int nShowCmd) {
AllocConsole();
FILE *f;
freopen_s(&f, "CONOUT$", "w", stdout);
freopen_s(&f, "CONOUT$", "w", stderr);
SetProcessDPIAware(); // DPI awareness
MainWindow window(hInstance);
if (!window.Create(L"Process Viewer Lite")) {
MessageBoxW(nullptr, L"Window creation failed", L"Error", MB_ICONERROR);
return 0;
}
window.Show(nShowCmd);
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return static_cast<int>(msg.wParam);
}