Skip to content

Commit f85d8ce

Browse files
committed
[A] 001
Upload full source code.
1 parent 74fce52 commit f85d8ce

File tree

7 files changed

+270
-0
lines changed

7 files changed

+270
-0
lines changed

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf

bin/.gitignore

Whitespace-only changes.

build/c.ico

1.12 KB
Binary file not shown.

build/gl_build.bat

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
REM This script builds the C/C++ project using the GCC compiler.
5+
6+
REM Store the original code page
7+
for /f "tokens=2 delims=:." %%i in ('chcp') do set "ORIGINAL_CODEPAGE=%%i"
8+
9+
REM Change the code page to UTF-8 (65001)
10+
chcp 65001 >nul
11+
12+
REM Compiling resources
13+
windres -F pe-i386 -i resources.rc -o resources.o
14+
if %errorlevel% neq 0 (
15+
echo Resource compilation failed.
16+
pause
17+
exit /b 1
18+
)
19+
20+
REM Save the original folder path
21+
pushd "%~dp0\..\src" || (
22+
echo Please check the code path!
23+
pause
24+
exit /b 1
25+
)
26+
27+
REM Perform the compilation. If you encounter any issues, feel free to ask using ISSUE.
28+
gcc.exe -o ../bin/CursorRestrict.exe CursorRestrict.c ../build/resources.o -m32 -std=c11 -Werror -Wall -Wextra -Wcomment -Wwrite-strings -Wimplicit -Wreturn-type -Wshadow -Wcast-qual -Wpedantic -Wformat=2 -Wformat-overflow=2 -Wformat-truncation=2 -Wformat-security -Wnull-dereference -Wstack-protector -Wtrampolines -Walloca -Wvla -Warray-bounds=2 -Wimplicit-fallthrough=3 -Wtraditional-conversion -Wshift-overflow=2 -Wstringop-overflow=4 -Wconversion -Warith-conversion -Wlogical-op -Wduplicated-cond -Wduplicated-branches -Wformat-signedness -Wstrict-overflow=4 -Wundef -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wstack-usage=1000000 -Wcast-align=strict -Wp,-DWIN32_LEAN_AND_MEAN,-D__USE_MINGW_ANSI_STDIO=0,-D_WIN32_WINNT=0x0601,-DWINVER=0x0601,-DSTRICT,-D_MSVCRT_ -Os -luser32 -nostartfiles -nostdlib -fanalyzer -finput-charset=utf-8 --freestanding -fno-ident -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -fuse-linker-plugin -ffunction-sections -fmerge-all-constants -Wl,--subsystem,windows,-e,_c,--gc-sections,--disable-dynamicbase,--disable-reloc-section,--relax,-s,--exclude-all-symbols || (
29+
echo Compilation failed.
30+
pause
31+
popd
32+
chcp %ORIGINAL_CODEPAGE% >nul
33+
exit /b 1
34+
)
35+
36+
REM Return to the original folder
37+
popd || (
38+
echo If compilation is complete, ignore this message.
39+
pause
40+
exit /b 1
41+
)
42+
43+
echo Success! CursorRestrict.exe has been generated under the bin folder.
44+
pause
45+
46+
REM Restore the code page
47+
chcp %ORIGINAL_CODEPAGE% >nul
48+
49+
exit /b 0

build/resources.rc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "version.h"
2+
3+
IDI_ICON1 ICON "c.ico"
4+
5+
1 VERSIONINFO
6+
FILEVERSION VER_FILE_VERSION
7+
PRODUCTVERSION VER_PRODUCT_VERSION
8+
FILEFLAGSMASK 0x3fL
9+
FILEFLAGS VER_FILEFLAGS
10+
FILEOS VER_FILEOS
11+
FILETYPE VER_FILETYPE
12+
FILESUBTYPE 0x0L
13+
BEGIN
14+
BLOCK "StringFileInfo"
15+
BEGIN
16+
BLOCK "040904E4"
17+
BEGIN
18+
VALUE "CompanyName", VER_COMPANY_STR
19+
VALUE "FileDescription", VER_FILEDESCRIPTION_STR
20+
VALUE "FileVersion", VER_FILE_VERSION_STR
21+
VALUE "InternalName", VER_INTERNALNAME_STR
22+
VALUE "LegalCopyright", VER_LEGALCOPYRIGHT_STR
23+
VALUE "LegalTrademarks1", VER_LEGALTRADEMARKS1_STR
24+
VALUE "LegalTrademarks2", VER_LEGALTRADEMARKS2_STR
25+
VALUE "OriginalFilename", VER_ORIGINALFILENAME_STR
26+
VALUE "ProductName", VER_PRODUCTNAME_STR
27+
VALUE "ProductVersion", VER_PRODUCT_VERSION_STR
28+
END
29+
END
30+
31+
BLOCK "VarFileInfo"
32+
BEGIN
33+
34+
/* The following line should only be modified for localized versions. */
35+
/* It consists of any number of WORD,WORD pairs, with each pair */
36+
/* describing a language,codepage combination supported by the file. */
37+
/* */
38+
/* For example, a file might have values "0x409,1252" indicating that it */
39+
/* supports English language (0x409) in the Windows ANSI codepage (1252). */
40+
41+
VALUE "Translation", 0x404, 1252
42+
43+
END
44+
END

