maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Maemo 5 / Fremantle (https://talk.maemo.org/forumdisplay.php?f=40)
-   -   Question: Determining window focus from shell script (https://talk.maemo.org/showthread.php?t=49960)

eol 2010-04-14 03:05

Question: Determining window focus from shell script
 
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 :)

eol 2010-04-19 13:26

Answer (Sort of!): Determining window focus from shell script
 
1 Attachment(s)
Replying to myself here :p

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.


All times are GMT. The time now is 20:24.

vBulletin® Version 3.8.8