Skip to content

Added modifier support to mouse events - #328

Merged
tomaka merged 1 commit into
rust-windowing:masterfrom
gilbertw1:feature-mouse-mods
Dec 26, 2017
Merged

tomaka merged 1 commit into
rust-windowing:masterfrom
gilbertw1:feature-mouse-mods

Conversation

@gilbertw1

Copy link
Copy Markdown
Contributor

This pull request adds modifier suppport to the MouseMoved, MouseWheel, and MouseInput events. This is modeled after the modifier support added for KeyboardInput in #112.

This will allow mouse events to achieve parity with keyboard events and support use cases like: alacritty/alacritty#146.

@tomaka tomaka added the S - api Design and usability label Oct 20, 2017
@elinorbgr

Copy link
Copy Markdown
Contributor

Please make it clear somewhere (for example by adding a TODO comment on top of the pointer_implementation function) that the wayland implementation of this is currently a stub.

@gilbertw1

gilbertw1 commented Oct 20, 2017 •

Copy link
Copy Markdown
Contributor Author

Will do! I'll take a quick crack at implementing modifier support for it first. I didn't previously because prior to the commit this morning all modifiers were stubs in the Wayland implementation.

@elinorbgr

Copy link
Copy Markdown
Contributor

I see. This will require some non-trivial changes to the backend, as the information of modifiers state is not currently available in the callback handling pointer events.

@gilbertw1

Copy link
Copy Markdown
Contributor Author

@vberger Thanks for the heads up. I've added a comment indicating that the Wayland implementation is currently a stub.

I will look into a solution, however it sounds as though it might be best to leave the Wayland implementation a stub and potentially implement it in a follow up pull request. In it's current state this PR only contains relatively simple changes.

@gilbertw1

Copy link
Copy Markdown
Contributor Author

Unfortunately MappedKeyboardImplementation doesn't expose the wayland modifiers callback. but Instead it uses those events only to keep it's internal modifier state up to date.

Our options for supporting modifiers on mouse events under wayland are:

  1. Update MappedKeyboardImplementation in wayland-rs to expose modifier state changes when it receives modifier events from wayland.
  2. Use wl_keyboard::Implementation instead of MappedKeyboardImplementation in order to receive the modifier events.

I'm not very familiar with any of these apis, Is there any value in using MappedKeyboardImplementation over wl_keyboard::Implementation apart from it tracking the modifier state internally?

@Ralith

Ralith commented Oct 21, 2017

Copy link
Copy Markdown
Contributor

This API change doesn't make sense to me. Mouse events aren't out of parity with keyboard events for lack of having keyboard event information in them any more than, say, resize events are. Why can't downstream apps track the state they care about themselves?

@gilbertw1

Copy link
Copy Markdown
Contributor Author

Hmmm ... it seems to make pretty good sense to me, but I guess I'm looking at it from the point X11/xlib (which I use primarily). which treats mouse motion events (XBindingEvent), mouse button click events (XButtonEvent), and keypress events (XKeyEvent) similarly, passing along modifier state with each.

Reference: https://tronche.com/gui/x/xlib/events/keyboard-pointer/keyboard-pointer.html

I don't really think comparing mouse input to resize events is a fair comparison. Mouse input such as button clicks are commonly bound to actions just as frequently as keypresses and I believe a consistent api for both would be desirable. I guess it's a question of ergonomics more than anything else, but I don't really see any dounside to including the modifier state here. The complexity of this change is very small, with supporting Wayland only being slightly non-trivial.

@gilbertw1

Copy link
Copy Markdown
Contributor Author

@Ralith Also I'm just curious, especially in case this pull request is rejected ... You make it sound pretty simple to track the state of the modifiers in a downstream app. Is there a reasobably easy way to do this in a cross platform way without reproducing much of the code in this library? How would you implement this?

@Ralith

Ralith commented Oct 21, 2017

Copy link
Copy Markdown
Contributor

The same way I'd track the state of any input; set the state on press, unset it on release. Admittedly, this may lead to desync if focus is lost across state boundaries on at least some platforms, which is why platform APIs treat modifiers specially at all. That said, I'm not happy about working around platform API design bugs by polluting winit events with random fragments of unrelated state. X11 is really not a great reference for sane API design.

The above is my opinion only; I'm not speaking on behalf of the project.

@jwilm

jwilm commented Oct 21, 2017

Copy link
Copy Markdown
Contributor

@Ralith we used to take that approach in Alacritty before winit ever supported sending mods with key input events, and it had the desync issue you’re mentioning.

@elinorbgr

elinorbgr commented Oct 21, 2017 •

Copy link
Copy Markdown
Contributor

@gilbertw1 MappedKeyboard is provided by wayland-kbd, a crate I maintain which provide keymap interpretation, so we definitely want to keep using it. On the other hand, its development is mostly driven by winit's needs, so adding a modifiers callback to it is entirely possible, and should be fairly easy to do.

Thus, I suggest to keep the possible implementation details for wayland to an other follow-up PR, and focus here on the question of whether we want this is winit at all.


