-
-
Notifications
You must be signed in to change notification settings - Fork 791
Add structured per-monitor display inventory #329
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
Merged
Jeomon
merged 3 commits into
CursorTouch:main
from
zengfanfan:codex/upstream-display-inventory
Jul 13, 2026
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| """DisplayInventory tool - read-only display and DPI metadata.""" | ||
|
|
||
| import json | ||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| from fastmcp import Context | ||
| from mcp.types import ToolAnnotations | ||
| from windows_mcp.infrastructure import with_analytics | ||
|
|
||
|
|
||
| def _rect_to_dict(rect: Any) -> dict[str, int] | None: | ||
| if rect is None: | ||
| return None | ||
| return { | ||
| "left": rect.left, | ||
| "top": rect.top, | ||
| "right": rect.right, | ||
| "bottom": rect.bottom, | ||
| "width": rect.width(), | ||
| "height": rect.height(), | ||
| } | ||
|
|
||
|
|
||
| def register( | ||
| mcp: Any, | ||
| *, | ||
| get_desktop: Callable[[], Any], | ||
| get_analytics: Callable[[], Any], | ||
| ) -> None: | ||
| @mcp.tool( | ||
| name="DisplayInventory", | ||
| description=( | ||
| "Read active display layout and DPI metadata. Reports display index, device name, " | ||
| "monitor/work-area bounds, resolution, orientation, primary flag, effective DPI, " | ||
| "and scale." | ||
| ), | ||
| annotations=ToolAnnotations( | ||
| title="DisplayInventory", | ||
| readOnlyHint=True, | ||
| destructiveHint=False, | ||
| idempotentHint=True, | ||
| openWorldHint=False, | ||
| ), | ||
| ) | ||
| @with_analytics(get_analytics(), "DisplayInventory-Tool") | ||
| def display_inventory_tool(ctx: Context = None) -> str: | ||
| displays = get_desktop().get_displays() | ||
| payload = { | ||
| "displays": [ | ||
| { | ||
| "index": display.index, | ||
| "device": display.device_name, | ||
| "primary": display.primary, | ||
| "bounds": _rect_to_dict(display.rect), | ||
| "work_area": _rect_to_dict(getattr(display, "work_rect", None)), | ||
| "resolution": f"{display.rect.width()}x{display.rect.height()}", | ||
| "orientation": getattr(display, "orientation", None), | ||
| "effective_dpi": getattr(display, "effective_dpi", None), | ||
| "scale": getattr(display, "scale", None), | ||
| } | ||
| for display in displays | ||
| ], | ||
| "count": len(displays), | ||
| } | ||
| return json.dumps(payload, indent=2) | ||
|
zengfanfan marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import asyncio | ||
| import json | ||
| from types import SimpleNamespace | ||
| from collections.abc import Callable | ||
|
|
||
| import pytest | ||
|
|
||
| from windows_mcp.tools import display as display_tool_module | ||
| from windows_mcp.uia import DisplayInfo, Rect, core | ||
|
|
||
|
|
||
| class FakeMCP: | ||
| def __init__(self) -> None: | ||
| self.tools: dict[str, Callable] = {} | ||
|
|
||
| def tool(self, *, name: str, **kwargs: object) -> Callable: | ||
| def decorator(func: Callable) -> Callable: | ||
| self.tools[name] = func | ||
| return func | ||
|
|
||
| return decorator | ||
|
|
||
|
|
||
| class FakeDesktop: | ||
| def get_displays(self) -> list[DisplayInfo]: | ||
| return [ | ||
| DisplayInfo( | ||
| index=0, | ||
| device_name="\\\\.\\DISPLAY1", | ||
| rect=Rect(0, 0, 1920, 1080), | ||
| primary=True, | ||
| work_rect=Rect(0, 0, 1920, 1040), | ||
| effective_dpi=144, | ||
| scale=1.5, | ||
| orientation="landscape", | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| def test_display_inventory_returns_display_dpi_metadata() -> None: | ||
| mcp = FakeMCP() | ||
| display_tool_module.register(mcp, get_desktop=FakeDesktop, get_analytics=lambda: None) | ||
|
|
||
| result = json.loads(asyncio.run(mcp.tools["DisplayInventory"]())) | ||
|
|
||
| assert result == { | ||
| "displays": [ | ||
| { | ||
| "index": 0, | ||
| "device": "\\\\.\\DISPLAY1", | ||
| "primary": True, | ||
| "bounds": { | ||
| "left": 0, | ||
| "top": 0, | ||
| "right": 1920, | ||
| "bottom": 1080, | ||
| "width": 1920, | ||
| "height": 1080, | ||
| }, | ||
| "work_area": { | ||
| "left": 0, | ||
| "top": 0, | ||
| "right": 1920, | ||
| "bottom": 1040, | ||
| "width": 1920, | ||
| "height": 1040, | ||
| }, | ||
| "resolution": "1920x1080", | ||
| "orientation": "landscape", | ||
| "effective_dpi": 144, | ||
| "scale": 1.5, | ||
| } | ||
| ], | ||
| "count": 1, | ||
| } | ||
|
|
||
|
|
||
| def test_monitor_dpi_falls_back_to_system_dpi(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| shcore = SimpleNamespace(GetDpiForMonitor=lambda *args: 1) | ||
| user32 = SimpleNamespace(GetDpiForSystem=lambda: 120) | ||
| monkeypatch.setattr(core.ctypes, "windll", SimpleNamespace(shcore=shcore, user32=user32)) | ||
|
|
||
| assert core._get_monitor_effective_dpi(123) == (120, 1.25) | ||
|
|
||
|
|
||
| def test_display_orientation_uses_current_mode(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| def enum_display_settings(device_name: str, mode: object, dev_mode: object) -> int: | ||
| core.ctypes.memmove( | ||
| core.ctypes.addressof(dev_mode) + 172, | ||
| core.struct.pack("<II", 1080, 1920), | ||
| 8, | ||
| ) | ||
| return 1 | ||
|
|
||
| user32 = SimpleNamespace(EnumDisplaySettingsW=enum_display_settings) | ||
| monkeypatch.setattr(core.ctypes, "windll", SimpleNamespace(user32=user32)) | ||
|
|
||
| assert core._get_display_orientation("\\\\.\\DISPLAY1", Rect(0, 0, 1080, 1920)) == "portrait" | ||
|
zengfanfan marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def test_display_orientation_falls_back_to_bounds(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| user32 = SimpleNamespace(EnumDisplaySettingsW=lambda *args: 0) | ||
| monkeypatch.setattr(core.ctypes, "windll", SimpleNamespace(user32=user32)) | ||
|
|
||
| assert core._get_display_orientation("DISPLAY0", Rect(0, 0, 1920, 1080)) == "landscape" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.