summaryrefslogtreecommitdiff
path: root/ar/.config/x11/xprofile
blob: c8de15914f8f14c0e936574de23d09d050a30c70 (plain)
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
#!/bin/sh

default="--mode 1920x1080 --rotate normal --scale 1.0x1.0"
default_dpi=96 # adjust the number if the resolution is higher than 1920x1080 or monitor size is bigger than 24"
displays=$(xrandr -q | grep -w "connected")
x=$(echo "$displays" | grep "mm" | sed -E "s/.* ([0-9]+)mm x ([0-9]+)mm.*/\1/")
y=$(echo "$displays" | grep "mm" | sed -E "s/.* ([0-9]+)mm x ([0-9]+)mm.*/\2/")
calx=$(((1920 + (x / 10) - 1) / (x / 10))) # Ceiling for x
caly=$(((1080 + (y / 10) - 1) / (y / 10))) # Ceiling for y
dpi=$(if [ "$calx" -gt "$caly" ]; then echo "$calx"; else echo "$caly"; fi)
dpi=$(if [ "$dpi" -lt "$default_dpi" ]; then echo "$default_dpi"; else echo "$dpi"; fi)

for connected in $(echo "$displays" | cut -d ' ' -f 1); do
  case $connected in
  eDP*) edp="$connected" ;;
  HDMI*) hdmi="$connected" ;;
  DP*) dp="$connected" ;;
  *) display="$connected" ;;
  esac
done

# If the lid is closed, turn off the laptop's screen
if grep -q "disabled" /sys/class/drm/card0-eDP-1/enabled || grep -q "closed" /proc/acpi/button/lid/LID/state; then
  if [ -n "$hdmi" ] && [ -z "$dp" ] && [ -n "$edp" ]; then
    xrandr --output "$edp" --off --output "$hdmi" --primary $default --dpi "$dpi"
  elif [ -z "$hdmi" ] && [ -n "$dp" ] && [ -n "$edp" ]; then
    xrandr --output "$edp" --off --output "$dp" --primary $default --dpi "$dpi"
  else
    xrandr --output "$edp" --off --output "$display" --primary $default --dpi "$dpi"
  fi
else
  # Apply display settings when lid is open
  if [ -n "$hdmi" ] && [ -z "$dp" ] && [ -n "$edp" ]; then
    xrandr --output "$edp" --pos 1920x0 $default --dpi "$dpi" --output "$hdmi" --primary --pos 0x0 $default --dpi "$dpi"
  elif [ -z "$hdmi" ] && [ -n "$dp" ] && [ -n "$edp" ]; then
    xrandr --output "$edp" --pos 1920x0 $default --dpi "$dpi" --output "$dp" --primary --pos 0x0 $default --dpi "$dpi"
  elif [ -z "$hdmi" ] && [ -z "$dp" ] && [ -n "$edp" ]; then
    xrandr --output "$edp" --primary $default --dpi "$dpi"
  else
    xrandr --output "$display" --primary --auto
  fi
fi

setbg & # set the background with the `setbg` script
# fcitx5 & # set the input method, but it will be separate with fcitx5-remote

# Uncomment to use Xresources colors/settings on startup
# xrdb "${XDG_CONFIG_HOME:-${HOME}/.config}/x11/xresources" &
# xrdbpid=$!

autostart="mpd xcompmgr dunst unclutter pipewire remapd"

for program in $autostart; do
  pidof -sx "$program" || "$program" &
done >/dev/null 2>&1

# Ensure that xrdb has finished running before moving on to start the WM/DE.
[ -n "$xrdbpid" ] && wait "$xrdbpid"