ff60e6ff9bc94549e2aaa38acd5b1bc4846486b0
[MonkeyD.git] / palm / lib / epoll.py
1 # Copyright (C) 2008-2009, Eduardo Silva <edsiper@gmail.com>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16
17 from ctypes import *
18
19 # Linux sys/epoll.h values
20 EPOLLIN = 0x001
21 EPOLLPRI = 0x002
22 EPOLLOUT = 0x004
23 EPOLLRDNORM = 0x040
24 EPOLLRDBAND = 0x080
25 EPOLLWRNORM = 0x100
26 EPOLLWRBAND = 0x200
27 EPOLLMSG = 0x400
28 EPOLLERR = 0x008
29 EPOLLHUP = 0x010
30 EPOLLRDHUP = 0x2000
31 EPOLLONESHOT = (1 << 30)
32 EPOLLET = (1 << 31)
33
34 EPOLL_CTL_ADD = 1 
35 EPOLL_CTL_DEL = 2
36 EPOLL_CTL_MOD = 3 
37
38 # epoll data structures
39 class epoll_data(Structure):
40     _fields_ = [("ptr", c_void_p),
41                 ("fd", c_int),
42                 ("u32", c_uint32),
43                 ("u64", c_uint64)]
44
45 class epoll_event(Structure):
46     _fields_ = [("events", c_uint32),
47                 ("data", epoll_data)]
48
49 # Main class
50 class EPoll(object):
51     _LIBC = "libc.so.6"
52
53     def __init__(self):
54         try:
55             self._lib = cdll.LoadLibrary(self._LIBC)
56         except:
57             print "Cannot open %s" % self._LIBC
58             exit(1)
59
60     def epoll_create(self, size):
61         try:
62             n = self._lib.epoll_create(size)
63             return n
64         except:
65             print "Error calling epoll_create()"
66             exit(1)
67
68     def epoll_wait(self, efd, events, max_events, timeout):
69         return self._lib.epoll_wait(efd, events, max_events, timeout)
70
71
72     def epoll_ctl(self, epfd, op, fd, event):
73         return self._lib.epoll_ctl(epfd, op, fd, byref(event))
74
75     def epoll_event(self, events, edata):
76         return byref(epoll_event(events, edata))
77