ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there any way to get the pid of the top active window,
and then get all foeils of this id, for example the title of the window,name if the process...
'xwininfo' gives you some specs but 'xprop' gives you more and including the Process Id. The problem with both is they require you to literally point out the window to use. I wonder if there's a tool that allows for automation since for instance IceWM's "icesh" can do 'icesh -window focus'...
then you should pass the window ID to another call of xprop using the -id option and looking for the string "_NET_WM_PID(CARDINAL)" to obtain the process ID. Maybe a little tricky, but this should address the doubt raised by unSpawn:
Moreover, you can use the -display option of xprop in situations where the DISPLAY information is not available to the shell (e.g. if using the xprop command in a script running from crontab).
Another way to obtain the ID of the active window is by means of the xdpyinfo command, for example:
Indeed you did not mention the programming language you were deal with, hence the shell was a common guess. Unfortunately, I'm not a C/C++ programmer, so I'm afraid you have to wait for someone more experienced in this topic. In the meanwhile a good starting point could be to look at the source code of the commands mentioned above. You will find them all at x.org.
I should write a simple java program to pause, resume and kill the process easily by GUI.
@colucix I will get the window id informations from xprop by grapping the window. After that i need to find the process id with these informations but i don't know how. Can you please write me a command which gets window id as parameter, and returns the process id. But this should work for chrome browser which start each tab as a new proces.
in xlib(warning xlib is messy) I get the topactive window from the windowstack like this:
starting with X_ is the atom of a property.
Code:
Window *get_win_list_stacked(unsigned long *len)
{
Atom type;
int form;
unsigned long remain;
unsigned char *list;
errno = 0;
if(XGetWindowProperty( global_display,
global_root_window,
X_NET_CLIENT_LIST_STACKING,
0,
1024,
False,
XA_WINDOW,
&type,
&form,
len,
&remain,
&list)
!= Success)
{
return 0;
}
return (Window*)list;
}
int minimized_window(Window win)
{
Atom type;
int form;
unsigned long i, len, remain;
Atom *atoms;
atoms = NULL;
XGetWindowProperty( global_display,
win,
X_NET_WM_STATE,
0,
1024,
False,
XA_ATOM,
&type,
&form,
&len,
&remain,
(unsigned char**)&atoms);
for(i=0; i<len; ++i) {
if(atoms[i]==X_NET_WM_STATE_HIDDEN) {
XFree(atoms);
return 1;
}
}
XFree(atoms);
return 0;
}
int is_skip_taskbar_or_pager_set(Window win)
{
Atom type;
int form;
unsigned long i, len, remain;
Atom *atoms;
atoms = NULL;
XGetWindowProperty( global_display,
win,
X_NET_WM_STATE,
0,
1024,
False,
XA_ATOM,
&type,
&form,
&len,
&remain,
(unsigned char**)&atoms);
for(i=0; i<len; ++i)
{
if((atoms[i]==X_NET_WM_STATE_SKIP_TASKBAR) || (atoms[i]==X_NET_WM_STATE_SKIP_PAGER))
{
XFree(atoms);
return 1;
}
}
XFree(atoms);
return 0;
}
Window get_top_window_from_stack()
{
int i;
unsigned long len;
Window *list;
Window ret;
ret = 0;
list = (Window*) get_win_list_stacked(&len);
for (i = 0; i < (int) len; i++) {
if (!is_skip_taskbar_or_pager_set((Window) list[i]) && !minimized_window((Window) list[i]))
ret = list[i];
}
XFree(list);
return ret;
}
then you call need to get the pid using your window id. This might be done with the _NET_WM_PID property(if the wm supports it or the programs sets it. I guess).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.