GithubHelp home page GithubHelp logo

liupold / pidswallow Goto Github PK

View Code? Open in Web Editor NEW
42.0 2.0 0.0 188 KB

A swallower script using process hierarchy.

License: Apache License 2.0

Shell 100.00%
bspwm window swallowing pstree wm process i3wm xdotool wms de

pidswallow's Introduction

A stupid simple swallower 😉.

(Based on Process hierarchy)

Super easy to config. Uses xdo WM/DE independent.

PKGBUILD-STATUS SHELLCHECK-STATUS license

Features

  • Based on process hierarchy (don't care about window focus).
  • cli like options. (super easy to use within scripts).
  • Window Managers and Desktop Environment Independent.
pidswallow (pid swallow WM/DE independent)
Hides terminal window automatically, so that you don't have to

pidswallow [OPTION ...]

OPTIONS:
        -h  --help              Show this message
        -s  --swallow <CWID>    Hides parent window of the given child window id.
        -v  --vomit <CWID>      Unhides parent window of the given child window id.
        -t  --toggle <CWID>     toggle between swallow and vomit. (default)
        -g  --glue              treat if parent and child window are same. (recommended)
        -l  --loop              listen and hide / unhide window on launch / remove.
        -V  --verbose           Shows useful information.

bugs/issues: https://github.com/liupold/pidswallow.
  • POSIX compliant shell script.
  • Just pass in the window id of the swallower.
  • Work on a toggle mode. (swallow if not swallowed else vomit).
  • Super fast. (Really!) (28.0ms). (using dash).
Benchmark #1: pidswallow -s 0x02A00003
  Time (mean ± σ):      28.0 ms ±   4.3 ms    [User: 13.8 ms, System: 18.0 ms]
  Range (min … max):    24.1 ms …  48.0 ms    74 runs

Demo

Demo

How it works

takes wid as as arg --> gets process tree --> check blacklist --> hide parent.

Dependencies

  1. xdo
  2. xprop (--loop and --glue).
  3. xev (--glue).
  4. xdotool (cross-workspace --glue, optional).

Installation

Using AUR

  • stable release. (Currently on 2.0)
yay -S pidswallow
  • dev (git)
yay -S pidswallow-dev-git

Manual

Add pidswallow to your path.

Autostart

  1. Launch when WM/DE starts (Example: .xinitrc, i3-config, bspwrc)
pgrep -fl 'pidswallow -gl' || pidswallow -gl
  1. Add the following to your bashrc, zshrc or shell init script. This step isn't strictly necessary, but it fixes problems with daemon-based terminals and also improves the performance of pidswallow. Note: You might want to skip this step if you use Alacritty (see Known Issues). Note: Your window manager has to focus new terminals as soon as they are spawned for this to work correctly.
[ -n "$DISPLAY" ]  && command -v xdo >/dev/null 2>&1 && xdo id > /tmp/term-wid-"$$"
trap "( rm -f /tmp/term-wid-"$$" )" EXIT HUP
  1. Restart wm and terminals.

Additional Configuration

Environment variables can be exported to change the behavior of pidswallow.

The following ones accept lists of space separated process names.

  • PIDSWALLOW_SHELL: your shell(s) (e.g bash). Default: Taken from $SHELL
  • PIDSWALLOW_TERMINAL: your terminal(s), as fallback if shell is not used (and if terminal supports it). Default: $TERMINAL
  • PIDSWALLOW_BLACKLIST: parent cannot be swallowed, (if you launch one term from another you might want to add it to blacklist). Default: same as $PIDSWALLOW_TERMINAL
  • PIDSWALLOW_GLUE_BLACKLIST: not touched by --glue. Default: empty

The ones following are executed in a subshell (/bin/sh) and support the special strings $pwid and $cwid, holding the parent and child window IDs, respectively.

  • PIDSWALLOW_SWALLOW_COMMAND: used to swallow (hide) windows. Default: xdo hide $pwid
  • PIDSWALLOW_VOMIT_COMMAND: used to vomit (unhide) windows. Default: xdo show $pwid
  • PIDSWALLOW_PREGLUE_HOOK: executed before swallowing new child window when using --glue. Useful for floating windows in tiled WMs. Default: empty

Tested on

(If you did please let me know, If it dosent work create a issue).

Known Issues

Tricks

Manual swallow (toggle)

  1. Add pidswallow to your path.
  2. run this and click on the child window (not the term) to swallow.
 xwininfo | awk '/Window id:/{print $4}' | pidswallow -gt
  1. or pass the window-id via keyboard shortcut. (Eg: sxhkd toggle).
super + v
    xdo id | pidswallow -gt

Launch a program from term wihout being swallowed.

setsid -f <command>  # this will not swallow the terminal.

WM specific recommendations.

bspwm

Add each set of lines to your bspwmrc, right before running pidswallow.

  • Let bspwm handle window hiding.
export PIDSWALLOW_SWALLOW_COMMAND='bspc node $pwid --flag hidden=on'
export PIDSWALLOW_VOMIT_COMMAND='bspc node $pwid --flag hidden=off'

This way bspwm will remember window positions and won't lose track of swallowed windows.

  • Follow floating state of parent (when using --glue).
export PIDSWALLOW_PREGLUE_HOOK='bspc query -N -n $pwid.floating >/dev/null && bspc node $cwid --state floating'

Check if parent window state is floating and apply the same to the child if that's the case. This example should work in most cases, but feel free to add more complex hooks to your setup. (e.g. to mimic more properties of the parent).

pidswallow's People

Contributors

liupold avatar seerlite avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pidswallow's Issues

Swallow stops after closing a window

Using xdo close on a seperate window from the windows currently being swallowed in bspwm on the same workspace as a swallowed window, causes the swallow effect to stop.

Unable to swallow Konsole on Plasma

$ pstree -ATlsp $$
systemd(1)---plasmashell(4588)---konsole(39639)---zsh(39645)---pstree(39710)
$ xdotool search --pid 39639                                    
73400321
73400323
73400362
73400382
71303175

For some reason, even though there's only 1 konsole instance with 1 konsole window, multiple WIDs are output (from the same PID!)
This is really confusing me. I'll check it out later.

Testing plasma also led me to find out about another issue that seems to happen even on the master branch nope, only DEs, so I'll check that first. Edit: Fixed that one

Plasma menu may be handled incorrectly in specific instances

First of all, a terminal has to be started from the plasma menu.
After that, when opening the plasma menu (making pidswallow check if it's a swallowable), if the first line of the pstree output (the line we're parsing) has a swallowable like konsole and the plasmashell, the plasma menu is gonna be handled incorrectly and moved to the top left corner of the screen.

Sorry for the rushed explanation. I'm coming back to this later to clarify exactly what's going on.

Jumping desktop focus.

Focus is jumping from one desktop to another during toggle.

Steps to reproduce:

  • Open a window at any given desktop(say x) (from terminal)
  • Move the window to other desktop.(say y)
  • Close the window (at y). Focus should stay at y, but moving to x.

Daemon Based Terminal Will not work

A lot of the terms are dbus based, (This will not work correctly)

  • urxvtd (daemon #17)
  • xfce-terminal
  • gnome-termiinal

We probably need to maintain a list of this,

Instructions for Xfce Terminal don't work globally.

Opening the terminal from somewhere other than the application finder (e.g. through exo-open, by defining the Xfce Terminal as a "default application") won't use the --disable-server flag. I'm not sure how to set it up.

xprop -spy doesn't detect window changes

xprop -spy only outputs lines when windows are focused. I'm not sure if this is intended.
I hadn't noticed this before because I have a special keybind that does something every once in a while that also triggers xprop -spy.

This is a problem in bspwm, for example, where moving the window around won't update the position and resizing won't update the size.

Edit: to reproduce, have a floating terminal and move it around with super + mouse (assuming that config hasn't been changed). Launching anything from that terminal will use the old position and not the new one.

Edit: I just realized it's kind of program-dependant. For instance, I'm testing Alacritty.

Sub-windows re-swallowing an alraedy swallowed parent

It happens because our only check for a window already being swallowed is this line, which only checks if the current child window (cwid) has any window swallowed.

In the case of sub-windows (e.g. when a program creates a pop-up window), the child window will be different to the one that originally swallowed. This causes pidswallow to think it's our first time swallowing it and proceeds to do it again.

This isn't noticeable with default configuration because xdotool unmapwindow will just fail and not do anything. However, when using the recommended bspwm-specific configuration, bspc node {%pwid} --flag hidden=on has a very different result when {%pwid} is left as blank.

Right now it's late for me so this looks like a hard thing to solve, maybe even requiring some changes to how the /tmp files are handled. But I might wake up tomorrow and realize it's some easy check.

Allow flags in any order

So, for example pidswallow -Vgl has the same behavior as pidswallow -lgV. This has the drawback of only allowing 1 action per invocation, but I think that's how it should work.
I already have something kind of prepared for this but I'm not sure how to handle return values.

Thoughts on this?

Issue with dmenu

There is probably some sort of work-around for this, but I think it's worth bringing up.

If I open dmenu to open something like firefox while the current focused window is a terminal that is whitelisted (in my case, st), the terminal swallows the program despite being launched from dmenu instead of from the terminal.

Is there a workaround for this? Thanks

EDIT: Oops, I'm commenting on the wrong project but since I'm here, I'll try this out too :)

Configuration for re-focusing parent window when vomiting child

What do you think of adding a configuration option for this? I personally like the parent to always regain focus.
Another idea I have is to monitor the child window focus. And only if it was focused when it closed, re-focus the parent. This feels like overkill though.

We shouldn't rely on pstree output to get parent processes

I was messing around again with pstree to understand what sed did in the new changes, and this is an example output I got:

$ pstree -ATsp "$$" | sed -e 's|(|:|g' -e 's|)||g'
systemd:1---systemd:709---alacritty:2734---zsh:2742-+-pstree:5774
                                                        `-sed:5775

I felt like this could break the script, so I started looking around and found this stackoverflow answer that recommends not parsing pstree. This leads me back to the conclusion that a better solution is to use the /proc filesystem once again.
This has the performance problem though, because we have to parse /proc/<...>/status for each and every parent until coming across a swallowable.
So right now I'm looking for an alternative to pstree that's reliable enough for scripting and know will work 100% of the time.

I know, maybe a scenario like the output I got above will never really happen, but I feel like being nitpicky and really want a solution that always works ;) hehe

Doesn't work if sxhkd is run as a systemd service.

Hi!
I'm using sxhkd as a systemd user service. I've made a small modification to the unit too (KillMode=process).

The thing is, this makes systemd the "parent" process. Running pidswallow with verbose=1 on an Alacritty window gives this output:

systemd(724)---alacritty(25793)---zsh(25799)

Thus, the script doesn't work.
I tried messing around with the script for a bit, but I don't know much about process handling and the variable names just confused me more. So I gave up and made an issue.

Is there a way to work around this?

Edit: I'm honestly not sure if systemd has anything to do with it. I'm just assuming here (sorry if I'm wrong), since using a simpler sxhkd & inside bspwmrc doesn't cause any problems.

No such file or directory

Hi

First of all, thanks a lot for the great work. Unfortunately I can't make it work. What am I missing?
I have the latest version. I am on Arch/bspwm and I use Alacritty.

I added alacritty to this line in pidswallow
swallowable=" $TERMINAL alacritty urxvt kitty "

I added that at the bottom of bspwmrc
bspc subscribe node_add node_remove \ | grep -o --line-buffered '0x[0-9A-F]\+$' | xargs -n1 pidswallow &

In Alacritty I get

sxiv Pictures/wallpapers-stray.jpg !9984 No such user name: found. /home/ioio85/.local/bin/pidswallow: line 38: pls report this issue to https://github.com/liupold/pidswallow: No such file or directory

In urxvt it does not work either but I don't get any error.

Thanks for your help

Shell-based swallowing doesn't always work correctly in Alacritty

Conversation started here

Happens when the shell init is sourced quickly, before the terminal window has appeared: xdo id won't catch the terminal as it doesn't exist yet.

This could be worked around when/if #2817 on Alacritty gets implemented. For now, the best way to go is to fall back to the terminal pid based approach (i.e. not adding setup lines to .bashrc/.zshrc/shell init).

Does not work with urxvt daemon

When running a urxvt in client-server mode xwininfo | awk '/Window id:/{print $4}' | tr '[a-f]' '[A-F]' | pidswallow -t fails with bspc error for an invalid descriptor. Also interesting is that printing the cname of the client as found by the swallow function reveals the name of the daemon (urxvtd). I have no idea if this is expected or not

not working with sxiv

Whether running sxiv directly from the terminal, or if it gets launched from a file manager like lf, the launching window is not getting swallowed.

terminal getting swallowed using dmenu

If my terminal is focused and I launch a program using dmenu, the terminal is being swallowed. That shouldn't be happening. The terminal should only be swallowed if I launch a program using the terminal.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.