Skip to content

Instantly share code, notes, and snippets.

@psyomn
Last active February 8, 2021 21:23
Show Gist options
  • Save psyomn/ba786f27dc97dbf340c8b671e1aa96e8 to your computer and use it in GitHub Desktop.
Save psyomn/ba786f27dc97dbf340c8b671e1aa96e8 to your computer and use it in GitHub Desktop.
usage of win32api along with linkage instructions in the cmake
#include <iostream>
#include <chrono>
#include <thread>
// windows specific screensaver stuff
// NB: you will need to download the windows sdk for this to work
// If you're running under cygwin, you should know that the headers cygwin provides for windows api development might
// not be the ones you're looking for (for example powerbase.h is not provided by cygwin to my knowledge).
// I got this to compile under ms visual studio.
#include <windows.h>
#include <powerbase.h>
class Sleepless {
public:
static void Enable() {
// this is for screensaver
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
// this is for power saving modes
SYSTEM_POWER_POLICY powerPolicy;
DWORD ret;
DWORD size = sizeof(SYSTEM_POWER_POLICY);
ret = CallNtPowerInformation(SystemPowerPolicyAc, nullptr, 0, &powerPolicy, size);
if ((ret != ERROR_SUCCESS) || (size != sizeof(SYSTEM_POWER_POLICY))) {
std::cout << "error calling nt power information" << std::endl;
return;
}
powerPolicy.VideoTimeout = 1000;
ret = CallNtPowerInformation(SystemPowerPolicyAc, &powerPolicy, size, nullptr, 0);
if ((ret != ERROR_SUCCESS))
{
std::cout << "problem doing thing" << std::endl;
}
}
static void Disable() {
SetThreadExecutionState(ES_CONTINUOUS);
}
private:
};
int main(int argc, char* argv[]) {
Sleepless sleepless;
std::cout << "disable sleeping" << std::endl;
sleepless.Enable();
std::this_thread::sleep_for(std::chrono::minutes(10));
std::cout << "enable sleeping" << std::endl;
sleepless.Disable();
return 0;
}
cmake_minimum_required (VERSION 3.8)
# Add source to this project's executable.
add_executable (cc_examples "cc_examples.cpp" "cc_examples.h")
target_link_libraries(cc_examples PowrProf.dll)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment