From cc1d49c43523b60f3c428b4ef0e7cd506a20163d Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Wed, 27 Jul 2022 02:32:33 +0300 Subject: [PATCH] add microtask system and temporary logging system --- meson.build | 2 ++ src/Logging.hpp | 5 +++++ src/Window.cpp | 26 +++++++++++++++++++++++++- src/Window.hpp | 4 ++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/Logging.hpp diff --git a/meson.build b/meson.build index 4805e76..124830d 100644 --- a/meson.build +++ b/meson.build @@ -5,6 +5,8 @@ project( ) project_description = 'The Raven user interface library' +add_project_arguments('-g3', language : 'cpp') + cairomm_dep = dependency('cairomm-1.0') pangocairo_dep = dependency('pangocairo') xlib_dep = dependency('x11') diff --git a/src/Logging.hpp b/src/Logging.hpp new file mode 100644 index 0000000..8f808cf --- /dev/null +++ b/src/Logging.hpp @@ -0,0 +1,5 @@ +#pragma once + +#define ERROR std::clog << "[ERROR] (" << __FILE__ << ":" << __LINE__ << "): " +#define INFO std::clog << "[INFO] (" << __FILE__ << ":" << __LINE__ << "): " +#define WARN std::clog << "[WARN] (" << __FILE__ << ":" << __LINE__ << "): " diff --git a/src/Window.cpp b/src/Window.cpp index 794509d..06807b8 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -10,6 +10,7 @@ #include "Window.hpp" #include "Events.hpp" #include "Point.hpp" +#include "Logging.hpp" namespace Raven { @@ -65,7 +66,10 @@ bool Window::dispatch_to_main_widget(Event &event) { if (!m_main_widget) return false; - //std::cout << "Dispatched " << event.name() << " to main widget" << std::endl; + // MouseMove events tend to spam the output + if (event.type() != EventType::MouseMove) { + INFO << "Dispatched event '" << event.name() << "' to main widget" << std::endl; + } m_main_widget->dispatch_event(event); return true; @@ -112,6 +116,7 @@ void Window::reflow() { } void Window::start_batch() { + //INFO << "New batch started. Current count: " << m_batches << std::endl; m_batches++; } @@ -122,6 +127,7 @@ void Window::end_batch() { // we are only interested in performing batch operations for the last batch if (m_batches > 1) { m_batches--; + //INFO << "Consumed batch. Current count: " << m_batches << std::endl; return; } @@ -135,6 +141,7 @@ void Window::end_batch() { m_did_relayout_during_batch = false; m_did_repaint_during_batch = false; + INFO << "Performed all batch actions." << std::endl; } void Window::run(bool block) { @@ -148,10 +155,12 @@ void Window::run(bool block) { switch (e.type) { case MapNotify: { + INFO << "[X11 EVENT] MapNotify" << std::endl; end_batch(); break; } case ConfigureNotify: { + INFO << "[X11 EVENT] ConfigureNotify" << std::endl; if (e.xconfigure.width != m_rect.width() || e.xconfigure.height != m_rect.height()) { m_xlib_surface->set_size(e.xconfigure.width, e.xconfigure.height); m_rect.set_width(e.xconfigure.width); @@ -169,24 +178,29 @@ void Window::run(bool block) { break; } case Expose: { + INFO << "[X11 EVENT] Expose" << std::endl; if (e.xexpose.count == 0) { reflow(); } break; } case ButtonRelease: { + INFO << "[X11 EVENT] ButtonRelease" << std::endl; auto point = Point(e.xbutton.x, e.xbutton.y); auto event = MouseButtonEvent(false, false, false, false, point); dispatch_to_main_widget(event); break; } case ButtonPress: { + INFO << "[X11 EVENT] ButtonPress" << std::endl; auto point = Point(e.xbutton.x, e.xbutton.y); auto event = MouseButtonEvent(e.xbutton.button == Button1, e.xbutton.button == Button2, e.xbutton.button == Button4, e.xbutton.button == Button5, point); dispatch_to_main_widget(event); break; } case MotionNotify: { + // spammy + //INFO << "[X11 EVENT] MotionNotify" << std::endl; auto point = Point(e.xmotion.x, e.xmotion.y); auto event = MouseMoveEvent(point); dispatch_to_main_widget(event); @@ -195,6 +209,16 @@ void Window::run(bool block) { break; } } + + if (XPending(m_x_display) == 0 && !m_microtasks.empty()) { + start_batch(); + while (!m_microtasks.empty()) { + auto callback = m_microtasks.front(); + callback(); + m_microtasks.pop(); + } + end_batch(); + } } } diff --git a/src/Window.hpp b/src/Window.hpp index 74084b5..397376c 100644 --- a/src/Window.hpp +++ b/src/Window.hpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace Raven { @@ -41,6 +42,8 @@ public: void relayout(); void reflow(); + void queue_microtask(std::function callback) { m_microtasks.push(callback); } + Box &rect() { return m_rect; } template @@ -59,6 +62,7 @@ private: int m_batches { 1 }; bool m_did_repaint_during_batch { false }; bool m_did_relayout_during_batch { false }; + std::queue> m_microtasks; Display *m_x_display { nullptr };