[go: up one dir, main page]

File: xidle.cc

package info (click to toggle)
imms 3.1.0~rc8-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,228 kB
  • ctags: 1,243
  • sloc: cpp: 7,839; ansic: 1,845; xml: 252; sh: 189; makefile: 140
file content (133 lines) | stat: -rw-r--r-- 3,052 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 Borrows from xautolock 2.1
 by Michel Eyckmans (MCE) <eyckmans@imec.be>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include "xidle.h"

#include <iostream>

#ifdef WITH_XSCREENSAVER
# include <X11/extensions/scrnsaver.h>
#endif

using std::cerr;
using std::endl;

#define SAMPLE_RATE     10
#define MIN_ACTIVE      1

XIdle::XIdle() : xidle_enabled(true), prev_mask(0),
                     prev_rootX(-1), prev_rootY(-1)
{
#ifdef WITH_XSCREENSAVER
    if ((display = XOpenDisplay(0)))
    {
        root = DefaultRootWindow(display);
        screen = ScreenOfDisplay(display, DefaultScreen(display));
    }
    else
    {
        cerr << "IMMS: Could not open X display." << endl;
        cerr << "IMMS: Disabling idleness detector." << endl;
    }
#endif

    reset();
}

void XIdle::reset()
{
    last_checked = time(0);
    active = 0;
}

bool XIdle::is_active()
{
    return active > MIN_ACTIVE;
}

void XIdle::query()
{
#ifdef WITH_XSCREENSAVER
    if (!xidle_enabled)
        return;

    if (active > MIN_ACTIVE || !display)
        return;

    if (time(0) < last_checked + SAMPLE_RATE)
        return;

    if (!query_idle_time())
        query_pointer();

    last_checked = time(0);
#endif
}

bool XIdle::query_idle_time()
{
#ifdef WITH_XSCREENSAVER
    time_t idle_time = 1000;

    static XScreenSaverInfo* mitInfo = 0;
    if (!mitInfo)
        mitInfo = XScreenSaverAllocInfo();
    XScreenSaverQueryInfo(display, DefaultRootWindow(display), mitInfo);
    idle_time = mitInfo->idle;

    if (idle_time < SAMPLE_RATE)
        return ++active;

#endif 
    return false;
}

bool XIdle::query_pointer()
{
#ifdef WITH_XSCREENSAVER
    Window dummyWin;
    unsigned int mask;
    int rootX = 0, rootY = 0, dummyInt;

    if (!XQueryPointer(display, root, &root, &dummyWin, &rootX, &rootY,
                &dummyInt, &dummyInt, &mask))
    {
        // Pointer has moved to another screen,
        // so let's find out which one.
        for (int i = -1; ++i < ScreenCount(display); )
        {
            if (root == RootWindow(display, i))
            {
                screen = ScreenOfDisplay(display, i);
                break;
            }
        }
    }

    if (rootX != prev_rootX || rootY != prev_rootY || mask != prev_mask)
    {
        prev_rootX = rootX;
        prev_rootY = rootY;
        prev_mask = mask;

        return ++active;
    }

#endif
    return false;
}