Atlas - SDL_bvideo.cc
Home / ext / SDL / src / video / haiku Lines: 1 | Size: 9591 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 22#include "SDL_internal.h" 23#include "../../core/haiku/SDL_BApp.h" 24 25#ifdef SDL_VIDEO_DRIVER_HAIKU 26 27#include "SDL_BWin.h" 28#include <Url.h> 29 30#ifdef __cplusplus 31extern "C" { 32#endif 33 34#include "SDL_bkeyboard.h" 35#include "SDL_bwindow.h" 36#include "SDL_bclipboard.h" 37#include "SDL_bvideo.h" 38#include "SDL_bopengl.h" 39#include "SDL_bmodes.h" 40#include "SDL_bframebuffer.h" 41#include "SDL_bevents.h" 42#include "SDL_bmessagebox.h" 43#include "../../events/SDL_keyboard_c.h" 44#include "../../events/SDL_mouse_c.h" 45 46static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) { 47 return (SDL_BWin *)(window->internal); 48} 49 50static SDL_VideoDevice * HAIKU_CreateDevice(void) 51{ 52 SDL_VideoDevice *device; 53 54 // Initialize all variables that we clean on shutdown 55 device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice)); 56 57 device->internal = NULL; /* FIXME: Is this the cause of some of the 58 SDL_Quit() errors? */ 59 60// TODO: Figure out if any initialization needs to go here 61 62 // Set the function pointers 63 device->VideoInit = HAIKU_VideoInit; 64 device->VideoQuit = HAIKU_VideoQuit; 65 device->GetDisplayBounds = HAIKU_GetDisplayBounds; 66 device->GetDisplayModes = HAIKU_GetDisplayModes; 67 device->SetDisplayMode = HAIKU_SetDisplayMode; 68 device->PumpEvents = HAIKU_PumpEvents; 69 70 device->CreateSDLWindow = HAIKU_CreateWindow; 71 device->SetWindowTitle = HAIKU_SetWindowTitle; 72 device->SetWindowPosition = HAIKU_SetWindowPosition; 73 device->SetWindowSize = HAIKU_SetWindowSize; 74 device->ShowWindow = HAIKU_ShowWindow; 75 device->HideWindow = HAIKU_HideWindow; 76 device->RaiseWindow = HAIKU_RaiseWindow; 77 device->MaximizeWindow = HAIKU_MaximizeWindow; 78 device->MinimizeWindow = HAIKU_MinimizeWindow; 79 device->RestoreWindow = HAIKU_RestoreWindow; 80 device->SetWindowBordered = HAIKU_SetWindowBordered; 81 device->SetWindowResizable = HAIKU_SetWindowResizable; 82 device->SetWindowFullscreen = HAIKU_SetWindowFullscreen; 83 device->SetWindowMouseGrab = HAIKU_SetWindowMouseGrab; 84 device->SetWindowMinimumSize = HAIKU_SetWindowMinimumSize; 85 device->SetWindowParent = HAIKU_SetWindowParent; 86 device->SetWindowModal = HAIKU_SetWindowModal; 87 device->DestroyWindow = HAIKU_DestroyWindow; 88 device->CreateWindowFramebuffer = HAIKU_CreateWindowFramebuffer; 89 device->UpdateWindowFramebuffer = HAIKU_UpdateWindowFramebuffer; 90 device->DestroyWindowFramebuffer = HAIKU_DestroyWindowFramebuffer; 91 92#ifdef SDL_VIDEO_OPENGL 93 device->GL_LoadLibrary = HAIKU_GL_LoadLibrary; 94 device->GL_GetProcAddress = HAIKU_GL_GetProcAddress; 95 device->GL_UnloadLibrary = HAIKU_GL_UnloadLibrary; 96 device->GL_CreateContext = HAIKU_GL_CreateContext; 97 device->GL_MakeCurrent = HAIKU_GL_MakeCurrent; 98 device->GL_SetSwapInterval = HAIKU_GL_SetSwapInterval; 99 device->GL_GetSwapInterval = HAIKU_GL_GetSwapInterval; 100 device->GL_SwapWindow = HAIKU_GL_SwapWindow; 101 device->GL_DestroyContext = HAIKU_GL_DestroyContext; 102#endif 103 104 device->SetClipboardText = HAIKU_SetClipboardText; 105 device->GetClipboardText = HAIKU_GetClipboardText; 106 device->HasClipboardText = HAIKU_HasClipboardText; 107 108 device->free = HAIKU_DeleteDevice; 109 110 return device; 111} 112 113VideoBootStrap HAIKU_bootstrap = { 114 "haiku", "Haiku graphics", 115 HAIKU_CreateDevice, 116 HAIKU_ShowMessageBox, 117 false 118}; 119 120void HAIKU_DeleteDevice(SDL_VideoDevice * device) 121{ 122 SDL_free(device->internal); 123 SDL_free(device); 124} 125 126struct SDL_CursorData 127{ 128 BCursor *cursor; 129}; 130 131static SDL_Cursor *HAIKU_CreateCursorAndData(BCursor *bcursor) 132{ 133 SDL_Cursor *cursor = (SDL_Cursor *)SDL_calloc(1, sizeof(*cursor)); 134 if (cursor) { 135 SDL_CursorData *data = (SDL_CursorData *)SDL_calloc(1, sizeof(*data)); 136 if (!data) { 137 SDL_free(cursor); 138 return NULL; 139 } 140 data->cursor = bcursor; 141 cursor->internal = data; 142 } 143 return cursor; 144} 145 146static SDL_Cursor * HAIKU_CreateSystemCursor(SDL_SystemCursor id) 147{ 148 BCursorID cursorId = B_CURSOR_ID_SYSTEM_DEFAULT; 149 150 switch(id) 151 { 152 #define CURSORCASE(sdlname, bname) case SDL_SYSTEM_CURSOR_##sdlname: cursorId = B_CURSOR_ID_##bname; break 153 CURSORCASE(DEFAULT, SYSTEM_DEFAULT); 154 CURSORCASE(TEXT, I_BEAM); 155 CURSORCASE(WAIT, PROGRESS); 156 CURSORCASE(CROSSHAIR, CROSS_HAIR); 157 CURSORCASE(PROGRESS, PROGRESS); 158 CURSORCASE(NWSE_RESIZE, RESIZE_NORTH_WEST_SOUTH_EAST); 159 CURSORCASE(NESW_RESIZE, RESIZE_NORTH_EAST_SOUTH_WEST); 160 CURSORCASE(EW_RESIZE, RESIZE_EAST_WEST); 161 CURSORCASE(NS_RESIZE, RESIZE_NORTH_SOUTH); 162 CURSORCASE(MOVE, MOVE); 163 CURSORCASE(NOT_ALLOWED, NOT_ALLOWED); 164 CURSORCASE(POINTER, FOLLOW_LINK); 165 CURSORCASE(NW_RESIZE, RESIZE_NORTH_WEST_SOUTH_EAST); 166 CURSORCASE(N_RESIZE, RESIZE_NORTH_SOUTH); 167 CURSORCASE(NE_RESIZE, RESIZE_NORTH_EAST_SOUTH_WEST); 168 CURSORCASE(E_RESIZE, RESIZE_EAST_WEST); 169 CURSORCASE(SE_RESIZE, RESIZE_NORTH_WEST_SOUTH_EAST); 170 CURSORCASE(S_RESIZE, RESIZE_NORTH_SOUTH); 171 CURSORCASE(SW_RESIZE, RESIZE_NORTH_EAST_SOUTH_WEST); 172 CURSORCASE(W_RESIZE, RESIZE_EAST_WEST); 173 #undef CURSORCASE 174 default: 175 SDL_assert(0); 176 return NULL; 177 } 178 179 return HAIKU_CreateCursorAndData(new BCursor(cursorId)); 180} 181 182static SDL_Cursor * HAIKU_CreateDefaultCursor() 183{ 184 SDL_SystemCursor id = SDL_GetDefaultSystemCursor(); 185 return HAIKU_CreateSystemCursor(id); 186} 187 188static void HAIKU_FreeCursor(SDL_Cursor * cursor) 189{ 190 SDL_CursorData *data = cursor->internal; 191 192 if (data) { 193 delete data->cursor; 194 } 195 SDL_free(data); 196 SDL_free(cursor); 197} 198 199static SDL_Cursor * HAIKU_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y) 200{ 201 SDL_Surface *converted; 202 203 converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ARGB8888); 204 if (!converted) { 205 return NULL; 206 } 207 208 BBitmap *cursorBitmap = new BBitmap(BRect(0, 0, surface->w - 1, surface->h - 1), B_RGBA32); 209 cursorBitmap->SetBits(converted->pixels, converted->h * converted->pitch, 0, B_RGBA32); 210 SDL_DestroySurface(converted); 211 212 return HAIKU_CreateCursorAndData(new BCursor(cursorBitmap, BPoint(hot_x, hot_y))); 213} 214 215static bool HAIKU_ShowCursor(SDL_Cursor *cursor) 216{ 217 SDL_Mouse *mouse = SDL_GetMouse(); 218 219 if (!mouse) { 220 return true; 221 } 222 223 if (cursor) { 224 BCursor *hCursor = cursor->internal->cursor; 225 be_app->SetCursor(hCursor); 226 } else { 227 BCursor *hCursor = new BCursor(B_CURSOR_ID_NO_CURSOR); 228 be_app->SetCursor(hCursor); 229 delete hCursor; 230 } 231 232 return true; 233} 234 235static bool HAIKU_SetRelativeMouseMode(bool enabled) 236{ 237 SDL_Window *window = SDL_GetMouseFocus(); 238 if (!window) { 239 return true; 240 } 241 242 SDL_BWin *bewin = _ToBeWin(window); 243 BView *_SDL_View = bewin->GetGLView(); 244 if (!_SDL_View) { 245 _SDL_View = bewin->GetView(); 246 if (!_SDL_View) 247 return false; 248 } 249 250 bewin->Lock(); 251 if (enabled) 252 _SDL_View->SetEventMask(B_POINTER_EVENTS, B_NO_POINTER_HISTORY); 253 else 254 _SDL_View->SetEventMask(0, 0); 255 bewin->Unlock(); 256 257 return true; 258} 259 260static void HAIKU_MouseInit(SDL_VideoDevice *_this) 261{ 262 SDL_Mouse *mouse = SDL_GetMouse(); 263 if (!mouse) { 264 return; 265 } 266 mouse->CreateCursor = HAIKU_CreateCursor; 267 mouse->CreateSystemCursor = HAIKU_CreateSystemCursor; 268 mouse->ShowCursor = HAIKU_ShowCursor; 269 mouse->FreeCursor = HAIKU_FreeCursor; 270 mouse->SetRelativeMouseMode = HAIKU_SetRelativeMouseMode; 271 272 SDL_SetDefaultCursor(HAIKU_CreateDefaultCursor()); 273} 274 275bool HAIKU_VideoInit(SDL_VideoDevice *_this) 276{ 277 // Initialize the Be Application for appserver interaction 278 if (!SDL_InitBeApp()) { 279 return false; 280 } 281 282 // Initialize video modes 283 HAIKU_InitModes(_this); 284 285 // Init the keymap 286 HAIKU_InitOSKeymap(); 287 288 HAIKU_MouseInit(_this); 289 290 // Assume we have a mouse and keyboard 291 SDL_AddKeyboard(SDL_DEFAULT_KEYBOARD_ID, NULL); 292 SDL_AddMouse(SDL_DEFAULT_MOUSE_ID, NULL); 293 294#ifdef SDL_VIDEO_OPENGL 295 // testgl application doesn't load library, just tries to load symbols 296 // is it correct? if so we have to load library here 297 HAIKU_GL_LoadLibrary(_this, NULL); 298#endif 299 300 // We're done! 301 return true; 302} 303 304void HAIKU_VideoQuit(SDL_VideoDevice *_this) 305{ 306 307 HAIKU_QuitModes(_this); 308 309 SDL_QuitBeApp(); 310} 311 312// just sticking this function in here so it's in a C++ source file. 313extern "C" 314bool HAIKU_OpenURL(const char *url) 315{ 316 BUrl burl(url); 317 const status_t rc = burl.OpenWithPreferredApplication(false); 318 if (rc != B_NO_ERROR) { 319 return SDL_SetError("URL open failed (err=%d)", (int)rc); 320 } 321 return true; 322} 323 324#ifdef __cplusplus 325} 326#endif 327 328#endif // SDL_VIDEO_DRIVER_HAIKU 329[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.