-
Notifications
You must be signed in to change notification settings - Fork 1
/
WindowEventWatcher.py
81 lines (64 loc) · 2.55 KB
/
WindowEventWatcher.py
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
import mdlog
log = mdlog.getLogger(__name__)
from EventLoop import getLoop
from EventList import FocusChangeEvent, WindowListEvent
from Window import Window, getWindowList, getFocusedWindow
from namedtuple import namedtuple
from copy import copy
import time
import re
REFRESH_TIME = 0.25
class WindowEventWatcher(object):
def __init__(self, eventQ, filterFunc=lambda x: False):
self.filterFunc = filterFunc
self.pushQ = eventQ
self.previousWindowId = None
self.previousWindowName = None
self.nextWindowList = getWindowList()
self.previousWindowList = []
# this is still too much of a perf hog, need real poll
getLoop().subscribeTimer(REFRESH_TIME, self)
def extractList(self, windowList):
return [(w.winId, w.name) for w in windowList]
def getEvents(self):
events = []
windowList = self.nextWindowList.result # force finishing
for window in windowList:
if time.time() - window.lastXpropTime > REFRESH_TIME:
window.refreshInfo()
# we only do filtering after refreshing, because otherwise
# once a window is filtered it will stay filtered forever
windowList = filter(self.filterFunc, windowList)
newWindowList = self.extractList(windowList)
if self.previousWindowList != newWindowList:
events.append(WindowListEvent(windowList))
self.previousWindowList = newWindowList
newWindow = getFocusedWindow()
newWindowId = newWindow.winId if newWindow else -1
newWindowName = newWindow.name if newWindow else ""
if self.previousWindowId != newWindowId or self.previousWindowName != newWindowName:
events.append(FocusChangeEvent(newWindow))
self.previousWindowId = newWindowId
self.previousWindowName = newWindowName
self.nextWindowList = getWindowList() # start async
return events
def __call__(self):
for e in self.getEvents():
self.pushQ.put(e)
if __name__ == "__main__":
import sys
import Queue
q = Queue.Queue()
sub = WindowEventWatcher(q, {".*Panel.*"})
try:
while True:
# without a timeout, ctrl-c doesn't work because.. python
ONEYEAR = 365 * 24 * 60 * 60
ev = q.get(True, ONEYEAR)
if isinstance(ev, WindowListEvent):
print [w.name for w in ev.windows if w.name != '']
else:
print str(ev)
except KeyboardInterrupt:
sub.stop()
sys.exit()