summaryrefslogtreecommitdiffstats
path: root/.local/bin/dmhc
blob: 2db5064cb683393c9251726cc7584a7a73c629bd (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
#!/usr/bin/env perl

use strict;
use warnings;
use POSIX qw(setsid);

use constant PIDFILE => '/tmp/halfcab.pid';


my @keys = (
	'warm',
	'sex',
	'purp',
	'blues',
	'burn',
	'white',
	'off',
	'scr',
);

my @values = (
	'hc 214 90 10',
	'hc 255 0 0',
	'hc 255 0 255',
	'hc 150 150 255',
	'hc 214 50 0',
	'hc 255 200 150',
	'hc 0 0 0',
	'',
);

my %list;
@list{@keys} = @values;

sub dmhc
{
	my $choice;
	my $url;
	my $pid;
	my $prefix;
	my $list;
	my $color;
	my $read_pid;

	$color = '#cc241d';
	if (@ARGV == 1) {
		$color = $ARGV[0];
	}
	$list = join("\n", @keys);
	$choice = `printf "$list" | dmenu -i -sb '$color' -shb '$color'`;
	chomp($choice);
	return unless exists $list{$choice};
	if (-e PIDFILE) {
		open(my $fh, '<', PIDFILE);
		$read_pid = <$fh>;
		chomp($read_pid);
		close($fh);
		kill('TERM', $read_pid);
		while (kill(0, $read_pid) != 0) {
			sleep(0.05);
		}
		unlink(PIDFILE);
	}
	$pid = fork();
	if ($pid > 0 && $choice eq 'scr') {
		open(my $fh, '>', PIDFILE) or die $!;
		print $fh $pid;
		close($fh);
	} elsif (not $pid) {
		exec($list{$choice}) if $choice ne 'scr';
		setsid();
		my $grep = system(
			'xrandr | command grep -E "DP-4.+\+1920" >/dev/null 2>&1'
		);
		exec('hc 1920') if not $grep;
		exec('hc');
	}
	return;
}

dmhc();

__END__