Commit 6fbe9e23faafa685fc32c4e03d636827139ca059

Ryan C. Gordon 2019-02-19T23:46:54

raspberry: expose second display. This lets apps see and choose between both an HDMI and DSI-connected display, such as a television and the Pi Foundation's official touchscreen. It only exposes the second display if the hardware reports that it is connected.

diff --git a/src/video/raspberry/SDL_rpivideo.c b/src/video/raspberry/SDL_rpivideo.c
index 6088775..0f1ac00 100644
--- a/src/video/raspberry/SDL_rpivideo.c
+++ b/src/video/raspberry/SDL_rpivideo.c
@@ -154,28 +154,34 @@ VideoBootStrap RPI_bootstrap = {
     RPI_Create
 };
 
+
 /*****************************************************************************/
 /* SDL Video and Display initialization/handling functions                   */
 /*****************************************************************************/
-int
-RPI_VideoInit(_THIS)
+
+static void
+AddDispManXDisplay(const int display_id)
 {
+    DISPMANX_MODEINFO_T modeinfo;
+    DISPMANX_DISPLAY_HANDLE_T handle;
     SDL_VideoDisplay display;
     SDL_DisplayMode current_mode;
     SDL_DisplayData *data;
-    uint32_t w,h;
 
-    /* Initialize BCM Host */
-    bcm_host_init();
-
-    SDL_zero(current_mode);
+    handle = vc_dispmanx_display_open(display_id);
+    if (!handle) {
+        return;  /* this display isn't available */
+    }
 
-    if (graphics_get_display_size( 0, &w, &h) < 0) {
-        return -1;
+    if (vc_dispmanx_display_get_info(handle, &modeinfo) < 0) {
+        vc_dispmanx_display_close(handle);
+        return;
     }
 
-    current_mode.w = w;
-    current_mode.h = h;
+    /* RPI_GetRefreshRate() doesn't distinguish between displays. I'm not sure the hardware distinguishes either */
+    SDL_zero(current_mode);
+    current_mode.w = modeinfo.width;
+    current_mode.h = modeinfo.height;
     current_mode.refresh_rate = RPI_GetRefreshRate();
     /* 32 bpp for default */
     current_mode.format = SDL_PIXELFORMAT_ABGR8888;
@@ -189,14 +195,25 @@ RPI_VideoInit(_THIS)
     /* Allocate display internal data */
     data = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
     if (data == NULL) {
-        return SDL_OutOfMemory();
+        vc_dispmanx_display_close(handle);
+        return;  /* oh well */
     }
 
-    data->dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
+    data->dispman_display = handle;
 
     display.driverdata = data;
 
     SDL_AddVideoDisplay(&display);
+}
+
+int
+RPI_VideoInit(_THIS)
+{
+    /* Initialize BCM Host */
+    bcm_host_init();
+
+    AddDispManXDisplay(DISPMANX_ID_MAIN_LCD);  /* your default display */
+    AddDispManXDisplay(DISPMANX_ID_FORCE_OTHER);  /* an "other" display...maybe DSI-connected screen while HDMI is your main */
 
 #ifdef SDL_INPUT_LINUXEV    
     if (SDL_EVDEV_Init() < 0) {