LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-19-2009, 04:48 PM   #1
sammy_aus
LQ Newbie
 
Registered: Dec 2009
Posts: 4

Rep: Reputation: 0
getting the pid of the top active window


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...

Thanx.
 
Old 12-20-2009, 10:43 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
'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'...
 
Old 12-20-2009, 11:42 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can obtain the ID of the active window using
Code:
xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}'
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:
Code:
 xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}') | awk '/_NET_WM_PID\(CARDINAL\)/{print $NF}'
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:
Code:
xdpyinfo | grep focus | grep -E -o 0x[0-9a-f]+
 
1 members found this post helpful.
Old 12-20-2009, 12:31 PM   #4
sammy_aus
LQ Newbie
 
Registered: Dec 2009
Posts: 4

Original Poster
Rep: Reputation: 0
C/C++

thank you,
but i meant getting pid in C/C++
 
Old 12-20-2009, 03:14 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 01-23-2012, 10:24 AM   #6
steve_kam
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Rep: Reputation: Disabled
Question

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.

Thank you!
 
Old 01-24-2012, 05:12 PM   #7
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
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).


otherwise check the source of xprop

Last edited by kalleanka; 01-24-2012 at 05:17 PM.
 
1 members found this post helpful.
Old 01-25-2012, 01:00 AM   #8
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
colucix ->


Thanks for sharing the commands! They are interesting and are something I did not know about.
 
  


Reply

Tags
pid, process, xlib



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unfocusable, always-on-top window Xyem Linux - Software 2 04-22-2009 02:13 AM
How to send characters on to the active window using X Window? abhinav.zoso Programming 3 01-24-2009 02:08 PM
Keep window on top in gnome wit_273 Linux - Desktop 3 03-27-2007 12:02 PM
Bash: check pid for active process (FreeBSD) Harpune Programming 2 05-16-2006 01:33 PM
Advanced question: finding pid of an x window PFudd Linux - Software 3 05-31-2005 05:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration