roipoussiere
roipoussiere

Reputation: 5976

How to correctly handle CreateNotify event with python xlib?

I would like to change a window property when a user just opens it.

In this example, I'm just trying to handle CreateNotify events:

#!/usr/bin/python
import Xlib
from Xlib import X, display, error
import time

disp = Xlib.display.Display()
root = disp.screen().root
root.change_attributes(event_mask=Xlib.X.SubstructureNotifyMask)

def getProp(win, prop):
    p = win.get_full_property(disp.intern_atom('_NET_WM_' + prop), 0)
    return [None] if (p is None) else p.value

while True:
    event = disp.next_event()
    if event.type == X.CreateNotify:
        newWin = event.window
        try:
            newWinName = getProp(newWin, 'NAME')
            newWinPID = getProp(newWin, 'PID')[0]

            if newWinName and newWinPID:
                print time.strftime('%H:%M:%S'), "- new window:", newWinPID, newWinName
            else:
                print 'NAME or PID property not found.'
            print

        except Xlib.error.BadWindow:
            print "BadWindow error"

This script catch correctly "new windows" events. However the script doesn't output exactly what I thought, so I have some questions. For this example I opened a Firefox window twice.

  1. Why all these events ? I expected to get 2 events, I got at last 6.
  2. Where come from these BadWindows errors ?
  3. Why some windows don't have _NET_WM_ID and _NET_WM_NAME properties ?
  4. Since several instances of some softwares (like Firefox) have the same PID, how to select a specific window (for example to change a property for only one firefox instances) ?

I use Linux Mint Cinnamon (Muffin window manager).

There is the output:

BadWindow error
19:58:16 - new window: 10510 firefox

NAME or PID property not found.

19:58:16 - new window: 8417 Firefox

19:58:16 - new window: 8417 Firefox

BadWindow error
NAME or PID property not found.

BadWindow error
BadWindow error
19:58:20 - new window: 10519 firefox

19:58:20 - new window: 8417 Firefox

NAME or PID property not found.

19:58:20 - new window: 8417 Firefox

BadWindow error
NAME or PID property not found.

Upvotes: 2

Views: 917

Answers (1)

Ikem Krueger
Ikem Krueger

Reputation: 193

Why all these events ? I expected to get 2 events, I got at last 6.

When your mouse hover over a button, there is a popup. This popup count as "window".

Where come from these BadWindows errors ?

Popup windows.

Why some windows don't have _NET_WM_ID and _NET_WM_NAME properties ?

Only windows with a border have a _NET_WM_ID and _NET_WM_NAME property.

Windows without a border don't have them.

And a popup window is a window without a border.

Upvotes: 2

Related Questions