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