Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rough sketch at making watch multi-platform. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 66 additions & 42 deletions watch
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ 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
try:
from Xlib import display
winmon_proto = 'xlib'
except: pass
try:
from osx import something_or_other
winmon_proto = 'osx'
except: pass
try:
from win32 import something_or_other
winmon_proto = 'win32'
except: pass

assert winmon_proto

D = display.Display()

oldname = newname = ''
oldid = newid = 0L
Expand All @@ -29,46 +40,61 @@ 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
class WindowEnvironment:

def winmon_xlib_init()
self.D = display.Display()

def winmon_xlib():
global oldname, newname, oldid, newid, oldtime
win = self.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

def winmon_osx():
pass

def winmon_win32():
pass

if has_xlib:
winmon = winmon_xlib

ss_last = None

class ScreenSaver:

def ss_event(fd):
global ss_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 ss_last:
delta = now - ss_last
else:
delta = timedelta()
print "%s %16s XSCREENSAVER" % (now.strftime('%s'), delta)
ss_last = None
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)
ss_last = now

fd_handlers[xss_proc.stdout] = xss_event
infds = [xss_proc.stdout]
def ss_init_xss():
global ss_proc = Popen(['xscreensaver-command', '-watch'], stdout=PIPE)

#profiler = hotshot.Profile()
fd_handlers[ss_proc.stdout] = ss_event
infds = [ss_proc.stdout]

# This is in a function for profiling purposes
def main():

winmon()
running = True
while running:
Expand All @@ -77,14 +103,12 @@ def main():
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()
if __name__ == '__main__':
main()