Differenze tra le versioni di "Individuare l'ID ed il nome di una finestra nonché il PID del programma mediante le funzioni del API di X11"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
 
La libreria '''X11''' ci consente anche di individuare il numero identificativo (''Id'') ed il nome di una qualsiasi finestra, nonché il PID del programma al quale quella finestra appartiene.
 
La libreria '''X11''' ci consente anche di individuare il numero identificativo (''Id'') ed il nome di una qualsiasi finestra, nonché il PID del programma al quale quella finestra appartiene.
  
Sarà necessario richiamare la libreria del sistema X (nella sua attuale versione): "''libX11.so.6.3.0''"
+
Sarà necessario richiamare la libreria del sistema X11: "''libX11.so.6.3.0'' ".
  
  
Riga 14: Riga 14:
 
  <FONT Color=gray>' ''Display *XOpenDisplay(char *display_name)''
 
  <FONT Color=gray>' ''Display *XOpenDisplay(char *display_name)''
 
  ' ''Opens a connection to the X server that controls a display.''</font>
 
  ' ''Opens a connection to the X server that controls a display.''</font>
  Private Extern XOpenDisplay(display$ As String) As Pointer
+
  Private Extern XOpenDisplay(display_name As Pointer) As Pointer
 
<FONT Color=gray>' ''XCloseDisplay (Display *display)''
 
' ''Closes a display or disconnect from the X server.''</font>
 
Private Extern XCloseDisplay(displayP As Pointer)
 
 
   
 
   
 
  <FONT Color=gray>' ''Window XDefaultRootWindow(Display *display)''
 
  <FONT Color=gray>' ''Window XDefaultRootWindow(Display *display)''
Riga 30: Riga 26:
 
  <FONT Color=gray>' ''int XGetWindowProperty(Display *display, Window w, Atom property, long long_offset, long long_length, Bool delete, Atom req_type, Atom *actual_type_return, int *actual_format_return, unsigned long *nitems_return, unsigned long *bytes_after_return, unsigned char **prop_return)''
 
  <FONT Color=gray>' ''int XGetWindowProperty(Display *display, Window w, Atom property, long long_offset, long long_length, Bool delete, Atom req_type, Atom *actual_type_return, int *actual_format_return, unsigned long *nitems_return, unsigned long *bytes_after_return, unsigned char **prop_return)''
 
  ' ''Returns the actual type of the property; the actual format of the property.''</font>
 
  ' ''Returns the actual type of the property; the actual format of the property.''</font>
  Private Extern XGetWindowProperty(displayP As Pointer, w As Long, py As Integer, lo As Long, ll As Long, d As Boolean, rt As Integer, at As Pointer, af As Pointer, ni As Pointer, ba As Pointer, pr As Pointer) As Integer
+
  Private Extern XGetWindowProperty(display As Pointer, w As Long, property_ As Integer, long_offset As Long, long_length As Long, delete As Boolean, req_type As Integer, actual_type_return As Pointer, actual_format_return As Pointer, nitems_return As Pointer, bytes_after_return As Pointer, prop_return As Pointer) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''Status XGetWMName(Display *display, Window w, XTextProperty *text_prop_return)''
 
  <FONT Color=gray>' ''Status XGetWMName(Display *display, Window w, XTextProperty *text_prop_return)''
 
  ' Calls XGetTextProperty() to obtain the WM_NAME property.''</font>
 
  ' Calls XGetTextProperty() to obtain the WM_NAME property.''</font>
  Private Extern XGetWMName(displayP As Pointer, w As Long, text_prop_return As Pointer) As Integer
+
  Private Extern XGetWMName(display As Pointer, w As Long, text_prop_return As Pointer) As Integer
 
   
 
   
 +
<FONT Color=gray>' ''XCloseDisplay (Display *display)''
 +
' ''Closes a display or disconnect from the X server.''</font>
 +
Private Extern XCloseDisplay(displayP As Pointer)
 +
 
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
Riga 47: Riga 47:
 
   Dim id As String
 
   Dim id As String
 
   
 
   
  disp = XOpenDisplay(Null)
+
  disp = XOpenDisplay(0)
  If IsNull(disp) Then Error.Raise("Impossibile aprire lo schermo !")
