Ottenere una schermata (screenshot) del desktop con le risorse del API di X11 e di Cairo

Da Gambas-it.org - Wikipedia.

E' possibile ottenere una schermata del desktop utilizzando alcune risorse esterne del API di X11 e di Cairo.

Per poter fruire di tali risorse, è necessario richiamare in Gambas le librerie condivise:

  • libX11.so.6.4.0
  • libcairo.so.2.11600.0


Mostriamo un esempio, nel quale verrà effettuata una schermata dell'intero desktop. In fine la schermata verrà salvata in un file di formato .png:

Library "libX11:6.4.0"

' 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

' int XDefaultScreen(display)
' Returns the default screen number.
Private Extern XDefaultScreen(display As Pointer) As Integer

' Window XDefaultRootWindow(display)
' Returns the root window for the default screen.
Private Extern XDefaultRootWindow(display As Pointer) As Long

' Visual *XDefaultVisual(display, screen_number)
' Returns the default visual type for the specified screen.
Private Extern XDefaultVisual(display As Pointer, scr_num As Integer) As Pointer

' int XDisplayWidth(display, screen_number)
' Returns the width of the screen in pixels.
Private Extern XDisplayWidth(display As Pointer, scr_num As Integer) As Integer

' int XDisplayHeight(display, screen_number)
' Returns an integer that describes the height of the screen in pixels.
Private Extern XDisplayHeight(display As Pointer, scr_num As Integer) As Integer

' XCloseDisplay(Display *display)
' Closes the connection to the X server for the display specified in the Display structure and destroys all windows.
Private Extern XCloseDisplay(display As Pointer)


Library "libcairo:2.11600.0"

' cairo_surface_t * cairo_xlib_surface_create (Display *dpy, Drawable drawable, Visual *visual, int width, int height)
' Creates an Xlib surface that draws to the given drawable.
Private Extern cairo_xlib_surface_create(dpy As Pointer, drawable As Long, visual As Pointer, width As Integer, height As Integer) As Pointer

' cairo_status_t cairo_surface_write_to_png(cairo_surface_t *surface, const char *filename)
' Writes the contents of surface to a new file filename as a PNG image.
Private Extern cairo_surface_write_to_png(cairo_surface As Pointer, filename As String) As Integer

' void cairo_surface_destroy(cairo_surface_t *surface)
' Decreases the reference count on surface by one.
Private Extern cairo_surface_destroy(cairo_surface As Pointer)


Public Sub Main()
 
 Dim dsp, vis, surface As Pointer
 Dim root As Long
 Dim scr, wi, he, err As Integer
 
' Prova a connettersi con il display:
 dsp = XOpenDisplay(0)
 If dsp == 0 Then Error.Raise("Impossibile aprire una connessione al server X !")
   
 scr = XDefaultScreen(dsp)
 root = XDefaultRootWindow(dsp)
   
 vis = XDefaultVisual(dsp, scr)
 If vis == 0 Then Error.Raise("Errore !")
 
 wi = XDisplayWidth(dsp, scr)  
 he = XDisplayHeight(dsp, scr)
   
' Ottiene la superficie di root del display:
 surface = cairo_xlib_surface_create(dsp, root, vis, wi, he)
 If surface == 0 Then Error.Raise("Errore !")
  
 err = cairo_surface_write_to_png(surface, "/tmp/schermata.png")
 If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !")
   
' Va in chiusura:
 cairo_surface_destroy(surface)
 XCloseDisplay(dsp)
 
End


Riferimenti