| 1 |
#include <stdio.h> |
| 2 |
|
| 3 |
#include "allegro5/allegro.h" |
| 4 |
#include "allegro5/allegro_opengl.h" |
| 5 |
#include "allegro5/internal/aintern_opengl.h" |
| 6 |
#include "allegro5/internal/aintern_vector.h" |
| 7 |
#include "allegro5/internal/aintern_raspberrypi.h" |
| 8 |
#include "allegro5/internal/aintern_x.h" |
| 9 |
#include "allegro5/internal/aintern_xwindow.h" |
| 10 |
|
| 11 |
#include <GLES/gl.h> |
| 12 |
#include <EGL/egl.h> |
| 13 |
#include <EGL/eglext.h> |
| 14 |
|
| 15 |
#include <bcm_host.h> |
| 16 |
|
| 17 |
#include "picursor.h" |
| 18 |
#define DEFAULT_CURSOR_WIDTH 17 |
| 19 |
#define DEFAULT_CURSOR_HEIGHT 28 |
| 20 |
|
| 21 |
static ALLEGRO_DISPLAY_INTERFACE *vt; |
| 22 |
|
| 23 |
static EGLDisplay egl_display; |
| 24 |
static EGLSurface egl_window; |
| 25 |
static EGLContext egl_context; |
| 26 |
static DISPMANX_UPDATE_HANDLE_T dispman_update; |
| 27 |
static DISPMANX_RESOURCE_HANDLE_T cursor_resource; |
| 28 |
static DISPMANX_DISPLAY_HANDLE_T dispman_display; |
| 29 |
static DISPMANX_ELEMENT_HANDLE_T cursor_element; |
| 30 |
static VC_RECT_T dst_rect; |
| 31 |
static VC_RECT_T src_rect; |
| 32 |
static bool cursor_added = false; |
| 33 |
static float mouse_scale_ratio_x = 1.0f, mouse_scale_ratio_y = 1.0f; |
| 34 |
|
| 35 |
struct ALLEGRO_DISPLAY_RASPBERRYPI_EXTRA { |
| 36 |
}; |
| 37 |
|
| 38 |
static int pot(int n) |
| 39 |
{ |
| 40 |
int i = 1; |
| 41 |
while (i < n) { |
| 42 |
i = i * 2; |
| 43 |
} |
| 44 |
return i; |
| 45 |
} |
| 46 |
|
| 47 |
static void set_cursor_data(ALLEGRO_DISPLAY_RASPBERRYPI *d, uint32_t *data, int width, int height) |
| 48 |
{ |
| 49 |
al_free(d->cursor_data); |
| 50 |
int spitch = sizeof(uint32_t) * width; |
| 51 |
int dpitch = pot(spitch); |
| 52 |
d->cursor_data = al_malloc(dpitch * height); |
| 53 |
int y; |
| 54 |
for (y = 0; y < height; y++) { |
| 55 |
uint8_t *p1 = (uint8_t *)d->cursor_data + y * dpitch; |
| 56 |
uint8_t *p2 = (uint8_t *)data + y * spitch; |
| 57 |
memcpy(p1, p2, spitch); |
| 58 |
} |
| 59 |
d->cursor_width = width; |
| 60 |
d->cursor_height = height; |
| 61 |
} |
| 62 |
|
| 63 |
static void delete_cursor_data(ALLEGRO_DISPLAY_RASPBERRYPI *d) |
| 64 |
{ |
| 65 |
al_free(d->cursor_data); |
| 66 |
d->cursor_data = NULL; |
| 67 |
} |
| 68 |
|
| 69 |
static void show_cursor(ALLEGRO_DISPLAY_RASPBERRYPI *d) |
| 70 |
{ |
| 71 |
if (d->cursor_data == NULL) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
int width = d->cursor_width; |
| 76 |
int height = d->cursor_height; |
| 77 |
|
| 78 |
if (cursor_added == false) { |
| 79 |
uint32_t unused; |
| 80 |
cursor_resource = vc_dispmanx_resource_create(VC_IMAGE_ARGB8888, width, height, &unused); |
| 81 |
|
| 82 |
VC_RECT_T r; |
| 83 |
r.x = 0; |
| 84 |
r.y = 0; |
| 85 |
r.width = width; |
| 86 |
r.height = height; |
| 87 |
|
| 88 |
int dpitch = pot(sizeof(uint32_t) * width); |
| 89 |
|
| 90 |
dispman_update = vc_dispmanx_update_start(0); |
| 91 |
vc_dispmanx_resource_write_data(cursor_resource, VC_IMAGE_ARGB8888, dpitch, d->cursor_data, &r); |
| 92 |
vc_dispmanx_update_submit_sync(dispman_update); |
| 93 |
|
| 94 |
ALLEGRO_MOUSE_STATE state; |
| 95 |
al_get_mouse_state(&state); |
| 96 |
|
| 97 |
dst_rect.x = state.x+d->cursor_offset_x; |
| 98 |
dst_rect.y = state.y+d->cursor_offset_y; |
| 99 |
dst_rect.width = width; |
| 100 |
dst_rect.height = height; |
| 101 |
src_rect.x = 0; |
| 102 |
src_rect.y = 0; |
| 103 |
src_rect.width = width << 16; |
| 104 |
src_rect.height = height << 16; |
| 105 |
|
| 106 |
dispman_update = vc_dispmanx_update_start(0); |
| 107 |
|
| 108 |
cursor_element = vc_dispmanx_element_add( |
| 109 |
dispman_update, |
| 110 |
dispman_display, |
| 111 |
0/*layer*/, |
| 112 |
&dst_rect, |
| 113 |
cursor_resource, |
| 114 |
&src_rect, |
| 115 |
DISPMANX_PROTECTION_NONE, |
| 116 |
0 /*alpha*/, |
| 117 |
0/*clamp*/, |
| 118 |
0/*transform*/ |
| 119 |
); |
| 120 |
|
| 121 |
vc_dispmanx_update_submit_sync(dispman_update); |
| 122 |
|
| 123 |
cursor_added = true; |
| 124 |
} |
| 125 |
else { |
| 126 |
ALLEGRO_DISPLAY *disp = (ALLEGRO_DISPLAY *)d; |
| 127 |
VC_RECT_T src, dst; |
| 128 |
src.x = 0; |
| 129 |
src.y = 0; |
| 130 |
src.width = d->cursor_width << 16; |
| 131 |
src.height = d->cursor_height << 16; |
| 132 |
ALLEGRO_MOUSE_STATE st; |
| 133 |
al_get_mouse_state(&st); |
| 134 |
st.x = (st.x+0.5) * d->screen_width / disp->w; |
| 135 |
st.y = (st.y+0.5) * d->screen_height / disp->h; |
| 136 |
dst.x = st.x+d->cursor_offset_x; |
| 137 |
dst.y = st.y+d->cursor_offset_y; |
| 138 |
dst.width = d->cursor_width; |
| 139 |
dst.height = d->cursor_height; |
| 140 |
dispman_update = vc_dispmanx_update_start(0); |
| 141 |
vc_dispmanx_element_change_attributes( |
| 142 |
dispman_update, |
| 143 |
cursor_element, |
| 144 |
0, |
| 145 |
0, |
| 146 |
0, |
| 147 |
&dst, |
| 148 |
&src, |
| 149 |
0, |
| 150 |
0 |
| 151 |
); |
| 152 |
vc_dispmanx_update_submit_sync(dispman_update); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
static void hide_cursor(ALLEGRO_DISPLAY_RASPBERRYPI *d) |
| 157 |
{ |
| 158 |
(void)d; |
| 159 |
|
| 160 |
if (!cursor_added) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
dispman_update = vc_dispmanx_update_start(0); |
| 165 |
vc_dispmanx_element_remove(dispman_update, cursor_element); |
| 166 |
vc_dispmanx_update_submit_sync(dispman_update); |
| 167 |
vc_dispmanx_resource_delete(cursor_resource); |
| 168 |
cursor_added = false; |
| 169 |
} |
| 170 |
|
| 171 |
/* Helper to set up GL state as we want it. */ |
| 172 |
static void setup_gl(ALLEGRO_DISPLAY *d) |
| 173 |
{ |
| 174 |
ALLEGRO_OGL_EXTRAS *ogl = d->ogl_extras; |
| 175 |
|
| 176 |
if (ogl->backbuffer) |
| 177 |
_al_ogl_resize_backbuffer(ogl->backbuffer, d->w, d->h); |
| 178 |
else |
| 179 |
ogl->backbuffer = _al_ogl_create_backbuffer(d); |
| 180 |
} |
| 181 |
|
| 182 |
void _al_raspberrypi_get_screen_info(int *dx, int *dy, |
| 183 |
int *screen_width, int *screen_height) |
| 184 |
{ |
| 185 |
graphics_get_display_size(0 /* LCD */, (uint32_t *)screen_width, (uint32_t *)screen_height); |
| 186 |
|
| 187 |
/* On TV-out the visible area (area used by X and console) |
| 188 |
* is different from that reported by the bcm functions. We |
| 189 |
* have to 1) read fbwidth and fbheight from /proc/cmdline |
| 190 |
* and also overscan parameters from /boot/config.txt so our |
| 191 |
* displays are the same size and overlap perfectly. |
| 192 |
*/ |
| 193 |
*dx = 0; |
| 194 |
*dy = 0; |
| 195 |
FILE *cmdline = fopen("/proc/cmdline", "r"); |
| 196 |
if (cmdline) { |
| 197 |
char line[1000]; |
| 198 |
int i; |
| 199 |
for (i = 0; i < 999; i++) { |
| 200 |
int c = fgetc(cmdline); |
| 201 |
if (c == EOF) break; |
| 202 |
line[i] = c; |
| 203 |
} |
| 204 |
line[i] = 0; |
| 205 |
const char *p = strstr(line, "fbwidth="); |
| 206 |
if (p) { |
| 207 |
const char *p2 = strstr(line, "fbheight="); |
| 208 |
if (p2) { |
| 209 |
p += strlen("fbwidth="); |
| 210 |
p2 += strlen("fbheight="); |
| 211 |
int w = atoi(p); |
| 212 |
int h = atoi(p2); |
| 213 |
const ALLEGRO_FILE_INTERFACE *file_interface = al_get_new_file_interface(); |
| 214 |
al_set_standard_file_interface(); |
| 215 |
ALLEGRO_CONFIG *cfg = al_load_config_file("/boot/config.txt"); |
| 216 |
if (cfg) { |
| 217 |
const char *disable_overscan = |
| 218 |
al_get_config_value( |
| 219 |
cfg, "", "disable_overscan" |
| 220 |
); |
| 221 |
// If overscan parameters are disabled don't process |
| 222 |
if (!disable_overscan || |
| 223 |
(disable_overscan && |
| 224 |
(!strcasecmp(disable_overscan, "false") || |
| 225 |
atoi(disable_overscan) == 0))) { |
| 226 |
const char *left = al_get_config_value( |
| 227 |
cfg, "", "overscan_left"); |
| 228 |
const char *right = al_get_config_value( |
| 229 |
cfg, "", "overscan_right"); |
| 230 |
const char *top = al_get_config_value( |
| 231 |
cfg, "", "overscan_top"); |
| 232 |
const char *bottom = al_get_config_value( |
| 233 |
cfg, "", "overscan_bottom"); |
| 234 |
int xx = left ? atoi(left) : 0; |
| 235 |
xx -= right ? atoi(right) : 0; |
| 236 |
int yy = top ? atoi(top) : 0; |
| 237 |
yy -= bottom ? atoi(bottom) : 0; |
| 238 |
*dx = (*screen_width - w + xx) / 2; |
| 239 |
*dy = (*screen_height - h + yy) / 2; |
| 240 |
*screen_width = w; |
| 241 |
*screen_height = h; |
| 242 |
} |
| 243 |
else { |
| 244 |
*dx = (*screen_width - w) / 2; |
| 245 |
*dy = (*screen_height - h) / 2; |
| 246 |
*screen_width = w; |
| 247 |
*screen_height = h; |
| 248 |
} |
| 249 |
al_destroy_config(cfg); |
| 250 |
} |
| 251 |
else { |
| 252 |
printf("Couldn't open /boot/config.txt\n"); |
| 253 |
} |
| 254 |
al_set_new_file_interface(file_interface); |
| 255 |
} |
| 256 |
} |
| 257 |
fclose(cmdline); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
void _al_raspberrypi_get_mouse_scale_ratios(float *x, float *y) |
| 262 |
{ |
| 263 |
*x = mouse_scale_ratio_x; |
| 264 |
*y = mouse_scale_ratio_y; |
| 265 |
} |
| 266 |
|
| 267 |
static bool pi_create_display(ALLEGRO_DISPLAY *display) |
| 268 |
{ |
| 269 |
ALLEGRO_DISPLAY_RASPBERRYPI *d = (void *)display; |
| 270 |
ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds = _al_get_new_display_settings(); |
| 271 |
|
| 272 |
egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 273 |
if (egl_display == EGL_NO_DISPLAY) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
int major, minor; |
| 278 |
if (!eglInitialize(egl_display, &major, &minor)) { |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
static EGLint attrib_list[] = |
| 283 |
{ |
| 284 |
EGL_DEPTH_SIZE, 0, |
| 285 |
EGL_STENCIL_SIZE, 0, |
| 286 |
EGL_RED_SIZE, 8, |
| 287 |
EGL_GREEN_SIZE, 8, |
| 288 |
EGL_BLUE_SIZE, 8, |
| 289 |
EGL_ALPHA_SIZE, 8, |
| 290 |
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 291 |
EGL_NONE |
| 292 |
}; |
| 293 |
|
| 294 |
attrib_list[1] = eds->settings[ALLEGRO_DEPTH_SIZE]; |
| 295 |
attrib_list[3] = eds->settings[ALLEGRO_STENCIL_SIZE]; |
| 296 |
|
| 297 |
if (eds->settings[ALLEGRO_RED_SIZE] || eds->settings[ALLEGRO_GREEN_SIZE] || |
| 298 |
eds->settings[ALLEGRO_BLUE_SIZE] || |
| 299 |
eds->settings[ALLEGRO_ALPHA_SIZE]) { |
| 300 |
attrib_list[5] = eds->settings[ALLEGRO_RED_SIZE]; |
| 301 |
attrib_list[7] = eds->settings[ALLEGRO_GREEN_SIZE]; |
| 302 |
attrib_list[9] = eds->settings[ALLEGRO_BLUE_SIZE]; |
| 303 |
attrib_list[11] = eds->settings[ALLEGRO_ALPHA_SIZE]; |
| 304 |
} |
| 305 |
else if (eds->settings[ALLEGRO_COLOR_SIZE] == 16) { |
| 306 |
attrib_list[5] = 5; |
| 307 |
attrib_list[7] = 6; |
| 308 |
attrib_list[9] = 5; |
| 309 |
attrib_list[11] = 0; |
| 310 |
} |
| 311 |
|
| 312 |
EGLConfig config; |
| 313 |
int num_configs; |
| 314 |
|
| 315 |
if (!eglChooseConfig(egl_display, attrib_list, &config, 1, &num_configs)) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
eglBindAPI(EGL_OPENGL_ES_API); |
| 320 |
|
| 321 |
int es_ver = (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) ? |
| 322 |
2 : 1; |
| 323 |
|
| 324 |
static EGLint ctxattr[3] = { |
| 325 |
EGL_CONTEXT_CLIENT_VERSION, 0xDEADBEEF, |
| 326 |
EGL_NONE |
| 327 |
}; |
| 328 |
|
| 329 |
ctxattr[1] = es_ver; |
| 330 |
|
| 331 |
egl_context = eglCreateContext(egl_display, config, EGL_NO_CONTEXT, ctxattr); |
| 332 |
if (egl_context == EGL_NO_CONTEXT) { |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
static EGL_DISPMANX_WINDOW_T nativewindow; |
| 337 |
DISPMANX_ELEMENT_HANDLE_T dispman_element; |
| 338 |
|
| 339 |
int dx, dy, screen_width, screen_height; |
| 340 |
_al_raspberrypi_get_screen_info(&dx, &dy, &screen_width, &screen_height); |
| 341 |
|
| 342 |
mouse_scale_ratio_x = (float)display->w / screen_width; |
| 343 |
mouse_scale_ratio_y = (float)display->h / screen_height; |
| 344 |
|
| 345 |
d->cursor_offset_x = dx; |
| 346 |
d->cursor_offset_y = dy; |
| 347 |
|
| 348 |
dst_rect.x = dx; |
| 349 |
dst_rect.y = dy; |
| 350 |
dst_rect.width = screen_width; |
| 351 |
dst_rect.height = screen_height; |
| 352 |
|
| 353 |
d->screen_width = screen_width; |
| 354 |
d->screen_height = screen_height; |
| 355 |
|
| 356 |
src_rect.x = 0; |
| 357 |
src_rect.y = 0; |
| 358 |
src_rect.width = display->w << 16; |
| 359 |
src_rect.height = display->h << 16; |
| 360 |
|
| 361 |
dispman_display = vc_dispmanx_display_open(0 /* LCD */); |
| 362 |
dispman_update = vc_dispmanx_update_start(0); |
| 363 |
|
| 364 |
dispman_element = vc_dispmanx_element_add ( |
| 365 |
dispman_update, |
| 366 |
dispman_display, |
| 367 |
0/*layer*/, |
| 368 |
&dst_rect, |
| 369 |
0/*src*/, |
| 370 |
&src_rect, |
| 371 |
DISPMANX_PROTECTION_NONE, |
| 372 |
0 /*alpha*/, |
| 373 |
0/*clamp*/, |
| 374 |
DISPMANX_NO_ROTATE/*transform*/ |
| 375 |
); |
| 376 |
|
| 377 |
nativewindow.element = dispman_element; |
| 378 |
nativewindow.width = display->w; |
| 379 |
nativewindow.height = display->h; |
| 380 |
vc_dispmanx_update_submit_sync(dispman_update); |
| 381 |
|
| 382 |
egl_window = eglCreateWindowSurface( |
| 383 |
egl_display, config, &nativewindow, NULL); |
| 384 |
if (egl_window == EGL_NO_SURFACE) { |
| 385 |
return false; |
| 386 |
} |
| 387 |
|
| 388 |
if (!eglMakeCurrent(egl_display, egl_window, egl_window, egl_context)) { |
| 389 |
return false; |
| 390 |
} |
| 391 |
|
| 392 |
if (!getenv("DISPLAY")) { |
| 393 |
_al_evdev_set_mouse_range(0, 0, display->w-1, display->h-1); |
| 394 |
} |
| 395 |
|
| 396 |
return true; |
| 397 |
} |
| 398 |
|
| 399 |
static ALLEGRO_DISPLAY *raspberrypi_create_display(int w, int h) |
| 400 |
{ |
| 401 |
ALLEGRO_DISPLAY_RASPBERRYPI *d = al_calloc(1, sizeof *d); |
| 402 |
ALLEGRO_DISPLAY *display = (void*)d; |
| 403 |
ALLEGRO_OGL_EXTRAS *ogl = al_calloc(1, sizeof *ogl); |
| 404 |
display->ogl_extras = ogl; |
| 405 |
display->vt = _al_get_raspberrypi_display_interface(); |
| 406 |
display->flags = al_get_new_display_flags(); |
| 407 |
|
| 408 |
ALLEGRO_SYSTEM_RASPBERRYPI *system = (void *)al_get_system_driver(); |
| 409 |
|
| 410 |
/* Add ourself to the list of displays. */ |
| 411 |
ALLEGRO_DISPLAY_RASPBERRYPI **add; |
| 412 |
add = _al_vector_alloc_back(&system->system.displays); |
| 413 |
*add = d; |
| 414 |
|
| 415 |
/* Each display is an event source. */ |
| 416 |
_al_event_source_init(&display->es); |
| 417 |
|
| 418 |
display->extra_settings.settings[ALLEGRO_COMPATIBLE_DISPLAY] = 1; |
| 419 |
|
| 420 |
display->w = w; |
| 421 |
display->h = h; |
| 422 |
|
| 423 |
display->flags |= ALLEGRO_OPENGL; |
| 424 |
#ifdef ALLEGRO_CFG_OPENGLES2 |
| 425 |
display->flags |= ALLEGRO_PROGRAMMABLE_PIPELINE; |
| 426 |
#endif |
| 427 |
#ifdef ALLEGRO_CFG_OPENGLES |
| 428 |
display->flags |= ALLEGRO_OPENGL_ES_PROFILE; |
| 429 |
#endif |
| 430 |
|
| 431 |
if (!pi_create_display(display)) { |
| 432 |
// FIXME: cleanup |
| 433 |
return NULL; |
| 434 |
} |
| 435 |
|
| 436 |
if (getenv("DISPLAY")) { |
| 437 |
_al_mutex_lock(&system->lock); |
| 438 |
Window root = RootWindow( |
| 439 |
system->x11display, DefaultScreen(system->x11display)); |
| 440 |
XWindowAttributes attr; |
| 441 |
XGetWindowAttributes(system->x11display, root, &attr); |
| 442 |
d->window = XCreateWindow( |
| 443 |
system->x11display, |
| 444 |
root, |
| 445 |
0, |
| 446 |
0, |
| 447 |
attr.width, |
| 448 |
attr.height, |
| 449 |
0, 0, |
| 450 |
InputOnly, |
| 451 |
DefaultVisual(system->x11display, 0), |
| 452 |
0, |
| 453 |
NULL |
| 454 |
); |
| 455 |
XGetWindowAttributes(system->x11display, d->window, &attr); |
| 456 |
XSelectInput( |
| 457 |
system->x11display, |
| 458 |
d->window, |
| 459 |
PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
| 460 |
); |
| 461 |
XMapWindow(system->x11display, d->window); |
| 462 |
_al_xwin_reset_size_hints(display); |
| 463 |
_al_xwin_set_fullscreen_window(display, 2); |
| 464 |
_al_xwin_set_size_hints(display, INT_MAX, INT_MAX); |
| 465 |
d->wm_delete_window_atom = XInternAtom(system->x11display, |
| 466 |
"WM_DELETE_WINDOW", False); |
| 467 |
XSetWMProtocols(system->x11display, d->window, &d->wm_delete_window_atom, 1); |
| 468 |
_al_mutex_unlock(&system->lock); |
| 469 |
} |
| 470 |
|
| 471 |
al_grab_mouse(display); |
| 472 |
|
| 473 |
_al_ogl_manage_extensions(display); |
| 474 |
_al_ogl_set_extensions(ogl->extension_api); |
| 475 |
|
| 476 |
setup_gl(display); |
| 477 |
|
| 478 |
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); |
| 479 |
|
| 480 |
if (al_is_mouse_installed() && !getenv("DISPLAY")) { |
| 481 |
_al_evdev_set_mouse_range(0, 0, display->w-1, display->h-1); |
| 482 |
} |
| 483 |
|
| 484 |
set_cursor_data(d, default_cursor, DEFAULT_CURSOR_WIDTH, DEFAULT_CURSOR_HEIGHT); |
| 485 |
|
| 486 |
/* Fill in opengl version */ |
| 487 |
const int v = display->ogl_extras->ogl_info.version; |
| 488 |
display->extra_settings.settings[ALLEGRO_OPENGL_MAJOR_VERSION] = (v >> 24) & 0xFF; |
| 489 |
display->extra_settings.settings[ALLEGRO_OPENGL_MINOR_VERSION] = (v >> 16) & 0xFF; |
| 490 |
|
| 491 |
return display; |
| 492 |
} |
| 493 |
|
| 494 |
static void raspberrypi_destroy_display(ALLEGRO_DISPLAY *d) |
| 495 |
{ |
| 496 |
ALLEGRO_DISPLAY_RASPBERRYPI *pidisplay = (ALLEGRO_DISPLAY_RASPBERRYPI *)d; |
| 497 |
|
| 498 |
hide_cursor(pidisplay); |
| 499 |
delete_cursor_data(pidisplay); |
| 500 |
|
| 501 |
_al_set_current_display_only(d); |
| 502 |
|
| 503 |
while (d->bitmaps._size > 0) { |
| 504 |
ALLEGRO_BITMAP **bptr = (ALLEGRO_BITMAP **)_al_vector_ref_back(&d->bitmaps); |
| 505 |
ALLEGRO_BITMAP *b = *bptr; |
| 506 |
_al_convert_to_memory_bitmap(b); |
| 507 |
} |
| 508 |
|
| 509 |
_al_event_source_free(&d->es); |
| 510 |
|
| 511 |
ALLEGRO_SYSTEM_RASPBERRYPI *system = (void *)al_get_system_driver(); |
| 512 |
_al_vector_find_and_delete(&system->system.displays, &d); |
| 513 |
|
| 514 |
eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 515 |
eglDestroySurface(egl_display, egl_window); |
| 516 |
eglDestroyContext(egl_display, egl_context); |
| 517 |
eglTerminate(egl_display); |
| 518 |
|
| 519 |
if (getenv("DISPLAY")) { |
| 520 |
_al_mutex_lock(&system->lock); |
| 521 |
XUnmapWindow(system->x11display, pidisplay->window); |
| 522 |
XDestroyWindow(system->x11display, pidisplay->window); |
| 523 |
_al_mutex_unlock(&system->lock); |
| 524 |
} |
| 525 |
|
| 526 |
if (system->mouse_grab_display == d) { |
| 527 |
system->mouse_grab_display = NULL; |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
static bool raspberrypi_set_current_display(ALLEGRO_DISPLAY *d) |
| 532 |
{ |
| 533 |
(void)d; |
| 534 |
// FIXME |
| 535 |
_al_ogl_update_render_state(d); |
| 536 |
return true; |
| 537 |
} |
| 538 |
|
| 539 |
static int raspberrypi_get_orientation(ALLEGRO_DISPLAY *d) |
| 540 |
{ |
| 541 |
(void)d; |
| 542 |
return ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
/* There can be only one window and only one OpenGL context, so all bitmaps |
| 547 |
* are compatible. |
| 548 |
*/ |
| 549 |
static bool raspberrypi_is_compatible_bitmap( |
| 550 |
ALLEGRO_DISPLAY *display, |
| 551 |
ALLEGRO_BITMAP *bitmap |
| 552 |
) { |
| 553 |
(void)display; |
| 554 |
(void)bitmap; |
| 555 |
return true; |
| 556 |
} |
| 557 |
|
| 558 |
/* Resizing is not possible. */ |
| 559 |
static bool raspberrypi_resize_display(ALLEGRO_DISPLAY *d, int w, int h) |
| 560 |
{ |
| 561 |
(void)d; |
| 562 |
(void)w; |
| 563 |
(void)h; |
| 564 |
return false; |
| 565 |
} |
| 566 |
|
| 567 |
/* The icon must be provided in the Info.plist file, it cannot be changed |
| 568 |
* at runtime. |
| 569 |
*/ |
| 570 |
static void raspberrypi_set_icons(ALLEGRO_DISPLAY *d, int num_icons, ALLEGRO_BITMAP *bitmaps[]) |
| 571 |
{ |
| 572 |
(void)d; |
| 573 |
(void)num_icons; |
| 574 |
(void)bitmaps; |
| 575 |
} |
| 576 |
|
| 577 |
/* There is no way to leave fullscreen so no window title is visible. */ |
| 578 |
static void raspberrypi_set_window_title(ALLEGRO_DISPLAY *display, char const *title) |
| 579 |
{ |
| 580 |
(void)display; |
| 581 |
(void)title; |
| 582 |
} |
| 583 |
|
| 584 |
/* The window always spans the entire screen right now. */ |
| 585 |
static void raspberrypi_set_window_position(ALLEGRO_DISPLAY *display, int x, int y) |
| 586 |
{ |
| 587 |
(void)display; |
| 588 |
(void)x; |
| 589 |
(void)y; |
| 590 |
} |
| 591 |
|
| 592 |
/* The window cannot be constrained. */ |
| 593 |
static bool raspberrypi_set_window_constraints(ALLEGRO_DISPLAY *display, |
| 594 |
int min_w, int min_h, int max_w, int max_h) |
| 595 |
{ |
| 596 |
(void)display; |
| 597 |
(void)min_w; |
| 598 |
(void)min_h; |
| 599 |
(void)max_w; |
| 600 |
(void)max_h; |
| 601 |
return false; |
| 602 |
} |
| 603 |
|
| 604 |
/* Always fullscreen. */ |
| 605 |
static bool raspberrypi_set_display_flag(ALLEGRO_DISPLAY *display, |
| 606 |
int flag, bool onoff) |
| 607 |
{ |
| 608 |
(void)display; |
| 609 |
(void)flag; |
| 610 |
(void)onoff; |
| 611 |
return false; |
| 612 |
} |
| 613 |
|
| 614 |
static void raspberrypi_get_window_position(ALLEGRO_DISPLAY *display, int *x, int *y) |
| 615 |
{ |
| 616 |
(void)display; |
| 617 |
*x = 0; |
| 618 |
*y = 0; |
| 619 |
} |
| 620 |
|
| 621 |
/* The window cannot be constrained. */ |
| 622 |
static bool raspberrypi_get_window_constraints(ALLEGRO_DISPLAY *display, |
| 623 |
int *min_w, int *min_h, int *max_w, int *max_h) |
| 624 |
{ |
| 625 |
(void)display; |
| 626 |
(void)min_w; |
| 627 |
(void)min_h; |
| 628 |
(void)max_w; |
| 629 |
(void)max_h; |
| 630 |
return false; |
| 631 |
} |
| 632 |
|
| 633 |
static bool raspberrypi_wait_for_vsync(ALLEGRO_DISPLAY *display) |
| 634 |
{ |
| 635 |
(void)display; |
| 636 |
return false; |
| 637 |
} |
| 638 |
|
| 639 |
static void raspberrypi_flip_display(ALLEGRO_DISPLAY *disp) |
| 640 |
{ |
| 641 |
eglSwapBuffers(egl_display, egl_window); |
| 642 |
|
| 643 |
if (cursor_added) { |
| 644 |
show_cursor((ALLEGRO_DISPLAY_RASPBERRYPI *)disp); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
static void raspberrypi_update_display_region(ALLEGRO_DISPLAY *d, int x, int y, |
| 649 |
int w, int h) |
| 650 |
{ |
| 651 |
(void)x; |
| 652 |
(void)y; |
| 653 |
(void)w; |
| 654 |
(void)h; |
| 655 |
raspberrypi_flip_display(d); |
| 656 |
} |
| 657 |
|
| 658 |
static bool raspberrypi_acknowledge_resize(ALLEGRO_DISPLAY *d) |
| 659 |
{ |
| 660 |
setup_gl(d); |
| 661 |
return true; |
| 662 |
} |
| 663 |
|
| 664 |
static bool raspberrypi_show_mouse_cursor(ALLEGRO_DISPLAY *display) |
| 665 |
{ |
| 666 |
ALLEGRO_DISPLAY_RASPBERRYPI *d = (void *)display; |
| 667 |
hide_cursor(d); |
| 668 |
show_cursor(d); |
| 669 |
return true; |
| 670 |
} |
| 671 |
|
| 672 |
static bool raspberrypi_hide_mouse_cursor(ALLEGRO_DISPLAY *display) |
| 673 |
{ |
| 674 |
ALLEGRO_DISPLAY_RASPBERRYPI *d = (void *)display; |
| 675 |
hide_cursor(d); |
| 676 |
return true; |
| 677 |
} |
| 678 |
|
| 679 |
static bool raspberrypi_set_mouse_cursor(ALLEGRO_DISPLAY *display, ALLEGRO_MOUSE_CURSOR *cursor) |
| 680 |
{ |
| 681 |
ALLEGRO_DISPLAY_RASPBERRYPI *d = (void *)display; |
| 682 |
ALLEGRO_MOUSE_CURSOR_RASPBERRYPI *pi_cursor = (void *)cursor; |
| 683 |
int w = al_get_bitmap_width(pi_cursor->bitmap); |
| 684 |
int h = al_get_bitmap_height(pi_cursor->bitmap); |
| 685 |
int pitch = w * sizeof(uint32_t); |
| 686 |
uint32_t *data = al_malloc(pitch * h); |
| 687 |
ALLEGRO_LOCKED_REGION *lr = al_lock_bitmap(pi_cursor->bitmap, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_READONLY); |
| 688 |
int y; |
| 689 |
for (y = 0; y < h; y++) { |
| 690 |
uint8_t *p = (uint8_t *)lr->data + lr->pitch * y; |
| 691 |
uint8_t *p2 = (uint8_t *)data + pitch * y; |
| 692 |
memcpy(p2, p, pitch); |
| 693 |
} |
| 694 |
al_unlock_bitmap(pi_cursor->bitmap); |
| 695 |
delete_cursor_data(d); |
| 696 |
set_cursor_data(d, data, w, h); |
| 697 |
al_free(data); |
| 698 |
if (cursor_added) { |
| 699 |
hide_cursor(d); |
| 700 |
show_cursor(d); |
| 701 |
} |
| 702 |
return true; |
| 703 |
} |
| 704 |
|
| 705 |
static bool raspberrypi_set_system_mouse_cursor(ALLEGRO_DISPLAY *display, ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id) |
| 706 |
{ |
| 707 |
(void)cursor_id; |
| 708 |
ALLEGRO_DISPLAY_RASPBERRYPI *d = (void *)display; |
| 709 |
delete_cursor_data(d); |
| 710 |
set_cursor_data(d, default_cursor, DEFAULT_CURSOR_WIDTH, DEFAULT_CURSOR_HEIGHT); |
| 711 |
return true; |
| 712 |
} |
| 713 |
|
| 714 |
/* Obtain a reference to this driver. */ |
| 715 |
ALLEGRO_DISPLAY_INTERFACE *_al_get_raspberrypi_display_interface(void) |
| 716 |
{ |
| 717 |
if (vt) |
| 718 |
return vt; |
| 719 |
|
| 720 |
vt = al_calloc(1, sizeof *vt); |
| 721 |
|
| 722 |
vt->create_display = raspberrypi_create_display; |
| 723 |
vt->destroy_display = raspberrypi_destroy_display; |
| 724 |
vt->set_current_display = raspberrypi_set_current_display; |
| 725 |
vt->flip_display = raspberrypi_flip_display; |
| 726 |
vt->update_display_region = raspberrypi_update_display_region; |
| 727 |
vt->acknowledge_resize = raspberrypi_acknowledge_resize; |
| 728 |
vt->create_bitmap = _al_ogl_create_bitmap; |
| 729 |
vt->get_backbuffer = _al_ogl_get_backbuffer; |
| 730 |
vt->set_target_bitmap = _al_ogl_set_target_bitmap; |
| 731 |
|
| 732 |
vt->get_orientation = raspberrypi_get_orientation; |
| 733 |
|
| 734 |
vt->is_compatible_bitmap = raspberrypi_is_compatible_bitmap; |
| 735 |
vt->resize_display = raspberrypi_resize_display; |
| 736 |
vt->set_icons = raspberrypi_set_icons; |
| 737 |
vt->set_window_title = raspberrypi_set_window_title; |
| 738 |
vt->set_window_position = raspberrypi_set_window_position; |
| 739 |
vt->get_window_position = raspberrypi_get_window_position; |
| 740 |
vt->set_window_constraints = raspberrypi_set_window_constraints; |
| 741 |
vt->get_window_constraints = raspberrypi_get_window_constraints; |
| 742 |
vt->set_display_flag = raspberrypi_set_display_flag; |
| 743 |
vt->wait_for_vsync = raspberrypi_wait_for_vsync; |
| 744 |
|
| 745 |
vt->update_render_state = _al_ogl_update_render_state; |
| 746 |
|
| 747 |
_al_ogl_add_drawing_functions(vt); |
| 748 |
|
| 749 |
vt->set_mouse_cursor = raspberrypi_set_mouse_cursor; |
| 750 |
vt->set_system_mouse_cursor = raspberrypi_set_system_mouse_cursor; |
| 751 |
vt->show_mouse_cursor = raspberrypi_show_mouse_cursor; |
| 752 |
vt->hide_mouse_cursor = raspberrypi_hide_mouse_cursor; |
| 753 |
|
| 754 |
return vt; |
| 755 |
} |
| 756 |
|