~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to kdesktop/xautolock.cc

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//----------------------------------------------------------------------------
 
2
//
 
3
// This file is part of the KDE project
 
4
//
 
5
// Copyright (c) 1999 Martin R. Jones <mjones@kde.org>
 
6
// Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org>
 
7
//
 
8
// KDE screensaver engine
 
9
//
 
10
 
 
11
#ifdef HAVE_CONFIG_H
 
12
#include <config.h>
 
13
#endif
 
14
 
 
15
#include "xautolock.h"
 
16
#include "xautolock.moc"
 
17
 
 
18
#include <kapplication.h>
 
19
#include <kdebug.h>
 
20
 
 
21
#include <X11/Xlib.h>
 
22
#include <X11/Xutil.h>
 
23
#include <ctime>
 
24
#include "xautolock_c.h"
 
25
 
 
26
 
 
27
int xautolock_useXidle = 0;
 
28
int xautolock_useMit = 0;
 
29
xautolock_corner_t xautolock_corners[ 4 ];
 
30
 
 
31
static XAutoLock* self = NULL;
 
32
 
 
33
static int catchFalseAlarms(Display *, XErrorEvent *)
 
34
{
 
35
    return 0;
 
36
}
 
37
 
 
38
//===========================================================================
 
39
//
 
40
// Detect user inactivity.
 
41
// Named XAutoLock after the program that it is based on.
 
42
//
 
43
XAutoLock::XAutoLock()
 
44
{
 
45
    self = this;
 
46
    int dummy = 0;
 
47
    dummy = dummy; // shut up
 
48
    xautolock_useXidle = 0;
 
49
    xautolock_useMit = 0;
 
50
    // code for corners (see #13360)
 
51
    // setting a corner to ca_forceLock or ca_dontLock
 
52
    // makes that corner start screensaver or prevent
 
53
    // screensaver start
 
54
    // the order is topleft, topright, bottomleft, bottomright
 
55
    // So when you add GUI for this, add a method changing these values.
 
56
#if 0 //now we have a GUI to configure it. => use value load to kconfig and not re-initialise value
 
57
    xautolock_corners[ 0 ] = ca_nothing;
 
58
    xautolock_corners[ 1 ] = ca_nothing;
 
59
    xautolock_corners[ 2 ] = ca_nothing;
 
60
    xautolock_corners[ 3 ] = ca_nothing;
 
61
#endif
 
62
#ifdef HAVE_XIDLE
 
63
    useXidle = XidleQueryExtension( qt_xdisplay(), &dummy, &dummy );
 
64
#endif
 
65
#ifdef HAVE_XSCREENSAVER
 
66
    if( !xautolock_useXidle )
 
67
        xautolock_useMit = XScreenSaverQueryExtension( qt_xdisplay(), &dummy, &dummy );
 
68
#endif
 
69
    if( !xautolock_useXidle && !xautolock_useMit )
 
70
    {
 
71
        kapp->installX11EventFilter( this );
 
72
        int (*oldHandler)(Display *, XErrorEvent *);
 
73
        oldHandler = XSetErrorHandler(catchFalseAlarms);
 
74
        XSync(qt_xdisplay(), False );
 
75
        xautolock_initDiy( qt_xdisplay());
 
76
        XSync(qt_xdisplay(), False );
 
77
        XSetErrorHandler(oldHandler);
 
78
    }
 
79
 
 
80
    mTimeout = DEFAULT_TIMEOUT;
 
81
    resetTrigger();
 
82
 
 
83
    time(&mLastTimeout);
 
84
    mActive = false;
 
85
 
 
86
    mTimerId = startTimer( CHECK_INTERVAL );
 
87
 
 
88
}
 
89
 
 
90
//---------------------------------------------------------------------------
 
91
//
 
92
// Destructor.
 
93
//
 
94
XAutoLock::~XAutoLock()
 
95
{
 
96
  self = NULL;
 
97
}
 
98
 
 
99
//---------------------------------------------------------------------------
 
100
//
 
101
// The time in seconds of continuous inactivity.
 
102
//
 
103
void XAutoLock::setTimeout(int t)
 
104
{
 
105
    mTimeout = t;
 
106
    resetTrigger();
 
107
}
 
