Poll (): differenze tra le versioni
(Creata pagina con "La funzione int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) attende un evento su un descrittore di file (''file descriptor''). In particolare la funzione ''poll...") |
Nessun oggetto della modifica |
||
| Riga 57: | Riga 57: | ||
read_C(pfd.fd, buf, 8) | read_C(pfd.fd, buf, 8) | ||
<FONT Color=gray>' ''Vengono mostrati i dati intercettati dal file-device del mouse | |||
' ''in caso di spostamento del mouse, nonché pressione dei tasti e della rotellina:''</font> | |||
Print Right(CStr(Pointer@(buf)), 6) | Print Right(CStr(Pointer@(buf)), 6) | ||
Versione delle 10:51, 20 set 2016
La funzione
int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
attende un evento su un descrittore di file (file descriptor). In particolare la funzione poll( ) permette ad un programma di monitorare un descrittore di file, ossia di restare in attesa fino a quando il descrittore di file osservato non diventa "pronto" per eseguire un'operazione di I/O sul suo file corrispondente (ad esempio, leggere senza bloccare o effettuarvi una breve scrittura).
Si è notato che la funzione poll( ) funziona, come dovuto, solo utilizzando le funzioni esterne delle librerie standard di C, all'uopo previste per l'apertura del file-device e la lettura del relativo file-descriptor.
Volendo utilizzare tale funzione in Gambas, bisognerà dichiararla con Extern, nonché bisognerà dichiarare la libreria di C: libc.so.6, nella quale essa è contenuta:
Private Extern poll(__fds As Pollfd, __nfds As Long, __timeout As Integer) As Integer
Mostriamo un semplice esempio, nel quale si porrà sotto osservazione in lettura il file-device "/dev/input/mouse0" del mouse. Se giungono dati dal file-device, il file-descriptor è pronto per essere letto; e pertanto si raccoglieranno appunto i dati provenienti dal predetto file-device del mouse. Ai fini del seguente esempio va ricordato che il file-device "/dev/input/mouse0" è protetto; pertanto bisognerà preliminarmente cambiare i suoi permessi.
Library "libc:6"
Private Const POLLIN As Integer = 1
Private Const O_RDONLY As Integer = 0
Public Struct Pollfd
fd As Integer
events As Short
revents As Short
End Struct
' int open (const char *__file, int __oflag, ...)
' Open FILE and return a new file descriptor for it, or -1 on error.
Private Extern puts(__s As String) As Integer
' int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
' Poll the file descriptors described by the NFDS structures starting at FDS.
Private Extern poll(__fds As Pollfd, __nfds As Long, __timeout As Integer) As Integer
' ssize_t read (int __fd, void *__buf, size_t __nbytes)
' Read NBYTES into BUF from FD.
Private Extern read_C(__fd As Integer, __buf As Pointer, __nbytes As Long) As Long Exec "read"
' int close (int __fd)
' Close the file descriptor FD.
Private Extern close_C(__fd As Integer) As Integer Exec "close"
Public Sub Main()
Dim pfd As New Pollfd
Dim microsec, rit As Integer
Dim buf As Pointer
microsec = 10
buf = Alloc(SizeOf(gb.Byte), 8)
While True
pfd.fd = open_C("/dev/input/mouse0", O_RDONLY)
pfd.events = POLLIN
rit = poll(pfd, 1, microsec)
read_C(pfd.fd, buf, 8)
' Vengono mostrati i dati intercettati dal file-device del mouse
' in caso di spostamento del mouse, nonché pressione dei tasti e della rotellina:
Print Right(CStr(Pointer@(buf)), 6)
close_C(pfd.fd)
Wend
End