zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_rpivideo.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2013 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 
22 #include "SDL_config.h"
23 
24 #if SDL_VIDEO_DRIVER_RPI
25 
26 /* References
27  * http://elinux.org/RPi_VideoCore_APIs
28  * https://github.com/raspberrypi/firmware/blob/master/opt/vc/src/hello_pi/hello_triangle/triangle.c
29  * http://cgit.freedesktop.org/wayland/weston/tree/src/rpi-renderer.c
30  * http://cgit.freedesktop.org/wayland/weston/tree/src/compositor-rpi.c
31  */
32 
33 /* SDL internals */
34 #include "../SDL_sysvideo.h"
35 #include "SDL_version.h"
36 #include "SDL_syswm.h"
37 #include "SDL_loadso.h"
38 #include "SDL_events.h"
39 #include "../../events/SDL_mouse_c.h"
40 #include "../../events/SDL_keyboard_c.h"
41 
42 #ifdef SDL_INPUT_LINUXEV
43 #include "../../input/evdev/SDL_evdev.h"
44 #endif
45 
46 /* RPI declarations */
47 #include "SDL_rpivideo.h"
48 #include "SDL_rpievents_c.h"
49 #include "SDL_rpiopengles.h"
50 #include "SDL_rpimouse.h"
51 
52 static int
53 RPI_Available(void)
54 {
55  return 1;
56 }
57 
58 static void
59 RPI_Destroy(SDL_VideoDevice * device)
60 {
61  /* SDL_VideoData *phdata = (SDL_VideoData *) device->driverdata; */
62 
63  if (device->driverdata != NULL) {
64  device->driverdata = NULL;
65  }
66 }
67 
68 static SDL_VideoDevice *
69 RPI_Create()
70 {
71  SDL_VideoDevice *device;
72  SDL_VideoData *phdata;
73 
74  /* Initialize SDL_VideoDevice structure */
75  device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
76  if (device == NULL) {
78  return NULL;
79  }
80 
81  /* Initialize internal data */
82  phdata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
83  if (phdata == NULL) {
85  SDL_free(device);
86  return NULL;
87  }
88 
89  device->driverdata = phdata;
90 
91  /* Setup amount of available displays and current display */
92  device->num_displays = 0;
93 
94  /* Set device free function */
95  device->free = RPI_Destroy;
96 
97  /* Setup all functions which we can handle */
98  device->VideoInit = RPI_VideoInit;
99  device->VideoQuit = RPI_VideoQuit;
102  device->CreateWindow = RPI_CreateWindow;
108  device->ShowWindow = RPI_ShowWindow;
109  device->HideWindow = RPI_HideWindow;
110  device->RaiseWindow = RPI_RaiseWindow;
126 
127  device->PumpEvents = RPI_PumpEvents;
128 
129  return device;
130 }
131 
132 VideoBootStrap RPI_bootstrap = {
133  "RPI",
134  "RPI Video Driver",
135  RPI_Available,
136  RPI_Create
137 };
138 
139 /*****************************************************************************/
140 /* SDL Video and Display initialization/handling functions */
141 /*****************************************************************************/
142 int
144 {
146  SDL_DisplayMode current_mode;
147  uint32_t w,h;
148 
149  /* Initialize BCM Host */
150  bcm_host_init();
151 
152  SDL_zero(current_mode);
153 
154  if (graphics_get_display_size( 0, &w, &h) < 0) {
155  return -1;
156  }
157 
158  current_mode.w = w;
159  current_mode.h = h;
160  /* FIXME: Is there a way to tell the actual refresh rate? */
161  current_mode.refresh_rate = 60;
162  /* 32 bpp for default */
163  current_mode.format = SDL_PIXELFORMAT_ABGR8888;
164 
165  current_mode.driverdata = NULL;
166 
167  SDL_zero(display);
168  display.desktop_mode = current_mode;
169  display.current_mode = current_mode;
170 
172 
173  /* Allocate display internal data */
174  data = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
175  if (data == NULL) {
176  return SDL_OutOfMemory();
177  }
178 
179  data->dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
180 
181  display.driverdata = data;
182 
183  SDL_AddVideoDisplay(&display);
184 
185 #ifdef SDL_INPUT_LINUXEV
186  SDL_EVDEV_Init();
187 #endif
188 
190 
191  return 1;
192 }
193 
194 void
196 {
197 #ifdef SDL_INPUT_LINUXEV
198  SDL_EVDEV_Quit();
199 #endif
200 }
201 
202 void
204 {
205  /* Only one display mode available, the current one */
206  SDL_AddDisplayMode(display, &display->current_mode);
207 }
208 
209 int
211 {
212  return 0;
213 }
214 
215 int
217 {
218  SDL_WindowData *wdata;
220  SDL_DisplayData *displaydata;
221  VC_RECT_T dst_rect;
222  VC_RECT_T src_rect;
223  VC_DISPMANX_ALPHA_T dispman_alpha;
224  DISPMANX_UPDATE_HANDLE_T dispman_update;
225 
226  /* Disable alpha, otherwise the app looks composed with whatever dispman is showing (X11, console,etc) */
227  dispman_alpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS;
228  dispman_alpha.opacity = 0xFF;
229  dispman_alpha.mask = 0;
230 
231  /* Allocate window internal data */
232  wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
233  if (wdata == NULL) {
234  return SDL_OutOfMemory();
235  }
236  display = SDL_GetDisplayForWindow(window);
237  displaydata = (SDL_DisplayData *) display->driverdata;
238 
239  /* Windows have one size for now */
240  window->w = display->desktop_mode.w;
241  window->h = display->desktop_mode.h;
242 
243  /* OpenGL ES is the law here, buddy */
244  window->flags |= SDL_WINDOW_OPENGL;
245 
246  /* Create a dispman element and associate a window to it */
247  dst_rect.x = 0;
248  dst_rect.y = 0;
249  dst_rect.width = window->w;
250  dst_rect.height = window->h;
251 
252  src_rect.x = 0;
253  src_rect.y = 0;
254  src_rect.width = window->w << 16;
255  src_rect.height = window->h << 16;
256 
257  dispman_update = vc_dispmanx_update_start( 0 );
258  wdata->dispman_window.element = vc_dispmanx_element_add ( dispman_update, displaydata->dispman_display, SDL_RPI_VIDEOLAYER /* layer */, &dst_rect, 0/*src*/, &src_rect, DISPMANX_PROTECTION_NONE, &dispman_alpha /*alpha*/, 0/*clamp*/, 0/*transform*/);
259  wdata->dispman_window.width = window->w;
260  wdata->dispman_window.height = window->h;
261  vc_dispmanx_update_submit_sync( dispman_update );
262 
263  if (!_this->egl_data) {
264  if (SDL_GL_LoadLibrary(NULL) < 0) {
265  return -1;
266  }
267  }
268  wdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) &wdata->dispman_window);
269 
270  if (wdata->egl_surface == EGL_NO_SURFACE) {
271  return SDL_SetError("Could not create GLES window surface");
272  }
273 
274  /* Setup driver data for this window */
275  window->driverdata = wdata;
276 
277  /* One window, it always has focus */
278  SDL_SetMouseFocus(window);
279  SDL_SetKeyboardFocus(window);
280 
281  /* Window has been successfully created */
282  return 0;
283 }
284 
285 int
286 RPI_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
287 {
288  return -1;
289 }
290 
291 void
293 {
294 }
295 void
297 {
298 }
299 void
301 {
302 }
303 void
305 {
306 }
307 void
309 {
310 }
311 void
313 {
314 }
315 void
317 {
318 }
319 void
321 {
322 }
323 void
325 {
326 }
327 void
329 {
330 }
331 void
332 RPI_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
333 {
334 
335 }
336 void
338 {
339 }
340 
341 /*****************************************************************************/
342 /* SDL Window Manager function */
343 /*****************************************************************************/
344 SDL_bool
345 RPI_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
346 {
347  if (info->version.major <= SDL_MAJOR_VERSION) {
348  return SDL_TRUE;
349  } else {
350  SDL_SetError("application not compiled with SDL %d.%d\n",
352  return SDL_FALSE;
353  }
354 
355  /* Failed to get window manager information */
356  return SDL_FALSE;
357 }
358 
359 #endif /* SDL_VIDEO_DRIVER_RPI */
360 
361 /* vi: set ts=4 sw=4 expandtab: */
void(* GetDisplayModes)(_THIS, SDL_VideoDisplay *display)
Definition: SDL_sysvideo.h:169
void(* MinimizeWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:195
#define SDL_RPI_VIDEOLAYER
Definition: SDL_rpivideo.h:53
#define SDL_MINOR_VERSION
Definition: SDL_version.h:61
SDL_bool(* GetWindowWMInfo)(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
Definition: SDL_sysvideo.h:215
void RPI_DestroyWindow(_THIS, SDL_Window *window)
void(* RaiseWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:193
void RPI_RestoreWindow(_THIS, SDL_Window *window)
void * RPI_GLES_GetProcAddress(_THIS, const char *proc)
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:7294
void SDL_SetKeyboardFocus(SDL_Window *window)
Definition: SDL_keyboard.c:611
DECLSPEC void *SDLCALL SDL_calloc(size_t nmemb, size_t size)
int RPI_CreateWindowFrom(_THIS, SDL_Window *window, const void *data)
#define NULL
Definition: ftobjs.h:61
void RPI_SetWindowTitle(_THIS, SDL_Window *window)
#define EGL_NO_SURFACE
Definition: egl.h:71
void RPI_SetWindowGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
void(* HideWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:192
int RPI_GLES_LoadLibrary(_THIS, const char *path)
int(* GL_MakeCurrent)(_THIS, SDL_Window *window, SDL_GLContext context)
Definition: SDL_sysvideo.h:226
#define SDL_MAJOR_VERSION
Definition: SDL_version.h:60
SDL_bool
Definition: SDL_stdinc.h:116
void RPI_GLES_UnloadLibrary(_THIS)
void RPI_GetDisplayModes(_THIS, SDL_VideoDisplay *display)
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
int RPI_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
DECLSPEC void SDLCALL SDL_free(void *mem)
void(* SetWindowGrab)(_THIS, SDL_Window *window, SDL_bool grabbed)
Definition: SDL_sysvideo.h:201
The structure that defines a display mode.
Definition: SDL_video.h:53
SDL_version version
Definition: SDL_syswm.h:161
Uint8 major
Definition: SDL_version.h:53
void(* SetWindowIcon)(_THIS, SDL_Window *window, SDL_Surface *icon)
Definition: SDL_sysvideo.h:186
void RPI_RaiseWindow(_THIS, SDL_Window *window)
int RPI_GLES_GetSwapInterval(_THIS)
void RPI_MaximizeWindow(_THIS, SDL_Window *window)
void SDL_SetMouseFocus(SDL_Window *window)
Definition: SDL_mouse.c:93
char * display
Definition: visualinfo.c:85
int(* CreateWindowFrom)(_THIS, SDL_Window *window, const void *data)
Definition: SDL_sysvideo.h:184
int SDL_AddVideoDisplay(const SDL_VideoDisplay *display)
Definition: SDL_video.c:568
void(* free)(_THIS)
Definition: SDL_sysvideo.h:327
int RPI_VideoInit(_THIS)
void RPI_SetWindowIcon(_THIS, SDL_Window *window, SDL_Surface *icon)
int(* GL_SetSwapInterval)(_THIS, int interval)
Definition: SDL_sysvideo.h:228
static SDL_VideoDevice * _this
Definition: SDL_video.c:92
EGLNativeWindowType NativeWindowType
Definition: eglplatform.h:117
int(* CreateWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:183
void(* GL_UnloadLibrary)(_THIS)
Definition: SDL_sysvideo.h:224
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
int(* VideoInit)(_THIS)
Definition: SDL_sysvideo.h:148
#define _THIS
void RPI_PumpEvents(_THIS)
EGL_DISPMANX_WINDOW_T dispman_window
Definition: SDL_rpivideo.h:47
void * driverdata
Definition: SDL_video.h:59
void RPI_HideWindow(_THIS, SDL_Window *window)
void RPI_SetWindowSize(_THIS, SDL_Window *window)
SDL_DisplayMode current_mode
Definition: SDL_sysvideo.h:120
void RPI_GLES_DeleteContext(_THIS, SDL_GLContext context)
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
Definition: SDL_error.c:53
DISPMANX_DISPLAY_HANDLE_T dispman_display
Definition: SDL_rpivideo.h:41
void(* SetWindowPosition)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:187
void(* PumpEvents)(_THIS)
Definition: SDL_sysvideo.h:237
void RPI_VideoQuit(_THIS)
int(* GL_GetSwapInterval)(_THIS)
Definition: SDL_sysvideo.h:229
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
void RPI_GLES_SwapWindow(_THIS, SDL_Window *window)
SDL_DisplayMode desktop_mode
Definition: SDL_sysvideo.h:119
unsigned int uint32_t
void(* GL_DeleteContext)(_THIS, SDL_GLContext context)
Definition: SDL_sysvideo.h:231
void(* MaximizeWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:194
void RPI_ShowWindow(_THIS, SDL_Window *window)
void RPI_MinimizeWindow(_THIS, SDL_Window *window)
void(* DestroyWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:202
int RPI_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
void(* SetWindowSize)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:188
int(* SetDisplayMode)(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
Definition: SDL_sysvideo.h:177
SDL_VideoDisplay * SDL_GetDisplayForWindow(SDL_Window *window)
Definition: SDL_video.c:996
SDL_GLContext RPI_GLES_CreateContext(_THIS, SDL_Window *window)
SDL_GLContext(* GL_CreateContext)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:225
DECLSPEC int SDLCALL SDL_GL_LoadLibrary(const char *path)
Dynamically load an OpenGL library.
Definition: SDL_video.c:2315
int(* GL_LoadLibrary)(_THIS, const char *path)
Definition: SDL_sysvideo.h:222
void(* GL_SwapWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:230
void(* VideoQuit)(_THIS)
Definition: SDL_sysvideo.h:154
void(* ShowWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:191
int RPI_CreateWindow(_THIS, SDL_Window *window)
SDL_bool SDL_AddDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
Definition: SDL_video.c:666
GLint GLint GLint GLint GLint w
Definition: gl2ext.h:1215
#define SDL_zero(x)
Definition: SDL_stdinc.h:254
void * driverdata
Definition: SDL_sysvideo.h:99
void(* SetWindowTitle)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:185
Uint32 format
Definition: SDL_video.h:55
Uint32 flags
Definition: SDL_sysvideo.h:81
SDL_bool RPI_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
void RPI_SetWindowPosition(_THIS, SDL_Window *window)
void *(* GL_GetProcAddress)(_THIS, const char *proc)
Definition: SDL_sysvideo.h:223
void RPI_InitMouse(_THIS)
GLenum mode
Definition: glew.h:2394
EGLSurface egl_surface
void(* RestoreWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:196
int RPI_GLES_SetSwapInterval(_THIS, int interval)