xrandr-ensure-primary

#519
Raw
Author
winny
Created
June 21, 2022, 4:55 p.m.
Expires
Never
Size
623 bytes
Hits
186
Syntax
Bash
Private
No
#!/usr/bin/env bash
#
# Ensure a display is set to primary.  i3 needs this to know where to put the
# notification area
#.

xrandr -q | awk '
  BEGIN { idx = 0; }
  /connected primary/ {
    printf("Display %s is already primary.\n", $1);
    primary=$1
    exit 0;
  }
  /^[^ ]+ connected/ {
    displays[idx++] = $1;
  }
  END {
    if (primary) {
      exit 0;
    } else if (idx == 0) {
      print "BUG: No displays found.";
      exit 1;
    } else {
      printf("Setting display %s to primary.\n", displays[0]);
      system(sprintf("xrandr --output %s --primary", displays[0]));
    }
  }
  '