-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support emscripten build #1145
Open
eagleoflqj
wants to merge
2
commits into
fcitx:master
Choose a base branch
from
eagleoflqj:emscripten
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
support emscripten build #1145
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include <emscripten.h> | ||
#include "event.h" | ||
|
||
namespace fcitx { | ||
|
||
template <typename Interface> | ||
struct JSEventSourceBase : public Interface { | ||
public: | ||
~JSEventSourceBase() override {} | ||
|
||
bool isEnabled() const override { return enabled_; } | ||
|
||
void setEnabled(bool enabled) override { enabled_ = enabled; } | ||
|
||
bool isOneShot() const override { return oneShot_; } | ||
|
||
void setOneShot() override { oneShot_ = true; } | ||
|
||
private: | ||
bool enabled_ = false; | ||
bool oneShot_ = false; | ||
}; | ||
|
||
struct JSEventSource : public JSEventSourceBase<EventSource> { | ||
JSEventSource(EventCallback _callback) | ||
: callback_(std::make_shared<EventCallback>(std::move(_callback))) {} | ||
|
||
std::shared_ptr<EventCallback> callback_; | ||
}; | ||
|
||
struct JSEventSourceIO : public JSEventSourceBase<EventSourceIO> { | ||
JSEventSourceIO(IOCallback _callback) {} | ||
|
||
int fd() const override { return 0; } | ||
|
||
void setFd(int fd) override {} | ||
|
||
IOEventFlags events() const override { return IOEventFlag::In; } | ||
|
||
void setEvents(IOEventFlags flags) override {} | ||
|
||
IOEventFlags revents() const override { return IOEventFlag::In; } | ||
}; | ||
|
||
void TimeEventCallback(void *arg); | ||
|
||
struct JSEventSourceTime : public JSEventSourceBase<EventSourceTime> { | ||
JSEventSourceTime(TimeCallback _callback, uint64_t time, clockid_t clockid) | ||
: callback_(std::make_shared<TimeCallback>(std::move(_callback))), | ||
time_(time), clockid_(clockid) { | ||
setOneShot(); | ||
} | ||
|
||
void setOneShot() override { | ||
int t = std::max<int64_t>(0, time_ - now(CLOCK_MONOTONIC)) / 1000; | ||
emscripten_async_call(TimeEventCallback, this, t); | ||
} | ||
|
||
uint64_t time() const override { return time_; } | ||
|
||
void setTime(uint64_t time) override { time_ = time; } | ||
|
||
uint64_t accuracy() const override { return 0; } | ||
|
||
void setAccuracy(uint64_t time) override {} | ||
|
||
clockid_t clock() const override { return clockid_; } | ||
|
||
std::shared_ptr<TimeCallback> callback_; | ||
|
||
private: | ||
uint64_t time_; | ||
clockid_t clockid_; | ||
}; | ||
|
||
void TimeEventCallback(void *arg) { | ||
auto source = static_cast<JSEventSourceTime *>(arg); | ||
(*source->callback_)(source, source->time()); | ||
} | ||
|
||
class EventLoopPrivate { | ||
public: | ||
EventLoopPrivate() {} | ||
|
||
~EventLoopPrivate() {} | ||
}; | ||
|
||
EventLoop::EventLoop() : d_ptr(std::make_unique<EventLoopPrivate>()) {} | ||
|
||
EventLoop::~EventLoop() = default; | ||
|
||
const char *EventLoop::impl() { return "js-event"; } | ||
|
||
void *EventLoop::nativeHandle() { return nullptr; } | ||
|
||
bool EventLoop::exec() { return true; } | ||
|
||
void EventLoop::exit() {} | ||
|
||
std::unique_ptr<EventSourceIO> EventLoop::addIOEvent(int fd, IOEventFlags flags, | ||
IOCallback callback) { | ||
auto source = std::make_unique<JSEventSourceIO>(std::move(callback)); | ||
return source; | ||
} | ||
|
||
std::unique_ptr<EventSourceTime> | ||
EventLoop::addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy, | ||
TimeCallback callback) { | ||
auto source = | ||
std::make_unique<JSEventSourceTime>(std::move(callback), usec, clock); | ||
return source; | ||
} | ||
|
||
std::unique_ptr<EventSource> EventLoop::addExitEvent(EventCallback callback) { | ||
auto source = std::make_unique<JSEventSource>(std::move(callback)); | ||
return source; | ||
} | ||
|
||
std::unique_ptr<EventSource> EventLoop::addDeferEvent(EventCallback callback) { | ||
return addTimeEvent( | ||
CLOCK_MONOTONIC, now(CLOCK_MONOTONIC), 0, | ||
[callback = std::move(callback)](EventSourceTime *source, uint64_t) { | ||
return callback(source); | ||
}); | ||
} | ||
|
||
std::unique_ptr<EventSource> EventLoop::addPostEvent(EventCallback callback) { | ||
return addTimeEvent( | ||
CLOCK_MONOTONIC, now(CLOCK_MONOTONIC), 0, | ||
[callback = std::move(callback)](EventSourceTime *source, uint64_t) { | ||
return callback(source); | ||
}); | ||
} | ||
} // namespace fcitx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like having an external file dependency.
I think we could somehow, create a stub for EventLoop and having a global factory class to allow external event loop implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea how to achieve that with a factory.
Added the event_js.cpp, since there is nothing specific to fcitx5-js.
emscripten_async_call
is generic API https://emscripten.org/docs/api_reference/emscripten.h.html#c.emscripten_async_callThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without breaking the API, here's what you can do.
In other words, use Dependency injection to make your js eventloop implementation live outside of the main repo.