2 * Copyright © 2009 Christopher Eby <kreed@kreed.org>
4 * This file is part of Inertia.
6 * Inertia is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
11 * Inertia is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See <http://www.gnu.org/licenses/> for the full license text.
23 #include <X11/extensions/sync.h>
24 #include <X11/extensions/scrnsaver.h>
26 static Display
*dpy
= NULL
;
28 static void get_alarm(XSyncAlarm
*alarm
, XSyncCounter counter
, XSyncTestType type
, XSyncValue value
)
30 XSyncAlarmAttributes attrs
;
33 XSyncIntToValue(&delta
, 0);
35 static const unsigned long flags
= XSyncCACounter
| XSyncCATestType
| XSyncCAValue
| XSyncCADelta
;
37 attrs
.trigger
.counter
= counter
;
38 attrs
.trigger
.test_type
= type
;
39 attrs
.trigger
.wait_value
= value
;
43 XSyncChangeAlarm(dpy
, *alarm
, flags
, &attrs
);
45 *alarm
= XSyncCreateAlarm(dpy
, flags
, &attrs
);
48 static void die(const char *errstr
)
50 fputs(errstr
, stderr
);
55 bool wait_for_idle_time(int idle_time
)
57 static int xsync_event_base
;
58 static XSyncAlarm idle_alarm
= None
;
59 static XSyncAlarm reset_alarm
= None
;
60 static XSyncCounter idle
= None
;
61 static XSyncValue idle_timeout
;
64 if (!(dpy
= XOpenDisplay(NULL
)))
65 die("Could not open X11 display; exiting.\n");
68 if (!XQueryExtension(dpy
, "XTEST", &dummy
, &dummy
, &dummy
))
69 die("XTEST extension not available; cannot reset idle time\n");
72 int xsync_major
= SYNC_MAJOR_VERSION
;
73 int xsync_minor
= SYNC_MINOR_VERSION
;
75 if (!XSyncQueryExtension(dpy
, &xsync_event_base
, &xsync_error_base
) || !XSyncInitialize(dpy
, &xsync_major
, &xsync_minor
))
76 die("No XSync extension; exiting.\n");
79 XSyncSystemCounter
*counters
= XSyncListSystemCounters(dpy
, &i
);
81 if (!strcmp(counters
[i
].name
, "IDLETIME"))
82 idle
= counters
[i
].counter
;
83 XSyncFreeSystemCounterList(counters
);
86 die("No IDLETIME counter! xorg-server 1.3 and higher should support it. Exiting.\n");
88 XSyncIntToValue(&idle_timeout
, idle_time
* 1000);
89 get_alarm(&idle_alarm
, idle
, XSyncPositiveComparison
, idle_timeout
);
94 while (!XNextEvent(dpy
, &ev
)) {
95 if (ev
.type
== xsync_event_base
+ XSyncAlarmNotify
) {
96 XSyncAlarmNotifyEvent
*e
= (XSyncAlarmNotifyEvent
*)&ev
;
98 if (e
->alarm
== idle_alarm
) {
100 XSyncValue reset_timeout
;
101 XSyncValue minus_one
;
103 XSyncIntToValue(&minus_one
, -1);
104 XSyncValueAdd(&reset_timeout
, e
->counter_value
, minus_one
, &overflow
);
105 get_alarm(&reset_alarm
, idle
, XSyncNegativeComparison
, reset_timeout
);
108 } else if (e
->alarm
== reset_alarm
) {
109 get_alarm(&idle_alarm
, idle
, XSyncPositiveComparison
, idle_timeout
);
117 void reset_idle_time()
120 if (!(dpy
= XOpenDisplay(NULL
))) {
121 fputs("Could not open X11 display.\n", stderr
);
126 XScreenSaverSuspend(dpy
, True
);
128 XScreenSaverSuspend(dpy
, False
);