Dzmitry
Dzmitry

Reputation: 23

XMonad runs the script only while tapping any keyboard key

The notification is run only after I touch the touchpad or press any key. I need to run a script due to some events in the XMonad environment unconditionally.

There is a code:

batteryCheck :: IO ()
batteryCheck =
  void $
  forkIO $
  forever $ do
    safeSpawn "bash" ["-c", "~/.xmonad/check_battery.sh"]
    threadDelay (1 * 1000000)

I call batteryCheck the two following ways:

myStartupHook = do
  liftIO batteryCheck
  spawnOnce "dunst &"
  ...

or

main = do
  batteryCheck
  xmproc <- spawnPipe "xmobar ~/.config/xmobar/xmobarrc"
  xmonad $ docks defaults

The script is the following:

> cat ~/.xmonad/check_battery.sh
#!/bin/bash

LOW_BATTERY_THRESHOLD=99 # for a test purpose
NOTIFICATION_ID_FILE="/tmp/battery_notification_id"

send_notification() {
    if [ ! -f "$NOTIFICATION_ID_FILE" ]; then
        NOTIFICATION_ID=$(dunstify -u critical "Low Battery" "Battery level is ${BATTERY_LEVEL}%. Please plug in your charger." -p)
        echo $NOTIFICATION_ID > $NOTIFICATION_ID_FILE
    fi
}

dismiss_notification() {
    if [ -f "$NOTIFICATION_ID_FILE" ]; then
        NOTIFICATION_ID=$(cat $NOTIFICATION_ID_FILE)
        dunstify -C $NOTIFICATION_ID
        rm $NOTIFICATION_ID_FILE
        notify-send -u low -c "battery" -i none "Dismiss" "Battery is now charging."
    fi
}

BATTERY_STATUS=$(acpi -b | grep -o 'Discharging')
BATTERY_LEVEL=$(acpi -b | grep -P -o '[0-9]+(?=%)')

if [ "$BATTERY_STATUS" == "Discharging" ]; then
    if [ "$BATTERY_LEVEL" -le "$LOW_BATTERY_THRESHOLD" ]; then
        send_notification
    fi
else
    dismiss_notification
fi

How to make it to run the script unconditionally?

Upvotes: 2

Views: 52

Answers (0)

Related Questions