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
49 , screenSaverDbusCookie(-1)
58 void screensaverFakeKeyEvent();
59 void startScreenSaverPrevention();
60 void stopScreenSaverPrevention();
62 static QString determineProgramName();
66 int screenSaverDbusCookie;
69 QTimer* screensaverTimer;
78 d( new Private( this, control ) )
81 d->startScreenSaverPrevention();
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()
110 QDBusMessage
message = QDBusMessage::createMethodCall(
111 "org.freedesktop.ScreenSaver",
"/ScreenSaver",
"org.freedesktop.ScreenSaver",
"Inhibit");
112 message << determineProgramName();
114 QDBusReply<uint> reply = QDBusConnection::sessionBus().call(message);
115 if (reply.isValid()) {
116 screenSaverDbusCookie = reply.value();
122 haveXTest = XTestQueryExtension(QX11Info::display(), &a, &b, &c, &e);
125 kDebug(297) <<
"--- No XTEST!";
130 if ( !XTestKeyCode ) {
131 XTestKeyCode = XKeysymToKeycode(QX11Info::display(), XK_Shift_L);
133 if ( !XTestKeyCode ) {
134 kDebug(297) <<
"--- No XKeyCode for XK_Shift_L!";
139 if ( !screensaverTimer ) {
140 screensaverTimer =
new QTimer( q );
141 connect( screensaverTimer, SIGNAL(
timeout()),
142 q, SLOT(screensaverFakeKeyEvent()) );
145 kDebug(297) <<
"---- using XTest";
148 screensaverFakeKeyEvent();
149 screensaverTimer->start( 55000 );
153 void KNotificationRestrictions::Private::stopScreenSaverPrevention()
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)) {
166 delete screensaverTimer;
167 screensaverTimer = 0;
171 QString KNotificationRestrictions::Private::determineProgramName()
177 if (appName.isEmpty() && qApp) {
178 appName = QCoreApplication::applicationName();
180 if (appName.isEmpty()) {
181 appName =
i18n(
"Unknown Application");
186 #include "knotificationrestrictions.moc"
void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
QString i18n(const char *text)
const KAboutData * aboutData() const
Causes the screensaver to be prevented from automatically turning on.
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KNotificationRestrictions provides a simple mechanism to avoid disruptions during full screen present...
virtual ~KNotificationRestrictions()
QString programName() const
const KComponentData & mainComponent()
KNotificationRestrictions(Services control=NonCriticalServices, QObject *parent=0)
Constructs a new service for restrict some services.