How to detect if an Xwindow has focus?

This is a discussion on How to detect if an Xwindow has focus? within the Xwindows forums, part of the Tools category; Note: Ideally, I'd like a shell (tool) solution, but if I have to write a C program for it, that's OK, too. Hence the crosspost. Now, to the question. Using ...

Go Back   Unix Linux Forum > Technologies & Tools > Tools > Xwindows

FixUnix.com - Unix Linux Forums

Unix Content Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 10-02-2008, 02:21 PM
Default How to detect if an Xwindow has focus?

Note: Ideally, I'd like a shell (tool) solution, but if I have to write
a C program for it, that's OK, too. Hence the crosspost.

Now, to the question. Using either xwininfo or xprop, I can get some
information about a given window (including its window ID). What I need
is a way to tell whether or not that window has the focus. I could not
see anything in either xwininfo or xprop that changed, based on the
window gaining or losing focus.

Reply With Quote
  #2  
Old 10-02-2008, 03:30 PM
Default Re: How to detect if an Xwindow has focus?

Kenny McCormack writes:
> Note: Ideally, I'd like a shell (tool) solution, but if I have to write
> a C program for it, that's OK, too. Hence the crosspost.


> Now, to the question. Using either xwininfo or xprop, I can get some
> information about a given window (including its window ID). What I need
> is a way to tell whether or not that window has the focus. I could not
> see anything in either xwininfo or xprop that changed, based on the
> window gaining or losing focus.


I'm not sure, but I think it is the responsibility of WM to decide which
window has the focus, so how about querying root window for the property
"_NET_ACTIVE_WINDOW", hmm...?

HTH
Ashish
--
() ascii ribbon campaign - against HTML e-mail
/\ www.asciiribbon.org - against proprietary attachments
·-- ·- ···· ·--- ·- ···- ·- ·--·-· --· -- ·- ·· ·-·· ·-·-·- -·-· --- --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAkjlIWcACgkQHy+EEHYuXnTspACgtn8tdEYxB2 xC4PmtHSX8KscU
o8AAn1ptRjgq0DoVrr+zVjR1fajSNm1o
=074U
-----END PGP SIGNATURE-----
Reply With Quote
  #3  
Old 10-02-2008, 04:21 PM
Default Re: How to detect if an Xwindow has focus?

In news:gc33eo$eir$1@news.xmission.com,
Kenny McCormack typed:

> Now, to the question. Using either xwininfo or xprop, I can get some
> information about a given window (including its window ID). What I
> need is a way to tell whether or not that window has the focus. I
> could not see anything in either xwininfo or xprop that changed,
> based on the window gaining or losing focus.


xdpyinfo | grep focus


Reply With Quote
  #4  
Old 10-02-2008, 05:06 PM
Default Re: How to detect if an Xwindow has focus?

In article <8763oapzfq.fsf@chateau.d.lf>,
Ashish Shukla श ष श ल wrote:
>-=-=-=-=-=-

....
>I'm not sure, but I think it is the responsibility of WM to decide which
>window has the focus, so how about querying root window for the property
>"_NET_ACTIVE_WINDOW", hmm...?


Yes; we have a winner! Thanks!

xprop -root

(then parsing the result) does the trick.

Reply With Quote
  #5  
Old 10-02-2008, 05:08 PM
Default Re: How to detect if an Xwindow has focus?

In article , b.jeswine wrote:
>In news:gc33eo$eir$1@news.xmission.com,
>Kenny McCormack typed:
>
>> Now, to the question. Using either xwininfo or xprop, I can get some
>> information about a given window (including its window ID). What I
>> need is a way to tell whether or not that window has the focus. I
>> could not see anything in either xwininfo or xprop that changed,
>> based on the window gaining or losing focus.

>
>xdpyinfo | grep focus


Yes, looks like that would work too. Any particular reason to prefer
that or the method with xprop? Which one is more efficient?

Reply With Quote
  #6  
Old 10-02-2008, 07:36 PM
Default Re: How to detect if an Xwindow has focus?

In news:gc3d88$m85$2@news.xmission.com,
Kenny McCormack typed:

>> xdpyinfo | grep focus

>
> Yes, looks like that would work too. Any particular reason to prefer
> that or the method with xprop? Which one is more efficient?


One of the beauties of Unix is that there are several different ways to
accomplish a given task. Choose the one that best meets your needs. Notice
the different hexadecimal values returned, however; they differ by 1:

$ time xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"
_NET_ACTIVE_WINDOW(WINDOW): window id # 0x320cbe1, 0x0

real 0m0.144s
user 0m0.006s
sys 0m0.010s

$ time xdpyinfo | grep focus
focus: window 0x320cbe2, revert to Parent

real 0m0.015s
user 0m0.002s
sys 0m0.012s





Reply With Quote
  #7  
Old 10-03-2008, 03:01 AM
Default Re: How to detect if an Xwindow has focus?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

b jeswine writes:

[...]

> $ time xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"
> _NET_ACTIVE_WINDOW(WINDOW): window id # 0x320cbe1, 0x0


> real 0m0.144s
> user 0m0.006s
> sys 0m0.010s


