| 1 |
/* |
| 2 |
* Copyright (C) 2017-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "OSScreenSaverFreedesktop.h" |
| 10 |
|
| 11 |
#include "CompileInfo.h" |
| 12 |
#include "guilib/LocalizeStrings.h" |
| 13 |
#include "utils/log.h" |
| 14 |
|
| 15 |
#include "platform/linux/DBusMessage.h" |
| 16 |
#include "platform/linux/DBusUtil.h" |
| 17 |
|
| 18 |
using namespace KODI::WINDOWING::LINUX; |
| 19 |
|
| 20 |
namespace |
| 21 |
{ |
| 22 |
const std::string SCREENSAVER_OBJECT = "/org/freedesktop/ScreenSaver"; |
| 23 |
const std::string SCREENSAVER_INTERFACE = "org.freedesktop.ScreenSaver"; |
| 24 |
} |
| 25 |
|
| 26 |
bool COSScreenSaverFreedesktop::IsAvailable() |
| 27 |
{ |
| 28 |
// Test by making a call to a function without side-effects |
| 29 |
CDBusMessage dummyMessage(SCREENSAVER_INTERFACE, SCREENSAVER_OBJECT, SCREENSAVER_INTERFACE, "GetActive"); |
| 30 |
CDBusError error; |
| 31 |
dummyMessage.SendSession(error); |
| 32 |
// We do not care whether GetActive() is actually supported, we're just checking for the name to be there |
| 33 |
// (GNOME for example does not support GetActive) |
| 34 |
return !error || error.Name() == DBUS_ERROR_NOT_SUPPORTED; |
| 35 |
} |
| 36 |
|
| 37 |
void COSScreenSaverFreedesktop::Inhibit() |
| 38 |
{ |
| 39 |
if (m_inhibited) |
| 40 |
{ |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
CDBusMessage inhibitMessage(SCREENSAVER_INTERFACE, SCREENSAVER_OBJECT, SCREENSAVER_INTERFACE, "Inhibit"); |
| 45 |
inhibitMessage.AppendArguments(std::string(CCompileInfo::GetAppName()), |
| 46 |
g_localizeStrings.Get(14086)); |
| 47 |
if (!inhibitMessage.SendSession()) |
| 48 |
{ |
| 49 |
// DBus call failed |
| 50 |
CLog::Log(LOGERROR, "Inhibiting freedesktop screen saver failed"); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if (!inhibitMessage.GetReplyArguments(&m_cookie)) |
| 55 |
{ |
| 56 |
CLog::Log(LOGERROR, "Could not retrieve cookie from org.freedesktop.ScreenSaver Inhibit response"); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
m_inhibited = true; |
| 61 |
} |
| 62 |
|
| 63 |
void COSScreenSaverFreedesktop::Uninhibit() |
| 64 |
{ |
| 65 |
if (!m_inhibited) |
| 66 |
{ |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
CDBusMessage uninhibitMessage(SCREENSAVER_INTERFACE, SCREENSAVER_OBJECT, SCREENSAVER_INTERFACE, "UnInhibit"); |
| 71 |
uninhibitMessage.AppendArgument(m_cookie); |
| 72 |
if (!uninhibitMessage.SendSession()) |
| 73 |
{ |
| 74 |
CLog::Log(LOGERROR, "Uninhibiting freedesktop screen saver failed"); |
| 75 |
} |
| 76 |
|
| 77 |
m_inhibited = false; |
| 78 |
} |