Created
November 15, 2018 08:14
-
-
Save crabdancing/cc1fb8aa5ef9e55d89bae28f13a10444 to your computer and use it in GitHub Desktop.
ACPID handler script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Copyleft (C) Alexandria Pettit GNU GPLv3 | |
# This is a little utility designed for Dell E6*** series laptops, with setups that use 'slock'. | |
# Its use case is that specific because I designed it specifically for my laptop. | |
# So this is really more of a personal dotfile thing than a program. | |
use strict; | |
use warnings; | |
# We need the 'say' subroutine | |
use feature qw(say); | |
# Dump the argument list recieved from acpid, | |
# for debugging purposes. | |
my $args_strlist = join('; ', @ARGV); | |
say("STATE CHANGE: $args_strlist"); | |
# stores file path for getting lid state | |
my $lidstate_file = '/proc/acpi/button/lid/LID/state'; | |
# stores state string retrieved from lid state file | |
my $state_string; | |
# stores file handle for getting lid state | |
my $lidstate_fh; | |
# We need the DISPLAY variable set so that | |
# our GUI programs won't freak | |
$ENV{DISPLAY} = ':0'; | |
# Stores state to put the screen in | |
# 1 for on, 0 for off. | |
# -1 is invalid and lets us know this wasn't set by a function | |
my $screen_state = -1; | |
sub open_lidstate_file { | |
open($lidstate_fh, '<', $lidstate_file) | |
or die "Error opening $lidstate_file"; | |
} | |
### Detect if lid is closed or not. Pretty simple. | |
sub is_lid_open { | |
# Go to start of file | |
sysseek($lidstate_fh, 0, 0); | |
# Format is "state: open" or | |
# "state: closed" | |
# must remove first 12 chars | |
$state_string = substr(<$lidstate_fh>, 12); | |
# Remove newline from end of string | |
chomp $state_string; | |
# Translate state to boolean values | |
if ($state_string eq 'open') { | |
return 1; | |
} elsif ($state_string eq 'closed') { | |
return 0; } | |
else { die "Failed to parse state from $lidstate_file"; } | |
} | |
### Sets screen state according to $screen_state | |
# 0 = off, 1 = on | |
sub update_screen_state { | |
# We need the DISPLAY variable set so that `xset` doesn't freak | |
$ENV{DISPLAY} = ':0'; | |
if ($screen_state == 1) { | |
say('Ensuring screen is on...'); | |
system('sudo -u a xset dpms force on'); | |
} | |
elsif ($screen_state == 0) { | |
say('Ensuring screen is off...'); | |
system('sudo -u a xset dpms force off'); | |
} | |
else { | |
say('Screen state is being left unchanged.'); | |
} | |
} | |
# HACKY FIX ALERT | |
# My current laptop has a bug that causes the backlight | |
# to turn on if you close the lid and then (un|)plug the power. | |
# so let's fix that | |
# If we got an AC adapter state update... | |
if ($ARGV[0] eq 'ac_adapter') { | |
# Get open file handle to get lidstate | |
open_lidstate_file(); | |
# If lid is not open... | |
if (not is_lid_open()) { | |
# Turn off the screen. | |
$screen_state = 0; | |
} | |
} | |
# Here is where we lock screen when lid is closed | |
# and ensure screen state changes in accorance with lid position. | |
if ($ARGV[0] eq 'button/lid' and $ARGV[1] eq 'LID') { | |
if($ARGV[2] eq 'close') { | |
say('Locking machine!'); | |
system('systemctl start slock'); | |
$screen_state = 0; | |
} elsif($ARGV[2] eq 'open') { | |
$screen_state = 1; | |
} | |
} | |
# update screen state to match $screen_state | |
update_screen_state(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment