Need help with script for monitor information [SOLVED]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Need help with script for monitor information [SOLVED]

Post by Logansfury »

Hello all,

I am using bash commands to build a conky widget for displaying monitor(s) information.

Im usually triple monitor but something happened to my system and im down to two.

I'm working with the command: ddcutil detect

Code: Select all

logansfury@OptiPlex-5040:~$ ddcutil detect
Invalid display
   I2C bus:  /dev/i2c-1
   EDID synopsis:
      Mfg id:               GSM
      Model:                LG ULTRAGEAR
      Product code:         30565
      Serial number:        xxxxxx
      Binary serial number: xxxxxx
      Manufacture year:     2023,  Week: 9
   DDC communication failed

Invalid display
   I2C bus:  /dev/i2c-5
   EDID synopsis:
      Mfg id:               GSM
      Model:                LG ULTRAGEAR
      Product code:         30565
      Serial number:        xxxxxx
      Binary serial number: xxxxxx
      Manufacture year:     2023,  Week: 9
   DDC communication failed

logansfury@OptiPlex-5040:~$
What edit to the code would print out the Manufacture year info please? An online resource I'm using is incapable of figuring it out.

Thanks for reading,

Logan
Last edited by Logansfury on Wed Mar 20, 2024 9:05 am, edited 1 time in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
terry_dwyer
Level 3
Level 3
Posts: 109
Joined: Sat Nov 07, 2020 2:15 am
Location: West Aus.

Re: Need help with script for monitor information

Post by terry_dwyer »

Code: Select all

sudo ddcutil detect > mondat ; cat mondat | egrep -E -w 'Model|year'  ; rm mondat
Have a look (google ) for the use of awk to print substrings from whatever you put in the mondat file
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Need help with script for monitor information

Post by Logansfury »

Thank you very much Terry!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
terry_dwyer
Level 3
Level 3
Posts: 109
Joined: Sat Nov 07, 2020 2:15 am
Location: West Aus.

Re: Need help with script for monitor information

Post by terry_dwyer »

Since you're working with multiple monitors, I have a script I've been fiddling with for a while now, and it may be of benefit to you since you are having issues with your monitor setup.

The script does nothing <TO> your system, it just examines your available display setup and provides a pile of useful info about your whole setup and how all the monitors and their geometry is laid out, taking scaling into account as well.
I've used this script with a 2 monitor display 43" centered above a 17" laptop display
and on my other setup with 2 x 24" displays side by side and centrally located above a 15" laptop
In my case, the 43" display has xrandr scaling if 1.75, and both the 24' displays are scaled to 1.25
In both cases, the laptops are set to 200% using the mint display utility.

Anyway, give it a try and see if you find it useful.

Code: Select all

#!/bin/bash

##########
#
# Project     :
# Started     :
# Author      : Terry Dwyer
# Module      :
# Description :
#
# If scaling is to be used, the legal values will be
# 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75 and 3
# Other values are likely to cause blurring of the display
# I have a 4k 43" monitor and use a scaling factor of 1.75
# and my son who has excellent vision tells me there is
# no blurring evident
#
# <<< Try using this >>>
# xrandr --listactivemonitors | grep DP-1
#  0: +*eDP-1-1 1920/344x1080/194+989+1080  eDP-1-1
#  1: +DP-1-2-1 1920/521x1080/293+0+0  DP-1-2-1
#  2: +DP-1-2-2 1920/521x1080/293+1920+0  DP-1-2-2
#
##########

# start of code here

# Get The caracteristics for the overall screen display (All monitors combined)
Screen=`xrandr | grep Screen | awk '{print $1, $2, $7, $8, $9, $10}' | sed s/,//`
# Declare the array to hold the info for each f the connected monitors
declare -a PortArray
# Get a list of connected monitors into a temp file
xrandr | grep " connected" > mondef.txt
# How many monitors are there
Nummon=`cat mondef.txt | wc -l`

i=1
echo ; echo
#Create an empty array

#PortArray=()

for i in $( seq 1 $((Nummon)) ) ; do
   #Primary=""
   #Read each line of the temp file with the monitor list
   Mon=`awk -v c=$i 'NR==c { print $0 }' mondef.txt`
   # Read each monitor definition into the array
   # Next, sed strips out the primary and prints the current line "1"
   # and the round braces surrounding some of the text in each line
   #PortArray[$i]=`echo "${Mon}" | sed -e 's/ primary//1'`
   PortArray[$i]=`echo "${Mon}" | sed -e 's/ primary//; s/(//; s/)//1'`
   #echo "${PortArray[$i]}"
   #echo "------------------------------------"
   #echo
done

