How to get an X11 Window from a Process ID?
来源:互联网 发布:义和团刀枪不入 知乎 编辑:程序博客网 时间:2024/05/01 04:08
http://stackoverflow.com/questions/151407/how-to-get-an-x11-window-from-a-process-id
Under Linux, my C++ application is using fork() and execv() to launch multiple instances of OpenOffice so as to view some powerpoint slide shows. This part works.
Next I want to be able to move the OpenOffice windows to specific locations on the display. I can do that with the XMoveResizeWindow() function but I need to find the Window for each instance.
I have the process ID of each instance, how can I find the X11 Window from that ?
UPDATE - Thanks to Andy's suggestion, I have pulled this off. I'm posting the code here to share it with the Stack Overflow community.
Unfortunately Open Office does not seem to set the _NET_WM_PID property so this doesn't ultimately solve my problem but it does answer the question.
chingliu-desktop tmp # g++ -o getwindow getwindow.cpp -I/usr/X11R6/include -L/usr/X11R6/lib -lX11// Attempt to identify a window by name or attribute.// by Adam Pierce <adam@doctort.org>#include <X11/Xlib.h>#include <X11/Xatom.h>#include <iostream>#include <list>using namespace std;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) { cout << "No such atom" << endl; return; } search(wRoot); } const list<Window> &result() const { return _result; }private: unsigned long _pid; Atom _atomPID; Display *_display; 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]); } }};int main(int argc, char **argv){ if(argc < 2) return 1; int pid = atoi(argv[1]); cout << "Searching for windows associated with PID " << pid << endl;// Start with the root window. Display *display = XOpenDisplay(0); WindowsMatchingPid match(display, XDefaultRootWindow(display), pid);// Print the result. const list<Window> &result = match.result(); for(list<Window>::const_iterator it = result.begin(); it != result.end(); it++) cout << "Window #" << (unsigned long)(*it) << endl; return 0;}
- How to get an X11 Window from a Process ID?
- Java How to get the PID from a process?
- How to get the process ID to kill a nohup process?
- How to get the DOM of a WebBrowser control from a window handle (VB6)
- How to get IHTMLDocument2 from a HWND
- Lesson5 How to open a new window from a pushbutton
- How to send an email from oracle workflow process using an AdHocRole?
- How to make a random selection from an SQL table
- How To Use An Embedded Resource From A Sub-Folder
- How to implement an algorithm from a scientific paper
- How to create an EMF model from a Java application ?
- How to Recover from a Truncate Command [ID 117055.1]
- How to get a global unique hardware ID
- How to Get WiFi From a Mile Away
- [cernRoot] how to get contours from a TH2D
- How to spawn a process
- From an inner class, "this" points to the inner object. How do I get a "this" pointer to the enclosing object?
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- C++ 第十周实验报告1
- c++虚函数, 接口, 抽象类
- 页面post传值
- 大文件上传 进度条显示 (仿csdn资源上传效果)
- RO段、RW段和ZI段 --Image$$??$$Limit 含义
- How to get an X11 Window from a Process ID?
- ARM 汇编伪指令宏的用法详解(MACRO-MEND)
- VS2005 打开VS2008方法
- C++ 第十周实验报告2---《根据个人月收入总额,计算出应缴税款和税后收入》
- 做程序真累。。
- 关于__irq 的使用
- Android项目在eclipse中无法打包apk文件[bin目录下没生成apk文件]问题解决
- 获取每个插件(bunlde)的国际化文件
- ARM微处理器的寄存器结构