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

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

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