wails icon indicating copy to clipboard operation
wails copied to clipboard

[v2] [Linux] Feat - Wayland Support

Open achhabra2 opened this issue 3 years ago • 9 comments

Is your feature request related to a problem? Please describe.

Fedora, Ubuntu, and other distros are moving to Wayland and moving away from X11. Currently wails will use the XWayland compatibility layer. It would be good to eventually support Wayland.

I first found out this was an issue because my app was listed on Flathub and I had used one of the template flatpak xml files that listed Wayland as the default socket. Users who were actually running Wayland would not be able to use the app due to GTK errors.

I figured out that this is due to the --socket=wayland finish arg. Here's the older flatpak xml I was using for reference.

This is the error that was coming up: Gtk-WARNING **: 15:42:30.500: cannot open display:

The fix was to force the X11 session -- see the updated flatpak xml

I am not a Linux expert by any means so I'm not sure what the longer term implications are, but thought it would be prudent to raise the request.

Describe the solution you'd like

Support for Wayland Sessions

Describe alternatives you've considered

Fallback to X11 / XWayland

Additional context

See the detailed description above

achhabra2 avatar May 22 '22 16:05 achhabra2

Hey 👋 Thanks for raising this. I'm no Linux expert either (at least not with Wayland!) so I'm not sure what's involved. Is this because the gtk libraries don't support it?

leaanthony avatar May 22 '22 22:05 leaanthony

I'm pretty clueless in this area and haven't had time to research it yet. I think the gtk libraries would support it. It may be a matter of using newer libraries, or potentially a different way of instantiating the window, maybe using a different api?

Some quick googling says things should just work with the right environment variables set, but doesn't work for me.

Anyway, we will probably need someone with more experience on this subject to comment unfortunately.

achhabra2 avatar May 23 '22 01:05 achhabra2

Let's see if others flag this and have a solution. Thanks for raising this 🙏

leaanthony avatar May 23 '22 05:05 leaanthony

https://github.com/wailsapp/wails/blob/794a8ce19f107106c6cd9d1f64569de0def16dd0/v2/internal/frontend/desktop/linux/frontend.go#L62

GTK3/GTK4 have wayland support and default to wayland for a long time, no need any environment variable, no special way to instantiating window, no special api ..... Unless you tell it not to use. GDK_BACKEND=x11 is used for forcing GTK fallback to X11. I don't know why you did it in the first place but just remove that line everything should be fine.

zoritle avatar May 28 '22 16:05 zoritle

Yeah, I remember that it was needed to make center work. It really didn't seem to work without it, like Wayland wasn't supported properly.

leaanthony avatar May 29 '22 07:05 leaanthony

@achhabra2 - Does commenting out https://github.com/wailsapp/wails/blob/794a8ce19f107106c6cd9d1f64569de0def16dd0/v2/internal/frontend/desktop/linux/frontend.go#L62 improve things for you?

leaanthony avatar Jun 22 '22 10:06 leaanthony

Yes it did -- kind of. I removed that line but somehow the variable was still coming through as GDK_BACKEND=x11. I tested this by printing out the environment variable in my main function before starting the wails app. So I ended up hard coding it to GDK_BACKEND=wayland,x11 and that seemed to fix it.

Incorporating both of the changes were needed to fully have a working wayland setup. I didn't have time to fully troubleshoot the chain of what was going on. In the end I did get it to work though :)

Btw -- I think you are correct in that centering windows would be broken on wayland since that functionality isn't supported (and I don't believe they intend to). Basically they don't want applications to be able to position windows absolutely on screen. Philosophically the window manager should decide where to place windows. I personally don't care about needing to center a window, but I know you had mentioned it in the previous comment. I found this issue that may provide more context.

achhabra2 avatar Jun 23 '22 04:06 achhabra2

Thanks for the feedback @achhabra2! My take away from this is that it should be configurable, with an explicit default in the templates. The linked issue highlights why supporting Linux is really hard.

leaanthony avatar Jun 23 '22 08:06 leaanthony

Totally agree. Thanks for all the work on this though. I know Linux is tough, but actually what I realized is that at least for my app there are significantly more Linux users than windows / mac users (like 3X). Just going off of downloads. Hopefully if we get some more visibility to this project there will be more community contribution.

achhabra2 avatar Jun 23 '22 13:06 achhabra2

Just weighing in that I was able to remove the GDK_BACKEND line (and the import of "os") and build the same application as @achhabra2 without any issues running in x11 or wayland, so it's possible whatever was requiring it to be set to "wayland,x11" is no longer an issue?

Here's the build script that produces a correctly-behaving binary (one that runs in a sway session where xwayland is disabled)-- lines 37 and 38 are where I've modified wails https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=riftshare

prurigro avatar Aug 30 '22 21:08 prurigro

Just weighing in that I was able to remove the GDK_BACKEND line (and the import of "os") and build the same application as @achhabra2 without any issues running in x11 or wayland, so it's possible whatever was requiring it to be set to "wayland,x11" is no longer an issue?

It's set under the app here https://github.com/achhabra2/riftshare/blob/805df56877bd664d64d6fef02fe7746906fddc57/main.go#L31

Here's the build script that produces a correctly-behaving binary (one that runs in a sway session where xwayland is disabled)-- lines 37 and 38 are where I've modified wails https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=riftshare

Wow that's awesome! Thanks for sharing

achhabra2 avatar Aug 30 '22 21:08 achhabra2

The reason it was there originally was because without it, Centering a window would fail to do anything.

leaanthony avatar Aug 30 '22 22:08 leaanthony

It's set under the app here https://github.com/achhabra2/riftshare/blob/805df56877bd664d64d6fef02fe7746906fddc57/main.go#L31

Oh curious-- I actually tried without patching those lines out of wails and the app wouldn't start up in sway with xwayland disabled, so I know for sure that what I changed had an effect... Is it possible that your tweak causes it to load in wayland eventually, but that wails executes earlier and ends up requiring xwayland for the initial execution?

Wow that's awesome! Thanks for sharing

Thanks for building RiftShare! I've been looking for something like croc and magic-wormhole that I could share with my windows and macos using friends who prefer GUIs for a while now, and this fits the bill perfectly :)

The reason it was there originally was because without it, Centering a window would fail to do anything.

I think this use case might not be applicable to RiftShare so I can't say if it's no longer an issue. That said, I'm not all that well versed in golang but if there's a way to read environment variables, you could first check to see if $GDK_BACKEND is set and leave it alone if it is, or if it isn't then $XDG_SESSION_TYPE will usually be set to either x11 or wayland and you could use that to set $GDK_BACKEND appropriately, falling back on x11 if you can't get anything else.

prurigro avatar Aug 30 '22 22:08 prurigro

We can improve this for sure. I'm happy to accept a PR on it if you fancy a go?

leaanthony avatar Aug 30 '22 22:08 leaanthony

I suppose it could be a good chance for me to get my feet wet in golang-- I'll see what I can do!

prurigro avatar Aug 31 '22 00:08 prurigro

@leaanthony Hey, I've created a pull request with improved logic that doesn't break things in wayland.

@achhabra2 In case you're curious, I updated my package for your app with a patch that removes where you've set the GDK_BACKEND and patches the above PR into wails. Assuming the PR gets accepted, once it makes it into a release you can safely remove that line in your project :) https://aur.archlinux.org/cgit/aur.git/tree/gdk-backend.patch?h=riftshare

prurigro avatar Aug 31 '22 05:08 prurigro