How to programmatically prevent linux computer from sleeping or turning on screensaver?

后端 未结 3 1989
既然无缘
既然无缘 2021-02-12 19:25

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 comp

相关标签:
3条回答
  • 2021-02-12 20:06

    I'm using QTDBUS using that

    QDBusConnection bus = QDBusConnection::sessionBus();
    if(bus.isConnected())
    {
        QString services[MAX_SERVICES] = {
            "org.freedesktop.ScreenSaver",
            "org.gnome.SessionManager"
        };
        QString paths[MAX_SERVICES] = {
            "/org/freedesktop/ScreenSaver",
            "/org/gnome/SessionManager"
        };
    
        for(int i = 0; i < MAX_SERVICES ; i++)
        {        
            QDBusInterface screenSaverInterface(
                services[i], paths[i],services[i], bus, this);
            if (!screenSaverInterface.isValid())
                continue;       
    
            QDBusReply<uint> reply = screenSaverInterface.call(
                "Inhibit", "YOUR_APP_NAME", "REASON");
            if (reply.isValid())
            {
                cookieID = reply.value();
                qDebug()<<"succesful"
            } else {   
                QDBusError error =reply.error();
                qDebug()<<error.message()<<error.name();   
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-12 20:09

    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:

    • Disable it for the duration of the program:
      • Using XScreenSaverSuspend
      • Using DBus, calling org.freedesktop.ScreenSaver.Inhibit
    • Ping it periodically:
      • Using XResetScreenSaver
      • Using DBus, calling org.freedesktop.ScreenSaver.SimulateUserActivity
    0 讨论(0)
  • 2021-02-12 20:17

    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...

    0 讨论(0)
提交回复
热议问题