Skip to content

Instantly share code, notes, and snippets.

@jnerin
Last active November 25, 2022 15:16
Show Gist options
  • Save jnerin/3fe083bca3b7097ee1b2 to your computer and use it in GitHub Desktop.
Save jnerin/3fe083bca3b7097ee1b2 to your computer and use it in GitHub Desktop.
Quick & dirty tool to generate fb.modes file from xorg detected valid modes. Just a way to avoid boring myself to death by manually generating modelines to convert them later to fb.modes with modeline2fb.
#!/usr/bin/perl
#
# Quick & dirty tool to generate fb.modes file from xorg detected valid modes.
# Just a way to avoid boring myself to death by manually generating modelines
# to convert them later to fb.modes with modeline2fb
#
# First we generate a X logfile with detailed info and later convert it.
#
# Usage:
# X :1 -logverbose 6
# cat /var/log/Xorg.1.log | ./xorg-log-extract-modelines.pl | modeline2fb -d 16 | sudo tee /etc/fb.modes
# Sample blocks from the log:
#
# A valid mode
#
#[ 26984.986] (II) NVIDIA(GPU-0): Validating Mode "1680x1050":
#[ 26984.986] (II) NVIDIA(GPU-0): 1680 x 1050 @ 60 Hz
#[ 26984.986] (II) NVIDIA(GPU-0): Mode Source: EDID
#[ 26984.986] (II) NVIDIA(GPU-0): Pixel Clock : 146.25 MHz
#[ 26984.986] (II) NVIDIA(GPU-0): HRes, HSyncStart : 1680, 1784
#[ 26984.986] (II) NVIDIA(GPU-0): HSyncEnd, HTotal : 1960, 2240
#[ 26984.986] (II) NVIDIA(GPU-0): VRes, VSyncStart : 1050, 1053
#[ 26984.986] (II) NVIDIA(GPU-0): VSyncEnd, VTotal : 1059, 1089
#[ 26984.986] (II) NVIDIA(GPU-0): H/V Polarity : -/+
#[ 26984.987] (II) NVIDIA(GPU-0): Viewport 1680x1050+0+0
#[ 26984.987] (II) NVIDIA(GPU-0): Horizontal Taps 0
#[ 26984.987] (II) NVIDIA(GPU-0): Vertical Taps 0
#[ 26984.987] (II) NVIDIA(GPU-0): Base SuperSample x4
#[ 26984.987] (II) NVIDIA(GPU-0): Base Depth 32
#[ 26984.987] (II) NVIDIA(GPU-0): Distributed Rendering 1
#[ 26984.987] (II) NVIDIA(GPU-0): Overlay Depth 32
#[ 26984.987] (II) NVIDIA(GPU-0): Mode is valid.
#
# An invalid mode
#
#[ 26985.015] (II) NVIDIA(GPU-0): Validating Mode "1680x1050":
#[ 26985.015] (II) NVIDIA(GPU-0): 1680 x 1050 @ 60 Hz
#[ 26985.015] (II) NVIDIA(GPU-0): Mode Source: X Configuration file ModeLine
#[ 26985.015] (II) NVIDIA(GPU-0): Pixel Clock : 119.00 MHz
#[ 26985.015] (II) NVIDIA(GPU-0): HRes, HSyncStart : 1680, 1728
#[ 26985.015] (II) NVIDIA(GPU-0): HSyncEnd, HTotal : 1760, 1840
#[ 26985.015] (II) NVIDIA(GPU-0): VRes, VSyncStart : 1050, 1053
#[ 26985.015] (II) NVIDIA(GPU-0): VSyncEnd, VTotal : 1059, 1080
#[ 26985.015] (II) NVIDIA(GPU-0): H/V Polarity : +/+
#[ 26985.015] (WW) NVIDIA(GPU-0): Mode is rejected: Only EDID-provided modes are allowed on
#[ 26985.015] (WW) NVIDIA(GPU-0): Maxdata/Belinea o.disp2.1_22W (DFP-0) (continuous
#[ 26985.015] (WW) NVIDIA(GPU-0): frequency modes not allowed).
use strict;
my $text;
my $modeline = qr/
Validating\ Mode\ "(?<mode>[^"]+)"
[^@]+@\ (?<refresh>[0-9]+)\ Hz
[^0-9]+(?<clock>[0-9\.]+)\ MHz
[^0-9]+(?<hres>[0-9]+),\ +(?<hsyncstart>[0-9]+)
[^0-9]+(?<hsyncend>[0-9]+),\ +(?<htotal>[0-9]+)
[^0-9]+(?<vres>[0-9]+),\ +(?<vsyncstart>[0-9]+)
[^0-9]+(?<vsyncend>[0-9]+),\ +(?<vtotal>[0-9]+)
[^:]+\:\ (?<hpolarity>[+-])\/(?<vpolarity>[+-])
(.*?)Mode.*?is\ (?<result>\w+) # Too risky to do [^M]+
/xs;
# (.*?)Mode\ is\ (?<result>\w+) # Lazy dot
# [^M]+Mode\ is\ (?<result>\w+) # Negated "M"
while (<STDIN>) {
$text .= $_; # Eat everything, we need whole blocks for the regexp
}
$text =~ s/^[^:]+://mg; # Dump the beginning of each line of the log
#print $text;
while ($text =~ m/$modeline/g){
print "ModeLine \"$+{mode}_$+{refresh}\" $+{clock} $+{hres} $+{hsyncstart} $+{hsyncend} $+{htotal} $+{vres} $+{vsyncstart} $+{vsyncend} $+{vtotal} $+{hpolarity}HSync $+{vpolarity}VSync\n" if ($+{result} eq "valid");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment