Skip to content

Commit 71cc86d

Browse files
committed
Added a bunch of comments because its probably good practice.
1 parent a1dacaa commit 71cc86d

File tree

7 files changed

+92
-69
lines changed

7 files changed

+92
-69
lines changed

src/app/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
#ifdef _WIN32
1+
#ifdef _WIN32 // Will only build the Windows version for Windows devices.
22
#include <windows.h>
33
extern int RunWindowsApp ( );
44

5+
// Windows expects a WinMain as the application entry point so it doesn't attempt to run it through the terminal.
56
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
6-
return RunWindowsApp();
7+
return RunWindowsApp(); // Call the main start function from WinApp.cpp.
78
}
8-
#else
9+
#else // For other operating systems just return 0 as they haven't been implemented yet.
910
int main ( ) {
1011
return 0;
1112
}

src/core/Platform.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
#ifdef _WIN32
2+
// Separate code for Windows devices.
23
#include "Platform.h"
34
#include "win/WinBackend.h"
45

56
namespace Platform {
67

78
void init ( ) {
8-
WinBackend::init();
9+
WinBackend::init(); // Start.
910
}
1011

1112
void shutdown ( ) {
12-
WinBackend::shutdown();
13+
WinBackend::shutdown(); // End.
1314
}
1415

1516
void setTargetWindow (WindowHandle w) {
16-
WinBackend::setTargetWindow(w);
17+
WinBackend::setTargetWindow(w); // Assign target window.
1718
}
1819

1920
void lockInput ( ) {
20-
WinBackend::lockInput();
21+
WinBackend::lockInput(); // Lock the keyboard to the targeted window.
2122
}
2223

2324
void unlockInput ( ) {
24-
WinBackend::unlockInput();
25+
WinBackend::unlockInput(); // Unlock the keyboard.
2526
}
2627

2728
bool processEvents ( ) {
28-
return WinBackend::processEvents();
29+
return WinBackend::processEvents(); // Process events.
2930
}
3031

3132
}
3233
#else
33-
34+
// Currently do nothing for non Windows devices.
3435
#endif

src/win/WinApp.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@
66

77

88
static const char * WINDOW_CLASS = "SplitLoafTray";
9-
static HINSTANCE g_hInstance = NULL;
9+
static HINSTANCE g_hInstance = NULL; // A handle to the current instance of the application.
1010

1111
LRESULT CALLBACK WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1212
switch (msg) {
1313

1414
case TRAY_CALLBACK:
1515
if (lParam == WM_RBUTTONUP) {
16-
ShowTrayMenu(hwnd);
16+
ShowTrayMenu(hwnd); // Show a menu when right clicked.
1717
}
1818
break;
1919

2020
case WM_COMMAND:
2121
switch (LOWORD(wParam)) {
2222
case CMD_SETTINGS:
23-
OpenSettingsWindow(g_hInstance, hwnd);
23+
OpenSettingsWindow(g_hInstance, hwnd); // Open settings window when that option is selected.
2424
break;
2525

2626
case CMD_EXIT:
27-
PostQuitMessage(0);
27+
PostQuitMessage(0); // Exit the program if the option is selected.
2828
break;
2929
}
3030
break;
3131

3232
case WM_DESTROY:
33-
RemoveTrayIcon();
33+
RemoveTrayIcon(); // Close the app and remove it from the System Tray.
3434
PostQuitMessage(0);
3535
return 0;
3636
}
3737

3838
return DefWindowProc(hwnd, msg, wParam, lParam);
3939
}
4040

