Add output configuration & integer scaling support
Browse files at this commit109668fa30bf65e56a2723ff29bcadc3e9a6e4caParent: cfa73c16 changed files +101 -24
- resources/default-config.kdl +8 -0
- src/backend/tty.rs +14 -2
- src/backend/winit.rs +12 -2
- src/config.rs +27 -0
- src/layout.rs +16 -7
- src/niri.rs +24 -13
resources/default-config.kdlmodified+8 -0
| @@ -27,6 +27,14 @@ input { | ||
| 27 | 27 | } |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | +// You can configure outputs by their name, which you can find with wayland-info(1). | |
| 31 | +// The built-in laptop monitor is usually called "eDP-1". | |
| 32 | +// Remember to uncommend the node by removing "/-"! | |
| 33 | +/-output "eDP-1" { | |
| 34 | + // Scale is a floating-point number, but at the moment only integer values work. | |
| 35 | + scale 2.0 | |
| 36 | +} | |
| 37 | + | |
| 30 | 38 | binds { |
| 31 | 39 | // Keys consist of modifiers separated by + signs, followed by an XKB key name |
| 32 | 40 | // in the end. To find an XKB name for a particular key, you may use a program |
src/backend/tty.rsmodified+14 -2
| @@ -21,7 +21,7 @@ use smithay::backend::session::libseat::LibSeatSession; | ||
| 21 | 21 | use smithay::backend::session::{Event as SessionEvent, Session}; |
| 22 | 22 | use smithay::backend::udev::{self, UdevBackend, UdevEvent}; |
| 23 | 23 | use smithay::desktop::utils::OutputPresentationFeedback; |
| 24 | -use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties, Subpixel}; | |
| 24 | +use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties, Subpixel, Scale}; | |
| 25 | 25 | use smithay::reexports::calloop::{Dispatcher, LoopHandle, RegistrationToken}; |
| 26 | 26 | use smithay::reexports::drm::control::{ |
| 27 | 27 | connector, crtc, Mode as DrmMode, ModeFlags, ModeTypeFlags, |
| @@ -500,6 +500,15 @@ impl Tty { | ||
| 500 | 500 | ); |
| 501 | 501 | debug!("connecting connector: {output_name}"); |
| 502 | 502 | |
| 503 | + let config = self | |
| 504 | + .config | |
| 505 | + .borrow() | |
| 506 | + .outputs | |
| 507 | + .iter() | |
| 508 | + .find(|o| o.name == output_name) | |
| 509 | + .cloned() | |
| 510 | + .unwrap_or_default(); | |
| 511 | + | |
| 503 | 512 | let device = self.output_device.as_mut().unwrap(); |
| 504 | 513 | |
| 505 | 514 | let mut mode = connector.modes().get(0); |
| @@ -534,6 +543,9 @@ impl Tty { | ||
| 534 | 543 | .map(|info| (info.manufacturer, info.model)) |
| 535 | 544 | .unwrap_or_else(|| ("Unknown".into(), "Unknown".into())); |
| 536 | 545 | |
| 546 | + let scale = config.scale.clamp(0.1, 10.); | |
| 547 | + let scale = scale.max(1.).round() as i32; | |
| 548 | + | |
| 537 | 549 | let output = Output::new( |
| 538 | 550 | output_name.clone(), |
| 539 | 551 | PhysicalProperties { |
| @@ -544,7 +556,7 @@ impl Tty { | ||
| 544 | 556 | }, |
| 545 | 557 | ); |
| 546 | 558 | let wl_mode = Mode::from(*mode); |
| 547 | - output.change_current_state(Some(wl_mode), None, None, Some((0, 0).into())); | |
| 559 | + output.change_current_state(Some(wl_mode), None, Some(Scale::Integer(scale)), None); | |
| 548 | 560 | output.set_preferred(wl_mode); |
| 549 | 561 | |
| 550 | 562 | output.user_data().insert_if_missing(|| TtyOutputState { |
src/backend/winit.rsmodified+12 -2
| @@ -8,7 +8,7 @@ use smithay::backend::renderer::damage::OutputDamageTracker; | ||
| 8 | 8 | use smithay::backend::renderer::gles::GlesRenderer; |
| 9 | 9 | use smithay::backend::renderer::{DebugFlags, Renderer}; |
| 10 | 10 | use smithay::backend::winit::{self, WinitError, WinitEvent, WinitGraphicsBackend}; |
| 11 | -use smithay::output::{Mode, Output, PhysicalProperties, Subpixel}; | |
| 11 | +use smithay::output::{Mode, Output, PhysicalProperties, Scale, Subpixel}; | |
| 12 | 12 | use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; |
| 13 | 13 | use smithay::reexports::calloop::LoopHandle; |
| 14 | 14 | use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; |
| @@ -38,6 +38,16 @@ impl Winit { | ||
| 38 | 38 | .with_title("niri"); |
| 39 | 39 | let (backend, mut winit_event_loop) = winit::init_from_builder(builder).unwrap(); |
| 40 | 40 | |
| 41 | + let output_config = config | |
| 42 | + .borrow() | |
| 43 | + .outputs | |
| 44 | + .iter() | |
| 45 | + .find(|o| o.name == "winit") | |
| 46 | + .cloned() | |
| 47 | + .unwrap_or_default(); | |
| 48 | + let scale = output_config.scale.clamp(0.1, 10.); | |
| 49 | + let scale = scale.max(1.).round() as i32; | |
| 50 | + | |
| 41 | 51 | let mode = Mode { |
| 42 | 52 | size: backend.window_size().physical_size, |
| 43 | 53 | refresh: 60_000, |
| @@ -55,8 +65,8 @@ impl Winit { | ||
| 55 | 65 | output.change_current_state( |
| 56 | 66 | Some(mode), |
| 57 | 67 | Some(Transform::Flipped180), |
| 68 | + Some(Scale::Integer(scale)), | |
| 58 | 69 | None, |
| 59 | - Some((0, 0).into()), | |
| 60 | 70 | ); |
| 61 | 71 | output.set_preferred(mode); |
| 62 | 72 |
src/config.rsmodified+27 -0
| @@ -11,6 +11,8 @@ use smithay::input::keyboard::Keysym; | ||
| 11 | 11 | pub struct Config { |
| 12 | 12 | #[knuffel(child, default)] |
| 13 | 13 | pub input: Input, |
| 14 | + #[knuffel(children(name = "output"))] | |
| 15 | + pub outputs: Vec<Output>, | |
| 14 | 16 | #[knuffel(child, default)] |
| 15 | 17 | pub binds: Binds, |
| 16 | 18 | #[knuffel(child, default)] |
| @@ -62,6 +64,23 @@ pub struct Touchpad { | ||
| 62 | 64 | pub accel_speed: f64, |
| 63 | 65 | } |
| 64 | 66 | |
| 67 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq)] | |
| 68 | +pub struct Output { | |
| 69 | + #[knuffel(argument)] | |
| 70 | + pub name: String, | |
| 71 | + #[knuffel(child, unwrap(argument), default = 1.)] | |
| 72 | + pub scale: f64, | |
| 73 | +} | |
| 74 | + | |
| 75 | +impl Default for Output { | |
| 76 | + fn default() -> Self { | |
| 77 | + Self { | |
| 78 | + name: String::new(), | |
| 79 | + scale: 1., | |
| 80 | + } | |
| 81 | + } | |
| 82 | +} | |
| 83 | + | |
| 65 | 84 | #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] |
| 66 | 85 | pub struct Binds(#[knuffel(children)] pub Vec<Bind>); |
| 67 | 86 | |
| @@ -263,6 +282,10 @@ mod tests { | ||
| 263 | 282 | } |
| 264 | 283 | } |
| 265 | 284 | |
| 285 | + output "eDP-1" { | |
| 286 | + scale 2.0 | |
| 287 | + } | |
| 288 | + | |
| 266 | 289 | binds { |
| 267 | 290 | Mod+T { spawn "alacritty"; } |
| 268 | 291 | Mod+Q { close-window; } |
| @@ -293,6 +316,10 @@ mod tests { | ||
| 293 | 316 | accel_speed: 0.2, |
| 294 | 317 | }, |
| 295 | 318 | }, |
| 319 | + outputs: vec![Output { | |
| 320 | + name: "eDP-1".to_owned(), | |
| 321 | + scale: 2., | |
| 322 | + }], | |
| 296 | 323 | binds: Binds(vec![ |
| 297 | 324 | Bind { |
| 298 | 325 | key: Key { |
src/layout.rsmodified+16 -7
| @@ -1231,6 +1231,7 @@ impl Monitor<Window> { | ||
| 1231 | 1231 | &self, |
| 1232 | 1232 | renderer: &mut GlesRenderer, |
| 1233 | 1233 | ) -> Vec<MonitorRenderElement<GlesRenderer>> { |
| 1234 | + let output_scale = Scale::from(self.output.current_scale().fractional_scale()); | |
| 1234 | 1235 | let output_transform = self.output.current_transform(); |
| 1235 | 1236 | let output_mode = self.output.current_mode().unwrap(); |
| 1236 | 1237 | let output_size = output_transform.transform_size(output_mode.size); |
| @@ -1251,7 +1252,7 @@ impl Monitor<Window> { | ||
| 1251 | 1252 | Some(RelocateRenderElement::from_element( |
| 1252 | 1253 | CropRenderElement::from_element( |
| 1253 | 1254 | elem, |
| 1254 | - 1., | |
| 1255 | + output_scale, | |
| 1255 | 1256 | Rectangle::from_loc_and_size((0, 0), output_size), |
| 1256 | 1257 | )?, |
| 1257 | 1258 | (0, -offset), |
| @@ -1262,7 +1263,7 @@ impl Monitor<Window> { | ||
| 1262 | 1263 | Some(RelocateRenderElement::from_element( |
| 1263 | 1264 | CropRenderElement::from_element( |
| 1264 | 1265 | elem, |
| 1265 | - 1., | |
| 1266 | + output_scale, | |
| 1266 | 1267 | Rectangle::from_loc_and_size((0, 0), output_size), |
| 1267 | 1268 | )?, |
| 1268 | 1269 | (0, -offset + output_size.h), |
| @@ -1279,7 +1280,7 @@ impl Monitor<Window> { | ||
| 1279 | 1280 | Some(RelocateRenderElement::from_element( |
| 1280 | 1281 | CropRenderElement::from_element( |
| 1281 | 1282 | elem, |
| 1282 | - 1., | |
| 1283 | + output_scale, | |
| 1283 | 1284 | Rectangle::from_loc_and_size((0, 0), output_size), |
| 1284 | 1285 | )?, |
| 1285 | 1286 | (0, 0), |
| @@ -1823,6 +1824,14 @@ impl Workspace<Window> { | ||
| 1823 | 1824 | return vec![]; |
| 1824 | 1825 | } |
| 1825 | 1826 | |
| 1827 | + // FIXME: workspaces should probably cache their last used scale so they can be correctly | |
| 1828 | + // rendered even with no outputs connected. | |
| 1829 | + let output_scale = self | |
| 1830 | + .output | |
| 1831 | + .as_ref() | |
| 1832 | + .map(|o| Scale::from(o.current_scale().fractional_scale())) | |
| 1833 | + .unwrap_or(Scale::from(1.)); | |
| 1834 | + | |
| 1826 | 1835 | let mut rv = vec![]; |
| 1827 | 1836 | let view_pos = self.view_pos(); |
| 1828 | 1837 | |
| @@ -1840,8 +1849,8 @@ impl Workspace<Window> { | ||
| 1840 | 1849 | } |
| 1841 | 1850 | rv.extend(active_win.render_elements( |
| 1842 | 1851 | renderer, |
| 1843 | - win_pos.to_physical(1), | |
| 1844 | - Scale::from(1.), | |
| 1852 | + win_pos.to_physical_precise_round(output_scale), | |
| 1853 | + output_scale, | |
| 1845 | 1854 | 1., |
| 1846 | 1855 | )); |
| 1847 | 1856 | |
| @@ -1862,8 +1871,8 @@ impl Workspace<Window> { | ||
| 1862 | 1871 | |
| 1863 | 1872 | rv.extend(win.render_elements( |
| 1864 | 1873 | renderer, |
| 1865 | - win_pos.to_physical(1), | |
| 1866 | - Scale::from(1.), | |
| 1874 | + win_pos.to_physical_precise_round(output_scale), | |
| 1875 | + output_scale, | |
| 1867 | 1876 | 1., |
| 1868 | 1877 | )); |
| 1869 | 1878 | } |
src/niri.rsmodified+24 -13
| @@ -799,6 +799,7 @@ impl Niri { | ||
| 799 | 799 | renderer: &mut GlesRenderer, |
| 800 | 800 | output: &Output, |
| 801 | 801 | ) -> Vec<OutputRenderElements<GlesRenderer>> { |
| 802 | + let output_scale = Scale::from(output.current_scale().fractional_scale()); | |
| 802 | 803 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; |
| 803 | 804 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); |
| 804 | 805 | |
| @@ -825,7 +826,7 @@ impl Niri { | ||
| 825 | 826 | } else { |
| 826 | 827 | default_hotspot |
| 827 | 828 | }; |
| 828 | - let pointer_pos = (pointer_pos - hotspot.to_f64()).to_physical_precise_round(1.); | |
| 829 | + let pointer_pos = (pointer_pos - hotspot.to_f64()).to_physical_precise_round(output_scale); | |
| 829 | 830 | |
| 830 | 831 | let mut pointer_elements = match &self.cursor_image { |
| 831 | 832 | CursorImageStatus::Hidden => vec![], |
| @@ -843,7 +844,7 @@ impl Niri { | ||
| 843 | 844 | renderer, |
| 844 | 845 | surface, |
| 845 | 846 | pointer_pos, |
| 846 | - 1., | |
| 847 | + output_scale, | |
| 847 | 848 | 1., |
| 848 | 849 | Kind::Cursor, |
| 849 | 850 | ), |
| @@ -854,7 +855,7 @@ impl Niri { | ||
| 854 | 855 | renderer, |
| 855 | 856 | dnd_icon, |
| 856 | 857 | pointer_pos, |
| 857 | - 1., | |
| 858 | + output_scale, | |
| 858 | 859 | 1., |
| 859 | 860 | Kind::Unspecified, |
| 860 | 861 | )); |
| @@ -871,6 +872,8 @@ impl Niri { | ||
| 871 | 872 | ) -> Vec<OutputRenderElements<GlesRenderer>> { |
| 872 | 873 | let _span = tracy_client::span!("Niri::render"); |
| 873 | 874 | |
| 875 | + let output_scale = Scale::from(output.current_scale().fractional_scale()); | |
| 876 | + | |
| 874 | 877 | // Get monitor elements. |
| 875 | 878 | let mon = self.monitor_set.monitor_for_output(output).unwrap(); |
| 876 | 879 | let monitor_elements = mon.render_elements(renderer); |
| @@ -901,8 +904,8 @@ impl Niri { | ||
| 901 | 904 | surface |
| 902 | 905 | .render_elements( |
| 903 | 906 | renderer, |
| 904 | - loc.to_physical_precise_round(1.), | |
| 905 | - Scale::from(1.), | |
| 907 | + loc.to_physical_precise_round(output_scale), | |
| 908 | + output_scale, | |
| 906 | 909 | 1., |
| 907 | 910 | ) |
| 908 | 911 | .into_iter() |
| @@ -926,8 +929,8 @@ impl Niri { | ||
| 926 | 929 | surface |
| 927 | 930 | .render_elements( |
| 928 | 931 | renderer, |
| 929 | - loc.to_physical_precise_round(1.), | |
| 930 | - Scale::from(1.), | |
| 932 | + loc.to_physical_precise_round(output_scale), | |
| 933 | + output_scale, | |
| 931 | 934 | 1., |
| 932 | 935 | ) |
| 933 | 936 | .into_iter() |
| @@ -1084,6 +1087,7 @@ impl Niri { | ||
| 1084 | 1087 | let _span = tracy_client::span!("Niri::send_for_screen_cast"); |
| 1085 | 1088 | |
| 1086 | 1089 | let size = output.current_mode().unwrap().size; |
| 1090 | + let scale = Scale::from(output.current_scale().fractional_scale()); | |
| 1087 | 1091 | |
| 1088 | 1092 | for cast in &mut self.casts { |
| 1089 | 1093 | if !cast.is_active.get() { |
| @@ -1119,7 +1123,7 @@ impl Niri { | ||
| 1119 | 1123 | let dmabuf = cast.dmabufs.borrow()[&fd].clone(); |
| 1120 | 1124 | |
| 1121 | 1125 | // FIXME: Hidden / embedded / metadata cursor |
| 1122 | - render_to_dmabuf(backend.renderer(), dmabuf, size, elements).unwrap(); | |
| 1126 | + render_to_dmabuf(backend.renderer(), dmabuf, size, scale, elements).unwrap(); | |
| 1123 | 1127 | |
| 1124 | 1128 | let maxsize = data.as_raw().maxsize; |
| 1125 | 1129 | let chunk = data.chunk_mut(); |
| @@ -1139,8 +1143,9 @@ impl Niri { | ||
| 1139 | 1143 | let _span = tracy_client::span!("Niri::screenshot"); |
| 1140 | 1144 | |
| 1141 | 1145 | let size = output.current_mode().unwrap().size; |
| 1146 | + let scale = Scale::from(output.current_scale().fractional_scale()); | |
| 1142 | 1147 | let elements = self.render(renderer, output, true); |
| 1143 | - let pixels = render_to_vec(renderer, size, &elements)?; | |
| 1148 | + let pixels = render_to_vec(renderer, size, scale, &elements)?; | |
| 1144 | 1149 | |
| 1145 | 1150 | let path = make_screenshot_path().context("error making screenshot path")?; |
| 1146 | 1151 | debug!("saving screenshot to {path:?}"); |
| @@ -1174,6 +1179,7 @@ impl Niri { | ||
| 1174 | 1179 | let outputs: Vec<_> = self.global_space.outputs().cloned().collect(); |
| 1175 | 1180 | for output in outputs { |
| 1176 | 1181 | let geom = self.global_space.output_geometry(&output).unwrap(); |
| 1182 | + // FIXME: this does not work when outputs can have non-1 scale. | |
| 1177 | 1183 | let geom = geom.to_physical(1); |
| 1178 | 1184 | |
| 1179 | 1185 | size.w = max(size.w, geom.loc.x + geom.size.w); |
| @@ -1185,7 +1191,8 @@ impl Niri { | ||
| 1185 | 1191 | })); |
| 1186 | 1192 | } |
| 1187 | 1193 | |
| 1188 | - let pixels = render_to_vec(renderer, size, &elements)?; | |
| 1194 | + // FIXME: scale. | |
| 1195 | + let pixels = render_to_vec(renderer, size, Scale::from(1.), &elements)?; | |
| 1189 | 1196 | |
| 1190 | 1197 | let path = make_screenshot_path().context("error making screenshot path")?; |
| 1191 | 1198 | debug!("saving screenshot to {path:?}"); |
| @@ -1232,6 +1239,7 @@ impl ClientData for ClientState { | ||
| 1232 | 1239 | fn render_and_download( |
| 1233 | 1240 | renderer: &mut GlesRenderer, |
| 1234 | 1241 | size: Size<i32, Physical>, |
| 1242 | + scale: Scale<f64>, | |
| 1235 | 1243 | elements: &[impl RenderElement<GlesRenderer>], |
| 1236 | 1244 | ) -> anyhow::Result<GlesMapping> { |
| 1237 | 1245 | let _span = tracy_client::span!("render_and_download"); |
| @@ -1255,7 +1263,7 @@ fn render_and_download( | ||
| 1255 | 1263 | |
| 1256 | 1264 | for element in elements.iter().rev() { |
| 1257 | 1265 | let src = element.src(); |
| 1258 | - let dst = element.geometry(Scale::from(1.)); | |
| 1266 | + let dst = element.geometry(scale); | |
| 1259 | 1267 | element |
| 1260 | 1268 | .draw(&mut frame, src, dst, &[output_rect]) |
| 1261 | 1269 | .context("error drawing element")?; |
| @@ -1273,11 +1281,13 @@ fn render_and_download( | ||
| 1273 | 1281 | fn render_to_vec( |
| 1274 | 1282 | renderer: &mut GlesRenderer, |
| 1275 | 1283 | size: Size<i32, Physical>, |
| 1284 | + scale: Scale<f64>, | |
| 1276 | 1285 | elements: &[impl RenderElement<GlesRenderer>], |
| 1277 | 1286 | ) -> anyhow::Result<Vec<u8>> { |
| 1278 | 1287 | let _span = tracy_client::span!("render_to_vec"); |
| 1279 | 1288 | |
| 1280 | - let mapping = render_and_download(renderer, size, elements).context("error rendering")?; | |
| 1289 | + let mapping = | |
| 1290 | + render_and_download(renderer, size, scale, elements).context("error rendering")?; | |
| 1281 | 1291 | let copy = renderer |
| 1282 | 1292 | .map_texture(&mapping) |
| 1283 | 1293 | .context("error mapping texture")?; |
| @@ -1288,6 +1298,7 @@ fn render_to_dmabuf( | ||
| 1288 | 1298 | renderer: &mut GlesRenderer, |
| 1289 | 1299 | dmabuf: Dmabuf, |
| 1290 | 1300 | size: Size<i32, Physical>, |
| 1301 | + scale: Scale<f64>, | |
| 1291 | 1302 | elements: &[OutputRenderElements<GlesRenderer>], |
| 1292 | 1303 | ) -> anyhow::Result<()> { |
| 1293 | 1304 | let _span = tracy_client::span!("render_to_dmabuf"); |
| @@ -1305,7 +1316,7 @@ fn render_to_dmabuf( | ||
| 1305 | 1316 | |
| 1306 | 1317 | for element in elements.iter().rev() { |
| 1307 | 1318 | let src = element.src(); |
| 1308 | - let dst = element.geometry(Scale::from(1.)); | |
| 1319 | + let dst = element.geometry(scale); | |
| 1309 | 1320 | element |
| 1310 | 1321 | .draw(&mut frame, src, dst, &[output_rect]) |
| 1311 | 1322 | .context("error drawing element")?; |