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.
(4 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
E' possibile ottenere una schermata del desktop utilizzando alcune risorse esterne del API di X11 e di Cairo. | 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 | + | Per poter fruire di tali risorse, è necessario richiamare in Gambas le librerie condivise: |
− | * libX11.so.6. | + | * libX11.so.6.4.0 |
− | * libcairo.so.2. | + | * 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: | 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. | + | Library "libX11:6.4.0" |
<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>' ''int XDefaultScreen(display)'' | <FONT Color=gray>' ''int XDefaultScreen(display)'' | ||
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 | + | 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 38: | Riga 38: | ||
− | Library "libcairo:2. | + | Library "libcairo:2.11600.0" |
<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 | + | 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 | + | Dim dsp, vis, surface As Pointer |
+ | Dim root As Long | ||
Dim scr, wi, he, err As Integer | Dim scr, wi, he, err As Integer | ||
<FONT Color=gray>' ''Prova a connettersi con il display:''</font> | <FONT Color=gray>' ''Prova a connettersi con il display:''</font> | ||
− | + | 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) | |
− | |||
− | |||
<FONT Color=gray>' ''Ottiene la superficie di root del display:''</font> | <FONT Color=gray>' ''Ottiene la superficie di root del display:''</font> | ||
− | + | surface = cairo_xlib_surface_create(dsp, root, vis, wi, he) | |
− | + | If surface == 0 Then Error.Raise("Errore !") | |
− | + | ||
− | + | err = cairo_surface_write_to_png(surface, "<FONT Color=gray>''/tmp/schermata.png''</font>") | |
+ | If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !") | ||
<FONT Color=gray>' ''Va in chiusura:''</font> | <FONT Color=gray>' ''Va in chiusura:''</font> | ||
− | + | cairo_surface_destroy(surface) | |
− | + | XCloseDisplay(dsp) | |
'''End''' | '''End''' | ||
− | |||
Versione attuale delle 15:55, 1 mag 2023
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