1 #include <lib/base/ebase.h>
6 #include <lib/base/eerror.h>
8 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
14 eSocketNotifier::~eSocketNotifier()
19 void eSocketNotifier::start()
24 context.addSocketNotifier(this);
28 void eSocketNotifier::stop()
31 context.removeSocketNotifier(this);
37 void eTimer::start(long msek, bool singleShot)
43 bSingleShot = singleShot;
45 gettimeofday(&nextActivation, 0);
46 // eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, msek);
47 nextActivation += (msek<0 ? 0 : msek);
48 // eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
49 context.addTimer(this);
57 context.removeTimer(this);
61 void eTimer::changeInterval(long msek)
63 if (bActive) // Timer is running?
65 context.removeTimer(this); // then stop
66 nextActivation -= interval; // sub old interval
69 bActive=true; // then activate Timer
71 interval = msek; // set new Interval
72 nextActivation += interval; // calc nextActivation
74 context.addTimer(this); // add Timer to context TimerList
77 void eTimer::activate() // Internal Funktion... called from eApplication
80 gettimeofday(&now, 0);
81 // 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 );
82 // eDebug("Timer emitted");
83 context.removeTimer(this);
87 nextActivation += interval;
88 context.addTimer(this);
98 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
100 notifiers.insert(std::pair<int,eSocketNotifier*> (sn->getFD(), sn));
103 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
105 notifiers.erase(sn->getFD());
108 void eMainloop::processOneEvent()
110 // process pending timers...
113 while (TimerList && (usec = timeout_usec( TimerList.begin()->getNextActivation() ) ) <= 0 )
114 TimerList.begin()->activate();
116 int fdAnz = notifiers.size();
117 pollfd* pfd = new pollfd[fdAnz]; // make new pollfd array
120 std::map<int,eSocketNotifier*>::iterator it(notifiers.begin());
121 for (int i=0; i < fdAnz; i++, it++)
123 pfd[i].fd = it->first;
124 pfd[i].events = it->second->getRequested();
127 int ret=poll(pfd, fdAnz, TimerList ? usec / 1000 : -1); // milli .. not micro seks
131 // eDebug("bin aussem poll raus und da war was");
132 for (int i=0; i < fdAnz ; i++)
134 if( notifiers.find(pfd[i].fd) == notifiers.end())
137 int req = notifiers[pfd[i].fd]->getRequested();
139 if ( pfd[i].revents & req )
141 notifiers[pfd[i].fd]->activate(pfd[i].revents);
145 } else if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
146 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd,pfd[i].revents);
150 eDebug("poll made error");
153 while ( TimerList && timeout_usec( TimerList.begin()->getNextActivation() ) <= 0 )
154 TimerList.begin()->activate();
160 int eMainloop::exec()
164 app_quit_now = false;
170 void eMainloop::enter_loop()
174 // Status der vorhandenen Loop merken
175 bool old_exit_loop = app_exit_loop;
177 app_exit_loop = false;
179 while (!app_exit_loop && !app_quit_now)
184 // wiederherstellen der vorherigen app_exit_loop
185 app_exit_loop = old_exit_loop;
191 // do something here on exit the last loop
195 void eMainloop::exit_loop() // call this to leave the current loop
197 app_exit_loop = true;
200 void eMainloop::quit( int ret ) // call this to leave all loops
206 eApplication* eApp = 0;