diff --git a/WindowServer/WSClientConnection.cpp b/WindowServer/WSClientConnection.cpp index ffb2e6856a..2e5fcf5e07 100644 --- a/WindowServer/WSClientConnection.cpp +++ b/WindowServer/WSClientConnection.cpp @@ -369,7 +369,7 @@ void WSClientConnection::handle_request(WSAPIDidFinishPaintingNotification& requ if (!window.has_painted_since_last_resize()) { if (window.last_lazy_resize_rect().size() == request.rect().size()) { window.set_has_painted_since_last_resize(true); - WSMessageLoop::the().post_message(&window, make(window.last_lazy_resize_rect(), window.rect())); + WSMessageLoop::the().post_message(window, make(window.last_lazy_resize_rect(), window.rect())); } } WSWindowManager::the().invalidate(window, request.rect()); diff --git a/WindowServer/WSMessageLoop.cpp b/WindowServer/WSMessageLoop.cpp index 0f13468812..2ba401d92e 100644 --- a/WindowServer/WSMessageLoop.cpp +++ b/WindowServer/WSMessageLoop.cpp @@ -60,28 +60,23 @@ int WSMessageLoop::exec() Vector messages = move(m_queued_messages); for (auto& queued_message : messages) { - auto* receiver = queued_message.receiver; + auto* receiver = queued_message.receiver.ptr(); auto& message = *queued_message.message; #ifdef WSEVENTLOOP_DEBUG dbgprintf("WSMessageLoop: receiver{%p} message %u\n", receiver, (unsigned)message.type()); #endif - if (!receiver) { - dbgprintf("WSMessage type %u with no receiver :(\n", message.type()); - ASSERT_NOT_REACHED(); - return 1; - } else { + if (receiver) receiver->on_message(message); - } } } } -void WSMessageLoop::post_message(WSMessageReceiver* receiver, OwnPtr&& message) +void WSMessageLoop::post_message(WSMessageReceiver& receiver, OwnPtr&& message) { #ifdef WSEVENTLOOP_DEBUG dbgprintf("WSMessageLoop::post_message: {%u} << receiver=%p, message=%p (type=%u)\n", m_queued_messages.size(), receiver, message.ptr(), message->type()); #endif - m_queued_messages.append({ receiver, move(message) }); + m_queued_messages.append({ receiver.make_weak_ptr(), move(message) }); } void WSMessageLoop::Timer::reload() @@ -263,7 +258,7 @@ void WSMessageLoop::notify_client_disconnected(int client_id) auto* client = WSClientConnection::from_client_id(client_id); if (!client) return; - post_message(client, make(client_id)); + post_message(*client, make(client_id)); } void WSMessageLoop::on_receive_from_client(int client_id, const WSAPI_ClientMessage& message) @@ -274,8 +269,7 @@ void WSMessageLoop::on_receive_from_client(int client_id, const WSAPI_ClientMess sched_yield(); #endif - WSClientConnection* client = WSClientConnection::from_client_id(client_id); - ASSERT(client); + WSClientConnection& client = *WSClientConnection::from_client_id(client_id); switch (message.type) { case WSAPI_ClientMessage::Type::CreateMenubar: post_message(client, make(client_id)); @@ -336,5 +330,7 @@ void WSMessageLoop::on_receive_from_client(int client_id, const WSAPI_ClientMess case WSAPI_ClientMessage::Type::SetGlobalCursorTracking: post_message(client, make(client_id, message.window_id, message.value)); break; + default: + break; } } diff --git a/WindowServer/WSMessageLoop.h b/WindowServer/WSMessageLoop.h index 5851c8a5db..752cd0d60c 100644 --- a/WindowServer/WSMessageLoop.h +++ b/WindowServer/WSMessageLoop.h @@ -5,6 +5,7 @@ #include #include #include +#include class WSMessageReceiver; struct WSAPI_ClientMessage; @@ -17,7 +18,7 @@ public: int exec(); - void post_message(WSMessageReceiver* receiver, OwnPtr&&); + void post_message(WSMessageReceiver& receiver, OwnPtr&&); static WSMessageLoop& the(); @@ -36,7 +37,7 @@ private: void drain_keyboard(); struct QueuedMessage { - WSMessageReceiver* receiver { nullptr }; + WeakPtr receiver; OwnPtr message; }; Vector m_queued_messages; diff --git a/WindowServer/WSScreen.cpp b/WindowServer/WSScreen.cpp index e21a227054..04d31cd51d 100644 --- a/WindowServer/WSScreen.cpp +++ b/WindowServer/WSScreen.cpp @@ -74,7 +74,7 @@ void WSScreen::on_receive_mouse_data(int dx, int dy, bool left_button, bool righ buttons |= (unsigned)MouseButton::Right; if (m_cursor_location != prev_location) { auto message = make(WSMessage::MouseMove, m_cursor_location, buttons); - WSMessageLoop::the().post_message(&WSWindowManager::the(), move(message)); + WSMessageLoop::the().post_message(WSWindowManager::the(), move(message)); } bool prev_left_button = m_left_mouse_button_pressed; bool prev_right_button = m_right_mouse_button_pressed; @@ -82,11 +82,11 @@ void WSScreen::on_receive_mouse_data(int dx, int dy, bool left_button, bool righ m_right_mouse_button_pressed = right_button; if (prev_left_button != left_button) { auto message = make(left_button ? WSMessage::MouseDown : WSMessage::MouseUp, m_cursor_location, buttons, MouseButton::Left); - WSMessageLoop::the().post_message(&WSWindowManager::the(), move(message)); + WSMessageLoop::the().post_message(WSWindowManager::the(), move(message)); } if (prev_right_button != right_button) { auto message = make(right_button ? WSMessage::MouseDown : WSMessage::MouseUp, m_cursor_location, buttons, MouseButton::Right); - WSMessageLoop::the().post_message(&WSWindowManager::the(), move(message)); + WSMessageLoop::the().post_message(WSWindowManager::the(), move(message)); } if (m_cursor_location != prev_location || prev_left_button != left_button) WSWindowManager::the().invalidate_cursor(); @@ -98,7 +98,7 @@ void WSScreen::on_receive_keyboard_data(KeyEvent kernel_event) message->m_shift = kernel_event.shift(); message->m_ctrl = kernel_event.ctrl(); message->m_alt = kernel_event.alt(); - WSMessageLoop::the().post_message(&WSWindowManager::the(), move(message)); + WSMessageLoop::the().post_message(WSWindowManager::the(), move(message)); } void WSScreen::set_y_offset(int offset) diff --git a/WindowServer/WSWindowManager.cpp b/WindowServer/WSWindowManager.cpp index b21a2245fc..d73db056d3 100644 --- a/WindowServer/WSWindowManager.cpp +++ b/WindowServer/WSWindowManager.cpp @@ -502,9 +502,13 @@ void WSWindowManager::start_window_resize(WSWindow& window, WSMouseEvent& event) int window_relative_y = event.y() - window_rect.y(); int hot_area_row = window_relative_y / (window_rect.height() / 3); int hot_area_column = window_relative_x / (window_rect.width() / 3); + ASSERT(hot_area_row >= 0 && hot_area_row <= 2); + ASSERT(hot_area_column >= 0 && hot_area_column <= 2); m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column]; - if (m_resize_direction == ResizeDirection::None) + if (m_resize_direction == ResizeDirection::None) { + ASSERT(!m_resize_window); return; + } #ifdef RESIZE_DEBUG printf("[WM] Begin resizing WSWindow{%p}\n", &window); @@ -550,7 +554,7 @@ void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& event_ #ifdef RESIZE_DEBUG printf("[WM] Finish resizing WSWindow{%p}\n", m_resize_window.ptr()); #endif - WSMessageLoop::the().post_message(m_resize_window.ptr(), make(m_resize_window->rect(), m_resize_window->rect())); + WSMessageLoop::the().post_message(*m_resize_window, make(m_resize_window->rect(), m_resize_window->rect())); invalidate(*m_resize_window); m_resize_window = nullptr; return; @@ -633,7 +637,7 @@ void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& event_ m_resize_window->set_has_painted_since_last_resize(false); dbgprintf("I'm gonna wait for %s\n", new_rect.to_string().characters()); m_resize_window->set_last_lazy_resize_rect(new_rect); - WSMessageLoop::the().post_message(m_resize_window.ptr(), make(old_rect, new_rect)); + WSMessageLoop::the().post_message(*m_resize_window, make(old_rect, new_rect)); } return; } @@ -911,12 +915,12 @@ void WSWindowManager::set_active_window(WSWindow* window) return; if (auto* previously_active_window = m_active_window.ptr()) { - WSMessageLoop::the().post_message(previously_active_window, make(WSMessage::WindowDeactivated)); + WSMessageLoop::the().post_message(*previously_active_window, make(WSMessage::WindowDeactivated)); invalidate(*previously_active_window); } m_active_window = window->make_weak_ptr(); if (m_active_window) { - WSMessageLoop::the().post_message(m_active_window.ptr(), make(WSMessage::WindowActivated)); + WSMessageLoop::the().post_message(*m_active_window, make(WSMessage::WindowActivated)); invalidate(*m_active_window); auto* client = window->client(); @@ -931,12 +935,12 @@ void WSWindowManager::set_hovered_window(WSWindow* window) return; if (m_hovered_window) - WSMessageLoop::the().post_message(m_hovered_window.ptr(), make(WSMessage::WindowLeft)); + WSMessageLoop::the().post_message(*m_hovered_window, make(WSMessage::WindowLeft)); m_hovered_window = window ? window->make_weak_ptr() : nullptr; if (m_hovered_window) - WSMessageLoop::the().post_message(m_hovered_window.ptr(), make(WSMessage::WindowEntered)); + WSMessageLoop::the().post_message(*m_hovered_window, make(WSMessage::WindowEntered)); } void WSWindowManager::invalidate() @@ -960,7 +964,7 @@ void WSWindowManager::invalidate(const Rect& a_rect, bool should_schedule_compos m_dirty_rects.add(rect); if (should_schedule_compose_event && !m_pending_compose_event) { - WSMessageLoop::the().post_message(this, make(WSMessage::WM_DeferredCompose)); + WSMessageLoop::the().post_message(*this, make(WSMessage::WM_DeferredCompose)); m_pending_compose_event = true; } }