]> git.tdb.fi Git - libs/core.git/blobdiff - source/event.h
Add files
[libs/core.git] / source / event.h
diff --git a/source/event.h b/source/event.h
new file mode 100644 (file)
index 0000000..091d637
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+This file is part of libmspframework
+Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+#ifndef MSP_FRAMEWORK_EVENT_H_
+#define MSP_FRAMEWORK_EVENT_H_
+
+#include <sigc++/sigc++.h>
+#include "pollable.h"
+
+namespace Msp {
+
+class Application;
+
+/**
+Events can be used in multi-threaded applictions to trigger actions in the main
+thread.
+*/
+class EventManager
+{
+public:
+       class Event
+       {
+       public:
+               sigc::signal<void> signal_triggered;
+
+               Event(EventManager &m, unsigned i): mgr(m), id(i) { }
+               unsigned get_id() const { return id; }
+               void trigger();
+       private:
+               EventManager &mgr;
+               unsigned id;
+       };
+
+       EventManager(Application &);
+       Event &create_event();
+private:
+       class Pipe: public Pollable
+       {
+       public:
+               Pipe();
+               int write(char *, unsigned);
+               int read(char *, unsigned);
+       private:
+               int fd[2];
+       
+               int get_fd() { return fd[0]; }
+       };
+
+       Application &app;
+       Pipe        pipe;
+       unsigned    next_id;
+       std::list<Event> events;
+
+       void data_available(short);
+};
+
+} // namespace Msp
+
+#endif