wmstartup: Add tasks frameworking
Because one-off services are kind of hacky
This commit is contained in:
		
							
								
								
									
										14
									
								
								.config/dtfscripts/common_task.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										14
									
								
								.config/dtfscripts/common_task.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| #! /usr/bin/env bash | ||||
| # | ||||
| # wmstartup service common functions | ||||
| # Copyright (C) 2018 salt <salt@lap-th-e560-0> | ||||
| # | ||||
| # Distributed under terms of the MIT license. | ||||
| # | ||||
|  | ||||
| # Basic logging service. Do not override unless necessary | ||||
| function tsk_log() { | ||||
| 	if [ -z ${1+x} ]; then return 1; fi | ||||
| 	dtf_log "$task: $1" | ||||
| } | ||||
|  | ||||
| @@ -1,15 +0,0 @@ | ||||
| #! /usr/bin/env bash | ||||
| # | ||||
| # Simple wmstartup service | ||||
| # Copyright (C) 2018 salt <salt@lap-th-e560-0> | ||||
| # | ||||
| # Distributed under terms of the MIT license. | ||||
| # | ||||
|  | ||||
| service_name="Krunner" | ||||
| service_process="krunner" | ||||
|  | ||||
| function start() { | ||||
| 	return 0 | ||||
| } | ||||
|  | ||||
| @@ -1,22 +0,0 @@ | ||||
| #! /usr/bin/env bash | ||||
| # | ||||
| # Simple wmstartup service | ||||
| # Copyright (C) 2018 salt <salt@lap-th-e560-0> | ||||
| # | ||||
| # Distributed under terms of the MIT license. | ||||
| # | ||||
|  | ||||
| service_name="Xresources" | ||||
| service_process="xrdbupdate" | ||||
| service_kill_on_reload="false" | ||||
|  | ||||
| function start() { | ||||
| 	svc_log "Updating" | ||||
| 	$service_process $service_flags > /dev/null 2>&1 & | ||||
| 	return 0 | ||||
| } | ||||
|  | ||||
| function stop() { | ||||
| 	return 1 | ||||
| } | ||||
|  | ||||
							
								
								
									
										20
									
								
								.config/dtfscripts/tasks/kill_krunner.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										20
									
								
								.config/dtfscripts/tasks/kill_krunner.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| #! /bin/sh | ||||
| if ! pgrep krunner > /dev/null 2>&1; then | ||||
| 	exit 0 | ||||
| fi | ||||
| killall krunner | ||||
| for i in {1..100}; do | ||||
| 	if ! pgrep krunner; then break ;fi | ||||
| 	sleep 0.01 | ||||
| 	if (( $i > 99 )); then | ||||
| 		killall -9 krunner | ||||
| 	fi | ||||
| done | ||||
| if pgrep krunner > /dev/null 2>&1; then | ||||
| 	tsk_log "Failed to kill krunner" | ||||
| 	exit 1 | ||||
| else | ||||
| 	tsk_log "Killed krunner" | ||||
| 	exit 0 | ||||
| fi | ||||
|  | ||||
							
								
								
									
										80
									
								
								.config/dtfscripts/tasks/monitorset.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										80
									
								
								.config/dtfscripts/tasks/monitorset.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,80 @@ | ||||
