]> git.tdb.fi Git - libs/core.git/blob - event.cpp
01b8542a6225c2d0fb7cd703e4bb32d93283ce32
[libs/core.git] / event.cpp
1 /*
2 This file is part of libmspframework
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #ifdef WIN32
7 #include <io.h>
8 #include <fcntl.h>
9 #endif
10
11 #include "application.h"
12 #include "event.h"
13
14 using namespace std;
15
16 namespace Msp {
17
18 EventManager::EventManager(Application &a):
19         app(a),
20         next_id(1)
21 {
22         app.add_pollable(&pipe, POLLIN).signal_event.connect(sigc::mem_fun(this, &EventManager::data_available));
23 }
24
25 EventManager::Event &EventManager::create_event()
26 {
27         events.push_back(Event(*this, next_id++));
28         return events.back();
29 }
30
31 void EventManager::data_available(short)
32 {
33         unsigned buf[1024];
34         int len=pipe.read((char *)buf, sizeof(buf));
35         for(unsigned i=0; i*sizeof(unsigned)<(unsigned)len; ++i)
36         {
37                 for(list<Event>::iterator j=events.begin(); j!=events.end(); ++j)
38                         if(j->get_id()==buf[i])
39                                 j->signal_triggered.emit();
40         }
41 }
42
43 void EventManager::Event::trigger()
44 {
45         mgr.pipe.write((char *)&id, sizeof(id));
46 }
47
48 EventManager::Pipe::Pipe()
49 {
50 #ifdef WIN32
51         _pipe(fd, 1024, _O_BINARY);
52 #else
53         ::pipe(fd);
54 #endif
55 }
56
57 int EventManager::Pipe::write(char *buf, unsigned len)
58 {
59 #ifdef WIN32
60         return _write(fd[1], buf, len);
61 #else
62         return ::write(fd[1], buf, len);
63 #endif
64 }
65
66 int EventManager::Pipe::read(char *buf, unsigned len)
67 {
68 #ifdef WIN32
69         return _read(fd[0], buf, len);
70 #else
71         return ::read(fd[0], buf, len);
72 #endif
73 }
74
75 } // namespace Msp