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 #include <LocaleRoster.h>
26 #if SDL_VIDEO_OPENGL
27 #include <OpenGLKit.h>
28 #endif
29 
30 #include "../../video/haiku/SDL_bkeyboard.h"
31 
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include "../../SDL_internal.h"
38 
39 #include "SDL_video.h"
40 
41 /* Local includes */
42 #include "../../events/SDL_events_c.h"
43 #include "../../video/haiku/SDL_bframebuffer.h"
44 
45 #ifdef __cplusplus
46 }
47 #endif
48 
49 #include <vector>
50 
51 
52 
53 
54 /* Forward declarations */
55 class SDL_BWin;
56 
57 /* Message constants */
58 enum ToSDL {
59  /* Intercepted by BWindow on its way to BView */
64  BAPP_REPAINT, /* from _UPDATE_ */
65  /* From BWindow */
66  BAPP_MAXIMIZE, /* from B_ZOOM */
68  BAPP_RESTORE, /* TODO: IMPLEMENT! */
71  BAPP_MOUSE_FOCUS, /* caused by MOUSE_MOVE */
72  BAPP_KEYBOARD_FOCUS, /* from WINDOW_ACTIVATED */
77 };
78 
79 
80 
81 /* Create a descendant of BApplication */
82 class SDL_BApp : public BApplication {
83 public:
84  SDL_BApp(const char* signature) :
85  BApplication(signature) {
86 #if SDL_VIDEO_OPENGL
88 #endif
89  }
90 
91 
92  virtual ~SDL_BApp() {
93  }
94 
95 
96 
97  /* Event-handling functions */
98  virtual void MessageReceived(BMessage* message) {
99  /* Sort out SDL-related messages */
100  switch ( message->what ) {
101  case BAPP_MOUSE_MOVED:
103  break;
104 
105  case BAPP_MOUSE_BUTTON:
107  break;
108 
109  case BAPP_MOUSE_WHEEL:
111  break;
112 
113  case BAPP_KEY:
115  break;
116 
117  case BAPP_REPAINT:
119  break;
120 
121  case BAPP_MAXIMIZE:
123  break;
124 
125  case BAPP_MINIMIZE:
127  break;
128 
129  case BAPP_SHOW:
131  break;
132 
133  case BAPP_HIDE:
135  break;
136 
137  case BAPP_MOUSE_FOCUS:
139  break;
140 
141  case BAPP_KEYBOARD_FOCUS:
143  break;
144 
147  break;
148 
149  case BAPP_WINDOW_MOVED:
151  break;
152 
153  case BAPP_WINDOW_RESIZED:
155  break;
156 
157  case B_LOCALE_CHANGED:
159  break;
160 
161  case BAPP_SCREEN_CHANGED:
162  /* TODO: Handle screen resize or workspace change */
163  break;
164 
165  default:
166  BApplication::MessageReceived(message);
167  break;
168  }
169  }
170 
171  /* Window creation/destruction methods */
172  int32 GetID(SDL_Window *win) {
173  int32 i;
174  for(i = 0; i < _GetNumWindowSlots(); ++i) {
175  if( GetSDLWindow(i) == NULL ) {
176  _SetSDLWindow(win, i);
177  return i;
178  }
179  }
180 
181  /* Expand the vector if all slots are full */
182  if( i == _GetNumWindowSlots() ) {
183  _PushBackWindow(win);
184  return i;
185  }
186 
187  /* TODO: error handling */
188  return 0;
189  }
190 
191  /* FIXME: Bad coding practice, but I can't include SDL_BWin.h here. Is
192  there another way to do this? */
193  void ClearID(SDL_BWin *bwin); /* Defined in SDL_BeApp.cc */
194 
195 
196  SDL_Window *GetSDLWindow(int32 winID) {
197  return _window_map[winID];
198  }
199 
200 #if SDL_VIDEO_OPENGL
201  void SetCurrentContext(BGLView *newContext) {
202  if(_current_context)
203  _current_context->UnlockGL();
204  _current_context = newContext;
205  if (_current_context)
206  _current_context->LockGL();
207  }
208 #endif
209 
210 private:
211  /* Event management */
212  void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType) {
213  SDL_Window *win;
214  int32 winID;
215  if(
216  !_GetWinID(msg, &winID)
217  ) {
218  return;
219  }
220  win = GetSDLWindow(winID);
221  SDL_SendWindowEvent(win, sdlEventType, 0, 0);
222  }
223 
224  void _HandleMouseMove(BMessage *msg) {
225  SDL_Window *win;
226  int32 winID;
227  int32 x = 0, y = 0;
228  if(
229  !_GetWinID(msg, &winID) ||
230  msg->FindInt32("x", &x) != B_OK || /* x movement */
231  msg->FindInt32("y", &y) != B_OK /* y movement */
232  ) {
233  return;
234  }
235  win = GetSDLWindow(winID);
236 
237  // Simple relative mode support for mouse.
238  if (SDL_GetMouse()->relative_mode) {
239  int winWidth, winHeight, winPosX, winPosY;
240  SDL_GetWindowSize(win, &winWidth, &winHeight);
241  SDL_GetWindowPosition(win, &winPosX, &winPosY);
242  int dx = x - (winWidth / 2);
243  int dy = y - (winHeight / 2);
244  SDL_SendMouseMotion(win, 0, SDL_GetMouse()->relative_mode, dx, dy);
245  set_mouse_position((winPosX + winWidth / 2), (winPosY + winHeight / 2));
246  if (!be_app->IsCursorHidden())
247  be_app->HideCursor();
248  } else {
249  SDL_SendMouseMotion(win, 0, 0, x, y);
250  if (SDL_ShowCursor(-1) && be_app->IsCursorHidden())
251  be_app->ShowCursor();
252  }
253 
254  /* Tell the application that the mouse passed over, redraw needed */
256  }
257 
258  void _HandleMouseButton(BMessage *msg) {
259  SDL_Window *win;
260  int32 winID;
261  int32 button, state; /* left/middle/right, pressed/released */
262  if(
263  !_GetWinID(msg, &winID) ||
264  msg->FindInt32("button-id", &button) != B_OK ||
265  msg->FindInt32("button-state", &state) != B_OK
266  ) {
267  return;
268  }
269  win = GetSDLWindow(winID);
270  SDL_SendMouseButton(win, 0, state, button);
271  }
272 
273  void _HandleMouseWheel(BMessage *msg) {
274  SDL_Window *win;
275  int32 winID;
276  int32 xTicks, yTicks;
277  if(
278  !_GetWinID(msg, &winID) ||
279  msg->FindInt32("xticks", &xTicks) != B_OK ||
280  msg->FindInt32("yticks", &yTicks) != B_OK
281  ) {
282  return;
283  }
284  win = GetSDLWindow(winID);
285  SDL_SendMouseWheel(win, 0, xTicks, -yTicks, SDL_MOUSEWHEEL_NORMAL);
286  }
287 
288  void _HandleKey(BMessage *msg) {
289  int32 scancode, state; /* scancode, pressed/released */
290  if(
291  msg->FindInt32("key-state", &state) != B_OK ||
292  msg->FindInt32("key-scancode", &scancode) != B_OK
293  ) {
294  return;
295  }
296 
297  /* Make sure this isn't a repeated event (key pressed and held) */
298  if(state == SDL_PRESSED && HAIKU_GetKeyState(scancode) == SDL_PRESSED) {
299  return;
300  }
301  HAIKU_SetKeyState(scancode, state);
303 
305  const int8 *keyUtf8;
306  ssize_t count;
307  if (msg->FindData("key-utf8", B_INT8_TYPE, (const void**)&keyUtf8, &count) == B_OK) {
309  SDL_zeroa(text);
310  SDL_memcpy(text, keyUtf8, count);
312  }
313  }
314  }
315 
316  void _HandleMouseFocus(BMessage *msg) {
317  SDL_Window *win;
318  int32 winID;
319  bool bSetFocus; /* If false, lose focus */
320  if(
321  !_GetWinID(msg, &winID) ||
322  msg->FindBool("focusGained", &bSetFocus) != B_OK
323  ) {
324  return;
325  }
326  win = GetSDLWindow(winID);
327  if(bSetFocus) {
328  SDL_SetMouseFocus(win);
329  } else if(SDL_GetMouseFocus() == win) {
330  /* Only lose all focus if this window was the current focus */
332  }
333  }
334 
335  void _HandleKeyboardFocus(BMessage *msg) {
336  SDL_Window *win;
337  int32 winID;
338  bool bSetFocus; /* If false, lose focus */
339  if(
340  !_GetWinID(msg, &winID) ||
341  msg->FindBool("focusGained", &bSetFocus) != B_OK
342  ) {
343  return;
344  }
345  win = GetSDLWindow(winID);
346  if(bSetFocus) {
348  } else if(SDL_GetKeyboardFocus() == win) {
349  /* Only lose all focus if this window was the current focus */
351  }
352  }
353 
354  void _HandleWindowMoved(BMessage *msg) {
355  SDL_Window *win;
356  int32 winID;
357  int32 xPos, yPos;
358  /* Get the window id and new x/y position of the window */
359  if(
360  !_GetWinID(msg, &winID) ||
361  msg->FindInt32("window-x", &xPos) != B_OK ||
362  msg->FindInt32("window-y", &yPos) != B_OK
363  ) {
364  return;
365  }
366  win = GetSDLWindow(winID);
367  SDL_SendWindowEvent(win, SDL_WINDOWEVENT_MOVED, xPos, yPos);
368  }
369 
370  void _HandleWindowResized(BMessage *msg) {
371  SDL_Window *win;
372  int32 winID;
373  int32 w, h;
374  /* Get the window id ]and new x/y position of the window */
375  if(
376  !_GetWinID(msg, &winID) ||
377  msg->FindInt32("window-w", &w) != B_OK ||
378  msg->FindInt32("window-h", &h) != B_OK
379  ) {
380  return;
381  }
382  win = GetSDLWindow(winID);
384  }
385 
386  bool _GetWinID(BMessage *msg, int32 *winID) {
387  return msg->FindInt32("window-id", winID) == B_OK;
388  }
389 
390 
391 
392  /* Vector functions: Wraps vector stuff in case we need to change
393  implementation */
394  void _SetSDLWindow(SDL_Window *win, int32 winID) {
395  _window_map[winID] = win;
396  }
397 
399  return _window_map.size();
400  }
401 
402 
403  void _PopBackWindow() {
404  _window_map.pop_back();
405  }
406 
408  _window_map.push_back(win);
409  }
410 
411 
412  /* Members */
413  std::vector<SDL_Window*> _window_map; /* Keeps track of SDL_Windows by index-id */
414 
415 #if SDL_VIDEO_OPENGL
417 #endif
418 };
419 
420 #endif
ToSDL
Definition: SDL_BApp.h:58
@ BAPP_MAXIMIZE
Definition: SDL_BApp.h:66
@ BAPP_SCREEN_CHANGED
Definition: SDL_BApp.h:76
@ BAPP_MOUSE_FOCUS
Definition: SDL_BApp.h:71
@ BAPP_WINDOW_MOVED
Definition: SDL_BApp.h:74
@ BAPP_WINDOW_CLOSE_REQUESTED
Definition: SDL_BApp.h:73
@ BAPP_RESTORE
Definition: SDL_BApp.h:68
@ BAPP_KEY
Definition: SDL_BApp.h:63
@ BAPP_MOUSE_BUTTON
Definition: SDL_BApp.h:61
@ BAPP_REPAINT
Definition: SDL_BApp.h:64
@ BAPP_HIDE
Definition: SDL_BApp.h:70
@ BAPP_KEYBOARD_FOCUS
Definition: SDL_BApp.h:72
@ BAPP_MOUSE_MOVED
Definition: SDL_BApp.h:60
@ BAPP_SHOW
Definition: SDL_BApp.h:69
@ BAPP_MINIMIZE
Definition: SDL_BApp.h:67
@ BAPP_WINDOW_RESIZED
Definition: SDL_BApp.h:75
@ BAPP_MOUSE_WHEEL
Definition: SDL_BApp.h:62
const char * signature
int HAIKU_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
int8 HAIKU_GetKeyState(int32 bkey)
void HAIKU_SetKeyState(int32 bkey, int8 state)
SDL_Scancode HAIKU_GetScancodeFromBeKey(int32 bkey)
#define SDL_GetWindowSize
#define SDL_ShowCursor
#define SDL_GetKeyboardFocus
#define SDL_EventState
#define SDL_GetMouseFocus
#define SDL_GetWindowPosition
#define SDL_memcpy
int SDL_SendLocaleChangedEvent(void)
Definition: SDL_events.c:1072
@ SDL_TEXTINPUT
Definition: SDL_events.h:101
#define SDL_TEXTINPUTEVENT_TEXT_SIZE
Definition: SDL_events.h:244
#define SDL_QUERY
Definition: SDL_events.h:792
#define SDL_PRESSED
Definition: SDL_events.h:50
void SDL_SetKeyboardFocus(SDL_Window *window)
Definition: SDL_keyboard.c:634
int SDL_SendKeyboardText(const char *text)
Definition: SDL_keyboard.c:848
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
Definition: SDL_keyboard.c:806
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
Definition: SDL_mouse.c:599
SDL_Mouse * SDL_GetMouse(void)
Definition: SDL_mouse.c:175
void SDL_SetMouseFocus(SDL_Window *window)
Definition: SDL_mouse.c:208
int SDL_SendMouseWheel(SDL_Window *window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
Definition: SDL_mouse.c:605
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
Definition: SDL_mouse.c:298
@ SDL_MOUSEWHEEL_NORMAL
Definition: SDL_mouse.h:68
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLuint GLsizei const GLchar * message
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
#define SDL_zeroa(x)
Definition: SDL_stdinc.h:428
@ SDL_WINDOWEVENT_HIDDEN
Definition: SDL_video.h:150
@ SDL_WINDOWEVENT_CLOSE
Definition: SDL_video.h:167
@ SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:155
@ SDL_WINDOWEVENT_SHOWN
Definition: SDL_video.h:149
@ SDL_WINDOWEVENT_MOVED
Definition: SDL_video.h:153
@ SDL_WINDOWEVENT_MINIMIZED
Definition: SDL_video.h:159
@ SDL_WINDOWEVENT_MAXIMIZED
Definition: SDL_video.h:160
@ SDL_WINDOWEVENT_EXPOSED
Definition: SDL_video.h:151
struct xkb_state * state
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
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
#define NULL
Definition: begin_code.h:163
void ClearID(SDL_BWin *bwin)
void _HandleWindowResized(BMessage *msg)
Definition: SDL_BApp.h:370
int32 _GetNumWindowSlots()
Definition: SDL_BApp.h:398
void _PopBackWindow()
Definition: SDL_BApp.h:403
virtual void MessageReceived(BMessage *message)
Definition: SDL_BApp.h:98
std::vector< SDL_Window * > _window_map
Definition: SDL_BApp.h:413
void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType)
Definition: SDL_BApp.h:212
void _HandleMouseFocus(BMessage *msg)
Definition: SDL_BApp.h:316
virtual ~SDL_BApp()
Definition: SDL_BApp.h:92
void _SetSDLWindow(SDL_Window *win, int32 winID)
Definition: SDL_BApp.h:394
void _HandleKeyboardFocus(BMessage *msg)
Definition: SDL_BApp.h:335
void _PushBackWindow(SDL_Window *win)
Definition: SDL_BApp.h:407
void _HandleWindowMoved(BMessage *msg)
Definition: SDL_BApp.h:354
void _HandleMouseWheel(BMessage *msg)
Definition: SDL_BApp.h:273
BGLView * _current_context
Definition: SDL_BApp.h:416
void SetCurrentContext(BGLView *newContext)
Definition: SDL_BApp.h:201
SDL_Window * GetSDLWindow(int32 winID)
Definition: SDL_BApp.h:196
void _HandleMouseMove(BMessage *msg)
Definition: SDL_BApp.h:224
int32 GetID(SDL_Window *win)
Definition: SDL_BApp.h:172
void _HandleKey(BMessage *msg)
Definition: SDL_BApp.h:288
void _HandleMouseButton(BMessage *msg)
Definition: SDL_BApp.h:258
SDL_BApp(const char *signature)
Definition: SDL_BApp.h:84
bool _GetWinID(BMessage *msg, int32 *winID)
Definition: SDL_BApp.h:386
The type used to identify a window.
Definition: SDL_sysvideo.h:75
SDL_Texture * button
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47