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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • 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 
43 class 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 
75 KNotificationRestrictions::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 
85 KNotificationRestrictions::~KNotificationRestrictions()
86 {
87  if (d->control & ScreenSaver) {
88  d->stopScreenSaverPrevention();
89  }
90 
91  delete d;
92 }
93 
94 void 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 
106 void 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 
153 void 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 
171 QString 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"
message
void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
i18n
QString i18n(const char *text)
kdebug.h
QDBusConnection::call
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const
QDBusReply
timeout
int timeout
QX11Info::display
Display * display()
QDBusReply::isValid
bool isValid() const
QDBusConnection::sessionBus
QDBusConnection sessionBus()
KComponentData::aboutData
const KAboutData * aboutData() const
KNotificationRestrictions::ScreenSaver
Causes the screensaver to be prevented from automatically turning on.
Definition: knotificationrestrictions.h:65
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
kglobal.h
KNotificationRestrictions
KNotificationRestrictions provides a simple mechanism to avoid disruptions during full screen present...
Definition: knotificationrestrictions.h:47
QTimer
QObject
QDBusReply::value
Type value() const
QString::isEmpty
bool isEmpty() const
QDBusConnection::send
bool send(const QDBusMessage &message) const
QString
KNotificationRestrictions::~KNotificationRestrictions
virtual ~KNotificationRestrictions()
Definition: knotificationrestrictions.cpp:85
KAboutData::programName
QString programName() const
QDBusMessage
KGlobal::mainComponent
const KComponentData & mainComponent()
kaboutdata.h
knotificationrestrictions.h
kcomponentdata.h
QDBusMessage::createMethodCall
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
KNotificationRestrictions::KNotificationRestrictions
KNotificationRestrictions(Services control=NonCriticalServices, QObject *parent=0)
Constructs a new service for restrict some services.
Definition: knotificationrestrictions.cpp:75
QCoreApplication::applicationName
QString applicationName()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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