Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'use-x-over-wayland' feature #182

Closed
wants to merge 1 commit into from
Closed

Add 'use-x-over-wayland' feature #182

wants to merge 1 commit into from

Conversation

alexheretic
Copy link
Contributor

@alexheretic alexheretic commented May 17, 2017

As the wayland code is currently not working well enough under a gnome wayland session (Ie currently the window example renders nothing), I've added a feature to allow XWayland usage, ie simply use X when in a wayland session. Now in a gnome wayland session I can run the window example with cargo run --example window --features use-x-over-wayland

It is debatable whether this should be default until the wayland code is in a better state. In any case this feature will allow me to use this lib in wayland.

@alexheretic
Copy link
Contributor Author

This can serve as a general work around for wayland-window issues ie #163 #109

@mitchmindtree
Copy link
Contributor

Ie currently the window example renders nothing

#188 should have solved this particular issue. I'm also in the middle of trying to fix this issue with @vberger's help, along with a couple other smaller winit issues, so hopefully the situation will have improved a little over the next few days/weeks.

That said, I agree that this would be a useful feature, even if only for the fact that the wayland backend won't be able to provide native looking window decorations for the forseeable future. It would probably also make testing the x11 backend much easier for wayland users.

I'd be happy to see this merged, but would prefer to wait for @tomaka to give the sign-off as this adds a new feature.

@tomaka
Copy link
Contributor

tomaka commented May 24, 2017

My opinion is that this is a very wrong usage of Cargo features, and that this thing should be decided at runtime.

@mitchmindtree
Copy link
Contributor

Perhaps this option could be better provided via the os::unix module somehow?

@alexheretic
Copy link
Contributor Author

alexheretic commented May 24, 2017

It probably would be more natural to choose at runtime by the library consumer, however the current code has it baked in to prefer wayland over x via static initialisation. Indeed the current API design implies exactly one backend chosen at compile time configured in the Cargo.toml file (using the target os).

Here we have something new, 2 different backends on the same target os.

Since winit chooses it's backend at compile time, and the API relies on there being only one, I see the following choices:

  • Redesign the API for all platforms to allow backend choice.
  • Fix Wayland backend so it is on par with X.
  • Allow unix backend preference at compile time

Obviously we want the second one, in the meantime with this PR I'm suggesting the last option. It has no effect on the API, will break nothing and is a small code change. It simply allows use of XWayland, and such usage is currently very desirable and will be useful as long as there are positive differences between X backend and Wayland.

@alexheretic
Copy link
Contributor Author

I suppose we could use an environment variable instead. Would that be preferred?

@tomaka
Copy link
Contributor

tomaka commented May 25, 2017

My opinion on this is that in an ideal world you would have Cargo features to enable/disable each backend individually, and backends would be enabled by default on their corresponding platform.

For example, the x11 Cargo feature would enable/disable the X11 backend. It would be enabled by default on Linux and disabled by default on Windows, but you could enable it and use it on Windows if you wanted to.

Then you could decide at runtime in which order the backends should be tried, or create a window with a specific backend.

When it comes to environment variables, I think it's a bad design when a library changes its behavior depending on an environment variable. I know the SDL does that for example, but in my opinion it is better when a library has a precise, controllable behavior from the point of view of the application that uses it.

@alexheretic
Copy link
Contributor Author

Yes I agree that the top level app dev should be able to choose between x and wayland. If we did it at runtime the switch would need to be done before any call to winit functions.

So a functional configuration could look like this:

fn main() {
    winit::preconfig::use_x11_linux_backend().unwrap(); // or some equivalent fn call
    // panics if backend already initialised

    let events_loop = winit::EventsLoop::new(); // call statically initialises backend
    winit::WindowBuilder::new().build(&events_loop).unwrap();
}

Or we could use an env variable USE_X11_LINUX_BACKEND that affects the static initialisation.
I don't think I agree than env vars are bad. In a way they are more natural as you set them before running by default (unlike the function that will panic, or just not work if used after winit-init). They also mean the end user can decide "wayland isn't working for me on my distro for whatever reason" and try to use XWayland.

