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);
59 context.removeTimer(this);
63 void eTimer::changeInterval(long msek)
65 if (bActive) // Timer is running?
67 context.removeTimer(this); // then stop
68 nextActivation -= interval; // sub old interval
71 bActive=true; // then activate Timer
73 interval = msek; // set new Interval
74 nextActivation += interval; // calc nextActivation
76 context.addTimer(this); // add Timer to context TimerList
79 void eTimer::activate() // Internal Function... called from eApplication
82 gettimeofday(&now, 0);
83 // 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 );
84 // eDebug("Timer emitted");
85 context.removeTimer(this);
89 nextActivation += interval;
90 context.addTimer(this);
100 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
102 notifiers.insert(std::pair<int,eSocketNotifier*> (sn->getFD(), sn));
105 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
107 notifiers.erase(sn->getFD());
110 void eMainloop::processOneEvent()
113 - we should use epoll(4)
114 - timer are checked twice. there was a strong reason for it, but i can't remember. (FIXME)
115 - for each time, we gettimeofday() and check wether the timer should fire.
116 we should do this all better - we know how long the poll last, so we know which
117 timers should fire. Problem is that a timer handler could have required so
118 much time that another timer fired.
120 A probably structure could look
124 time = gettimeofday()
125 timeout = calculate_pending_timers(time);
127 doPoll(timeout or infinite);
129 if (poll_had_results)
130 handle_poll_handler();
132 fire_timers(time + timeout)
135 the gettimeofday() call is required because fire_timers could last more
138 when poll did no timeout, we don't handle timers, as this will be done
139 in the next iteration (without adding overhead - we had to get the new
143 // first, process pending timers...
146 while (TimerList && (usec = timeout_usec( TimerList.begin()->getNextActivation() ) ) <= 0 )
147 TimerList.begin()->activate();
149 // build the poll aray
150 int fdAnz = notifiers.size();
151 pollfd* pfd = new pollfd[fdAnz]; // make new pollfd array
153 std::map<int,eSocketNotifier*>::iterator it(notifiers.begin());
154 for (int i=0; i < fdAnz; i++, it++)
156 pfd[i].fd = it->first;
157 pfd[i].events = it->second->getRequested();
160 // to the poll. When there are no timers, we have an infinite timeout
161 int ret=poll(pfd, fdAnz, TimerList ? usec / 1000 : -1); // convert to us
165 for (int i=0; i < fdAnz ; i++)
167 if( notifiers.find(pfd[i].fd) == notifiers.end())
170 int req = notifiers[pfd[i].fd]->getRequested();
172 if ( pfd[i].revents & req )
174 notifiers[pfd[i].fd]->activate(pfd[i].revents);
178 } else if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
179 eFatal("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d) -> FIX YOUR CODE", pfd[i].fd,pfd[i].revents);
183 /* when we got a signal, we get EINTR. we do not care,
184 because we check current time in timers anyway. */
186 eDebug("poll made error (%m)");
190 while ( TimerList && timeout_usec( TimerList.begin()->getNextActivation() ) <= 0 )
191 TimerList.begin()->activate();
197 int eMainloop::exec()
201 app_quit_now = false;
207 /* use with care! better: don't use it anymore. it was used for gui stuff, but
208 doesn't allow multiple paths (or active dialogs, if you want it that way.) */
209 void eMainloop::enter_loop()
213 // Status der vorhandenen Loop merken
214 bool old_exit_loop = app_exit_loop;
216 app_exit_loop = false;
218 while (!app_exit_loop && !app_quit_now)
223 // wiederherstellen der vorherigen app_exit_loop
224 app_exit_loop = old_exit_loop;
230 // do something here on exit the last loop
234 void eMainloop::exit_loop() // call this to leave the current loop
236 app_exit_loop = true;
239 void eMainloop::quit( int ret ) // call this to leave all loops
245 eApplication* eApp = 0;