more events stuff
[voxelands-alt.git] / src / client / events_w32.c
blobb5ab480179aa61b6b7d4ac9884d7bd8eecbecc65
1 /************************************************************************
2 * events_w32.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #define _WM_EXPOSE_ALL
22 #include "wm.h"
24 #ifdef WIN32
26 #include <wctype.h>
28 int w32_key_to_sym(sym_t *s, uint32_t key)
30 uint32_t wc;
31 uint16_t str[2];
33 switch (key) {
34 case VK_LSHIFT:
35 case VK_RSHIFT:
36 case VK_SHIFT:
37 s->type = SYM_TYPE_MOD;
38 s->sym = SYM_MOD_SHIFT;
39 break;
40 case VK_LCONTROL:
41 case VK_RCONTROL:
42 s->type = SYM_TYPE_MOD;
43 s->sym = SYM_MOD_CTRL;
44 break;
45 case VK_MENU: /* alt */
46 s->type = SYM_TYPE_MOD;
47 s->sym = SYM_MOD_ALT;
48 break;
49 case VK_LWIN:
50 case VK_RWIN:
51 s->type = SYM_TYPE_MOD;
52 s->sym = SYM_MOD_SUPER;
53 break;
54 case VK_SPACE:
55 s->type = SYM_TYPE_SKEY;
56 s->sym = SYM_KEY_SPACE;
57 break;
58 case VK_ESCAPE:
59 s->type = SYM_TYPE_SKEY;
60 s->sym = SYM_KEY_ESCAPE;
61 break;
62 case VK_TAB:
63 s->type = SYM_TYPE_SKEY;
64 s->sym = SYM_KEY_TAB;
65 break;
66 case VK_NUMPAD0:
67 s->type = SYM_TYPE_SKEY;
68 s->sym = SYM_KEY_KP0;
69 break;
70 case VK_NUMPAD1:
71 s->type = SYM_TYPE_SKEY;
72 s->sym = SYM_KEY_KP1;
73 break;
74 case VK_NUMPAD2:
75 s->type = SYM_TYPE_SKEY;
76 s->sym = SYM_KEY_KP2;
77 break;
78 case VK_NUMPAD3:
79 s->type = SYM_TYPE_SKEY;
80 s->sym = SYM_KEY_KP3;
81 break;
82 case VK_NUMPAD4:
83 s->type = SYM_TYPE_SKEY;
84 s->sym = SYM_KEY_KP4;
85 break;
86 case VK_NUMPAD5:
87 s->type = SYM_TYPE_SKEY;
88 s->sym = SYM_KEY_KP5;
89 break;
90 case VK_NUMPAD6:
91 s->type = SYM_TYPE_SKEY;
92 s->sym = SYM_KEY_KP6;
93 break;
94 case VK_NUMPAD7:
95 s->type = SYM_TYPE_SKEY;
96 s->sym = SYM_KEY_KP7;
97 break;
98 case VK_NUMPAD8:
99 s->type = SYM_TYPE_SKEY;
100 s->sym = SYM_KEY_KP8;
101 break;
102 case VK_NUMPAD9:
103 s->type = SYM_TYPE_SKEY;
104 s->sym = SYM_KEY_KP9;
105 break;
106 case VK_LEFT:
107 s->type = SYM_TYPE_SKEY;
108 s->sym = SYM_KEY_LEFT;
109 break;
110 case VK_UP:
111 s->type = SYM_TYPE_SKEY;
112 s->sym = SYM_KEY_UP;
113 break;
114 case VK_RIGHT:
115 s->type = SYM_TYPE_SKEY;
116 s->sym = SYM_KEY_RIGHT;
117 break;
118 case VK_DOWN:
119 s->type = SYM_TYPE_SKEY;
120 s->sym = SYM_KEY_DOWN;
121 break;
122 case VK_F1:
123 s->type = SYM_TYPE_SKEY;
124 s->sym = SYM_KEY_F1;
125 break;
126 case VK_F2:
127 s->type = SYM_TYPE_SKEY;
128 s->sym = SYM_KEY_F2;
129 break;
130 case VK_F3:
131 s->type = SYM_TYPE_SKEY;
132 s->sym = SYM_KEY_F3;
133 break;
134 case VK_F4:
135 s->type = SYM_TYPE_SKEY;
136 s->sym = SYM_KEY_F4;
137 break;
138 case VK_F5:
139 s->type = SYM_TYPE_SKEY;
140 s->sym = SYM_KEY_F5;
141 break;
142 case VK_F6:
143 s->type = SYM_TYPE_SKEY;
144 s->sym = SYM_KEY_F6;
145 break;
146 case VK_F7:
147 s->type = SYM_TYPE_SKEY;
148 s->sym = SYM_KEY_F7;
149 break;
150 case VK_F8:
151 s->type = SYM_TYPE_SKEY;
152 s->sym = SYM_KEY_F8;
153 break;
154 case VK_F9:
155 s->type = SYM_TYPE_SKEY;
156 s->sym = SYM_KEY_F9;
157 break;
158 case VK_F10:
159 s->type = SYM_TYPE_SKEY;
160 s->sym = SYM_KEY_F10;
161 break;
162 case VK_F11:
163 s->type = SYM_TYPE_SKEY;
164 s->sym = SYM_KEY_F11;
165 break;
166 case VK_F12:
167 s->type = SYM_TYPE_SKEY;
168 s->sym = SYM_KEY_F12;
169 break;
170 case VK_BACK:
171 s->type = SYM_TYPE_SKEY;
172 s->sym = SYM_KEY_BCKSPC;
173 break;
174 case VK_RETURN:
175 s->type = SYM_TYPE_SKEY;
176 s->sym = SYM_KEY_RETURN;
177 break;
178 case VK_DELETE:
179 s->type = SYM_TYPE_SKEY;
180 s->sym = SYM_KEY_DELETE;
181 break;
182 case VK_MULTIPLY:
183 s->type = SYM_TYPE_SKEY;
184 s->sym = SYM_KEY_KPMUL;
185 break;
186 case VK_ADD:
187 s->type = SYM_TYPE_SKEY;
188 s->sym = SYM_KEY_KPADD;
189 break;
190 case VK_SUBTRACT:
191 s->type = SYM_TYPE_SKEY;
192 s->sym = SYM_KEY_KPSUB;
193 break;
194 case VK_DECIMAL:
195 s->type = SYM_TYPE_SKEY;
196 s->sym = SYM_KEY_KPDOT;
197 break;
198 case VK_DIVIDE:
199 s->type = SYM_TYPE_SKEY;
200 s->sym = SYM_KEY_KPDIV;
201 break;
202 case VK_CLEAR:
203 case VK_PAUSE:
204 case VK_SCROLL:
205 case VK_HOME:
206 case VK_PRIOR:
207 case VK_NEXT:
208 case VK_END:
209 case VK_SELECT:
210 case VK_PRINT:
211 case VK_EXECUTE:
212 case VK_INSERT:
213 case VK_CANCEL:
214 case VK_HELP:
215 case VK_NUMLOCK:
216 case VK_CAPITAL:
217 break;
218 default:
219 wc = MapVirtualKey(key,2);
220 if (wc) {
221 str[0] = LOWORD(wc);
222 str[1] = 0;
223 s->sym = utf16_toutf32(str);
224 if (!iswgraph(s->sym))
225 s->sym = 0;
230 return 0;
233 /* oh god w32, wtf */
234 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
236 event_t e;
237 uint16_t buff[5];
238 BYTE ks[256];
239 e.type = EVENT_NONE;
240 e.sym.type = SYM_TYPE_NONE;
241 e.sym.sym = 0;
242 e.sym.ch = 0;
244 switch (uMsg) {
245 case WM_ACTIVATE:
246 return 0;
247 case WM_SYSCOMMAND:
249 switch (wParam) {
250 case SC_SCREENSAVE:
251 case SC_MONITORPOWER:
252 return 0;
254 break;
256 case WM_CLOSE:
257 PostQuitMessage(0);
258 return 0;
259 case WM_KEYDOWN:
261 /* this should put the character created by the key into e.sym.ch,
262 * and either the unmodified (ignore shift, etc) character, or special value into e.sym.sym */
263 e.type = EVENT_KEY_DOWN;
264 GetKeyboardState(ks);
265 if (ToUnicode(wParam, MapVirtualKey(wParam,0), ks, (LPWSTR)buff, 5, 0) > 0)
266 e.ch = utf16_toutf32(buff);
267 w32_key_to_sym(&e.sym,wParam);
268 if (e.sym.sym)
269 events_handle(&e);
270 return 0;
272 case WM_KEYUP:
274 /* this should put the unmodified (ignore shift, etc) character, or special value into e.sym.sym */
275 e.type = EVENT_KEY_UP;
276 w32_key_to_sym(&e.sym,wParam);
277 if (e.sym.sym)
278 events_handle(&e);
279 return 0;
281 case WM_SIZE:
283 wm_data.size.width = LOWORD(lParam);
284 wm_data.size.height = HIWORD(lParam);
285 return 0;
287 case WM_LBUTTONDOWN:
289 e.type = EVENT_BUTTON_DOWN;
290 e.sym.type = SYM_TYPE_MOUSE;
291 e.sym.sym = MOUSE_BUTTON_LEFT;
292 events_handle(&e);
293 return 0;
295 case WM_LBUTTONUP:
297 e.type = EVENT_BUTTON_UP;
298 e.sym.type = SYM_TYPE_MOUSE;
299 e.sym.sym = MOUSE_BUTTON_LEFT;
300 events_handle(&e);
301 return 0;
303 case WM_MBUTTONDOWN:
305 e.type = EVENT_BUTTON_DOWN;
306 e.sym.type = SYM_TYPE_MOUSE;
307 e.sym.sym = MOUSE_BUTTON_CENTRE;
308 events_handle(&e);
309 return 0;
311 case WM_MBUTTONUP:
313 e.type = EVENT_BUTTON_UP;
314 e.sym.type = SYM_TYPE_MOUSE;
315 e.sym.sym = MOUSE_BUTTON_CENTRE;
316 events_handle(&e);
317 return 0;
319 case WM_RBUTTONDOWN:
321 e.type = EVENT_BUTTON_DOWN;
322 e.sym.type = SYM_TYPE_MOUSE;
323 e.sym.sym = MOUSE_BUTTON_RIGHT;
324 events_handle(&e);
325 return 0;
327 case WM_RBUTTONUP:
329 e.type = EVENT_BUTTON_UP;
330 e.sym.type = SYM_TYPE_MOUSE;
331 e.sym.sym = MOUSE_BUTTON_RIGHT;
332 events_handle(&e);
333 return 0;
335 case WM_MOUSEMOVE:
337 e.type = EVENT_MOUSE_MOTION;
338 e.x = LOWORD(lParam);
339 e.y = HIWORD(lParam);
340 events_handle(&e);
341 return 0;
343 case WM_MOUSEWHEEL:
345 e.type = EVENT_BUTTON_DOWN;
346 e.sym.type = SYM_TYPE_MOUSE;
347 if (wParam == VK_XBUTTON1) {
348 e.sym.sym = MOUSE_BUTTON_UP;
349 events_handle(&e);
350 }else if (wParam == VK_XBUTTON2) {
351 e.sym.sym = MOUSE_BUTTON_DOWN;
352 events_handle(&e);
354 return 0;
358 /* pass unhandled messages to DefWindowProc */
359 return DefWindowProc(hWnd,uMsg,wParam,lParam);
363 /* read and act on events */
364 void events_main()
366 MSG msg;
367 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
368 if (msg.message == WM_QUIT) {
369 client_state(VLSTATE_EXIT);
370 }else{
371 TranslateMessage(&msg);
372 DispatchMessage(&msg);
375 events_trigger_active();
377 #endif