forge
Explore
Log in

Don't you dare go Hollow

tty: Extract pick_mode()

Browse files at this commit

eb4946c3d8efb3da3b0370ed49da437007bfe165Ivan MolodetskikhParent: 5f440f7
src/backend/tty.rsmodified+78 -65
@@ -32,7 +32,7 @@ use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties, Subpix
32 use smithay::reexports::calloop::timer::{TimeoutAction, Timer};32 use smithay::reexports::calloop::timer::{TimeoutAction, Timer};
33 use smithay::reexports::calloop::{Dispatcher, LoopHandle, RegistrationToken};33 use smithay::reexports::calloop::{Dispatcher, LoopHandle, RegistrationToken};
34 use smithay::reexports::drm::control::{34 use smithay::reexports::drm::control::{
35- connector, crtc, property, Device, Mode as DrmMode, ModeFlags, ModeTypeFlags,35+ self, connector, crtc, property, Device, Mode as DrmMode, ModeFlags, ModeTypeFlags,
36 };36 };
37 use smithay::reexports::gbm::Modifier;37 use smithay::reexports::gbm::Modifier;
38 use smithay::reexports::input::Libinput;38 use smithay::reexports::input::Libinput;
@@ -620,74 +620,26 @@ impl Tty {
620 trace!("{m:?}");620 trace!("{m:?}");
621 }621 }
622 622
623- let mut mode = None;623+ let (mode, fallback) =
624-624+ pick_mode(&connector, config.mode).ok_or_else(|| anyhow!("no mode"))?;
625- if let Some(target) = &config.mode {625+ if fallback {
626- let refresh = target.refresh.map(|r| (r * 1000.).round() as i32);626+ let target = config.mode.unwrap();
627-627+ warn!(
628- for m in connector.modes() {628+ "configured mode {}x{}{} could not be found, falling back to preferred",
629- if m.size() != (target.width, target.height) {629+ target.width,
630- continue;630+ target.height,
631- }631+ if let Some(refresh) = target.refresh {
632-632+ format!("@{refresh}")
633- if let Some(refresh) = refresh {
634- // If refresh is set, only pick modes with matching refresh.
635- let wl_mode = Mode::from(*m);
636- if wl_mode.refresh == refresh {
637- mode = Some(m);
638- }
639- } else if let Some(curr) = mode {
640- // If refresh isn't set, pick the mode with the highest refresh.
641- if curr.vrefresh() < m.vrefresh() {
642- mode = Some(m);
643- }
644 } else {633 } else {
645- mode = Some(m);634+ String::new()
646- }635+ },
647- }636+ );
648-
649- if mode.is_none() {
650- warn!(
651- "configured mode {}x{}{} could not be found, falling back to preferred",
652- target.width,
653- target.height,
654- if let Some(refresh) = target.refresh {
655- format!("@{refresh}")
656- } else {
657- String::new()
658- },
659- );
660- }
661- }
662-
663- if mode.is_none() {
664- // Pick a preferred mode.
665- for m in connector.modes() {
666- if !m.mode_type().contains(ModeTypeFlags::PREFERRED) {
667- continue;
668- }
669-
670- if let Some(curr) = mode {
671- if curr.vrefresh() < m.vrefresh() {
672- mode = Some(m);
673- }
674- } else {
675- mode = Some(m);
676- }
677- }
678- }
679-
680- if mode.is_none() {
681- // Last attempt.
682- mode = connector.modes().first();
683 }637 }
684-
685- let mode = mode.ok_or_else(|| anyhow!("no mode"))?;
686 debug!("picking mode: {mode:?}");638 debug!("picking mode: {mode:?}");
687 639
688 let surface = device640 let surface = device
689 .drm641 .drm
690- .create_surface(crtc, *mode, &[connector.handle()])?;642+ .create_surface(crtc, mode, &[connector.handle()])?;
691 643
692 // Create GBM allocator.644 // Create GBM allocator.
693 let gbm_flags = GbmBufferFlags::RENDERING | GbmBufferFlags::SCANOUT;645 let gbm_flags = GbmBufferFlags::RENDERING | GbmBufferFlags::SCANOUT;
@@ -710,7 +662,7 @@ impl Tty {
710 },662 },
711 );663 );
712 664
713- let wl_mode = Mode::from(*mode);665+ let wl_mode = Mode::from(mode);
714 output.change_current_state(Some(wl_mode), None, None, None);666 output.change_current_state(Some(wl_mode), None, None, None);
715 output.set_preferred(wl_mode);667 output.set_preferred(wl_mode);
716 668
@@ -801,7 +753,7 @@ impl Tty {
801 let res = device.surfaces.insert(crtc, surface);753 let res = device.surfaces.insert(crtc, surface);
802 assert!(res.is_none(), "crtc must not have already existed");754 assert!(res.is_none(), "crtc must not have already existed");
803 755
804- niri.add_output(output.clone(), Some(refresh_interval(*mode)));756+ niri.add_output(output.clone(), Some(refresh_interval(mode)));
805 757
806 // Power on all monitors if necessary and queue a redraw on the new one.758 // Power on all monitors if necessary and queue a redraw on the new one.
807 niri.event_loop.insert_idle(move |state| {759 niri.event_loop.insert_idle(move |state| {
@@ -1459,3 +1411,64 @@ fn queue_estimated_vblank_timer(
1459 .unwrap();1411 .unwrap();
1460 output_state.redraw_state = RedrawState::WaitingForEstimatedVBlank(token);1412 output_state.redraw_state = RedrawState::WaitingForEstimatedVBlank(token);
1461 }1413 }
1414+
1415+fn pick_mode(
1416+ connector: &connector::Info,
1417+ target: Option<niri_config::Mode>,
1418+) -> Option<(control::Mode, bool)> {
1419+ let mut mode = None;
1420+ let mut fallback = false;
1421+
1422+ if let Some(target) = target {
1423+ let refresh = target.refresh.map(|r| (r * 1000.).round() as i32);
1424+
1425+ for m in connector.modes() {
1426+ if m.size() != (target.width, target.height) {
1427+ continue;
1428+ }
1429+
1430+ if let Some(refresh) = refresh {
1431+ // If refresh is set, only pick modes with matching refresh.
1432+ let wl_mode = Mode::from(*m);
1433+ if wl_mode.refresh == refresh {
1434+ mode = Some(m);
1435+ }
1436+ } else if let Some(curr) = mode {
1437+ // If refresh isn't set, pick the mode with the highest refresh.
1438+ if curr.vrefresh() < m.vrefresh() {
1439+ mode = Some(m);
1440+ }
1441+ } else {
1442+ mode = Some(m);
1443+ }
1444+ }
1445+
1446+ if mode.is_none() {
1447+ fallback = true;
1448+ }
1449+ }
1450+
1451+ if mode.is_none() {
1452+ // Pick a preferred mode.
1453+ for m in connector.modes() {
1454+ if !m.mode_type().contains(ModeTypeFlags::PREFERRED) {
1455+ continue;
1456+ }
1457+
1458+ if let Some(curr) = mode {
1459+ if curr.vrefresh() < m.vrefresh() {
1460+ mode = Some(m);
1461+ }
1462+ } else {
1463+ mode = Some(m);
1464+ }
1465+ }
1466+ }
1467+
1468+ if mode.is_none() {
1469+ // Last attempt.
1470+ mode = connector.modes().first();
1471+ }
1472+
1473+ mode.map(|m| (*m, fallback))
1474+}