Skip to content

Commit

Permalink
add build and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
spyoungtech committed Jul 9, 2024
1 parent 15bd5ac commit 52c5511
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 12 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
on:
push:
branches:
- main
tags:
- 'v*.*.*'
pull_request: {}
env:
COLUMNS: 150

jobs:

build:
runs-on: windows
matrix:
python-version:
- '3.8'
- '3.9'
- '3.10'
- '3.11'
- '3.12'

steps:
- uses: actions/checkout@v4

- name: install rust stable
uses: dtolnay/rust-toolchain@stable

- name: cache rust
uses: Swatinem/rust-cache@v2
with:
key: test-v0

- name: set up python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist --interpreter ${{ matrix.python-version || '3.8 3.9 3.10 3.11 3.12 pypy3.9 pypy3.10' }}
rust-toolchain: stable
docker-options: -e CI

- uses: actions/upload-artifact@v4
with:
name: pypi_files_${{ matrix.interpreter }}
path: dist



release:
needs: [build]
if: success() && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps:
- uses: actions/checkout@v4

- name: set up python
uses: actions/setup-python@v5
with:
python-version: '3.10'

- run: pip install -U twine


- name: get dist artifacts
uses: actions/download-artifact@v4
with:
pattern: pypi_files_*
merge-multiple: true
path: dist

- run: twine check --strict dist/*

- name: Release PyPI
uses: pypa/gh-action-pypi-publish@release/v1

- name: Release GitHub
uses: softprops/action-gh-release@v1
with:
files: |
dist/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# wmutil

Utility functions for getting monitor (display) information on Windows.



```python
import wmutil


# Enumerate all monitors
print('Enumerating monitors:')
for monitor in wmutil.enumerate_monitors():
# Print monitor attributes
print(monitor, monitor.name, monitor.size, monitor.position, monitor.refresh_rate_millihertz, monitor.handle, sep='\n\t')


# Get primary monitor
primary_monitor = wmutil.get_primary_monitor()

# Get a monitor based on point coordinates
monitor = wmutil.get_monitor_from_point(0, 0)

# compare monitor objects
if monitor == primary_monitor:
print('it is the primary monitor')


# Get monitor from an HWND
from ahk import AHK # pip install ahk[binary]
ahk = AHK()

window = ahk.active_window
hwnd = int(window.id, 0)
monitor_for_active_window = wmutil.get_window_monitor(hwnd)
print(window.title, 'is using monitor', monitor_for_active_window.name)

```

Example output:

```
Enumerating monitors:
<wmutil.Monitor object; handle=491197379>
\\.\DISPLAY1
(1920, 1080)
(-3840, -418)
60000
491197379
<wmutil.Monitor object; handle=85595795>
\\.\DISPLAY2
(3440, 1440)
(0, 0)
60000
85595795
it is the primary monitor
Untitled - Notepad is using monitor \\.\DISPLAY2
```

Notes:

- `monitor.size` may not necessarily reflect the monitor's resolution, but rather is the geometry used for drawing or moving windows
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ classifiers = [
'Operating System :: Microsoft :: Windows',
'Operating System :: MacOS',
]
readme = "README.md"
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ unsafe extern "system" fn monitor_enum_proc(
// Python bindings

#[pyclass(module = "wmutil")]
struct MonitorInfo {
struct Monitor {
monitor_handle: MonitorHandle,
}

#[pymethods]
impl MonitorInfo {
impl Monitor {
#[getter]
fn name(&self) -> String {
self.monitor_handle.name().unwrap_or(String::from("Unknown monitor name"))
Expand Down Expand Up @@ -302,44 +302,44 @@ impl MonitorInfo {
}

#[pyfunction]
fn get_primary_monitor() -> MonitorInfo {
fn get_primary_monitor() -> Monitor {
let handle = primary_monitor();
MonitorInfo {
Monitor {
monitor_handle: handle
}
}

#[pyfunction]
fn get_window_monitor(hwnd: isize) -> MonitorInfo {
fn get_window_monitor(hwnd: isize) -> Monitor {
let handle = current_monitor(hwnd.into());
MonitorInfo {
Monitor {
monitor_handle: handle
}
}

#[pyfunction]
fn enumerate_monitors() -> Vec<MonitorInfo> {
let mut monitors: Vec<MonitorInfo> = Vec::new();
fn enumerate_monitors() -> Vec<Monitor> {
let mut monitors: Vec<Monitor> = Vec::new();
for monitor in available_monitors() {
monitors.push(MonitorInfo { monitor_handle: monitor })
monitors.push(Monitor { monitor_handle: monitor })
}
monitors
}

#[pyfunction]
fn get_monitor_from_point(x: i32, y: i32) -> MonitorInfo {
fn get_monitor_from_point(x: i32, y: i32) -> Monitor {
let point = POINT {x, y};
let hmonitor = unsafe { MonitorFromPoint(point, MONITOR_DEFAULTTOPRIMARY) };
let handle = MonitorHandle::new(hmonitor);
MonitorInfo {
Monitor {
monitor_handle: handle
}
}


#[pymodule]
fn wmutil(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<MonitorInfo>()?;
m.add_class::<Monitor>()?;
m.add_function(wrap_pyfunction!(enumerate_monitors, m)?);
m.add_function(wrap_pyfunction!(get_window_monitor, m)?);
m.add_function(wrap_pyfunction!(get_primary_monitor, m)?);
Expand Down

0 comments on commit 52c5511

Please sign in to comment.