• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • notifications
knotificationrestrictions.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "knotificationrestrictions.h"
21
22#include <kaboutdata.h>
23#include <kcomponentdata.h>
24#include <kdebug.h>
25#include <kglobal.h>
26#include <klocale.h>
27
28#include <QtGui/QApplication>
29#include <QtDBus/QDBusConnection>
30#include <QtDBus/QDBusMessage>
31#include <QtDBus/QDBusReply>
32
33#include <config.h>
34
35#ifdef HAVE_XTEST
36#include <QTimer>
37#include <QX11Info>
38
39#include <X11/keysym.h>
40#include <X11/extensions/XTest.h>
41#endif // HAVE_XTEST
42
43class KNotificationRestrictions::Private
44{
45 public:
46 Private( KNotificationRestrictions* qq, Services c )
47 : q( qq ),
48 control(c)
49 , screenSaverDbusCookie(-1)
50#ifdef HAVE_XTEST
51 ,screensaverTimer(0),
52 haveXTest(0),
53 XTestKeyCode(0)
54#endif // HAVE_XTEST
55 {
56 }
57
58 void screensaverFakeKeyEvent();
59 void startScreenSaverPrevention();
60 void stopScreenSaverPrevention();
61
62 static QString determineProgramName();
63
64 KNotificationRestrictions* q;
65 Services control;
66 int screenSaverDbusCookie;
67 QString reason;
68#ifdef HAVE_XTEST
69 QTimer* screensaverTimer;
70 int haveXTest;
71 int XTestKeyCode;
72#endif // HAVE_XTEST
73};
74
75KNotificationRestrictions::KNotificationRestrictions( Services control,
76 QObject* parent )
77 : QObject(parent),
78 d( new Private( this, control ) )
79{
80 if (d->control & ScreenSaver) {
81 d->startScreenSaverPrevention();
82 }
83}
84
85KNotificationRestrictions::~KNotificationRestrictions()
86{
87 if (d->control & ScreenSaver) {
88 d->stopScreenSaverPrevention();
89 }
90
91 delete d;
92}
93
94void KNotificationRestrictions::Private::screensaverFakeKeyEvent()
95{
96 kDebug(297);
97#ifdef HAVE_XTEST
98 kDebug(297) << "---- using XTestFakeKeyEvent";
99 Display* display = QX11Info::display();
100 XTestFakeKeyEvent(display, XTestKeyCode, true, CurrentTime);
101 XTestFakeKeyEvent(display, XTestKeyCode, false, CurrentTime);
102 XSync(display, false);
103#endif // HAVE_XTEST
104}
105
106void KNotificationRestrictions::Private::startScreenSaverPrevention()
107{
108 kDebug(297);
109
110 QDBusMessage message = QDBusMessage::createMethodCall(
111 "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "Inhibit");
112 message << determineProgramName();
113 message << reason;
114 QDBusReply<uint> reply = QDBusConnection::sessionBus().call(message);
115 if (reply.isValid()) {
116 screenSaverDbusCookie = reply.value();
117 return;
118 }
119#ifdef HAVE_XTEST
120 if ( !haveXTest ) {
121 int a,b,c,e;
122 haveXTest = XTestQueryExtension(QX11Info::display(), &a, &b, &c, &e);
123
124 if ( !haveXTest ) {
125 kDebug(297) << "--- No XTEST!";
126 return;
127 }
128 }
129
130 if ( !XTestKeyCode ) {
131 XTestKeyCode = XKeysymToKeycode(QX11Info::display(), XK_Shift_L);
132
133 if ( !XTestKeyCode ) {
134 kDebug(297) << "--- No XKeyCode for XK_Shift_L!";
135 return;
136 }
137 }
138
139 if ( !screensaverTimer ) {
140 screensaverTimer = new QTimer( q );
141 connect( screensaverTimer, SIGNAL(timeout()),
142 q, SLOT(screensaverFakeKeyEvent()) );
143 }
144
145 kDebug(297) << "---- using XTest";
146 // send a fake event right away in case this got started after a period of
147 // innactivity leading to the screensaver set to activate in <55s
148 screensaverFakeKeyEvent();
149 screensaverTimer->start( 55000 );
150#endif // HAVE_XTEST
151}
152
153void KNotificationRestrictions::Private::stopScreenSaverPrevention()
154{
155
156 if (screenSaverDbusCookie != -1) {
157 QDBusMessage message = QDBusMessage::createMethodCall(
158 "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "UnInhibit");
159 message << static_cast<uint>(screenSaverDbusCookie);
160 screenSaverDbusCookie = -1;
161 if (QDBusConnection::sessionBus().send(message)) {
162 return;
163 }
164 }
165#ifdef HAVE_XTEST
166 delete screensaverTimer;
167 screensaverTimer = 0;
168#endif // HAVE_XTEST
169}
170
171QString KNotificationRestrictions::Private::determineProgramName()
172{
173 QString appName;
174 if (KGlobal::mainComponent().isValid()) {
175 appName = KGlobal::mainComponent().aboutData()->programName();
176 }
177 if (appName.isEmpty() && qApp) {
178 appName = QCoreApplication::applicationName();
179 }
180 if (appName.isEmpty()) {
181 appName = i18n("Unknown Application");
182 }
183 return appName;
184}
185
186#include "knotificationrestrictions.moc"
KAboutData::programName
QString programName() const
KComponentData::aboutData
const KAboutData * aboutData() const
KNotificationRestrictions
KNotificationRestrictions provides a simple mechanism to avoid disruptions during full screen present...
Definition: knotificationrestrictions.h:48
KNotificationRestrictions::~KNotificationRestrictions
virtual ~KNotificationRestrictions()
Definition: knotificationrestrictions.cpp:85
KNotificationRestrictions::KNotificationRestrictions
KNotificationRestrictions(Services control=NonCriticalServices, QObject *parent=0)
Constructs a new service for restrict some services.
Definition: knotificationrestrictions.cpp:75
KNotificationRestrictions::ScreenSaver
@ ScreenSaver
Causes the screensaver to be prevented from automatically turning on.
Definition: knotificationrestrictions.h:65
QObject
kDebug
#define kDebug
kaboutdata.h
kcomponentdata.h
kdebug.h
kglobal.h
timeout
int timeout
klocale.h
i18n
QString i18n(const char *text)
knotificationrestrictions.h
KGlobal::mainComponent
const KComponentData & mainComponent()
message
void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal