Recent

Author Topic: source for search PID and XWindowID  (Read 119 times)

nocturnus

  • Newbie
  • Posts: 1
source for search PID and XWindowID
« on: October 20, 2020, 01:42:36 am »
Hello, I want to leave you the source to obtain the pid and the XwindowID, I achieved it through https://forum.lazarus.freepascal.org/index.php?topic=10148.0 and. that

Code: Pascal  [Select][+][-]
  1. {
  2. Coder: Nocturnus / urnusdev@zohomail.com
  3. XWINDOW ID AND PID  ( LINUX )
  4. Obtener listado PID e ID de la XWINDOW
  5.  
  6. Debian 10
  7. Free Pascal Compiler version 3.2.0+dfsg-8 [2020/08/22] for x86_64
  8. }
  9.  
  10. program findXIDs;
  11.  
  12. uses
  13.   x,
  14.   xlib,
  15.   xatom,
  16.   unix,
  17.   xutil,
  18.   ctypes,
  19.   sysutils
  20.   ;
  21.  
  22. function chl(Display:PDisplay; childw:TWindow;atomPID:TAtom):boolean;
  23. var
  24.   j,xnwindows:integer;
  25.   xmyw,xparentw,uwin:TWindow;
  26.   xchildw:PPWindow;
  27.   prop:ppcuchar;
  28.   bytes_after_ret: pculong;
  29.   type_ret: TAtom;
  30.   format_ret: integer;
  31.   nitems_ret: pculong;
  32.   myprop: PCardinal;
  33.   ELPID:TWINDOW;
  34. begin
  35.   XQueryTree(Display, childw, @xmyw, @xparentw, @xchildw, @xnwindows);
  36.      for j := 0 to xnwindows - 1 do
  37.   begin
  38.      uwin := TWindow(xchildw[j]);
  39.      XGetWindowProperty(
  40.      Display,
  41.      TWindow(xchildw[j]),
  42.      atomPID,
  43.      0,
  44.      1,
  45.      False,
  46.      XA_CARDINAL,
  47.      @type_ret,
  48.      @format_ret,
  49.      @nitems_ret,
  50.      @bytes_after_ret,
  51.      @prop
  52.      );
  53.  
  54.      if prop > PPcuchar(0) then
  55.      begin
  56.         myprop := PCardinal(prop);
  57.         ELPID := TWindow(myprop^);
  58.         writeln(' * PID:'+ELPID.ToString+' WindowID: 0x'+uwin.ToHexString(8));
  59.      end;
  60.    end;
  61. end;
  62.  
  63. function search():Boolean;
  64. var
  65.   Display:PDisplay;
  66.   atomPID:TAtom;
  67.   type_ret: TAtom;
  68.   format_ret: integer;
  69.   nitems_ret: pculong;
  70.   bytes_after_ret: pculong;
  71.   rootw: TWindow;
  72.   myw: TWindow;
  73.   parentw: TWindow;
  74.   childw: PWindow;
  75.   windowattr: TXWindowAttributes;
  76.   n, c: PChar;
  77.   wname, cname: string;
  78.   nwindows: integer;
  79.   i: integer;
  80.   prop:ppcuchar;
  81.   myprop: PCardinal;
  82.   ELPID:TWindow;
  83. begin
  84.              result := false;
  85.              Display:= XOpenDisplay(nil);
  86.              rootw := RootWindow(Display, DefaultScreen(Display));
  87.  
  88.              atomPID:=XInternAtom(Display,'_NET_WM_PID',true);
  89.              XQueryTree(Display, rootw, @myw, @parentw, @childw, @nwindows);
  90.         for i := 0 to nwindows - 1 do
  91.         begin
  92.  
  93.      XGetWindowProperty(
  94.      Display,
  95.      childw[i],
  96.      atomPID,
  97.      0,
  98.      1,
  99.      False,
  100.      XA_CARDINAL,
  101.      @type_ret,
  102.      @format_ret,
  103.      @nitems_ret,
  104.      @bytes_after_ret,
  105.      @prop
  106.      );
  107.  
  108.    chl(Display, childw[i],atomPID);
  109.     if XGetWindowAttributes(Display, childw[i], @windowattr) <> 1 then
  110.       continue;
  111. {    if windowattr.override_redirect = 1 then
  112.       continue;
  113.                 }
  114.     if XFetchName(Display, childw[i], @n) = 1 then
  115.       wname := n
  116.     else wname := 'n/a';
  117.     if XGetClassHint(Display, childw[i], @c) = 1 then
  118.       cname := c
  119.     else cname := 'n/a';
  120.  
  121.    if prop > PPcuchar(0) then
  122.      begin
  123.  
  124.        myprop := PCardinal(prop);
  125.        ELPID := TWindow(myprop^);
  126.        writeln(inttostr(i) + ' Name: ' + wname + ' Class: ' + cname + ' PID:'+ELPID.ToString);
  127.  
  128.      end;
  129.   end;
  130.   XFree(childw);
  131.   XCloseDisplay(Display);
  132.  
  133.   result :=false;
  134. end;
  135.  
  136. begin
  137.       search();
  138. end.

[Edited to add code tags - please see How to use the Forum]
« Last Edit: October 20, 2020, 02:13:33 am by trev »

 

TinyPortal © 2005-2018