Or you can only retrieve the value of property "_NET_ACTIVE_WINDOW"
instead of retreiving values of all properties:

% xprop -root _NET_ACTIVE_WINDOW
_NET_ACTIVE_WINDOW(WINDOW): window id # 0x105cd9d

Ashish
- --
() ascii ribbon campaign - against HTML e-mail
/\ www.asciiribbon.org - against proprietary attachments
·-- ·- ···· ·--- ·- ···- ·- ·--·-· --· -- ·- ·· ·-·· ·-·-·- -·-· --- --
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAkjlw1EACgkQHy+EEHYuXnRn6gCgrWbZdk5G+t g3W0pjlWaJ9n82
TJcAoMJ/V+EiJpZ3e74suhZeuH4qompZ
=8JLy
-----END PGP SIGNATURE-----
Reply With Quote
  #8  
Old 10-03-2008, 09:18 AM
Default Re: How to detect if an Xwindow has focus?

On 10/3/2008 2:01 AM, Ashish Shukla आशीष शुक्ल wrote:

> Ashish
> - --
> () ascii ribbon campaign - against HTML e-mail
> /\ www.asciiribbon.org - against proprietary attachments
> ·-- ·- ···· ·--- ·- ···- ·- ·--·-· --· -- ·- ·· ·-·· ·-·-·- -·-· --- --
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.9 (GNU/Linux)
>
> iEYEARECAAYFAkjlw1EACgkQHy+EEHYuXnRn6gCgrWbZdk5G+t g3W0pjlWaJ9n82
> TJcAoMJ/V+EiJpZ3e74suhZeuH4qompZ
> =8JLy
> -----END PGP SIGNATURE-----


That's pretty good - being against HTML email and proprietary attachmentswhile
using a PGP signature in a newsgroup. It's like espousing the virtues of being a
vegetarian while eating a hamburger.

Ed.

Reply With Quote
  #9  
Old 10-03-2008, 09:37 AM
Default Re: How to detect if an Xwindow has focus?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ed Morton writes:
> On 10/3/2008 2:01 AM, Ashish Shukla आशीष शुक्ल wrote:
>
>> Ashish
>> - --
>> () ascii ribbon campaign - against HTML e-mail
>> /\ www.asciiribbon.org - against proprietary attachments
>> ·-- ·- ···· ·--- ·- ···- ·- ·--·-· --· -- ·- ·· ·-·· ·-·-·- -·-· --- --
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v2.0.9 (GNU/Linux)
>>
>> iEYEARECAAYFAkjlw1EACgkQHy+EEHYuXnRn6gCgrWbZdk5G+t g3W0pjlWaJ9n82
>> TJcAoMJ/V+EiJpZ3e74suhZeuH4qompZ
>> =8JLy
>> -----END PGP SIGNATURE-----


> That's pretty good - being against HTML email and proprietary attachments while
> using a PGP signature in a newsgroup. It's like espousing the virtues of being a
> vegetarian while eating a hamburger.


You can parse a OpenPGP signature using GnuPG, which is a free software,
AFAIK, OpenPGP is also documented in RFC 4880.

Ashish
- --
() ascii ribbon campaign - against HTML e-mail
/\ www.asciiribbon.org - against proprietary attachments
·-- ·- ···· ·--- ·- ···- ·- ·--·-· --· -- ·- ·· ·-·· ·-·-·- -·-· --- --
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAkjmIBsACgkQHy+EEHYuXnQfPQCeLazTfZVKhH zZtrRF15esrHr4
SzkAniXopUGOrvcV7rCOAxFuMsewWOxZ
=8ZNC
-----END PGP SIGNATURE-----
Reply With Quote
  #10  
Old 10-03-2008, 10:38 AM
Default Re: How to detect if an Xwindow has focus?

In article <87wsgqw4ae.fsf@chateau.d.lf>,
Ashish Shukla श ष श ल wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>b jeswine writes:
>
>[...]
>
>> $ time xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"
>> _NET_ACTIVE_WINDOW(WINDOW): window id # 0x320cbe1, 0x0

>
>> real 0m0.144s
>> user 0m0.006s
>> sys 0m0.010s

>
>Or you can only retrieve the value of property "_NET_ACTIVE_WINDOW"
>instead of retreiving values of all properties:
>
>% xprop -root _NET_ACTIVE_WINDOW
>_NET_ACTIVE_WINDOW(WINDOW): window id # 0x105cd9d


My testing of comparing run times of xprop vs. xdpyinfo was
inconclusive, due, I think, to caching effects - i.e., the first time
retrieving the information took longer than subsequent runs. After
that, results were, as I say, inconclusive. I think it is fair to say
that both commands are basically pulling from the same information store.

Anyway, I'm glad to see that the xprop command you give (where it gets
only the property in which one is interested) does work, and, to the
extent that anything conclusive can be said about timings, seems to be
the best. It's funny; although I thought that it had to be possible, I
couldn't tease it out of the manpage that that syntax would work.

Reply With Quote
  #11  
Old 10-03-2008, 11:27 AM
Default Re: How to detect if an Xwindow has focus?

