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

mac: add support for --force-window-position #15500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

NekoAsakura
Copy link
Contributor

(that's a lot less changes than i anticipated.)

This pull request includes a bug fix and adds support for a feature.


  1. In Common::getWindowGeometry -> (NSRect, Bool)
    return (wr, Bool(geo.flags & Int32(VO_WIN_FORCE_POS)))

Int32(VO_WIN_FORCE_POS) is always a constant 1, irrespective of --force-window-position. As a result, whenever --geometry is passed, forcePosition is returned as 1.

You can easily verify this by:
a) Adding a breakpoint just before the return statement, or
b) Including a debug line such as print("Bool", Bool(geo.flags & Int32(VO_WIN_FORCE_POS)))
Then:

build/mpv --geometry=500:500 '/Users/neko/Downloads/test.mp4'
build/mpv --geometry=500:500 --force-window-position '/Users/neko/Downloads/test.mp4'

[Notice] The objc func control used variable forcePosition in one case within a switch statement. However, I don't know the corresponding trigger condition. Could someone who knows provide the related code or help me test it?

let (_, wr, forcePosition) = self.getInitProperties(vo)


  1. Add support for --force-window-position
    simple.
    https://developer.apple.com/documentation/appkit/nswindow/ismovable

@kasper93 kasper93 requested a review from Akemi December 12, 2024 21:33
@NekoAsakura NekoAsakura changed the title mac: add support for --force-window-position WIP: mac: add support for --force-window-position Dec 12, 2024
@NekoAsakura
Copy link
Contributor Author

After testing, it was found that the mouse can still interact with the NSView (rather NSWindow) to move the window. This issue is currently being fixed.

Copy link

github-actions bot commented Dec 12, 2024

Download the artifacts for this pull request:

Windows
macOS

@NekoAsakura NekoAsakura changed the title WIP: mac: add support for --force-window-position mac: add support for --force-window-position Dec 12, 2024
@NekoAsakura
Copy link
Contributor Author

Fixed. I assume that --force-window-position does not want to disable full-screen mode. Let me know if further adjustments are required.

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.

sry this is not doing what the option is supposed to do.

@@ -437,7 +444,7 @@ class Common: NSObject {
let y = CGFloat(-geo.win.y1)
let x = CGFloat(geo.win.x0)
let wr = screen.convertRectFromBacking(NSRect(x: x, y: y, width: width, height: height))
return (wr, Bool(geo.flags & Int32(VO_WIN_FORCE_POS)))
return (wr, Bool(geo.flags & Int32(option.vo.force_window_position)))
Copy link
Member

Choose a reason for hiding this comment

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

that's wrong. this is a bitwise and operation, eg it is supposed to check if VO_WIN_FORCE_POS is included in the set bits of geo.flags. in this case it's already properly set in the underlying function, eg the geo pointer that is passed to vo_calc_window_geometry.

mpv/video/out/win_state.c

Lines 137 to 139 in dbb3291

if (opts->geometry.xy_valid || opts->force_window_position || force_center)
out_geo->flags |= VO_WIN_FORCE_POS;
}

see also other implementations https://github.com/search?q=repo%3Ampv-player%2Fmpv%20VO_WIN_FORCE_POS&type=code

