WSEventLoop: Remove inheritance from CEventLoop

The only reason for the inheritance was to add FDs to the select set.

Since CNotifier is available (and now also quite useful), we can make use of it
instead, and remove the inheritance.
This commit is contained in:
Robin Burchell 2019-07-17 11:09:09 +02:00 committed by Andreas Kling
parent 6eaa6826fa
commit 3837de0573
Notes: sideshowbarker 2024-07-19 13:12:38 +09:00
6 changed files with 30 additions and 45 deletions

View File

@ -40,6 +40,12 @@ WSEventLoop::WSEventLoop()
m_server_notifier = make<CNotifier>(m_server_sock.fd(), CNotifier::Read);
m_server_notifier->on_ready_to_read = [this] { drain_server(); };
m_keyboard_notifier = make<CNotifier>(m_keyboard_fd, CNotifier::Read);
m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); };
m_mouse_notifier = make<CNotifier>(m_mouse_fd, CNotifier::Read);
m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); };
}
WSEventLoop::~WSEventLoop()
@ -57,7 +63,7 @@ void WSEventLoop::drain_server()
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
new WSClientConnection(client_fd, client_id);
CIPCServerSideClientCreator<WSClientConnection>(client_fd, client_id);
}
}
@ -105,22 +111,3 @@ void WSEventLoop::drain_keyboard()
}
}
void WSEventLoop::add_file_descriptors_for_select(fd_set& fds, int& max_fd_added)
{
auto add_fd_to_set = [&max_fd_added](int fd, auto& set) {
FD_SET(fd, &set);
if (fd > max_fd_added)
max_fd_added = fd;
};
add_fd_to_set(m_keyboard_fd, fds);
add_fd_to_set(m_mouse_fd, fds);
}
void WSEventLoop::process_file_descriptors_after_select(const fd_set& fds)
{
if (FD_ISSET(m_keyboard_fd, &fds))
drain_keyboard();
if (FD_ISSET(m_mouse_fd, &fds))
drain_mouse();
}

View File

@ -8,25 +8,23 @@
class WSClientConnection;
struct WSAPI_ClientMessage;
class WSEventLoop : public CEventLoop {
class WSEventLoop {
public:
WSEventLoop();
virtual ~WSEventLoop() override;
virtual ~WSEventLoop();
static WSEventLoop& the() { return static_cast<WSEventLoop&>(CEventLoop::current()); }
int exec() { return m_event_loop.exec(); }
private:
virtual void add_file_descriptors_for_select(fd_set&, int& max_fd_added) override;
virtual void process_file_descriptors_after_select(const fd_set&) override;
void drain_server();
void drain_mouse();
void drain_keyboard();
void drain_client(WSClientConnection&);
bool on_receive_from_client(int client_id, const WSAPI_ClientMessage&, ByteBuffer&& extra_data);
CEventLoop m_event_loop;
int m_keyboard_fd { -1 };
OwnPtr<CNotifier> m_keyboard_notifier;
int m_mouse_fd { -1 };
OwnPtr<CNotifier> m_mouse_notifier;
CLocalSocket m_server_sock;
OwnPtr<CNotifier> m_server_notifier;
};

View File

@ -71,19 +71,19 @@ void WSScreen::on_receive_mouse_data(int dx, int dy, int dz, unsigned buttons)
if (!(changed_buttons & (unsigned)button))
return;
auto message = make<WSMouseEvent>(buttons & (unsigned)button ? WSEvent::MouseDown : WSEvent::MouseUp, m_cursor_location, buttons, button, m_modifiers);
WSEventLoop::the().post_event(WSWindowManager::the(), move(message));
CEventLoop::current().post_event(WSWindowManager::the(), move(message));
};
post_mousedown_or_mouseup_if_needed(MouseButton::Left);
post_mousedown_or_mouseup_if_needed(MouseButton::Right);
post_mousedown_or_mouseup_if_needed(MouseButton::Middle);
if (m_cursor_location != prev_location) {
auto message = make<WSMouseEvent>(WSEvent::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
WSEventLoop::the().post_event(WSWindowManager::the(), move(message));
CEventLoop::current().post_event(WSWindowManager::the(), move(message));
}
if (dz) {
auto message = make<WSMouseEvent>(WSEvent::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, dz);
WSEventLoop::the().post_event(WSWindowManager::the(), move(message));
CEventLoop::current().post_event(WSWindowManager::the(), move(message));
}
if (m_cursor_location != prev_location)
@ -94,7 +94,7 @@ void WSScreen::on_receive_keyboard_data(KeyEvent kernel_event)
{
m_modifiers = kernel_event.modifiers();
auto message = make<WSKeyEvent>(kernel_event.is_press() ? WSEvent::KeyDown : WSEvent::KeyUp, kernel_event.key, kernel_event.character, kernel_event.modifiers());
WSEventLoop::the().post_event(WSWindowManager::the(), move(message));
CEventLoop::current().post_event(WSWindowManager::the(), move(message));
}
void WSScreen::set_y_offset(int offset)

View File

@ -171,7 +171,7 @@ void WSWindow::set_maximized(bool maximized)
set_rect(m_unmaximized_rect);
}
m_frame.did_set_maximized({}, maximized);
WSEventLoop::the().post_event(*this, make<WSResizeEvent>(old_rect, m_rect));
CEventLoop::current().post_event(*this, make<WSResizeEvent>(old_rect, m_rect));
}
void WSWindow::event(CEvent& event)

