… | |
… | |
23 | #include <cassert> |
23 | #include <cassert> |
24 | |
24 | |
25 | #include "rxvtvec.h" |
25 | #include "rxvtvec.h" |
26 | #include "callback.h" |
26 | #include "callback.h" |
27 | |
27 | |
28 | typedef double tstamp; |
28 | #define IOM_IO 1 |
|
|
29 | #define IOM_TIME 1 |
|
|
30 | #undef IOM_CHECK |
29 | |
31 | |
|
|
32 | #if IOM_IO |
|
|
33 | typedef double tstamp; |
30 | extern tstamp NOW; |
34 | extern tstamp NOW; |
31 | |
35 | |
32 | struct io_watcher; |
36 | struct io_watcher; |
|
|
37 | #endif |
|
|
38 | #if IOM_TIME |
33 | struct time_watcher; |
39 | struct time_watcher; |
|
|
40 | #endif |
|
|
41 | #if IOM_CHECK |
|
|
42 | struct check_watcher; |
|
|
43 | #endif |
34 | |
44 | |
35 | class io_manager { |
45 | class io_manager { |
|
|
46 | #if IOM_IO |
36 | simplevec<io_watcher *> iow; |
47 | simplevec<io_watcher *> iow; |
|
|
48 | #endif |
|
|
49 | #if IOM_CHECK |
|
|
50 | simplevec<check_watcher *> cw; |
|
|
51 | #endif |
|
|
52 | #if IOM_TIME |
37 | simplevec<time_watcher *> tw; // actually a heap |
53 | simplevec<time_watcher *> tw; |
38 | |
54 | |
39 | void idle_cb (time_watcher &w); time_watcher *idle; |
55 | void idle_cb (time_watcher &w); time_watcher *idle; |
|
|
56 | #endif |
|
|
57 | |
|
|
58 | template<class watcher> |
|
|
59 | void reg (watcher *w, simplevec<watcher *> &queue); |
|
|
60 | |
|
|
61 | template<class watcher> |
|
|
62 | void unreg (watcher *w, simplevec<watcher *> &queue); |
|
|
63 | |
40 | public: |
64 | public: |
41 | // register a watcher |
65 | // register a watcher |
42 | void reg (io_watcher *w); |
66 | #if IOM_IO |
43 | void unreg (io_watcher *w); |
67 | void reg (io_watcher *w); void unreg (io_watcher *w); |
44 | void reg (time_watcher *w); |
68 | #endif |
45 | void unreg (time_watcher *w); |
69 | #if IOM_TIME |
|
|
70 | void reg (time_watcher *w); void unreg (time_watcher *w); |
|
|
71 | #endif |
|
|
72 | #if IOM_CHECK |
|
|
73 | void reg (check_watcher *w); void unreg (check_watcher *w); |
|
|
74 | #endif |
46 | |
75 | |
47 | void loop (); |
76 | void loop (); |
48 | |
77 | |
49 | io_manager (); |
78 | io_manager (); |
50 | ~io_manager (); |
79 | ~io_manager (); |
51 | }; |
80 | }; |
52 | |
81 | |
53 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
82 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
54 | |
83 | |
|
|
84 | #if IOM_IO |
55 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
85 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
56 | |
86 | |
57 | struct io_watcher : callback2<void, io_watcher &, short> { |
87 | struct io_watcher : callback2<void, io_watcher &, short> { |
58 | int fd; |
88 | int fd; |
59 | short events; |
89 | short events; |
… | |
… | |
69 | |
99 | |
70 | void set(short events_) { set (fd, events_); } |
100 | void set(short events_) { set (fd, events_); } |
71 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
101 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
72 | void stop () { iom.unreg (this); } |
102 | void stop () { iom.unreg (this); } |
73 | }; |
103 | }; |
|
|
104 | #endif |
74 | |
105 | |
75 | #define TSTAMP_CANCEL -1. |
106 | #if IOM_TIME |
|
|
107 | enum { TSTAMP_CANCEL = -1 }; |
76 | |
108 | |
77 | struct time_watcher : callback1<void, time_watcher &> { |
109 | struct time_watcher : callback1<void, time_watcher &> { |
78 | tstamp at; |
110 | tstamp at; |
79 | |
111 | |
80 | template<class O1, class O2> |
112 | template<class O1, class O2> |
… | |
… | |
96 | { |
128 | { |
97 | stop (); |
129 | stop (); |
98 | at = when; |
130 | at = when; |
99 | } |
131 | } |
100 | }; |
132 | }; |
|
|
133 | #endif |
|
|
134 | |
|
|
135 | #if IOM_CHECK |
|
|
136 | // run before checking for new events |
|
|
137 | struct check_watcher : callback1<void, check_watcher &> { |
|
|
138 | template<class O1, class O2> |
|
|
139 | check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
|
|
140 | : callback1<void, check_watcher &>(object,method) |
|
|
141 | { } |
|
|
142 | |
|
|
143 | ~check_watcher (); |
|
|
144 | |
|
|
145 | void start () { iom.reg (this); } |
|
|
146 | void stop () { iom.unreg (this); } |
|
|
147 | }; |
|
|
148 | #endif |
101 | |
149 | |
102 | #endif |
150 | #endif |
103 | |
151 | |