]> git.mar77i.info Git - laptop-config/blob - setup_screens.sh
initial commit
[laptop-config] / setup_screens.sh
1 #!/bin/bash
2
3 kernel_pkg_ver() {
4 pacman -Qi linux | sed -rn 's/^Version\s*:\s*(.*)$/\1/p'
5 }
6
7 kernel_running_ver() {
8 uname -r | sed -r 's/-/./'
9 }
10
11 try_finding_stylus() {
12 local found=0
13 if [[ "$(kernel_pkg_ver)" != "$(kernel_running_ver)" ]]; then
14 echo "Warning: Kernel was updated: You need a reboot!" >&2
15 fi
16 echo "Tip with the stylus on monitor ${1}"
17 for (( i = 0; i < 16; i++ )); do
18 sleep 1
19 if grep -Fq "${2}" < <(xinput list --name-only); then
20 found=1
21 break
22 fi
23 done
24 if (( !found )); then
25 echo "Error: Stylus was not found: ${2}" >&2
26 return 1
27 fi
28
29 }
30
31 setup_styluses() {
32 local -A styluses=(
33 [eDP-1]="ELAN9008:00 04F3:425B Stylus Pen (0)"
34 [eDP-2]="ELAN9009:00 04F3:425A Stylus Pen (0)"
35 )
36 local mon stylus
37 for mon in "${!styluses[@]}"; do
38 stylus="${styluses[${mon}]}"
39 xinput disable "${stylus}"
40 if grep -q "${mon}" < <(xrandr --listactivemonitors); then
41 if ! grep -Fq "${stylus}" < <(xinput list --name-only); then
42 try_finding_stylus "${mon}" "${stylus}" || return 1
43 fi
44 xinput enable "${stylus}"
45 xinput --map-to-output "${stylus}" "${mon}"
46 echo "$(date) mapped stylus pen ${stylus%% *} to ${mon}" >&2
47 else
48 echo "$(date) disabled stylus pen ${stylus%% *}" >&2
49 fi
50 done
51 }
52
53 setup_touchpads() {
54 local touchpads=(
55 "ELAN9008:00 04F3:425B"
56 "ELAN9008:00 04F3:425B Touchpad"
57 "ELAN9009:00 04F3:425A"
58 "ELAN9009:00 04F3:425A Touchpad"
59 )
60 local touchpad
61 for touchpad in "${touchpads[@]}"; do
62 xinput disable "${touchpad}"
63 done
64 }
65
66 check_config () {
67 local config
68 for config in "${configs[@]}"; do
69 if [[ "${1}" == "${config}" ]]; then
70 return 0
71 fi
72 done
73 return 1
74 }
75
76 exit_script() {
77 local ret="${?}"
78 if (( wait_for_keypress )); then
79 echo "Press a key!"
80 read -n1
81 fi
82 echo exit "${1:-${ret}}"
83 }
84
85 help() {
86 echo "TODO: help text"
87 exit 0
88 }
89
90 wait_for_keypress=0
91 load_saved=
92 configs=()
93
94 if ! output="$(lxqt-config-monitor --list-saved 2>/dev/null)"; then
95 lxqt-config-monitor --list-saved
96 exit_script
97 elif [[ "${output}" ]]; then
98 mapfile -d $'\n' -t configs < <(printf '%s\n' "${output}")
99 fi
100 unset output
101 list_saved_failed="${?}"
102 while (( ${#} )); do
103 case "${1}" in
104 --wait-for-keypress)
105 wait_for_keypress=1
106 ;;
107 --load-saved)
108 load_saved="${2}"
109 shift
110 ;;
111 --help)
112 help
113 ;;
114 *)
115 echo "Error: unknown argument: ${1}" >&2
116 exit_script 1
117 ;;
118 esac
119 shift
120 done
121
122 if [[ "${load_saved}" ]]; then
123 (( list_saved_failed )) && exit "${list_saved_failed}"
124 if check_config "${load_saved}"; then
125 lxqt-config-monitor --load-saved "${load_saved}" || exit 1
126 sleep .4
127 else
128 echo "Error: no such saved config: ${load_saved}" >&2
129 exit_script 1
130 fi
131 fi
132
133 setup_touchpads
134 setup_styluses
135
136 exit_script