acpi-handler.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/slua
  2. -- acpi olay yönetim betiği
  3. -- milisarge 2021
  4. function os.capture(cmd, raw)
  5. local f = assert(io.popen(cmd, 'r'))
  6. local s = assert(f:read('*a'))
  7. f:close()
  8. if raw then return s end
  9. s = string.gsub(s, '^%s+', '')
  10. s = string.gsub(s, '%s+$', '')
  11. s = string.gsub(s, '[\n\r]+', ' ')
  12. return s
  13. end
  14. p1=""
  15. p2=""
  16. p3=""
  17. if arg[1] ~= nil then p1=arg[1] end
  18. if arg[2] ~= nil then p2=arg[2] end
  19. if arg[3] ~= nil then p3=arg[3] end
  20. local mode=""
  21. local display=os.capture("wlopm | awk '{print $1}'| head -n 1")
  22. local lock_cmd="swaylock -f -c 000000"
  23. local suspend_ram_cmd="echo -n mem | tee /sys/power/state"
  24. local bs_cmd="wlopm --toggle "..display
  25. local notify_cmd="notify-send"
  26. modes={
  27. lid={
  28. open="lock",
  29. close="suspend_ram"
  30. },
  31. sleep="blackscreen",
  32. headphone={
  33. plug="notify",
  34. unplug="notify",
  35. }
  36. }
  37. functions={
  38. suspend_ram=function() os.execute(suspend_ram_cmd) end,
  39. hibernate=function() print("--- hibernate") end,
  40. blackscreen=function() os.execute(bs_cmd) end,
  41. nothing=function() print("--- nothing") end,
  42. lock=function() os.execute(lock_cmd) end,
  43. notify=function(title, msg) os.execute(notify_cmd.." -a "..title.." "..msg) end,
  44. }
  45. if p1 and p2 and p3 then
  46. if p1 == "button/volumeup" and p2 == "VOLUP" then
  47. print("--- volumeup triggered")
  48. elseif p1 == "button/volumedown" and p2 == "VOLDN" then
  49. print("--- volumedown triggered")
  50. elseif p1 == "button/sleep" and (p2 == "SBTN" or p2 == "SLBP") then
  51. print("---",p1,p2)
  52. mode=modes.sleep
  53. functions[mode]()
  54. elseif p1 == "button/lid" and p2 == "LID" then
  55. if p3 == "open" then
  56. mode=modes.lid.open
  57. functions[mode]()
  58. elseif p3 == "close" then
  59. mode=modes.lid.close
  60. functions[mode]()
  61. else
  62. print(p1,p2,p3,"not implemented")
  63. end
  64. elseif p1 == "jack/headphone" and p2 == "HEADPHONE" then
  65. if p3 == "plug" then
  66. mode=modes.headphone.plug
  67. functions[mode]("Kulaklık","takıldı")
  68. elseif p3 == "unplug" then
  69. mode=modes.headphone.unplug
  70. functions[mode]("Kulaklık","çıkarıldı")
  71. else
  72. print(p1,p2,p3,"not implemented")
  73. end
  74. end
  75. else
  76. print(p1,p2,"not implemented")
  77. end