#!/usr/bin/env perl use strict; use warnings; use Sys::Hostname; use constant OS => `uname | tr -d '\n'`; use constant { SLOCK_PATH => 'slock', XSCREENSAVER_PATH => 'xscreensaver-command', ACPI_PATH => '/usr/sbin/acpiconf', KILL_PATH => '/usr/bin/killall', SHUTDOWN_PATH => '/sbin/shutdown', LOGINCTL_PATH => '/usr/bin/loginctl', NOTIF_PATH => 'notify-send', HOSTNAME => (split /\./, hostname())[0] }; use constant LIST => [ 'lock screen', 'sleep', 'logout', 'reboot', 'shutdown' ]; use constant CONFIRM => [ "no", "yes" ]; sub confirm { my ($var, $color) = @_; my $list; my $choice; for (@{+CONFIRM}) { $list .= $_ . "\n"; } $choice = `printf "%s" "$list" | \ dmenu -i -p "$var?" -sb '$color' -shb '$color'`; chomp $choice; if ($choice eq ${+CONFIRM}[1]) { return (1); } return (0); } sub action { my ($var, $color) = @_; my $pid; chomp $var; if ($var eq ${+LIST}[0]) { exec(XSCREENSAVER_PATH, '-lock'); } elsif ($var eq ${+LIST}[1]) { $pid = fork(); if (not $pid) { exec(XSCREENSAVER_PATH, '-lock'); } else { system( NOTIF_PATH, '-u', 'normal', 'shutdown', ' g\'night, partner!' ); sleep 2; if (HOSTNAME eq 'mother') { exec(LOGINCTL_PATH, 'suspend'); } elsif (HOSTNAME eq 'po-rbo') { exec('systemctl', 'suspend'); } exec(ACPI_PATH, '-s', '3'); } } elsif ($var eq ${+LIST}[2]) { if (confirm($var, $color) == 1) { if (HOSTNAME eq 'mother') { exec(LOGINCTL_PATH, 'terminate-session', 'self'); } exec(KILL_PATH, 'dwm'); } } elsif ($var eq ${+LIST}[3]) { if (confirm($var, $color) == 1) { if (HOSTNAME eq 'mother') { exec(LOGINCTL_PATH, 'reboot'); } elsif (HOSTNAME eq 'po-rbo') { exec('systemctl', 'reboot'); } system( NOTIF_PATH, '-u', 'normal', 'shutdown', ' rebootin\', partner!' ); exec(SHUTDOWN_PATH, '-r', 'now'); } } elsif ($var eq ${+LIST}[4]) { if (confirm($var, $color) == 1) { system( NOTIF_PATH, '-u', 'normal', 'shutdown', ' farewell, partner!' ); sleep 2; if (HOSTNAME eq 'mother') { exec(LOGINCTL_PATH, 'poweroff'); } elsif (HOSTNAME eq 'po-rbo') { exec('systemctl', 'poweroff'); } exec(SHUTDOWN_PATH, '-p', 'now'); } } return; } sub main { my $list; my $choice; my $color; $color = '#cc241d'; if (@ARGV == 1) { $color = $ARGV[0]; } for (@{+LIST}) { $list .= $_ . "\n"; } $choice = `printf "%s" "$list" | dmenu -i -sb '$color' -shb '$color'`; action($choice, $color); return (0); } main(); __END__