/usr/bin/icewm-menu-xrandr is in icewm-common 1.4.3.0~pre-20180217-3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | #!/usr/bin/perl
#
# Create config to enable some popular monitor configurations
#
# Copyright (c) 2017 Eduard Bloch
# License: Simplified BSD License
#
#use Data::Dumper;
use strict;
my $curmon;
my $primary;
my @connected;
my $disco;
my @active;
my $readedid;
my $curedid;
# monitor name mapping, either from cache or built from EDID info later
my %realname;
# sysfs data lookup disabled for now because of not being reliable.
# With Nouveau, some -A token is added to sysfs names.
# With Radeon, the enumeration is not mapped correctly, like this:
# $ ls /sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/drm/card0/*-* -d
# /sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/drm/card0/card0-DP-1 /sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/drm/card0/card0-HDMI-A-1
# /sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/drm/card0/card0-DVI-D-1
# $ xrandr | grep conne
# DisplayPort-0 disconnected primary (normal left inverted right x axis y axis)
# HDMI-A-0 disconnected (normal left inverted right x axis y axis)
# DVI-D-0 connected 1280x1024+0+0 (normal left inverted right x axis y axis) 376mm x 301mm
#
#my $useSysfs=0;
my $xrandr = "xrandr";
sub get_contents {
my $filename = shift;
open my $fh, '<', $filename or return "";
my $data = do { local $/; <$fh> };
return $data;
}
# content cache
my $cacheFingerprint = "";
my $cacheDir = $ENV{XDG_CACHE_HOME} ? $ENV{XDG_CACHE_HOME} : $ENV{HOME}."/.cache";
mkdir $cacheDir if ! -e $cacheDir;
$cacheDir.="/icewm";
mkdir $cacheDir if ! -e $cacheDir;
my $cachePath = "$cacheDir/xrandrmenu.cache";
# start of the EDID descriptor which contains the display name
my $header = chr(0).chr(0xfc).chr(0);
my $end = chr(0x0a);
my @edids = sort(<"/sys/devices/*/*/*/drm/card*/card*-*/edid">, <"/sys/devices/*/*/drm/card*/card*-*/edid">);
## Unfortunately unreliable!
##
## if($useSysfs)
## {
## # xrandr --verbose can be slow, grab data from sysfs if possible
## # Unclear how to map X screens to DRM reliably, use the shortcut only
## # when just one graphics card is installed.
## if($#edids == 0)
## {
## foreach(<"$edidGlobPat">)
## {
## my $data = get_contents($_);
## next if !$data;
## my $hdump;
## /card0.card0-([^\/]+)\/edid/;
## my $connector = $1;
## #$hdump = unpack("H*", $data);
## #$realname{$connector} = pack("H*", $1) if($hdump =~ /00fc00(.*?)0a/)
## #
## # digging through descriptors, https://en.wikipedia.org/wiki/Extended_Display_Identification_Data
## my $pos = index $data, $header, 54;
## if($pos>0)
## {
## $data = substr $data, $pos+3, 15;
## # sanitize first or further RE fail on binary cruft
## $data =~ s/\W/ /g;
## $data =~ s/ *$|^\W*//g;
## #print "# sysfs name: $data\n";
## $realname{$connector} = $data.'~'.$connector;
## }
## }
## }
## }
## else
{
foreach(@edids)
{
my $more = get_contents($_);
next if ! $more;
$more =~ s/\W/_/g; # should be sufficient, and faster than hashing
$cacheFingerprint.= $_.$more
}
if($cacheFingerprint)
{
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat(__FILE__);
$cacheFingerprint.="$atime$mtime$ctime$size$dev$ino\n";
my $fd;
if(open($fd, "<", $cachePath))
{
my $hit = 0;
my @temp;
while(<$fd>)
{
if (!$hit)
{
$hit = ($_ eq $cacheFingerprint) ;
last if !$hit;
next;
}
chomp;
push @temp, $_;
}
%realname = @temp;
}
}
}
my $fishEdid = !%realname;
$xrandr.=" --verbose" if $fishEdid;
# the disconnected displays might become zombies... make sure to turn them off
my $turnRestOff ="";
for(`$xrandr`)
{
if(/^(\S+)\s+(connected|disconnected)\s+(primary)?/)
{
$curmon = $1;
$primary = $curmon if $3;
$disco = ($2 eq "disconnected");
if($disco)
{
#$turnRestOff.=" --display $curmon --off";
}
else
{
push(@connected, $curmon);
}
}
# detect active only when there is a flag in the resolution list...
push(@active, $curmon) if(/^\s.*\*/ && !$disco);
if($fishEdid)
{
# another bit of interest is the EDID stuff, while we are at it...
if(/EDID:/)
{
$readedid = 1;
$curedid = "";
}
elsif(!/:/)
{
if($readedid)
{
/(\w+)/;
$curedid.=$1;
}
}
else
{
if($readedid)
{
$readedid = 0;
if($curedid =~ /00fc00(.*?)0a/)
{
my $name=pack("H*", $1);
# XXX: not sure about endian, check EDID spec
#print "moo, $curedid is $1 for $curmon aka $name\n";
$realname{$curmon} = $name.'~'.$curmon;
}
}
}
}
}
# has primary become disconnected? Just removed? Let the first one be primary then
$primary=$connected[0] if($primary && !grep(/$primary/, @connected));
print "# Next primary: $primary\n";
#print Dumper(\@active,\@connected,$primary, \%realname);
my $second;
# assuming that the primary is always among the connected screens...
# but prefer those which are already active
foreach(@active, @connected)
{
next if $_ eq $primary;
$second = $_;
last;
}
my @notPrimary = grep {$_ ne $primary} @connected;
my @notPrimOrSec = grep {$_ ne $primary && $_ ne $second} @connected;
my $turnNonPrimSecOff = join(" ", map { "--output '$_' --off" } @notPrimOrSec);
sub getName {
my $id = shift;
return $realname{$id} if exists $realname{$id};
# uber-clever xrandr hides the bustype prefix if the name is unambiguous but the kernel doesn't, so try to find the real name there
foreach("A", "D", "I")
{
my $altName = $id;
return $realname{$altName} if($altName =~ s/-(\d)/-A-$1/ && exists $realname{$altName});
}
return $id;
}
# single head activation
my $i=1;
foreach my $mon ($primary, @notPrimary)
{
my $cmd = "prog '". getName($mon)."' setscreen$i sh -c 'xrandr ".
join(" ", map { "--output $_ ".($_ eq $mon ? "--auto --primary" : "--off") } @connected)
."$turnRestOff'\n";
print $cmd;
$i++;
}
exit 0 if 1 == @connected;
# predefined multihead setups
my $cmd = "prog '".getName($primary)." + ".getName($second)."' setscreen12 sh -c 'xrandr --output $primary --auto --output $second --auto --right-of $primary $turnNonPrimSecOff $turnRestOff'\n";
print $cmd;
my $cmd = "prog '".getName($second)." + ".getName($primary)."' setscreen21 sh -c 'xrandr --output $primary --auto --output $second --auto --left-of $primary $turnNonPrimSecOff $turnRestOff'\n";
print $cmd;
my $cmd = "prog '".join(" / ", map { getName($_) } @connected)."' setscreen_all sh -c 'xrandr "
.join(" ", map { "--output $_ --auto ".( $_ eq $primary ? "" : "--same-as $primary")} @connected)
."$turnRestOff'\n";
print $cmd;
my $prev="";
my $cmd = "# all monitors connected from left to right\nprog '"
.join(" + ", map { getName($_) } @connected)."' setscreen_chain sh -c 'xrandr";
for(@connected)
{
$cmd.=" --output $_ --auto";
$cmd.=" --right-of $prev" if $prev;
$prev=$_;
}
$cmd.="$turnRestOff'\n";
print $cmd;
#print Dumper(%realname);
my $fd;
if(open($fd, ">", $cachePath))
{
print $fd $cacheFingerprint;
print $fd "$_\n$realname{$_}\n" foreach(keys %realname);
close $fd;
}
|