프로세스 ID에서 X11 창을 얻는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/151407

  •  02-07-2019
  •  | 
  •  

문제

Linux에서 C ++ 응용 프로그램은 Fork () 및 execv ()를 사용하여 일부 PowerPoint 슬라이드 쇼를 볼 수 있도록 여러 개 OpenOffice 인스턴스를 시작합니다. 이 부분은 작동합니다.

다음으로 OpenOffice Windows를 디스플레이의 특정 위치로 옮길 수 있습니다. xmoveresizewindow () 함수로 그렇게 할 수 있지만 각 인스턴스에 대한 창을 찾아야합니다.

각 인스턴스의 프로세스 ID가 있습니다. 어떻게 X11 창을 찾을 수 있습니까?


업데이트 - Andy의 제안 덕분에 나는 이것을 뽑았습니다. 스택 오버플로 커뮤니티와 공유하기 위해 여기에 코드를 게시하고 있습니다.

불행히도 Open Office는 _NET_WM_PID 속성을 설정하지 않는 것처럼 보이므로 궁극적으로 내 문제를 해결하지는 않지만 질문에 대답합니다.

// 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;
}
도움이 되었습니까?

해결책

내가 이것을하는 유일한 방법은 당신이 찾고있는 것을 찾을 때까지 창의 나무를 가로 지르는 것입니다. 횡단은 어렵지 않습니다 (예제가 필요한 경우 XwinInfo -Root -Tree가 XwinInfo.c를 보면 무엇을하는지 확인하십시오).

그러나 찾고있는 창을 어떻게 식별합니까? 약간 응용 프로그램은 _net_wm_pid라는 창 속성을 설정합니다.

나는 그 오픈 오피스를 믿는다 ~이다 해당 속성을 설정하는 응용 프로그램 중 하나 (대부분의 그놈 앱과 마찬가지로) 운이 좋을 것입니다.

다른 팁

/Proc/PID/Environ에 WindowID라는 변수가 포함되어 있는지 확인합니다.

파티에 늦게. 그러나 2004 년에 Harald Welte는 LD_PRELOAD를 통해 XCreateWinDow () 호출을 랩핑하는 코드 스 니펫을 게시하고 프로세스 ID를 _NET_WM_PID에 저장합니다. 이렇게하면 생성 된 각 창에 PID 항목이 있는지 확인합니다.

http://www.mail-archive.com/devel@xvel@xfree86.org/msg05806.html

설치 해보세요 xdotool, 그 다음에:

#!/bin/bash
# --any and --name present only as a work-around, see: https://github.com/jordansissel/xdotool/issues/14
ids=$(xdotool search --any --pid "$1" --name "dummy")

나는 많은 신분증을 얻습니다. 나는 이것을 사용하여 단자 창을 긴 명령으로 수행 할 때 긴급한 것으로 설정하고 프로그램을 사용합니다. seturgent. 난 그냥 내가 얻는 모든 ID를 살펴 봅니다. xdotool 그리고 달리기 seturgent 그들에게.

좋은 방법이 없습니다. 내가 보는 유일한 실제 옵션은 다음과 같습니다.

  1. 프로세스 주소 공간을 둘러보고 연결 정보 및 창 ID를 찾을 수 있습니다.
  2. Netstat 또는 LSOF 또는 IPC를 사용하여 연결을 XSERVER에 매핑하려고 시도한 다음 (어떻게 든 Root가 필요합니다) 연결 정보를 찾아 찾을 수 있습니다.
  3. 인스턴스를 스폰 할 때 다른 창이 매핑 될 때까지 기다릴 수 있습니다.

각 인스턴스의 프로세스 ID가 있습니까? OOO에 대한 나의 경험은 OOO의 두 번째 인스턴스를 실행하려는 시도는 단지 OOO의 첫 번째 인스턴스와 대화하고 추가 파일을 열라고 말한다는 것입니다.

X의 메시지 중단 기능을 사용하여 창문을 멋지게 요청해야한다고 생각합니다. OOO가 어딘가에 커버를 문서화하기를 바랍니다.

파이썬을 사용하면 방법을 찾았습니다 여기, 아이디어는 왔습니다 번 슈시

응용 프로그램을 시작한 경우 CMD 문자열을 알아야합니다. xprop, 당신은 항상 모든 XID를 통과하고 PID가 원하는 PID와 동일한 지 확인할 수 있습니다.

import subprocess
import re

import struct
import xcffib as xcb
import xcffib.xproto

