diff --git a/herbstluftwm/autostart b/herbstluftwm/autostart index daf70af..0117248 100755 --- a/herbstluftwm/autostart +++ b/herbstluftwm/autostart @@ -48,7 +48,7 @@ hc keybind $Mod-q close #spawn shit hc keybind $Mod-Return spawn urxvt -hc keybind $Mod-Shift-Return spawn xterm /bin/bash +hc keybind $Mod-Shift-Return spawn ~/.config/herbstluftwm/cwd_helper.py hc keybind $Mod-i spawn urxvt -e nohup zsh -c "chromium &" #need to reliably use the zshconf chromium if [[ $HOST =~ atlantis* ]]; then hc keybind $Mod-l spawn i3lock -i ~/.config/i3lock/bg.png -t diff --git a/herbstluftwm/cwd_helper.py b/herbstluftwm/cwd_helper.py new file mode 100755 index 0000000..917629b --- /dev/null +++ b/herbstluftwm/cwd_helper.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 + +import subprocess +import datetime +import sys +import os +import shlex +import psutil + +def error(s): + with open("herbstlog",'a') as f: + time = str(datetime.datetime.now().time())[:-7] #cut seconds at the end + f.write(time + "ERROR" + os.path.basename(__file__) + s) + +out = subprocess.check_output(['xdpyinfo']).decode().split('\n') + +############ GET FOCUSED WINDOW ID ############## +window = -1 +for l in out: + if l.startswith('focus:'): + for field in l.split(' '): + field = field.rstrip(',') + if(field.startswith('0x')): + window = int(field,16) + if window == -1: + error("Failed to get window_focus\n") +if window==-1: + sys.exit() + +############ IF URXVT GET PID ########### +pid = -1 +out = subprocess.check_output(shlex.split('xprop -id '+hex(window))).decode().split('\n') +for l in out: + if l.startswith('WM_CLASS(STRING)') and 'urxvt' in l: + break; +else: + sys.exit(); + +for l in out: + if l.startswith('_NET_WM_PID(CARDINAL)'): + pid = int(l.split(' ')[-1]) +if pid==-1: + error("Failed to get PID") + +process = psutil.Process(pid) +for p in process.children(): #recursive=false + if p.name()=='zsh' or p.name()=='bash': + subprocess.Popen(shlex.split('urxvt -cd ' + p.cwd())) + break +sys.exit() diff --git a/herbstluftwm/cwd_spawn.sh b/herbstluftwm/cwd_spawn.sh deleted file mode 100644 index b485484..0000000 --- a/herbstluftwm/cwd_spawn.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -ID=$(xdpyinfo | grep focus | cut -f4 -d " ") -PID=$(($(xprop -id $ID | grep -m 1 PID | cut -d " " -f 3) + 2)) -if [ -e "/proc/$PID/cwd" ] -then - urxvt -cd $(readlink /proc/$PID/cwd) & -else - urxvt -fi -