LCOV - code coverage report
Current view: top level - source/lib - input.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 25 31 80.6 %
Date: 2023-01-19 00:18:29 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /* Copyright (C) 2021 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : /*
      24             :  * SDL input redirector; dispatches to multiple handlers.
      25             :  */
      26             : 
      27             : #include "precompiled.h"
      28             : #include "input.h"
      29             : 
      30             : #include "lib/external_libraries/libsdl.h"
      31             : 
      32             : #include <list>
      33             : #include <stdio.h>
      34             : #include <stdlib.h>
      35             : 
      36             : const size_t MAX_HANDLERS = 10;
      37             : static InHandler handler_stack[MAX_HANDLERS];
      38             : static size_t handler_stack_top = 0;
      39             : 
      40           1 : static std::list<SDL_Event_> priority_events;
      41             : 
      42           9 : void in_add_handler(InHandler handler)
      43             : {
      44           9 :     ENSURE(handler);
      45             : 
      46           9 :     if(handler_stack_top >= MAX_HANDLERS)
      47           0 :         WARN_IF_ERR(ERR::LIMIT);
      48             : 
      49           9 :     handler_stack[handler_stack_top++] = handler;
      50           9 : }
      51             : 
      52           0 : void in_reset_handlers()
      53             : {
      54           0 :     handler_stack_top = 0;
      55           0 : }
      56             : 
      57             : // send ev to each handler until one returns IN_HANDLED
      58           7 : void in_dispatch_event(const SDL_Event_* ev)
      59             : {
      60          70 :     for(int i = (int)handler_stack_top-1; i >= 0; i--)
      61             :     {
      62          63 :         ENSURE(handler_stack[i] && ev);
      63          63 :         InReaction ret = handler_stack[i](ev);
      64             :         // .. done, return
      65          63 :         if(ret == IN_HANDLED)
      66           0 :             return;
      67             :         // .. next handler
      68          63 :         else if(ret == IN_PASS)
      69          63 :             continue;
      70             :         // .. invalid return value
      71             :         else
      72           0 :             DEBUG_WARN_ERR(ERR::LOGIC); // invalid handler return value
      73             :     }
      74             : }
      75             : 
      76          78 : void in_push_priority_event(const SDL_Event_* event)
      77             : {
      78          78 :     priority_events.push_back(*event);
      79          78 : }
      80             : 
      81         118 : int in_poll_priority_event(SDL_Event_* event)
      82             : {
      83         118 :     if (priority_events.empty())
      84          40 :         return 0;
      85             : 
      86          78 :     *event = priority_events.front();
      87          78 :     priority_events.pop_front();
      88          78 :     return 1;
      89             : }
      90             : 
      91          10 : int in_poll_event(SDL_Event_* event)
      92             : {
      93          10 :     return in_poll_priority_event(event) ? 1 : SDL_PollEvent(&event->ev);
      94           3 : }

Generated by: LCOV version 1.13