def get_property_value(property_reply):
    assert isinstance(property_reply, xcb.xproto.GetPropertyReply)

    if property_reply.format == 8:
        if 0 in property_reply.value:
            ret = []
            s = ''
            for o in property_reply.value:
                if o == 0:
                    ret.append(s)
                    s = ''
                else:
                    s += chr(o)
        else:
            ret = str(property_reply.value.buf())

        return ret
    elif property_reply.format in (16, 32):
        return list(struct.unpack('I' * property_reply.value_len,
                                  property_reply.value.buf()))

    return None

def getProperty(connection, ident, propertyName):

    propertyType = eval(' xcb.xproto.Atom.%s' % propertyName)

    try:
        return connection.core.GetProperty(False, ident, propertyType,
                                        xcb.xproto.GetPropertyType.Any,
                                        0, 2 ** 32 - 1)
    except:
        return None


c = xcb.connect()
root = c.get_setup().roots[0].root

_NET_CLIENT_LIST = c.core.InternAtom(True, len('_NET_CLIENT_LIST'),
                                     '_NET_CLIENT_LIST').reply().atom


raw_clientlist = c.core.GetProperty(False, root, _NET_CLIENT_LIST,
                                    xcb.xproto.GetPropertyType.Any,
                                    0, 2 ** 32 - 1).reply()

clientlist = get_property_value(raw_clientlist)

cookies = {}

for ident in clientlist:
    wm_command = getProperty(c, ident, 'WM_COMMAND')
    cookies[ident] = (wm_command)

xids=[]

for ident in cookies:
    cmd = get_property_value(cookies[ident].reply())
    if cmd and spref in cmd:
        xids.append(ident)

for xid in xids:
    pid = subprocess.check_output('xprop -id %s _NET_WM_PID' % xid, shell=True)
    pid = re.search('(?<=\s=\s)\d+', pid).group()

    if int(pid) == self.pid:
        print 'found pid:', pid
        break

print 'your xid:', xid

현대적인 C ++ 기능을 사용하여 OP 코드를 다시 구현할 수있는 자유를 얻었습니다. 그것은 동일한 기능을 유지하지만 조금 더 잘 읽는다고 생각합니다. 또한 벡터 삽입이 던져도 누출되지 않습니다.

// Attempt to identify a window by name or attribute.
// originally written by Adam Pierce <adam@doctort.org>
// revised by Dario Pellegrini <pellegrini.dario@gmail.com>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <iostream>
#include <vector>


std::vector<Window> pid2windows(pid_t pid, Display* display, Window w) {
  struct implementation {
    struct FreeWrapRAII {
      void * data;
      FreeWrapRAII(void * data): data(data) {}
      ~FreeWrapRAII(){ XFree(data); }
    };

    std::vector<Window> result;
    pid_t pid;
    Display* display;
    Atom atomPID;

    implementation(pid_t pid, Display* display): pid(pid), display(display) {
      // Get the PID property atom
      atomPID = XInternAtom(display, "_NET_WM_PID", True);
      if(atomPID == None) {
        throw std::runtime_error("pid2windows: no such atom");
      }
    }

    std::vector<Window> getChildren(Window w) {
      Window    wRoot;
      Window    wParent;
      Window   *wChild;
      unsigned  nChildren;
      std::vector<Window> children;
      if(0 != XQueryTree(display, w, &wRoot, &wParent, &wChild, &nChildren)) {
        FreeWrapRAII tmp( wChild );
        children.insert(children.end(), wChild, wChild+nChildren);
      }
      return children;
    }

    void emplaceIfMatches(Window w) {
      // Get the PID for the given 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) {
          FreeWrapRAII tmp( propPID );
          if(pid == *reinterpret_cast<pid_t*>(propPID)) {
            result.emplace_back(w);
          }
        }
      }
    }

    void recurse( Window w) {
      emplaceIfMatches(w);
      for (auto & child: getChildren(w)) {
        recurse(child);
      }
    }

    std::vector<Window> operator()( Window w ) {
      result.clear();
      recurse(w);
      return result;
    }
  };
  //back to pid2windows function
  return implementation{pid, display}(w);
}

std::vector<Window> pid2windows(const size_t pid, Display* display) {
  return pid2windows(pid, display, XDefaultRootWindow(display));
}


int main(int argc, char **argv) {
  if(argc < 2)
    return 1;

  int pid = atoi(argv[1]);
  std::cout << "Searching for windows associated with PID " << pid << std::endl;

  // Start with the root window.
  Display *display = XOpenDisplay(0);
  auto res = pid2windows(pid, display);

  // Print the result.
  for( auto & w: res) {
    std::cout << "Window #" << static_cast<unsigned long>(w) << std::endl;
  }

  XCloseDisplay(display);
  return 0;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top