Differenze tra le versioni di "Ottenere informazioni sulle applicazioni client in esecuzione su un display con le funzioni del API di X11"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
 
La libreria '''X11''' ci consente anche di ottenere alcune informazioni generiche sulle applicazioni client in esecuzione su un display.
 
La libreria '''X11''' ci consente anche di ottenere alcune informazioni generiche sulle applicazioni client in esecuzione su un display.
  
Sarà necessario richiamare la libreria del sistema X (nella sua attuale versione): "''libX11.so.6.3.0''" , nonché la libreria Xmu (nella sua attuale versione): "''libXmu.so.6.2.0''"
+
Sarà necessario richiamare la libreria del sistema X11: "''libX11.so.6.3.0'' " , nonché la libreria Xmu: "''libXmu.so.6.2.0'' ".
 
 
  
 
Mostriamo di seguito un possibile breve codice al riguardo:
 
Mostriamo di seguito un possibile breve codice al riguardo:
 +
Library "libX11:6.3.0"
 +
 
  Public Struct XTextProperty
 
  Public Struct XTextProperty
 
   value As Pointer
 
   value As Pointer
Riga 16: Riga 17:
 
   res_class As Pointer
 
   res_class As Pointer
 
  End Struct
 
  End Struct
 
 
Library "libX11:6.3.0"
 
 
   
 
   
 
  Private Const XA_STRING As Integer = 31
 
  Private Const XA_STRING As Integer = 31
Riga 24: Riga 22:
 
  <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>' ''Window XDefaultRootWindow(Display *display)''
 
  <FONT Color=gray>' ''Window XDefaultRootWindow(Display *display)''
 
  ' ''Return the root window for the default screen.''</font>
 
  ' ''Return the root window for the default screen.''</font>
  Private Extern XDefaultRootWindow(displayP As Pointer) As Integer
+
  Private Extern XDefaultRootWindow(display As Pointer) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''Status XGetWMClientMachine(Display *display, Window w, XTextProperty *text_prop_return)
 
  <FONT Color=gray>' ''Status XGetWMClientMachine(Display *display, Window w, XTextProperty *text_prop_return)
Riga 42: Riga 40:
 
  Private Extern XFree(po As Pointer) As Integer
 
  Private Extern XFree(po As Pointer) As Integer
 
   
 
   
  <FONT Color=gray>' ''char * XGetAtomName(Display *display, Atom atom) as pointer''
+
  <FONT Color=gray>' ''char * XGetAtomName(Display *display, Atom atom)''
 
  ' ''Returns the name associated with the specified atom.''</font>
 
  ' ''Returns the name associated with the specified atom.''</font>
  Private Extern XGetAtomName(display As Pointer, atom As Long)
+
  Private Extern XGetAtomName(display As Pointer, atom As Long) As Pointer
 
   
 
   
 
  <FONT Color=gray>' ''XGetWMName(Display *display, Window w, XTextProperty *text_prop_return)''
 
  <FONT Color=gray>' ''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 XTextProperty) As Integer
+
  Private Extern XGetWMName(display As Pointer, w As Long, text_prop_return As XTextProperty) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''Status XGetWMIconName(Display *display, Window w, XTextProperty *text_prop_return)''
 
  <FONT Color=gray>' ''Status XGetWMIconName(Display *display, Window w, XTextProperty *text_prop_return)''
 
  ' ''Calls XGetTextProperty() to obtain the WM_ICON_NAME property.''</font>
 
  ' ''Calls XGetTextProperty() to obtain the WM_ICON_NAME property.''</font>
  Private Extern XGetWMIconName(displayP As Pointer, w As Long, text_prop_return As XTextProperty) As Integer
+
  Private Extern XGetWMIconName(display As Pointer, w As Long, text_prop_return As XTextProperty) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''Status XQueryTree(Display *display, Window w, Window *root_return, Window *parent_return, Window **children_return, unsigned int *nchildren_return)''
 
  <FONT Color=gray>' ''Status XQueryTree(Display *display, Window w, Window *root_return, Window *parent_return, Window **children_return, unsigned int *nchildren_return)''
 
  ' ''Returns the root ID, the parent window ID, a pointer to the list of children windows (NULL when there are no children), and the number of children in the list for the specified window.''</font>
 
  ' ''Returns the root ID, the parent window ID, a pointer to the list of children windows (NULL when there are no children), and the number of children in the list for the specified window.''</font>
  Private Extern XQueryTree(displayP As Pointer, w As Long, root_return As Pointer, parent_return As Pointer, children_return As Pointer, nchildren_return As Pointer) As Integer
