XWINDOW 中 WINDOWID 的获取

XWINDOW 下获取 WINDOWID

原文链接:https://blog.csdn.net/superkeep/article/details/88948634

linux下的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
#方法一
xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| cut -d ' ' -f 5

#方法二
xwininfo -name ShowAnimationProject | grep "Window id"| cut -d " " -f 4

#方法三
findpid=$1

known_windows=$(xwininfo -root -children|sed -e 's/^ *//'|grep -E "^0x"|awk '{ print $1 }')

for id in ${known_windows}
do
xp=$(xprop -id $id _NET_WM_PID)
if test $? -eq 0; then
pid=$(xprop -id $id _NET_WM_PID|cut -d'=' -f2|tr -d ' ')

if test "x${pid}" = x${findpid}
then
echo "Windows Id: $id"
fi
fi
done

通过代码方式获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef WINDOWIDUTIL_H
#define WINDOWIDUTIL_H
#include<QDebug>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <iostream>
#include <list>
#include <stdlib.h>

class WindowsMatchingPid
{
public:
WindowsMatchingPid(Display *display, Window wRoot, unsigned long pid)
: _display(display),
_pid(pid)
{
// Get the PID property atom.
_atomPID = XInternAtom(display, "_NET_WM_PID", True);
if(_atomPID == None)
{
std::cout << "No such atom" << std::endl;
return;
}

search(wRoot);
}

const std::list<Window> &result() const { return _result; }

private:
unsigned long _pid;
Atom _atomPID;
Display *_display;
std::list<Window> _result;

void
search(Window w)
{
// Get the PID for the current Window.
Atom type;
int format;
unsigned long nItems;
unsigned long bytesAfter;
unsigned char *propPID = 0;
if(Success == XGetWindowProperty(
_display,
w,
_atomPID,
0,
1,
False,
XA_CARDINAL,
&type,
&format,
&nItems,
&bytesAfter,
&propPID))
{
if(propPID != 0)
{
// If the PID matches, add this window to the result set.
if(_pid == *((unsigned long *)propPID))
{
_result.push_back(w);
}

XFree(propPID);
}
}

// Recurse into child windows.
Window wRoot;
Window wParent;
Window *wChild;
unsigned nChildren;
if(0 != XQueryTree(_display, w, &wRoot, &wParent, &wChild, &nChildren))
{
for(unsigned i = 0; i < nChildren; i++)
search(wChild[i]);
}

//XFree(propPID);
}
};

unsigned long
get_win_id_from_pid(qint64 pid)
{
//std::cout << "Searching for windows associated with PID " << pid << std::endl;

// Start with the root window.
Display *display = XOpenDisplay(0);

WindowsMatchingPid match(display, XDefaultRootWindow(display), pid);

// Print the result.
const std::list<Window> &result = match.result();

long win_id = 0;
for(std::list<Window>::const_iterator pos = result.begin(); pos != result.end(); pos++)
{
std::cout << "Window #" << (unsigned long)(*pos) << std::endl;
win_id = (unsigned long)(*pos);
}

return win_id;
}
#endif // WINDOWIDUTIL_H
# Linux

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×