SDL 2.0
SDL_BApp.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 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
33extern "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 */
54class SDL_BWin;
55
56/* Message constants */
57enum 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 */
81class SDL_BApp : public BApplication {
82public:
83 SDL_BApp(const char* signature) :
84 BApplication(signature) {
85#if SDL_VIDEO_OPENGL
86 _current_context = NULL;
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
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
142 break;
143
146 break;
147
150 break;
151
154 break;
155
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
205private:
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 SDL_SendMouseMotion(win, 0, 0, x, y);
232
233 /* Tell the application that the mouse passed over, redraw needed */
235 }
236
237 void _HandleMouseButton(BMessage *msg) {
238 SDL_Window *win;
239 int32 winID;
240 int32 button, state; /* left/middle/right, pressed/released */
241 if(
242 !_GetWinID(msg, &winID) ||
243 msg->FindInt32("button-id", &button) != B_OK ||
244 msg->FindInt32("button-state", &state) != B_OK
245 ) {
246 return;
247 }
248 win = GetSDLWindow(winID);
250 }
251
252 void _HandleMouseWheel(BMessage *msg) {
253 SDL_Window *win;
254 int32 winID;
255 int32 xTicks, yTicks;
256 if(
257 !_GetWinID(msg, &winID) ||
258 msg->FindInt32("xticks", &xTicks) != B_OK ||
259 msg->FindInt32("yticks", &yTicks) != B_OK
260 ) {
261 return;
262 }
263 win = GetSDLWindow(winID);
264 SDL_SendMouseWheel(win, 0, xTicks, yTicks, SDL_MOUSEWHEEL_NORMAL);
265 }
266
267 void _HandleKey(BMessage *msg) {
268 int32 scancode, state; /* scancode, pressed/released */
269 if(
270 msg->FindInt32("key-state", &state) != B_OK ||
271 msg->FindInt32("key-scancode", &scancode) != B_OK
272 ) {
273 return;
274 }
275
276 /* Make sure this isn't a repeated event (key pressed and held) */
277 if(state == SDL_PRESSED && HAIKU_GetKeyState(scancode) == SDL_PRESSED) {
278 return;
279 }
280 HAIKU_SetKeyState(scancode, state);
282
284 const int8 *keyUtf8;
285 ssize_t count;
286 if (msg->FindData("key-utf8", B_INT8_TYPE, (const void**)&keyUtf8, &count) == B_OK) {
288 SDL_zero(text);
289 SDL_memcpy(text, keyUtf8, count);
291 }
292 }
293 }
294
295 void _HandleMouseFocus(BMessage *msg) {
296 SDL_Window *win;
297 int32 winID;
298 bool bSetFocus; /* If false, lose focus */
299 if(
300 !_GetWinID(msg, &winID) ||
301 msg->FindBool("focusGained", &bSetFocus) != B_OK
302 ) {
303 return;
304 }
305 win = GetSDLWindow(winID);
306 if(bSetFocus) {
308 } else if(SDL_GetMouseFocus() == win) {
309 /* Only lose all focus if this window was the current focus */
311 }
312 }
313
314 void _HandleKeyboardFocus(BMessage *msg) {
315 SDL_Window *win;
316 int32 winID;
317 bool bSetFocus; /* If false, lose focus */
318 if(
319 !_GetWinID(msg, &winID) ||
320 msg->FindBool("focusGained", &bSetFocus) != B_OK
321 ) {
322 return;
323 }
324 win = GetSDLWindow(winID);
325 if(bSetFocus) {
327 } else if(SDL_GetKeyboardFocus() == win) {
328 /* Only lose all focus if this window was the current focus */
330 }
331 }
332
333 void _HandleWindowMoved(BMessage *msg) {
334 SDL_Window *win;
335 int32 winID;
336 int32 xPos, yPos;
337 /* Get the window id and new x/y position of the window */
338 if(
339 !_GetWinID(msg, &winID) ||
340 msg->FindInt32("window-x", &xPos) != B_OK ||
341 msg->FindInt32("window-y", &yPos) != B_OK
342 ) {
343 return;
344 }
345 win = GetSDLWindow(winID);
347 }
348
349 void _HandleWindowResized(BMessage *msg) {
350 SDL_Window *win;
351 int32 winID;
352 int32 w, h;
353 /* Get the window id ]and new x/y position of the window */
354 if(
355 !_GetWinID(msg, &winID) ||
356 msg->FindInt32("window-w", &w) != B_OK ||
357 msg->FindInt32("window-h", &h) != B_OK
358 ) {
359 return;
360 }
361 win = GetSDLWindow(winID);
363 }
364
365 bool _GetWinID(BMessage *msg, int32 *winID) {
366 return msg->FindInt32("window-id", winID) == B_OK;
367 }
368
369
370
371 /* Vector functions: Wraps vector stuff in case we need to change
372 implementation */
373 void _SetSDLWindow(SDL_Window *win, int32 winID) {
374 _window_map[winID] = win;
375 }
376
378 return _window_map.size();
379 }
380
381
383 _window_map.pop_back();
384 }
385
387 _window_map.push_back(win);
388 }
389
390
391 /* Members */
392 std::vector<SDL_Window*> _window_map; /* Keeps track of SDL_Windows by index-id */
393
394#if SDL_VIDEO_OPENGL
395 BGLView *_current_context;
396#endif
397};
398
399#endif
ToSDL
Definition: SDL_BApp.h:57
@ BAPP_MAXIMIZE
Definition: SDL_BApp.h:65
@ BAPP_SCREEN_CHANGED
Definition: SDL_BApp.h:75
@ BAPP_MOUSE_FOCUS
Definition: SDL_BApp.h:70
@ BAPP_WINDOW_MOVED
Definition: SDL_BApp.h:73
@ BAPP_WINDOW_CLOSE_REQUESTED
Definition: SDL_BApp.h:72
@ BAPP_RESTORE
Definition: SDL_BApp.h:67
@ BAPP_KEY
Definition: SDL_BApp.h:62
@ BAPP_MOUSE_BUTTON
Definition: SDL_BApp.h:60
@ BAPP_REPAINT
Definition: SDL_BApp.h:63
@ BAPP_HIDE
Definition: SDL_BApp.h:69
@ BAPP_KEYBOARD_FOCUS
Definition: SDL_BApp.h:71
@ BAPP_MOUSE_MOVED
Definition: SDL_BApp.h:59
@ BAPP_SHOW
Definition: SDL_BApp.h:68
@ BAPP_MINIMIZE
Definition: SDL_BApp.h:66
@ BAPP_WINDOW_RESIZED
Definition: SDL_BApp.h:74
@ BAPP_MOUSE_WHEEL
Definition: SDL_BApp.h:61
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_GetKeyboardFocus
#define SDL_EventState
#define SDL_GetMouseFocus
#define SDL_memcpy
@ SDL_TEXTINPUT
Definition: SDL_events.h:99
#define SDL_TEXTINPUTEVENT_TEXT_SIZE
Definition: SDL_events.h:238
#define SDL_QUERY
Definition: SDL_events.h:756
#define SDL_PRESSED
Definition: SDL_events.h:50
void SDL_SetKeyboardFocus(SDL_Window *window)
Definition: SDL_keyboard.c:630
int SDL_SendKeyboardText(const char *text)
Definition: SDL_keyboard.c:789
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
Definition: SDL_keyboard.c:679
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
Definition: SDL_mouse.c:605
void SDL_SetMouseFocus(SDL_Window *window)
Definition: SDL_mouse.c:211
int SDL_SendMouseWheel(SDL_Window *window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
Definition: SDL_mouse.c:611
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
Definition: SDL_mouse.c:301
@ 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_zero(x)
Definition: SDL_stdinc.h:416
@ 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:167
void ClearID(SDL_BWin *bwin)
void _HandleWindowResized(BMessage *msg)
Definition: SDL_BApp.h:349
int32 _GetNumWindowSlots()
Definition: SDL_BApp.h:377
void _PopBackWindow()
Definition: SDL_BApp.h:382
virtual void MessageReceived(BMessage *message)
Definition: SDL_BApp.h:97
std::vector< SDL_Window * > _window_map
Definition: SDL_BApp.h:392
SDL_Window * GetSDLWindow(int32 winID)
Definition: SDL_BApp.h:191
void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType)
Definition: SDL_BApp.h:207
void _HandleMouseFocus(BMessage *msg)
Definition: SDL_BApp.h:295
virtual ~SDL_BApp()
Definition: SDL_BApp.h:91
void _SetSDLWindow(SDL_Window *win, int32 winID)
Definition: SDL_BApp.h:373
void _HandleKeyboardFocus(BMessage *msg)
Definition: SDL_BApp.h:314
void _PushBackWindow(SDL_Window *win)
Definition: SDL_BApp.h:386
void _HandleWindowMoved(BMessage *msg)
Definition: SDL_BApp.h:333
void _HandleMouseWheel(BMessage *msg)
Definition: SDL_BApp.h:252
void _HandleMouseMove(BMessage *msg)
Definition: SDL_BApp.h:219
int32 GetID(SDL_Window *win)
Definition: SDL_BApp.h:167
void _HandleKey(BMessage *msg)
Definition: SDL_BApp.h:267
void _HandleMouseButton(BMessage *msg)
Definition: SDL_BApp.h:237
SDL_BApp(const char *signature)
Definition: SDL_BApp.h:83
bool _GetWinID(BMessage *msg, int32 *winID)
Definition: SDL_BApp.h:365
The type used to identify a window.
Definition: SDL_sysvideo.h:74
SDL_Texture * button
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47