1 | #| |
---|
2 | Copyright (c) 2006 Risto Laakso |
---|
3 | All rights reserved. |
---|
4 | |
---|
5 | Redistribution and use in source and binary forms, with or without |
---|
6 | modification, are permitted provided that the following conditions |
---|
7 | are met: |
---|
8 | 1. Redistributions of source code must retain the above copyright |
---|
9 | notice, this list of conditions and the following disclaimer. |
---|
10 | 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | notice, this list of conditions and the following disclaimer in the |
---|
12 | documentation and/or other materials provided with the distribution. |
---|
13 | 3. The name of the author may not be used to endorse or promote products |
---|
14 | derived from this software without specific prior written permission. |
---|
15 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
17 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |# |
---|
27 | (in-package :event-notification) |
---|
28 | |
---|
29 | #+(or darwin macosx freebsd) |
---|
30 | (progn |
---|
31 | |
---|
32 | (defun make-event-queue () |
---|
33 | (%kqueue)) |
---|
34 | |
---|
35 | (defun ev-set (kevent ident filter flags fflags data udata) |
---|
36 | (setf (foreign-slot-value kevent 'kevent 'ident) ident |
---|
37 | (foreign-slot-value kevent 'kevent 'filter) filter |
---|
38 | (foreign-slot-value kevent 'kevent 'flags) flags |
---|
39 | (foreign-slot-value kevent 'kevent 'fflags) fflags |
---|
40 | (foreign-slot-value kevent 'kevent 'data) data |
---|
41 | (foreign-slot-value kevent 'kevent 'udata) udata)) |
---|
42 | |
---|
43 | |
---|
44 | (defun add-fd (event-queue fd mode &optional (trigger :edge)) |
---|
45 | "Add FD to EVENT-QUEUE in MODE (or :read :write)." |
---|
46 | (with-foreign-object (ke 'kevent) |
---|
47 | (ev-set ke fd |
---|
48 | (ecase mode (:read +evfilt-read+) (:write +evfilt-write+)) |
---|
49 | (logior +ev-add+ (if (eql :edge trigger) +ev-clear+ 0)) |
---|
50 | 0 0 (null-pointer)) |
---|
51 | (%kevent event-queue ke 1 (null-pointer) 0 (null-pointer))) |
---|
52 | ) |
---|
53 | |
---|
54 | (defun remove-fd (event-queue fd mode) |
---|
55 | "Remove FD from EVENT-QUEUE in MODE (or :read :write)." |
---|
56 | (with-foreign-object (ke 'kevent) |
---|
57 | (ev-set ke fd |
---|
58 | (ecase mode (:read +evfilt-read+) (:write +evfilt-write+)) |
---|
59 | +ev-delete+ 0 0 (null-pointer)) |
---|
60 | (%kevent event-queue ke 1 (null-pointer) 0 (null-pointer)))) |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | (define-condition poll-error (error) ()) |
---|
65 | |
---|
66 | (defun poll-events (event-queue) |
---|
67 | |
---|
68 | (with-foreign-object (ke 'kevent 10) |
---|
69 | (memzero ke (* +kevent-size+ 10)) |
---|
70 | (loop for res = (%kevent event-queue (null-pointer) 0 ke 10 (null-pointer)) do |
---|
71 | (case res |
---|
72 | (-1 (error 'poll-error)) |
---|
73 | (0 nil) |
---|
74 | (t |
---|
75 | (let ((idents nil)) |
---|
76 | (loop for i from 0 below res do |
---|
77 | (push (foreign-slot-value (mem-aref ke 'kevent i) 'kevent 'ident) idents)) |
---|
78 | (return idents))))))) |
---|
79 | |
---|
80 | ) |
---|