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

screenshot: add screenshot-to-clipboard command #15568

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

rcombs
Copy link
Contributor

@rcombs rcombs commented Dec 25, 2024

Successor to #13837, post-#15355 and #15564.

Currently implemented only on macOS.

@rcombs rcombs requested a review from Akemi December 25, 2024 03:36
@rcombs rcombs force-pushed the screenshot-clipboard branch from 3d5afcb to 7caaf04 Compare December 25, 2024 03:43
Copy link

github-actions bot commented Dec 25, 2024

Download the artifacts for this pull request:

Windows
macOS

@rcombs rcombs force-pushed the screenshot-clipboard branch from 7caaf04 to d72d66b Compare December 25, 2024 04:37

dest.params.color = plcsp

if mp_image_swscale(&dest, &image, SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP | SWS_ACCURATE_RND) < 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use the same pattern and options as other screenshot code. See 2c43d2b

I will remove mp_image_swscale to avoid confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why add more brittle boilerplate? Seems more reasonable to me to add global and log options to mp_image_swscale and then remove the duplication in screenshot.c, image_writer.c, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rebased with this change; the relevant changeset is net-red (+11 -20) so I think it's a reasonable reduction in duplication. Also improved one path through player/screenshot that wasn't using the global options (nor checking for errors).

@rcombs rcombs force-pushed the screenshot-clipboard branch 2 times, most recently from f0df0fa to aac01fb Compare December 25, 2024 21:48
Copy link
Member

@Akemi Akemi left a comment

Choose a reason for hiding this comment

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

the whole setImage function is very convoluted. do you think we can split it into some logical parts/helper functions, format selection (format, colour space, bit depth, etc), compatibility test, fallback/conversion, etc? it's fine not doing it in this PR and we do it later.

osdep/mac/clipboard.swift Outdated Show resolved Hide resolved
osdep/mac/clipboard.swift Outdated Show resolved Hide resolved
osdep/mac/clipboard.swift Outdated Show resolved Hide resolved
osdep/mac/clipboard.swift Outdated Show resolved Hide resolved
withUnsafeMutableBytes(of: &dest.stride) { (stridePtr) in
withUnsafeMutableBytes(of: &dest.planes) { (planesPtr) in
let destStrides = stridePtr.baseAddress!.assumingMemoryBound(to: type(of: image.stride.0))
let destPlanes = planesPtr.baseAddress!.assumingMemoryBound(to: type(of: image.planes.0))
Copy link
Member

Choose a reason for hiding this comment

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

would be nice to get rid of the forced unwrapping here. don't want mpv to completely fail because the clipboard code fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These pointers are the addresses of members of a stack object; they can't be null.

Copy link
Member

Choose a reason for hiding this comment

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

i don't want to compromise even more on the force unwrappings than i already did on the previous occurrences. i also don't want to discuss the sense of it in any case.

please handle every unwrapping in an appropriate way and get rid of the force unwrappings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to use guard lets here and added the extra handling for that, but also added an assert for debug builds.

osdep/mac/clipboard.swift Outdated Show resolved Hide resolved
osdep/mac/clipboard.swift Outdated Show resolved Hide resolved
osdep/mac/clipboard.swift Outdated Show resolved Hide resolved
@@ -447,6 +447,12 @@ Property Manipulation
Playlist Manipulation
~~~~~~~~~~~~~~~~~~~~~

``screenshot-to-clipboard [<flags>]``
Copy link
Member

@Akemi Akemi Dec 27, 2024

Choose a reason for hiding this comment

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

the documentation for this should be under Screenshot Commands not Playlist Manipulation.

also please correct me if i am wrong. this is raw bitmap data which we pass here or at least not compressed data (png, jpg, whatever is configured with our screenshot options)? so shouldn't this be screenshot-raw-to-clipboard?

eg i would expect screenshot-to-clipboard to write a screenshot to the clipboard in the same format screenshot writes a screenshot to the file system. so it should be affected by 'all' the screenshot-* options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

screenshot-to-file also ignores the format option, though?

Copy link
Member

Choose a reason for hiding this comment

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

i guess the documentation mentions the screenshot-to-file ignores at least the format option. my expectations were a bit betrayed then.

Take a screenshot and save it to a given file. The format of the file will be guessed by the extension (and --screenshot-format is ignored - the behaviour when the extension is missing or unknown is arbitrary).

though thinking a bit about the future. what if we want to actually add screenshot-to-clipboard command that wants to add the aforementioned case (eg adding a screenshot as png, jpeg, etc)? would we add a new flag to this command? i am just wondering and trying to prevent a possible 'mess'.

though if we do what @na-na-hi proposed we don't need to think further about this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a conceptual difference between an "image on the clipboard" and a "file on the clipboard", and generally copying ephemeral image data does not result in you having a file on the clipboard. On macOS, images on the clipboard are commonly represented as PNG or TIFF data (sometimes both), but there is no eg JPEG clipboard format. There's also no concept of arbitrarily ephemeral file contents on the clipboard (only a file path). The same is true on Windows afaik, except that the image format is BMP instead of PNG/TIFF. I have no idea how this works on linux, apart from that apparently on X it's common for apps to expose several different types for a copied image.

Images copied out of mpv on this branch are PNG data (but that's an implementation detail, and it basically never matters to a user).

@rcombs rcombs force-pushed the screenshot-clipboard branch from aac01fb to 5e26514 Compare December 28, 2024 02:08
@rcombs
Copy link
Contributor Author

rcombs commented Dec 28, 2024

Rebased with suggestions applied; split the function up to the extent that was possible without adding substantial duplication or major restructuring.

@na-na-hi
Copy link
Contributor

I think there is no need for this to be a command. I designed the clipboard API with the ability for format conversion in mind: when the data formats in clipboard_access_params and clipboard_data are different, the backend can perform automatic format conversion if possible. This makes it possible to use the operating system's built-in data format conversion abilities.

In this specific case, it is possible to make the clipboard/image property writable, and a client can just set the clipboard content by writing a special token of MPV_FORMAT_STRING type like @screenshot window to the property.

Alternatively, writing a MPV_FORMAT_NODE nodemap containing information of the raw image data to that property should write that image to the clipboard, so a client can also use screenshot-raw command to acquire the image data and write the data to clipboard.

These approaches allow for more flexibility of setting image clipboard contents while not introducing a new command.

@rcombs
Copy link
Contributor Author

rcombs commented Dec 28, 2024

I think supporting setting an MPV_FORMAT_NODE makes sense in the long term, but designing an API for it that's actually useful across a variety of pixel formats, colorspaces, etc. is going to be tricky, so I'd prefer to leave it out of this PR for now (see also the screenshot-raw command, which returns a node, but in a single hardcoded pixel format with no colorspace information).

I wouldn't object to having this be set clipboard/image "@screenshot window" or the like, though I do think the symmetry the current approach has with screenshot-to-file is probably a bit more intuitive for users?

Also, it probably makes most sense for any magic-string @screenshot support to happen up at the command/property level, rather than within each individual backend.

@Akemi
Copy link
Member

Akemi commented Dec 28, 2024

tested the the mac parts and works as expected in programs that can handle those pastes. besides that one open point i am fine with he swift parts of the code.

would be nice if we could resolve the rest of the open points in a timely manner:

  • the user facing API
  • implementation on the mpv core side

@rcombs rcombs force-pushed the screenshot-clipboard branch from 5e26514 to 7fde240 Compare December 31, 2024 03:24
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