Search code examples
pythonxlib

How to get a variable loaded from .Xresources?


I'm using Regolith which loads a bunch of Xresources, with variables like i3-wm.font: typeface_wm in some Xresources file. As far as I understand, these should be loaded into to X11 window system, which could be read from there. How can I get this value in a python script, without needing to parse the files myself?

I found the python3-xlib module, which I suppose should be able to do this, but I couldn't really make head nor tails from it ... I understand that Xlib is pretty low level and complicated, but I am really hoping there is an Xlib.get_resource("i3-wm.font") like command that can get me the resources I need.


Solution

  • You're right, this seems complicated!

    I found some random code on github here that has some clues, though. The distilled version to allow for just reading is:

    import Xlib
    from Xlib.Xatom import RESOURCE_MANAGER, STRING
    
    res_prop = Xlib.display.Display().screen().root.get_full_property(RESOURCE_MANAGER, STRING)
    res_kv = (line.split(':', 1) for line in res_prop.value.decode().split('\n'))
    res_dict = {kv[0]: kv[1].strip() for kv in res_kv if len(kv) == 2}
    

    Though keep in mind this just grabs the entire db and shoves it into a dict so you can look up exact keys. In particular, it does none of the globbing that the rest of X pays attention to. It should be enough to get you started, however. In particular res_dict['i3-wm.font'] will get that for you.