Linux 检索监视器名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10500521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux retrieve monitor names
提问by Jānis Gruzis
Situation: I'm using multiple monitors and I want to get their names in bash. Currently I'm using Ubuntu 10.04.
情况:我正在使用多个显示器,我想在 bash 中获取它们的名称。目前我使用的是 Ubuntu 10.04。
I know about xrandr. From it I can get only statistics data. What I want is to read all monitor names in an array to work with them.
我知道 xrandr。从它我只能得到统计数据。我想要的是读取数组中的所有监视器名称以使用它们。
Is there a clear way to do that without cutting names from some kind of string? A clear way would be reading them from file. A not clear way would be to pipe xrandr output to some sort a function to cut names out from it.
有没有一种明确的方法可以在不从某种字符串中删除名称的情况下做到这一点?一个明确的方法是从文件中读取它们。一种不清楚的方法是将 xrandr 输出通过管道传输到某种函数以从中删除名称。
采纳答案by Beni Cherniavsky-Paskin
sudo get-edid
didn't work for me. (EDIT: now works on another computer, Lubuntu 14.10; I'd blame BIOS differences but that's a random guess...)
sudo get-edid
对我不起作用。(编辑:现在可以在另一台计算机上运行,Lubuntu 14.10;我会责怪 BIOS 差异,但这是一个随机猜测......)
Anyway under X, xrandr --verbose
prints the EDID block. Here is a quick and dirty way to extract it and pass to parse-edid
:
无论如何,在 X 下,xrandr --verbose
打印 EDID 块。这是一种快速而肮脏的方法来提取它并传递给parse-edid
:
#!/bin/bash
xrandr --verbose | perl -ne '
if ((/EDID(_DATA)?:/.../:/) && !/:/) {
s/^\s+//;
chomp;
$hex .= $_;
} elsif ($hex) {
# Use "|strings" if you dont have read-edid package installed
# and just want to see (or grep) the human-readable parts.
open FH, "|parse-edid";
print FH pack("H*", $hex);
$hex = "";
}'
回答by Diego Torres Milano
You may try ddcprobe
and/or get-edid
您可以尝试ddcprobe
和/或get-edid
$ sudo apt-get install xresprobe read-edid
$ sudo ddcprobe
$ sudo get-edid
回答by pzanoni
If you don't want to parse xrandr
output, write a C program using libXrandr
that gets only what you want. If all you want to do is to query information, it can be done quickly. Read this document.
如果您不想解析xrandr
输出,请使用libXrandr
仅获取您想要的内容编写 C 程序。如果您只想查询信息,可以很快完成。阅读本文档。
If you want to get the realmonitor name, an alternative to @dtmilano's solution is to get the EDID property of the monitor using libXrandr and then manually parse it and print (read the EDID specification).
如果你想获得真正的监视器名称,@dtmilano 解决方案的替代方法是使用 libXrandr 获取监视器的 EDID 属性,然后手动解析它并打印(阅读 EDID 规范)。
回答by ghoti
You're looking for EDIDinformation, which is passed along an I2C bus and interpreted by your video driver. As dtmilano says, get-edit from ddcprobe should work.
您正在寻找EDID信息,该信息通过 I2C 总线传递并由您的视频驱动程序进行解释。正如 dtmilano 所说,来自 ddcprobe 的 get-edit 应该可以工作。
You can also get this information by logging your X start:
您还可以通过记录您的 X 开始来获取此信息:
startx -- -logverbose 6
Years ago, I used a package called read-edidto gather this information.
多年前,我使用一个名为read-edid的包来收集这些信息。
The read-edid package may be available in Ubuntu already, according to this blog postfrom 2009.
回答by MestreLion
Inspired by Beni's answer, this will read the EDIDdata using xrandr
and extract the monitors names according to the EDID specification, with no need of any external tools like parse-edid
:
受 Beni 回答的启发,这将根据EDID 规范读取EDID数据xrandr
并提取显示器名称,无需任何外部工具,例如:parse-edid
#!/bin/bash
while read -r output hex conn; do
[[ -z "$conn" ]] && conn=${output%%-*}
echo "# $output $conn $(xxd -r -p <<< "$hex")"
done < <(xrandr --prop | awk '
!/^[ \t]/ {
if (output && hex) print output, hex, conn
output=
hex=""
}
/ConnectorType:/ {conn=}
/[:.]/ && h {
sub(/.*000000fc00/, "", hex)
hex = substr(hex, 0, 26) "0a"
sub(/0a.*/, "", hex)
h=0
}
h {sub(/[ \t]+/, ""); hex = hex $ monitor-switch --list
Connected monitors:
# DFP5 HDMI HT-R391
# DFP7 DVI-I DELL U2412M
$ monitor-switch --list
Connected monitors:
# DisplayPort-1 DisplayPort DELL U2412M
# DisplayPort-3 DisplayPort DELL U2415
# HDMI-A-2 HDMI LG TV
}
/EDID.*:/ {h=1}' | sort
)
Uses awk
to precisely extract the monitor name only, and no extra garbage from the EDID, hence "magic numbers" like 000000fc00
, 26
and 0a
. Finally uses xxd
to convert from hex to ASCII, printing one monitor name per line.
用于仅awk
精确提取监视器名称,而不会从 EDID 中提取额外的垃圾,因此“幻数”如,和。最后用于从十六进制转换为 ASCII,每行打印一个监视器名称。000000fc00
26
0a
xxd
Based on this solution I made a handy script to switch monitors, which can also be used to simply list monitor info:
基于这个解决方案,我制作了一个方便的脚本来切换监视器,它也可以用来简单地列出监视器信息:
#!/bin/bash
#
#
# get-monitors.sh
#
# Get monitor name and some other properties of connected monitors
# by investigating the output of xrandr command and EDID data
# provided by it.
#
# Copyright (C) 2015,2016 Jarno Suni <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. See <http://www.gnu.org/licenses/gpl.html>
set -o nounset
set -o errexit
# EDID format:
# http://en.wikipedia.org/wiki/Extended_Display_Identification_Data#EDID_1.3_data_format
# http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf
declare -r us=';' # separator string;
# If EDID has more than one field with same tag, concatenate them,
# but add this string in between.
declare -r fs=$'\x1f' # Field separator for internal use;
# must be a character that does not occur in data fields.
declare -r invalid_edid_tag='--bad EDID--'
# If base EDID is invalid, don't try to extract information from it,
# but assign this string to the fields.
# Get information in these arrays:
declare -a outs # Output names
declare -a conns # Connection type names (if available)
declare -a names # Monitor names (but empty for some laptop displays)
declare -a datas # Extra data; may include laptop display brand name
# and model name
declare -i no # number of connected outputs (to be counted)
# xrandr command to use as a source of information:
declare -r xrandr_output_cmd="xrandr --prop"
hex_to_ascii() {
echo -n "" | xxd -r -p
}
ascii_to_hex() {
echo -n "" | xxd -p
}
get_info() {
no=0
declare OIFS=$IFS;
IFS=$fs
while read -r output conn hexn hexd; do
outs[no]="${output}"
conns[no]="${conn}"
names[no]="$(hex_to_ascii "$hexn")"
datas[no]="$(hex_to_ascii "$hexd")"
(( ++no ))
done < <(eval $xrandr_output_cmd | gawk -v gfs="$fs" '
function print_fields() {
print output, conn, hexn, hexd
conn=""; hexn=""; hexd=""
}
function append_hex_field(src_hex,position,app_hex, n) {
n=substr(src_hex,position+10,26)
sub(/0a.*/, "", n)
# EDID specification says field ends by 0x0a
# (\n), if it is shorter than 13 bytes.
#sub(/(20)+$/, "", n)
# strip whitespace at the end of ascii string
if (n && app_hex) return app_hex sp n
else return app_hex n
}
function get_hex_edid( hex) {
getline
while (/^[ \t]*[[:xdigit:]]+$/) {
sub(/[ \t]*/, "")
hex = hex $ sudo apt-get install -y hwinfo
...
$ hwinfo --monitor --short
monitor:
SONY TV
AUO LCD Monitor
getline
}
return hex
}
function valid_edid(hex, a, sum) {
if (length(hex)<256) return 0
for ( a=1; a<=256; a+=2 ) {
# this requires gawk
sum+=strtonum("0x" substr(hex,a,2))
# this requires --non-decimal-data for gawk:
#sum+=sprintf("%d", "0x" substr(hex,a,2))
}
if (sum % 256) return 0
return 1
}
BEGIN {
OFS=gfs
}
/[^[:blank:]]+ connected/ {
if (unprinted) print_fields()
unprinted=1
output=
}
/[^[:blank:]]+ disconnected/ {
if (unprinted) print_fields()
unprinted=0
}
/^[[:blank:]]*EDID.*:/ {
hex=get_hex_edid()
if (valid_edid(hex)) {
for ( c=109; c<=217; c+=36 ) {
switch (substr(hex,c,10)) {
case "000000fc00" :
hexn=append_hex_field(hex,c,hexn)
break
case "000000fe00" :
hexd=append_hex_field(hex,c,hexd)
break
}
}
} else {
# set special value to denote invalid EDID
hexn=iet; hexd=iet
}
}
/ConnectorType:/ {
conn=
}
END {
if (unprinted) print_fields()
}' sp=$(ascii_to_hex $us) iet=$(ascii_to_hex $invalid_edid_tag))
IFS="$OIFS"
}
get_info
# print the colums of each display quoted in one row
for (( i=0; i<$no; i++ )); do
echo "'${outs[i]}' '${conns[i]}' '${names[i]}' '${datas[i]}'"
done
回答by jarno
I know this is a dirty way, but it gives me some monitor model name even better than sudo get-edid|parse-edid
. It reads information in arrays, and outputs it in a way that can be read like you would read a file. You may modify it according to your needs.
我知道这是一种肮脏的方式,但它给了我一些比sudo get-edid|parse-edid
. 它读取数组中的信息,并以一种可以像读取文件一样读取的方式输出它。您可以根据需要对其进行修改。
$ while true;
> do
> hwinfo --monitor --short;
> sleep 2;
> done >> monitor.log &
回答by codeman48
Tested on Ubuntu 16.04, 18.04. (I know its too late to answer but this solution is relevant today)
在 Ubuntu 16.04、18.04 上测试。(我知道现在回答已经太晚了,但这个解决方案今天很重要)
##代码##I have two monitors attached. One with the laptop and the other is an external display. As soon as the external monitor is plugged-in or out, this command reflects the change. You continuously need to poll. Removing the --short
option gives more detailed information.
我连接了两个显示器。一个是笔记本电脑,另一个是外接显示器。只要插入或拔出外接显示器,此命令就会反映更改。你不断需要轮询。删除该--short
选项会提供更详细的信息。
You can poll the state with the following background job:
您可以使用以下后台作业轮询状态:
##代码##The while true
loop runs infinite times. The sleep 2
pauses each iteration of the loop for 2 seconds. And the output of hwinfo --monitor --short
is appended to monitor.log
. This log file can give you the activity history of monitor plug-in and plug-out.
在while true
循环运行无限次。该sleep 2
暂停循环2秒的每次迭代。并将 的输出hwinfo --monitor --short
附加到monitor.log
. 该日志文件可以为您提供监视器插件和插件的活动历史记录。
FYI: I am using a background (daemon) python script using the above command (and other similar ones) to detect if someone is doing some HW plug-ins and plug-outs with the systems in the computer lab. If so, I get appropriate notifications that someone plugged-out/in a monitor, mouse or keyboard in almost real-time!
仅供参考:我正在使用上述命令(和其他类似命令)的后台(守护程序)python 脚本来检测是否有人正在使用计算机实验室中的系统进行一些硬件插件和插件。如果是这样,我会收到有关有人几乎实时插入/插入显示器、鼠标或键盘的适当通知!
More info about hwinfo
command is here. Its man pageis also a good source.