1 /* Copyright (C) 2017 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 #include "Globals.h"
20 
21 #include "network/NetClient.h"
22 #include "ps/GameSetup/Config.h"
23 #include "soundmanager/ISoundManager.h"
24 
25 bool g_app_minimized = false;
26 bool g_app_has_focus = true;
27 
28 std::map<int32_t, bool> g_keys;
29 int g_mouse_x = 50, g_mouse_y = 50;
30 bool g_mouse_active = true;
31 
32 // g_mouse_buttons[0] is unused. The order of entries is as in KeyName.h for MOUSE_*
33 bool g_mouse_buttons[MOUSE_LAST - MOUSE_BASE] = {0};
34 
35 PIFrequencyFilter g_frequencyFilter;
36 
37 // updates the state of the above; never swallows messages.
GlobalsInputHandler(const SDL_Event_ * ev)38 InReaction GlobalsInputHandler(const SDL_Event_* ev)
39 {
40 	size_t c;
41 
42 	switch(ev->ev.type)
43 	{
44 	case SDL_WINDOWEVENT:
45 		switch(ev->ev.window.event)
46 		{
47 		case SDL_WINDOWEVENT_MINIMIZED:
48 			g_app_minimized = true;
49 			break;
50 		case SDL_WINDOWEVENT_EXPOSED:
51 		case SDL_WINDOWEVENT_RESTORED:
52 			g_app_minimized = false;
53 			break;
54 		case SDL_WINDOWEVENT_FOCUS_GAINED:
55 			g_app_has_focus = true;
56 			break;
57 		case SDL_WINDOWEVENT_FOCUS_LOST:
58 			g_app_has_focus = false;
59 			break;
60 		case SDL_WINDOWEVENT_ENTER:
61 			g_mouse_active = true;
62 			break;
63 		case SDL_WINDOWEVENT_LEAVE:
64 			g_mouse_active = false;
65 			break;
66 		}
67 		return IN_PASS;
68 
69 	case SDL_MOUSEMOTION:
70 		g_mouse_x = ev->ev.motion.x;
71 		g_mouse_y = ev->ev.motion.y;
72 		return IN_PASS;
73 
74 	case SDL_MOUSEBUTTONDOWN:
75 	case SDL_MOUSEBUTTONUP:
76 		c = ev->ev.button.button;
77 		if(c < ARRAY_SIZE(g_mouse_buttons))
78 			g_mouse_buttons[c] = (ev->ev.type == SDL_MOUSEBUTTONDOWN);
79 		else
80 		{
81 			// don't complain: just ignore people with too many mouse buttons
82 			//debug_warn(L"invalid mouse button");
83 		}
84 		return IN_PASS;
85 
86 	case SDL_KEYDOWN:
87 	case SDL_KEYUP:
88 		g_keys[ev->ev.key.keysym.sym] = (ev->ev.type == SDL_KEYDOWN);
89 		return IN_PASS;
90 
91 	default:
92 		return IN_PASS;
93 	}
94 
95 	UNREACHABLE;
96 }
97