Cavalry not attacking soldiers on Jebel Barkal wasn't a successful idea.
[0ad.git] / source / lib / input.cpp
bloba2161230039655d604776cb31b29b5ebdb476337
1 /* Copyright (C) 2017 Wildfire Games.
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
24 * SDL input redirector; dispatches to multiple handlers.
27 #include "precompiled.h"
28 #include "input.h"
30 #include <stdio.h>
31 #include <stdlib.h>
33 #include "lib/external_libraries/libsdl.h"
35 const size_t MAX_HANDLERS = 9;
36 static InHandler handler_stack[MAX_HANDLERS];
37 static size_t handler_stack_top = 0;
39 static std::list<SDL_Event_> priority_events;
41 void in_add_handler(InHandler handler)
43 ENSURE(handler);
45 if(handler_stack_top >= MAX_HANDLERS)
46 WARN_IF_ERR(ERR::LIMIT);
48 handler_stack[handler_stack_top++] = handler;
51 void in_reset_handlers()
53 handler_stack_top = 0;
56 // send ev to each handler until one returns IN_HANDLED
57 void in_dispatch_event(const SDL_Event_* ev)
59 for(int i = (int)handler_stack_top-1; i >= 0; i--)
61 ENSURE(handler_stack[i] && ev);
62 InReaction ret = handler_stack[i](ev);
63 // .. done, return
64 if(ret == IN_HANDLED)
65 return;
66 // .. next handler
67 else if(ret == IN_PASS)
68 continue;
69 // .. invalid return value
70 else
71 DEBUG_WARN_ERR(ERR::LOGIC); // invalid handler return value
75 void in_push_priority_event(const SDL_Event_* event)
77 priority_events.push_back(*event);
80 int in_poll_priority_event(SDL_Event_* event)
82 if (priority_events.empty())
83 return 0;
85 *event = priority_events.front();
86 priority_events.pop_front();
87 return 1;
90 int in_poll_event(SDL_Event_* event)
92 return in_poll_priority_event(event) ? 1 : SDL_PollEvent(&event->ev);