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

前端 未结 5 2056
小鲜肉
小鲜肉 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 08:55

    There is a command line tool that can do this: wmctrl. The source code of the tool is here (C++): wmctrl sources

    Relevant part of the code (you will need to get some of the functions used here from the project under the link above):

    static int activate_window (Display *disp, Window win, /* {{{ */
        gboolean switch_desktop) {
    unsigned long *desktop;
    
    /* desktop ID */
    if ((desktop = (unsigned long *)get_property(disp, win,
            XA_CARDINAL, "_NET_WM_DESKTOP", NULL)) == NULL) {
        if ((desktop = (unsigned long *)get_property(disp, win,
                XA_CARDINAL, "_WIN_WORKSPACE", NULL)) == NULL) {
            p_verbose("Cannot find desktop ID of the window.\n");
        }
    }
    
    if (switch_desktop && desktop) {
        if (client_msg(disp, DefaultRootWindow(disp), 
                    "_NET_CURRENT_DESKTOP", 
                    *desktop, 0, 0, 0, 0) != EXIT_SUCCESS) {
            p_verbose("Cannot switch desktop.\n");
        }
        g_free(desktop);
    }
    
    client_msg(disp, win, "_NET_ACTIVE_WINDOW", 
            0, 0, 0, 0, 0);
    XMapRaised(disp, win);
    
    return EXIT_SUCCESS;
    }
    

    Hint: remember to call

    XCloseDisplay(display);
    

    Somewhere after the call to this function or you will see no effect.

提交回复
热议问题