OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4AppSDL.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 /* A wrapper class to OS dependent event and window interfaces, SDL version */
18 
19 #include "C4Include.h"
20 #include "platform/C4App.h"
21 
22 #include "C4Version.h"
23 #include "game/C4Application.h"
24 #include "graphics/C4DrawGL.h"
25 #include "gui/C4Gui.h"
26 #include "gui/C4MouseControl.h"
27 #include "platform/C4GamePadCon.h"
28 #include "platform/C4Window.h"
29 
30 static void sdlToC4MCBtn(const SDL_MouseButtonEvent &e, int32_t& button, DWORD& flags)
31 {
32  button = C4MC_Button_None;
33  flags = SDL_GetModState();
34 
35  switch (e.button)
36  {
37  case SDL_BUTTON_LEFT:
38  if (e.state == SDL_PRESSED)
39  button = e.clicks == 2 ? C4MC_Button_LeftDouble : C4MC_Button_LeftDown;
40  else
41  button = C4MC_Button_LeftUp;
42  break;
43  case SDL_BUTTON_RIGHT:
44  if (e.state == SDL_PRESSED)
45  button = e.clicks == 2 ? C4MC_Button_RightDouble : C4MC_Button_RightDown;
46  else
47  button = C4MC_Button_RightUp;
48  break;
49  case SDL_BUTTON_MIDDLE:
50  if (e.state == SDL_PRESSED)
51  button = e.clicks == 2 ? C4MC_Button_MiddleDouble : C4MC_Button_MiddleDown;
52  else
53  button = C4MC_Button_MiddleUp;
54  break;
55  case SDL_BUTTON_X1:
56  if (e.state == SDL_PRESSED)
57  button = e.clicks == 2 ? C4MC_Button_X1Double : C4MC_Button_X1Down;
58  else
59  button = C4MC_Button_X1Up;
60  break;
61  case SDL_BUTTON_X2:
62  if (e.state == SDL_PRESSED)
63  button = e.clicks == 2 ? C4MC_Button_X2Double : C4MC_Button_X2Down;
64  else
65  button = C4MC_Button_X2Up;
66  break;
67  }
68 }
69 
70 /* C4AbstractApp */
71 
73  Active(false), pWindow(nullptr), fQuitMsgReceived(false),
74  // main thread
75 #ifdef HAVE_PTHREAD
76  MainThread (pthread_self()),
77 #endif
78 #ifdef _WIN32
79  idMainThread(0),
80 #endif
81  fDspModeSet(false)
82 {
83 }
84 
86 
87 bool C4AbstractApp::Init(int argc, char * argv[])
88 {
89  // Set locale
90  setlocale(LC_ALL,"");
91 
92  if (SDL_Init(SDL_INIT_VIDEO) != 0)
93  {
94  LogF("SDL_Init: %s", SDL_GetError());
95  return false;
96  }
97 
98  //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
99 
100  // Custom initialization
101  return DoInit (argc, argv);
102 }
103 
104 
106 {
107  SDL_Quit();
108 }
109 
111 {
112  fQuitMsgReceived = true;
113 }
114 
116 {
117  // Always fail after quit message
118  if (fQuitMsgReceived)
119  return false;
120 
121  // Handle pending SDL messages
122  SDL_Event event;
123  while (SDL_PollEvent(&event))
124  {
125  HandleSDLEvent(event);
126  }
127  return true;
128 }
129 
130 #define SDL_SCANCODE_KEYCODE \
131  X(SDL_SCANCODE_LSHIFT, K_SHIFT_L) \
132  X(SDL_SCANCODE_RSHIFT, K_SHIFT_R) \
133  X(SDL_SCANCODE_LCTRL, K_CONTROL_L) \
134  X(SDL_SCANCODE_RCTRL, K_CONTROL_R) \
135  X(SDL_SCANCODE_LALT, K_ALT_L) \
136  X(SDL_SCANCODE_RALT, K_ALT_R) \
137  X(SDL_SCANCODE_F1, K_F1) \
138  X(SDL_SCANCODE_F2, K_F2) \
139  X(SDL_SCANCODE_F3, K_F3) \
140  X(SDL_SCANCODE_F4, K_F4) \
141  X(SDL_SCANCODE_F5, K_F5) \
142  X(SDL_SCANCODE_F6, K_F6) \
143  X(SDL_SCANCODE_F7, K_F7) \
144  X(SDL_SCANCODE_F8, K_F8) \
145  X(SDL_SCANCODE_F9, K_F9) \
146  X(SDL_SCANCODE_F10, K_F10) \
147  X(SDL_SCANCODE_F11, K_F11) \
148  X(SDL_SCANCODE_F12, K_F12) \
149  X(SDL_SCANCODE_KP_PLUS, K_ADD) \
150  X(SDL_SCANCODE_KP_MINUS, K_SUBTRACT) \
151  X(SDL_SCANCODE_KP_MULTIPLY, K_MULTIPLY) \
152  X(SDL_SCANCODE_ESCAPE, K_ESCAPE) \
153  X(SDL_SCANCODE_PAUSE, K_PAUSE) \
154  X(SDL_SCANCODE_TAB, K_TAB) \
155  X(SDL_SCANCODE_RETURN, K_RETURN) \
156  X(SDL_SCANCODE_DELETE, K_DELETE) \
157  X(SDL_SCANCODE_INSERT, K_INSERT) \
158  X(SDL_SCANCODE_BACKSPACE, K_BACK) \
159  X(SDL_SCANCODE_SPACE, K_SPACE) \
160  X(SDL_SCANCODE_UP, K_UP) \
161  X(SDL_SCANCODE_DOWN, K_DOWN) \
162  X(SDL_SCANCODE_LEFT, K_LEFT) \
163  X(SDL_SCANCODE_RIGHT, K_RIGHT) \
164  X(SDL_SCANCODE_HOME, K_HOME) \
165  X(SDL_SCANCODE_END, K_END) \
166  X(SDL_SCANCODE_SCROLLLOCK, K_SCROLL) \
167  X(SDL_SCANCODE_MENU, K_MENU) \
168  X(SDL_SCANCODE_PAGEUP, K_PAGEUP) \
169  X(SDL_SCANCODE_PAGEDOWN, K_PAGEDOWN) \
170  X(SDL_SCANCODE_1, K_1) \
171  X(SDL_SCANCODE_2, K_2) \
172  X(SDL_SCANCODE_3, K_3) \
173  X(SDL_SCANCODE_4, K_4) \
174  X(SDL_SCANCODE_5, K_5) \
175  X(SDL_SCANCODE_6, K_6) \
176  X(SDL_SCANCODE_7, K_7) \
177  X(SDL_SCANCODE_8, K_8) \
178  X(SDL_SCANCODE_9, K_9) \
179  X(SDL_SCANCODE_0, K_0) \
180  X(SDL_SCANCODE_A, K_A) \
181  X(SDL_SCANCODE_B, K_B) \
182  X(SDL_SCANCODE_C, K_C) \
183  X(SDL_SCANCODE_D, K_D) \
184  X(SDL_SCANCODE_E, K_E) \
185  X(SDL_SCANCODE_F, K_F) \
186  X(SDL_SCANCODE_G, K_G) \
187  X(SDL_SCANCODE_H, K_H) \
188  X(SDL_SCANCODE_I, K_I) \
189  X(SDL_SCANCODE_J, K_J) \
190  X(SDL_SCANCODE_K, K_K) \
191  X(SDL_SCANCODE_L, K_L) \
192  X(SDL_SCANCODE_M, K_M) \
193  X(SDL_SCANCODE_N, K_N) \
194  X(SDL_SCANCODE_O, K_O) \
195  X(SDL_SCANCODE_P, K_P) \
196  X(SDL_SCANCODE_Q, K_Q) \
197  X(SDL_SCANCODE_R, K_R) \
198  X(SDL_SCANCODE_S, K_S) \
199  X(SDL_SCANCODE_T, K_T) \
200  X(SDL_SCANCODE_U, K_U) \
201  X(SDL_SCANCODE_V, K_V) \
202  X(SDL_SCANCODE_W, K_W) \
203  X(SDL_SCANCODE_X, K_X) \
204  X(SDL_SCANCODE_Y, K_Y) \
205  X(SDL_SCANCODE_Z, K_Z) \
206  X(SDL_SCANCODE_MINUS, K_MINUS) \
207  X(SDL_SCANCODE_EQUALS, K_EQUAL) \
208  X(SDL_SCANCODE_LEFTBRACKET, K_LEFT_BRACKET) \
209  X(SDL_SCANCODE_RIGHTBRACKET, K_RIGHT_BRACKET) \
210  X(SDL_SCANCODE_SEMICOLON, K_SEMICOLON) \
211  X(SDL_SCANCODE_APOSTROPHE, K_APOSTROPHE) \
212  X(SDL_SCANCODE_GRAVE, K_GRAVE_ACCENT) \
213  X(SDL_SCANCODE_BACKSLASH, K_BACKSLASH) \
214  X(SDL_SCANCODE_COMMA, K_COMMA) \
215  X(SDL_SCANCODE_PERIOD, K_PERIOD) \
216  X(SDL_SCANCODE_SLASH, K_SLASH) \
217  X(SDL_SCANCODE_CAPSLOCK, K_CAPS) \
218  X(SDL_SCANCODE_NUMLOCKCLEAR, K_NUM) \
219  X(SDL_SCANCODE_KP_7, K_NUM7) \
220  X(SDL_SCANCODE_KP_8, K_NUM8) \
221  X(SDL_SCANCODE_KP_9, K_NUM9) \
222  X(SDL_SCANCODE_KP_4, K_NUM4) \
223  X(SDL_SCANCODE_KP_5, K_NUM5) \
224  X(SDL_SCANCODE_KP_6, K_NUM6) \
225  X(SDL_SCANCODE_KP_1, K_NUM1) \
226  X(SDL_SCANCODE_KP_2, K_NUM2) \
227  X(SDL_SCANCODE_KP_3, K_NUM3) \
228  X(SDL_SCANCODE_KP_0, K_NUM0) \
229  X(SDL_SCANCODE_KP_PERIOD, K_DECIMAL) \
230  X(SDL_SCANCODE_NONUSBACKSLASH, K_86) \
231  X(SDL_SCANCODE_KP_ENTER, K_NUM_RETURN) \
232  X(SDL_SCANCODE_KP_DIVIDE, K_DIVIDE) \
233  X(SDL_SCANCODE_LGUI, K_WIN_L) \
234  X(SDL_SCANCODE_RGUI, K_WIN_R) \
235  X(SDL_SCANCODE_PRINTSCREEN, K_PRINT) \
236 
237 static C4KeyCode sdl_scancode_to_keycode(SDL_Scancode scancode)
238 {
239  switch (scancode)
240  {
241 #define X(sdl, oc) case sdl: return oc;
243 #undef X
244  default: return 0; // silence warnings.
245  }
246 }
247 
248 const char* KeycodeToString(C4KeyCode code)
249 {
250  SDL_Scancode scancode;
251  switch (code)
252  {
253 #define X(sdl, oc) case oc: scancode = sdl; break;
255 #undef X
256  default:
257  return nullptr;
258  }
259  return SDL_GetScancodeName(scancode);
260 }
261 
262 void C4AbstractApp::HandleSDLEvent(SDL_Event& e)
263 {
264  DWORD flags;
265  // Directly handle QUIT messages.
266  switch (e.type)
267  {
268  case SDL_QUIT:
269  Quit();
270  break;
271  case SDL_TEXTINPUT:
272  ::pGUI->CharIn(e.text.text);
273  break;
274  case SDL_KEYDOWN:
275  {
276  Game.DoKeyboardInput(sdl_scancode_to_keycode(e.key.keysym.scancode), KEYEV_Down,
277  e.key.keysym.mod & (KMOD_LALT | KMOD_RALT),
278  e.key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL),
279  e.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT),
280  e.key.repeat > 0, nullptr);
281  break;
282  }
283  case SDL_KEYUP:
284  Game.DoKeyboardInput(sdl_scancode_to_keycode(e.key.keysym.scancode), KEYEV_Up,
285  e.key.keysym.mod & (KMOD_LALT | KMOD_RALT),
286  e.key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL),
287  e.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT), false, nullptr);
288  break;
289  case SDL_MOUSEMOTION:
290  C4GUI::MouseMove(C4MC_Button_None, e.motion.x, e.motion.y, 0, nullptr);
291  break;
292  case SDL_MOUSEBUTTONUP:
293  case SDL_MOUSEBUTTONDOWN:
294  int32_t button;
295  sdlToC4MCBtn(e.button, button, flags);
296  C4GUI::MouseMove(button, e.button.x, e.button.y, flags, nullptr);
297  break;
298  case SDL_MOUSEWHEEL:
299  flags = e.wheel.y > 0 ? (+32) << 16 : (DWORD) (-32) << 16;
300  flags += SDL_GetModState();
301  int x, y;
302  SDL_GetMouseState(&x, &y);
303  C4GUI::MouseMove(C4MC_Button_Wheel, x, y, flags, nullptr);
304  break;
305  case SDL_CONTROLLERAXISMOTION:
306  case SDL_CONTROLLERBUTTONDOWN:
307  case SDL_CONTROLLERBUTTONUP:
308  Application.pGamePadControl->FeedEvent(e, C4GamePadControl::FEED_BUTTONS);
309  break;
310  case SDL_JOYDEVICEADDED:
311  case SDL_CONTROLLERDEVICEADDED:
312  case SDL_CONTROLLERDEVICEREMOVED:
313  Application.pGamePadControl->CheckGamePad(e);
314  break;
315  case SDL_WINDOWEVENT:
316  // Forward to C4Window instance.
317  auto window = static_cast<C4Window*>(SDL_GetWindowData(SDL_GetWindowFromID(e.window.windowID), "C4Window"));
318  window->HandleSDLEvent(e.window);
319  break;
320  }
321 }
322 
323 static int modeCount = 0;
324 
325 static int bits_per_pixel(int format)
326 {
327  // C4Draw::BITS_PER_PIXEL is 32, and other parts of the code expect
328  // the mode's bpp to match exactly. 24 is fully compatible, so just
329  // pretend it's 32 in that case to adhere to the expected interface.
330  int bbp = SDL_BITSPERPIXEL(format);
331  if (bbp == 24) bbp = 32;
332  return bbp;
333 }
334 
335 bool C4AbstractApp::GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, int32_t *piRefreshRate, uint32_t iMonitor)
336 {
337  if (!modeCount)
338  {
339  modeCount = SDL_GetNumDisplayModes(iMonitor);
340  }
341 
342  if (iIndex >= modeCount)
343  return false;
344 
345  SDL_DisplayMode mode;
346  SDL_GetDisplayMode(iMonitor, iIndex, &mode);
347  *piXRes = mode.w;
348  *piYRes = mode.h;
349  *piBitDepth = bits_per_pixel(mode.format);
350  if (piRefreshRate) *piRefreshRate = mode.refresh_rate;
351  return true;
352 }
353 
354 bool C4AbstractApp::SetVideoMode(int iXRes, int iYRes, unsigned int RefreshRate, unsigned int iMonitor, bool fFullScreen)
355 {
356  int res;
357  if (!fFullScreen)
358  {
359  if (iXRes == -1)
360  {
361  SDL_DisplayMode desktop_mode;
362  res = SDL_GetDesktopDisplayMode(iMonitor, &desktop_mode);
363  if (res)
364  {
365  Error(SDL_GetError());
366  LogF("SDL_GetDesktopDisplayMode: %s", SDL_GetError());
367  return false;
368  }
369 
370  iXRes = desktop_mode.w;
371  iYRes = desktop_mode.h;
372  }
373 
374  res = SDL_SetWindowFullscreen(pWindow->window, 0);
375  if (res)
376  {
377  Error(SDL_GetError());
378  LogF("SDL_SetWindowFullscreen: %s", SDL_GetError());
379  return false;
380  }
381 
382  pWindow->SetSize(iXRes, iYRes);
383  OnResolutionChanged(iXRes, iYRes);
384  return true;
385  }
386  SDL_DisplayMode mode;
387  if (iXRes < 0 || iYRes < 0)
388  {
389  res = SDL_SetWindowFullscreen(pWindow->window, SDL_WINDOW_FULLSCREEN_DESKTOP);
390  if (res)
391  {
392  Error(SDL_GetError());
393  LogF("SDL_SetWindowFullscreen: %s", SDL_GetError());
394  return false;
395  }
396  res = SDL_GetDesktopDisplayMode(iMonitor, &mode);
397  if (res)
398  {
399  Error(SDL_GetError());
400  LogF("SDL_GetDesktopDisplayMode: %s", SDL_GetError());
401  return false;
402  }
403  OnResolutionChanged(mode.w, mode.h);
404  return true;
405  }
406 
407  for (int i = 0; i < modeCount; ++i)
408  {
409  res = SDL_GetDisplayMode(iMonitor, i, &mode);
410 
411  if (res)
412  {
413  Error(SDL_GetError());
414  LogF("SDL_GetDisplayMode: %s", SDL_GetError());
415  return false;
416  }
417 
418  if (mode.w == iXRes && mode.h == iYRes && (RefreshRate == 0 || mode.refresh_rate == RefreshRate) && bits_per_pixel(mode.format) == C4Draw::COLOR_DEPTH)
419  {
420  res = SDL_SetWindowDisplayMode(pWindow->window, &mode);
421  if (res)
422  {
423  Error(SDL_GetError());
424  LogF("SDL_SetWindowDisplayMode: %s", SDL_GetError());
425  return false;
426  }
427  res = SDL_SetWindowFullscreen(pWindow->window, SDL_WINDOW_FULLSCREEN);
428  if (res)
429  {
430  Error(SDL_GetError());
431  LogF("SDL_SetWindowFullscreen: %s", SDL_GetError());
432  return false;
433  }
434  OnResolutionChanged(mode.w, mode.h);
435  return true;
436  }
437  }
438 
439  Error("No such resolution available");
440  return false;
441 }
442 
444 {
445  if (pWindow && pWindow->window)
446  SDL_SetWindowFullscreen(pWindow->window, 0);
447 }
448 
449 bool C4AbstractApp::Copy(const std::string &text, bool fClipboard)
450 {
451  return SDL_SetClipboardText(text.c_str()) == 0;
452 }
453 
454 std::string C4AbstractApp::Paste(bool fClipboard)
455 {
456  char * text = SDL_GetClipboardText();
457  std::string buf(text);
458  SDL_free(text);
459  return buf;
460 }
461 
462 bool C4AbstractApp::IsClipboardFull(bool fClipboard)
463 {
464  return SDL_HasClipboardText();
465 }
466 
467 void C4AbstractApp::MessageDialog(const char * message)
468 {
469  SDL_ShowSimpleMessageBox(0, C4ENGINECAPTION, message, pWindow ? pWindow->window : nullptr);
470 }
const int32_t C4MC_Button_MiddleUp
virtual void OnResolutionChanged(unsigned int iXRes, unsigned int iYRes)=0
const int32_t C4MC_Button_LeftDown
C4Game Game
Definition: C4Globals.cpp:52
void MessageDialog(const char *message)
Definition: C4AppMac.mm:62
static constexpr int COLOR_DEPTH
Definition: C4Draw.h:89
~C4AbstractApp() override
Definition: C4AppT.cpp:37
void MouseMove(int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam, class C4Viewport *pVP)
Definition: C4Gui.h:2829
bool SetVideoMode(int iXRes, int iYRes, unsigned int iRefreshRate, unsigned int iMonitor, bool fFullScreen)
Definition: C4AppSDL.cpp:354
const int32_t C4MC_Button_RightUp
const int32_t C4MC_Button_X2Down
const int32_t C4MC_Button_MiddleDouble
const char * KeycodeToString(C4KeyCode code)
Definition: C4AppSDL.cpp:248
const int32_t C4MC_Button_X1Double
bool DoKeyboardInput(C4KeyCode vk_code, C4KeyEventType eEventType, bool fAlt, bool fCtrl, bool fShift, bool fRepeated, class C4GUI::Dialog *pForDialog=nullptr, bool fPlrCtrlOnly=false, int32_t iStrength=-1)
Definition: C4Game.cpp:1865
const int32_t C4MC_Button_LeftDouble
bool FlushMessages()
Definition: C4AppSDL.cpp:115
void SetSize(unsigned int cx, unsigned int cy)
Definition: C4AppT.cpp:110
const int32_t C4MC_Button_X2Double
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
const int32_t C4MC_Button_LeftUp
const int32_t C4MC_Button_X1Up
virtual bool DoInit(int argc, char *argv[])=0
void Error(const char *m)
Definition: C4App.h:99
const int32_t C4MC_Button_X1Down
const int32_t C4MC_Button_RightDown
bool fQuitMsgReceived
Definition: C4App.h:81
virtual void Quit()
Definition: C4AppSDL.cpp:110
virtual bool CharIn(const char *c)
Definition: C4Gui.cpp:779
const int32_t C4MC_Button_Wheel
bool GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, int32_t *piRefreshRate, uint32_t iMonitor)
Definition: C4AppSDL.cpp:335
bool Copy(const std::string &text, bool fClipboard=true)
Definition: C4AppMac.mm:33
void RestoreVideoMode()
Definition: C4AppSDL.cpp:443
bool Init(int argc, char *argv[])
Definition: C4AppSDL.cpp:87
virtual void Clear()
Definition: C4AppSDL.cpp:105
const int32_t C4MC_Button_RightDouble
const int32_t C4MC_Button_None
std::string Paste(bool fClipboard=true)
Definition: C4AppMac.mm:46
#define SDL_SCANCODE_KEYCODE
Definition: C4AppSDL.cpp:130
const int32_t C4MC_Button_MiddleDown
C4GamePadControl * pGamePadControl
Definition: C4Application.h:43
uint32_t DWORD
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:250
C4Window * pWindow
Definition: C4App.h:80
unsigned long C4KeyCode
C4Application Application
Definition: C4Globals.cpp:44
bool IsClipboardFull(bool fClipboard=true)
Definition: C4AppMac.mm:57
const int32_t C4MC_Button_X2Up