Search code examples
pythongtkpygtkx11

Python/X11: find out if user switches virtual desktops


I'm looking for a way to determine if the user switches virtual desktops under X11.

I'm using Python with X11 libraries and PyGTK. I found some working examples in C, but I lack the expertise to translate them into Python, and I read the source code of several X11 pager applications (fbpanel, pypanel), but I can't seem to find what I'm looking for.

Do I have to register for a signal? Using X11 or GTK? Do I have to busy-wait?

I'm completely new to both X11 and GTK, so any hints/help would be greatly appreciated.

Greets, Philip

PS: My current efforts can be found here.


Solution

  • Here is a GTK based solution:

    screen = gtk.gdk.screen_get_default()
    root = screen.get_root_window()
    root.set_events(gtk.gdk.SUBSTRUCTURE_MASK)
    root.add_filter(event_filter)
    
    def event_filter(event, user_data):
            # process event
            return gtk.gdk.FILTER_CONTINUE
    

    Apparently, the SUBSTRUCTURE_MASK contains events which are typically associated with workspace switches. Nevertheless, this solution feels a bit awkward. Any ideas?

    Greets, Philip