X11: raise an existing window via command line?
Asked Answered
A

2

17

Given an X client window ID, is there a way to raise that window from the command line?

$ xlsclients -a
Window 0x3000001:
  Machine:  ohm
  Name:  Terminal
  Icon Name:  foo
  Command:  foo
  Instance/Class:  foo/bar

$ xraise -id 0x3000001   <-- this is what I would like to do
Autogamy answered 22/6, 2009 at 19:27 Comment(0)
L
12

You can also use wmctrl, which does similar things

Lifeline answered 8/2, 2011 at 16:43 Comment(1)
wmctrl didn't do anything on my machine. XSetInputFocus(), shown below, was able to do this.Eberhard
D
10

Like... http://zaurus.daemons.gr/menaie/build/hacks/xraise/xraise.c?

EDIT: Inserting the code in the answer :

#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

/* "borrowed" from xwininfo/dsimple.c */

Window Window_With_Name(dpy, top, name)
     Display *dpy;
     Window top;
     char *name;
{
    Window *children, dummy;
    unsigned int nchildren;
    int i;
    Window w=0;
    char *window_name;

    if (XFetchName(dpy, top, &window_name) && (strstr(window_name, name) == window_name))
      return(top);

    if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
      return(0);

    for (i=0; i<nchildren; i++)
        {
        w = Window_With_Name(dpy, children[i], name);
        if (w)
          break;
    }
    if (children) XFree ((char *)children);
      return(w);
}

int main(int argc, char **argv)
{
    Window window;
    Display *dpy;

    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s window-name\n", argv[0]);
        exit(1);
    }

    dpy = XOpenDisplay(":0");
    if (!dpy)
    {
        fprintf(stderr, "Cannot open display.\n");
        exit(1);
    }

    window = Window_With_Name(dpy, DefaultRootWindow(dpy), argv[1]);
    if (!window)
    {
        fprintf(stderr, "Cannot find a window by that name.\n");
        exit(1);
    }

    XSetInputFocus(dpy, window, RevertToPointerRoot, CurrentTime);
    if(!XRaiseWindow(dpy, window))
    {
        fprintf(stderr, "XRaiseWindow error?\n");
        exit(1);
    }

    XCloseDisplay(dpy);
}
Depend answered 22/6, 2009 at 19:39 Comment(1)
exactly, thanks! I commented out the XSetInputFocus() call, so now it works perfectly for what I need.Autogamy

© 2022 - 2024 — McMap. All rights reserved.