How do I bring a processes window to the foreground on X Windows? (C++)

前端 未结 5 2028
小鲜肉
小鲜肉 2020-12-25 08:47

I have the PID for the process (and the name), I want to bring it to the front on linux (ubuntu). On mac I would simply do SetFrontProcess(pid), on windows I\'d

5条回答
  •  再見小時候
    2020-12-25 09:06

    I thought this would be easy since /proc seemingly has the required data but /proc/${pid}/environ doesn't provide the correct window ID since it is usually the child of the parent who really owns the window where the process is running. To get the proper windowid you need to parse the xwininfo output then you can use xdotool to change the focus.

    CMD_PID= && while IFS= read -r -d '' var; do 
    if grep -q "^WINDOWID=" <<< "$var"; then 
     winid=$(printf '0x%x\n' $(sed 's/WINDOWID=//' <<< "${var}"))
     child_cnt=0 && IFS=$(echo -en "\n\b") && for a in $(xwininfo -root -tree | tac | sed -n "/${winid}/,\$p"); do 
     grep -q -i "children" <<< "${a}" && let child_cnt+=1
     ((${child_cnt} == 2)) && real_winid=$(grep -o '0x[^ ]*' <<< "${last_line}") && break
     last_line="${a}"
     done 
     xdotool windowraise ${real_winid} 
     break 
    fi 
    done < /proc/${CMD_PID}/environ
    

提交回复
热议问题