1

While trying to write a script involving wmctrl ( since MATE on LMDE 2 doesn't offer as many advanced features as other WM configurations involving Compiz, which I don't have on LMDE 2), I came across the problem of identifying the most recently created window among a set of windows with the same title.
This problem arose, because as is stated here, I cannot identify that window by using PIDs.
So, I am wondering if my assumption, that window IDs are given out in ascending order ( EDIT: with some cap, of course) holds.

If so, please provide me with a concise explanation ( I think this should be fairly easy to answer for anybody knowing enough).

Closely related: I would also be glad to know if wmctrl -l actually sorts windows with the same title in ascending (hex-)numerical order. It seems that way, but I haven't found any officially documented statement regarding this.

  • 1
    Window ids are not given out by any window manager. They are assigned by the X11 server. – Celada Aug 30 '15 at 22:49
2

Window IDs are given out by the X server. The window manager doesn't get a say.

Window IDs encode the client that the window belongs to in the upper 12 bits. The lower 12 bits are assigned sequentially at first, but if an intermediate number becomes free, it can be reused. So comparing window IDs does not give a reliable indication of which window was created last.

Note that even if you could identify the window by a process ID, that wouldn't help find which one is the most recent, since PIDs do not always increase over time. Even on Unix variants that assign PIDs sequentially (which is not universal), they wrap once they reach the maximum value (which is as low as 32767 by default on Linux). Furthermore, X11 clients can run on different machines from the server, but process IDs are only meaningful on one machine.

Freedesktop-compliant applications, including Mate Terminal, set the _NET_WM_USER_TIME property of a window whenever there's user activity (for Mate Terminal, that means input). You can query the property with xprop -id … _NET_WM_USER_TIME. That tells you when a window was last active, not when it was created.

If you want to reliably identify a window, one possibility is to set its title. Another possibility is to set an environment variable with a unique value and use ps or /proc to locate the process with that unique value. You can find the process ID of the client that opened an X11 window through its _NET_WM_PID property, if present, which you can query with xprop or with xdotool getwindowpid. Windows that have the _NET_WM_PID property should also have the property WM_CLIENT_MACHINE set to the host name of the machine where the client process is running. It will help for that to use separate processes for each window.

  • It is possible for 2 windows, from 2 different processes, to have the same process PID. If they reside on different operating system instances. – ctrl-alt-delor Aug 31 '15 at 11:27
1

My guess is that they are like PIDs, allocated in ascending order, except when they are not:

PIDs wrap around.

Let us assume (for now) that they only go up. What would happen on a system that is running for a very long time? Id will get too big, or ID size must be allowed to grow, 32bit, 64bit, 128bit …

If they had to go up only then they could run out, with plenty of system resources still free (no reuse, so eventually system will die from ID exhaustion).

  • it's hard for me to imagine that PIDs would simply wrap around. init has PID 1? :P – polynomial_donut Aug 30 '15 at 22:02
  • Not simply. It will not reuse any that are in use. – ctrl-alt-delor Aug 30 '15 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.