+
  If disp == 0 Then Error.Raise("Impossibile aprire lo schermo !")
 
      
 
      
  rootW = XDefaultRootWindow(disp)
+
  rootW = XDefaultRootWindow(disp)
 
      
 
      
  x_num_Atom = XInternAtom(disp, "_NET_CLIENT_LIST", False)
+
  x_num_Atom = XInternAtom(disp, "_NET_CLIENT_LIST", False)
 
      
 
      
  err = XGetWindowProperty(disp, rootW, x_num_Atom, 0, MAX_PROPERTY_VALUE_LEN / 4, False, XA_WINDOW, VarPtr(tipo), VarPtr(formato), VarPtr(n_fin), VarPtr(bytes_succ), VarPtr(datID))
+
  err = XGetWindowProperty(disp, rootW, x_num_Atom, 0, MAX_PROPERTY_VALUE_LEN / 4, False, XA_WINDOW, VarPtr(tipo), VarPtr(formato), VarPtr(n_fin), VarPtr(bytes_succ), VarPtr(datID))
  If err <> 0 Then Error.Raise("Impossibile ottenere dati dalla funzione 'XGetWindowProperty' !")
+
  If err <> 0 Then Error.Raise("Impossibile ottenere dati dalla funzione 'XGetWindowProperty' !")
 
      
 
      
  If XA_WINDOW <> tipo Then Error.Raise("Tipo invalido di proprietà '_NET_CLIENT_LIST' !")
+
  If XA_WINDOW <> tipo Then Error.Raise("Tipo invalido di proprietà '_NET_CLIENT_LIST' !")
 
      
 
      
  Print "Id", Null, "PID", "Nome finestra\n"
+
  Print "Id", Null, "PID", "Nome finestra\n"
 
      
 
      
  p = Alloc(32)
+
  p = Alloc(SizeOf(gb.Byte), 32)
 
      
 
      
  stId = Memory datID For Read
+
  stId = Memory datID For Read
 
   
 
   
  For b = 1 To n_fin * 2
+
  For b = 1 To n_fin * 2
+
    Read #stId, i
    Read #stId, i
+
    If i > 0 Then
+
      XGetWMName(disp, i, p)
    If i > 0 Then
+
      id = Hex(i)
      XGetWMName(disp, i, p)
+
      x_num_Atom = XInternAtom(disp, "_NET_WM_PID", False)
      id = Hex(i)
+
      err = XGetWindowProperty(disp, i, x_num_Atom, 0, MAX_PROPERTY_VALUE_LEN / 4, False, XA_CARDINAL, VarPtr(tipo), VarPtr(formato), VarPtr(n_fin), VarPtr(bytes_succ), VarPtr(datPID))
   
+
      If err <> 0 Then Error.Raise("Impossibile ottenere dati dalla funzione 'XGetWindowProperty' !")
      x_num_Atom = XInternAtom(disp, "_NET_WM_PID", False)
+
      If datPID > 0 Then
      err = XGetWindowProperty(disp, i, x_num_Atom, 0, MAX_PROPERTY_VALUE_LEN / 4, False, XA_CARDINAL, VarPtr(tipo), VarPtr(formato), VarPtr(n_fin), VarPtr(bytes_succ), VarPtr(datPID))
+
        stPid = Memory datPID For Read
      If err <> 0 Then Error.Raise("Impossibile ottenere dati dalla funzione 'XGetWindowProperty' !")
+
        Read #stPid, pid
+
      Endif
      If IsNull(datPID) = False Then
+
      Print id, Null, pid, String@(Pointer@(p))
        stPid = Memory datPID For Read
+
    Endif
        Read #stPid, pid
+
   Next
      Endif
+
     
      Print id, Null, pid, String@(Pointer@(p))
 
    Endif
 
    
 
  Next
 
   
 
   
 
 
  <FONT Color=gray>' ''Va in chiusura:''</font>
 
  <FONT Color=gray>' ''Va in chiusura:''</font>
  stPid.Close
+
  stPid.Close
  stId.Close
+
  stId.Close
  Free(p)
+
  Free(p)
  XCloseDisplay(disp)
+
  XCloseDisplay(disp)
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione delle 10:54, 15 dic 2021

La libreria X11 ci consente anche di individuare il numero identificativo (Id) ed il nome di una qualsiasi finestra, nonché il PID del programma al quale quella finestra appartiene.

