Commit 005e2c59a7ea1e07eb6c5282cc722dd5334eb033

Manuel Alfayate Corchete 2021-02-09T00:57:25

[KMS/DRM] Bugfix number #5535: Improve reliability, by wahil1976.

diff --git a/src/video/kmsdrm/SDL_kmsdrmsym.h b/src/video/kmsdrm/SDL_kmsdrmsym.h
index cea8bb9..bb78677 100644
--- a/src/video/kmsdrm/SDL_kmsdrmsym.h
+++ b/src/video/kmsdrm/SDL_kmsdrmsym.h
@@ -41,6 +41,8 @@ SDL_KMSDRM_SYM(void,drmModeFreeCrtc,(drmModeCrtcPtr ptr))
 SDL_KMSDRM_SYM(void,drmModeFreeConnector,(drmModeConnectorPtr ptr))
 SDL_KMSDRM_SYM(void,drmModeFreeEncoder,(drmModeEncoderPtr ptr))
 SDL_KMSDRM_SYM(int,drmGetCap,(int fd, uint64_t capability, uint64_t *value))
+SDL_KMSDRM_SYM(int,drmSetMaster,(int fd))
+SDL_KMSDRM_SYM(int,drmAuthMagic,(int fd, drm_magic_t magic))
 SDL_KMSDRM_SYM(drmModeResPtr,drmModeGetResources,(int fd))
 SDL_KMSDRM_SYM(int,drmModeAddFB,(int fd, uint32_t width, uint32_t height, uint8_t depth,
                          uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index cbbe7b0..f6f651d 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -70,6 +70,7 @@ check_modestting(int devindex)
     SDL_bool available = SDL_FALSE;
     char device[512];
     int drm_fd;
+    int i;
 
     SDL_snprintf(device, sizeof (device), KMSDRM_DRI_DEVFMT, KMSDRM_DRI_PATH, devindex);
 
@@ -89,7 +90,22 @@ check_modestting(int devindex)
                  && resources->count_encoders > 0
                  && resources->count_crtcs > 0)
                 {
-                    available = SDL_TRUE;
+                    available = SDL_FALSE;
+                    for (i = 0; i < resources->count_connectors; i++) {
+                        drmModeConnector *conn = KMSDRM_drmModeGetConnector(drm_fd,
+                            resources->connectors[i]);
+
+                        if (!conn) {
+                            continue;
+                        }
+
+                        if (conn->connection == DRM_MODE_CONNECTED && conn->count_modes) {
+                            available = SDL_TRUE;
+                            break;
+                        }
+
+                        KMSDRM_drmModeFreeConnector(conn);
+                    }
                 }
                 KMSDRM_drmModeFreeResources(resources);
             }
@@ -751,6 +767,15 @@ KMSDRM_GBMInit (_THIS, SDL_DisplayData *dispdata)
     /* Reopen the FD! */
     viddata->drm_fd = open(viddata->devpath, O_RDWR | O_CLOEXEC);
 
+    /* Set the FD we just opened as current DRM master. */
+    KMSDRM_drmSetMaster(viddata->drm_fd);
+
+    /* Check if we are the current DRM master. */
+    if (KMSDRM_drmAuthMagic(viddata->drm_fd, 0) == -EACCES) {
+        ret = SDL_SetError("DRM device is claimed by another program as master.");
+        return ret;
+    }
+
     /* Create the GBM device. */
     viddata->gbm_dev = KMSDRM_gbm_create_device(viddata->drm_fd);
     if (!viddata->gbm_dev) {