1 #include <lib/base/ebase.h>
7 #include <lib/base/eerror.h>
9 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
15 eSocketNotifier::~eSocketNotifier()
20 void eSocketNotifier::start()
25 context.addSocketNotifier(this);
29 void eSocketNotifier::stop()
32 context.removeSocketNotifier(this);
38 void eTimer::start(long msek, bool singleShot)
44 bSingleShot = singleShot;
46 gettimeofday(&nextActivation, 0);
47 // eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, msek);
48 nextActivation += (msek<0 ? 0 : msek);
49 // eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
50 context.addTimer(this);
58 context.removeTimer(this);
62 void eTimer::changeInterval(long msek)
64 if (bActive) // Timer is running?
66 context.removeTimer(this); // then stop
67 nextActivation -= interval; // sub old interval
70 bActive=true; // then activate Timer
72 interval = msek; // set new Interval
73 nextActivation += interval; // calc nextActivation
75 context.addTimer(this); // add Timer to context TimerList
78 void eTimer::activate() // Internal Function... called from eApplication
81 gettimeofday(&now, 0);
82 // eDebug("this = %p\nnow sec = %d, usec = %d\nnextActivation sec = %d, usec = %d", this, now.tv_sec, now.tv_usec, nextActivation.tv_sec, nextActivation.tv_usec );
83 // eDebug("Timer emitted");
84 context.removeTimer(this);
88 nextActivation += interval;
89 context.addTimer(this);
99 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
101 notifiers.insert(std::pair<int,eSocketNotifier*> (sn->getFD(), sn));
104 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
106 notifiers.erase(sn->getFD());
109 void eMainloop::processOneEvent()
112 - we should use epoll(4)
113 - timer are checked twice. there was a strong reason for it, but i can't remember. (FIXME)
114 - for each time, we gettimeofday() and check wether the timer should fire.
115 we should do this all better - we know how long the poll last, so we know which
116 timers should fire. Problem is that a timer handler could have required so
117 much time that another timer fired.
119 A probably structure could look
123 time = gettimeofday()
124 timeout = calculate_pending_timers(time);
126 doPoll(timeout or infinite);
128 if (poll_had_results)
129 handle_poll_handler();
131 fire_timers(time + timeout)
134 the gettimeofday() call is required because fire_timers could last more
137 when poll did no timeout, we don't handle timers, as this will be done
138 in the next iteration (without adding overhead - we had to get the new
142 // first, process pending timers...
145 while (TimerList && (usec = timeout_usec( TimerList.begin()->getNextActivation() ) ) <= 0 )
146 TimerList.begin()->activate();
148 // build the poll aray
149 int fdAnz = notifiers.size();
150 pollfd* pfd = new pollfd[fdAnz]; // make new pollfd array
152 std::map<int,eSocketNotifier*>::iterator it(notifiers.begin());
153 for (int i=0; i < fdAnz; i++, it++)
155 pfd[i].fd = it->first;
156 pfd[i].events = it->second->getRequested();
159 // to the poll. When there are no timers, we have an infinite timeout
160 int ret=poll(pfd, fdAnz, TimerList ? usec / 1000 : -1); // convert to us
164 for (int i=0; i < fdAnz ; i++)
166 if( notifiers.find(pfd[i].fd) == notifiers.end())
169 int req = notifiers[pfd[i].fd]->getRequested();
171 if ( pfd[i].revents & req )
173 notifiers[pfd[i].fd]->activate(pfd[i].revents);
177 } else if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
178 eFatal("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d) -> FIX YOUR CODE", pfd[i].fd,pfd[i].revents);
182 /* when we got a signal, we get EINTR. we do not care,
183 because we check current time in timers anyway. */
185 eDebug("poll made error (%m)");
189 while ( TimerList && timeout_usec( TimerList.begin()->getNextActivation() ) <= 0 )
190 TimerList.begin()->activate();
196 int eMainloop::exec()
200 app_quit_now = false;
206 /* use with care! better: don't use it anymore. it was used for gui stuff, but
207 doesn't allow multiple paths (or active dialogs, if you want it that way.) */
208 void eMainloop::enter_loop()
212 // Status der vorhandenen Loop merken
213 bool old_exit_loop = app_exit_loop;
215 app_exit_loop = false;
217 while (!app_exit_loop && !app_quit_now)
222 // wiederherstellen der vorherigen app_exit_loop
223 app_exit_loop = old_exit_loop;
229 // do something here on exit the last loop
233 void eMainloop::exit_loop() // call this to leave the current loop
235 app_exit_loop = true;
238 void eMainloop::quit( int ret ) // call this to leave all loops
244 eApplication* eApp = 0;