JasonLovesDoggo / JasonLovesDoggo/displayindex
Fails to identify display if display 0 is right of display 1
- Dominant language
- Go
- Stars
- 4
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
Hi, I've used this library along with fyne in a [screenshot tool](https://github.com/giftig/gshoot/blob/master/main.go#L62) which has been working perfectly fine on my desktop, but I've recently discovered doesn't work correctly on my laptop when plugged into an external monitor.
My platform is linux (Fedora, with XFCE and X11).
I did some digging into what the library is doing and printed some diagnostics using the underlying APIs:
```go
n := screenshot.NumActiveDisplays()
fmt.Printf("Num active displays: %d\n", n)
x, y, err := getCursorPosition()
fmt.Printf("Cursor pos: %d, %d", x, y)
bounds0 := screenshot.GetDisplayBounds(0)
bounds1 := screenshot.GetDisplayBounds(1)
fmt.Printf("Display 0 bounds: %v\n", bounds0)
fmt.Printf("Display 1 bounds: %v\n", bounds1)
```
and got these results:
```
activedisplay $ go run main.go
Num active displays: 2
Display 0 bounds: (0,0)-(2256,1504)
Display 1 bounds: (-3840,0)-(0,2160)
```
As you can see, because the bounds are relative to display #0, display #1 has a negative x position. When using `CurrentDisplayIndex()` I found that it returned an error when the cursor was on Display #0 or outside of (0, 2256) on display #1, and it returned the wrong display when on display #1.
I tried to copypasta your `getCursorPosition` function so that I could call it directly and check the position it's determined from the X11 lib, but it didn't like the C import and I see it's using a linked C library, so most likely I missed some setup I need to get that to work. So I didn't confirm the coordinates it was seeing, but I used `xdotool` to check what X11 thinks the coordinates are, most likely the lib is seeing:
```
$ xdotool getmouselocation
x:775 y:716 screen:0 window:50331662
$ xdotool getmouselocation
x:5031 y:1285 screen:0 window:54525955
```
...and as you can see, it returns positive coordinates only, relative to the upper left of the upper-left-most display, presumably.
So I'd wager the problem is that you need to account for the bounds provided by `screenshot.GetDisplayBounds(n)` and the coordinates returned by X11 having different reference points. Unfortunately this could be fairly complicated to account for with multiple screens and possible layouts. If I get some time I'll try to tackle it and raise a PR.
**tl;dr: on X11, display bounds may be negative coordinates relative to screen #0 but mouse coordinates will always be positive**
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with CurrentDisplayIndex and the getCursorPosition path, comparing X11 cursor coordinates with the values from screenshot.GetDisplayBounds. Reproduce the reported layouts, including display 1 to the left of display 0, and verify that the returned display index is correct for the cursor on every display.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100