Hacking is always fun. That’s why I always lose focus on everything once I find something to hack around. And that’s why I spent whole last night trying to figure out how to mimic Window focus event in a shell script. Though this is completely new to _me_, you maybe very much comfortable with implementing it :) . If so please let me know in comments.
Anyway here is my story.
I found this nice command line IM client called centerIM (Thanx @chathuraw for info). Once I got it set up I was just scanning on it’s documentation page & came across this interesting part “External actions & auto-responses“. I wrote a script right away for GUI notifications, inspired by notify.pl script for irssi. Here is the script if you are interested.
Since the load of IM messages was very high at a time & It was useless when the terminal window with centerIM was already focused (which means I’m chatting on centerIM) I was thinking of getting notifications only when centerIM window is not in focus. Few Google searches lead me to very limited resources & I chose xwininfo & xdpyinfo commands for my work.
Workaround: I’m using gnome for now & so gnome-terminal as my terminal emulator. I had to set Edit >Pprofile Preferences > Title & command option to keep initial title, in order to have “Terminal” as the terminal window name all the time.
And ran xininfo to get following output.
chanux@nim:~$ xwininfo -name Terminal
xwininfo: Window id: 0x3800003 "Terminal"
Absolute upper-left X: 0
Absolute upper-left Y: 52
Relative upper-left X: 0
Relative upper-left Y: 27
Visual Class: TrueColor
Border width: 0
Colormap: 0x20 (installed)
Bit Gravity State: NorthWestGravity
Window Gravity State: NorthWestGravity
Backing Store State: NotUseful
Save Under State: no
Map State: IsViewable
Override Redirect State: no
Corners: +0+52 -0+52 -0-25 +0-25
This can be used to get the window id of the window which runs the script.
Then I can find the focused window at the moment as following.
chanux@nim:~$ xdpyinfo |grep focus
focus: window 0x3800004, revert to Parent
I fetched the window IDs with a little bit more work.
Anyway the problem I had is that, I get 0x3800003 (at this example) or likewise for the Terminal window id & even when the Terminal id is focused I get 0x3800004 as the focused window id. Yes I know with some more work I can manage to handle that & come to a point that I can compare those window Ids to check for the focused window. But I really like to know why that difference come up. Anyone have an explanation? Or anyone like to mess with this?
Following is the way I fetched window ids from above outputs
chanux@nim:~$ xwininfo -name "Terminal" | grep xwininfo | cut -d " " -f 4
chanux@nim:~$ xdpyinfo | grep focus | cut -d " " -f 4 | sed s/,//