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 | + | 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 | ||
− | |||
− | |||
− | |||
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( | + | 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( | + | 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) | + | <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( | + | 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( | + | 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( | + | 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( | + | 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( | + | 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( | + | 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(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 | |
<FONT Color=gray>' ''Va in chiusura:''</font> | <FONT Color=gray>' ''Va in chiusura:''</font> | ||
− | + | XCloseDisplay(dis) | |
− | + | 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 | |
− | + | 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 | |
<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); ":" | |
− | + | 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 | |
<FONT Color=gray>' ''Scrive il comando dell'applicazione:''</font> | <FONT Color=gray>' ''Scrive il comando dell'applicazione:''</font> | ||
− | + | 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''' | '''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 | |
− | + | Print "''" | |
− | + | Return | |
Endif | 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''' | '''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 " | |
− | + | 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''' | '''End''' |
Versione delle 11: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