LCOV - code coverage report
Current view: top level - source/ps - Input.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 43 49 87.8 %
Date: 2026-12-03 01:19:39 Functions: 13 17 76.5 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "ps/Input.h"
      21             : 
      22             : #include "lib/debug.h"
      23             : #include "lib/external_libraries/libsdl.h"
      24             : #include "ps/Profile.h"
      25             : #include "ps/TouchInput.h"
      26             : #include "scriptinterface/JSON.h"
      27             : #include "scriptinterface/Conversions.h"
      28             : #include "scriptinterface/Request.h"
      29             : 
      30             : #include <utility>
      31             : 
      32             : namespace Input
      33             : {
      34           8 : Manager::Manager() :
      35           8 :     m_PriorityEvents{std::make_unique<std::queue<SDL_Event>>()}
      36             : {
      37           8 : }
      38             : 
      39           8 : Manager::~Manager() = default;
      40             : 
      41          80 : void Manager::PushPriorityEvent(const SDL_Event& event)
      42             : {
      43          80 :     m_PriorityEvents->push(event);
      44          80 : }
      45             : 
      46          11 : void Manager::DispatchEvent(const SDL_Event& event)
      47             : {
      48             :     // Looks like std::find_if, but std::find_if does not guarantee the order of the handlers.
      49         108 :     for (const auto handler : m_Handlers)
      50             :     {
      51         100 :         if (handler && (*handler)(event) == Reaction::HANDLED)
      52             :             return;
      53             :     }
      54             : }
      55             : 
      56          44 : Manager::PollEventsResult::EventIterator::EventIterator(PollEventsResult& range) :
      57          44 :     m_Storage{&range}
      58             : {
      59           0 :     ++*this;
      60           0 : }
      61             : 
      62          81 : Manager::PollEventsResult::EventIterator::reference Manager::PollEventsResult::EventIterator::operator*()
      63             : {
      64          81 :     return *m_Storage->m_Event;
      65             : }
      66             : 
      67           3 : Manager::PollEventsResult::EventIterator::pointer Manager::PollEventsResult::EventIterator::operator->()
      68             : {
      69           3 :     return &**this;
      70             : }
      71             : 
      72         127 : Manager::PollEventsResult::EventIterator& Manager::PollEventsResult::EventIterator::operator++()
      73             : {
      74         127 :     std::queue<SDL_Event>& priorityEvents{m_Storage->m_PriorityEvents};
      75         127 :     if (!priorityEvents.empty())
      76             :     {
      77          80 :         *m_Storage->m_Event = priorityEvents.front();
      78          80 :         priorityEvents.pop();
      79             :     }
      80          47 :     else if (!SDL_PollEvent(m_Storage->m_Event.get()))
      81          44 :         m_Storage = nullptr;
      82             : 
      83         127 :     return *this;
      84             : }
      85             : 
      86           0 : Manager::PollEventsResult::EventIterator& Manager::PollEventsResult::EventIterator::operator++(int)
      87             : {
      88           0 :     ++(*this);
      89           0 :     return *this;
      90             : }
      91             : 
      92         127 : bool Manager::PollEventsResult::EventIterator::operator==(const EventIterator& other) const
      93             : {
      94         127 :     return m_Storage == other.m_Storage;
      95             : }
      96             : 
      97          44 : Manager::PollEventsResult::PollEventsResult(std::queue<SDL_Event>& priorityEvents) :
      98          44 :     m_PriorityEvents{priorityEvents},
      99             :     m_Event{std::make_unique<SDL_Event>()}
     100             : {
     101           0 : }
     102             : 
     103          44 : Manager::PollEventsResult::EventIterator Manager::PollEventsResult::begin()
     104             : {
     105          44 :     return EventIterator{*this};
     106             : }
     107             : 
     108          47 : Manager::PollEventsResult::EventIterator Manager::PollEventsResult::end()
     109             : {
     110          47 :     return EventIterator{};
     111             : }
     112             : 
     113          44 : Manager::PollEventsResult Manager::PollEvents()
     114             : {
     115          44 :     return PollEventsResult{*m_PriorityEvents};
     116             : }
     117             : 
     118             : 
     119          41 : HandlerBase::HandlerBase(HandlerBase*& pos) noexcept :
     120          41 :     m_ToReset{pos}
     121             : {
     122          41 :     const auto old = std::exchange(m_ToReset, this);
     123          41 :     ENSURE(old == nullptr && "There is already a handler registered to this slot.");
     124          41 : }
     125             : 
     126          41 : HandlerBase::~HandlerBase()
     127             : {
     128          41 :     const auto old = std::exchange(m_ToReset, nullptr);
     129          41 :     ENSURE(old == this && "No handler is registered to this slot.");
     130          41 : }
     131             : } // namespace Input

Generated by: LCOV version 1.16