summaryrefslogtreecommitdiffstats
path: root/.local/bin/dmlog
blob: 8d70ca5668de8163daa300b6f5d3b1be7d10dc95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/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())
};
use constant LIST => [
	'lock screen',
	'sleep',
	'logout',
	'reboot',
	'shutdown'
	];
use constant CONFIRM => [
	"no",
	"yes"
	];

sub confirm
{
	my ($var) = @_;
	my $list;
	my $choice;

	for (@{+CONFIRM}) {
		$list .= $_ . "\n";
	}
	$choice = `printf "%s" "$list" | dmenu -i -p "$var?" -m 0`;
	chomp $choice;
	if ($choice eq ${+CONFIRM}[1]) {
		return (1);
	}
	return (0);
}

sub action
{
	my ($var) = @_;
	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) == 1) {
			if (HOSTNAME eq 'mother') {
				exec(LOGINCTL_PATH, 'terminate-session', 'self');
			}
			exec(KILL_PATH, 'dwm');
		}
	}
	elsif ($var eq ${+LIST}[3]) {
		if (confirm($var) == 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) == 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;

	for (@{+LIST}) {
		$list .= $_ . "\n";
	}
	$choice = `printf "%s" "$list" | dmenu -i -m 0`;
	action($choice);
	return (0);
}

main();

__END__