1 /* This file is part of the KDE libraries
2 Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
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.
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.
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.
20 #include "knotificationrestrictions.h"
22 #include <kaboutdata.h>
23 #include <kcomponentdata.h>
28 #include <QtGui/QApplication>
29 #include <QtDBus/QDBusConnection>
30 #include <QtDBus/QDBusMessage>
31 #include <QtDBus/QDBusReply>
39 #include <X11/keysym.h>
40 #include <X11/extensions/XTest.h>
43 class KNotificationRestrictions::Private
46 Private( KNotificationRestrictions
* qq
, Services c
)
49 , screenSaverDbusCookie(-1)
58 void screensaverFakeKeyEvent();
59 void startScreenSaverPrevention();
60 void stopScreenSaverPrevention();
62 static QString
determineProgramName();
64 KNotificationRestrictions
* q
;
66 int screenSaverDbusCookie
;
69 QTimer
* screensaverTimer
;
75 KNotificationRestrictions::KNotificationRestrictions( Services control
,
78 d( new Private( this, control
) )
80 if (d
->control
& ScreenSaver
) {
81 d
->startScreenSaverPrevention();
85 KNotificationRestrictions::~KNotificationRestrictions()
87 if (d
->control
& ScreenSaver
) {
88 d
->stopScreenSaverPrevention();
94 void KNotificationRestrictions::Private::screensaverFakeKeyEvent()
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);
106 void KNotificationRestrictions::Private::startScreenSaverPrevention()
109 QDBusMessage message
= QDBusMessage::createMethodCall(
110 "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "Inhibit");
111 message
<< determineProgramName();
113 QDBusReply
<uint
> reply
= QDBusConnection::sessionBus().call(message
);
114 if (reply
.isValid()) {
115 screenSaverDbusCookie
= reply
.value();
121 haveXTest
= XTestQueryExtension(QX11Info::display(), &a
, &b
, &c
, &e
);
124 kDebug(297) << "--- No XTEST!";
129 if ( !XTestKeyCode
) {
130 XTestKeyCode
= XKeysymToKeycode(QX11Info::display(), XK_Shift_L
);
132 if ( !XTestKeyCode
) {
133 kDebug(297) << "--- No XKeyCode for XK_Shift_L!";
138 if ( !screensaverTimer
) {
139 screensaverTimer
= new QTimer( q
);
140 connect( screensaverTimer
, SIGNAL(timeout()),
141 q
, SLOT(screensaverFakeKeyEvent()) );
144 kDebug(297) << "---- using XTest";
145 // send a fake event right away in case this got started after a period of
146 // innactivity leading to the screensaver set to activate in <55s
147 screensaverFakeKeyEvent();
148 screensaverTimer
->start( 55000 );
152 void KNotificationRestrictions::Private::stopScreenSaverPrevention()
154 if (screenSaverDbusCookie
!= -1) {
155 QDBusMessage message
= QDBusMessage::createMethodCall(
156 "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "UnInhibit");
157 message
<< static_cast<uint
>(screenSaverDbusCookie
);
158 screenSaverDbusCookie
= -1;
159 if (QDBusConnection::sessionBus().send(message
)) {
164 delete screensaverTimer
;
165 screensaverTimer
= 0;
169 QString
KNotificationRestrictions::Private::determineProgramName()
172 if (KGlobal::mainComponent().isValid()) {
173 appName
= KGlobal::mainComponent().aboutData()->programName();
175 if (appName
.isEmpty() && qApp
) {
176 appName
= QCoreApplication::applicationName();
178 if (appName
.isEmpty()) {
179 appName
= i18n("Unknown Application");
184 #include "knotificationrestrictions.moc"