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

Added touch gesture to companion protocol #2402

Merged
merged 52 commits into from
Jul 30, 2024
Merged

Conversation

albaintor
Copy link
Contributor

Hi, I have added the implementation of touch gestures and updated the documentation.

I have tested it successfully.
To test it :

atv = await pyatv.connect(config, loop)
companion = cast(FacadeRemoteControl, atv.remote_control).get(Protocol.Companion)
if companion:
    api: CompanionAPI = companion.api
    # Simulate a gesture from x=100, y=100 to x=1000, y=1000 in 1000 milliseconds
    await api.touch_gesture(100, 100, 1000, 1000, 1000)

@albaintor
Copy link
Contributor Author

albaintor commented Jun 18, 2024

Hi, I have added the new interface and its implementation including the facadeAppleTV class and brought a fix to my code.
Now it works more simpler :

atv = await pyatv.connect(config, loop)
    feature = atv.features.get_feature(FeatureName.TouchGesture)
    print("Feature : "+feature.state.name)
    await atv.touchgestures.touch_gesture(900, 900, 10, 10, 1000)

I have added an additional method in the new interface. This method above is kind of a helper function. But if one wants to simulate a customized gesture there is the touch_event also (I'm thinking of a slider in a UI that would generate events constantly based on slider position)

@IsakTheHacker
Copy link

Hi, amazing work you've done here! Have been waiting for this for so long. Do you think it would be possible to have this working for real time gestures, just like the way it works for the Siri Remote and in control center on iOS? I have no idea how such an interface would work or even look like though, but that would be very awesome. Everything looks exceptionally good by the way and I hope @postlund can take a look at this soon

@albaintor
Copy link
Contributor Author

albaintor commented Jun 18, 2024

Hi, I had a great help from @postlund or else I couldn't find out how to reproduce the streams.

As written above I implemented different methods :

  1. Touch_gesture that is a helper function to simulate touch from point A to point B in a given time (shorter time means faster move as well as the distance between A & B). It should be used if you want to map common gestures (horizontal swipe from left to right or opposite) to a single command.
  2. Touch_event is the base method for gestures : this would be used for more advanced usage. For example if you press a slider from left to right then right to left without releasing it, you would have to synchronize the range of the slider with the range of the touchpad every few milliseconds. Same approach for a virtual touchpad

I didn't implement tap/double tap although this is possible as I think that those commands are already available in the library

@albaintor
Copy link
Contributor Author

albaintor commented Jun 19, 2024

Touchpad.tool.1.mp4

I have made a tool to simulate the touchpad gestures and the right algorithm behind
Available in the repository : pyatv/scripts/atvui.py

Copy link
Owner

@postlund postlund left a comment

Choose a reason for hiding this comment

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

A few comment but it looks very good, nice addition! 👍

@postlund
Copy link
Owner

A section in the atvremote documentation with some examples would be great as well!

@postlund
Copy link
Owner

One last thing that I forgot about... Some kind of basic tests for the added API methods are needed. Only a "good case" is needed, just to ensure we verify something. Should be fairly easy to add in https://github.com/postlund/pyatv/blob/master/tests/protocols/companion/test_companion_functional.py

@albaintor
Copy link
Contributor Author

A section in the atvremote documentation with some examples would be great as well!

Yes I will add that

@albaintor
Copy link
Contributor Author

One last thing that I forgot about... Some kind of basic tests for the added API methods are needed. Only a "good case" is needed, just to ensure we verify something. Should be fairly easy to add in https://github.com/postlund/pyatv/blob/master/tests/protocols/companion/test_companion_functional.py

I don't think this is possible : touch events are sent with no feedback by Appletv

@postlund
Copy link
Owner

One last thing that I forgot about... Some kind of basic tests for the added API methods are needed. Only a "good case" is needed, just to ensure we verify something. Should be fairly easy to add in https://github.com/postlund/pyatv/blob/master/tests/protocols/companion/test_companion_functional.py

I don't think this is possible : touch events are sent with no feedback by Appletv

I have implanted fake "server sides" of each protocol. So you can just store the events and verify what you received. No interaction with an Apple TV is used (it's just unit tests).

@albaintor
Copy link
Contributor Author

One last thing that I forgot about... Some kind of basic tests for the added API methods are needed. Only a "good case" is needed, just to ensure we verify something. Should be fairly easy to add in https://github.com/postlund/pyatv/blob/master/tests/protocols/companion/test_companion_functional.py

I don't think this is possible : touch events are sent with no feedback by Appletv

I have implanted fake "server sides" of each protocol. So you can just store the events and verify what you received. No interaction with an Apple TV is used (it's just unit tests).

Ok got it, I am on it

@albaintor
Copy link
Contributor Author

Hi,
all done. I'll let you review. I have also updated the atvremote script to support touch calls

@albaintor
Copy link
Contributor Author

As an example :

# Horizontal touch gesture from left to right in 200ms
atvremote.py --id <appletvid> touch=0,0,800,0,200
# Touch click
atvremote.py --id <appletvid> touch_click

@albaintor albaintor requested a review from postlund June 21, 2024 11:21
@postlund
Copy link
Owner

I'll try to look at it tonight, great work so far! Also remember that we should add some developer documentation as well at some point, in case someone wants to use it (a subpage here https://pyatv.dev/development/).

@albaintor
Copy link
Contributor Author

albaintor commented Jun 21, 2024

I'll try to look at it tonight, great work so far! Also remember that we should add some developer documentation as well at some point, in case someone wants to use it (a subpage here https://pyatv.dev/development/).

Yes I a little new to python's coding (since this year), however I found your code very well organized apart all the offered features for all-in-one library.
I got a little struggled though by the fact that I had to apply modifications in several files (don't remember exactly but : companion/api, companion/protocol, interfaces, and facadeappletv), so yeah it would deserve a howto guide to develop

@albaintor
Copy link
Contributor Author

@albaintor There are a few linting stuff to fix.

@albaintor albaintor closed this Jul 21, 2024
@albaintor
Copy link
Contributor Author

Hi,
it was sorting issues. Should be fixed now thanks

@postlund postlund reopened this Jul 21, 2024
@postlund
Copy link
Owner

@albaintor I don't think you managed to push any changes?

@albaintor
Copy link
Contributor Author

@albaintor I don't think you managed to push any changes?

Are you sure ? I have pushed everything on my side
For example : pyatv/interfaces.py:1284 --> method renamed click instead of touch_click (N-1 commit)
And linting fix pushed from albaintor@d307a8c
For example : pyatv/core/facade.py
image

@postlund
Copy link
Owner

@albaintor I just noted that there are no pushed changes after my last comment according to the PR. There are also still checks that are failing, you can check that below.

@albaintor
Copy link
Contributor Author

albaintor commented Jul 22, 2024

Hi I think I know what is going on : something is wrong on the build action
image

The black command fails with the new "match case" code in tests/fake_device/companion.py
The target python should be added in the black : -t py310 or whatever

EDIT :
Addin black --target-version py310 --fast --check . within chickn.yaml would resolve the problem in order to accept python >3.10 match / case routines. But it also raises new linting issues. Just want to be sure what do you want to do from there
See ref psf/black#2242

@postlund
Copy link
Owner

Hi I think I know what is going on : something is wrong on the build action image

The black command fails with the new "match case" code in tests/fake_device/companion.py The target python should be added in the black : -t py310 or whatever

EDIT : Addin black --target-version py310 --fast --check . within chickn.yaml would resolve the problem in order to accept python >3.10 match / case routines. But it also raises new linting issues. Just want to be sure what do you want to do from there See ref psf/black#2242

Ah, I don't want to drop support for older Python versions just because of this. Just revert to an if-statement instead. You can use ./scripts/chickn.py -t fixup to deal with linting properly (it's what's used by CI).

@albaintor
Copy link
Contributor Author

Hi I think I know what is going on : something is wrong on the build action image
The black command fails with the new "match case" code in tests/fake_device/companion.py The target python should be added in the black : -t py310 or whatever
EDIT : Addin black --target-version py310 --fast --check . within chickn.yaml would resolve the problem in order to accept python >3.10 match / case routines. But it also raises new linting issues. Just want to be sure what do you want to do from there See ref psf/black#2242

Ah, I don't want to drop support for older Python versions just because of this. Just revert to an if-statement instead. You can use ./scripts/chickn.py -t fixup to deal with linting properly (it's what's used by CI).

Hi, this is all done including linting
There are some linting warnings but not related to my modifications

@postlund
Copy link
Owner

As long as the linting fails, you must fix it. You can suppress too-many-branches and you probably have to move the existing suppression for logging.TRAFFIC.

@albaintor
Copy link
Contributor Author

As long as the linting fails, you must fix it. You can suppress too-many-branches and you probably have to move the existing suppression for logging.TRAFFIC.

It should be okay this time

@albaintor
Copy link
Contributor Author

All tests passed !

@postlund
Copy link
Owner

Yes! 🥳

@postlund postlund merged commit 9f2f7cd into postlund:master Jul 30, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants