SDL  2.0
SDL_BApp.h
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #ifndef SDL_BAPP_H
22 #define SDL_BAPP_H
23 
24 #include <InterfaceKit.h>
25 #if SDL_VIDEO_OPENGL
26 #include <OpenGLKit.h>
27 #endif
28 
29 #include "../../video/haiku/SDL_bkeyboard.h"
30 
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include "../../SDL_internal.h"
37 
38 #include "SDL_video.h"
39 
40 /* Local includes */
41 #include "../../events/SDL_events_c.h"
42 #include "../../video/haiku/SDL_bframebuffer.h"
43 
44 #ifdef __cplusplus
45 }
46 #endif
47 
48 #include <vector>
49 
50 
51 
52 
53 /* Forward declarations */
54 class SDL_BWin;
55 
56 /* Message constants */
57 enum ToSDL {
58  /* Intercepted by BWindow on its way to BView */
63  BAPP_REPAINT, /* from _UPDATE_ */
64  /* From BWindow */
65  BAPP_MAXIMIZE, /* from B_ZOOM */
67  BAPP_RESTORE, /* TODO: IMPLEMENT! */
70  BAPP_MOUSE_FOCUS, /* caused by MOUSE_MOVE */
71  BAPP_KEYBOARD_FOCUS, /* from WINDOW_ACTIVATED */
76 };
77 
78 
79 
80 /* Create a descendant of BApplication */
81 class SDL_BApp : public BApplication {
82 public:
83  SDL_BApp(const char* signature) :
84  BApplication(signature) {
85 #if SDL_VIDEO_OPENGL
87 #endif
88  }
89 
90 
91  virtual ~SDL_BApp() {
92  }
93 
94 
95 
96  /* Event-handling functions */
97  virtual void MessageReceived(BMessage* message) {
98  /* Sort out SDL-related messages */
99  switch ( message->what ) {
100  case BAPP_MOUSE_MOVED:
102  break;
103 
104  case BAPP_MOUSE_BUTTON:
106  break;
107 
108  case BAPP_MOUSE_WHEEL:
110  break;
111 
112  case BAPP_KEY:
114  break;
115 
116  case BAPP_REPAINT:
118  break;
119 
120  case BAPP_MAXIMIZE:
122  break;
123 
124  case BAPP_MINIMIZE:
126  break;
127 
128  case BAPP_SHOW:
130  break;
131 
132  case BAPP_HIDE:
134  break;
135 
136  case BAPP_MOUSE_FOCUS:
138  break;
139 
140  case BAPP_KEYBOARD_FOCUS:
142  break;
143 
146  break;
147 
148  case BAPP_WINDOW_MOVED:
150  break;
151 
152  case BAPP_WINDOW_RESIZED:
154  break;
155 
156  case BAPP_SCREEN_CHANGED:
157  /* TODO: Handle screen resize or workspace change */
158  break;
159 
160  default:
161  BApplication::MessageReceived(message);
162  break;
163  }
164  }
165 
166  /* Window creation/destruction methods */
167  int32 GetID(SDL_Window *win) {
168  int32 i;
169  for(i = 0; i < _GetNumWindowSlots(); ++i) {
170  if( GetSDLWindow(i) == NULL ) {
171  _SetSDLWindow(win, i);
172  return i;
173  }
174  }
175 
176  /* Expand the vector if all slots are full */
177  if( i == _GetNumWindowSlots() ) {
178  _PushBackWindow(win);
179  return i;
180  }
181 
182  /* TODO: error handling */
183  return 0;
184  }
185 
186  /* FIXME: Bad coding practice, but I can't include SDL_BWin.h here. Is
187  there another way to do this? */
188  void ClearID(SDL_BWin *bwin); /* Defined in SDL_BeApp.cc */
189 
190 
191  SDL_Window *GetSDLWindow(int32 winID) {
192  return _window_map[winID];
193  }
194 
195 #if SDL_VIDEO_OPENGL
196  void SetCurrentContext(BGLView *newContext) {
197  if(_current_context)
198  _current_context->UnlockGL();
199  _current_context = newContext;
200  if (_current_context)
201  _current_context->LockGL();
202  }
203 #endif
204 
205 private:
206  /* Event management */
207  void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType) {
208  SDL_Window *win;
209  int32 winID;
210  if(
211  !_GetWinID(msg, &winID)
212  ) {
213  return;
214  }
215  win = GetSDLWindow(winID);
216  SDL_SendWindowEvent(win, sdlEventType, 0, 0);
217  }
218 
219  void _HandleMouseMove(BMessage *msg) {
220  SDL_Window *win;
221  int32 winID;
222  int32 x = 0, y = 0;
223  if(
224  !_GetWinID(msg, &winID) ||
225  msg->FindInt32("x", &x) != B_OK || /* x movement */
226  msg->FindInt32("y", &y) != B_OK /* y movement */
227  ) {
228  return;
229  }
230  win = GetSDLWindow(winID);
231 
232  // Simple relative mode support for mouse.
233  if (SDL_GetMouse()->relative_mode) {
234  int winWidth, winHeight, winPosX, winPosY;
235  SDL_GetWindowSize(win, &winWidth, &winHeight);
236  SDL_GetWindowPosition(win, &winPosX, &winPosY);
237  int dx = x - (winWidth / 2);
238  int dy = y - (winHeight / 2);
239  SDL_SendMouseMotion(win, 0, SDL_GetMouse()->relative_mode, dx, dy);
240  set_mouse_position((winPosX + winWidth / 2), (winPosY + winHeight / 2));
241  if (!be_app->IsCursorHidden())
242  be_app->HideCursor();
243  } else {
244  SDL_SendMouseMotion(win, 0, 0, x, y);
245  if (SDL_ShowCursor(-1) && be_app->IsCursorHidden())
246  be_app->ShowCursor();
247  }
248 
249  /* Tell the application that the mouse passed over, redraw needed */
251  }
252 
253  void _HandleMouseButton(BMessage *msg) {
254  SDL_Window *win;
255  int32 winID;
256  int32 button, state; /* left/middle/right, pressed/released */
257  if(
258  !_GetWinID(msg, &winID) ||
259  msg->FindInt32("button-id", &button) != B_OK ||
260  msg->FindInt32("button-state", &state) != B_OK
261  ) {
262  return;
263  }
264  win = GetSDLWindow(winID);
265  SDL_SendMouseButton(win, 0, state, button);
266  }
267 
268  void _HandleMouseWheel(BMessage *msg) {
269  SDL_Window *win;
270  int32 winID;
271  int32 xTicks, yTicks;
272  if(
273  !_GetWinID(msg, &winID) ||
274  msg->FindInt32("xticks", &xTicks) != B_OK ||
275  msg->FindInt32("yticks", &yTicks) != B_OK
276  ) {
277  return;
278  }
279  win = GetSDLWindow(winID);
280  SDL_SendMouseWheel(win, 0, xTicks, -yTicks, SDL_MOUSEWHEEL_NORMAL);
281  }
282 
283  void _HandleKey(BMessage *msg) {
284  int32 scancode, state; /* scancode, pressed/released */
285  if(
286  msg->FindInt32("key-state", &state) != B_OK ||
287  msg->FindInt32("key-scancode", &scancode) != B_OK
288  ) {
289  return;
290  }
291 
292  /* Make sure this isn't a repeated event (key pressed and held) */
293  if(state == SDL_PRESSED && HAIKU_GetKeyState(scancode) == SDL_PRESSED) {
294  return;
295  }
296  HAIKU_SetKeyState(scancode, state);
298 
300  const int8 *keyUtf8;
301  ssize_t count;
302  if (msg->FindData("key-utf8", B_INT8_TYPE, (const void**)&keyUtf8, &count) == B_OK) {
304  SDL_zeroa(text);
305  SDL_memcpy(text, keyUtf8, count);
307  }
308  }
309  }
310 
311  void _HandleMouseFocus(BMessage *msg) {
312  SDL_Window *win;
313  int32 winID;
314  bool bSetFocus; /* If false, lose focus */
315  if(
316  !_GetWinID(msg, &winID) ||
317  msg->FindBool("focusGained", &bSetFocus) != B_OK
318  ) {
319  return;
320  }
321  win = GetSDLWindow(winID);
322  if(bSetFocus) {
323  SDL_SetMouseFocus(win);
324  } else if(SDL_GetMouseFocus() == win) {
325  /* Only lose all focus if this window was the current focus */
327  }
328  }
329 
330  void _HandleKeyboardFocus(BMessage *msg) {
331  SDL_Window *win;
332  int32 winID;
333  bool bSetFocus; /* If false, lose focus */
334  if(
335  !_GetWinID(msg, &winID) ||
336  msg->FindBool("focusGained", &bSetFocus) != B_OK
337  ) {
338  return;
339  }
340  win = GetSDLWindow(winID);
341  if(bSetFocus) {
343  } else if(SDL_GetKeyboardFocus() == win) {
344  /* Only lose all focus if this window was the current focus */
346  }
347  }
348 
349  void _HandleWindowMoved(BMessage *msg) {
350  SDL_Window *win;
351  int32 winID;
352  int32 xPos, yPos;
353  /* Get the window id and new x/y position of the window */
354  if(
355  !_GetWinID(msg, &winID) ||
356  msg->FindInt32("window-x", &xPos) != B_OK ||
357  msg->FindInt32("window-y", &yPos) != B_OK
358  ) {
359  return;
360  }
361  win = GetSDLWindow(winID);
362  SDL_SendWindowEvent(win, SDL_WINDOWEVENT_MOVED, xPos, yPos);
363  }
364 
365  void _HandleWindowResized(BMessage *msg) {
366  SDL_Window *win;
367  int32 winID;
368  int32 w, h;
369  /* Get the window id ]and new x/y position of the window */
370  if(
371  !_GetWinID(msg, &winID) ||
372  msg->FindInt32("window-w", &w) != B_OK ||
373  msg->FindInt32("window-h", &h) != B_OK
374  ) {
375  return;
376  }
377  win = GetSDLWindow(winID);
379  }
380 
381  bool _GetWinID(BMessage *msg, int32 *winID) {
382  return msg->FindInt32("window-id", winID) == B_OK;
383  }
384 
385 
386 
387  /* Vector functions: Wraps vector stuff in case we need to change
388  implementation */
389  void _SetSDLWindow(SDL_Window *win, int32 winID) {
390  _window_map[winID] = win;
391  }
392 
394  return _window_map.size();
395  }
396 
397 
398  void _PopBackWindow() {
399  _window_map.pop_back();
400  }
401 
403  _window_map.push_back(win);
404  }
405 
406 
407  /* Members */
408  std::vector<SDL_Window*> _window_map; /* Keeps track of SDL_Windows by index-id */
409 
410 #if SDL_VIDEO_OPENGL
412 #endif
413 };
414 
415 #endif
SDL_BApp::~SDL_BApp
virtual ~SDL_BApp()
Definition: SDL_BApp.h:91
SDL_GetMouse
SDL_Mouse * SDL_GetMouse(void)
Definition: SDL_mouse.c:170
SDL_BApp::_HandleMouseFocus
void _HandleMouseFocus(BMessage *msg)
Definition: SDL_BApp.h:311
SDL_BApp::_HandleBasicWindowEvent
void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType)
Definition: SDL_BApp.h:207
SDL_BApp::_GetNumWindowSlots
int32 _GetNumWindowSlots()
Definition: SDL_BApp.h:393
SDL_BApp::_GetWinID
bool _GetWinID(BMessage *msg, int32 *winID)
Definition: SDL_BApp.h:381
SDL_BApp::_HandleMouseWheel
void _HandleMouseWheel(BMessage *msg)
Definition: SDL_BApp.h:268
SDL_BApp::_HandleWindowMoved
void _HandleWindowMoved(BMessage *msg)
Definition: SDL_BApp.h:349
BAPP_RESTORE
@ BAPP_RESTORE
Definition: SDL_BApp.h:67
SDL_BApp::_PopBackWindow
void _PopBackWindow()
Definition: SDL_BApp.h:398
BAPP_MOUSE_MOVED
@ BAPP_MOUSE_MOVED
Definition: SDL_BApp.h:59
NULL
#define NULL
Definition: begin_code.h:167
message
GLuint GLsizei const GLchar * message
Definition: SDL_opengl_glext.h:2486
HAIKU_GetKeyState
int8 HAIKU_GetKeyState(int32 bkey)
SDL_WINDOWEVENT_CLOSE
@ SDL_WINDOWEVENT_CLOSE
Definition: SDL_video.h:166
BAPP_KEY
@ BAPP_KEY
Definition: SDL_BApp.h:62
SDL_BWin
Definition: SDL_BWin.h:65
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
BAPP_HIDE
@ BAPP_HIDE
Definition: SDL_BApp.h:69
BAPP_SHOW
@ BAPP_SHOW
Definition: SDL_BApp.h:68
BAPP_MINIMIZE
@ BAPP_MINIMIZE
Definition: SDL_BApp.h:66
SDL_WINDOWEVENT_RESIZED
@ SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:154
BAPP_REPAINT
@ BAPP_REPAINT
Definition: SDL_BApp.h:63
SDL_BApp::_SetSDLWindow
void _SetSDLWindow(SDL_Window *win, int32 winID)
Definition: SDL_BApp.h:389
SDL_SetKeyboardFocus
void SDL_SetKeyboardFocus(SDL_Window *window)
Definition: SDL_keyboard.c:630
BAPP_KEYBOARD_FOCUS
@ BAPP_KEYBOARD_FOCUS
Definition: SDL_BApp.h:71
SDL_BApp::_HandleKeyboardFocus
void _HandleKeyboardFocus(BMessage *msg)
Definition: SDL_BApp.h:330
SDL_WINDOWEVENT_MOVED
@ SDL_WINDOWEVENT_MOVED
Definition: SDL_video.h:152
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
SDL_zeroa
#define SDL_zeroa(x)
Definition: SDL_stdinc.h:420
SDL_SetMouseFocus
void SDL_SetMouseFocus(SDL_Window *window)
Definition: SDL_mouse.c:203
SDL_SendKeyboardKey
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
Definition: SDL_keyboard.c:679
SDL_BApp::_PushBackWindow
void _PushBackWindow(SDL_Window *win)
Definition: SDL_BApp.h:402
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:75
SDL_GetKeyboardFocus
#define SDL_GetKeyboardFocus
Definition: SDL_dynapi_overrides.h:216
SDL_WINDOWEVENT_SHOWN
@ SDL_WINDOWEVENT_SHOWN
Definition: SDL_video.h:148
SDL_GetWindowSize
#define SDL_GetWindowSize
Definition: SDL_dynapi_overrides.h:527
SDL_SendKeyboardText
int SDL_SendKeyboardText(const char *text)
Definition: SDL_keyboard.c:789
SDL_QUERY
#define SDL_QUERY
Definition: SDL_events.h:757
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
SDL_BApp::ClearID
void ClearID(SDL_BWin *bwin)
SDL_BApp::_window_map
std::vector< SDL_Window * > _window_map
Definition: SDL_BApp.h:408
BAPP_MOUSE_FOCUS
@ BAPP_MOUSE_FOCUS
Definition: SDL_BApp.h:70
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_TEXTINPUT
@ SDL_TEXTINPUT
Definition: SDL_events.h:99
HAIKU_SetKeyState
void HAIKU_SetKeyState(int32 bkey, int8 state)
HAIKU_GetScancodeFromBeKey
SDL_Scancode HAIKU_GetScancodeFromBeKey(int32 bkey)
SDL_SendMouseMotion
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
Definition: SDL_mouse.c:293
SDL_WINDOWEVENT_EXPOSED
@ SDL_WINDOWEVENT_EXPOSED
Definition: SDL_video.h:150
SDL_GetWindowPosition
#define SDL_GetWindowPosition
Definition: SDL_dynapi_overrides.h:525
SDL_BApp::_HandleKey
void _HandleKey(BMessage *msg)
Definition: SDL_BApp.h:283
text
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
HAIKU_UpdateWindowFramebuffer
int HAIKU_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
SDL_WINDOWEVENT_MINIMIZED
@ SDL_WINDOWEVENT_MINIMIZED
Definition: SDL_video.h:158
SDL_BApp::GetSDLWindow
SDL_Window * GetSDLWindow(int32 winID)
Definition: SDL_BApp.h:191
SDL_BApp::MessageReceived
virtual void MessageReceived(BMessage *message)
Definition: SDL_BApp.h:97
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_BApp::_current_context
BGLView * _current_context
Definition: SDL_BApp.h:411
SDL_SendMouseWheel
int SDL_SendMouseWheel(SDL_Window *window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
Definition: SDL_mouse.c:600
SDL_TEXTINPUTEVENT_TEXT_SIZE
#define SDL_TEXTINPUTEVENT_TEXT_SIZE
Definition: SDL_events.h:238
SDL_BApp::_HandleMouseMove
void _HandleMouseMove(BMessage *msg)
Definition: SDL_BApp.h:219
SDL_BApp::GetID
int32 GetID(SDL_Window *win)
Definition: SDL_BApp.h:167
SDL_GetMouseFocus
#define SDL_GetMouseFocus
Definition: SDL_dynapi_overrides.h:245
SDL_BApp::_HandleMouseButton
void _HandleMouseButton(BMessage *msg)
Definition: SDL_BApp.h:253
BAPP_WINDOW_CLOSE_REQUESTED
@ BAPP_WINDOW_CLOSE_REQUESTED
Definition: SDL_BApp.h:72
SDL_EventState
#define SDL_EventState
Definition: SDL_dynapi_overrides.h:131
SDL_SendWindowEvent
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
Definition: SDL_windowevents.c:74
ToSDL
ToSDL
Definition: SDL_BApp.h:57
BAPP_WINDOW_MOVED
@ BAPP_WINDOW_MOVED
Definition: SDL_BApp.h:73
BAPP_WINDOW_RESIZED
@ BAPP_WINDOW_RESIZED
Definition: SDL_BApp.h:74
BAPP_MAXIMIZE
@ BAPP_MAXIMIZE
Definition: SDL_BApp.h:65
SDL_MOUSEWHEEL_NORMAL
@ SDL_MOUSEWHEEL_NORMAL
Definition: SDL_mouse.h:68
SDL_ShowCursor
#define SDL_ShowCursor
Definition: SDL_dynapi_overrides.h:258
SDL_WINDOWEVENT_HIDDEN
@ SDL_WINDOWEVENT_HIDDEN
Definition: SDL_video.h:149
SDL_video.h
BAPP_SCREEN_CHANGED
@ BAPP_SCREEN_CHANGED
Definition: SDL_BApp.h:75
SDL_BApp
Definition: SDL_BApp.h:81
SDL_BApp::_HandleWindowResized
void _HandleWindowResized(BMessage *msg)
Definition: SDL_BApp.h:365
SDL_BApp::SetCurrentContext
void SetCurrentContext(BGLView *newContext)
Definition: SDL_BApp.h:196
BAPP_MOUSE_BUTTON
@ BAPP_MOUSE_BUTTON
Definition: SDL_BApp.h:60
SDL_BApp::SDL_BApp
SDL_BApp(const char *signature)
Definition: SDL_BApp.h:83
state
struct xkb_state * state
Definition: SDL_waylandsym.h:114
signature
const char * signature
button
SDL_Texture * button
Definition: testgamecontroller.c:67
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_SendMouseButton
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
Definition: SDL_mouse.c:594
SDL_WINDOWEVENT_MAXIMIZED
@ SDL_WINDOWEVENT_MAXIMIZED
Definition: SDL_video.h:159
BAPP_MOUSE_WHEEL
@ BAPP_MOUSE_WHEEL
Definition: SDL_BApp.h:61
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:734