3

I have the following script (in my PATH):

#!/usr/bin/env bash

main()
{
   local state=$(sudo rfkill list wifi -n -o SOFT)
   if [[ $state == 'blocked' ]]
   then
       sudo rfkill unblock wifi
       state='Unblocked'
   else
       sudo rfkill block wifi
       state='Blocked'
   fi

   notify-send 'Wi-Fi' "$state"
   exit 0
}

main $@

Running the script from the command line works as expected, then I add the following shortcut to my.xbindkeysrc:

"kill-wifi"
     XF86WLAN

But the notifications, and the Wi-Fi interface get stuck in one of the two states, blocked or unblocked, it doesn't toggle. Sometimes, if I press several times the XF86WLAN key, I get a toggle.

The weird thing is that using another key to trigger the script, such as F8, the whole thing works fine, but I want to leave F8 for purposes other than toggling the WiFi.

So one of my guesses was that there's "something" binding the XF86LAN KeySym that messes up when my script runs. But then commenting out the command that actually kills the WiFi interface, produces the right notifications (but I'm not actually doing anything useful).

Any pointers would be appreciated.

Bobby Wan-Kenobi
  • 885
  • 9
  • 18
  • Tangentially, not quoting `"$@"` is a bug, though it's probably completely unrelated to your problem. – tripleee Jul 12 '20 at 18:16
  • Exactly, the script is not even receiving any arguments whatsoever – Bobby Wan-Kenobi Jul 12 '20 at 18:21
  • Apparently my question has received 2 votes to close, hmm, I wonder what's that guideline I'm failing to fulfill. – Bobby Wan-Kenobi Jul 12 '20 at 20:36
  • The close voters seem to think this belongs on [su]. I'm thinking maybe too broad or needs debugging details, but mostly just fine but hard to answer without a lot of additional effort. – tripleee Jul 13 '20 at 05:04

1 Answers1

0

Aaaanyways, for anyone in my same situation, install urfkill which automatically listen for the XF86WLAN event. Then in your script, simply produce the notification:

#!/usr/bin/env bash

main()
{
   local state=$(sudo rfkill list wifi -n -o SOFT)
   notify-send "Wi-Fi" "$state"
}
main

Note that the script uses another utility named rfkill only for getting the state of the Wi-Fi interface, and spit it in the notification.

Finally in your .xbindkeysrc:

"kill-wifi"
    XF86WLAN

You could be using other hotkeys daemon like sxhkd or even the configuration file of i3 or sway, in any case your shell script only is used for notify the state of the Wifi interface, the real work of toggling the Wifi on and off is done by urfkill.

The good news is that now that button with the little antena symbol on your laptop actually toggles on/off your Wi-Fi card and you still have free to use the underlying Function key (F8 in my case) ;-)

P.S. If anyone reads this and knows why my first approach was failing (race condition or whatever), please feel free to let me know how to solve it.

Bobby Wan-Kenobi
  • 885
  • 9
  • 18