Skip to content

Commit

Permalink
Inhibit screensaver when game is running (#426)
Browse files Browse the repository at this point in the history
Former-commit-id: 1cb7b0a
  • Loading branch information
tkashkin committed Aug 24, 2020
1 parent 03e394b commit fd10049
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
4 changes: 4 additions & 0 deletions data/com.github.tkashkin.gamehub.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@
<default>true</default>
<summary>Import tags</summary>
</key>
<key name="inhibit-screensaver" type="b">
<default>true</default>
<summary>Inhibit screensaver while game is running</summary>
</key>
</schema>

<!-- Auth -->
Expand Down
2 changes: 2 additions & 0 deletions src/data/runnables/traits/HasExecutableFile.vala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ namespace GameHub.Data.Runnables.Traits
if(!can_be_launched(true)) return;

Runnable.IsAnyRunnableRunning = is_running = true;
Inhibitor.inhibit(_("%s is running").printf(name));
update_status();

yield pre_run();
Expand All @@ -129,6 +130,7 @@ namespace GameHub.Data.Runnables.Traits

Timeout.add_seconds(1, () => {
Runnable.IsAnyRunnableRunning = is_running = false;
Inhibitor.uninhibit();
update_status();
return Source.REMOVE;
});
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ sources = [
'utils/Parser.vala',
'utils/BinaryVDF.vala',
'utils/SignalRateLimiter.vala',
'utils/Inhibitor.vala',

'settings/Settings.vala',
'settings/UI.vala',
Expand Down
1 change: 1 addition & 0 deletions src/settings/UI.vala
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ namespace GameHub.Settings.UI
public bool grid_doubleclick { get; set; }
public bool merge_games { get; set; }
public bool import_tags { get; set; }
public bool inhibit_screensaver { get; set; }

public Behavior()
{
Expand Down
4 changes: 4 additions & 0 deletions src/ui/dialogs/SettingsDialog/pages/ui/Behavior.vala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Pages.UI
var sgrp_tags = new SettingsGroup(_("Tags"));
sgrp_tags.add_setting(new SwitchSetting.bind(_("Import tags from sources"), null, settings, "import-tags"));
add_widget(sgrp_tags);

var sgrp_system = new SettingsGroup(_("System"));
sgrp_system.add_setting(new SwitchSetting.bind(_("Inhibit screensaver while game is running"), _("Request session manager to prevent suspending while game is running"), settings, "inhibit-screensaver"));
add_widget(sgrp_system);
}
}
}
100 changes: 100 additions & 0 deletions src/utils/Inhibitor.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
This file is part of GameHub.
Copyright (C) 2018-2019 Anatoliy Kashkin
GameHub is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GameHub is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GameHub. If not, see <https://www.gnu.org/licenses/>.
*/

using Gtk;

namespace GameHub.Utils
{
public class Inhibitor
{
private static DBusFreeDesktopScreenSaver? fd_screensaver = null;

private static uint32? fd_screensaver_inhibit_id = null;
private static uint? gtk_inhibit_id = null;

private static void dbus_connect()
{
if(fd_screensaver != null) return;
try
{
fd_screensaver = Bus.get_proxy_sync(BusType.SESSION, "org.freedesktop.ScreenSaver", "/ScreenSaver", DBusProxyFlags.NONE);
}
catch(Error e)
{
warning("[ScreenSaver.dbus_connect] Failed to connect to DBus: %s", e.message);
}
}

public static void inhibit(string? reason = null)
{
if(!GameHub.Settings.UI.Behavior.instance.inhibit_screensaver) return;
reason = reason ?? _("Game is running");
if(fd_screensaver_inhibit_id == null)
{
dbus_connect();
if(fd_screensaver != null)
{
try
{
fd_screensaver_inhibit_id = fd_screensaver.inhibit("GameHub", reason);
}
catch(Error e)
{
warning("[ScreenSaver.inhibit] Failed to inhibit screensaver via DBus: %s", e.message);
}
}
}
if(gtk_inhibit_id == null)
{
gtk_inhibit_id = GameHub.Application.instance.inhibit(GameHub.Application.instance.active_window, ApplicationInhibitFlags.IDLE | ApplicationInhibitFlags.SUSPEND, reason);
}
}

public static void uninhibit()
{
if(fd_screensaver_inhibit_id != null)
{
dbus_connect();
if(fd_screensaver != null)
{
try
{
fd_screensaver.un_inhibit(fd_screensaver_inhibit_id);
fd_screensaver_inhibit_id = null;
}
catch(Error e)
{
warning("[ScreenSaver.uninhibit] Failed to uninhibit screensaver via DBus: %s", e.message);
}
}
}
if(gtk_inhibit_id != null)
{
GameHub.Application.instance.uninhibit(gtk_inhibit_id);
gtk_inhibit_id = null;
}
}

[DBus(name = "org.freedesktop.ScreenSaver")]
private interface DBusFreeDesktopScreenSaver: Object
{
public abstract uint32 inhibit(string app_name, string reason) throws Error;
public abstract void un_inhibit(uint32 cookie) throws Error;
}
}
}

0 comments on commit fd10049

Please sign in to comment.