+
  Private Extern XQueryTree(display As Pointer, w As Long, root_return As Pointer, parent_return As Pointer, children_return As Pointer, nchildren_return As Pointer) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''Status XGetClassHint(Display *display, Window w, XClassHint *class_hints_return)''
 
  <FONT Color=gray>' ''Status XGetClassHint(Display *display, Window w, XClassHint *class_hints_return)''
 
  ' ''Returns the class hint of the specified window to the members of the supplied structure.''</font>
 
  ' ''Returns the class hint of the specified window to the members of the supplied structure.''</font>
  Private Extern XGetClassHint(displayP As Pointer, w As Long, class_hints_return As XClassHint) As Integer
+
  Private Extern XGetClassHint(display As Pointer, w As Long, class_hints_return As XClassHint) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''XCloseDisplay (Display *display)''
 
  <FONT Color=gray>' ''XCloseDisplay (Display *display)''
 
  ' ''Closes a display or disconnect from the X server.''</font>
 
  ' ''Closes a display or disconnect from the X server.''</font>
  Private Extern XCloseDisplay(displayP As Pointer)
+
  Private Extern XCloseDisplay(display As Pointer)
 
   
 
   
 
   
 
   
 
  <FONT Color=gray>' ''Window XmuClientWindow(Display *dpy, Window win)''
 
  <FONT Color=gray>' ''Window XmuClientWindow(Display *dpy, Window win)''
 
  ' ''Finds a window at or below the specified window which has a WM_STATE property.''</font>
 
  ' ''Finds a window at or below the specified window which has a WM_STATE property.''</font>
  Private Extern XmuClientWindow(displayP As Pointer, w As Long) As Long In "libXmu:6.2.0"
+
  Private Extern XmuClientWindow(display As Pointer, w As Long) As Long In "libXmu:6.2.0"
 
   
 
   
 
   
 
   
Riga 84: Riga 82:
 
   Dim st As Stream
 
   Dim st As Stream
 
      
 
      
  dis = XOpenDisplay(Null)
+
  dis = XOpenDisplay(0)
  If IsNull(dis) Then Error.Raise("Impossibile aprire lo schermo !")
+
  If dis == 0 Then Error.Raise("Impossibile aprire lo schermo !")
 
      
 
      
  root = XDefaultRootWindow(dis)
+
  root = XDefaultRootWindow(dis)
  Proprieta_Clienti(dis, root)
+
  Proprieta_Clienti(dis, root)
 
  If Not XQueryTree(dis, root, VarPtr(dm), VarPtr(dm), VarPtr(figli), VarPtr(nc)) Then Return
 
 
   
 
   
  st = Memory figli For Read
+
  If Not XQueryTree(dis, root, VarPtr(dm), VarPtr(dm), VarPtr(figli), VarPtr(nc)) Then Return
 
   
 
   
  For i = 0 To nc - 1
+
  st = Memory figli For Read
    Read #st, l
+
  For i = 0 To nc - 1
    client = XmuClientWindow(dis, l)
+
    Read #st, l
    If client <> 0 Then Proprieta_Clienti(dis, client)
+
    client = XmuClientWindow(dis, l)
  Next
+
    If client <> 0 Then Proprieta_Clienti(dis, client)
  st.Close
+
  Next
   
+
  st.Close
 
      
 
      
 
  <FONT Color=gray>' ''Va in chiusura:''</font>
 
  <FONT Color=gray>' ''Va in chiusura:''</font>
  XCloseDisplay(dis)
+
  XCloseDisplay(dis)
  exitus(0)
+
  exitus(0)
 
      
 
      
 
  '''End'''
 
  '''End'''
+
 
 
 
  '''Private''' Procedure Proprieta_Clienti(dis As Pointer, w As Long)
 
  '''Private''' Procedure Proprieta_Clienti(dis As Pointer, w As Long)
 
   
 
   
Riga 118: Riga 113:
 
   Dim s As String
 
   Dim s As String
 
    
 
    
  If Not XGetWMClientMachine(dis, w, mxtp) Then
+
  If Not XGetWMClientMachine(dis, w, mxtp) Then
    mxtp.value = 0
