-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch
executable file
·90 lines (72 loc) · 2.32 KB
/
watch
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
#!/usr/bin/python
# To make this event driven, watch the root window for
# _NET_ACTIVE_WINDOW property changes. The down side of event driven
# is that the window observation may not coincide with the observed
# property change event. (i.e. active window changes twice before we
# can observe window focus once). Also, there's more back and forth
# when windows do switch (prop change event + get_input_focus).
import os
import re
import time
from datetime import datetime, timedelta
from Xlib import display
from select import select
from subprocess import Popen, PIPE
import hotshot
D = display.Display()
oldname = newname = ''
oldid = newid = 0L
oldtime = datetime.now()
infds = []
outfds = []
errfds = []
fd_handlers = {}
def winmon():
global oldname, newname, oldid, newid, oldtime
win = D.get_input_focus()
newid = win.focus.id
newname = win.focus.get_wm_name()
if newname != oldname or newid != oldid:
now = datetime.now()
print "%s %16s %#0.8x %s" % (now.strftime('%s'), (now-oldtime), oldid, oldname)
oldtime = now
oldname = newname
oldid = newid
xss_last = None
def xss_event(fd):
global xss_last
str = fd.readline()
match = re.match(r'(?P<msg>(BLANK|UNBLANK|LOCK)) (?P<time>.*)', str)
if match:
msg = match.groupdict()['msg']
now = datetime(*time.strptime(match.groupdict()['time'], '%c')[0:6])
if msg == 'UNBLANK':
if xss_last:
delta = now - xss_last
else:
delta = timedelta()
print "%s %16s XSCREENSAVER" % (now.strftime('%s'), delta)
xss_last = None
else:
xss_last = now
xss_proc = Popen(['xscreensaver-command', '-watch'], stdout=PIPE)
fd_handlers[xss_proc.stdout] = xss_event
infds = [xss_proc.stdout]
#profiler = hotshot.Profile()
def main():
winmon()
running = True
while running:
try:
(infda,outfda,errfda) = select(infds, outfds, errfds, 0.3)
if infda:
for fd in infda:
fd_handlers[fd](fd)
# Always check window state
winmon()
except KeyboardInterrupt:
running = False
# Because otherwise they don't clean up when we go away...
os.kill(xss_proc.pid, 15)
#profiler.runcall(main)
main()