# This is just to check that each array element is acessible
echo "---------------- 0 ------------------"
echo "Display definition: ${Screen}"
i=1
for i in $( seq 1 $((Nummon)) ) ; do
   echo "---------------- $i ------------------"
   echo "Monitor $i: ${PortArray[$i]}"
   #echo "-------------------------------------"
   # Native Res / Monitor res = xrandr scaling factor
   # This applies to both horizontal and vertical values
   # We can conclude that if $NatRes and $MonRes are not
   # the same string value then scaling has been applied
   Port=`echo ${PortArray[$i]} | cut -d " " -f 1`
   # Pfield tells you if the monitor is set to be primary
   # NatRes This is the natural res for the monitor
   MonStr=`xrandr | grep "$Port" -A 1`
   Pfield=`echo ${MonStr} | cut -d " " -f 3`
   if [ ${Pfield} == primary ] ; then
      Primary=Yes
      NatRes=`echo ${MonStr} | cut -d " " -f 16`
   else
      Primary=No
      NatRes=`echo ${MonStr} | cut -d " " -f 15`
   fi
   # NatRes=`echo ${PortArray[$i]} | cut -d " " -f 3 | cut -d "+" -f 1`<< Doesnt work
   # This is the monitor offset in the definition of screen 0
   # Not necessary because $Hoffset and Voffset do the same
   # MonOffset=`xrandr | grep "eDP-1-1" | cut -d " " -f 4 | cut -d "+" -f 2,3`
   # This is the resolution the monitor is currently set to
   MonRes=`echo ${PortArray[$i]} | cut -d " " -f 3 | cut -d "+" -f 1`
   # Compare the string values for NatRes and $MonRes
   if [ $NatRes != $MonRes ] ; then
      Natural=`echo $NatRes | cut -d "x" -f 1`
      Scaled=`echo $MonRes | cut -d "x" -f 1`
#echo "MonRes: $MonRes   Scaled: $Scaled   Natural: $Natural"
#echo "  >> Press any key to continue" ; read z
      Scale=`printf %.2f "$((10**2 * $Scaled / $Natural))e-2"`
   else
      Scale="No Scaling"
   fi
   # This is the Horizontal "X" and Vertical "Y" resolutions of the monitor
   MonX=`echo ${MonRes} | cut -d "x" -f 1` # Horiz
   MonY=`echo ${MonRes} | cut -d "x" -f 2` # Ver
   # Think of the total display areas occupied by all monitors to be
   # a rectangle that each monitor covers a non-overlapping part of.
   # Horizontal offset is the amount in pixels the monitor is distant from
   # the left origin, I.E. the left edge of the total display area
   # The Vertical offset is the distance the monitor is fron the top
   # edge of the total display area
   Hoffset=`echo ${PortArray[$i]} | cut -d " " -f 3 | cut -d "+" -f 2`
   Voffset=`echo ${PortArray[$i]} | cut -d " " -f 3 | cut -d "+" -f 3`

   #echo "Index $i: $Mon"
   echo "=========>>>> Port $i: $Port"
   if [ -n ${Primary} ] ; then
      echo "========>>>> Primary: ${Primary}"
   else
      echo "========>>>> Primary: ${Primary}"
   fi
   echo "=====>>>> Native res: $NatRes"
   echo "====>>>> Monitor res: $MonRes"
   echo "==>>>> Scaling value: $Scale"
   echo "=> Horizontal offset: $Hoffset"
   echo "==>> Vertical offset: $Voffset"
# To determine if the two monitors are centered vertically
# Divide $MonRes by 2, for both monitors add $Hoffset to the
# smaller value if the original larger value is the same as
# the other two added values, both monitors are centered vertically
   if [ ${Hoffset} -eq 0 ] && [ ${Voffset} -eq 0 ] ; then
      echo "*** Monitor on ${Port} is Top left"
      MonleftX=${MonX}
      MonleftY=${MonY}
   fi
   if [ ${Hoffset} -eq ${MonX} ] && [ ${Voffset} -eq 0 ]; then
      echo "*** Monitor on Port ${Port} is Top right"
      MonrightX=${MonX}
      MonrightY=${MonY}
   fi
   if [ ${Voffset} -gt 0 ] && [ ${Hoffset} -gt 0 ] ; then
      #More logic goes here for the horizontal placement
      echo "*** Monitor on ${Port} is below Top monitor"
   fi
   echo
done

echo "-------------------------------------"
echo
echo
echo "The array contains ${#PortArray[@]} elements"

echo "  >> Press <Enter> key to exit" ; read a ; clear
exit 0

Topleft horiz and Vert offsets will equal zero
Topright horiz will be equal or greater than Horiz width of Topleft. vert offset will be zero
Middle could be in the middle of a horizontal line of the middle could be below both other monitors

This assumes the monitor arrangement are:
   ----------------------
   | TopLeft | TopRight |
   ----------------------
         | Laptop |
         ----------


exit 0

# EOF
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Need help with script for monitor information

Post by Logansfury »

Hello Terry,

Thank you for the code! Looking at it I note the prerequisite of:

This assumes the monitor arrangement are:
----------------------
| TopLeft | TopRight |
----------------------
| Laptop |

My setup when working is:

---------------------------
| left | middle| right
---------------------------

and currently is at:

---------------
| left | right
---------------

Will the script work with my setups?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
terry_dwyer
Level 3
Level 3
Posts: 109
Joined: Sat Nov 07, 2020 2:15 am
Location: West Aus.

Re: Need help with script for monitor information

Post by terry_dwyer »

It's been a while since I did anything with the script,so I'm not sure. Why don't you run it and see. It won't do anything but examine your system and report what it finds..
What you assume is not a prerequisite. What you read at the bottom of the script is a description of one of some possible configurations it may be possible to describe. Ignore it.

Edit: Sorry I missed part of your question. If you have two monitors side by side and they have the mating sides set correctly it will most certainly be able to describe what you have setup. Even if yhere is some separation between the mating sides (why would there be) it should not have any trouble.
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Need help with script for monitor information [SOLVED]

Post by Logansfury »

I will definitely play with this!

As to the thread, I finally beat the answer out of an online resource and I have my Manufacture field filled.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Post Reply

Return to “Scripts & Bash”