X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fevent.cpp;fp=source%2Fevent.cpp;h=b314fca1730543c0697bfd4abd6f11e8572e1a10;hp=0000000000000000000000000000000000000000;hb=1013e3c216cdf8e0ecc0f3b1e8314989b5333818;hpb=5780f0826f057f99593de46a583be7c098efaf90 diff --git a/source/event.cpp b/source/event.cpp new file mode 100644 index 0000000..b314fca --- /dev/null +++ b/source/event.cpp @@ -0,0 +1,58 @@ +/* +This file is part of libmspframework +Copyright © 2006 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ +#include "application.h" +#include "event.h" + +using namespace std; + +namespace Msp { + +EventManager::EventManager(Application &a): + app(a), + next_id(1) +{ + app.add_pollable(&pipe, POLLIN).signal_event.connect(sigc::mem_fun(this, &EventManager::data_available)); +} + +EventManager::Event &EventManager::create_event() +{ + events.push_back(Event(*this, next_id++)); + return events.back(); +} + +void EventManager::data_available(short) +{ + unsigned buf[1024]; + int len=pipe.read((char *)buf, sizeof(buf)); + for(unsigned i=0; i*sizeof(unsigned)<(unsigned)len; ++i) + { + for(list::iterator j=events.begin(); j!=events.end(); ++j) + if(j->get_id()==buf[i]) + j->signal_triggered.emit(); + } +} + +void EventManager::Event::trigger() +{ + mgr.pipe.write((char *)&id, sizeof(id)); +} + +EventManager::Pipe::Pipe() +{ + ::pipe(fd); +} + +int EventManager::Pipe::write(char *buf, unsigned len) +{ + return ::write(fd[1], buf, len); +} + +int EventManager::Pipe::read(char *buf, unsigned len) +{ + return ::read(fd[0], buf, len); +} + +} // namespace Msp