| #! /bin/bash | ||||
| # | ||||
| # monitorset.sh | ||||
| # Copyright (C) 2018 salt <salt@dsk-cstm-0> | ||||
| # | ||||
| # Distributed under terms of the MIT license. | ||||
| # | ||||
|  | ||||
| if ! pgrep bspwm > /dev/null 2>&1; then | ||||
| 	tsk_log "Not running in bspwm" | ||||
| 	exit 1 | ||||
| fi | ||||
|  | ||||
| monitor_primary=$(xrandr -q | awk '/primary/{print $1}') | ||||
| monitors_secondary=($(xrandr -q | grep ' connected' | grep -v 'primary' | awk '{print $1}')) | ||||
|  | ||||
| desktops_total=${BSPWM_DESKTOPS_TOTAL:-8} | ||||
| desktops_primary=${BSPWM_DESKTOPS_PRIMARY:-6} | ||||
| desktops_secondary=$(($desktops_total - $desktops_primary)) | ||||
|  | ||||
| # Sanity checks | ||||
| if ! [[ "$desktops_total" -ge 1 ]]; then | ||||
| 	tsk_log "desktops_total cannot be $desktops_total, defaulting to 8" | ||||
| 	desktops_total=8 | ||||
| fi | ||||
| if ! [[ "$desktops_primary" -ge 1 ]]; then | ||||
| 	tsk_log "desktops_primary cannot be $desktops_primary, defaulting to 6" | ||||
| 	desktops_primary=6 | ||||
| fi | ||||
|  | ||||
| # Do we have enough desktops for all monitors? | ||||
| # We can correct this without defaulting, so don't error | ||||
| if [[ $desktops_secondary -lt ${#monitors_secondary[@]} ]]; then | ||||
| 	tsk_log "Configuration would leave some monitors without desktops; adding more" | ||||
| 	desktops_secondary=${#monitors_secondary[@]} | ||||
| 	desktops_total=$(($desktops_secondary + $desktops_primary)) | ||||
| 	deskpermon=1 | ||||
| 	tsk_log "Remaining desktops set to $desktops_secondary, highest desktop is now $desktops_total" | ||||
| fi | ||||
|  | ||||
| # Can we actually reach all of these desktops via keybinds? | ||||
| if [[ $desktops_total -gt 10 ]]; then | ||||
| 	tsk_log "More than ten desktops were allocated! Not all of these can be reached via keybinds!" | ||||
| 	if [[ $(($desktops_total - $desktops_secondary)) -gt 0 ]]; then | ||||
| 		tsk_log "Adjusting primary desktop reservation" | ||||
| 		desktops_primary=$(($desktops_total - $desktops_secondary)) | ||||
| 		tsk_log "Set reserved desktops to $desktops_primary" | ||||
| 	else | ||||
| 		tsk_log "Cannot resolve this situation without starving the primary monitor of desktops!" | ||||
| 		tsk_log "This may result in desktops being allocated that cannot be easily accessed!" | ||||
| 	fi | ||||
| fi | ||||
|  | ||||
|  | ||||
| # The code that actually sets the monitors up | ||||
| # First, do we actually have any secondaries? | ||||
| if [[ $(echo ${monitors_secondary:-0}) == "0" ]]; then | ||||
| 	# We only have one monitor, so give it everything | ||||
| 	layout=$(seq --separator=" " 1 $desktops_total) | ||||
| 	tsk_log "Found one monitor: $monitor_primary, so giving it layout \"$layout\"" | ||||
| 	bspc monitor $monitor_primary -d $layout | ||||
| else | ||||
| 	deskpermon=$(($desktops_secondary / ${#monitors_secondary[@]})) | ||||
| 	layout=$(seq --separator=" " 1 $desktops_primary) | ||||
| 	tsk_log "Giving primary monitor $monitor_primary layout \"$layout\"" | ||||
| 	bspc monitor $monitor_primary -d $layout | ||||
| 	unset layout | ||||
| 	tsk_log "Attempting to put $desktops_secondary desktops across ${#monitors_secondary[@]} monitors, $deskpermon each" | ||||
| 	for m in $(seq 0 $((${#monitors_secondary[@]} - 1))); do | ||||
| 		layout=$(seq --separator=" " $((1 + $m + $desktops_primary)) ${#monitors_secondary[@]} "$desktops_total") | ||||
| 		tsk_log "Giving monitor $m (${monitors_secondary[$m]}) layout \"$layout\"" | ||||
| 		bspc monitor ${monitors_secondary[$m]} -d $layout | ||||
| 	done | ||||
| fi | ||||
| unset monitor_primary | ||||
| unset monitors_secondary | ||||
| unset desktops_total | ||||
| unset desktops_primary | ||||
| unset desktops_secondary | ||||
|  | ||||
							
								
								
									
										14
									
								
								.config/dtfscripts/tasks/xrdbupdate.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										14
									
								
								.config/dtfscripts/tasks/xrdbupdate.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| #! /bin/sh | ||||
| if [ -r ~/.Xresources ]; then | ||||
| 	tsk_log "Loading .Xresources" | ||||
| 	xrdb ~/.Xresources | ||||
| fi | ||||
| for file in $XDG_CONFIG_HOME/xrdb/*.xresources; do | ||||
| 	if ! [ -r "$file" ]; then | ||||
| 		tsk_log "Could not read file \"$file\"" | ||||
| 	else | ||||
| 		tsk_log "Merging in file \"$file\"" | ||||
| 		xrdb -merge "$file" | ||||
| 	fi | ||||
| done | ||||
|  | ||||
| @@ -25,6 +25,7 @@ | ||||
| unset TERMINAL | ||||
| # Specific to these dtfscripts | ||||
| unset DTF_SERVICES | ||||
| unset DTF_TASKS | ||||
| # Polybar | ||||
| unset PB_MONITOR | ||||
| unset PB_WM_RESTACK | ||||
| @@ -64,6 +65,7 @@ fi | ||||
| ## Set some more variables | ||||
| export DTF_CONFIG_HOME="$XDG_CONFIG_HOME/dtfscripts" | ||||
| export DTF_SERVICES_DIR="$DTF_CONFIG_HOME/services" | ||||
| export DTF_TASKS_DIR="$DTF_CONFIG_HOME/tasks" | ||||
|  | ||||
| ## Load device-specific scripts | ||||
| if [[ -x "$DTF_CONFIG_HOME/devices/$(hostname).rc" ]]; then | ||||
| @@ -71,9 +73,20 @@ if [[ -x "$DTF_CONFIG_HOME/devices/$(hostname).rc" ]]; then | ||||
| 	source $HOME/.config/dtfscripts/devices/$(hostname).rc | ||||
| fi | ||||
|  | ||||
| ## Determine which tasks to run | ||||
| # If DTF_TASKS is set, use that | ||||
| # Otherwise, run everything | ||||
| if [[ "$DTF_TASKS" == "" ]]; then | ||||
| 	dtf_log "Performing all tasks" | ||||
| 	for task in $DTF_TASKS_DIR/*; do | ||||
| 		export DTF_TASKS="$DTF_TASKS $(basename "$task")" | ||||
| 	done | ||||
| else | ||||
| 	dtf_log "Performing a subset of all tasks" | ||||
| fi | ||||
|  | ||||
| ## Determine which services to run | ||||
| # If DTF_SERVICES is set, use that | ||||
| # Otherwise, populate it with every service | ||||
| # Same as above | ||||
| if [[ "$DTF_SERVICES" == "" ]]; then | ||||
| 	dtf_log "Starting all available services" | ||||
| 	for service in $DTF_SERVICES_DIR/*.dtf; do | ||||
| @@ -83,6 +96,23 @@ else | ||||
| 	dtf_log "Starting a subset of available services" | ||||
| fi | ||||
|  | ||||
| ## Run tasks | ||||
| for task in $DTF_TASKS; do | ||||
| 	task_full="$DTF_TASKS_DIR/$task" | ||||
| 	( | ||||
| 	if ! [ -r "$task_full" ]; then | ||||
| 		dtf_log "Task is unreadable or missing: $task" | ||||
| 		exit 1 | ||||
| 	fi | ||||
| 	if ! [ -x "$task_full" ]; then | ||||
| 		dtf_log "Task is unexecutable: $task" | ||||
| 		exit 1 | ||||
| 	fi | ||||
| 	source "$DTF_CONFIG_HOME/common_task.sh" | ||||
| 	source "$task_full" | ||||
| 	) | ||||
| done | ||||
|  | ||||
| ## Run startup scripts | ||||
| for service in $DTF_SERVICES; do | ||||
| 	service_full="$DTF_SERVICES_DIR/$service" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user