Debian Codegraph @

src/keyboard.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "keyboard.h"
20
21 #include "joystick.h"
22 #include "mouse.h"
23 #include "network.h"
24 #include "opentyr.h"
25 #include "video.h"
26 #include "video_scale.h"
27
28 #include "SDL.h"
29
30 #include <stdio.h>
31
32 JE_boolean ESCPressed;
33
34 JE_boolean newkey, newmouse, keydown, mousedown;
35 SDL_Scancode lastkey_scan;
36 SDL_Keymod lastkey_mod;
37 Uint8 lastmouse_but;
38 Sint32 lastmouse_x, lastmouse_y;
39 JE_boolean mouse_pressed[3] = {false, false, false};
40 Sint32 mouse_x, mouse_y;
41
42 bool windowHasFocus;
43
44 Uint8 keysactive[SDL_NUM_SCANCODES];
45
46 bool new_text;
47 char last_text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
48
49 static bool mouseRelativeEnabled;
50
51 // Relative mouse position in window coordinates.
52 static Sint32 mouseWindowXRelative;
53 static Sint32 mouseWindowYRelative;
54
55 void flush_events_buffer(void)
56 {
57 SDL_Event ev;
58
59 while (SDL_PollEvent(&ev));
60 }
61
62 void wait_input(JE_boolean keyboard, JE_boolean mouse, JE_boolean joystick)
63 {
64 service_SDL_events(false);
65 while (!((keyboard && keydown) || (mouse && mousedown) || (joystick && joydown)))
66 {
67 SDL_Delay(SDL_POLL_INTERVAL);
68 push_joysticks_as_keyboard();
69 service_SDL_events(false);
70
71 #ifdef WITH_NETWORK
72 if (isNetworkGame)
73 network_check();
74 #endif
75 }
76 }
77
78 void wait_noinput(JE_boolean keyboard, JE_boolean mouse, JE_boolean joystick)
79 {
80 service_SDL_events(false);
81 while ((keyboard && keydown) || (mouse && mousedown) || (joystick && joydown))
82 {
83 SDL_Delay(SDL_POLL_INTERVAL);
84 poll_joysticks();
85 service_SDL_events(false);
86
87 #ifdef WITH_NETWORK
88 if (isNetworkGame)
89 network_check();
90 #endif
91 }
92 }
93
94 void init_keyboard(void)
95 {
96 //SDL_EnableKeyRepeat(500, 60); TODO Find if SDL2 has an equivalent.
97
98 newkey = newmouse = false;
99 keydown = mousedown = false;
100
101 SDL_ShowCursor(SDL_FALSE);
102
103 #if SDL_VERSION_ATLEAST(2, 26, 0)
104 SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_SYSTEM_SCALE, "1");
105 #endif
106 }
107
108 void mouseSetRelative(bool enable)
109 {
110 SDL_SetRelativeMouseMode(enable && windowHasFocus);
111
112 mouseRelativeEnabled = enable;
113
114 mouseWindowXRelative = 0;
115 mouseWindowYRelative = 0;
116 }
117
118 JE_word JE_mousePosition(JE_word *mouseX, JE_word *mouseY)
119 {
120 service_SDL_events(false);
121 *mouseX = mouse_x;
122 *mouseY = mouse_y;
123 return mousedown ? lastmouse_but : 0;
124 }
125
126 void mouseGetRelativePosition(Sint32 *const out_x, Sint32 *const out_y)
127 {
128 service_SDL_events(false);
129
130 scaleWindowDistanceToScreen(&mouseWindowXRelative, &mouseWindowYRelative);
131 *out_x = mouseWindowXRelative;
132 *out_y = mouseWindowYRelative;
133
134 mouseWindowXRelative = 0;
135 mouseWindowYRelative = 0;
136 }
137
138 void service_SDL_events(JE_boolean clear_new)
139 {
140 SDL_Event ev;
141
142 if (clear_new)
143 {
144 newkey = false;
145 newmouse = false;
146 new_text = false;
147 }
148
149 while (SDL_PollEvent(&ev))
150 {
151 switch (ev.type)
152 {
153 case SDL_WINDOWEVENT:
154 switch (ev.window.event)
155 {
156 case SDL_WINDOWEVENT_FOCUS_LOST:
157 windowHasFocus = false;
158
159 mouseSetRelative(mouseRelativeEnabled);
160 break;
161
162 case SDL_WINDOWEVENT_FOCUS_GAINED:
163 windowHasFocus = true;
164
165 mouseSetRelative(mouseRelativeEnabled);
166 break;
167
168 case SDL_WINDOWEVENT_RESIZED:
169 video_on_win_resize();
170 break;
171 }
172 break;
173
174 case SDL_KEYDOWN:
175 /* <alt><enter> toggle fullscreen */
176 if (ev.key.keysym.mod & KMOD_ALT && ev.key.keysym.scancode == SDL_SCANCODE_RETURN)
177 {
178 toggle_fullscreen();
179 break;
180 }
181
182 keysactive[ev.key.keysym.scancode] = 1;
183
184 newkey = true;
185 lastkey_scan = ev.key.keysym.scancode;
186 lastkey_mod = ev.key.keysym.mod;
187 keydown = true;
188
189 mouseInactive = true;
190 return;
191
192 case SDL_KEYUP:
193 keysactive[ev.key.keysym.scancode] = 0;
194 keydown = false;
195 return;
196
197 case SDL_MOUSEMOTION:
198 mouse_x = ev.motion.x;
199 mouse_y = ev.motion.y;
200 mapWindowPointToScreen(&mouse_x, &mouse_y);
201
202 if (mouseRelativeEnabled && windowHasFocus)
203 {
204 mouseWindowXRelative += ev.motion.xrel;
205 mouseWindowYRelative += ev.motion.yrel;
206 }
207
208 // Show system mouse pointer if outside screen.
209 SDL_ShowCursor(mouse_x < 0 || mouse_x >= vga_width ||
210 mouse_y < 0 || mouse_y >= vga_height ? SDL_TRUE : SDL_FALSE);
211
212 if (ev.motion.xrel != 0 || ev.motion.yrel != 0)
213 mouseInactive = false;
214 break;
215
216 case SDL_MOUSEBUTTONDOWN:
217 mouseInactive = false;
218
219 // fall through
220 case SDL_MOUSEBUTTONUP:
221 mapWindowPointToScreen(&ev.button.x, &ev.button.y);
222 if (ev.type == SDL_MOUSEBUTTONDOWN)
223 {
224 newmouse = true;
225 lastmouse_but = ev.button.button;
226 lastmouse_x = ev.button.x;
227 lastmouse_y = ev.button.y;
228 mousedown = true;
229 }
230 else
231 {
232 mousedown = false;
233 }
234 switch (ev.button.button)
235 {
236 case SDL_BUTTON_LEFT:
237 mouse_pressed[0] = mousedown; break;
238 case SDL_BUTTON_RIGHT:
239 mouse_pressed[1] = mousedown; break;
240 case SDL_BUTTON_MIDDLE:
241 mouse_pressed[2] = mousedown; break;
242 }
243 break;
244
245 case SDL_TEXTINPUT:
246 SDL_strlcpy(last_text, ev.text.text, COUNTOF(last_text));
247 new_text = true;
248 break;
249
250 case SDL_TEXTEDITING:
251 break;
252
253 case SDL_QUIT:
254 /* TODO: Call the cleanup code here. */
255 exit(0);
256 break;
257 }
258 }
259 }
260
261 void JE_clearKeyboard(void)
262 {
263 // /!\ Doesn't seems important. I think. D:
264 }