leejunyoung5819 wayland-android-remote
공개Ubuntu(Wayland/X11)에서 Android 기기 화면 미러링 및 원격 제어 (Rust)
| 1 | //! X11 창 관리자에게 창 비율을 알린다 (`WM_NORMAL_HINTS`의 최소·최대 비율). |
| 2 | //! |
| 3 | //! Wayland에서는 앱이 크기를 바꿔 커밋하면 컴포지터가 따르지만, X11에서는 가장자리를 끄는 |
| 4 | //! 동안 창 관리자가 크기를 정하고 앱의 크기 요청을 덮어쓴다. 비율 힌트를 주면 창 관리자가 |
| 5 | //! 끄는 동안에도 비율을 지킨다. winit 0.30은 비율 힌트를 지원하지 않아 따로 연결을 열어 쓴다. |
| 6 | //! |
| 7 | //! Mutter는 ICCCM과 달리 `base_size`를 빼지 않고 창 전체 크기에 비율을 적용한다. 제목줄·옆 |
| 8 | //! 막대처럼 고정된 부분이 있어 휴대폰 화면 비율과 창 전체 비율이 크기마다 조금씩 다르므로, |
| 9 | //! 크기 조절이 끝나면 정확한 크기로 맞추고 그 크기의 비율로 힌트를 다시 준다. |
| 10 | |
| 11 | use egui_winit::winit; |
| 12 | use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle}; |
| 13 | use winit::window::Window; |
| 14 | use x11rb::properties::{AspectRatio, WmSizeHints}; |
| 15 | use x11rb::rust_connection::RustConnection; |
| 16 | use x11rb::wrapper::ConnectionExt as _; |
| 17 | |
| 18 | pub struct AspectHint { |
| 19 | conn: RustConnection, |
| 20 | window: u32, |
| 21 | last: Option<(i32, i32)>, |
| 22 | } |
| 23 | |
| 24 | impl AspectHint { |
| 25 | /// X11 창이면 힌트를 줄 연결을 연다. Wayland 창이면 `None`. |
| 26 | pub fn for_window(window: &Window) -> Option<Self> { |
| 27 | let id = match window.window_handle().ok()?.as_raw() { |
| 28 | RawWindowHandle::Xlib(h) => u32::try_from(h.window).ok()?, |
| 29 | RawWindowHandle::Xcb(h) => h.window.get(), |
| 30 | _ => return None, |
| 31 | }; |
| 32 | match x11rb::connect(None) { |
| 33 | Ok((conn, _)) => Some(Self { |
| 34 | conn, |
| 35 | window: id, |
| 36 | last: None, |
| 37 | }), |
| 38 | Err(e) => { |
| 39 | log::warn!("X11 비율 힌트용 연결 실패: {e}"); |
| 40 | None |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// 창 비율을 창 전체 크기 `width`×`height`(물리 픽셀)의 비율로 고정한다. |
| 46 | /// |
| 47 | /// 서버가 처리할 때까지 기다리므로, 뒤이어 winit이 보내는 크기 요청에는 새 비율이 적용된다. |
| 48 | pub fn set(&mut self, width: u32, height: u32) { |
| 49 | let Some(ratio) = reduce(width, height) else { |
| 50 | return; |
| 51 | }; |
| 52 | if self.last == Some(ratio) { |
| 53 | return; |
| 54 | } |
| 55 | if let Err(e) = self.apply(ratio) { |
| 56 | log::debug!("X11 비율 힌트 설정 실패: {e}"); |
| 57 | return; |
| 58 | } |
| 59 | self.last = Some(ratio); |
| 60 | } |
| 61 | |
| 62 | fn apply(&self, (num, den): (i32, i32)) -> Result<(), Box<dyn std::error::Error>> { |
| 63 | // winit이 넣은 최소 크기 등 다른 값은 그대로 둔다. |
| 64 | let mut hints = WmSizeHints::get_normal_hints(&self.conn, self.window)? |
| 65 | .reply()? |
| 66 | .unwrap_or_default(); |
| 67 | let ratio = AspectRatio::new(num, den); |
| 68 | hints.aspect = Some((ratio, ratio)); |
| 69 | hints.set_normal_hints(&self.conn, self.window)?; |
| 70 | self.conn.sync()?; |
| 71 | Ok(()) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// `width`/`height`를 기약분수로 줄인다. 0이거나 i32를 넘으면 `None`. |
| 76 | fn reduce(width: u32, height: u32) -> Option<(i32, i32)> { |
| 77 | if width == 0 || height == 0 { |
| 78 | return None; |
| 79 | } |
| 80 | let (mut a, mut b) = (width, height); |
| 81 | while b != 0 { |
| 82 | (a, b) = (b, a % b); |
| 83 | } |
| 84 | Some(( |
| 85 | i32::try_from(width / a).ok()?, |
| 86 | i32::try_from(height / a).ok()?, |
| 87 | )) |
| 88 | } |
| 89 | |
| 90 | #[cfg(test)] |
| 91 | mod tests { |
| 92 | use super::reduce; |
| 93 | |
| 94 | #[test] |
| 95 | fn reduces_ratio() { |
| 96 | assert_eq!(reduce(1080, 2400), Some((9, 20))); |
| 97 | assert_eq!(reduce(457, 911), Some((457, 911))); |
| 98 | assert_eq!(reduce(0, 10), None); |
| 99 | assert_eq!(reduce(10, 0), None); |
| 100 | } |
| 101 | } |