Reply
Thread Tools
Posts: 11 | Thanked: 8 times | Joined on Mar 2010 @ Sabah, Malaysia
#1
Greetings,

I'm writing a simple shell script that needs the user to go to the camera, take a picture, and manually return to xterminal (which is already open previously).

My question: Is there any way I can detect which application currently has the focus from a shell script? Or the reverse: Is there any way that I can check whether an application has been "backgrounded"?

Alternatively, rather than using a "pure" shell script, is there any simple python implementation of the above? In this case I can call the python interpreter to check the application state from the shell script and process accordingly.

Thanks
 
Posts: 11 | Thanked: 8 times | Joined on Mar 2010 @ Sabah, Malaysia
#2
Replying to myself here

I found that xprop can be used to enumerate all windows in an X11 session and retrieve the top-most (focused) window.

For the technically-inclined, I have uploaded some scripts for your reference/use.

This method requires installation of x11-utils from the Maemo Fremantle/Tools repository. The instructions can be found here: http://talk.maemo.org/showthread.php...731#post448731.

After installing x11-utils, xprop will be available on the console.

The uploaded files consist of:
  • x11-wait-for-topmost.sh
  • x11-xprop-get-topmost.sh (x11-wait-for-topmost.sh depends on this file)

Usage: x11-wait-for-topmost.sh [WM_CLASS] [EVENT] [TIMEOUT]
  • WM_CLASS is the class name of the window you are targeting e.g. osso-xterm, camera-ui;
  • (optional) EVENT is the type of event you are looking for: focus or blur - focus simply means that the window has become top-most, and blur meaning that the targeted window is no longer top-most (defaults to focus);
  • (optional) TIMEOUT is the number of seconds before exiting if the focus/blur event on the target windows has not occurred (defaults to 600 seconds i.e. 10 minutes)

Example:
Code:
./x11-wait-for-topmost.sh camera-ui
./x11-wait-for-topmost.sh camera-ui focus
Both of these commands do the same thing, the script will wait until the camera application runs and receives the focus (try opening the camera slide).
Notes:
  • The script will print the text event-triggered to stdout when it is successful. You can easily use another script to capture and test for this output.
  • x11-xprop-get-topmost.sh is the file that actually calls xprop. If you want to see the bare-bones xprop calls, you will find it inside this file.
  • Both files are designed to be reusable in your own scripts. They also come with some sanity checks built-in e.g. x11-xprop-get-topmost.sh actually checks first if you have xprop installed.
  • You do not have to be root to run either script.
Attached Files
File Type: gz x11.20100419.tar.gz (973 Bytes, 141 views)
 

The Following 2 Users Say Thank You to eol For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 08:47.