0

I have a laptop with Void-Linux and Wayland desktop environment (sway). I'm trying to bind laptop's open/close event to custom command, but I can't scan key events here. I tried it with showkey --scancodes and showkey --keycodes from console outside Wayland environment, but it shows nothing when I open or close laptop. How to correctly scan open/close events or bind it in Wayland?

PS: I don't really want to install some complex tools like laptop-mode tools (if it's possible), I just need to run very primitive bash scripts on open/close.

g4s8
  • 438
  • 6
  • 18
  • Does this help? [https://unix.stackexchange.com/questions/148197/determine-status-of-laptop-lid](https://unix.stackexchange.com/questions/148197/determine-status-of-laptop-lid) – Peregrino69 Oct 05 '21 at 11:10
  • @Peregrino69 thanks, it could be used to check current status in a `while` loop if I don't find a way to receive events about state changes – g4s8 Oct 05 '21 at 11:16

1 Answers1

0

As a workaround, I created a script based on @peregrino69 comment: it's reading LID state from /proc/acpi/button/lid/LID0/state every second and executes a callback function if state changed:

#!/bin/bash

_state="open"

function on_state_open {
  # run commands on open
}

function on_state_close {
  # run commands on close
}

function on_state_change {
  local update="$1"
  local change=false
  if [[ "${_state}" != "$update" ]]; then
    change=true
  fi
  _state="$update"
  if $change; then
    case "${_state}" in
      "open")
        on_state_open
        ;;
      "closed")
        on_state_close
        ;;
    esac
  fi
}

while true; do
  snapshot=$(cat /proc/acpi/button/lid/LID0/state | awk -d' ' '{print $2}')
  on_state_change "$snapshot"
  sleep 1
done
g4s8
  • 438
  • 6
  • 18