{"id":13196,"url":"https://patchwork.libcamera.org/api/patches/13196/?format=json","web_url":"https://patchwork.libcamera.org/patch/13196/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210804124314.8044-2-laurent.pinchart@ideasonboard.com>","date":"2021-08-04T12:43:07","name":"[libcamera-devel,v3,1/8] cam: event_loop: Add support for file descriptor events","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"83e3c8309304246c13202581eb82b3c5108c06ff","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/13196/mbox/","series":[{"id":2303,"url":"https://patchwork.libcamera.org/api/series/2303/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2303","date":"2021-08-04T12:43:06","name":"libcamera: Add DRM/KMS viewfinder display to cam","version":3,"mbox":"https://patchwork.libcamera.org/series/2303/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13196/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13196/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 1D60CC3235\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  4 Aug 2021 12:43:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A944A6888D;\n\tWed,  4 Aug 2021 14:43:32 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 471C46880F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  4 Aug 2021 14:43:30 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D64C6891\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  4 Aug 2021 14:43:29 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"AheqcOWW\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1628081010;\n\tbh=snoZbTLRJ3Q4XSYBQAHNFepvcP99fkqBjgz1hEsawvc=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=AheqcOWWQobBrimePvej2MYd5gcR6t43aEWSZRSMGhk0kHibZtyBAW9bBlZvZ54yF\n\t1KWHG6jpkO1kwqR7m8BPvahaZOeZH3QQ5iKSPdFsb6E6IX/X4BlG0QfLjpq/u7FUUJ\n\t3UXO3IyrK2DfkEkff56RP10/4A8XauM8Y1sJorL4=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  4 Aug 2021 15:43:07 +0300","Message-Id":"<20210804124314.8044-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.31.1","In-Reply-To":"<20210804124314.8044-1-laurent.pinchart@ideasonboard.com>","References":"<20210804124314.8044-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 1/8] cam: event_loop: Add support for\n\tfile descriptor events","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Extend the EventLoop class to support watching file descriptors for\nread and write events.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nReviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n---\n src/cam/event_loop.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++\n src/cam/event_loop.h   | 20 +++++++++++++++++++\n 2 files changed, 64 insertions(+)","diff":"diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp\nindex 6a4c47f287d0..e25784c083cf 100644\n--- a/src/cam/event_loop.cpp\n+++ b/src/cam/event_loop.cpp\n@@ -10,6 +10,7 @@\n #include <assert.h>\n #include <event2/event.h>\n #include <event2/thread.h>\n+#include <iostream>\n \n EventLoop *EventLoop::instance_ = nullptr;\n \n@@ -26,6 +27,7 @@ EventLoop::~EventLoop()\n {\n \tinstance_ = nullptr;\n \n+\tevents_.clear();\n \tevent_base_free(base_);\n \tlibevent_global_shutdown();\n }\n@@ -58,6 +60,30 @@ void EventLoop::callLater(const std::function<void()> &func)\n \tevent_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr);\n }\n \n+void EventLoop::addEvent(int fd, EventType type,\n+\t\t\t const std::function<void()> &callback)\n+{\n+\tstd::unique_ptr<Event> event = std::make_unique<Event>(callback);\n+\tshort events = (type & Read ? EV_READ : 0)\n+\t\t     | (type & Write ? EV_WRITE : 0)\n+\t\t     | EV_PERSIST;\n+\n+\tevent->event_ = event_new(base_, fd, events, &EventLoop::Event::dispatch,\n+\t\t\t\t  event.get());\n+\tif (!event->event_) {\n+\t\tstd::cerr << \"Failed to create event for fd \" << fd << std::endl;\n+\t\treturn;\n+\t}\n+\n+\tint ret = event_add(event->event_, nullptr);\n+\tif (ret < 0) {\n+\t\tstd::cerr << \"Failed to add event for fd \" << fd << std::endl;\n+\t\treturn;\n+\t}\n+\n+\tevents_.push_back(std::move(event));\n+}\n+\n void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd,\n \t\t\t\t [[maybe_unused]] short flags, void *param)\n {\n@@ -80,3 +106,21 @@ void EventLoop::dispatchCall()\n \n \tcall();\n }\n+\n+EventLoop::Event::Event(const std::function<void()> &callback)\n+\t: callback_(callback), event_(nullptr)\n+{\n+}\n+\n+EventLoop::Event::~Event()\n+{\n+\tevent_del(event_);\n+\tevent_free(event_);\n+}\n+\n+void EventLoop::Event::dispatch([[maybe_unused]] int fd,\n+\t\t\t\t[[maybe_unused]] short events, void *arg)\n+{\n+\tEvent *event = static_cast<Event *>(arg);\n+\tevent->callback_();\n+}\ndiff --git a/src/cam/event_loop.h b/src/cam/event_loop.h\nindex ba3ba3a4a675..57bb6fb34aa7 100644\n--- a/src/cam/event_loop.h\n+++ b/src/cam/event_loop.h\n@@ -8,6 +8,7 @@\n #define __CAM_EVENT_LOOP_H__\n \n #include <functional>\n+#include <memory>\n #include <list>\n #include <mutex>\n \n@@ -18,6 +19,11 @@ struct event_base;\n class EventLoop\n {\n public:\n+\tenum EventType {\n+\t\tRead = 1,\n+\t\tWrite = 2,\n+\t};\n+\n \tEventLoop();\n \t~EventLoop();\n \n@@ -28,13 +34,27 @@ public:\n \n \tvoid callLater(const std::function<void()> &func);\n \n+\tvoid addEvent(int fd, EventType type,\n+\t\t      const std::function<void()> &handler);\n+\n private:\n+\tstruct Event {\n+\t\tEvent(const std::function<void()> &callback);\n+\t\t~Event();\n+\n+\t\tstatic void dispatch(int fd, short events, void *arg);\n+\n+\t\tstd::function<void()> callback_;\n+\t\tstruct event *event_;\n+\t};\n+\n \tstatic EventLoop *instance_;\n \n \tstruct event_base *base_;\n \tint exitCode_;\n \n \tstd::list<std::function<void()>> calls_;\n+\tstd::list<std::unique_ptr<Event>> events_;\n \tstd::mutex lock_;\n \n \tstatic void dispatchCallback(evutil_socket_t fd, short flags,\n","prefixes":["libcamera-devel","v3","1/8"]}