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

Da Gambas-it.org - Wikipedia.
Riga 19: Riga 19:
 
  <FONT Color=gray>' ''Window XDefaultRootWindow(display)''
 
  <FONT Color=gray>' ''Window XDefaultRootWindow(display)''
 
  ' ''Returns the root window for the default screen.''</font>
 
  ' ''Returns the root window for the default screen.''</font>
  Private Extern XDefaultRootWindow(display As Pointer) As Pointer
+
  Private Extern XDefaultRootWindow(display As Pointer) As Long
 
   
 
   
 
  <FONT Color=gray>' ''Visual *XDefaultVisual(display, screen_number)''
 
  <FONT Color=gray>' ''Visual *XDefaultVisual(display, screen_number)''
Riga 42: Riga 42:
 
  <FONT Color=gray>' ''cairo_surface_t * cairo_xlib_surface_create (Display *dpy, Drawable drawable, Visual *visual, int width, int height)''
 
  <FONT Color=gray>' ''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.''</font>
 
  ' ''Creates an Xlib surface that draws to the given drawable.''</font>
  Private Extern cairo_xlib_surface_create(dpy As Pointer, drawable As Pointer, visual As Pointer, width As Integer, height As Integer) As Pointer
+
  Private Extern cairo_xlib_surface_create(dpy As Pointer, drawable As Long, visual As Pointer, width As Integer, height As Integer) As Pointer
 
   
 
   
 
  <FONT Color=gray>' ''cairo_status_t cairo_surface_write_to_png(cairo_surface_t *surface, const char *filename)''
 
  <FONT Color=gray>' ''cairo_status_t cairo_surface_write_to_png(cairo_surface_t *surface, const char *filename)''
Riga 55: Riga 55:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
    
 
    
   Dim dsp, root, vis, surface As Pointer
+
   Dim dsp, vis, surface As Pointer
 +
  Dim root As Long
 
   Dim scr, wi, he, err As Integer
 
   Dim scr, wi, he, err As Integer
 
    
 
    

Versione delle 05:57, 30 nov 2015

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 dinamiche e condivise:

  • libX11.so.6.3.0
  • libcairo.so.2.11301.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.3.0"

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display$ As String) 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.11301.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(Null)
   If IsNull(dsp) Then Error.Raise("Impossibile aprire una connessione al server X !")
   
   scr = XDefaultScreen(dsp)
   
   root = XDefaultRootWindow(dsp)
   
   vis = XDefaultVisual(dsp, scr)
   
   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)
   
   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