1 //This file is part of Glest Shared Library (www.glest.org)
2 //Copyright (C) 2005 Matthias Braun <matze@braunis.de>
3
4 //You can redistribute this code and/or modify it under
5 //the terms of the GNU General Public License as published by the Free Software
6 //Foundation; either version 2 of the License, or (at your option) any later
7 //version.
8
9 #include "window.h"
10
11 #include <iostream>
12 #include <stdexcept>
13 #include <cassert>
14 #include <cctype>
15
16 #include "conversion.h"
17 #include "platform_util.h"
18 #include "sdl_private.h"
19 #include "noimpl.h"
20 #include "util.h"
21 #include "opengl.h"
22
23 #ifdef WIN32
24
25 #include "SDL_syswm.h"
26
27 #endif
28
29 #include "leak_dumper.h"
30
31 using namespace Shared::Util;
32 using namespace std;
33
34 namespace Shared{ namespace Platform{
35
36 // =======================================
37 // WINDOW
38 // =======================================
39
40 // ========== STATIC INITIALIZATIONS ==========
41
42 static Window* global_window = 0;
43 static int oldX=0,oldY=0;
44 SDL_Window *Window::sdlWindow = 0;
45 int64 Window::lastMouseEvent = 0; /** for use in mouse hover calculations */
46 Vec2i Window::mousePos;
47 MouseState Window::mouseState;
48 bool Window::isKeyPressedDown = false;
49 bool Window::isFullScreen = false;
50 SDL_keysym Window::keystate;
51 int64 Window::lastToggle = -1000;
52
53 bool Window::isActive = false;
54 #ifdef WIN32
55 bool Window::allowAltEnterFullscreenToggle = false;
56 #else
57 bool Window::allowAltEnterFullscreenToggle = true;
58 #endif
59 int Window::lastShowMouseState = 0;
60
61 bool Window::tryVSynch = false;
62
63 map<wchar_t,bool> Window::mapAllowedKeys;
64
65 //bool Window::masterserverMode = false;
66
67 // ========== PUBLIC ==========
68
69 #ifdef WIN32
70
GetSDLWindow()71 static HWND GetSDLWindow()
72 {
73 SDL_SysWMinfo info;
74 SDL_VERSION(&info.version);
75 if (SDL_GetWindowWMInfo(Window::getSDLWindow(),&info) == -1)
76 return NULL;
77 return info.info.win.window;
78 }
79
80 #endif
81
isUnprintableChar(SDL_keysym key,SDL_Keymod mod)82 static bool isUnprintableChar(SDL_keysym key, SDL_Keymod mod) {
83 switch (key.sym) {
84 // We want to allow some, which are handled specially
85 case SDLK_RETURN:
86 case SDLK_TAB:
87 case SDLK_BACKSPACE:
88 case SDLK_DELETE:
89 case SDLK_HOME:
90 case SDLK_END:
91 case SDLK_LEFT:
92 case SDLK_RIGHT:
93 case SDLK_UP:
94 case SDLK_DOWN:
95 case SDLK_PAGEUP:
96 case SDLK_PAGEDOWN:
97 return true;
98 default:// do nothing
99 break;
100 }
101 // U+0000 to U+001F are control characters
102 /* Don't post text events for unprintable characters */
103
104 if(StartsWith(SDL_GetKeyName(key.sym),"SDLK_KP")){
105 return false;
106 }
107 if (key.sym > 127) {
108 return true;
109 }
110 if(key.sym < 0x20) {
111 return true;
112 }
113
114 //printf("isUnprintableChar returns false for [%d]\n",key.sym);
115 return false;
116
117 }
118
Window()119 Window::Window() {
120 this->sdlWindow=0;
121 // Default to 1x1 until set by caller to avoid divide by 0
122 //this->w = 1;
123 //this->h = 1;
124
125 for(int idx = 0; idx < mbCount; idx++) {
126 lastMouseDown[idx] = 0;
127 lastMouseX[idx] = 0;
128 lastMouseY[idx] = 0;
129 }
130
131 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
132 assert(global_window == 0);
133
134 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
135
136 global_window = this;
137 Window::isActive = true;
138
139 lastMouseEvent = 0;
140 mousePos = Vec2i(0);
141 mouseState.clear();
142
143 #ifdef WIN32
144 init_win32();
145 #endif
146 }
147
Window(SDL_Window * sdlWindow)148 Window::Window(SDL_Window *sdlWindow) {
149 this->sdlWindow=sdlWindow;
150 // Default to 1x1 until set by caller to avoid divide by 0
151 //this->w = 1;
152 //this->h = 1;
153
154 for(int idx = 0; idx < mbCount; idx++) {
155 lastMouseDown[idx] = 0;
156 lastMouseX[idx] = 0;
157 lastMouseY[idx] = 0;
158 }
159
160 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
161 assert(global_window == 0);
162
163 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
164
165 global_window = this;
166 Window::isActive = true;
167
168 lastMouseEvent = 0;
169 mousePos = Vec2i(0);
170 mouseState.clear();
171
172 #ifdef WIN32
173 init_win32();
174 #endif
175 }
176
~Window()177 Window::~Window() {
178 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
179 #ifdef WIN32
180 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
181 done_win32();
182 #endif
183
184 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
185 assert(global_window == this);
186 global_window = 0;
187 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
188 }
189
setSDLWindow(SDL_Window * window)190 void Window::setSDLWindow(SDL_Window *window) {
191 Window::sdlWindow = window;
192 }
getSDLWindow()193 SDL_Window *Window::getSDLWindow() {
194 return Window::sdlWindow;
195 }
196
handleEvent()197 bool Window::handleEvent() {
198 string codeLocation = "a";
199
200 SDL_Event event;
201 SDL_GetMouseState(&oldX,&oldY);
202
203 //codeLocation = "b";
204
205 SDL_StartTextInput();
206 while(SDL_PollEvent(&event)) {
207 try {
208 codeLocation = "c";
209
210 switch(event.type) {
211 case SDL_MOUSEBUTTONDOWN:
212 case SDL_MOUSEBUTTONUP:
213 case SDL_MOUSEMOTION:
214
215 //printf("In [%s::%s] Line :%d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
216 codeLocation = "d";
217
218 setLastMouseEvent(Chrono::getCurMillis());
219 setMousePos(Vec2i(event.button.x, event.button.y));
220 break;
221 }
222
223 codeLocation = "d";
224
225 switch(event.type) {
226 case SDL_QUIT:
227 //printf("In [%s::%s] Line :%d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
228 codeLocation = "e";
229 return false;
230 case SDL_MOUSEBUTTONDOWN:
231 //printf("In [%s::%s] Line :%d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
232 codeLocation = "f";
233
234 if(global_window) {
235 global_window->handleMouseDown(event);
236 }
237 break;
238 case SDL_MOUSEBUTTONUP: {
239 //printf("In [%s::%s] Line :%d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
240 codeLocation = "g";
241 if(global_window) {
242 MouseButton b = getMouseButton(event.button.button);
243 setMouseState(b, false);
244
245 global_window->eventMouseUp(event.button.x,
246 event.button.y,getMouseButton(event.button.button));
247 }
248 break;
249 }
250 case SDL_MOUSEWHEEL: {
251 //printf("In [%s::%s] Line :%d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
252 codeLocation = "g2";
253 if(global_window) {
254 global_window->handleMouseWheel(event);
255 }
256 break;
257 }
258 case SDL_MOUSEMOTION: {
259 //printf("In [%s::%s] Line :%d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
260 //MouseState ms;
261 //ms.leftMouse = (event.motion.state & SDL_BUTTON_LMASK) != 0;
262 //ms.rightMouse = (event.motion.state & SDL_BUTTON_RMASK) != 0;
263 //ms.centerMouse = (event.motion.state & SDL_BUTTON_MMASK) != 0;
264 codeLocation = "h";
265
266 setMouseState(mbLeft, (event.motion.state & SDL_BUTTON_LMASK) == SDL_BUTTON_LMASK);
267 setMouseState(mbRight, (event.motion.state & SDL_BUTTON_RMASK) == SDL_BUTTON_RMASK);
268 setMouseState(mbCenter, (event.motion.state & SDL_BUTTON_MMASK) == SDL_BUTTON_MMASK);
269
270 if(global_window) {
271 global_window->eventMouseMove(event.motion.x, event.motion.y, &getMouseState()); //&ms);
272 }
273 break;
274 }
275 case SDL_TEXTINPUT: {
276 //case SDL_TEXTEDITING:
277 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== START OF SDL SDL_TEXTINPUT ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
278 codeLocation = "i";
279 Window::isKeyPressedDown = true;
280 //#ifdef WIN32
281 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("KD mod = %d : %d\n",event.key.keysym.mod,SDL_GetModState());
282 event.key.keysym.mod = SDL_GetModState();
283 //#endif
284
285 string keyName = SDL_GetKeyName(event.text.text[0]);
286 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] Raw SDL key [%d - %c] mod [%d] scancode [%d] keyName [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,event.key.keysym.sym,event.key.keysym.sym,event.key.keysym.mod,event.key.keysym.scancode,keyName.c_str());
287 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Raw SDL key [%d] mod [%d] scancode [%d] keyName [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,event.key.keysym.sym,event.key.keysym.mod,event.key.keysym.scancode,keyName.c_str());
288
289 #ifdef WIN32
290 /* handle ALT+f4 */
291 if((keyName == "f4" || keyName == "F4")
292 && (event.key.keysym.mod & (KMOD_LALT | KMOD_RALT))) {
293 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d] ALT-F4 pressed.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
294 return false;
295 }
296 #endif
297 if(global_window) {
298 if(global_window->eventTextInput(event.text.text) == false) {
299 event.key.keysym.sym = event.text.text[0];
300 global_window->eventKeyDown(event.key);
301 global_window->eventKeyPress(event.key);
302 }
303 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
304 }
305 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== END OF SDL SDL_TEXTINPUT ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
306 break;
307 }
308 case SDL_KEYDOWN: {
309 //printf("In SDL_KEYDOWN\n");
310 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== START OF SDL SDL_KEYDOWN ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
311 keystate = event.key.keysym;
312 bool keyDownConsumed=false;
313 if(global_window) {
314 keyDownConsumed=global_window->eventSdlKeyDown(event.key);
315 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
316 switch (event.key.keysym.sym) {
317 case SDLK_v: {
318 if (event.key.keysym.mod & KMOD_CTRL) {
319 /* Ctrl-V, paste form clipbord */
320 char *text = SDL_GetClipboardText();
321 if (*text) {
322 printf("Clipboard text: %s\n", text);
323 if(global_window->eventTextInput(text) == true) {
324 keyDownConsumed=true;
325 }
326 } else {
327 printf("Clipboard text is empty\n");
328 }
329 SDL_free(text);
330 }
331 break;
332 }
333 default:
334 break;
335 }
336 }
337
338 // // Stop keys which would be handled twice ( one time as text input, one time as key down )
339 SDL_Keymod mod = SDL_GetModState();
340 if (!isUnprintableChar(event.key.keysym,mod)) {
341 //printf("In SDL_KEYDOWN key SKIP [%d]\n",event.key.keysym.sym);
342 break;
343 }
344 codeLocation = "i";
345 Window::isKeyPressedDown = true;
346 //#ifdef WIN32
347 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("KD mod = %d : %d\n",event.key.keysym.mod,SDL_GetModState());
348 event.key.keysym.mod = SDL_GetModState();
349 //#endif
350 string keyName = SDL_GetKeyName(event.key.keysym.sym);
351 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] Raw SDL key [%d - %c] mod [%d] scancode [%d] keyName [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,event.key.keysym.sym,event.key.keysym.sym,event.key.keysym.mod,event.key.keysym.scancode,keyName.c_str());
352 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Raw SDL key [%d] mod [%d] scancode [%d] keyName [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,event.key.keysym.sym,event.key.keysym.mod,event.key.keysym.scancode,keyName.c_str());
353
354 //printf("In SDL_KEYDOWN key [%d] keyName [%s] mod: %d\n",event.key.keysym.sym,keyName.c_str(),event.key.keysym.mod);
355
356 // handle ALT+Return
357 if( (keyName == "Return" || keyName == "Enter")
358 && (event.key.keysym.mod & (KMOD_LALT | KMOD_RALT))) {
359 if(event.key.repeat!=0) break;
360 if (Chrono::getCurMillis() - getLastToggle() > 100) {
361 toggleFullscreen();
362 setLastToggle(Chrono::getCurMillis());
363 };
364 keyDownConsumed=true;
365 }
366 #ifdef WIN32
367 // handle ALT+f4
368 if((keyName == "f4" || keyName == "F4")
369 && (event.key.keysym.mod & (KMOD_LALT | KMOD_RALT))) {
370 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d] ALT-F4 pressed.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
371 return false;
372 }
373 #endif
374 if(global_window) {
375 //char key = getKey(event.key.keysym,true);
376 //key = tolower(key);
377 //if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("******************* key [%d]\n",key);
378
379 //event.key.keysym.mod = SDL_GetModState();
380 if(!keyDownConsumed){
381 global_window->eventKeyDown(event.key);
382 global_window->eventKeyPress(event.key);
383 }
384
385 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
386 }
387
388 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== END OF SDL SDL_KEYDOWN ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
389 break;
390 }
391 case SDL_KEYUP:{
392 //printf("In [%s::%s] Line :%d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
393
394 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== START OF SDL SDL_KEYUP ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
395
396 codeLocation = "j";
397
398 Window::isKeyPressedDown = false;
399 //#ifdef WIN32
400 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("KU mod = %d : %d\n",event.key.keysym.mod,SDL_GetModState());
401 event.key.keysym.mod = SDL_GetModState();
402 //#endif
403
404 keystate = event.key.keysym;
405
406 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] KEY_UP, Raw SDL key [%d] mod [%d] scancode [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,event.key.keysym.sym,event.key.keysym.mod,event.key.keysym.scancode);
407 //printf("In [%s::%s Line: %d] KEY_UP, Raw SDL key [%d] mod [%d] scancode [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,event.key.keysym.sym,event.key.keysym.mod,event.key.keysym.scancode);
408
409 if(global_window) {
410 global_window->eventKeyUp(event.key);
411 }
412
413 // here is the problem, we have with too many key up events:
414 // string keyName = SDL_GetKeyName(event.key.keysym.sym);
415 // if( (keyName == "Return" || keyName == "Enter")){
416 // setLastToggle(-1000);
417 // }
418
419 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== END OF SDL SDL_KEYUP ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
420
421 break;
422 }
423 case SDL_WINDOWEVENT:
424 {
425 codeLocation = "k";
426 // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] SDL_ACTIVEEVENT event.active.state = %d event.active. = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,event.active.state,event.active.gain);
427 //
428 // if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] Window::isActive = %d event.active.state = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,Window::isActive,event.active.state);
429 //
430 // // Check if the program has lost window focus
431 // if ((event.active.state & (SDL_APPACTIVE | SDL_APPINPUTFOCUS))) {
432 // if (event.active.gain == 0) {
433 // Window::isActive = false;
434 // }
435 // else {
436 // Window::isActive = true;
437 // }
438 //
439 // if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] Window::isActive = %d \n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,Window::isActive);
440 //
441 // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,Window::isActive);
442 //
443 // bool willShowCursor = (!Window::isActive || (Window::lastShowMouseState == SDL_ENABLE) || Window::getUseDefaultCursorOnly());
444 // showCursor(willShowCursor);
445 // }
446
447 //printf("In SDL_WINDOWEVENT, event.window.event: %d\n",event.window.event);
448
449 /*
450 switch(event.window.event) {
451 case SDL_WINDOWEVENT_ENTER:
452 printf("In SDL_WINDOWEVENT_ENTER\n");
453 showCursor(true);
454 break;
455 case SDL_WINDOWEVENT_LEAVE:
456 printf("In SDL_WINDOWEVENT_LEAVE\n");
457 showCursor(false);
458 break;
459 case SDL_WINDOWEVENT_FOCUS_GAINED:
460 printf("SDL_WINDOWEVENT_FOCUS_GAINED\n");
461 showCursor(true);
462 break;
463 case SDL_WINDOWEVENT_FOCUS_LOST:
464 printf("SDL_WINDOWEVENT_FOCUS_LOST\n");
465 showCursor(false);
466 break;
467 }
468 */
469 //showCursor(false);
470
471 if(global_window) {
472 global_window->eventWindowEvent(event.window);
473 }
474
475 }
476 break;
477 }
478 }
479 catch(const char *e){
480 SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] (a1) Couldn't process event: [%s] codeLocation = %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e,codeLocation.c_str());
481 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] (a1) Couldn't process event: [%s] codeLocation = %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e,codeLocation.c_str());
482 throw megaglest_runtime_error(e);
483 }
484 catch(const std::runtime_error& e) {
485 SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] (a2) Couldn't process event: [%s] codeLocation = %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what(),codeLocation.c_str());
486 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] (a2) Couldn't process event: [%s] codeLocation = %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what(),codeLocation.c_str());
487 throw megaglest_runtime_error(e.what());
488 }
489 catch(const std::exception& e) {
490 SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] (b) Couldn't process event: [%s] codeLocation = %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what(),codeLocation.c_str());
491 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] (b) Couldn't process event: [%s] codeLocation = %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what(),codeLocation.c_str());
492 }
493 catch(...) {
494 SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] (c) Couldn't process event: [UNKNOWN ERROR] codeLocation = %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,codeLocation.c_str());
495 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] (c) Couldn't process event: [UNKNOWN ERROR] codeLocation = %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,codeLocation.c_str());
496 }
497 }
498 SDL_StopTextInput();
499
500 return true;
501 }
502
revertMousePos()503 void Window::revertMousePos() {
504 SDL_WarpMouseInWindow(sdlWindow,oldX, oldY);
505 }
506
getOldMousePos()507 Vec2i Window::getOldMousePos() {
508 return Vec2i(oldX, oldY);
509 }
510
getText()511 string Window::getText() {
512 const char* c = 0;
513 //SDL_WM_GetCaption(&c, 0);
514 c=SDL_GetWindowTitle(sdlWindow);
515
516 return string(c);
517 }
518
getAspect()519 float Window::getAspect() {
520 return static_cast<float>(getClientH())/getClientW();
521 }
522
setText(string text)523 void Window::setText(string text) {
524 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
525 //SDL_WM_SetCaption(text.c_str(), 0);
526 SDL_SetWindowTitle(sdlWindow,text.c_str());
527 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
528 }
529
setSize(int w,int h)530 void Window::setSize(int w, int h) {
531 //this->w = w;
532 //this->h = h;
533 Private::ScreenWidth = w;
534 Private::ScreenHeight = h;
535 }
536
setPos(int x,int y)537 void Window::setPos(int x, int y) {
538 if(x != 0 || y != 0) {
539 NOIMPL;
540 return;
541 }
542 }
543
minimize()544 void Window::minimize() {
545 NOIMPL;
546 }
547
setEnabled(bool enabled)548 void Window::setEnabled(bool enabled) {
549 NOIMPL;
550 }
551
setVisible(bool visible)552 void Window::setVisible(bool visible) {
553 NOIMPL;
554 }
555
setStyle(WindowStyle windowStyle)556 void Window::setStyle(WindowStyle windowStyle) {
557 if(windowStyle == wsFullscreen)
558 return;
559 // NOIMPL;
560 }
561
create()562 void Window::create() {
563 // nothing here
564 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
565 #ifdef WIN32
566 ontop_win32(this->getScreenWidth(),this->getScreenHeight());
567 #endif
568 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
569 }
570
destroy()571 void Window::destroy() {
572 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
573
574 SDL_Event event;
575 event.type = SDL_QUIT;
576 SDL_PushEvent(&event);
577
578 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
579 }
580
setupGraphicsScreen(int depthBits,int stencilBits,bool hardware_acceleration,bool fullscreen_anti_aliasing)581 void Window::setupGraphicsScreen(int depthBits, int stencilBits, bool hardware_acceleration, bool fullscreen_anti_aliasing) {
582 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
583
584 static int newDepthBits = depthBits;
585 static int newStencilBits = stencilBits;
586 if(depthBits >= 0)
587 newDepthBits = depthBits;
588 if(stencilBits >= 0)
589 newStencilBits = stencilBits;
590
591 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
592
593 if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) {
594 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
595
596 if(fullscreen_anti_aliasing == true) {
597 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1);
598 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
599 }
600 if(hardware_acceleration == true) {
601 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
602 }
603
604 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
605
606 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
607 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 1);
608 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 1);
609 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 1);
610 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, newStencilBits);
611 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, newDepthBits);
612
613 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
614
615 //const SDL_VideoInfo *info = SDL_GetVideoInfo();
616 #ifdef SDL_GL_SWAP_CONTROL
617 if(Window::tryVSynch == true) {
618 /* we want vsync for smooth scrolling */
619 //SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
620 SDL_GL_SetSwapInterval(1);
621 }
622 #endif
623
624 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
625
626 // setup LOD bias factor
627 //const float lodBias = std::max(std::min( configHandler->Get("TextureLODBias", 0.0f) , 4.0f), -4.0f);
628 const float lodBias = max(min(0.0f,4.0f),-4.0f);
629 //if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("\n\n\n\n\n$$$$ In [%s::%s Line: %d] lodBias = %f\n\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,lodBias);
630 if (std::fabs(lodBias) > 0.01f) {
631 glTexEnvf(GL_TEXTURE_FILTER_CONTROL,GL_TEXTURE_LOD_BIAS, lodBias );
632 }
633 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
634 }
635 }
636
toggleFullscreen()637 void Window::toggleFullscreen() {
638 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
639 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
640
641 Window::isFullScreen = !Window::isFullScreen;
642
643 if(global_window) {
644 global_window->eventToggleFullScreen(Window::isFullScreen);
645 }
646 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
647 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
648 }
649
handleMouseWheel(SDL_Event event)650 void Window::handleMouseWheel(SDL_Event event) {
651 int x;
652 int y;
653
654 if (event.type != SDL_MOUSEWHEEL) {
655 return;
656 }
657
658 if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled)
659 SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
660
661 SDL_GetMouseState(&x, &y);
662
663 // // windows implementation uses 120 for the resolution of a standard mouse
664 // // wheel notch. However, newer mice have finer resolutions. I dunno if SDL
665 // // handles those, but for now we're going to say that each mouse wheel
666 // // movement is 120.
667 eventMouseWheel(x, y, event.wheel.y * 120);
668 return;
669 }
670
handleMouseDown(SDL_Event event)671 void Window::handleMouseDown(SDL_Event event) {
672 static const Uint32 DOUBLECLICKTIME = 500;
673
674 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
675
676 MouseButton button = getMouseButton(event.button.button);
677
678 Uint32 ticks = SDL_GetTicks();
679 int n = (int) button;
680
681 assert(n >= 0 && n < mbCount);
682 if(n >= 0 && n < mbCount) {
683 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
684
685 static const int DOUBLECLICKDELTA = 5;
686 if(ticks - lastMouseDown[n] < DOUBLECLICKTIME
687 && abs(lastMouseX[n] - event.button.x) < DOUBLECLICKDELTA
688 && abs(lastMouseY[n] - event.button.y) < DOUBLECLICKDELTA) {
689
690 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
691 eventMouseDown(event.button.x, event.button.y, button);
692 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
693 eventMouseDoubleClick(event.button.x, event.button.y, button);
694 }
695 else {
696 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
697 eventMouseDown(event.button.x, event.button.y, button);
698 }
699 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
700
701 lastMouseDown[n] = ticks;
702 lastMouseX[n] = event.button.x;
703 lastMouseY[n] = event.button.y;
704
705 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
706 }
707 }
708
getMouseButton(int sdlButton)709 MouseButton Window::getMouseButton(int sdlButton) {
710 switch(sdlButton) {
711 case SDL_BUTTON_LEFT:
712 return mbLeft;
713 case SDL_BUTTON_RIGHT:
714 return mbRight;
715 case SDL_BUTTON_MIDDLE:
716 return mbCenter;
717 default:
718 //throw std::runtime_error("Mouse Button > 3 not handled.");
719 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Mouse Button [%d] not handled.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,sdlButton);
720
721 return mbUnknown;
722 }
723 }
724
convertStringtoSDLKey(const string & value)725 wchar_t Window::convertStringtoSDLKey(const string &value) {
726 wchar_t result = SDLK_UNKNOWN;
727
728 if(value.length() >= 1) {
729 if(value.length() == 3 && value[0] == '\'' && value[2] == '\'') {
730 result = (SDL_Keycode)value[1];
731 }
732 else {
733 bool foundKey = false;
734 if(value.length() > 1) {
735 for(int i = SDLK_UNKNOWN; i < SDL_NUM_SCANCODES; ++i) {
736 SDL_Keycode key = static_cast<SDL_Keycode>(i);
737 string keyName = SDL_GetKeyName(key);
738 if(value == keyName) {
739 result = key;
740 foundKey = true;
741 break;
742 }
743 }
744 }
745
746 if(foundKey == false) {
747 result = (SDL_Keycode)value[0];
748 }
749 }
750 }
751 else {
752 string sError = "Unsupported key name: [" + value + "]";
753 throw megaglest_runtime_error(sError.c_str());
754 }
755
756 // Because SDL is based on lower Ascii
757 //result = tolower(result);
758 return result;
759 }
760
addAllowedKeys(string keyList)761 void Window::addAllowedKeys(string keyList) {
762 clearAllowedKeys();
763
764 if(keyList.empty() == false) {
765 vector<string> keys;
766 Tokenize(keyList,keys,",");
767 if(keys.empty() == false) {
768 for(unsigned int keyIndex = 0; keyIndex < keys.size(); ++keyIndex) {
769 string key = trim(keys[keyIndex]);
770
771 wchar_t sdl_key = convertStringtoSDLKey(key);
772 if(sdl_key != SDLK_UNKNOWN) {
773 mapAllowedKeys[sdl_key] = true;
774 }
775
776 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("key: %d [%s] IS ALLOWED\n",sdl_key, key.c_str());
777 //printf("key: %d [%s] IS ALLOWED\n",sdl_key, key.c_str());
778 }
779 }
780 }
781 }
clearAllowedKeys()782 void Window::clearAllowedKeys() {
783 mapAllowedKeys.clear();
784 }
785
isAllowedKey(wchar_t key)786 bool Window::isAllowedKey(wchar_t key) {
787 map<wchar_t,bool>::const_iterator iterFind = mapAllowedKeys.find(key);
788 bool result =(iterFind != mapAllowedKeys.end());
789
790 if(SystemFlags::VERBOSE_MODE_ENABLED) {
791 string keyName = SDL_GetKeyName((SDL_Keycode)key);
792 printf("key: %d [%s] allowed result: %d\n",key,keyName.c_str(),result);
793 }
794
795 return result;
796 }
797
isKeyPressed(SDL_Keycode compareKey,SDL_KeyboardEvent input,bool modifiersAllowed)798 bool isKeyPressed(SDL_Keycode compareKey, SDL_KeyboardEvent input,bool modifiersAllowed) {
799 vector<int> modifiersToCheck;
800 if(modifiersAllowed == false) {
801 modifiersToCheck.push_back(KMOD_LCTRL);
802 modifiersToCheck.push_back(KMOD_RCTRL);
803 modifiersToCheck.push_back(KMOD_LALT);
804 modifiersToCheck.push_back(KMOD_RALT);
805 }
806
807 bool result = isKeyPressed(compareKey, input, modifiersToCheck);
808 return result;
809 }
isKeyPressed(SDL_Keycode compareKey,SDL_KeyboardEvent input,vector<int> modifiersToCheck)810 bool isKeyPressed(SDL_Keycode compareKey, SDL_KeyboardEvent input,vector<int> modifiersToCheck) {
811 //Uint16 c = SDLK_UNKNOWN;
812 SDL_Keycode c = SDLK_UNKNOWN;
813 //if(input.keysym.unicode > 0 && input.keysym.unicode < 0x80) {
814 if(input.keysym.sym > 0) {
815 c = input.keysym.sym;
816 }
817
818 //printf("START isKeyPressed input = %d compare = %d mod = %d\n",c,compareKey,input.keysym.mod);
819
820
821 // if(compareKey == SDLK_QUESTION && (c == SDLK_SLASH && (input.keysym.mod & (KMOD_SHIFT)))) {
822 // return true;
823 // }
824 // else if(compareKey == SDLK_SLASH && (c == SDLK_SLASH && (input.keysym.mod & (KMOD_SHIFT)))) {
825 // return false;
826 // }
827
828 //// string unicodeKeyName = SDL_GetKeyName((SDLKey)input.keysym.unicode);
829 ////
830 //// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] input.keysym.unicode = %d input.keysym.mod = %d input.keysym.sym = %d unicodeKeyName [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,input.keysym.unicode,input.keysym.mod,input.keysym.sym,unicodeKeyName.c_str());
831 //// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] input.keysym.unicode = %d input.keysym.mod = %d input.keysym.sym = %d unicodeKeyName [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,input.keysym.unicode,input.keysym.mod,input.keysym.sym,unicodeKeyName.c_str());
832 //
833 // // When modifiers are pressed the unicode result is wrong
834 // // example CTRL-3 will give the ESCAPE vslue 27 in unicode
835 // if( !(input.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) &&
836 // !(input.keysym.mod & (KMOD_LALT | KMOD_RALT)) &&
837 // !(input.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT)) ) {
838 // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
839 // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
840 //
841 // c = input.keysym.sym;
842 // //c = toupper(c);
843 // }
844 // else if((input.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT)) &&
845 // (input.keysym.sym == SDLK_QUESTION ||
846 // input.keysym.sym == SDLK_AT ||
847 // input.keysym.sym == SDLK_COLON ||
848 // input.keysym.sym == SDLK_LESS ||
849 // input.keysym.sym == SDLK_GREATER ||
850 // input.keysym.sym == SDLK_CARET ||
851 // input.keysym.sym == SDLK_UNDERSCORE ||
852 // input.keysym.sym == SDLK_BACKQUOTE ||
853 // input.keysym.sym == SDLK_EXCLAIM ||
854 // input.keysym.sym == SDLK_QUOTEDBL ||
855 // input.keysym.sym == SDLK_HASH ||
856 // input.keysym.sym == SDLK_DOLLAR ||
857 // input.keysym.sym == SDLK_AMPERSAND ||
858 // input.keysym.sym == SDLK_QUOTE ||
859 // input.keysym.sym == SDLK_LEFTPAREN ||
860 // input.keysym.sym == SDLK_RIGHTPAREN ||
861 // input.keysym.sym == SDLK_ASTERISK ||
862 // input.keysym.sym == SDLK_KP_MULTIPLY ||
863 // input.keysym.sym == SDLK_PLUS ||
864 // input.keysym.sym == SDLK_COMMA ||
865 // input.keysym.sym == SDLK_MINUS ||
866 // input.keysym.sym == SDLK_PERIOD ||
867 // input.keysym.sym == SDLK_SLASH ||
868 // // Need to allow Shift + # key for AZERTY style keyboards
869 // input.keysym.sym == SDLK_0 ||
870 // input.keysym.sym == SDLK_1 ||
871 // input.keysym.sym == SDLK_2 ||
872 // input.keysym.sym == SDLK_3 ||
873 // input.keysym.sym == SDLK_4 ||
874 // input.keysym.sym == SDLK_5 ||
875 // input.keysym.sym == SDLK_6 ||
876 // input.keysym.sym == SDLK_7 ||
877 // input.keysym.sym == SDLK_8 ||
878 // input.keysym.sym == SDLK_9
879 // )) {
880 // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
881 // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
882 //
883 // c = input.keysym.sym;
884 // }
885 // else if(input.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
886 // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
887 // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
888 //
889 // if( (input.keysym.sym >= SDLK_0 && input.keysym.sym <= SDLK_9) ||
890 // (input.keysym.sym >= SDLK_KP_0 && input.keysym.sym <= SDLK_KP_9)) {
891 // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
892 // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
893 //
894 // c = input.keysym.sym;
895 // }
896 // }
897 //
898 // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] #1 (c & 0xFF) [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,(c & 0xFF));
899 // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] #1 (c & 0xFF) [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,(c & 0xFF));
900 // }
901 // //if(c == 0) {
902 // if(c <= SDLK_UNKNOWN.sym || c >= SDLK_LAST.sym) {
903 // c = input.keysym.sym;
904 // }
905
906 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %u] c = [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
907
908 //c = (c & 0xFF);
909 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] returning key [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
910 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] returning key [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
911
912 // SDL does NOT handle lowercase
913 if(c >= 'A' && c <= 'Z') {
914 c = tolower(c);
915 }
916 // SDL does NOT handle lowercase
917 if(compareKey >= 'A' && compareKey <= 'Z') {
918 compareKey = (SDL_Keycode)tolower((char)compareKey);
919 }
920
921 bool result = (c == compareKey);
922 //printf("result = %d input = %d compare = %d\n",result,c,compareKey);
923
924 if(result == false) {
925 if(compareKey == SDLK_RETURN) {
926 result = (c == SDLK_KP_ENTER);
927 }
928 else if(compareKey == SDLK_KP_ENTER) {
929 result = (c == SDLK_RETURN);
930 }
931 else if(compareKey == SDLK_BACKSPACE) {
932 result = (c == SDLK_DELETE);
933 }
934 }
935 // else if(compareKey == SDLK_ASTERISK) {
936 // result = (c == SDLK_KP_MULTIPLY);
937 // }
938 // else if(compareKey == SDLK_KP_MULTIPLY) {
939 // result = (c == SDLK_ASTERISK);
940 // }
941 // else if (compareKey == SDLK_0) {
942 // result = (c == SDLK_KP_0);
943 // } else if (compareKey == SDLK_1) {
944 // result = (c == SDLK_KP_1);
945 // } else if (compareKey == SDLK_2) {
946 // result = (c == SDLK_KP_2);
947 // } else if (compareKey == SDLK_3) {
948 // result = (c == SDLK_KP_3);
949 // } else if (compareKey == SDLK_4) {
950 // result = (c == SDLK_KP_4);
951 // } else if (compareKey == SDLK_5) {
952 // result = (c == SDLK_KP_5);
953 // } else if (compareKey == SDLK_6) {
954 // result = (c == SDLK_KP_6);
955 // } else if (compareKey == SDLK_7) {
956 // result = (c == SDLK_KP_7);
957 // } else if (compareKey == SDLK_8) {
958 // result = (c == SDLK_KP_8);
959 // } else if (compareKey == SDLK_9) {
960 // result = (c == SDLK_KP_9);
961 // } else if (compareKey == SDLK_KP_0) {
962 // result = (c == SDLK_0);
963 // } else if (compareKey == SDLK_KP_1) {
964 // result = (c == SDLK_1);
965 // } else if (compareKey == SDLK_KP_2) {
966 // result = (c == SDLK_2);
967 // } else if (compareKey == SDLK_KP_3) {
968 // result = (c == SDLK_3);
969 // } else if (compareKey == SDLK_KP_4) {
970 // result = (c == SDLK_4);
971 // } else if (compareKey == SDLK_KP_5) {
972 // result = (c == SDLK_5);
973 // } else if (compareKey == SDLK_KP_6) {
974 // result = (c == SDLK_6);
975 // } else if (compareKey == SDLK_KP_7) {
976 // result = (c == SDLK_7);
977 // } else if (compareKey == SDLK_KP_8) {
978 // result = (c == SDLK_8);
979 // } else if (compareKey == SDLK_KP_9) {
980 // result = (c == SDLK_9);
981 // }
982 // }
983
984 if(result == true) {
985 //printf("input.keysym.mod = %d\n",input.keysym.mod);
986
987 for(unsigned int i = 0; i < modifiersToCheck.size(); ++i) {
988 if( (input.keysym.mod & modifiersToCheck[i])) {
989 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] result *WOULD HAVE BEEN TRUE* but is false due to: input.keysym.mod = %d modifiersToCheck[i] = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,input.keysym.mod,modifiersToCheck[i]);
990 result = false;
991 break;
992 }
993 }
994 }
995 string compareKeyName = SDL_GetKeyName(compareKey);
996 string pressKeyName = SDL_GetKeyName((SDL_Keycode)c);
997
998 //printf ("In [%s::%s Line: %d] compareKey [%d - %s] pressed key [%d - %s] result = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,compareKey,compareKeyName.c_str(),c,pressKeyName.c_str(),result);
999
1000 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] compareKey [%d - %s] pressed key [%d - %s] result = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,compareKey,compareKeyName.c_str(),c,pressKeyName.c_str(),result);
1001 //printf ("ISPRESS compareKey [%d - %s] pressed key [%d - %s] input.keysym.sym [%d] input.keysym.unicode [%d] mod = %d result = %d\n",
1002 // compareKey,compareKeyName.c_str(),c,pressKeyName.c_str(),input.keysym.sym,input.keysym.unicode,input.keysym.mod,result);
1003
1004 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] compareKey [%d - %s] pressed key [%d - %s] result = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,compareKey,compareKeyName.c_str(),c,pressKeyName.c_str(),result);
1005
1006 return result;
1007 }
1008
extractKeyPressedUnicode(SDL_KeyboardEvent input)1009 wchar_t extractKeyPressedUnicode(SDL_KeyboardEvent input) {
1010 wchar_t c = SDLK_UNKNOWN;
1011 //if(input.keysym.unicode > 0 && input.keysym.unicode < 0x80) {
1012 if(input.keysym.sym > 0) {
1013 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] input.keysym.sym = %d input.keysym.mod = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,input.keysym.sym,input.keysym.mod);
1014
1015 c = input.keysym.sym;
1016 // if(c <= SDLK_UNKNOWN || c >= SDLK_LAST) {
1017 // c = SDLKey(c & 0xFF);
1018 // }
1019
1020 //c = toupper(c);
1021
1022 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] #1 (c & 0xFF) [%d] c = [%lc]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,(c & 0xFF),c);
1023 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] #1 (c & 0xFF) [%d] c = [%lc]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,(c & 0xFF),c);
1024 }
1025 if(c == SDLK_UNKNOWN) {
1026 c = input.keysym.sym;
1027 }
1028
1029 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %u] c = [%d][%lc]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
1030
1031 //c = (SDLKey)(c & 0xFF);
1032 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] returning key [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
1033 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] returning key [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
1034
1035 string pressKeyName = SDL_GetKeyName((SDL_Keycode)c);
1036 //string inputKeyName = SDL_GetKeyName(input.keysym.sym);
1037
1038 //printf ("PRESS pressed key [%d - %s] input.keysym.sym [%d] input.keysym.unicode [%d] mod = %d\n",
1039 // c,pressKeyName.c_str(),input.keysym.sym,input.keysym.unicode,input.keysym.mod);
1040
1041 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] pressed key [%d - %s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c,pressKeyName.c_str());
1042 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] pressed key [%d - %s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c,pressKeyName.c_str());
1043
1044 return c;
1045 }
1046
extractKeyPressedUnicodeLength(string text)1047 vector<int> extractKeyPressedUnicodeLength(string text) {
1048 vector<int> result;
1049 unsigned int i = 0;
1050 for(i = 0; i < text.length();) {
1051 char c = text[i];
1052 wchar_t keyW = c;
1053 wchar_t textAppend[] = { keyW, 0 };
1054
1055 if(*textAppend) {
1056 wchar_t newKey = textAppend[0];
1057 if (newKey < 0x80) {
1058 result.push_back(1);
1059 //printf("1 char, textCharLength = %d\n",textCharLength.size());
1060 }
1061 else if (newKey < 0x800) {
1062 result.push_back(2);
1063 //printf("2 char, textCharLength = %d\n",textCharLength.size());
1064 }
1065 else {
1066 result.push_back(3);
1067 //printf("3 char, textCharLength = %d\n",textCharLength.size());
1068 }
1069 i += result[result.size()-1];
1070 }
1071 }
1072 return result;
1073 }
1074
extractKeyPressed(SDL_KeyboardEvent input)1075 SDL_Keycode extractKeyPressed(SDL_KeyboardEvent input) {
1076 SDL_Keycode c = SDLK_UNKNOWN;
1077 //if(input.keysym.unicode > 0 && input.keysym.unicode < 0x80) {
1078 //if(input.keysym.sym > 0) {
1079 // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] input.keysym.sym = %d input.keysym.mod = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,input.keysym.sym,input.keysym.mod);
1080
1081 // c = input.keysym.sym;
1082 // if(c <= SDLK_UNKNOWN || c >= SDLK_LAST) {
1083 // c = SDLKey(c & 0xFF);
1084 // }
1085
1086 //c = toupper(c);
1087
1088 //if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] #1 (c & 0xFF) [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,(c & 0xFF));
1089 //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] #1 (c & 0xFF) [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,(c & 0xFF));
1090 //}
1091 //if(c <= SDLK_UNKNOWN) {
1092 c = input.keysym.sym;
1093 //}
1094
1095 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %u] c = [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
1096
1097 //c = (SDLKey)(c & 0xFF);
1098 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] returning key [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
1099 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] returning key [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c);
1100
1101 string pressKeyName = SDL_GetKeyName(c);
1102 //string inputKeyName = SDL_GetKeyName(input.keysym.sym);
1103
1104 // printf ("PRESS pressed key [%d - %s] input.keysym.sym [%d] mod = %d\n",
1105 // c,pressKeyName.c_str(),input.keysym.sym,input.keysym.mod);
1106
1107 if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] pressed key [%d - %s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c,pressKeyName.c_str());
1108 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] pressed key [%d - %s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,c,pressKeyName.c_str());
1109
1110 return c;
1111 }
1112
isAllowedInputTextKey(wchar_t & key)1113 bool isAllowedInputTextKey(wchar_t &key) {
1114 if(Window::isAllowedKey(key) == true) {
1115 return true;
1116 }
1117
1118 bool result = (
1119 key != SDLK_DELETE &&
1120 key != SDLK_BACKSPACE &&
1121 key != SDLK_TAB &&
1122 key != SDLK_CLEAR &&
1123 key != SDLK_RETURN &&
1124 key != SDLK_PAUSE &&
1125 key != SDLK_UP &&
1126 key != SDLK_DOWN &&
1127 key != SDLK_RIGHT &&
1128 key != SDLK_LEFT &&
1129 key != SDLK_INSERT &&
1130 key != SDLK_HOME &&
1131 key != SDLK_END &&
1132 key != SDLK_PAGEUP &&
1133 key != SDLK_PAGEDOWN &&
1134 key != SDLK_F1 &&
1135 key != SDLK_F2 &&
1136 key != SDLK_F3 &&
1137 key != SDLK_F4 &&
1138 key != SDLK_F5 &&
1139 key != SDLK_F6 &&
1140 key != SDLK_F7 &&
1141 key != SDLK_F8 &&
1142 key != SDLK_F9 &&
1143 key != SDLK_F10 &&
1144 key != SDLK_F11 &&
1145 key != SDLK_F12 &&
1146 key != SDLK_F13 &&
1147 key != SDLK_F14 &&
1148 key != SDLK_F15 &&
1149 key != SDLK_NUMLOCKCLEAR &&
1150 key != SDLK_CAPSLOCK &&
1151 key != SDLK_SCROLLLOCK &&
1152 key != SDLK_RSHIFT &&
1153 key != SDLK_LSHIFT &&
1154 key != SDLK_RCTRL &&
1155 key != SDLK_LCTRL &&
1156 key != SDLK_RALT &&
1157 key != SDLK_LALT &&
1158 key != SDLK_RGUI &&
1159 key != SDLK_LGUI &&
1160 key != SDLK_MODE &&
1161 key != SDLK_HELP &&
1162 key != SDLK_PRINTSCREEN &&
1163 key != SDLK_SYSREQ &&
1164 key != SDLK_PAUSE &&
1165 key != SDLK_MENU &&
1166 key != SDLK_POWER);
1167
1168 string inputKeyName = SDL_GetKeyName((SDL_Keycode)key);
1169 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] pressed key [%d - %s] result = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key,inputKeyName.c_str(),result);
1170 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] pressed key [%d - %s] result = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key,inputKeyName.c_str(),result);
1171
1172 return result;
1173 }
1174
isAllowedInputTextKey(SDL_Keycode key)1175 bool isAllowedInputTextKey(SDL_Keycode key) {
1176 if(Window::isAllowedKey(key) == true) {
1177 return true;
1178 }
1179
1180 bool result = (
1181 key != SDLK_DELETE &&
1182 key != SDLK_BACKSPACE &&
1183 key != SDLK_TAB &&
1184 key != SDLK_CLEAR &&
1185 key != SDLK_RETURN &&
1186 key != SDLK_PAUSE &&
1187 key != SDLK_UP &&
1188 key != SDLK_DOWN &&
1189 key != SDLK_RIGHT &&
1190 key != SDLK_LEFT &&
1191 key != SDLK_INSERT &&
1192 key != SDLK_HOME &&
1193 key != SDLK_END &&
1194 key != SDLK_PAGEUP &&
1195 key != SDLK_PAGEDOWN &&
1196 key != SDLK_F1 &&
1197 key != SDLK_F2 &&
1198 key != SDLK_F3 &&
1199 key != SDLK_F4 &&
1200 key != SDLK_F5 &&
1201 key != SDLK_F6 &&
1202 key != SDLK_F7 &&
1203 key != SDLK_F8 &&
1204 key != SDLK_F9 &&
1205 key != SDLK_F10 &&
1206 key != SDLK_F11 &&
1207 key != SDLK_F12 &&
1208 key != SDLK_F13 &&
1209 key != SDLK_F14 &&
1210 key != SDLK_F15 &&
1211 key != SDLK_NUMLOCKCLEAR &&
1212 key != SDLK_CAPSLOCK &&
1213 key != SDLK_SCROLLLOCK &&
1214 key != SDLK_RSHIFT &&
1215 key != SDLK_LSHIFT &&
1216 key != SDLK_RCTRL &&
1217 key != SDLK_LCTRL &&
1218 key != SDLK_RALT &&
1219 key != SDLK_LALT &&
1220 key != SDLK_RGUI &&
1221 key != SDLK_LGUI &&
1222 key != SDLK_MODE &&
1223 key != SDLK_HELP &&
1224 key != SDLK_PRINTSCREEN &&
1225 key != SDLK_SYSREQ &&
1226 key != SDLK_PAUSE &&
1227 key != SDLK_MENU &&
1228 key != SDLK_POWER);
1229
1230 string inputKeyName = SDL_GetKeyName(key);
1231 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] pressed key [%d - %s] result = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key,inputKeyName.c_str(),result);
1232 if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] pressed key [%d - %s] result = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key,inputKeyName.c_str(),result);
1233
1234 return result;
1235 }
1236
isKeyStateModPressed(int mod)1237 bool Window::isKeyStateModPressed(int mod) {
1238 if(SystemFlags::VERBOSE_MODE_ENABLED) printf("isKeyStateModPressed mod = %d, keystate.mod = %d, keystate.mod & mod = %d\n",mod,keystate.mod,(keystate.mod & mod));
1239
1240 if(keystate.mod & mod) {
1241 return true;
1242 }
1243 return false;
1244 }
1245
extractLastKeyPressed()1246 wchar_t Window::extractLastKeyPressed() {
1247 return keystate.sym;
1248 }
1249
1250 }}//end namespace
1251