+
    mxtp.value = 0
    mxtp.encoding = 0
+
    mxtp.encoding = 0
  Endif
+
  Endif
 
   
 
   
  clip = Alloc(8)
+
  clip = Alloc(Sizeof(gb.Integer), 2)
 
   
 
   
  If Not XGetCommand(dis, w, VarPtr(clip), VarPtr(cli)) Then
+
  If Not XGetCommand(dis, w, VarPtr(clip), VarPtr(cli)) Then
    If IsNull(mxtp.value) = False Then XFree(mxtp.value)
+
    If mxtp.value > 0 Then XFree(mxtp.value)
    Return
+
    Return
  Endif
+
  Endif
 
   
 
   
 
  <FONT Color=gray>' ''Scrive l'informazione d'intestazione dell'applicazione:''</font>
 
  <FONT Color=gray>' ''Scrive l'informazione d'intestazione dell'applicazione:''</font>
  Print "\nFinestra &"; Hex(w); ":"
+
  Print "\nFinestra &"; Hex(w); ":"
  If IsNull(mxtp.value) Then Print "nullo", Hex(w)
+
  If mxtp.value == 0 Then Print "nullo", Hex(w)
  Testo(dis, "  Machine:  ", mxtp)
+
  Testo(dis, "  Machine:  ", mxtp)
 
   
 
   
  If XGetWMName(dis, w, nxtp) Then
+
  If XGetWMName(dis, w, nxtp) Then
    Testo(dis, "  Name:  ", nxtp)
+
    Testo(dis, "  Name:  ", nxtp)
    If IsNull(nxtp.value) = False Then XFree(nxtp.value)
+
    If nxtp.value > 0 Then XFree(nxtp.value)
  Endif
+
  Endif
 
   
 
   
  If IsNull(mxtp.value) = False Then XFree(mxtp.value)
+
  If mxtp.value > 0 Then XFree(mxtp.value)
 
   
 
   
  If XGetWMIconName(dis, w, xtp) <> 0 Then
+
  If XGetWMIconName(dis, w, xtp) <> 0 Then
    Testo(dis, "  Icon Name:  ", xtp)
+
    Testo(dis, "  Icon Name:  ", xtp)
    If IsNull(xtp.value) = False Then XFree(xtp.value)
+
    If xtp.value > 0 Then XFree(xtp.value)
  Endif
+
  Endif
 
   
 
   
 
  <FONT Color=gray>' ''Scrive il comando dell'applicazione:''</font>
 
  <FONT Color=gray>' ''Scrive il comando dell'applicazione:''</font>
  st = Memory clip For Read
+
  st = Memory clip For Read
 +
  While i < cli
 +
    Read #st, p
 +
    Print "  Command:  "; String@(p)
 +
    Inc i
 +
  Wend
 +
  st.Close
 
   
 
   
  While i < cli
+
  s = "  Instance/Class:  "
    Read #st, p
+
  If XGetClassHint(dis, w, clh) Then
    Print "  Command:  "; String@(p)
+
    If clh.res_name > 0 Then
    Inc i
+
      s &= String@(clh.res_name)
  Wend
+
      XFree(clh.res_name)
  st.Close
+
    Endif
+
    s &= "/"
+
    If clh.res_class > 0 Then
  s = "  Instance/Class:  "
+
      s &= String@(clh.res_class)
  If XGetClassHint(dis, w, clh) Then
+
      XFree(clh.res_class)
    If IsNull(clh.res_name) = False Then
+
    Endif
      s &= String@(clh.res_name)
+
  Endif
      XFree(clh.res_name)
+
  Print s
    Endif
+
   
    s &= "/"
 
    If IsNull(clh.res_class) = False Then
 
      s &= String@(clh.res_class)
 
      XFree(clh.res_class)
 
    Endif
 
  Endif
 
  Print s
 
   
 
 
  '''End'''
 
  '''End'''
 
 
   
 
   
 
  '''Private''' Procedure Testo(dis As Pointer, s As String, xtp As XTextproperty)
 
  '''Private''' Procedure Testo(dis As Pointer, s As String, xtp As XTextproperty)
 
    
 
    
  If (xtp.encoding = 0) Or (xtp.formatI = 0) Then
+
  If (xtp.encoding == 0) Or (xtp.formatI == 0) Then
    Print "''"
