2018-05-03 16:15:00 -06:00
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
2018-06-19 18:07:31 +00:00
|
|
|
#include <algorithm>
|
2018-05-03 16:15:00 -06:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "iwaitable.hpp"
|
|
|
|
|
2018-06-14 17:50:01 -06:00
|
|
|
typedef Result (*EventCallback)(void *arg, Handle *handles, size_t num_handles, u64 timeout);
|
2018-05-03 16:15:00 -06:00
|
|
|
|
2018-05-03 23:58:25 -06:00
|
|
|
class IEvent : public IWaitable {
|
2018-05-03 16:15:00 -06:00
|
|
|
protected:
|
|
|
|
std::vector<Handle> handles;
|
|
|
|
EventCallback callback;
|
2018-06-14 17:50:01 -06:00
|
|
|
void *arg;
|
2018-05-03 16:15:00 -06:00
|
|
|
|
|
|
|
public:
|
2018-06-14 17:50:01 -06:00
|
|
|
IEvent(Handle wait_h, void *a, EventCallback callback) {
|
2018-05-03 16:15:00 -06:00
|
|
|
if (wait_h) {
|
|
|
|
this->handles.push_back(wait_h);
|
|
|
|
}
|
2018-06-14 17:50:01 -06:00
|
|
|
this->arg = a;
|
2018-05-03 16:15:00 -06:00
|
|
|
this->callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
~IEvent() {
|
2018-06-19 18:07:31 +00:00
|
|
|
std::for_each(handles.begin(), handles.end(), svcCloseHandle);
|
2018-05-03 16:15:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result signal_event() = 0;
|
|
|
|
|
2018-06-12 16:00:09 -06:00
|
|
|
/* IWaitable */
|
2018-05-03 16:15:00 -06:00
|
|
|
virtual Handle get_handle() {
|
|
|
|
if (handles.size() > 0) {
|
|
|
|
return this->handles[0];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void handle_deferred() {
|
|
|
|
/* TODO: Panic, because we can never defer an event. */
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result handle_signaled(u64 timeout) {
|
2018-06-14 17:50:01 -06:00
|
|
|
return this->callback(this->arg, this->handles.data(), this->handles.size(), timeout);
|
2018-05-03 16:15:00 -06:00
|
|
|
}
|
|
|
|
|
2018-06-14 23:32:01 -06:00
|
|
|
static Result PanicCallback(void *arg, Handle *handles, size_t num_handles, u64 timeout) {
|
2018-05-03 16:15:00 -06:00
|
|
|
/* TODO: Panic. */
|
|
|
|
return 0xCAFE;
|
|
|
|
}
|
2018-06-19 18:07:31 +00:00
|
|
|
};
|