In article <87wsgqw4ae.fsf@chateau.d.lf>,
Ashish Shukla श ष श ल wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>b jeswine writes:
>
>[...]
>
>> $ time xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"
>> _NET_ACTIVE_WINDOW(WINDOW): window id # 0x320cbe1, 0x0

>
>> real 0m0.144s
>> user 0m0.006s
>> sys 0m0.010s

>
>Or you can only retrieve the value of property "_NET_ACTIVE_WINDOW"
>instead of retreiving values of all properties:
>
>% xprop -root _NET_ACTIVE_WINDOW
>_NET_ACTIVE_WINDOW(WINDOW): window id # 0x105cd9d


My testing of comparing run times of xprop vs. xdpyinfo was
inconclusive, due, I think, to caching effects - i.e., the first time
retrieving the information took longer than subsequent runs. After
that, results were, as I say, inconclusive. I think it is fair to say
that both commands are basically pulling from the same information store.

Anyway, I'm glad to see that the xprop command you give (where it gets
only the property in which one is interested) does work, and, to the
extent that anything conclusive can be said about timings, seems to be
the best. It's funny; although I thought that it had to be possible, I
couldn't tease it out of the manpage that that syntax would work.

P.S. Sorry for posting twice - I got screwed up because somebody
changed the newsgroups - I am following this thread in c.u.s.

Reply With Quote
  #12  
Old 10-03-2008, 11:29 AM
Default Junk in Usenet postings (Was: How to detect if an Xwindow has focus?)

In article <87iqs9x0ix.fsf@chateau.d.lf>,
Ashish Shukla श ष श ल wrote:
....
>You can parse a OpenPGP signature using GnuPG, which is a free software,


You can parse HTML using lynx, which is free software, etc, etc.

Which: a) completely misses the point, and b) doesn't change the fact that
HTML in Usenet postings is evil.

Reply With Quote
  #13  
Old 10-03-2008, 01:51 PM
Default Re: Junk in Usenet postings

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kenny McCormack writes:

> In article <87iqs9x0ix.fsf@chateau.d.lf>,
> Ashish Shukla *¤ *¤¶*¥ *¤· *¤¶ *¥ *¤ *¥ *¤² wrote:
> ...
>> You can parse a OpenPGP signature using GnuPG, which is a free software,


> You can parse HTML using lynx, which is free software, etc, etc.


> Which: a) completely misses the point, and b) doesn't change the fact that
> HTML in Usenet postings is evil.


but OpenPGP is not any proprietary standard or HTML.

Ashish
- --
() ascii ribbon campaign - against HTML e-mail
/\ www.asciiribbon.org - against proprietary attachments
·-- ·- ···· ·--- ·- ···- ·- ·--·-· --· -- ·- ·· ·-·· ·-·-·- -·-· --- --
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAkjmW60ACgkQHy+EEHYuXnScAACfZ6v4wuNpdA C9HoQsk4k4k9xY
mCwAnRCHmxTST7mvikiIS6vUNy5u0PZF
=7aYC
-----END PGP SIGNATURE-----
Reply With Quote
  #14  
Old 10-03-2008, 02:13 PM
Default Re: How to detect if an Xwindow has focus?

On Oct 3, 11:27*am, gaze...@shell.xmission.com (Kenny McCormack)
wrote:
> In article <87wsgqw4ae....@chateau.d.lf>,
> Ashish Shukla श ष श ल wrote:
>
>
>
> >-----BEGIN PGP SIGNED MESSAGE-----
> >Hash: SHA1

>
> >b jeswine writes:

>
> >[...]

>
> >> $ time xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"
> >> _NET_ACTIVE_WINDOW(WINDOW): window id # 0x320cbe1, 0x0

>
> >> real * *0m0.144s
> >> user * *0m0.006s
> >> sys * * 0m0.010s

>
> >Or you can only retrieve the value of property "_NET_ACTIVE_WINDOW"
> >instead of retreiving values of all properties:

>
> >% xprop -root _NET_ACTIVE_WINDOW
> >_NET_ACTIVE_WINDOW(WINDOW): window id # 0x105cd9d

>
> My testing of comparing run times of xprop vs. xdpyinfo was
> inconclusive, due, I think, to caching effects - i.e., the first time
> retrieving the information took longer than subsequent runs. *After
> that, results were, as I say, inconclusive. *I think it is fair to say
> that both commands are basically pulling from the same information store.
>
> Anyway, I'm glad to see that the xprop command you give (where it gets
> only the property in which one is interested) does work, and, to the
> extent that anything conclusive can be said about timings, seems to be
> the best. *It's funny; although I thought that it had to be possible, I
> couldn't tease it out of the manpage that that syntax would work.
>
> P.S. *Sorry for posting twice - I got screwed up because somebody
> changed the newsgroups - I am following this thread in c.u.s.


From the example at the bottom of the manpage:

EXAMPLES

To display the name of the root window: xprop -root WM_NAME
Reply With Quote
Reply

Thread Tools


All times are GMT -5. The time now is 12:50 AM.

In an effort to better serve ads to our visitors, cookies are used on Fixunix.com. For more information, check out our Privacy Policy.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Ad Management by RedTyger