SDL_BApp.h
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL_BAPP_H
#define SDL_BAPP_H
#include <InterfaceKit.h>
#include <OpenGLKit.h>
#include "../../video/haiku/SDL_bkeyboard.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "../../SDL_internal.h"
#include "SDL_video.h"
/* Local includes */
#include "../../events/SDL_events_c.h"
#include "../../video/haiku/SDL_bkeyboard.h"
#include "../../video/haiku/SDL_bframebuffer.h"
#ifdef __cplusplus
}
#endif
#include <vector>
/* Forward declarations */
class SDL_BWin;
/* Message constants */
enum ToSDL {
/* Intercepted by BWindow on its way to BView */
BAPP_MOUSE_MOVED,
BAPP_MOUSE_BUTTON,
BAPP_MOUSE_WHEEL,
BAPP_KEY,
BAPP_REPAINT, /* from _UPDATE_ */
/* From BWindow */
BAPP_MAXIMIZE, /* from B_ZOOM */
BAPP_MINIMIZE,
BAPP_RESTORE, /* TODO: IMPLEMENT! */
BAPP_SHOW,
BAPP_HIDE,
BAPP_MOUSE_FOCUS, /* caused by MOUSE_MOVE */
BAPP_KEYBOARD_FOCUS, /* from WINDOW_ACTIVATED */
BAPP_WINDOW_CLOSE_REQUESTED,
BAPP_WINDOW_MOVED,
BAPP_WINDOW_RESIZED,
BAPP_SCREEN_CHANGED
};
/* Create a descendant of BApplication */
class SDL_BApp : public BApplication {
public:
SDL_BApp(const char* signature) :
BApplication(signature) {
_current_context = NULL;
}
virtual ~SDL_BApp() {
}
/* Event-handling functions */
virtual void MessageReceived(BMessage* message) {
/* Sort out SDL-related messages */
switch ( message->what ) {
case BAPP_MOUSE_MOVED:
_HandleMouseMove(message);
break;
case BAPP_MOUSE_BUTTON:
_HandleMouseButton(message);
break;
case BAPP_MOUSE_WHEEL:
_HandleMouseWheel(message);
break;
case BAPP_KEY:
_HandleKey(message);
break;
case BAPP_REPAINT:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_EXPOSED);
break;
case BAPP_MAXIMIZE:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_MAXIMIZED);
break;
case BAPP_MINIMIZE:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_MINIMIZED);
break;
case BAPP_SHOW:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_SHOWN);
break;
case BAPP_HIDE:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_HIDDEN);
break;
case BAPP_MOUSE_FOCUS:
_HandleMouseFocus(message);
break;
case BAPP_KEYBOARD_FOCUS:
_HandleKeyboardFocus(message);
break;
case BAPP_WINDOW_CLOSE_REQUESTED:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_CLOSE);
break;
case BAPP_WINDOW_MOVED:
_HandleWindowMoved(message);
break;
case BAPP_WINDOW_RESIZED:
_HandleWindowResized(message);
break;
case BAPP_SCREEN_CHANGED:
/* TODO: Handle screen resize or workspace change */
break;
default:
BApplication::MessageReceived(message);
break;
}
}
/* Window creation/destruction methods */
int32 GetID(SDL_Window *win) {
int32 i;
for(i = 0; i < _GetNumWindowSlots(); ++i) {
if( GetSDLWindow(i) == NULL ) {
_SetSDLWindow(win, i);
return i;
}
}
/* Expand the vector if all slots are full */
if( i == _GetNumWindowSlots() ) {
_PushBackWindow(win);
return i;
}
/* TODO: error handling */
return 0;
}
/* FIXME: Bad coding practice, but I can't include SDL_BWin.h here. Is
there another way to do this? */
void ClearID(SDL_BWin *bwin); /* Defined in SDL_BeApp.cc */
SDL_Window *GetSDLWindow(int32 winID) {
return _window_map[winID];
}
void SetCurrentContext(BGLView *newContext) {
if(_current_context)
_current_context->UnlockGL();
_current_context = newContext;
_current_context->LockGL();
}
private:
/* Event management */
void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType) {
SDL_Window *win;
int32 winID;
if(
!_GetWinID(msg, &winID)
) {
return;
}
win = GetSDLWindow(winID);
SDL_SendWindowEvent(win, sdlEventType, 0, 0);
}
void _HandleMouseMove(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 x = 0, y = 0;
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("x", &x) != B_OK || /* x movement */
msg->FindInt32("y", &y) != B_OK /* y movement */
) {
return;
}
win = GetSDLWindow(winID);
SDL_SendMouseMotion(win, 0, 0, x, y);
/* Tell the application that the mouse passed over, redraw needed */
BE_UpdateWindowFramebuffer(NULL,win,NULL,-1);
}
void _HandleMouseButton(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 button, state; /* left/middle/right, pressed/released */
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("button-id", &button) != B_OK ||
msg->FindInt32("button-state", &state) != B_OK
) {
return;
}
win = GetSDLWindow(winID);
SDL_SendMouseButton(win, 0, state, button);
}
void _HandleMouseWheel(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 xTicks, yTicks;
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("xticks", &xTicks) != B_OK ||
msg->FindInt32("yticks", &yTicks) != B_OK
) {
return;
}
win = GetSDLWindow(winID);
SDL_SendMouseWheel(win, 0, xTicks, yTicks, SDL_MOUSEWHEEL_NORMAL);
}
void _HandleKey(BMessage *msg) {
int32 scancode, state; /* scancode, pressed/released */
if(
msg->FindInt32("key-state", &state) != B_OK ||
msg->FindInt32("key-scancode", &scancode) != B_OK
) {
return;
}
/* Make sure this isn't a repeated event (key pressed and held) */
if(state == SDL_PRESSED && BE_GetKeyState(scancode) == SDL_PRESSED) {
return;
}
BE_SetKeyState(scancode, state);
SDL_SendKeyboardKey(state, BE_GetScancodeFromBeKey(scancode));
}
void _HandleMouseFocus(BMessage *msg) {
SDL_Window *win;
int32 winID;
bool bSetFocus; /* If false, lose focus */
if(
!_GetWinID(msg, &winID) ||
msg->FindBool("focusGained", &bSetFocus) != B_OK
) {
return;
}
win = GetSDLWindow(winID);
if(bSetFocus) {
SDL_SetMouseFocus(win);
} else if(SDL_GetMouseFocus() == win) {
/* Only lose all focus if this window was the current focus */
SDL_SetMouseFocus(NULL);
}
}
void _HandleKeyboardFocus(BMessage *msg) {
SDL_Window *win;
int32 winID;
bool bSetFocus; /* If false, lose focus */
if(
!_GetWinID(msg, &winID) ||
msg->FindBool("focusGained", &bSetFocus) != B_OK
) {
return;
}
win = GetSDLWindow(winID);
if(bSetFocus) {
SDL_SetKeyboardFocus(win);
} else if(SDL_GetKeyboardFocus() == win) {
/* Only lose all focus if this window was the current focus */
SDL_SetKeyboardFocus(NULL);
}
}
void _HandleWindowMoved(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 xPos, yPos;
/* Get the window id and new x/y position of the window */
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("window-x", &xPos) != B_OK ||
msg->FindInt32("window-y", &yPos) != B_OK
) {
return;
}
win = GetSDLWindow(winID);
SDL_SendWindowEvent(win, SDL_WINDOWEVENT_MOVED, xPos, yPos);
}
void _HandleWindowResized(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 w, h;
/* Get the window id ]and new x/y position of the window */
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("window-w", &w) != B_OK ||
msg->FindInt32("window-h", &h) != B_OK
) {
return;
}
win = GetSDLWindow(winID);
SDL_SendWindowEvent(win, SDL_WINDOWEVENT_RESIZED, w, h);
}
bool _GetWinID(BMessage *msg, int32 *winID) {
return msg->FindInt32("window-id", winID) == B_OK;
}
/* Vector functions: Wraps vector stuff in case we need to change
implementation */
void _SetSDLWindow(SDL_Window *win, int32 winID) {
_window_map[winID] = win;
}
int32 _GetNumWindowSlots() {
return _window_map.size();
}
void _PopBackWindow() {
_window_map.pop_back();
}
void _PushBackWindow(SDL_Window *win) {
_window_map.push_back(win);
}
/* Members */
std::vector<SDL_Window*> _window_map; /* Keeps track of SDL_Windows by index-id */
display_mode *_saved_mode;
BGLView *_current_context;
};
#endif
SDL_BApp::_GetWinID
bool _GetWinID(BMessage *msg, int32 *winID)
Definition: SDL_BApp.h:347
BAPP_HIDE
Definition: SDL_BApp.h:68
BAPP_SCREEN_CHANGED
Definition: SDL_BApp.h:74
BAPP_SHOW
Definition: SDL_BApp.h:67
BAPP_WINDOW_MOVED
Definition: SDL_BApp.h:72
button
SDL_Texture * button
Definition: testgamecontroller.c:67
SDL_BApp::_HandleMouseButton
void _HandleMouseButton(BMessage *msg)
Definition: SDL_BApp.h:230
SDL_SetKeyboardFocus
void SDL_SetKeyboardFocus(SDL_Window *window)
Definition: SDL_keyboard.c:612
SDL_BApp::MessageReceived
virtual void MessageReceived(BMessage *message)
Definition: SDL_BApp.h:94
SDL_WINDOWEVENT_MINIMIZED
Definition: SDL_video.h:149
BAPP_MOUSE_WHEEL
Definition: SDL_BApp.h:60
message
GLuint GLsizei const GLchar * message
Definition: SDL_opengl_glext.h:2483
BAPP_WINDOW_RESIZED
Definition: SDL_BApp.h:73
SDL_BApp::_HandleWindowMoved
void _HandleWindowMoved(BMessage *msg)
Definition: SDL_BApp.h:315
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
SDL_BApp::~SDL_BApp
virtual ~SDL_BApp()
Definition: SDL_BApp.h:88
SDL_MOUSEWHEEL_NORMAL
Definition: SDL_mouse.h:68
BAPP_MOUSE_BUTTON
Definition: SDL_BApp.h:59
state
struct xkb_state * state
Definition: SDL_waylandsym.h:94
BE_SetKeyState
void BE_SetKeyState(int32 bkey, int8 state)
SDL_WINDOWEVENT_MAXIMIZED
Definition: SDL_video.h:150
SDL_SendWindowEvent
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
Definition: SDL_windowevents.c:74
SDL_SetMouseFocus
void SDL_SetMouseFocus(SDL_Window *window)
Definition: SDL_mouse.c:103
SDL_GetKeyboardFocus
#define SDL_GetKeyboardFocus
Definition: SDL_dynapi_overrides.h:222
ToSDL
ToSDL
Definition: SDL_BApp.h:56
BAPP_MAXIMIZE
Definition: SDL_BApp.h:64
SDL_BApp::_HandleKeyboardFocus
void _HandleKeyboardFocus(BMessage *msg)
Definition: SDL_BApp.h:296
BAPP_KEY
Definition: SDL_BApp.h:61
SDL_BApp::_GetNumWindowSlots
int32 _GetNumWindowSlots()
Definition: SDL_BApp.h:359
SDL_WINDOWEVENT_HIDDEN
Definition: SDL_video.h:140
SDL_SendKeyboardKey
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
Definition: SDL_keyboard.c:661
SDL_BApp::_HandleMouseFocus
void _HandleMouseFocus(BMessage *msg)
Definition: SDL_BApp.h:277
BAPP_REPAINT
Definition: SDL_BApp.h:62
SDL_BApp::SetCurrentContext
void SetCurrentContext(BGLView *newContext)
Definition: SDL_BApp.h:192
SDL_BApp::_SetSDLWindow
void _SetSDLWindow(SDL_Window *win, int32 winID)
Definition: SDL_BApp.h:355
SDL_BApp
Definition: SDL_BApp.h:80
SDL_BApp::_HandleMouseMove
void _HandleMouseMove(BMessage *msg)
Definition: SDL_BApp.h:212
SDL_video.h
SDL_BApp::_window_map
std::vector< SDL_Window * > _window_map
Definition: SDL_BApp.h:374
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_SendMouseMotion
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
Definition: SDL_mouse.c:188
SDL_BApp::SDL_BApp
SDL_BApp(const char *signature)
Definition: SDL_BApp.h:82
BAPP_MOUSE_MOVED
Definition: SDL_BApp.h:58
BAPP_MOUSE_FOCUS
Definition: SDL_BApp.h:69
BE_GetScancodeFromBeKey
SDL_Scancode BE_GetScancodeFromBeKey(int32 bkey)
SDL_BApp::_HandleKey
void _HandleKey(BMessage *msg)
Definition: SDL_BApp.h:260
SDL_WINDOWEVENT_CLOSE
Definition: SDL_video.h:157
BE_GetKeyState
int8 BE_GetKeyState(int32 bkey)
BE_UpdateWindowFramebuffer
int BE_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
SDL_BApp::GetSDLWindow
SDL_Window * GetSDLWindow(int32 winID)
Definition: SDL_BApp.h:188
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:42
BAPP_KEYBOARD_FOCUS
Definition: SDL_BApp.h:70
SDL_BApp::GetID
int32 GetID(SDL_Window *win)
Definition: SDL_BApp.h:164
NULL
#define NULL
Definition: begin_code.h:143
SDL_BApp::_PushBackWindow
void _PushBackWindow(SDL_Window *win)
Definition: SDL_BApp.h:368
SDL_GetMouseFocus
#define SDL_GetMouseFocus
Definition: SDL_dynapi_overrides.h:251
SDL_BWin
Definition: SDL_BWin.h:63
SDL_BApp::ClearID
void ClearID(SDL_BWin *bwin)
SDL_WINDOWEVENT_SHOWN
Definition: SDL_video.h:139
BAPP_MINIMIZE
Definition: SDL_BApp.h:65
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:71
BAPP_WINDOW_CLOSE_REQUESTED
Definition: SDL_BApp.h:71
SDL_BApp::_saved_mode
display_mode * _saved_mode
Definition: SDL_BApp.h:376
SDL_WINDOWEVENT_EXPOSED
Definition: SDL_video.h:141
BAPP_RESTORE
Definition: SDL_BApp.h:66
SDL_BApp::_HandleBasicWindowEvent
void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType)
Definition: SDL_BApp.h:200
SDL_SendMouseWheel
int SDL_SendMouseWheel(SDL_Window *window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction)
Definition: SDL_mouse.c:406
SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:145
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_WINDOWEVENT_MOVED
Definition: SDL_video.h:143
SDL_SendMouseButton
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
Definition: SDL_mouse.c:326
SDL_BApp::_PopBackWindow
void _PopBackWindow()
Definition: SDL_BApp.h:364
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
SDL_BApp::_HandleWindowResized
void _HandleWindowResized(BMessage *msg)
Definition: SDL_BApp.h:331
SDL_BApp::_current_context
BGLView * _current_context
Definition: SDL_BApp.h:377
SDL_BApp::_HandleMouseWheel
void _HandleMouseWheel(BMessage *msg)
Definition: SDL_BApp.h:245