1. pkgname=asus-screen-brightness
  2. pkgver=1.0
  3. pkgrel=1
  4. pkgdesc="Helper for adjusting screen brightness in Asus Zenbook UX31A and similar models"
  5. url="https://wiki.archlinux.org/index.php/ASUS_Zenbook_Prime_UX31A#Screen_backlight"
  6. arch=('any')
  7. license=(FDL1.3)
  8. depends=('bash')
  9. optdepends=('systemd: automatically grant user access to keyboard backlight control')
  10.  
  11. # general note: this package contains only two files; I don't see any added
  12. # value in stuffing these into a tarball when I can also place them in here
  13. # using heredocs (cf. http://stackoverflow.com/a/2954835/334761 for reference)
  14. build() {
  15.     mkdir -p $srcdir/$pkgname-$pkgver/
  16.     cd $srcdir/$pkgname-$pkgver/
  17.     # write the backlight helper script
  18.     cat <<-'EOF' > ./asus-screen-brightness
  19.         #!/bin/bash
  20.        
  21.         path="/sys/class/backlight/intel_backlight"
  22.        
  23.         # max should be 4296
  24.         max=$(cat ${path}/max_brightness)
  25.        
  26.         step=$(($max/10))
  27.         # for a value smaller than treshold, the steps are smaller for a more accurate setting at low in          tensity
  28.         treshold=$((2*$step))
  29.         previous=$(cat ${path}/brightness)
  30.        
  31.         function commit {
  32.             if [[ $1 = [0-9]* ]]
  33.             then
  34.                 if [[ $1 -gt $max ]]
  35.                 then
  36.                     next=$max
  37.                 elif [[ $1 -lt 0 ]]
  38.                 then
  39.                     next=0
  40.                 else
  41.                     next=$1
  42.                 fi
  43.                 echo $next >> ${path}/brightness
  44.                 exit 0
  45.             else
  46.                 exit 1
  47.             fi
  48.         }
  49.        
  50.         case "$1" in
  51.          up)
  52.             if [[ $previous -le $treshold ]]
  53.             then
  54.                 step=$(($step/4))
  55.             fi
  56.             commit $(($previous + $step))
  57.            ;;
  58.          down)
  59.             if [[ $previous -le $treshold ]]
  60.             then
  61.                 step=$(($step/4))
  62.             fi
  63.             commit $(($previous - $step))
  64.            ;;
  65.          max)
  66.              commit $max
  67.            ;;
  68.          off)
  69.              commit 0
  70.            ;;
  71.          show)
  72.              echo $previous
  73.            ;;
  74.          night)
  75.              commit $(($max/4))
  76.              ;;
  77.          allowusers)
  78.              # Allow members of users group to change brightness
  79.              sudo chgrp users ${path}/brightness
  80.              sudo chmod g+w ${path}/brightness
  81.            ;;
  82.          disallowusers)
  83.              # Disallow members of users group to change brightness
  84.              sudo chgrp root ${path}/brightness
  85.              sudo chmod g-w ${path}/brightness
  86.            ;;
  87.          *)
  88.              commit $1
  89.         esac
  90.        
  91.         exit 0     
  92.  
  93.     EOF
  94.     # write the systemd unit file
  95.     cat <<-'EOF' > ./asus-screen-brightness.service
  96.         [Unit]
  97.         Description=Allow user access to screen brightness
  98.         After=systemd-udevd.service
  99.        
  100.         [Service]
  101.         Type=oneshot
  102.         RemainAfterExit=yes
  103.         ExecStart=/usr/bin/asus-screen-brightness allowusers
  104.         ExecStop=/usr/bin/asus-screen-brightness disallowusers
  105.        
  106.         [Install]
  107.         WantedBy=multi-user.target
  108.     EOF
  109. }
  110.  
  111. package() {
  112.     cd $srcdir/$pkgname-$pkgver/
  113.     install -D -m 0755 asus-screen-brightness "$pkgdir/usr/bin/asus-screen-brightness"
  114.     install -D -m 0644 asus-screen-brightness.service "$pkgdir/usr/lib/systemd/system/asus-screen-brightness.service"
  115. }