Search code examples
linuxarchlinuxxlibxorg

How to give focus to a single window in xorg?


I am not that well versed with xorg and the xlib libraray.I am not a C programmer.I installed st-simple terminal.Heard the fact that we can run only a terminal or a browser in xorg server without a window manager in a online article and confirmed it on arch wiki. .So to try it I ran.

startx /usr/local/bin/st

st did start but it did not have focus.Though I could write in it.I just want a solution which tells me how to give st focus as the unfocused cursor puts me off.

Thanks in advance


Solution

  • That st thing probably doesn't believe in PointerRoot and focus-follows-mouse. Try giving it the focus explicitly with XSetInputFocus(3).

    If you have a C compiler and the xorg -dev or -devel packages installed, try running this inside your st terminal:

    echo '
    #include <X11/Xlib.h>
    #include <stdlib.h>
    int main(){
        Display *dpy; char *ws; Window w;
        if((dpy = XOpenDisplay(0)) && (ws = getenv("WINDOWID")) && (w = strtoul(ws, 0, 0))){
            XSetInputFocus(dpy, w, RevertToPointerRoot, 0); XSync(dpy, False);
        }
    }' | cc -Wall -lX11 -x c - -o getfocus && ./getfocus
    
    

    There may be utilities like xdotool able to do that, but as far as I know they're not installed by default.

    Even better would be to fix that "suckless" terminal emulator so it sucks less and not assume a window manager and a point-to-type interface.