• 3 Posts
  • 41 Comments
Joined 1 year ago
cake
Cake day: June 20th, 2023

help-circle
  • I did a workaround by editing a script I found and don’t understand using xprop, xwininfo, and xdotool. I probably should have mentioned that I’m using X, most of this stuff doesn’t work on Wayland. Here’s my script so far:

    #!/bin/bash
    
    # The script is looking for a window with a given WM_CLASS property. The window
    # will be attached to the root node (R in the graph below), the root note is
    # what you get from 'xprop -root'. There are two different types of windows with
    # the same WM_CLASS (Q and W) but their tree structures are different. However,
    # the tree structure of the two windows are different as seen in the graph
    # below.
    #
    #     R
    #    / \
    #   A   Y
    #  /\    \
    # B  C    X
    #     \    \
    #      Q    W
    #
    # To run the script 'xprop' and 'xwininfo' must be installed.
    #
    # NOTE: This script will not work if a window is not brought to top when it is
    # created.
    
    class_name=TeamViewer
    
    # regex for extracting hex id's
    grep_id='0[xX][a-zA-Z0-9]\{7\}'
    
    function grep_parent_id {
        local result="`xwininfo -tree -id $1 | grep 'Parent window id:' | grep -o $grep_id`"
        echo "$result"
    }
    
    # for every change in the root window's _NET_ACTIVE_WINDOW property, which
    # is supposed to hold the currently active window (I believe this depends on the WM)
    # note that the _NET_ACTIVE_WINDOW event will be triggered if a window that is already
    # open is brought to top.
    xprop -spy -root _NET_ACTIVE_WINDOW | grep --line-buffered -o $grep_id |
    while read -r id; do
        class="`xprop -id $id WM_CLASS | grep $class_name`"
        if [ -n "$class" ]; then
            parent_id=$(grep_parent_id $id)
            grand_parent_id=$(grep_parent_id $parent_id)
            
            grand_parent_tree="`xwininfo -id $grand_parent_id -tree`"
            
            # If the the grand_parent has two children we have found window Q
            tree_check="`grep -o '2 children:' <<< $grand_parent_tree`"
            if [ -z "$tree_check" ]; then
    	    if xprop -id "$id" | grep -q 'WM_NAME(STRING) = "TeamViewer Authentication"'; then
    	            echo "Found window (W), it has ID: $id"
    		    echo "key Tab
    		    key space
    		    key shift+Tab
    		    key shift+Tab
    		    key Down
    		    key Tab
    		    type {my-username}
    		    key Tab" | xdotool -
    		    # wait for the window to be closed
    		    xprop -spy -id $id > /dev/null 2>&1
                fi
            fi
        fi
    done
    

    It spits out errors after the window closes because I think it’s trying to get windows properties from the now closed window, but it gets the job done!