@@ -411,6 +417,7 @@ class Common: NSObject {

func getWindowGeometry(forScreen screen: NSScreen,
videoOut vo: UnsafeMutablePointer<vo>) -> (NSRect, Bool) {
let option = OptionHelper(vo, vo.pointee.global)
Copy link
Member

Choose a reason for hiding this comment

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

you can just use option (class var), no need to instantiate a new option helper

@@ -104,6 +104,12 @@ class Common: NSObject {
window.setMinimized(minimized)
window.makeMain()
window.makeKey()
window.isMovable = !forcePosition
Copy link
Member

Choose a reason for hiding this comment

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

are you misunderstanding the option here (https://github.com/mpv-player/mpv/blob/master/DOCS/man/options.rst?plain=1#L3569-L3572)?

Forcefully move mpv's video output window to default location whenever
there is a change in video parameters, video stream or file. This used to
be the default behavior. Currently only affects X11 and SDL VOs.

this just prevents window dragging on title bar and background.

i believe the only part missing here is the the position on init. though i have to check what exactly didn't work properly.

@NekoAsakura
Copy link
Contributor Author

I shall have a look.

@NekoAsakura
Copy link
Contributor Author

NekoAsakura commented Dec 13, 2024

@Akemi Hi, I reviewed the code, and for the first commit, my question is:

if (opts->geometry.xy_valid || opts->force_window_position || force_center)

Why it's
opts->geometry.xy_valid || opts->force_window_position || force_center rather
opts->geometry.xy_valid && opts->force_window_position || force_center?
Is this the intended behaviour?

As for the second commit, yes, I misunderstood the meaning of this option. My current understanding is that when running the script below, the option expects the top-left corner of the window (as defined by the geometry setting) to remain fixed when switching between different resolutions. Is that right?

#!/usr/bin/env bash

EXEC_PATH="mpv"

while [[ "$1" != "" ]]; do
    case "$1" in
        --exec_path )
            shift
            EXEC_PATH="$1"
            ;;
    esac
    shift
done

RESOLUTIONS=(
"1280x720"
"854x480"
"640x360"
"426x240"
)

DURATION=1.5

rm -f res_*.mkv list.txt recycle.mkv

for RES in "${RESOLUTIONS[@]}"; do
    WIDTH=$(echo $RES | cut -d'x' -f1)
    HEIGHT=$(echo $RES | cut -d'x' -f2)
    OUTPUT="res_${WIDTH}x${HEIGHT}.mkv"
    ffmpeg -f lavfi -i "testsrc=size=${WIDTH}x${HEIGHT}:rate=30:duration=${DURATION}" -pix_fmt yuv420p -c:v libx264 -crf 20 -y "${OUTPUT}"
    echo "file '$(pwd)/${OUTPUT}'" >> list.txt
done

ffmpeg -f concat -safe 0 -i list.txt -c copy recycle.mkv

rm -f res_*.mkv list.txt

ffmpeg -stream_loop -1 -re -i recycle.mkv -c copy -f matroska - 2>/dev/null | "${EXEC_PATH}" --geometry=500:500 --force-window-position -

Save it into test_force_pos.sh and run

bash ./test_force_pos.sh

or

bash ./test_force_pos.sh --exec_path <your_compile_path>

@Akemi
Copy link
Member

Akemi commented Dec 14, 2024

  1. yes, i think it's intended.
  2. force-window-position is independent from geometry options but can be influenced by it. my understanding of this option is, when used the position of the window is reset (to default position) every time "there is a change in video parameters, video stream or file".

specific example, without geometry options set but with --force-window-position:

  1. open a video file, the window is centered on the current screen
  2. move the window somewhere else on that screen
  3. file changes to the next video file > window is forced to the default location (centered on the screen). default location is the same location as opening the file directly on init. without force-window-position the file stays at the location from 2., though is 'centered resized'

the default location can be influenced by the various geometry/autofit options.

@Akemi
Copy link
Member

Akemi commented Dec 14, 2024

for macvk this most likely needs to be added here.

the manual needs updating to for this option, when adding this https://github.com/mpv-player/mpv/blame/master/DOCS/man/options.rst#L3569-L3572

@NekoAsakura
Copy link
Contributor Author

@Akemi, could you review the newly submitted commit? Now the fixed position is anchored to the top-left corner. (centre alignment would be more complex as window?.unfsContentFrame is affected when user moving and resizing window, we might need a new variable to store last initial parameters.)

@Akemi
Copy link
Member

Akemi commented Dec 15, 2024

i don't know what you mean "Now the fixed position is anchored to the top-left corner"? what exactly is the problem or what behaviour changed from the previous behaviour? the frame is set according to what vo_calc_window_geometry returns.

please also update the manual.

@NekoAsakura
Copy link
Contributor Author

NekoAsakura commented Dec 15, 2024

I don’t have an X11 testing environment, so I’m unsure about the behaviour of --force-window-position on other VOs. When using --force-window-position, we need to define an original point, which is currently set to the top-left corner. This is because, on macvk, the window position is updated by CGRect, and the original of CGRect is in the upper-left corner -- when we defined it with a flipped coordinate space. I simply want to confirm that this original point is the desired one (or at least consistent with the behaviour of other VOs).

[Edit]
When update manual, should I name it 'cocoa-cb' or 'macvk'?

@Akemi
Copy link
Member

Akemi commented Dec 15, 2024

first of all the manual is not completely clear how this is supposed to behave especially with --auto-window-resize. on X11 --force-window-position apparently it's just a recenter window. i am in the process to clarify how this is supposed to work.

i am not sure why it's relevant that the window is positioned by the top left corner? it was like this before and is supposed to be like that (see geometry https://github.com/mpv-player/mpv/blob/master/DOCS/man/options.rst?plain=1#L3398-L3399 "from the top-left corner"). if it were an issue, it would be unrelated to this and would need fixing somewhere else (eg we only flip the origin here https://github.com/mpv-player/mpv/blob/master/video/out/mac/common.swift#L436-L437 to align it with the macOS coordinate system).
you update the whole frame, which is calculated taking the size into consideration, so it is kinda irrelevant atm. if we would only update the position though and expect a centered window that would be a completely different matter.

either macOS or macvk, the former if we update cocoa-cb too to support this.

@NekoAsakura
Copy link
Contributor Author

Then it seems there’s nothing more to modify in this PR.

However, I did notice that the geometry calculations are incorrect. In my test code, I used --geometry=500:500, but the result was (308, 275). Can you verify this issue?

@Akemi
Copy link
Member

Akemi commented Dec 15, 2024

i can't verify nor deny any issue like this. this depends on your setup (hdipi, resolution, screen, config, etc) and which values you actually compare. this is the wrong place for reporting an issue in the first place.

a simple check on my setup does what it is supposed to do on the main monitor. though that doesn't mean that something isn't broken or broke at some point because of various reasons.

@NekoAsakura
Copy link
Contributor Author

It is certainly not a requirement for this PR. I’m just mentioning it to see if this issue has already been identified. I will carry out a full test later and submit a new issue.

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.

2 participants