+
    Print "''"
    Return
+
    Return
 
   Endif
 
   Endif
 
   
 
   
  If (xtp.encoding = XA_STRING) And (xtp.formatI = 8) Then
+
  If (xtp.encoding = XA_STRING) And (xtp.formatI == 8) Then
    If IsNull(xtp.value) = False Then
+
    If xtp.value > 0 Then
      s &= String@(xtp.value)
+
      s &= String@(xtp.value)
      Print s
+
      Print s
    Endif
+
    Endif
  Else
+
  Else
    s &= Sconosciuto(dis, xtp.encoding, xtp.formatI)
+
    s &= Sconosciuto(dis, xtp.encoding, xtp.formatI)
  Endif
+
  Endif
 
   
 
   
 
  '''End'''
 
  '''End'''
 
 
   
 
   
 
  '''Private''' Function Sconosciuto(dis As Pointer, tipo As Long, formato As Integer) As String
 
  '''Private''' Function Sconosciuto(dis As Pointer, tipo As Long, formato As Integer) As String
Riga 198: Riga 189:
 
   Dim s As String
 
   Dim s As String
 
    
 
    
  s = "<Tipo sconosciuto "
+
  s = "<Tipo sconosciuto "
 
      
 
      
  If tipo = 0 Then
+
  If tipo == 0 Then
    s &= "Nessuno"
+
    s &= "Nessuno"
  Else If IsNull(XGetAtomName(dis, tipo)) = False Then
+
  Else If XGetAtomName(dis, tipo) > 0 Then
    s &= XGetAtomName(dis, tipo)
+
    s &= XGetAtomName(dis, tipo)
  Else
+
  Else
    Print Null
+
    Print Null
  Endif
+
  Endif
 
   
 
   
  s &= CStr(tipo) & " o formato " & CLong(formato) & ">"
+
  s &= CStr(tipo) & " o formato " & CLong(formato) & ">"
 
      
 
      
  Return s
+
  Return s
 
    
 
    
 
  '''End'''
 
  '''End'''

Versione delle 12:13, 15 dic 2021

La libreria X11 ci consente anche di ottenere alcune informazioni generiche sulle applicazioni client in esecuzione su un display.

Sarà necessario richiamare la libreria del sistema X11: "libX11.so.6.3.0 " , nonché la libreria Xmu: "libXmu.so.6.2.0 ".

Mostriamo di seguito un possibile breve codice al riguardo:

Library "libX11:6.3.0"

Public Struct XTextProperty
  value As Pointer
  encoding As Long
  formatI As Integer
  nitems As Long
End Struct

Public Struct XClassHint
  res_name As Pointer
  res_class As Pointer
End Struct

Private Const XA_STRING As Integer = 31

' 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(display As Pointer) As Integer

' Status XGetWMClientMachine(Display *display, Window w, XTextProperty *text_prop_return)
' Performs an XGetTextProperty() on the WM_CLIENT_MACHINE property.
Private Extern XGetWMClientMachine(display As Pointer, w As Long, text_prop_return As XTextProperty) As Integer

' Status XGetCommand(Display *display, Window w, char ***argv_return, int *argc_return)
' Reads the WM_COMMAND property from the specified window and returns a string list.
Private Extern XGetCommand(display As Pointer, w As Long, argv_return As Pointer, argc_return As Pointer) As Integer

' int XFree(void *)
' It's a general-purpose Xlib routine that frees the specified data.
Private Extern XFree(po As Pointer) As Integer

' char * XGetAtomName(Display *display, Atom atom)
' Returns the name associated with the specified atom.
Private Extern XGetAtomName(display As Pointer, atom As Long) As Pointer

' 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 XTextProperty) As Integer

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

' Status XQueryTree(Display *display, Window w, Window *root_return, Window *parent_return, Window **children_return, unsigned int *nchildren_return)
' Returns the root ID, the parent window ID, a pointer to the list of children windows (NULL when there are no children), and the number of children in the list for the specified window.
Private Extern XQueryTree(display As Pointer, w As Long, root_return As Pointer, parent_return As Pointer, children_return As Pointer, nchildren_return As Pointer) As Integer

' Status XGetClassHint(Display *display, Window w, XClassHint *class_hints_return)
' Returns the class hint of the specified window to the members of the supplied structure.
Private Extern XGetClassHint(display As Pointer, w As Long, class_hints_return As XClassHint) As Integer

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