108
 
 
109
//---------------------------------------------------------------------------
 
110
//
 
111
// Start watching Activity
 
112
//
 
113
void XAutoLock::start()
 
114
{
 
115
    resetTrigger();
 
116
    time(&mLastTimeout);
 
117
    mActive = true;
 
118
}
 
119
 
 
120
//---------------------------------------------------------------------------
 
121
//
 
122
// Stop watching Activity
 
123
//
 
124
void XAutoLock::stop()
 
125
{
 
126
    mActive = false;
 
127
}
 
128
 
 
129
//---------------------------------------------------------------------------
 
130
//
 
131
// Reset the trigger time.
 
132
//
 
133
void XAutoLock::resetTrigger()
 
134
{
 
135
    mTrigger = time(0) + mTimeout;
 
136
}
 
137
 
 
138
//---------------------------------------------------------------------------
 
139
//
 
140
// Set the remaining time to 't', if it's shorter than already set.
 
141
//
 
142
void XAutoLock::setTrigger( time_t t )
 
143
{
 
144
    if( t < mTrigger )
 
145
        mTrigger = t;
 
146
}
 
147
 
 
148
//---------------------------------------------------------------------------
 
149
//
 
150
// Process new windows and check the mouse.
 
151
//
 
152
void XAutoLock::timerEvent(QTimerEvent *ev)
 
153
{
 
154
    if (ev->timerId() != mTimerId)
 
155
    {
 
156
        return;
 
157
    }
 
158
 
 
159
    int (*oldHandler)(Display *, XErrorEvent *) = NULL;
 
160
    if( !xautolock_useXidle && !xautolock_useMit )
 
161
    { // only the diy way needs special X handler
 
162
        XSync( qt_xdisplay(), False );
 
163
        oldHandler = XSetErrorHandler(catchFalseAlarms);
 
164
    }
 
165
 
 
166
    xautolock_processQueue();
 
167
 
 
168
    time_t now = time(0);
 
169
    if ((now > mLastTimeout && now - mLastTimeout > TIME_CHANGE_LIMIT) ||
 
170
        (mLastTimeout > now && mLastTimeout - now > TIME_CHANGE_LIMIT+1))
 
171
    {
 
172
        /* the time has changed in one large jump.  This could be because
 
173
           the date was changed, or the machine was suspended.  We'll just
 
174
           reset the triger. */
 
175
        resetTrigger();
 
176
    }
 
177
 
 
178
    mLastTimeout = now;
 
179
 
 
180
    xautolock_queryIdleTime( qt_xdisplay());
 
181
    xautolock_queryPointer( qt_xdisplay());
 
182
 
 
183
    if( !xautolock_useXidle && !xautolock_useMit )
 
184
        XSetErrorHandler(oldHandler);
 
185
 
 
186
    if (now >= mTrigger)
 
187
    {
 
188
        resetTrigger();
 
189
        if (mActive)
 
190
            emit timeout();
 
191
    }
 
192
}
 
193
 
 
194
bool XAutoLock::x11Event( XEvent* ev )
 
195
{
 
196
    xautolock_processEvent( ev );
 
197
// don't futher process key events that were received only because XAutoLock wants them
 
198
    if( ev->type == KeyPress && !ev->xkey.send_event
 
199
        && !xautolock_useXidle && !xautolock_useMit
 
200
        && !QWidget::find( ev->xkey.window ))
 
201
        return true;
 
202
    return false;
 
203
}
 
204
 
 
205
bool XAutoLock::ignoreWindow( WId w )
 
206
{
 
207
    if( w != qt_xrootwin() && QWidget::find( w ))
 
208
        return true;
 
209
    return false;
 
210
}
 
211
 
 
212
extern "C"
 
213
void xautolock_resetTriggers()
 
214
{
 
215
  self->resetTrigger();
 
216
}
 
217
 
 
218
extern "C"
 
219
void xautolock_setTrigger( time_t t )
 
220
{
 
221
  self->setTrigger( t );
 
222
}
 
223
 
 
224
extern "C"
 
225
int xautolock_ignoreWindow( Window w )
 
226
{
 
227
   return self->ignoreWindow( w );
 
228
}