Skip to content

Commit 1a66a28

Browse files
committed
[U] 002
1 parent f6d390e commit 1a66a28

File tree

5 files changed

+91
-67
lines changed

5 files changed

+91
-67
lines changed

build/c.ico

-1.12 KB
Binary file not shown.

build/gl_build.bat

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pushd "%~dp0\..\src" || (
2525
)
2626

2727
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 || (
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 -lkernel32 -luser32 -lmsvcrt -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 || (
2929
echo Compilation failed.
3030
pause
3131
popd
@@ -40,6 +40,9 @@ popd || (
4040
exit /b 1
4141
)
4242

43+
REM Delete resource compilation output
44+
del resources.o
45+
4346
echo Success! CursorRestrict.exe has been generated under the bin folder.
4447
pause
4548

build/resources.rc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "version.h"
22

3-
IDI_ICON1 ICON "c.ico"
4-
53
1 VERSIONINFO
64
FILEVERSION VER_FILE_VERSION
75
PRODUCTVERSION VER_PRODUCT_VERSION

build/version.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#define VERSION_UNSAFE 0
55

66
#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"
7+
#define VER_FILEDESCRIPTION_STR "A program to restrict your cursor from moving out of the game window.(Shift+F11 toggle,Shift+F12 exit)"
8+
#define VER_INTERNALNAME_STR "Open Source Software"
9+
#define VER_PRODUCTNAME_STR "Open Source Software"
1010
#define VER_LEGALCOPYRIGHT_STR "Open Source Software"
1111
#define VER_LEGALTRADEMARKS1_STR ""
1212
#define VER_LEGALTRADEMARKS2_STR ""

src/CursorRestrict.c

Lines changed: 84 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,114 @@
11
#include <Windows.h>
22
#include <stdbool.h>
33
#include <stdio.h>
4-
// Author: https://github.com/spacemiqote
4+
//Author:https://github.com/spacemiqote
55

6-
HHOOK keyboardHook;
76
RECT cursorBounds;
87
bool boundsEnabled = false;
9-
10-
void SetCursorBounds(int left, int top, int right, int bottom);
8+
void SetCursorBounds(int,int,int,int);
119
void ReleaseCursorBounds(void);
12-
void SetCursorBoundsToWindow(HWND hwnd);
10+
void SetCursorBoundsToWindow(HWND);
1311
void ToggleCursorBounds(void);
1412
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);
13+
void c(HINSTANCE hInstance);
1914

2015
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);
16+
cursorBounds.left = left;
17+
cursorBounds.top = top;
18+
cursorBounds.right = right;
19+
cursorBounds.bottom = bottom;
20+
ClipCursor(&cursorBounds);
2621
}
2722

2823
void ReleaseCursorBounds() {
29-
ClipCursor(NULL);
30-
boundsEnabled = false;
24+
ClipCursor(NULL);
25+
boundsEnabled = false;
3126
}
3227

3328
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-
}
29+
RECT clientRect;
30+
GetClientRect(hwnd, &clientRect);
31+
MapWindowPoints(hwnd, NULL, (LPPOINT)&clientRect, 2);
32+
SetCursorBounds(clientRect.left, clientRect.top, clientRect.right, clientRect.bottom);
33+
boundsEnabled = true;
4634
}
4735

48-
void ToggleCursorBounds() {
49-
if (boundsEnabled) {
50-
ReleaseCursorBounds();
51-
} else {
52-
HWND foregroundWindow = GetForegroundWindow();
53-
if (foregroundWindow != NULL) {
54-
SetCursorBoundsToWindow(foregroundWindow);
55-
}
56-
}
36+
void ToggleCursorBounds(){
37+
if (boundsEnabled){
38+
ReleaseCursorBounds();
39+
}else{
40+
HWND foregroundWindow=GetForegroundWindow();
41+
if(foregroundWindow!=NULL)SetCursorBoundsToWindow(foregroundWindow);
42+
}
5743
}
5844

5945
void ExitApplication() {
60-
ReleaseCursorBounds();
61-
UninstallKeyboardHook();
62-
PostQuitMessage(0);
46+
ReleaseCursorBounds();
47+
PostQuitMessage(0);
6348
}
6449

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);
50+
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
51+
if (uMsg==WM_INPUT){
52+
UINT dwSize=0;
53+
GetRawInputData((HRAWINPUT)lParam,RID_INPUT,NULL,&dwSize,sizeof(RAWINPUTHEADER));
54+
LPBYTE lpb=(LPBYTE)HeapAlloc(GetProcessHeap(),0,dwSize);
55+
if(lpb==NULL)return 0;
56+
if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,lpb,
57+
&dwSize,sizeof(RAWINPUTHEADER))!=dwSize){
58+
HeapFree(GetProcessHeap(),0,lpb);
59+
return 0;
60+
}
61+
RAWINPUT raw;
62+
CopyMemory(&raw,lpb,sizeof(RAWINPUT));
63+
HeapFree(GetProcessHeap(),0,lpb);
64+
if(raw.header.dwType==RIM_TYPEKEYBOARD){
65+
RAWKEYBOARD rawKeyboard=raw.data.keyboard;
66+
USHORT vkCode=rawKeyboard.VKey;
67+
if(rawKeyboard.Message==WM_KEYDOWN){
68+
if(GetAsyncKeyState(VK_SHIFT)<0){
69+
if(vkCode==VK_F11)ToggleCursorBounds();
70+
else if(vkCode==VK_F12)ExitApplication();
71+
}
72+
}
73+
}
74+
}
75+
return DefWindowProc(hwnd, uMsg, wParam, lParam);
7576
}
7677

77-
void InstallKeyboardHook() {
78-
keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, 0);
78+
void InstallRawInputHook(HWND hwnd){
79+
RAWINPUTDEVICE rid[1];
80+
rid[0].usUsagePage=1;
81+
rid[0].usUsage=6;
82+
rid[0].dwFlags=RIDEV_INPUTSINK;
83+
rid[0].hwndTarget=hwnd;
84+
if(!RegisterRawInputDevices(rid, 1, sizeof(rid[0])))return;
7985
}
8086

81-
void c() {
82-
InstallKeyboardHook();
83-
84-
MSG msg;
85-
while (GetMessage(&msg, NULL, 0, 0)) {
86-
TranslateMessage(&msg);
87-
DispatchMessage(&msg);
88-
}
87+
HWND CreateMessageOnlyWindow(HINSTANCE hInstance) {
88+
WNDCLASS wc={0};
89+
wc.lpfnWndProc=WindowProc;
90+
wc.hInstance=hInstance;
91+
wc.lpszClassName="CursorRestrict";
92+
if(!RegisterClass(&wc))return NULL;
93+
HWND hwnd = CreateWindowEx(
94+
0,
95+
"CursorRestrict",
96+
NULL,
97+
0,
98+
0,0,0,0,
99+
HWND_MESSAGE, NULL, hInstance, NULL
100+
);
101+
if(!hwnd)return 0;
102+
return hwnd;
103+
}
89104

90-
UninstallKeyboardHook();
105+
void c(HINSTANCE hInstance){
106+
HWND hwnd=CreateMessageOnlyWindow(hInstance);
107+
if(!hwnd)return;
108+
InstallRawInputHook(hwnd);
109+
MSG msg;
110+
while (GetMessage(&msg, NULL, 0, 0)) {
111+
TranslateMessage(&msg);
112+
DispatchMessage(&msg);
113+
}
91114
}

0 commit comments

Comments
 (0)