View File

@ -247,7 +247,7 @@ void WSWindowManager::add_window(WSWindow& window)
m_windows_in_order.append(&window);
if (window.is_fullscreen()) {
WSEventLoop::the().post_event(window, make<WSResizeEvent>(window.rect(), WSScreen::the().rect()));
CEventLoop::current().post_event(window, make<WSResizeEvent>(window.rect(), WSScreen::the().rect()));
window.set_rect(WSScreen::the().rect());
}
@ -294,7 +294,7 @@ void WSWindowManager::remove_window(WSWindow& window)
if (!(listener.wm_event_mask() & WSAPI_WMEventMask::WindowRemovals))
return IterationDecision::Continue;
if (window.client())
WSEventLoop::the().post_event(listener, make<WSWMWindowRemovedEvent>(window.client()->client_id(), window.window_id()));
CEventLoop::current().post_event(listener, make<WSWMWindowRemovedEvent>(window.client()->client_id(), window.window_id()));
return IterationDecision::Continue;
});
}
@ -304,7 +304,7 @@ void WSWindowManager::tell_wm_listener_about_window(WSWindow& listener, WSWindow
if (!(listener.wm_event_mask() & WSAPI_WMEventMask::WindowStateChanges))
return;
if (window.client())
WSEventLoop::the().post_event(listener, make<WSWMWindowStateChangedEvent>(window.client()->client_id(), window.window_id(), window.title(), window.rect(), window.is_active(), window.type(), window.is_minimized()));
CEventLoop::current().post_event(listener, make<WSWMWindowStateChangedEvent>(window.client()->client_id(), window.window_id(), window.title(), window.rect(), window.is_active(), window.type(), window.is_minimized()));
}
void WSWindowManager::tell_wm_listener_about_window_rect(WSWindow& listener, WSWindow& window)
@ -312,7 +312,7 @@ void WSWindowManager::tell_wm_listener_about_window_rect(WSWindow& listener, WSW
if (!(listener.wm_event_mask() & WSAPI_WMEventMask::WindowRectChanges))
return;
if (window.client())
WSEventLoop::the().post_event(listener, make<WSWMWindowRectChangedEvent>(window.client()->client_id(), window.window_id(), window.rect()));
CEventLoop::current().post_event(listener, make<WSWMWindowRectChangedEvent>(window.client()->client_id(), window.window_id(), window.rect()));
}
void WSWindowManager::tell_wm_listener_about_window_icon(WSWindow& listener, WSWindow& window)
@ -320,7 +320,7 @@ void WSWindowManager::tell_wm_listener_about_window_icon(WSWindow& listener, WSW
if (!(listener.wm_event_mask() & WSAPI_WMEventMask::WindowIconChanges))
return;
if (window.client())
WSEventLoop::the().post_event(listener, make<WSWMWindowIconChangedEvent>(window.client()->client_id(), window.window_id(), window.icon_path()));
CEventLoop::current().post_event(listener, make<WSWMWindowIconChangedEvent>(window.client()->client_id(), window.window_id(), window.icon_path()));
}
void WSWindowManager::tell_wm_listeners_window_state_changed(WSWindow& window)
@ -490,7 +490,7 @@ bool WSWindowManager::process_ongoing_window_resize(const WSMouseEvent& event, W
#ifdef RESIZE_DEBUG
dbg() << "[WM] Finish resizing WSWindow{" << m_resize_window << "}";
#endif
WSEventLoop::the().post_event(*m_resize_window, make<WSResizeEvent>(m_resize_window->rect(), m_resize_window->rect()));
CEventLoop::current().post_event(*m_resize_window, make<WSResizeEvent>(m_resize_window->rect(), m_resize_window->rect()));
invalidate(*m_resize_window);
if (m_resize_window->rect().contains(event.position()))
hovered_window = m_resize_window;
@ -575,7 +575,7 @@ bool WSWindowManager::process_ongoing_window_resize(const WSMouseEvent& event, W
dbg() << "[WM] Resizing, original: " << m_resize_window_original_rect << ", now: " << new_rect;
#endif
m_resize_window->set_rect(new_rect);
WSEventLoop::the().post_event(*m_resize_window, make<WSResizeEvent>(old_rect, new_rect));
CEventLoop::current().post_event(*m_resize_window, make<WSResizeEvent>(old_rect, new_rect));
return true;
}
@ -904,12 +904,12 @@ void WSWindowManager::set_active_window(WSWindow* window)
auto* previously_active_window = m_active_window.ptr();
if (previously_active_window) {
WSEventLoop::the().post_event(*previously_active_window, make<WSEvent>(WSEvent::WindowDeactivated));
CEventLoop::current().post_event(*previously_active_window, make<WSEvent>(WSEvent::WindowDeactivated));
invalidate(*previously_active_window);
}
m_active_window = window->make_weak_ptr();
if (m_active_window) {
WSEventLoop::the().post_event(*m_active_window, make<WSEvent>(WSEvent::WindowActivated));
CEventLoop::current().post_event(*m_active_window, make<WSEvent>(WSEvent::WindowActivated));
invalidate(*m_active_window);
auto* client = window->client();
@ -927,12 +927,12 @@ void WSWindowManager::set_hovered_window(WSWindow* window)
return;
if (m_hovered_window)
WSEventLoop::the().post_event(*m_hovered_window, make<WSEvent>(WSEvent::WindowLeft));
CEventLoop::current().post_event(*m_hovered_window, make<WSEvent>(WSEvent::WindowLeft));
m_hovered_window = window ? window->make_weak_ptr() : nullptr;
if (m_hovered_window)
WSEventLoop::the().post_event(*m_hovered_window, make<WSEvent>(WSEvent::WindowEntered));
CEventLoop::current().post_event(*m_hovered_window, make<WSEvent>(WSEvent::WindowEntered));
}
void WSWindowManager::invalidate()

View File

@ -27,6 +27,6 @@ int main(int, char**)
WSWindowManager window_manager;
dbgprintf("Entering WindowServer main loop.\n");
WSEventLoop::the().exec();
loop.exec();
ASSERT_NOT_REACHED();
}