41-
int RunWindowsApp ( ) {
41+
int RunWindowsApp ( ) { // Create the application.
4242
g_hInstance = GetModuleHandle(NULL);
4343

4444
WNDCLASS wc = {};
@@ -49,9 +49,9 @@ int RunWindowsApp ( ) {
4949

5050
HWND hwnd = CreateWindowEx(0, WINDOW_CLASS, "Split Loaf",0, 0, 0, 0, 0,NULL, NULL, g_hInstance, NULL);
5151

52-
InitTrayIcon(hwnd);
52+
InitTrayIcon(hwnd); // Create the System Tray icon.
5353

54-
Platform::init();
54+
Platform::init(); // Start the main program.
5555

5656
MSG msg {};
5757
while (true) {
@@ -60,7 +60,7 @@ int RunWindowsApp ( ) {
6060
DispatchMessage(& msg);
6161

6262
if (msg.message == WM_QUIT) {
63-
Platform::shutdown();
63+
Platform::shutdown(); // Exit app if quit.
6464
return 0;
6565
}
6666
}

src/win/WinBackend.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include "WinTray.h"
55
#include "core/Platform.h"
66

7+
// IDs for handling the button events.
78
#define TRAY_CALLBACK (WM_USER + 1)
8-
99
#define CMD_SETTINGS 1001
10-
#define CMD_EXIT 1002
10+
#define CMD_EXIT 1002
1111

1212

1313
static HHOOK keyboardHook = NULL;
@@ -16,49 +16,50 @@ static bool locked = false;
1616
static bool targetHasFocus = false;
1717

1818
HHOOK & WinHooks_GetKeyboardHook ( ) {
19-
return keyboardHook;
19+
return keyboardHook; // Gives the keyboard hook.
2020
}
2121
HWND & WinHooks_GetTargetWindow ( ) {
22-
return targetWindow;
22+
return targetWindow; // Gives the target window.
2323
}
2424
bool & WinHooks_GetLockedFlag ( ) {
25-
return locked;
25+
return locked; // Gives the current state of locked.
2626
}
2727
bool & WinHooks_GetTargetFocusFlag ( ) {
28-
return targetHasFocus;
28+
return targetHasFocus; // Gives the state of the target window.
2929
}
3030

31-
void WinBackend::init ( ) {
31+
void WinBackend::init ( ) { // Sets up the keyboard hook.
3232
keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,GetModuleHandle(NULL),0);
3333
}
3434

35-
void WinBackend::shutdown ( ) {
35+
void WinBackend::shutdown ( ) { // Removes the hook when closed.
3636
UnhookWindowsHookEx(keyboardHook);
3737
}
3838

3939
void WinBackend::setTargetWindow (WindowHandle w) {
40-
targetWindow = (HWND)w;
40+
targetWindow = (HWND)w; // Sets the target window.
4141
}
4242

4343
void WinBackend::lockInput ( ) {
44-
locked = true;
44+
locked = true; // Sets the locked value.
4545
}
4646

4747
void WinBackend::unlockInput ( ) {
48-
locked = false;
48+
locked = false; // Unlock the keyboard.
4949
targetHasFocus = false;
5050
}
5151

5252
void WinBackend::sendVirtualKey (uint32_t vk) {
53+
// Send a key down signal to the target window.
54+
// Key up isn't important so can be left to be handled by Windows to allow modifier keys to be used.
5355
INPUT in = {0};
5456

5557
in.type = INPUT_KEYBOARD;
5658
in.ki.wVk = vk;
5759

58-
5960
SendInput(1, & in, sizeof(INPUT));
6061
}
6162

62-
bool WinBackend::processEvents ( ) {
63+
bool WinBackend::processEvents ( ) { // Handle processes.
6364
return true;
6465
}

src/win/WinHooks.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,43 @@
66
#include <string>
77
#include <algorithm>
88

9-
std::string ToLower (const std::string & s) {
9+
std::string ToLower (const std::string & s) { // Set the string to lowercase.
1010
std::string out = s;
1111
std::transform(out.begin(), out.end(), out.begin(), ::tolower);
1212
return out;
1313
}
1414

15-
std::string ToUpper (const std::string & s) {
15+
std::string ToUpper (const std::string & s) { // Set the string to uppercase.
1616
std::string out = s;
1717
std::transform(out.begin(), out.end(), out.begin(), ::toupper);
1818
return out;
1919
}
2020

21-
std::string GetWindowTitle (HWND hwnd) {
21+
std::string GetWindowTitle (HWND hwnd) { // Get the name of the target window.
2222
char buffer[256];
2323
GetWindowTextA(hwnd, buffer, sizeof(buffer));
2424
return std::string(buffer);
2525
}
2626

27-
void Tray_SetIdle ( ) {
27+
void Tray_SetIdle ( ) { // Set the tooltip for the System Tray to indicate state of the program as idle.
2828
UpdateTrayTooltip("Split Loaf - Idle");
2929
}
3030

3131
void Tray_SetTargeted (const std::string & windowName) {
32-
UpdateTrayTooltip("Split Loaf - " + ToLower(windowName));
32+
UpdateTrayTooltip("Split Loaf - " + ToLower(windowName)); // When a window is targeted it shows the app name in lowercase.
3333
}
3434

3535
void Tray_SetLocked (const std::string & windowName) {
36-
UpdateTrayTooltip("Split Loaf - " + ToUpper(windowName));
36+
UpdateTrayTooltip("Split Loaf - " + ToUpper(windowName)); // When the keyboard is locked, the name of the window is shown in capitals.
3737
}
3838

3939
LRESULT CALLBACK LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam) {
4040
if (nCode != HC_ACTION) {
41+
// If no action go to next.
4142
return CallNextHookEx(NULL, nCode, wParam, lParam);
4243
}
4344

45+
// Get the current values of the program state.
4446
auto & targetWindow = WinHooks_GetTargetWindow();
4547
auto & locked = WinHooks_GetLockedFlag();
4648
auto & targetHasFocus = WinHooks_GetTargetFocusFlag();
@@ -51,26 +53,32 @@ LRESULT CALLBACK LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam)
5153
return CallNextHookEx(NULL, nCode, wParam, lParam);
5254
}
5355

54-
if (wParam == WM_KEYDOWN) {
56+
if (wParam == WM_KEYDOWN) { // For key press downs.
5557
if (kbd -> vkCode == VK_F8) {
58+
// If F8 is pressed set the target window to the current one.
5659
POINT p;
5760
GetCursorPos(& p);
5861
targetWindow = WindowFromPoint(p);
5962
if (targetWindow) {
63+
// If there is a suitable target window, update the tooltip and title.
6064
std::string title = GetWindowTitle(targetWindow);
6165
Tray_SetTargeted(title);
6266
} else {
67+
// If not a suitable target, set it to idle.
6368
Tray_SetIdle();
6469
}
6570
return 1;
6671
}
6772

6873
if (kbd -> vkCode == VK_F6) {
74+
// If F6 is pressed, lock the keyboard to the target window.
6975
locked = (targetWindow != NULL);
7076
if (locked) {
77+
// If valid, update tooltip and name.
7178
std::string title = GetWindowTitle(targetWindow);
7279
Tray_SetLocked(title);
7380

81+
// Set the target window to be the main active window.
7482
SetForegroundWindow(targetWindow);
7583
Sleep(1);
7684
targetHasFocus = 1;
@@ -79,33 +87,37 @@ LRESULT CALLBACK LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam)
7987
}
8088

8189
if (kbd -> vkCode == VK_F7) {
90+
// If F7 is pressed unlock the keyboard.
8291
locked = false;
8392
targetHasFocus = false;
8493
if (targetWindow) {
94+
// If valid update the name and tooltip.
8595
std::string title = GetWindowTitle(targetWindow);
8696
Tray_SetTargeted(title);
8797
} else {
98+
// If no valid window, set idle.
8899
Tray_SetIdle();
89100
}
90101
return 1;
91102
}
92103
}
93104

94105
if (locked && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) {
95-
if (targetWindow) {
106+
// For key presses down while locked.
107+
if (targetWindow) { // If there is a target window.
96108
HWND fg = GetForegroundWindow();
97109

98-
if (fg != targetWindow) {
99-
SetForegroundWindow(targetWindow);
100-
Sleep(1);
101-
targetHasFocus = 1;
110+
if (fg != targetWindow) { // If the target window is not the active one.
111+
SetForegroundWindow(targetWindow); // Make it active.
112+
Sleep(1); // Sleep so it can be processed.
113+
targetHasFocus = 1; // Say its been made active.
102114
}
103115

104-
WinBackend::sendVirtualKey(kbd -> vkCode);
116+
WinBackend::sendVirtualKey(kbd -> vkCode); // Redirect the keyboard presses.
105117
return 1;
106118
}
107119
}
108120

109-
return CallNextHookEx (NULL, nCode, wParam, lParam);
121+
return CallNextHookEx (NULL, nCode, wParam, lParam); // Move to the next.
110122
}
111123

0 commit comments

Comments
 (0)