Sarà necessario richiamare la libreria del sistema X11: "libX11.so.6.3.0 ".


Mostriamo di seguito un possibile breve codice al riguardo:

Private Const MAX_PROPERTY_VALUE_LEN As Integer = 4096

Library "libX11:6.3.0"

Private Const XA_CARDINAL As Integer = 6
Private Const XA_WINDOW As Integer = 33

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display_name As Pointer) As Pointer

' Window XDefaultRootWindow(Display *display)
' Return the root window for the default screen.
Private Extern XDefaultRootWindow(displayP As Pointer) As Integer

' Atom XInternAtom(Display *display, char *atom_name, Bool only_if_exists)
' Returns the atom identifier associated with the specified atom_name string.
Private Extern XInternAtom(displayP As Pointer, atom_name As String, only_if_exists As Boolean) As Integer

' int XGetWindowProperty(Display *display, Window w, Atom property, long long_offset, long long_length, Bool delete, Atom req_type, Atom *actual_type_return, int *actual_format_return, unsigned long *nitems_return, unsigned long *bytes_after_return, unsigned char **prop_return)
' Returns the actual type of the property; the actual format of the property.
Private Extern XGetWindowProperty(display As Pointer, w As Long, property_ As Integer, long_offset As Long, long_length As Long, delete As Boolean, req_type As Integer, actual_type_return As Pointer, actual_format_return As Pointer, nitems_return As Pointer, bytes_after_return As Pointer, prop_return As Pointer) As Integer

' Status XGetWMName(Display *display, Window w, XTextProperty *text_prop_return)
' Calls XGetTextProperty() to obtain the WM_NAME property.
Private Extern XGetWMName(display As Pointer, w As Long, text_prop_return As Pointer) As Integer

' XCloseDisplay (Display *display)
' Closes a display or disconnect from the X server.
Private Extern XCloseDisplay(displayP As Pointer)
 

Public Sub Main()

 Dim disp, datID, p, datPID As Pointer
 Dim x_num_Atom, tipo, formato As Integer
 Dim err, n_fin, bytes_succ, i, pid As Integer
 Dim rootW As Long
 Dim stId, stPid As Stream
 Dim b As Byte
 Dim id As String

 disp = XOpenDisplay(0)
 If disp == 0 Then Error.Raise("Impossibile aprire lo schermo !")
   
 rootW = XDefaultRootWindow(disp)
   
 x_num_Atom = XInternAtom(disp, "_NET_CLIENT_LIST", False)
   
 err = XGetWindowProperty(disp, rootW, x_num_Atom, 0, MAX_PROPERTY_VALUE_LEN / 4, False, XA_WINDOW, VarPtr(tipo), VarPtr(formato), VarPtr(n_fin), VarPtr(bytes_succ), VarPtr(datID))
 If err <> 0 Then Error.Raise("Impossibile ottenere dati dalla funzione 'XGetWindowProperty' !")
   
 If XA_WINDOW <> tipo Then Error.Raise("Tipo invalido di proprietà '_NET_CLIENT_LIST' !")
   
 Print "Id", Null, "PID", "Nome finestra\n"
   
 p = Alloc(SizeOf(gb.Byte), 32)
   
 stId = Memory datID For Read

 For b = 1 To n_fin * 2
   Read #stId, i
   If i > 0 Then
     XGetWMName(disp, i, p)
     id = Hex(i)
     x_num_Atom = XInternAtom(disp, "_NET_WM_PID", False)
     err = XGetWindowProperty(disp, i, x_num_Atom, 0, MAX_PROPERTY_VALUE_LEN / 4, False, XA_CARDINAL, VarPtr(tipo), VarPtr(formato), VarPtr(n_fin), VarPtr(bytes_succ), VarPtr(datPID))
     If err <> 0 Then Error.Raise("Impossibile ottenere dati dalla funzione 'XGetWindowProperty' !")
     If datPID > 0 Then
       stPid = Memory datPID For Read
       Read #stPid, pid
     Endif
     Print id, Null, pid, String@(Pointer@(p))
   Endif
 Next
     
' Va in chiusura:
 stPid.Close
 stId.Close
 Free(p)
 XCloseDisplay(disp)

End