View Full Version : kde pid from window id and vice versa

10th April 2012, 10:00

I need a way to get the window id from pid of a process. And the reverse too, ie from the window id I need to get the pid of the process. I have the exepath and from the /proc i get the pid and from the pid I need to get the window Id( or a list of window ids).

I am trying to write an app which will check if a particular process is currently running or not. If it is running then get its main window and make it active and set focus to it. If it is not running then start it and make it the active window.

I can go platform specific on this. It need not be cross platform or qt. I am looking for code on KDE, X and gnome.

Thank you.

14th May 2012, 05:42
If the you are doing this because you want a Qt application to prohibit multiple copies of itself then QtSingleApplication (http://qt.gitorious.org/qt-solutions/qt-solutions/trees/master/qtsingleapplication) can do that for Qt applications.

If you want a general, nothing-to-do-with-Qt, approach for X11 systems then I suggest you look at the source of xwininfo to get an idea how to traverse the window information. The "-all" option shows the PID probably using the approach here: http://unix.stackexchange.com/questions/5478/what-process-created-this-x11-window
The same information is visible using xprop.

18th May 2013, 10:17
The other way is to implement what you said manually. That is, to check the state of a process you need to execute a short kernel code to search through kernel process table and extract the process you are looking for. For the other purpose, that is to get the pid, read about fork() wait_pid().