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

前端 未结 5 2019
小鲜肉
小鲜肉 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:05

    I haven't tried this myself, but putting these two methods together may work:

    The XRaiseWindow API Call in xlib lets you raise a Window to the front if you know the Window ID.

    http://www.unix.com/man-page/Linux/3/XRaiseWindow/

    This stackoverflow answer explains how to get a Window ID from a PID:

    How to get an X11 Window from a Process ID?

    EDIT:

    I've had limited success with XRaiseWindow. The Following program does work under twm window manager, but not ion which I usually use. The Window Manager must have ways of preventing applications from 'popping up'. To make this work, i also had to pass it the Window ID of the Window Manager's frame for the window, not the window itself. run xwininfo -frame and click on the window and you get the frame ID instead, compile this program with gcc test.c -lX and pass it that hexid on the command line and it will raise the window.

     #include 
     #include 
     #include 
    
     int main(int argc, char **argv)
     {
       Display *dsp = XOpenDisplay(NULL);
       long id = strtol(argv[1], NULL, 16);
       XSetWindowAttributes xswa;
       xswa.override_redirect=True;
       XChangeWindowAttributes (dsp,id,CWOverrideRedirect, &xswa);
       XRaiseWindow ( dsp, id );
       XCloseDisplay ( dsp );
     }
    

提交回复
热议问题