At first I had the same reaction as @Ralith , aka "what does this have to do with pointer events at all?", but I understand the need of having this information when processing pointer events (many games behave differently on click/shift+click/ctrl+click. And given the way winit treats it, it's not easy for downstream apps to track them precisely, mostly in case of lost/gained focus.

To solve this, I currently see two alternatives:

  • Forward the modifiers state with every input event (what this PR starts, but we'll likely be asked for touch events or whatever in the future probably)
  • Add a new ModifiersChanged event

Thoughts @tomaka , @Ralith , @mitchmindtree ?

@jwilm

jwilm commented Oct 21, 2017

Copy link
Copy Markdown
Contributor

@vberger wouldn't a ModifiersChanged event have the exact same issue as the application tracking them manually?

Since most platforms simply include the modifier state with each native event already, it seems like the most reliable solution is to just forward them as part of the winit event. This has the added benefit of being easier for developers using winit to consume.

We discussed these same points in the PR which added keyboard mods to the winit keyboard events.

@elinorbgr

Copy link
Copy Markdown
Contributor

Since most platforms simply include the modifier state with each native event already [...]

Fair point. If wayland is the only platform to emit "modifiers changed" events, then sure, the simplest is to align it with what other platforms to.

@Ralith

Ralith commented Oct 21, 2017

Copy link
Copy Markdown
Contributor

Accurate modifier state tracking, regardless of focus, can be implemented on X11 via the XkbStateNotify event. This would be more work than the current PR, but would allow the API pollution to be totally avoided. I don't see a need for a new event type either; key press/release would do fine for communicating the information.

No idea about windows/OSX, though. At worst you could poll the modifier state before emitting other events.

@elinorbgr

Copy link
Copy Markdown
Contributor

@Ralith The main issue I think is a scenario like this:

The winit app is not focused. The user holds <shift> then click on the window. The winit client is thus focused, and receives a button click event, that it should interpret in the light of the fact that the shift modifier is currently down.

Currently, winit's API has no way to reflect this, unless maybe emulating keypresses to forward the modifiers state, but this seems wrong to me.

@Ralith

Ralith commented Oct 21, 2017

Copy link
Copy Markdown
Contributor

@vberger what's wrong with emulating keypresses to communicate the fact that some keypresses happened?

@elinorbgr

Copy link
Copy Markdown
Contributor

To be honest it's mostly edge cases.

But imagine a game has some special action bound on a modifier keypress, and the person playing this game also has a meta action configured on their desktop environment using this key as well. There could be interfering.

On the other hand, I'll admit the keypress emulation would be nice for some projects using winit, like smithay, so... I have no hard opinion on that.

@gilbertw1

Copy link
Copy Markdown
Contributor Author

Any more thoughts on this?

I personally lean towards forwarding the modifiers state with each event since the precedent is already set with keypress events, it's the simplest solution from an implementation and consumer standpoint, and removes any ambiguity. Whichever option is ultimately decided upon, I'd be happy to amend this pull request or create a new one implementing the desired behavior.

Thanks!

@jwilm

jwilm commented Dec 23, 2017

Copy link
Copy Markdown
Contributor

I'd like to propose that we move forward with the API implemented in this PR, and here's why.

  1. This API is consistent with what winit already does for KeyboardEvent
  2. Although there is desire for a different solution that doesn't extend WindowEvent::MouseInput, there has been no consensus on the proposals so far. If one these alternatives gets implemented, we are still in a place where revving the winit version is a relatively common occurrence.
  3. Merging this PR would make the capability available to applications today rather than being blocked on us designing an alternative solution.
  4. There is precedent for this in GLFW. As Glutin is presenting itself as a pure Rust alternative, there is value in trying to provide such capability in the analogous Rust way; in this case, the WindowEvent::MouseInput variant. For reference:
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
  1. Finally, it sounds like if we never devoted time to an alternative solution, the current approach can be supported across all platforms.

@jwilm

jwilm commented Dec 24, 2017

Copy link
Copy Markdown
Contributor

@Ralith @vberger what do you think about my proposal above? There's several 👍 at this point, and I wanted to make sure you had a chance to respond before merging since you both proposed alternative solutions.

@Ralith

Ralith commented Dec 24, 2017

Copy link
Copy Markdown
Contributor

I don't have code for a better solution, and the proposed solution is arguably conventional, so my concerns shouldn't be blockers.

@gilbertw1
gilbertw1 force-pushed the feature-mouse-mods branch 5 times, most recently from 939bb8e to 1222b70 Compare December 26, 2017 14:26
@gilbertw1

Copy link
Copy Markdown
Contributor Author

Just a heads up, I've rebased this PR against master and added emscripten support to it.

@tomaka tomaka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
Please also add an entry in the CHANGELOG.

device_id: ::DeviceId(::platform::DeviceId::Wayland(DeviceId)),
state: state,
button: button,
modifiers: ModifiersState::default(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a // TODO comment whenever you use this dummy value.

@gilbertw1

Copy link
Copy Markdown
Contributor Author

@tomaka I've updated the PR to relect both of your comments.

@tomaka
tomaka merged commit 0117208 into rust-windowing:master Dec 26, 2017
tmfink pushed a commit to tmfink/winit that referenced this pull request Jan 5, 2022
Only define `precision highp sampler2D` in OpenGL ES.
madsmtm pushed a commit to madsmtm/winit that referenced this pull request Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S - api Design and usability

Development

Successfully merging this pull request may close these issues.

5 participants