Next: ACPI reference, Up: ACPI API [Contents][Index]
The following code snippet is the heart of the acpi-listener application distributed with guile-netlink. When you run the command at the command line (it takes no arguments), it will sit there silently and forever, waiting for an ACPI event to happen (perhaps you can put your computer to suspended sleep and then re-awaken it), at which time a message will be printed with the data of that event.
;; Use guile-netlinkʼs ACPI interface. The use of a prefix is discretionary; ;; it is useful to have in example code. (use-modules ((netlink acpi) #:prefix ACPI::)) (define (action event) ;; [1] ((@ (ice-9 format) format) #t "device: ~20a, bus ID: ~15a, type: ~8,'0x, data: ~8,'0x\n" (ACPI::event-device-class event) (ACPI::event-bus-id event) (ACPI::event-kind event) (ACPI::event-data event))) ((@ (ice-9 receive) receive) (ACPI::read-selector ACPI::process-events ACPI::close-connection) ;; [3] (ACPI::connect-events) ;; [2] (let loop () ;; [4] (select (list (ACPI::read-selector)) '() '()) (ACPI::process-events action) (loop)) (ACPI::close-connection)) ;; [5]
The action we take on receiving events is defined as action
[1], and
simply writes the data elements of the event to the command-line.
The connection to the kernel is made with the ACPI::connect-events
[2]
procedure call, which gives us the ACPI::read-selector
,
ACPI::process-events
and ACPI::close-connection
[3] procedures
for managing the connection.
We then simply go into a tight loop [4] waiting for data, and processing that when it comes in.
If, by some unattainable miracle, we get out of that loop and want to stop
monitoring for kernel events, we can call the ACPI::close-connection
procedure [5].