From c5dc671ca17b072cbb549a680ccc6ea19cd803b9 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 10 Sep 2024 01:33:16 +0100 Subject: [PATCH] client: cleanup IPC stuff with a macro --- src/browser/client.cxx | 88 +++++++++++++----------------------------- src/browser/client.hxx | 2 +- 2 files changed, 27 insertions(+), 63 deletions(-) diff --git a/src/browser/client.cxx b/src/browser/client.cxx index 5a0d380..4be92cf 100644 --- a/src/browser/client.cxx +++ b/src/browser/client.cxx @@ -290,11 +290,11 @@ void Browser::Client::IPCHandleClosed(int fd) { } // convenience function, assumes the mutex is already locked -CefRefPtr Browser::Client::GetWindowFromFDAndID(GameClient* client, uint64_t id) { +CefRefPtr Browser::Client::GetWindowFromFDAndIDs(GameClient* client, uint64_t plugin_id, uint64_t window_id) { for (CefRefPtr& p: client->plugins) { - if (p->deleted) continue; + if (p->deleted || p->uid != plugin_id) continue; for (CefRefPtr& w: p->windows_osr) { - if (!w->IsDeleted() && w->ID() == id) { + if (!w->IsDeleted() && w->ID() == window_id) { return w; } } @@ -330,6 +330,7 @@ bool Browser::Client::IPCHandleMessage(int fd) { } GameClient* const client = &(*it); + uint64_t plugin_id, window_id; switch (message.message_type) { case IPC_MSG_IDENTIFY: { client->identity = new char[message.items + 1]; @@ -381,70 +382,33 @@ bool Browser::Client::IPCHandleMessage(int fd) { break; } case IPC_MSG_OSRUPDATE_ACK: { - uint64_t plugin_window_id; - _bolt_ipc_receive(fd, &plugin_window_id, sizeof(plugin_window_id)); - CefRefPtr window = this->GetWindowFromFDAndID(client, plugin_window_id); + _bolt_ipc_receive(fd, &plugin_id, sizeof(plugin_id)); + _bolt_ipc_receive(fd, &window_id, sizeof(window_id)); + CefRefPtr window = this->GetWindowFromFDAndIDs(client, plugin_id, window_id); if (window) window->HandleAck(); break; } - case IPC_MSG_EVRESIZE: { - uint64_t plugin_window_id; - ResizeEvent event; - _bolt_ipc_receive(fd, &plugin_window_id, sizeof(plugin_window_id)); - _bolt_ipc_receive(fd, &event, sizeof(event)); - CefRefPtr window = this->GetWindowFromFDAndID(client, plugin_window_id); - if (window) window->HandleResize(&event); - break; - } - case IPC_MSG_EVMOUSEMOTION: { - uint64_t plugin_window_id; - MouseMotionEvent event; - _bolt_ipc_receive(fd, &plugin_window_id, sizeof(plugin_window_id)); - _bolt_ipc_receive(fd, &event, sizeof(event)); - CefRefPtr window = this->GetWindowFromFDAndID(client, plugin_window_id); - if (window) window->HandleMouseMotion(&event); - break; - } - case IPC_MSG_EVMOUSEBUTTON: { - uint64_t plugin_window_id; - MouseButtonEvent event; - _bolt_ipc_receive(fd, &plugin_window_id, sizeof(plugin_window_id)); - _bolt_ipc_receive(fd, &event, sizeof(event)); - CefRefPtr window = this->GetWindowFromFDAndID(client, plugin_window_id); - if (window) window->HandleMouseButton(&event); - break; - } - case IPC_MSG_EVMOUSEBUTTONUP: { - uint64_t plugin_window_id; - MouseButtonEvent event; - _bolt_ipc_receive(fd, &plugin_window_id, sizeof(plugin_window_id)); - _bolt_ipc_receive(fd, &event, sizeof(event)); - CefRefPtr window = this->GetWindowFromFDAndID(client, plugin_window_id); - if (window) window->HandleMouseButtonUp(&event); - break; - } - case IPC_MSG_EVSCROLL: { - uint64_t plugin_window_id; - MouseScrollEvent event; - _bolt_ipc_receive(fd, &plugin_window_id, sizeof(plugin_window_id)); - _bolt_ipc_receive(fd, &event, sizeof(event)); - CefRefPtr window = this->GetWindowFromFDAndID(client, plugin_window_id); - if (window) window->HandleScroll(&event); - break; - } - case IPC_MSG_EVMOUSELEAVE: { - uint64_t plugin_window_id; - MouseMotionEvent event; - _bolt_ipc_receive(fd, &plugin_window_id, sizeof(plugin_window_id)); - _bolt_ipc_receive(fd, &event, sizeof(event)); - CefRefPtr window = this->GetWindowFromFDAndID(client, plugin_window_id); - if (window) window->HandleMouseLeave(&event); - break; - } - default: { + +#define DEF_OSR_EVENT(EVNAME, HANDLER, EVTYPE) case IPC_MSG_EV##EVNAME: { \ + EVTYPE event; \ + _bolt_ipc_receive(fd, &plugin_id, sizeof(plugin_id)); \ + _bolt_ipc_receive(fd, &window_id, sizeof(window_id)); \ + _bolt_ipc_receive(fd, &event, sizeof(event)); \ + CefRefPtr window = this->GetWindowFromFDAndIDs(client, plugin_id, window_id); \ + if (window) window->HANDLER(&event); \ + break; \ +} + DEF_OSR_EVENT(RESIZE, HandleResize, ResizeEvent) + DEF_OSR_EVENT(MOUSEMOTION, HandleMouseMotion, MouseMotionEvent) + DEF_OSR_EVENT(MOUSEBUTTON, HandleMouseButton, MouseButtonEvent) + DEF_OSR_EVENT(MOUSEBUTTONUP, HandleMouseButtonUp, MouseButtonEvent) + DEF_OSR_EVENT(SCROLL, HandleScroll, MouseScrollEvent) + DEF_OSR_EVENT(MOUSELEAVE, HandleMouseLeave, MouseMotionEvent) +#undef DEF_OSR_EVENT + + default: fmt::print("[I] got unknown message type {}\n", (int)message.message_type); break; - } } this->game_clients_lock.unlock(); return true; diff --git a/src/browser/client.hxx b/src/browser/client.hxx index 6e13358..8657ecf 100644 --- a/src/browser/client.hxx +++ b/src/browser/client.hxx @@ -187,7 +187,7 @@ namespace Browser { uint64_t next_plugin_uid; std::mutex game_clients_lock; std::vector game_clients; - CefRefPtr GetWindowFromFDAndID(GameClient* client, uint64_t id); + CefRefPtr GetWindowFromFDAndIDs(GameClient* client, uint64_t plugin_id, uint64_t window_id); CefRefPtr GetPluginFromFDAndID(GameClient* client, uint64_t id); #endif