如何以编程方式阻止linux计算机休眠或打开屏幕保护程序?

时间:2021-09-04 18:10:54

While developing a small cross-platform game on C++, I got stuck with following issue: when players are playing with a USB gamepad without touching a keyboard or mouse, the computer sleeps automatically while they're playing.

当我在c++开发一个小型跨平台游戏时,我遇到了以下问题:当玩家在玩USB游戏时,如果不碰键盘或鼠标,电脑就会自动休眠。

In Windows, it can be done easily using SetThreadExecutionState function. In OS X, I think it can be done with UpdateSystemActivity but not tested yet.

在Windows中,使用SetThreadExecutionState函数很容易实现。在OS X中,我认为它可以通过UpdateSystemActivity完成,但还没有经过测试。

But the problem is, in Linux there's nothing like a common API between DE's. I've found that in Gnome you can stop the auto-suspending by using DBus calls Inhibit() and Uninhibit(), but it works only for Gnome.

但问题是,在Linux中,没有什么比DE之间的通用API更好的了。我发现在Gnome中,可以使用DBus调用禁止()和un禁止()来停止自动挂起,但它只适用于Gnome。

So, is there exists a programatically cross-DE way (or non-DE way, for example if user is using something like dwm or awesome wm) to prevent a Linux system (using Xorg and without root access of course) from sleeping or turning on screensaver without changing user configuration files?

那么,是否存在一种程序上的交叉方式(或者非交叉方式,例如,如果用户正在使用dwm或awesome wm之类的东西)来防止Linux系统(使用Xorg,当然没有root权限)在不更改用户配置文件的情况下休眠或打开屏幕保护程序?

PS: Don't think that it's too complicated, but don't know how unfortunately.

附注:不要认为它太复杂,但不要知道有多不幸。

2 个解决方案

#1


7  

From a quick look at how mplayer and SDL do it, there are two things you can do to prevent the screensaver from firing up:

从快速看mplayer和SDL如何做到这一点,你可以做两件事来阻止屏幕保护程序的启动:

  • Disable it for the duration of the program:
    • Using XScreenSaverSuspend
    • 使用XScreenSaverSuspend
    • Using DBus, calling org.freedesktop.ScreenSaver.Inhibit
    • 使用DBus org.freedesktop.ScreenSaver.Inhibit打电话
  • 在程序期间禁用它:使用DBus使用XScreenSaverSuspend,调用org. freedesktop.screensaver .禁止。
  • Ping it periodically:
    • Using XResetScreenSaver
    • 使用XResetScreenSaver
    • Using DBus, calling org.freedesktop.ScreenSaver.SimulateUserActivity
    • 使用DBus org.freedesktop.ScreenSaver.SimulateUserActivity打电话
  • 定期的Ping:使用DBus的XResetScreenSaver,调用org.freedesktop.ScreenSaver.SimulateUserActivity。

#2


2  

As far as I can tell, things with xdg in the name are the way to go for cross-desktop-environment functionality. There appears to be a commandline utility called xdg-screensaver. It seems to have a bunch of screensavers hardwired and then fall back to xset s off/xset s default, so you might want to just call it when it's installed, or fall back to copying part of its logic when it's not...

据我所知,名称中带有xdg的东西是实现跨平台桌面环境功能的途径。似乎有一个命令行实用程序称为xdgg -screensaver。它似乎有一堆屏幕保护程序,然后又回到xset off/xset s的默认值,所以您可能只想在安装时调用它,或者在不安装时复制它的部分逻辑……

#1


7  

From a quick look at how mplayer and SDL do it, there are two things you can do to prevent the screensaver from firing up:

从快速看mplayer和SDL如何做到这一点,你可以做两件事来阻止屏幕保护程序的启动:

  • Disable it for the duration of the program:
    • Using XScreenSaverSuspend
    • 使用XScreenSaverSuspend
    • Using DBus, calling org.freedesktop.ScreenSaver.Inhibit
    • 使用DBus org.freedesktop.ScreenSaver.Inhibit打电话
  • 在程序期间禁用它:使用DBus使用XScreenSaverSuspend,调用org. freedesktop.screensaver .禁止。
  • Ping it periodically:
    • Using XResetScreenSaver
    • 使用XResetScreenSaver
    • Using DBus, calling org.freedesktop.ScreenSaver.SimulateUserActivity
    • 使用DBus org.freedesktop.ScreenSaver.SimulateUserActivity打电话
  • 定期的Ping:使用DBus的XResetScreenSaver,调用org.freedesktop.ScreenSaver.SimulateUserActivity。

#2


2  

As far as I can tell, things with xdg in the name are the way to go for cross-desktop-environment functionality. There appears to be a commandline utility called xdg-screensaver. It seems to have a bunch of screensavers hardwired and then fall back to xset s off/xset s default, so you might want to just call it when it's installed, or fall back to copying part of its logic when it's not...

据我所知,名称中带有xdg的东西是实现跨平台桌面环境功能的途径。似乎有一个命令行实用程序称为xdgg -screensaver。它似乎有一堆屏幕保护程序,然后又回到xset off/xset s的默认值,所以您可能只想在安装时调用它,或者在不安装时复制它的部分逻辑……