EventLoopImplementationQt.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/NonnullOwnPtr.h>
  10. #include <AK/OwnPtr.h>
  11. #include <LibCore/EventLoopImplementation.h>
  12. #include <QEvent>
  13. #include <QEventLoop>
  14. #include <QSocketNotifier>
  15. #include <QTimer>
  16. namespace Ladybird {
  17. class EventLoopImplementationQtEventTarget;
  18. class EventLoopManagerQt final : public Core::EventLoopManager {
  19. public:
  20. EventLoopManagerQt();
  21. virtual ~EventLoopManagerQt() override;
  22. virtual NonnullOwnPtr<Core::EventLoopImplementation> make_implementation() override;
  23. virtual intptr_t register_timer(Core::EventReceiver&, int milliseconds, bool should_reload, Core::TimerShouldFireWhenNotVisible) override;
  24. virtual void unregister_timer(intptr_t timer_id) override;
  25. virtual void register_notifier(Core::Notifier&) override;
  26. virtual void unregister_notifier(Core::Notifier&) override;
  27. virtual void did_post_event() override;
  28. static bool event_target_received_event(Badge<EventLoopImplementationQtEventTarget>, QEvent* event);
  29. // FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them.
  30. virtual int register_signal(int, Function<void(int)>) override { return 0; }
  31. virtual void unregister_signal(int) override { }
  32. private:
  33. NonnullOwnPtr<EventLoopImplementationQtEventTarget> m_main_thread_event_target;
  34. };
  35. class QtEventLoopManagerEvent final : public QEvent {
  36. public:
  37. static QEvent::Type process_event_queue_event_type()
  38. {
  39. static auto const type = static_cast<QEvent::Type>(QEvent::registerEventType());
  40. return type;
  41. }
  42. QtEventLoopManagerEvent(QEvent::Type type)
  43. : QEvent(type)
  44. {
  45. }
  46. };
  47. class EventLoopImplementationQt final : public Core::EventLoopImplementation {
  48. public:
  49. static NonnullOwnPtr<EventLoopImplementationQt> create() { return adopt_own(*new EventLoopImplementationQt); }
  50. virtual ~EventLoopImplementationQt() override;
  51. virtual int exec() override;
  52. virtual size_t pump(PumpMode) override;
  53. virtual void quit(int) override;
  54. virtual void wake() override;
  55. virtual void post_event(Core::EventReceiver& receiver, NonnullOwnPtr<Core::Event>&&) override;
  56. // FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them.
  57. virtual void unquit() override { }
  58. virtual bool was_exit_requested() const override { return false; }
  59. virtual void notify_forked_and_in_child() override { }
  60. void set_main_loop() { m_main_loop = true; }
  61. private:
  62. friend class EventLoopManagerQt;
  63. EventLoopImplementationQt();
  64. bool is_main_loop() const { return m_main_loop; }
  65. QEventLoop m_event_loop;
  66. bool m_main_loop { false };
  67. };
  68. }