' Window XmuClientWindow(Display *dpy, Window win)
' Finds a window at or below the specified window which has a WM_STATE property.
Private Extern XmuClientWindow(display As Pointer, w As Long) As Long In "libXmu:6.2.0"


' void exit(int status)
' Terminates the calling process immediately.Any Open file descriptors belonging To the process are closed And any children Of the process are inherited.
Private Extern exitus(status As Integer) In "libc:6" Exec "exit"


Public Sub Main()

 Dim i, nc As Integer
 Dim dis, figli As Pointer
 Dim dm, root, client, l As Long
 Dim st As Stream
   
 dis = XOpenDisplay(0)
 If dis == 0 Then Error.Raise("Impossibile aprire lo schermo !")
   
 root = XDefaultRootWindow(dis)
 Proprieta_Clienti(dis, root)

 If Not XQueryTree(dis, root, VarPtr(dm), VarPtr(dm), VarPtr(figli), VarPtr(nc)) Then Return

 st = Memory figli For Read
 For i = 0 To nc - 1
   Read #st, l
   client = XmuClientWindow(dis, l)
   If client <> 0 Then Proprieta_Clienti(dis, client)
 Next
 st.Close
   
' Va in chiusura:
 XCloseDisplay(dis)
 exitus(0)
   
End
 
Private Procedure Proprieta_Clienti(dis As Pointer, w As Long)

 Dim mxtp, nxtp, xtp As New XTextproperty
 Dim clh As New XClassHint
 Dim cli, i As Integer
 Dim clip, p As Pointer
 Dim st As Stream
 Dim s As String
 
 If Not XGetWMClientMachine(dis, w, mxtp) Then
   mxtp.value = 0
   mxtp.encoding = 0
 Endif

 clip = Alloc(Sizeof(gb.Integer), 2)

 If Not XGetCommand(dis, w, VarPtr(clip), VarPtr(cli)) Then
   If mxtp.value > 0 Then XFree(mxtp.value)
   Return
 Endif

' Scrive l'informazione d'intestazione dell'applicazione:
 Print "\nFinestra &"; Hex(w); ":"
 If mxtp.value == 0 Then Print "nullo", Hex(w)
 Testo(dis, "  Machine:  ", mxtp)

 If XGetWMName(dis, w, nxtp) Then
   Testo(dis, "  Name:  ", nxtp)
   If nxtp.value > 0 Then XFree(nxtp.value)
 Endif

 If mxtp.value > 0 Then XFree(mxtp.value)

 If XGetWMIconName(dis, w, xtp) <> 0 Then
   Testo(dis, "  Icon Name:  ", xtp)
   If xtp.value > 0 Then XFree(xtp.value)
 Endif

' Scrive il comando dell'applicazione:
 st = Memory clip For Read
 While i < cli
   Read #st, p
   Print "  Command:  "; String@(p)
   Inc i
 Wend
 st.Close

 s = "  Instance/Class:  "
 If XGetClassHint(dis, w, clh) Then
   If clh.res_name > 0 Then
     s &= String@(clh.res_name)
     XFree(clh.res_name)
   Endif
   s &= "/"
   If clh.res_class > 0 Then
     s &= String@(clh.res_class)
     XFree(clh.res_class)
   Endif
 Endif
 Print s
   
End

Private Procedure Testo(dis As Pointer, s As String, xtp As XTextproperty)
 
 If (xtp.encoding == 0) Or (xtp.formatI == 0) Then
   Print ""
   Return
  Endif

 If (xtp.encoding = XA_STRING) And (xtp.formatI == 8) Then
   If xtp.value > 0 Then
     s &= String@(xtp.value)
     Print s
   Endif
 Else
   s &= Sconosciuto(dis, xtp.encoding, xtp.formatI)
 Endif

End

Private Function Sconosciuto(dis As Pointer, tipo As Long, formato As Integer) As String
  
 Dim s As String
 
 s = "<Tipo sconosciuto "
   
 If tipo == 0 Then
   s &= "Nessuno"
 Else If XGetAtomName(dis, tipo) > 0 Then
   s &= XGetAtomName(dis, tipo)
 Else
   Print Null
 Endif

 s &= CStr(tipo) & " o formato " & CLong(formato) & ">"
   
 Return s
 
End