Skip to content

Commit 761e5d5

Browse files
committed
update: add debounce
1 parent 825e228 commit 761e5d5

5 files changed

Lines changed: 40 additions & 14 deletions

File tree

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SortIncludes: false

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ FetchContent_Declare(iniparser
1414
FetchContent_MakeAvailable(iniparser)
1515

1616
add_library(RCOBJ OBJECT src/resource.rc)
17-
add_executable(BrightnessControl WIN32 src/app.cpp $<TARGET_OBJECTS:RCOBJ>)
17+
add_executable(BrightnessControl WIN32 src/app.cpp src/utils.cpp $<TARGET_OBJECTS:RCOBJ>)
1818
target_link_libraries(BrightnessControl PRIVATE comctl32 uuid inicpp)

src/app.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
#include <windows.h>
2-
#include <commctrl.h>
1+
#include <cstdint>
32
#include <cstdio>
43
#include <cstdlib>
5-
#include <fileapi.h>
64
#include <fstream>
7-
#include <inicpp.h>
5+
6+
#include <windows.h>
7+
#include <commctrl.h>
8+
#include <fileapi.h>
89
#include <minwindef.h>
910
#include <powrprof.h>
1011

12+
#include <inicpp.h>
13+
14+
#include "utils.hpp"
15+
1116
#define APP_ICON_RESOURCE 100
17+
#define DEBOUNCE_DURATION 1000
1218

1319
NOTIFYICONDATA nid;
1420
HWND hSliderWindow, hSlider, hTextLabel;
1521
HHOOK hMouseHook;
1622
HPOWERNOTIFY hPowerNotify;
23+
1724
bool isVisible = false;
1825
std::string rwPath;
26+
uint64_t lastChangeBrightness = getMillis();
27+
int lastBrightness = 50;
1928

2029
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
2130
LRESULT CALLBACK SliderWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
@@ -159,6 +168,11 @@ void updateText() {
159168
* Change brightness by sending data to RWEveryting
160169
*/
161170
void changeBrightness() {
171+
int brightness = SendMessage(hSlider, TBM_GETPOS, 0, 0);
172+
uint64_t currentMillis = getMillis();
173+
if (lastBrightness == brightness || currentMillis - lastChangeBrightness < DEBOUNCE_DURATION)
174+
return;
175+
162176
char tempDirPath[MAX_PATH];
163177
char tempFileName[MAX_PATH];
164178

@@ -169,7 +183,6 @@ void changeBrightness() {
169183
return;
170184

171185
char buffer[20];
172-
int brightness = SendMessage(hSlider, TBM_GETPOS, 0, 0);
173186
std::snprintf(buffer, 20, "mul(Local4, %d)", brightness);
174187

175188
std::ofstream file(tempFileName);
@@ -195,14 +208,15 @@ void changeBrightness() {
195208
}
196209

197210
DeleteFile(tempFileName);
211+
lastChangeBrightness = getMillis();
198212
}
199213

200214
/**
201215
* Check if the display state changed to on (0x1)
202216
*
203-
* @param {WPARAM} wParam :
204-
* @param {LPARAM} lParam :
205-
* @return {bool} :
217+
* @param {WPARAM} wParam :
218+
* @param {LPARAM} lParam :
219+
* @return {bool} :
206220
*/
207221
bool isDisplayOn(WPARAM wParam, LPARAM lParam) {
208222
return wParam == PBT_POWERSETTINGCHANGE &&
@@ -220,11 +234,10 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
220234
} else if (LOWORD(lParam) == WM_RBUTTONUP)
221235
showContextMenu(hwnd);
222236

223-
} else if (uMsg == WM_POWERBROADCAST &&
224-
(wParam == PBT_APMRESUMEAUTOMATIC ||
225-
wParam == PBT_APMRESUMESUSPEND ||
226-
wParam == PBT_APMPOWERSTATUSCHANGE ||
227-
isDisplayOn(wParam, lParam)))
237+
} else if (uMsg == WM_POWERBROADCAST && (wParam == PBT_APMRESUMEAUTOMATIC ||
238+
wParam == PBT_APMRESUMESUSPEND ||
239+
wParam == PBT_APMPOWERSTATUSCHANGE ||
240+
isDisplayOn(wParam, lParam)))
228241
changeBrightness();
229242

230243
else if (uMsg == WM_DESTROY || (uMsg == WM_COMMAND && LOWORD(wParam) == 1002))

src/utils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <chrono>
2+
#include <cstdint>
3+
4+
uint64_t getMillis() {
5+
using namespace std::chrono;
6+
return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
7+
.count();
8+
}

src/utils.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include <cstdint>
3+
4+
uint64_t getMillis();

0 commit comments

Comments
 (0)