diff options
Diffstat (limited to 'kdesktop/dbus/screensaver')
-rw-r--r-- | kdesktop/dbus/screensaver/CMakeLists.txt | 47 | ||||
-rw-r--r-- | kdesktop/dbus/screensaver/dbusscreensaverservice.cpp | 168 | ||||
-rw-r--r-- | kdesktop/dbus/screensaver/dbusscreensaverservice.h | 127 | ||||
-rw-r--r-- | kdesktop/dbus/screensaver/screensaverInterfaceImpl.cpp | 167 | ||||
-rw-r--r-- | kdesktop/dbus/screensaver/screensaverInterfaceImpl.h | 136 |
5 files changed, 645 insertions, 0 deletions
diff --git a/kdesktop/dbus/screensaver/CMakeLists.txt b/kdesktop/dbus/screensaver/CMakeLists.txt new file mode 100644 index 000000000..7d21e2f83 --- /dev/null +++ b/kdesktop/dbus/screensaver/CMakeLists.txt @@ -0,0 +1,47 @@ +################################################# +# +# (C) 2024 Emanoil Kotsev +# deloptes (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_BINARY_DIR}/kdesktop + ${TQT_INCLUDE_DIRS} + ${TDE_INCLUDE_DIR} + ${DBUS_TQT_INCLUDE_DIRS} +) + +set( ScreenSaver_HDRS dbusbaseNode.h introspectableInterface.h screensaverInterface.h screensaverNode.h screensaverProxy.h) +set( ScreenSaver_SRCS dbusbaseNode.cpp introspectableInterface.cpp screensaverInterface.cpp screensaverNode.cpp screensaverProxy.cpp ) + +##### DbusScreenSaver ######################### +add_custom_command( + OUTPUT ${ScreenSaver_HDRS} ${ScreenSaver_SRCS} + COMMAND ${DBUSXML2QT3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/kdesktop/dbus/interfaces/org.freedesktop.ScreenSaver.xml 2>/dev/null + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/kdesktop/dbus/screensaver +) + +##### dbusscreensaverservice #################### + +set( target dbusscreensaverservice ) + +set( ${target}_SRCS + dbusscreensaverservice.cpp screensaverInterfaceImpl.cpp +) + +tde_add_library( ${target} STATIC_PIC AUTOMOC + SOURCES ${${target}_SRCS} ${ScreenSaver_SRCS} + LINK ${DBUS_TQT_LIBRARIES} +) + +##### headers ################################### + +install( FILES dbusscreensaverservice.h screensaverInterfaceImpl.h + DESTINATION ${INCLUDE_INSTALL_DIR}/kdesktop/dbus +) diff --git a/kdesktop/dbus/screensaver/dbusscreensaverservice.cpp b/kdesktop/dbus/screensaver/dbusscreensaverservice.cpp new file mode 100644 index 000000000..651890d87 --- /dev/null +++ b/kdesktop/dbus/screensaver/dbusscreensaverservice.cpp @@ -0,0 +1,168 @@ +/* + * dbusscreensaverservice.cpp + * + * (C) 2024 Emanoil Kotsev + * deloptes (AT) gmail.com + * + * tdebase Copyright (C) 2009 tdebase development team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <tdelocale.h> +#include <tqdbusobjectpath.h> + +#include "dbusscreensaverservice.h" + +RootNodeService::RootNodeService(TQT_DBusConnection &connection) : + DBusBaseNode(), m_connection(connection) +{ + addChildNode("org"); + registerObject(m_connection, "/"); +} + +RootNodeService::~RootNodeService() +{ +} + +TQT_DBusObjectBase* RootNodeService::createInterface(const TQString& interfaceName) +{ + return (TQT_DBusObjectBase*) m_interfaces[interfaceName]; +} + +OrgNodeService::OrgNodeService(TQT_DBusConnection &connection) : + DBusBaseNode(), m_connection(connection) +{ + addChildNode("freedesktop"); + registerObject(m_connection, "/org"); +} + +OrgNodeService::~OrgNodeService() +{ +} + +TQT_DBusObjectBase* OrgNodeService::createInterface(const TQString& interfaceName) +{ + return (TQT_DBusObjectBase*) m_interfaces[interfaceName]; +} + +FreeDesktopNodeService::FreeDesktopNodeService(TQT_DBusConnection &connection) : + DBusBaseNode(), m_connection(connection) +{ + addChildNode("ScreenSaver"); + registerObject(m_connection, "/org/freedesktop"); +} + +FreeDesktopNodeService::~FreeDesktopNodeService() +{ +} + +TQT_DBusObjectBase* FreeDesktopNodeService::createInterface(const TQString& interfaceName) +{ + return (TQT_DBusObjectBase*) m_interfaces[interfaceName]; +} + +ScreenSaverService::ScreenSaverService(TQT_DBusConnection &conn) : + org::freedesktop::screensaverNode(), + m_connection(conn), + screenSaverInterface(new ScreenSaverInterfaceImpl(conn)) +{ + m_interfaces.insert("org.freedesktop.DBus.Introspectable", this); + m_interfaces.insert("org.freedesktop.ScreenSaver", screenSaverInterface); + registerObject(m_connection, DBUS_SCREENSAVER_SERVICE_PATH); +} + +ScreenSaverService::~ScreenSaverService() +{ + if(screenSaverInterface) + { + screenSaverInterface->restoreState(); + delete screenSaverInterface; + } +} + +TQT_DBusObjectBase* ScreenSaverService::createInterface(const TQString& interfaceName) +{ + return (TQT_DBusObjectBase*) m_interfaces[interfaceName]; +} + + + +TDEDbusScreenSaver::TDEDbusScreenSaver() +{ + // open connection to DBus and configure the service + if (!configureService()) + { + tqDebug("Failed to configure the screen saver service"); + } +} + +TDEDbusScreenSaver::~TDEDbusScreenSaver() +{ + // unconfigure the DBus service and close connection + if (!unconfigureService()) + { + tqDebug("Failed to properly close the screen saver service"); + } + + delete screenSaverService; + delete freeDesktopNodeService; + delete orgService; + delete rootService; +} + +bool TDEDbusScreenSaver::isConnectedToDBUS() +{ + return m_connection.isConnected(); +} + +bool TDEDbusScreenSaver::configureService() +{ + m_connection = TQT_DBusConnection::addConnection(TQT_DBusConnection::SessionBus, DBUS_SCREENSAVER_SERVICE); + + if (!m_connection.isConnected()) + { + tqDebug(i18n("Failed to open connection to system message bus: %1").arg(m_connection.lastError().message())); + return false; + } + + // try to get a specific service name + if (!m_connection.requestName(DBUS_SCREENSAVER_SERVICE_NAME)) + { + tqWarning(i18n("Requesting name %1 failed. " + "The object will only be addressable through unique name '%2'").arg( + DBUS_SCREENSAVER_SERVICE_NAME).arg(m_connection.uniqueName())); + return false; + } + + rootService = new RootNodeService(m_connection); + orgService = new OrgNodeService(m_connection); + freeDesktopNodeService = new FreeDesktopNodeService(m_connection); + screenSaverService = new ScreenSaverService(m_connection); + return true; +} + +bool TDEDbusScreenSaver::unconfigureService() +{ + screenSaverService->screenSaverInterface->restoreState(); // will restore the original state + + screenSaverService=nullptr; + freeDesktopNodeService=nullptr; + orgService=nullptr; + rootService=nullptr; + // close D-Bus connection + m_connection.closeConnection(DBUS_SCREENSAVER_SERVICE); + + return true; +} diff --git a/kdesktop/dbus/screensaver/dbusscreensaverservice.h b/kdesktop/dbus/screensaver/dbusscreensaverservice.h new file mode 100644 index 000000000..4f4feb7ec --- /dev/null +++ b/kdesktop/dbus/screensaver/dbusscreensaverservice.h @@ -0,0 +1,127 @@ +/* + * dbusscreensaverservice.h + * + * (C) 2024 Emanoil Kotsev + * deloptes (AT) gmail.com + * + * tdebase Copyright (C) 2009 tdebase development team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef KDESKTOP_LOCK_DBUS_SCREENSAVER_DBUSSCREENSAVERSERVICE_H_ +#define KDESKTOP_LOCK_DBUS_SCREENSAVER_DBUSSCREENSAVERSERVICE_H_ + +#include <tqmap.h> +#include <tqdbusconnection.h> + +#include "screensaverNode.h" +#include "dbusbaseNode.h" +#include "screensaverInterfaceImpl.h" + +class ScreenSaverService: public org::freedesktop::screensaverNode +{ + friend class TDEDbusScreenSaver; + +public: + ScreenSaverService(TQT_DBusConnection&); + virtual ~ScreenSaverService(); + +protected: + virtual TQT_DBusObjectBase* createInterface(const TQString&); + ScreenSaverInterfaceImpl *screenSaverInterface; + +private: + TQMap<TQString, TQT_DBusObjectBase*> m_interfaces; + TQT_DBusConnection m_connection; +}; + +class RootNodeService: public DBusBaseNode +{ +public: + RootNodeService(TQT_DBusConnection&); + virtual ~RootNodeService(); +protected: + virtual TQT_DBusObjectBase* createInterface(const TQString&); +private: + TQMap<TQString, TQT_DBusObjectBase*> m_interfaces; + TQT_DBusConnection m_connection; +}; + +class OrgNodeService: public DBusBaseNode +{ +public: + OrgNodeService(TQT_DBusConnection&); + virtual ~OrgNodeService(); +protected: + virtual TQT_DBusObjectBase* createInterface(const TQString&); +private: + TQMap<TQString, TQT_DBusObjectBase*> m_interfaces; + TQT_DBusConnection m_connection; +}; + +class FreeDesktopNodeService: public DBusBaseNode +{ +public: + FreeDesktopNodeService(TQT_DBusConnection&); + virtual ~FreeDesktopNodeService(); +protected: + virtual TQT_DBusObjectBase* createInterface(const TQString&); +private: + TQMap<TQString, TQT_DBusObjectBase*> m_interfaces; + TQT_DBusConnection m_connection; +}; + +class TDEDbusScreenSaver +{ + +public: + TDEDbusScreenSaver(); + virtual ~TDEDbusScreenSaver(); + + /*! + * This function return information about connection status to the DBUS daemon. + * \return boolean with the state of the connection to D-Bus + * \retval true if connected + * \retval false if disconnected + */ + bool isConnectedToDBUS(); + +private: + /*! + * This function initialize the connection to the D-Bus daemon. + * \return boolean with the result of the operation + * \retval true if successfully configured D-Bus connection + * \retval false if unsuccessful + */ + bool configureService(); + + /*! + * This function is to close the connection to D-Bus + * \return boolean with the result of the operation + * \retval true if successfully unset D-Bus connection + * \retval false if unsuccessful + */ + bool unconfigureService(); + +private: + TQT_DBusConnection m_connection; + + RootNodeService *rootService; + OrgNodeService *orgService; + FreeDesktopNodeService *freeDesktopNodeService; + ScreenSaverService *screenSaverService; + +}; + +#endif /* KDESKTOP_LOCK_DBUS_SCREENSAVER_DBUSSCREENSAVERSERVICE_H_ */ diff --git a/kdesktop/dbus/screensaver/screensaverInterfaceImpl.cpp b/kdesktop/dbus/screensaver/screensaverInterfaceImpl.cpp new file mode 100644 index 000000000..e4d7c7064 --- /dev/null +++ b/kdesktop/dbus/screensaver/screensaverInterfaceImpl.cpp @@ -0,0 +1,167 @@ +/* + * screensaverInterfaceImpl.cpp + * + * + * (C) 2024 Emanoil Kotsev + * deloptes (AT) gmail.com + * + * tdebase Copyright (C) 2009 tdebase development team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "screensaverInterfaceImpl.h" + +ScreenSaverInterfaceImpl::ScreenSaverInterfaceImpl(TQT_DBusConnection &conn) : + m_connection(&conn), + m_ccount(1), + m_kdesktopdcoprefobj("kdesktop", "KScreensaverIface") +{ + isScreenSaverEnabled = screenSaverIsEnabled(); + tqDebug(TQString("ScreenSaver isEnabled = %1").arg((isScreenSaverEnabled)?"yes":"no")); +} + +ScreenSaverInterfaceImpl::~ScreenSaverInterfaceImpl() +{ +} + +void ScreenSaverInterfaceImpl::restoreState() +{ + // restore the state + forceScreenSaver(isScreenSaverEnabled); +} + +/*! + * Implement virtual methods + * + */ +void ScreenSaverInterfaceImpl::handleMethodReply(const TQT_DBusMessage& reply) +{ + m_connection->send(reply); +} + +bool ScreenSaverInterfaceImpl::handleSignalSend(const TQT_DBusMessage& reply) +{ + return true; +} + +TQString ScreenSaverInterfaceImpl::objectPath() const +{ + return TQString(DBUS_SCREENSAVER_SERVICE_PATH); +} + +bool ScreenSaverInterfaceImpl::Lock(TQT_DBusError& dbuserror) { + + DCOPReply reply = m_kdesktopdcoprefobj.call("lock"); + if (!reply.isValid()) + { + TQString e("ScreenSaverInterfaceImpl::Lock: there was some error using DCOP."); + tqDebug(e); + dbuserror = TQT_DBusError::stdFailed(e); + return false; + } + return true; +} + +bool ScreenSaverInterfaceImpl::SetActive(bool& arg0, bool e, TQT_DBusError& dbuserror) { + + DCOPReply reply = m_kdesktopdcoprefobj.call("setBlankOnly",e); + if (!reply.isValid()) + { + arg0 = false; + TQString err("ScreenSaverInterfaceImpl::Lock: there was some error using DCOP."); + tqDebug(err); + dbuserror = TQT_DBusError::stdFailed(err); + return false; + } + arg0 = true; + return true; +} + +bool ScreenSaverInterfaceImpl::Inhibit(const TQString& application_name, const TQString& reason_for_inhibit, TQ_UINT32& cookie, TQT_DBusError& dbuserror) { + + //this is to make sure we have the actual state - it could have been changed meanwhile + //however some other application like kplayer may have disabled the screensaver + //when we call this function + //isScreenSaverEnabled = screenSaverIsEnabled(); + if (isScreenSaverEnabled && m_cookies.isEmpty()) // disable only once + { + if (!forceScreenSaver(false)) + { + dbuserror = TQT_DBusError::stdFailed(TQString("Failed to disable the screen saver")); + } + } + + cookie=m_ccount++; + ScreenSaverInterfaceImpl::Pair v; + v.name = application_name; + v.value = reason_for_inhibit; + m_cookies[cookie] = v; + tqDebug(TQString("Inhibit: cookie(%1), application(%2), reason(%3)") + .arg(cookie) + .arg(m_cookies[cookie].name) + .arg(m_cookies[cookie].value).local8Bit()); + + return true; +} + +bool ScreenSaverInterfaceImpl::UnInhibit(TQ_UINT32 cookie, TQT_DBusError& dbuserror) { + tqDebug(TQString("UnInhibit: cookie(%1), application(%2), reason(%3)") + .arg(cookie) + .arg(m_cookies[cookie].name) + .arg(m_cookies[cookie].value).local8Bit()); + + m_cookies.remove(cookie); + if (m_cookies.isEmpty()) + { + // restore states when all applications finished + if (!forceScreenSaver(isScreenSaverEnabled)) + { + dbuserror = TQT_DBusError::stdFailed(TQString("Failed to switch ScreenSaver %1!").arg( + (isScreenSaverEnabled) ? "on" : "off")); + } + } + return true; +} + +bool ScreenSaverInterfaceImpl::screenSaverIsEnabled() +{ + DCOPReply reply = m_kdesktopdcoprefobj.call("isEnabled"); + bool on = false; + if (!reply.isValid()) + { + tqDebug("ScreenSaverInterfaceImpl::screenSaverIsEnabled(): there was some error using DCOP."); + } + else + { + if (!reply.get(on)) + { + tqDebug("ScreenSaverInterfaceImpl::screenSaverIsEnabled(): there was some error getting the value from DCOPReply"); + } + } + return on; +} + +bool ScreenSaverInterfaceImpl::forceScreenSaver(bool on) +{ + DCOPReply reply = m_kdesktopdcoprefobj.call("enable", on); + if (!reply.isValid()) + { + tqDebug(TQString("Failed to switch ScreenSaver %1!").arg((on) ? "on" : "off")); + return false; + } + return true; +} +// End of File diff --git a/kdesktop/dbus/screensaver/screensaverInterfaceImpl.h b/kdesktop/dbus/screensaver/screensaverInterfaceImpl.h new file mode 100644 index 000000000..0cec20720 --- /dev/null +++ b/kdesktop/dbus/screensaver/screensaverInterfaceImpl.h @@ -0,0 +1,136 @@ +/* + * screensaverInterfaceImpl.h + * + * (C) 2024 Emanoil Kotsev + * deloptes (AT) gmail.com + * + * tdebase Copyright (C) 2009 tdebase development team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef KDESKTOP_SCREENSAVERINTERFACEIMPL_H_ +#define KDESKTOP_SCREENSAVERINTERFACEIMPL_H_ + +#include <dcopref.h> +#include <tqpair.h> +#include <tqmap.h> +#include <tqdbusconnection.h> + +#include "screensaverInterface.h" + +#define DBUS_SCREENSAVER_SERVICE "TDEDbusScreenSaver" +#define DBUS_SCREENSAVER_SERVICE_NAME "org.freedesktop.ScreenSaver" +#define DBUS_SCREENSAVER_SERVICE_PATH "/org/freedesktop/ScreenSaver" + +class ScreenSaverInterfaceImpl : public org::freedesktop::ScreenSaverInterface +{ + /** + * + * Idle inhibition is achieved by the application calling an Inhibit method on a well-known D-Bus name. + * + * Inhibition will stop when the UnInhibit method is called, or the application disconnects from the + * D-Bus session bus (which usually happens upon exit). + * Implementations of this well-known bus name must have an object /org/freedesktop/ScreenSaver which + * implements the org.freedesktop.ScreenSaver interface. + * + * https://specifications.freedesktop.org/idle-inhibit-spec/latest/ch03.html + */ + +public: + ScreenSaverInterfaceImpl(TQT_DBusConnection&); + virtual ~ScreenSaverInterfaceImpl(); + + void restoreState(); + +protected: + + /** + * void Lock() + * This method gets called when the service daemon + * locks the screen + */ + virtual bool Lock(TQT_DBusError& error); + + /** + * Name: SetActive + * Args: DBUS_TYPE_BOOLEAN state + * state: TRUE to request activation, + * FALSE to request deactivation + * Returns: (nothing) + * Description: Request a change in the state of the screensaver. + * Set to TRUE to request that the screensaver activate. + * Active means that the screensaver has blanked the + * screen and may run a graphical theme. This does + * not necessary mean that the screen is locked. + * + * Not implemented + */ + virtual bool SetActive(bool& arg0, bool e, TQT_DBusError& error); + + /** + * + * Name: Inhibit + * Args: DBUS_TYPE_STRING "application-name" + * A unique identifier for the application, usually a reverse domain + * (such as 'org.freedesktop.example'). + * DBUS_TYPE_STRING "reason for inhibit" + * A human-readable and possibly translated string + * explaining the reason why idleness is inhibited + * (such as 'Playing a movie'). + * Returns: INT cookie + * This is a random number used to identify the request. + * To be passed to UnInhibit when done. + * Description: Request that saving the screen due to system idleness + * be blocked until UnInhibit is called or the + * calling process exits. + * + * https://specifications.freedesktop.org/idle-inhibit-spec/latest/re01.html + * https://lists.freedesktop.org/archives/xdg/2007-March/009187.html + */ + + virtual bool Inhibit(const TQString& application_name, const TQString& reason_for_inhibit, TQ_UINT32& cookie, TQT_DBusError& error); + + /** + * + * Name: UnInhibit + * Args: DBUS_TYPE_UINT32 cookie + * A cookie representing the inhibition request, + * as returned by the 'Inhibit' function. + * Returns: (nothing) + * Description: Cancel a previous call to Inhibit() identified by the cookie. + */ + virtual bool UnInhibit(TQ_UINT32 cookie, TQT_DBusError& error); + + virtual void handleMethodReply(const TQT_DBusMessage& reply); + virtual bool handleSignalSend(const TQT_DBusMessage& reply); + virtual TQString objectPath() const; + +private: + bool screenSaverIsEnabled(); + bool forceScreenSaver(bool); + + struct Pair { + TQString name; + TQString value; + }; + + TQT_DBusConnection *m_connection; + TQMap<TQ_UINT32,Pair> m_cookies; + TQ_UINT32 m_ccount; + + DCOPRef m_kdesktopdcoprefobj; + bool isScreenSaverEnabled; +}; + +#endif /* KDESKTOP_SCREENSAVERINTERFACEIMPL_H_ */ |