build/version.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#define VERSION_YEAR 2024
2+
#define VERSION_MONTH 04
3+
#define VERSION_DAY 05
4+
#define VERSION_UNSAFE 0
5+
6+
#define VER_COPYRIGHT_STR "Open Source Software"
7+
#define VER_FILEDESCRIPTION_STR "A program to restrict your cursor from moving out of the game window.(Alt+F12 toggle Restrict,Alt+f11 exit)"
8+
#define VER_INTERNALNAME_STR "Cursor Restrict Software"
9+
#define VER_PRODUCTNAME_STR "Cursor Controller Software"
10+
#define VER_LEGALCOPYRIGHT_STR "Open Source Software"
11+
#define VER_LEGALTRADEMARKS1_STR ""
12+
#define VER_LEGALTRADEMARKS2_STR ""
13+
#define VER_ORIGINALFILENAME_STR "CursorRestrict.exe"
14+
#define VER_COMPANY_STR "spacemiqote(author)"
15+
16+
#define STRINGIZE2(s) #s
17+
#define STRINGIZE(s) STRINGIZE2(s)
18+
#if (VERSION_UNSAFE == 1)
19+
#define MY_VERSION_NUM STRINGIZE(VERSION_YEAR) STRINGIZE(VERSION_MONTH) "." STRINGIZE(VERSION_DAY) STRINGIZE(VERSION_UNSAFE)
20+
#else
21+
#define MY_VERSION_NUM STRINGIZE(VERSION_YEAR) STRINGIZE(VERSION_MONTH) "." STRINGIZE(VERSION_DAY)
22+
#endif
23+
#define VER_FILE_VERSION VERSION_YEAR, VERSION_MONTH, VERSION_DAY, VERSION_UNSAFE
24+
#define VER_FILE_VERSION_STR STRINGIZE(VERSION_YEAR) "." STRINGIZE(VERSION_MONTH) "." STRINGIZE(VERSION_DAY) "." STRINGIZE(VERSION_UNSAFE)
25+
#define VER_PRODUCT_VERSION VER_FILE_VERSION
26+
#define VER_PRODUCT_VERSION_STR VER_FILE_VERSION_STR
27+
#ifdef _DEBUG
28+
#define VER_VER_DEBUG VS_FF_DEBUG
29+
#else
30+
#define VER_VER_DEBUG 0
31+
#endif
32+
#define VER_FILEOS 0x00L
33+
#define VER_FILEFLAGS VER_VER_DEBUG
34+
#define VER_FILETYPE 0x01L

src/CursorRestrict.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <Windows.h>
2+
#include <stdbool.h>
3+
#include <stdio.h>
4+
// Author: https://github.com/spacemiqote
5+
6+
HHOOK keyboardHook;
7+
RECT cursorBounds;
8+
bool boundsEnabled = false;
9+
10+
void SetCursorBounds(int left, int top, int right, int bottom);
11+
void ReleaseCursorBounds(void);
12+
void SetCursorBoundsToWindow(HWND hwnd);
13+
void ToggleCursorBounds(void);
14+
void ExitApplication(void);
15+
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
16+
void InstallKeyboardHook(void);
17+
void UninstallKeyboardHook(void);
18+
void c(void);
19+
20+
void SetCursorBounds(int left, int top, int right, int bottom) {
21+
cursorBounds.left = left;
22+
cursorBounds.top = top;
23+
cursorBounds.right = right;
24+
cursorBounds.bottom = bottom;
25+
ClipCursor(&cursorBounds);
26+
}
27+
28+
void ReleaseCursorBounds() {
29+
ClipCursor(NULL);
30+
boundsEnabled = false;
31+
}
32+
33+
void SetCursorBoundsToWindow(HWND hwnd) {
34+
RECT clientRect;
35+
GetClientRect(hwnd, &clientRect);
36+
MapWindowPoints(hwnd, NULL, (LPPOINT)&clientRect, 2);
37+
SetCursorBounds(clientRect.left, clientRect.top, clientRect.right, clientRect.bottom);
38+
boundsEnabled = true;
39+
}
40+
41+
void UninstallKeyboardHook() {
42+
if (keyboardHook != NULL) {
43+
UnhookWindowsHookEx(keyboardHook);
44+
keyboardHook = NULL;
45+
}
46+
}
47+
48+
void ToggleCursorBounds() {
49+
if (boundsEnabled) {
50+
ReleaseCursorBounds();
51+
} else {
52+
HWND foregroundWindow = GetForegroundWindow();
53+
if (foregroundWindow != NULL) {
54+
SetCursorBoundsToWindow(foregroundWindow);
55+
}
56+
}
57+
}
58+
59+
void ExitApplication() {
60+
ReleaseCursorBounds();
61+
UninstallKeyboardHook();
62+
PostQuitMessage(0);
63+
}
64+
65+
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
66+
if (nCode == HC_ACTION && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) {
67+
KBDLLHOOKSTRUCT* pKeyboard = (KBDLLHOOKSTRUCT*)lParam;
68+
if (pKeyboard->vkCode == VK_F12 && (GetAsyncKeyState(VK_MENU) & 0x8000)) {
69+
ToggleCursorBounds();
70+
} else if (pKeyboard->vkCode == VK_F11 && (GetAsyncKeyState(VK_MENU) & 0x8000)) {
71+
ExitApplication();
72+
}
73+
}
74+
return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
75+
}
76+
77+
void InstallKeyboardHook() {
78+
keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, 0);
79+
}
80+
81+
void c() {
82+
InstallKeyboardHook();
83+
84+
MSG msg;
85+
while (GetMessage(&msg, NULL, 0, 0)) {
86+
TranslateMessage(&msg);
87+
DispatchMessage(&msg);
88+
}
89+
90+
UninstallKeyboardHook();
91+
}

0 commit comments

Comments
 (0)