Are there other better ways to introduce runtime configuration?

@mitchmindtree
Copy link
Contributor

mitchmindtree commented May 25, 2017

If we were to go with a function call that sets some global like in your example, it would probably be better to use the existing os module (which handles OS-specific code), e.g. winit::os::unix::prefer_x11_over_wayland or something like this. However, I imagine it would probably be more suitable to add this as an alternative EventsLoop constructor (impld in the os::unix module), as a user shouldn't be able to call this after an EventsLoop has already been created.

Alternatively, winit could provide an enum which enumerated the available backends of the platform along with an EventsLoop builder that allowed specifying the order of preference, e.g. EventsLoop::with_backend_preferences(&[Backend::X11, Backend::Wayland]) or something along these lines.

@alexheretic
Copy link
Contributor Author

alexheretic commented May 25, 2017

All the winit functions initialise the backend though, not just EventLoop construction. Ie winit::get_available_monitors(). But I suppose EventsLoop::with_backend_preferences could just panic if the already initialised backend is invalid to that selection.

By tethering the choice to EventsLoop you make EventsLoop the definitive first call to winit. I'm not sure that's best with the other public functions being around. (Although I'm not fully aware of the general design directions of winit).

Maybe static init is bad. In general I prefer explicit instance initialisation without static happenings.

fn main() {
    let winit_context = winit::Context::builder()
        .prefer_x11_over_wayland()
        .build();
    // winit_context.get_available_monitors();
    // winit_context.get_primary_monitor();
    // winit_context.new_events_loop() // or maybe EventsLoop::new(&winit_context)

    winit::WindowBuilder::new().build(&winit_context).unwrap();
}

At the moment it seems like you're using EventsLoop (shouldn't it be 'EventLoop' anyway?), as both a handle on events for the user, and a context for the window. So maybe the separation would be a good thing.

You'd also be able to build two differently configured contexts in the same program.

But we're moving into the whole 'API redesign' thing I mentioned earlier. I'm sure you guys don't particularly care how I prefer to design APIs, but I do care about being able to use winit in a wayland session this year. Can I?

@mitchmindtree
Copy link
Contributor

In generally I prefer explicit instance initialisation without static happenings.

I agree with this, I'd be happy to see removal of the global stuff. Though I think in your example the Window should probably still be built with the EventsLoop (it could get a handle to the context via the EventsLoop while registering with it for events).

shouldn't it be 'EventLoop' anyway?

I agree but this should be discussed in its own issue (or in a PR that fixes it).

I'm sure you guys don't particularly care how I prefer to design APIs, but I do care about being able to use winit in a wayland session this year.

I personally do care and would prefer to see winit's API improved. Whether or not we should add a temp hack for this in the meantime is probably up to tomaka.

@alexheretic
Copy link
Contributor Author

Well in any case it looks like this PR is the wrong approach. So how about we:

  • Work on removing the static init assumptions of the API and allow the flexibility to do stuff like select backend preference at runtime (a bit like above example, to be discussed in a new issue)
  • Add an environment variable WINIT_PREFER_BACKEND=X11 (or equivalent) logic now, to be removed when this desired functionality can be done properly in the API

If we get non-static init to work, the PR would naturally remove the env variable logic around the statically initialised backend. @mitchmindtree @tomaka would you agree that this is an ok compromise?

Perhaps I can help with both. In any case thank you for taking the time to look at this, with your answers I think we can close this PR.

@alexheretic
Copy link
Contributor Author

Closed in favour of #199

tmfink pushed a commit to tmfink/winit that referenced this pull request Jan 5, 2022
madsmtm added a commit to madsmtm/winit that referenced this pull request Jun 11, 2022
* refactor(windows): `begin_resize_drag` now similar to gtk's (rust-windowing#200)

* refactor(windows): `begin_resize_drag` now similart to gtk's

* fix

