获取顶部窗口的pid和详细信息 [英] Getting pid and details for topmost window

查看:103
本文介绍了获取顶部窗口的pid和详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何获取顶部活动窗口的PID,然后如何使用PID获取窗口的属性吗?我的意思是进程名称,程序名称等属性.

Does anyone know how to get the PID of the top active window and then how to get the properties of the window using the PID? I mean properties like process name, program name, etc.

我正在Linux(Ubuntu 9.10)下使用Qt.

I'm using Qt under Linux (Ubuntu 9.10).

推荐答案

在Linux中有一个调用xprop的命令,它是一个用于在X服务器中显示窗口属性的实用程序.在linux中,xprop -root为您提供根Windows属性以及其他活动程序.那么您可以使用以下命令获取活动窗口的ID:

there is a command in linux call xprop which is a utility for displaying window properties in an X server. In linux xprop -root gives you the root windows properties and also other active programs. then you can get the ID of the active window using this command:

xprop -root | grep _NET_ACTIVE_WINDOW\(WINDOW\)

要获取 just 的活动窗口ID(在行首不包含"_NET_ACTIVE_WINDOW(WINDOW):window id#"),请使用以下命令:

to get just the active window ID ( without "_NET_ACTIVE_WINDOW(WINDOW): window id # " in the beginning of the line ) use this command:

xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}'

现在您可以将此命令输出保存在用户定义的变量中:

now you can save this command output in a user defined variable:

myid=xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}'

xprop具有一个属性调用-id.此参数允许用户在命令行上选择窗口ID.我们应该在输出中寻找_NET_WM_PID(CARDINAL)...因此我们使用以下命令:

xprop have an attribute call -id. This argument allows the user to select window id on the command line. We should look for _NET_WM_PID(CARDINAL) in output ... so we use this command:

xprop -id $myid | awk '/_NET_WM_PID\(CARDINAL\)/{print $NF}'

这为您提供了最活跃的窗口进程ID.

this gives you the topmost active window process ID.

变得更加棘手,只需1个命令即可完成所有工作...:

to be more trickey and do all things in just 1 command ... :

 xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}') | awk '/_NET_WM_PID\(CARDINAL\)/{print $NF}'

现在,我可以使用popen函数通过C ++程序(在Linux中)运行这些命令,抓取标准输出并打印或保存. popen创建一个管道,以便我们可以读取正在调用的程序的输出.

Now I can run these commands via my C++ program ( in linux ) using popen function, grab stdout and print or save it. popen creates a pipe so we can read the output of the program we are invoking.

(您还可以使用'/proc'文件系统并获取PID的更多详细信息('/proc/ YOUR_PID /status'))

( you can also use '/proc' file system and get more detail of a PID ('/proc/YOUR_PID/status') )

#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;

inline std::string exec(char* cmd) {
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
                result += buffer;
    }
    pclose(pipe);
    return result;
}

int main()
{
    //we uses \\ instead of \ ( \ is a escape character ) in this string
 cout << exec("xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\\(WINDOW\\)/{print $NF}') | awk '/_NET_WM_PID\\(CARDINAL\\)/{print $NF}'").c_str(); 
 return 0;
}

这篇关于获取顶部窗口的pid和详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