Fixed bug 3369 - RaspberryPI ability to specify a Dispmanx layer

Albert Casals

On a RaspberryPI, it might become convenient to specify the Dispmanx layer SDL uses.
Currently, it is hardcoded to be 10000 to sit above most applications.

This can be specially useful when integrating other graphical apps and frameworks like OMXplayer, QT5 etc.. in order to have more flexibility on their Z-order.
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 9efb955..dd15464 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -700,6 +700,14 @@
 #define SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING "SDL_WINDOWS_DISABLE_THREAD_NAMING"
 
 /**
+ * \brief Tell SDL which Dispmanx layer to use on a Raspberry PI
+ *
+ * Also known as Z-order. The variable can take a negative or positive value.
+ * The default is 10000.
+ */
+#define SDL_HINT_RPI_VIDEO_LAYER           "SDL_RPI_VIDEO_LAYER"
+
+/**
  *  \brief  An enumeration of hint priorities
  */
 typedef enum
diff --git a/src/video/raspberry/SDL_rpimouse.c b/src/video/raspberry/SDL_rpimouse.c
index 3a84254..ae9bdfd 100644
--- a/src/video/raspberry/SDL_rpimouse.c
+++ b/src/video/raspberry/SDL_rpimouse.c
@@ -24,6 +24,7 @@
 
 #include "SDL_assert.h"
 #include "SDL_surface.h"
+#include "SDL_hints.h"
 
 #include "SDL_rpivideo.h"
 #include "SDL_rpimouse.h"
@@ -117,7 +118,9 @@
     SDL_VideoDisplay *display;
     SDL_DisplayData *data;
     VC_DISPMANX_ALPHA_T alpha = {  DISPMANX_FLAGS_ALPHA_FROM_SOURCE /* flags */ , 255 /*opacity 0->255*/,  0 /* mask */ };
-    
+    uint32_t layer = SDL_RPI_MOUSELAYER;
+    const char *env;
+
     mouse = SDL_GetMouse();
     if (mouse == NULL) {
         return -1;
@@ -167,9 +170,14 @@
         update = vc_dispmanx_update_start(10);
         SDL_assert(update);
 
+        env = SDL_GetHint(SDL_HINT_RPI_VIDEO_LAYER);
+        if (env) {
+            layer = SDL_atoi(env) + 1;
+        }
+
         curdata->element = vc_dispmanx_element_add(update,
                                                     data->dispman_display,
-                                                    SDL_RPI_MOUSELAYER, // layer
+                                                    layer,
                                                     &dst_rect,
                                                     curdata->resource,
                                                     &src_rect,
diff --git a/src/video/raspberry/SDL_rpivideo.c b/src/video/raspberry/SDL_rpivideo.c
index 40a398a..67dc77b 100644
--- a/src/video/raspberry/SDL_rpivideo.c
+++ b/src/video/raspberry/SDL_rpivideo.c
@@ -38,6 +38,7 @@
 #include "SDL_events.h"
 #include "../../events/SDL_mouse_c.h"
 #include "../../events/SDL_keyboard_c.h"
+#include "SDL_hints.h"
 
 #ifdef SDL_INPUT_LINUXEV
 #include "../../core/linux/SDL_evdev.h"
@@ -221,6 +222,8 @@
     VC_RECT_T src_rect;
     VC_DISPMANX_ALPHA_T         dispman_alpha;
     DISPMANX_UPDATE_HANDLE_T dispman_update;
+    uint32_t layer = SDL_RPI_VIDEOLAYER;
+    const char *env;
 
     /* Disable alpha, otherwise the app looks composed with whatever dispman is showing (X11, console,etc) */
     dispman_alpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS; 
@@ -253,11 +256,25 @@
     src_rect.width = window->w << 16;
     src_rect.height = window->h << 16;
 
+    env = SDL_GetHint(SDL_HINT_RPI_VIDEO_LAYER);
+    if (env) {
+        layer = SDL_atoi(env);
+    }
+
     dispman_update = vc_dispmanx_update_start( 0 );
-    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*/);
+    wdata->dispman_window.element = vc_dispmanx_element_add (dispman_update,
+                                                             displaydata->dispman_display,
+                                                             layer /* layer */,
+                                                             &dst_rect,
+                                                             0 /*src*/,
+                                                             &src_rect,
+                                                             DISPMANX_PROTECTION_NONE,
+                                                             &dispman_alpha /*alpha*/,
+                                                             0 /*clamp*/,
+                                                             0 /*transform*/);
     wdata->dispman_window.width = window->w;
     wdata->dispman_window.height = window->h;
-    vc_dispmanx_update_submit_sync( dispman_update );
+    vc_dispmanx_update_submit_sync(dispman_update);
     
     if (!_this->egl_data) {
         if (SDL_GL_LoadLibrary(NULL) < 0) {