* feat(linux): skipping taskbar will now also skip pager (rust-windowing#198)

* refactor(linux): clean dummy device_id (rust-windowing#195)

* refactor(linux): clean dummy device_id

* fmt

* feat(linux): allow resizing undecorated window using touch (rust-windowing#199)

* refactor(windows): only skip taskbar if needed when `set_visible` is called (rust-windowing#196)

* fix: increase borderless resizing inset (rust-windowing#202)

* fix: increase borderless resizing inset

* update some comments

* Replace winapi with windows crate bindings shared with WRY (rust-windowing#206)

* fix(deps): update rust crate libayatana-appindicator to 0.1.6 (rust-windowing#190)

Co-authored-by: Renovate Bot <[email protected]>

* Add Windows crate and webview2-com-sys bindings

* Initial port to webview2-com-sys

* Finish conversion and remove winapi

* Fix renamed lint warning

* Fix all match arms referencing const variables

* Put back the assert instead of expect

* Point to the published version of webview2-com-sys

* Cleanup slightly weird BOOL handling

* Replace mem::zeroed with Default::default

* Add a summary in .changes

* Remove extra projects not in config.json

* Fix clippy warnings

* Update to 32-bit compatible webview2-com-sys

* Better fix for merge conflict

* Fix clippy errors on Windows

* Use path prefix to prevent variable shadowing

* Fix Windows clippy warnings with nightly toolchain

* Fix Linux nightly/stable clippy warnings

* Fix macOS nightly/stable clippy warnings

* Put back public *mut libc::c_void for consistency

* Re-run cargo fmt

* Move call_default_window_proc to util mod

* Remove unnecessary util::to_wstring calls

* Don't repeat LRESULT expression in match arms

* Replace bitwise operations with util functions

* Cleanup more bit mask & shift with util fns

* Prefer from conversions instead of as cast

* Implement get_xbutton_wparam

* Use *mut libc::c_void for return types

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Renovate Bot <[email protected]>

* fix(keyboard): add mapping for space key on Windows (rust-windowing#209)

* fix(keyboard): add mapping for space key on Windows

* change file

* feat: impl Clone for EventLoopWindowTarget (rust-windowing#211)

* chore: add `on_issue_closed.yml` (rust-windowing#214)

* Update tray dependency version (rust-windowing#217)

* Delete on_issue_closed.yml (rust-windowing#221)

* refactor(linux): event loop (rust-windowing#233)

* Use crossbeam::channel

* Fix crossbeam channel import

* Add check on poll event

* Fix deadlock when unregistering shortcut on Linux (rust-windowing#230)

* Add fullscreen monitor selection support on Linux (rust-windowing#235)

* Add fullscreen monitor support on Linux

* Add change file

* Remove todo on videomode

* Fix clippy

* Update to 2021 edition (rust-windowing#236)

* Update to 2021 edition

* Fix clippy

* Add run_return on Linux (rust-windowing#237)

* Add run_return on Linux

* Add main context

* Add run_return trait on Linux (rust-windowing#238)

* Fix: rust-windowing#239 Update webview2-com and windows crates (rust-windowing#240)

* Replace webivew2-com-sys with prebuilt windows

* Use windows utility instead of direct GetLastError

* Bump windows version and add changelog

* Run cargo fmt

* Restore inverted matches macro

* Scope constants in match arms

* Fix inverted null check

* Update src/platform_impl/windows/util.rs

Co-authored-by: Amr Bashir <[email protected]>

* Use env_logger instead of simple_logger (rust-windowing#241)

* Use env_logger instead of simple_logger

* Make clippy happy

* Cherry pick commits to next (rust-windowing#244)

* feat(macos): Add `unhide_application` method, closes rust-windowing#182 (rust-windowing#231)

* feat(macos): Add `unhide_application` method

* Update src/platform/macos.rs

Co-authored-by: Amr Bashir <[email protected]>

* Reanme to `show_application()`

* Remove broken doc link

Co-authored-by: Amr Bashir <[email protected]>

* feat: Allow more strings to parse to keycode (rust-windowing#229)

* feat: support accelerator key strings `,` `-` `.` `Space` `Tab` and `F13`-`F24` (rust-windowing#228)

* feat(macOS): support more accelerator key strings

* Move function keys together

* Add `,` `-` `.` `Space` `F20-F24` for Windows

* Remove support for accelerators not found in `winapi`

* Add `,` `-` `.` `Space` `F13-F24` for Linux

* Update .changes

* Add the rest for Windows

* Add the rest for Linux

* Add the rest on macOS

* Update accelerator-strings.md

* Fix git comments

Co-authored-by: Kasper <[email protected]>
Co-authored-by: Amr Bashir <[email protected]>

* Add redraw events on Linux (rust-windowing#245)

* Add redraw events on Linux

* Update doc of RequestRedraw Event

* Add change file

* Fix missing menu bar on borderless window (rust-windowing#247)

Credit goes to irh's work on winit commit f2de847

* refactor: improve `set_skip_taskbar` impl on Windows (rust-windowing#250)

* fix: emit errors on parsing an invalid accelerator for string, closes rust-windowing#135 (rust-windowing#252)

* chore: update comment

* fix(linux): fix focus events not firing properly (rust-windowing#253)

* fix(linux): fix focus events not firing properly

* add changelog

* chore: update focus events error message

* chore: fmt

* fix: revert windows-rs 0.28 version bump

* fix(linux): fix native menu items (rust-windowing#256)

* chore: remove examples commited by accident

* Update `ReceivedImeText` (rust-windowing#251)

* Allow receiving text without Ime on Windows

* Avoid panic todo

* Receive text without ime on mac

* Fix CursorMoved event on Linux

* Add ReceivedImeText on Linux

This only add Simple IME from GTK for now. We should add the actual IME
from system in the future.

* Fix redraw event that causes inifinite loop (rust-windowing#260)

* Fix redraw event that causes inifinite loop

* Refactor event loop

* Remove unused function

* Add doc comment on linux's run_return

* Ignore doc test on run_return

* Add non blocking iteration on Linux (rust-windowing#261)

* Docs: SystemTrayExtWindows::remove() is gone (rust-windowing#262)

Fix docs following rust-windowing#153

* Fix busy loop on Linux (rust-windowing#265)

* Update windows crate to 0.29.0 (rust-windowing#266)

* Update to windows 0.29.0

* Add change description

* Remove clippy check (rust-windowing#267)

* refactor(windows): align util function with win32 names

* chore: update PR template

* fix(linux): fire resized & moved events on min/maximize, closes rust-windowing#219 (rust-windowing#254)

* feat(linux): implement `raw_window_handle()` (rust-windowing#269)

* chore(deps): update to raw-window-handle 0.4

* add linux raw-window-handle support

* update macos/ios/android

* fix ios

* Fix core-video-sys dependency (rust-windowing#274)

* The `cocoa` crate links to AppKit, which made the symbol `CGDisplayCreateUUIDFromDisplayID` from ApplicationServices/ColorSync (which AppKit uses internally) available to us on macOS 10.8 to 10.13. (rust-windowing#275)

However, this does not work on macOS 10.7 (where AppKit does not link to ColorSync internally). Instead of relying on this, we should just link to ApplicationServices directly.

* Fix some invalid msg_send! usage (rust-windowing#276)

* Revert "Fix some invalid msg_send! usage (rust-windowing#276)" (rust-windowing#277)

This reverts commit a3a2e0cfc49ddfa8cdf65cf9870fb8e3d45b4bc0.

* Revert "The `cocoa` crate links to AppKit, which made the symbol `CGDisplayCreateUUIDFromDisplayID` from ApplicationServices/ColorSync (which AppKit uses internally) available to us on macOS 10.8 to 10.13. (rust-windowing#275)" (rust-windowing#279)

This reverts commit 6f9c468f26ddb60e29be2139397bfaf3b30eab1e.

* The `cocoa` crate links to AppKit, which made the symbol `CGDisplayCreateUUIDFromDisplayID` from ApplicationServices/ColorSync (which AppKit uses internally) available to us on macOS 10.8 to 10.13. (rust-windowing#280)

However, this does not work on macOS 10.7 (where AppKit does not link to ColorSync internally). Instead of relying on this, we should just link to ApplicationServices directly.

Co-authored-by: madsmtm <[email protected]>

* Fix some invalid msg_send! usage (rust-windowing#278)

Co-authored-by: madsmtm <[email protected]>

* Add exit code to ControlFlow::Exit (rust-windowing#281)

* Add exit code to ControlFlow::Exit

* Cargo fmt

* Add change files

Co-authored-by:  multisn8 <[email protected]>

* Add new_any_thread to Unix event loop (rust-windowing#282)

* Update windows crate to 0.30.0 (rust-windowing#283)

* Update windows crate to 0.30.0

* Simplify new-type usage

* Fix boxing in GWL_USERDATA

* Make sure everyone is using Get/SetWindowLongPtrW

* build the system_tray module when "ayatana" feature is enabled (rust-windowing#285)

Without those cfg feature checks, the "ayatana" feature does
actually not enable anything.

* Fix click events missing whe tray has menu (rust-windowing#291)

* Fix click events missing whe tray has menu

* Add change file

* Fix crash when tray has no menu (rust-windowing#294)

* chore: update pull request commit exmple

* fix(windows): send correct position for system tray events, closes rust-windowing#295 (rust-windowing#300)

* fix(windows): revert maximized state handling to winit impl, closes rust-windowing#193 (rust-windowing#299)

* fix(windows): revet maximized state handling to winit impl, closes rust-windowing#193

* add chanefile [skip ci]

* fix: `MenuItem::Quit` on Windows (rust-windowing#303)

* fix: `MenuItem::Close` on Windows

* use `PostQuitMessage` instead

Co-authored-by: amrbashir <[email protected]>

* feat: v1 audit by Radically Open Security (rust-windowing#304)

* Update to gtk 0.15 (rust-windowing#288)

* Update to gtk 0.15

* Fix picky none on set_geometry_hint

* Fix CursorMoved position

Co-authored-by: Amr Bashir <[email protected]>
Co-authored-by: Bill Avery <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Renovate Bot <[email protected]>
Co-authored-by: Lucas Fernandes Nogueira <[email protected]>
Co-authored-by: Kasper <[email protected]>
Co-authored-by: amrbashir <[email protected]>
Co-authored-by: Jay Oster <[email protected]>
Co-authored-by: madsmtm <[email protected]>
Co-authored-by: multisn8 <[email protected]>
Co-authored-by: Aurélien Jacobs <[email protected]>
Co-authored-by: Lucas Fernandes Nogueira <[email protected]>
madsmtm pushed a commit to madsmtm/winit that referenced this pull request Jun 11, 2022
* feat(macos): Add `unhide_application` method, closes rust-windowing#182 (rust-windowing#231)

* feat(macos): Add `unhide_application` method

* Update src/platform/macos.rs

Co-authored-by: Amr Bashir <[email protected]>

* Reanme to `show_application()`

* Remove broken doc link

Co-authored-by: Amr Bashir <[email protected]>

* feat: Allow more strings to parse to keycode (rust-windowing#229)

* feat: support accelerator key strings `,` `-` `.` `Space` `Tab` and `F13`-`F24` (rust-windowing#228)

* feat(macOS): support more accelerator key strings

* Move function keys together

* Add `,` `-` `.` `Space` `F20-F24` for Windows

* Remove support for accelerators not found in `winapi`

* Add `,` `-` `.` `Space` `F13-F24` for Linux

* Update .changes

* Add the rest for Windows

* Add the rest for Linux

* Add the rest on macOS

* Update accelerator-strings.md

* publish new versions

Co-authored-by: Kasper <[email protected]>
Co-authored-by: Amr Bashir <[email protected]>
Co-authored-by: wusyong <[email protected]>
Co-authored-by: Ngo Iok Ui (Wu Yu Wei) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants