如何以编程方式防止linux计算机休眠或打开屏保? [英] How to programmatically prevent linux computer from sleeping or turning on screensaver?

查看:60
本文介绍了如何以编程方式防止linux计算机休眠或打开屏保?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++上开发小型跨平台游戏时,我遇到了以下问题:当玩家在不触摸键盘或鼠标的情况下使用USB游戏板进行游戏时,计算机在游戏时会自动进入睡眠状态.

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.

在Windows中,可以使用 SetThreadExecutionState 函数.我认为在OS X中,可以使用 UpdateSystemActivity ,但尚未测试.

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.

但是问题是,在Linux中,没有什么像DE之间的通用API一样.我发现在Gnome中,您可以通过使用DBus调用 Inhibit() Uninhibit()来停止自动暂停,但这仅适用于Gnome.

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.

因此,是否存在以编程方式进行跨DE的方式(或非DE的方式),例如,如果用户使用的是 dwm awesome wm )防止 Linux 系统(使用 Xorg 并且没有root访问权限)在不更改用户配置文件的情况下休眠或打开屏幕保护程序?

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?

PS:不要以为它太复杂了,但是不知道它是多么不幸.

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

推荐答案

通过快速查看mplayer和SDL如何做到这一点,您可以执行两项操作来防止屏幕保护程序启动:

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:

  • 在程序运行期间将其禁用:
    • 使用 XScreenSaverSuspend
    • 使用DBus,调用 org.freedesktop.ScreenSaver.Inhibit
    • 使用 XResetScreenSaver
    • 使用DBus,调用 org.freedesktop.ScreenSaver.SimulateUserActivity

    这篇关于如何以编程方式防止linux计算机休眠或打开屏保?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