FlorianRhiem / pyGLFW

Python bindings for GLFW
MIT License
232 stars 36 forks source link

Switching between X11 and Wayland #69

Closed danchitnis closed 1 year ago

danchitnis commented 1 year ago

I noticed that you ship this library in Pypi with two separate binaries for X11 and Wayland versions of GLFW. But how do you enforce the choice of one or the other when both systems are present in one's device? I am debugging an issue using pyGLFW on WSL2 here.

FlorianRhiem commented 1 year ago

That is decided depending on the XDG_SESSION_TYPE environment variable. If it is wayland, then Wayland is used. Otherwise, X11 is the default. You can set the environment variable PYGLFW_LIBRARY to the absolute path of the GLFW library during debugging, to make sure the right library is used. I hope that helps!

danchitnis commented 1 year ago

Thanks. Do you think adding a switch will be helpful to select between the two manually? Since you are shipping the GLFW binaries, I think putting the switch inside pyGLFW rather than the outside would make more sense. WSLg is going to be having a confusing situation in the foreseeable future, so a manual switch is needed to help the end users.

FlorianRhiem commented 1 year ago

Sure, XDG_SESSION_TYPE is meant to serve as a best guess, but a pyGLFW specific environment variable could be used to force using wayland or x11, without having to specify the variable using PYGLFW_LIBRARY.

I've implemented something like this in the feature-pylgfw-library-variant branch. You should be able to set PYGLFW_LIBRARY_VARIANT to wayland or x11 to select either variant instead of picking based on XDG_SESSION_TYPE. Please give it a try and let me know if that solves the issue for you!

danchitnis commented 1 year ago

Thanks for implementing this. So, in general, it works, but there is a minor bug.

When I set XDG_SESSION_TYPE to wayland it uses Wayland as expected.

When I set XDG_SESSION_TYPE to anything random, it uses X11 as expected.

However, if I set XDG_SESSION_TYPE to x11, then it gives me a context creation error.

Your code logic seems correct. Are you using XDG_SESSION_TYPE anywhere else in the library? In WSLg, the context creation works with EGL, and by setting the OpenGL version to 1.5, so I wonder if you are modifying these elsewhere based on XDG_SESSION_TYPE.

FlorianRhiem commented 1 year ago

No, XDG_SESSION_TYPE is only checked by pyGLFW to add a search path for finding the library. When you get that context creation error, you can check glfw._glfw, the internal handle for the loaded GLFW library, to see which one is being used.

danchitnis commented 1 year ago

I think XDG_SESSION_TYPE is used elsewhere in the system. I tried PYGLFW_SESSION_TYPE instead, and it works as expected. Can you change XDG_SESSION_TYPE to PYGLFW_SESSION_TYPE in your code? Also, are you happy to merge into the main branch?

FlorianRhiem commented 1 year ago

Yes, XDG_SESSION_TYPE is an existing environment variable, that pyGLFW detects to see if wayland or X11 is active. The solution implemented on the branch mentioned above does not rely on that, but instead adds support for PYGLFW_LIBRARY_VARIANT, so I'm a bit confused why I should also support a new PYGLFW_SESSION_TYPE in addition to that?

danchitnis commented 1 year ago

I mistook PYGLFW_LIBRARY_VARIANT with PYGLFW_LIBRARY! yes, everything is working as expected in this branch. No need for a new variable. Still appreciate if you could merge and publish on Pypi.

FlorianRhiem commented 1 year ago

Done!