#!/usr/local/bin/perl use strict; use warnings; use Capture::Tiny qw(capture); use constant { SXHKD_PATH => '/usr/local/bin/sxhkd', PGREP_PATH => '/bin/pgrep', PKILL_PATH => '/bin/pkill', XRANDR_PATH => '/usr/local/bin/xrandr', MAINSCREEN_PATH => '/usr/home/jozan/.local/bin/mainscreen', DUALSCREEN_PATH => '/usr/home/jozan/.local/bin/dualscreen', TRIPLESCREEN_PATH => '/usr/home/jozan/.local/bin/triplescreen', BSPC_PATH => '/usr/local/bin/bspc', COMPTON_PATH => '/usr/local/bin/compton', FEH_PATH => '/usr/local/bin/feh', XSET_PATH => '/usr/local/bin/xset', SETXKBMAP_PATH => '/usr/local/bin/setxkbmap', DUNST_PATH => '/usr/local/bin/dunst', LOWBAT_PATH => '/usr/local/bin/lowbat', EMACS_PATH => '/usr/local/bin/emacs', ALACRITTY_PATH => '/usr/local/bin/alacritty', TERMS_THREAD => '/usr/home/jozan/.config/bspwm/terms_thread.pl', NETWORK_THREAD => '/usr/home/jozan/.config/bspwm/network_thread.pl' }; use constant { BSP_BORDER_WIDTH => 1, BSP_WINDOW_GAP => 0, BSP_BORDER_COLOR => '#b92121', BSP_SPLIT_RATIO => '0.50', BSP_BORDERLESS_MONOCLE => 'true', BSP_GAPLESS_MONOCLE => 'true', BSP_SINGLE_MONOCLE => 'true', XSET_R_RATE_DELAY => 200, XSET_R_RATE_RATE => 100, }; use constant WALLPAPER_PATH => '/usr/home/jozan/Pictures/wallpaper.jpg'; sub run_if_dead { my @argv = @_; my $bin; my $pid; $bin = $argv[0]; $bin =~ s/.+\///g; my (undef, undef, $retval) = capture { system( PGREP_PATH, $bin ); }; $retval = ($retval >> 8) & 0xff; if ($retval != 0) { $pid = fork(); if (not $pid) { exec(@argv); exit; } } return; } sub kill_some { system(PKILL_PATH, 'alacritty'); return; } sub enable_screens { my $stdout; my $screens; $screens = 1; ($stdout, undef, undef) = capture { system(XRANDR_PATH); }; if ($stdout =~ m/HDMI1 connected/ && $stdout =~ m/VGA1 connected/ == 1) { $screens = 3; system(TRIPLESCREEN_PATH); } elsif ($stdout =~ m/DP1 connected/) { $screens = 2; system(DUALSCREEN_PATH); } else { system(MAINSCREEN_PATH); } return $screens; } sub bspc_configs { system(BSPC_PATH, 'config', 'border_width', BSP_BORDER_WIDTH); system(BSPC_PATH, 'config', 'window_gap', BSP_WINDOW_GAP); system(BSPC_PATH, 'config', 'focused_border_color', BSP_BORDER_COLOR); system(BSPC_PATH, 'config', 'split_ratio', BSP_SPLIT_RATIO); system(BSPC_PATH, 'config', 'borderless_monocle', BSP_BORDERLESS_MONOCLE); system(BSPC_PATH, 'config', 'gapless_monocle', BSP_GAPLESS_MONOCLE); system(BSPC_PATH, 'config', 'single_monocle', BSP_SINGLE_MONOCLE); return; } sub bspc_rules { system(BSPC_PATH, 'rule', '-a', 'Emacs', 'state=tiled'); system(BSPC_PATH, 'rule', '-a', 'Firefox', 'desktop=08', 'follow=true'); system(BSPC_PATH, 'rule', '-a', 'Wine', 'desktop=07', 'state=floating'); system(BSPC_PATH, 'rule', '-a', 'Dunst', 'layer=above'); system(BSPC_PATH, 'rule', '-a', 'Zathura', 'state=tiled'); system(BSPC_PATH, 'rule', '-a', 'qTox', 'desktop=12'); system(BSPC_PATH, 'rule', '-a', 'DergodsRealmII', 'state=floating'); system(BSPC_PATH, 'rule', '-a', 'obs', 'desktop=12', 'follow=true'); return; } sub run_bg_programs { my ($screens) = @_; run_if_dead(SXHKD_PATH); run_if_dead(COMPTON_PATH); run_if_dead(DUNST_PATH); run_if_dead(LOWBAT_PATH); if ($screens > 1) { system(FEH_PATH, '--bg-fill', WALLPAPER_PATH, '--bg-fill', WALLPAPER_PATH); } else { system(FEH_PATH, '--bg-fill', WALLPAPER_PATH); } system(XSET_PATH, 'r', 'rate', XSET_R_RATE_DELAY, XSET_R_RATE_RATE); system(SETXKBMAP_PATH, '-layout', 'us,fr', '-option', 'grp:alt_shift_toggle'); return; } sub run_fg_programs { my ($screens) = @_; my $pid; $pid = fork(); if (not $pid) { exec(TERMS_THREAD, $screens); exit; } sleep(14); run_if_dead(EMACS_PATH); return; } sub run_network_programs { my $pid; $pid = fork(); if (not $pid) { exec(NETWORK_THREAD); exit; } return; } sub main { my $screens; kill_some(); $screens = enable_screens(); bspc_configs(); bspc_rules(); run_bg_programs($screens); run_fg_programs($screens); run_network_programs(); return; } main(); __END__