Phoenix icon indicating copy to clipboard operation
Phoenix copied to clipboard

XWayland is required even though wxPython programs runs under wayland

Open bhepple opened this issue 1 year ago • 10 comments

Operating system: Void Linux wxPython version & source: wxPython-4.2.1_4 from voidlinux repo Python version & source: python3-3.12.3_1

Description of the problem: On linux, wxPython programs can run under wayland for example in the sway WM.

However, if XWayland is not available and enabled they will not run:

Unable to access the X Display, is $DISPLAY set properly?

Any wxPython program will do eg:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title)
        top_panel = wx.Panel(self)
        top_panel_sizer = wx.BoxSizer(wx.VERTICAL)
        top_panel.SetSizer(top_panel_sizer)
        collapsible_pane = wx.CollapsiblePane(top_panel, label="Collapsible Pane",
                                              style=wx.CP_DEFAULT_STYLE|wx.CP_NO_TLW_RESIZE)
        self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnPaneChanged, collapsible_pane)
        pane = collapsible_pane.GetPane()
        pane_sizer = wx.BoxSizer(wx.VERTICAL)
        pane.SetSizer(pane_sizer)
        text_box = wx.TextCtrl(pane, value = "foo")
        pane_sizer.Add(text_box, 1, wx.ALL | wx.EXPAND, 5)
        top_panel_sizer.Add(collapsible_pane, 1, wx.ALL | wx.EXPAND, 5)
        
    def OnPaneChanged(self, evt=None):
        self.Layout()
        
if __name__ == "__main__":
    app = wx.App()
    frm = MyFrame(None, title="Minimal wxPython Program")
    frm.Show()
    app.MainLoop()

bhepple avatar Jun 08 '24 05:06 bhepple

Confirmed. wxPython calls XOpenDisplay unconditionally here: https://github.com/wxWidgets/Phoenix/blob/master/src/app_ex.cpp#L303

swt2c avatar Jun 11 '24 01:06 swt2c

You can probably hack around this problem by monkey-patching IsDisplayAvailable:

wx.PyApp.IsDisplayAvailable = lambda _: True

I wonder if the check still makes sense, but it does seem at least somewhat useful. If I remove it on a system with no display (e.g., over an SSH connection), wxPython startup will just hang there.

swt2c avatar Jun 11 '24 01:06 swt2c

Thanks for that - works just fine without Xwayland now.

The problem for me is that my Xwayland is rather buggy and brings down the sway session occasionally - so this should be a life changer.

bhepple avatar Jun 11 '24 03:06 bhepple

Up to you if you want to close this. I'm OK with the patch but I still think it's a bit janky that otherwise you must have Xwayland.

bhepple avatar Jun 11 '24 07:06 bhepple

No, this is definitely an issue that we need to address. I'm just not sure how exactly.

swt2c avatar Jun 11 '24 13:06 swt2c

I'm curious why this precheck function exists at all. Don't you get some sensible error when you try to actually initialize the toolkit?

nanonyme avatar Jul 30 '24 10:07 nanonyme

nanonyme - I don't normally. However with Xwayland unavailable I get this:

Unable to access the X Display, is $DISPLAY set properly?

.... and it exits with status 1.

But perhaps I misunderstood your question. If there's something you'd like me to run on sway/wayland with or without Xwayland, do let me know.

bhepple avatar Jul 30 '24 22:07 bhepple

The question was directed to maintainer as it was architectural question about this toolkit.

nanonyme avatar Jul 30 '24 23:07 nanonyme

I'm curious why this precheck function exists at all. Don't you get some sensible error when you try to actually initialize the toolkit?

Good question that I don't know the answer to. It seems this display check has existed at least 18 years, so it has been there a while. Would need to ask @RobinD42.

swt2c avatar Jul 30 '24 23:07 swt2c

Please let's do that. It seems in current state toolkit is hardcoded to only support X11 on Linux, not Wayland. 18 years ago Wayland didn't even exist. By far simplest way forward would be to remove aforementioned check and let things break later but seems need more info to make that decision.

nanonyme avatar Jul 31 '24 11:07 nanonyme

Just out of curiosity... Where does this stand?

EvilSupahFly avatar Nov 06 '24 22:11 EvilSupahFly

I do plan to work on it eventually:

  • Update IsDisplayAvailable to support Wayland also
  • Remove IsDisplayAvailable check during startup as it seems unnecessary.

In the meantime perhaps you can use the workaround listed above.

swt2c avatar Nov 06 '24 23:11 swt2c

FYI, I submitted a PR to make IsDisplayAvailable work on Wayland (and any other toolkits supported by GTK). Please let me know if this works well enough. If not, we can look into removing the check altogether.

swt2c avatar Nov 08 '24 22:11 swt2c