- wake up early in the afternoon at 11:30
- go online and check mail for 20 min
- get ready for lunch by 13:00
- go to the lab and spend time with my lappy till 20:00
- again spend time in front of lappy
- go to bed
Looks like I spend most of the time in front of my lappy. Wonder what I do and for how long!!!
What if we had a program that monitors for how much time do we spend looking at each window on our machine. Actually why don't we write one. This brilliant idea was not mine but my friend Puneeth's. We tried to write this program long back ( on the night of 1st day, PyCon India ). But python Xlib documentation sucks. Atleast that is what I felt when I tried to refer it. Anyways luckily that night punch got quite close to finishing this program. Dunno if he finished it later on. But I wanted to try it out. I actually saw this documentation long before we started coding for this program. I wanted to have an option on right click menu to close the current window. I found something called NACT for adding options to right click on nautilus and a program called xclose that closes window using window display name. I could get xclose running and NACT configured but forgot about it later on.
Now I start googling. I found this link that got me started.Punch figured out that there is something called display and then screen and then window. Luckily I remembered those words. The blog post also talks about the same. I tried my hands on Xlib using ipython and found out what all this window thingy is all about.
Here is a small intro to Xlib that must get you started.
from Xlib.display import Display
# initialize the display
disp = Display()
# list no of screens available
disp.screen_count() # only one in my case.. my laptop screen..
# get the screen to work on
screen = disp.screen() # disp.screen(0) also works
## everything we see is a hierarchy of windows
root_window = screen.root
# this window object will have its parent equal to zero
print root_window.get_wm_name() ## the obvious
root_tree = root_window.query_tree()
root_tree.children ## gives you a list of window objects, its children
root_tree.parent ## lets you access its parent
Now I could not achieve what I exactly wanted. Figuring out what we concentrate on, looking at the cascade of windows and their sizes involves AI. All I wanted was the window that had focus and for how long. There is a method called event notify that notifies a focus change event but It involves a lot more digging into Xlib documentation and some patience to try out all that on ipython. For now, all I am going with the brute force method, polling the Xserver every n seconds. I ran the script while I was writing the blog post and here are the results.
02/19/10-16:10:58 -> ans@ans-laptop: ~/Desktop/xlib
02/19/10-16:11:08 -> Google Docs�-�Upload a File - Vimperator
02/19/10-16:11:18 -> Google Docs - All items - Vimperator
02/19/10-16:11:28 -> poll-xserver.py - Google Docs - Vimperator
02/19/10-16:11:38 -> poll-xserver.py - Google Docs - Vimperator
02/19/10-16:11:48 -> poll-xserver.py - Google Docs - Vimperator
02/19/10-16:11:58 -> Blogger: me, myself and py - Create Post - Google Chrome
02/19/10-16:12:08 -> Blogger: me, myself and py - Create Post - Google Chrome
02/19/10-16:12:18 -> Blogger: me, myself and py - Create Post - Google Chrome
02/19/10-16:12:28 -> Blogger: me, myself and py - Create Post - Google Chrome
02/19/10-16:12:38 -> Blogger: me, myself and py - Create Post - Google Chrome
I find them pretty satisfactory. Here is the code in case some one wants to hav a luk.
Happy Coding :)