1 /* Copyright (C) 2021 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"
30 #include "lib/external_libraries/libsdl.h"
36 const size_t MAX_HANDLERS
= 10;
37 static InHandler handler_stack
[MAX_HANDLERS
];
38 static size_t handler_stack_top
= 0;
40 static std::list
<SDL_Event_
> priority_events
;
42 void in_add_handler(InHandler handler
)
46 if(handler_stack_top
>= MAX_HANDLERS
)
47 WARN_IF_ERR(ERR::LIMIT
);
49 handler_stack
[handler_stack_top
++] = handler
;
52 void in_reset_handlers()
54 handler_stack_top
= 0;
57 // send ev to each handler until one returns IN_HANDLED
58 void in_dispatch_event(const SDL_Event_
* ev
)
60 for(int i
= (int)handler_stack_top
-1; i
>= 0; i
--)
62 ENSURE(handler_stack
[i
] && ev
);
63 InReaction ret
= handler_stack
[i
](ev
);
68 else if(ret
== IN_PASS
)
70 // .. invalid return value
72 DEBUG_WARN_ERR(ERR::LOGIC
); // invalid handler return value
76 void in_push_priority_event(const SDL_Event_
* event
)
78 priority_events
.push_back(*event
);
81 int in_poll_priority_event(SDL_Event_
* event
)
83 if (priority_events
.empty())
86 *event
= priority_events
.front();
87 priority_events
.pop_front();
91 int in_poll_event(SDL_Event_
* event
)
93 return in_poll_priority_event(event
) ? 1 : SDL_PollEvent(&event
->ev
);