summaryrefslogtreecommitdiffstats
path: root/patches/SDL2-2.0.8/0002-sdl-2.0.8-KMSDRM-find-available-card-if-called-witho.patch
blob: e889f87aeb3d06ff599f3957eed544f23de5b0af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
From 212c7c68ec228a1058f39497d18c65e9312d00df Mon Sep 17 00:00:00 2001
From: Michael Grzeschik <m.grzeschik@pengutronix.de>
Date: Thu, 12 Jul 2018 13:48:54 +0200
Subject: [PATCH] sdl-2.0.8: KMSDRM: find available card if called without
 index

Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
---
 src/video/kmsdrm/SDL_kmsdrmvideo.c | 84 ++++++++++++++++++++++++++++--
 1 file changed, 80 insertions(+), 4 deletions(-)

diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index 7855eeddb..f63c91b0a 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -41,15 +41,22 @@
 #include "SDL_kmsdrmopengles.h"
 #include "SDL_kmsdrmmouse.h"
 #include "SDL_kmsdrmdyn.h"
+#include <sys/stat.h>
+#include <dirent.h>
+#include <errno.h>
 
-#define KMSDRM_DRI_CARD_0 "/dev/dri/card0"
+#define KMSDRM_DRI_PATH "/dev/dri/"
 
 static int
-KMSDRM_Available(void)
+check_modestting(int devindex)
 {
     int available = 0;
+    char device[512];
+    int drm_fd;
+
+    sprintf(device, "%scard%d", KMSDRM_DRI_PATH, devindex);
 
-    int drm_fd = open(KMSDRM_DRI_CARD_0, O_RDWR | O_CLOEXEC);
+    drm_fd = open(device, O_RDWR | O_CLOEXEC);
     if (drm_fd >= 0) {
         if (SDL_KMSDRM_LoadSymbols()) {
             drmModeRes *resources = KMSDRM_drmModeGetResources(drm_fd);
@@ -65,6 +72,72 @@ KMSDRM_Available(void)
     return available;
 }
 
+static int get_dricount(void)
+{
+    int devcount = 0;
+    struct dirent *res;
+    struct stat sb;
+    DIR *folder;
+
+    if (!(stat(KMSDRM_DRI_PATH, &sb) == 0
+                && S_ISDIR(sb.st_mode))) {
+        printf("The path %s cannot be opened or is not available\n",
+               KMSDRM_DRI_PATH);
+        return 0;
+    }
+
+    if (access(KMSDRM_DRI_PATH, F_OK) == -1) {
+        printf("The path %s cannot be opened\n",
+               KMSDRM_DRI_PATH);
+        return 0;
+    }
+
+    folder = opendir(KMSDRM_DRI_PATH);
+    if (!folder)
+        return 0;
+
+    while ((res = readdir(folder))) {
+        if (res->d_type == DT_CHR)
+            devcount++;
+    }
+
+    closedir(folder);
+
+    return devcount;
+}
+
+static int
+get_driindex(void)
+{
+    int i = 0;
+    int available = 0;
+    int devcount = get_dricount();
+
+    for (i = 0; i < devcount; i++) {
+        if (check_modestting(i)) {
+            available = 1;
+            break;
+        }
+    }
+
+    if (available)
+        return i;
+
+    return -ENOENT;
+}
+
+static int
+KMSDRM_Available(void)
+{
+    int ret = -ENOENT;
+
+    ret = get_driindex();
+    if (ret >= 0)
+        return 1;
+
+    return ret;
+}
+
 static void
 KMSDRM_Destroy(SDL_VideoDevice * device)
 {
@@ -83,7 +156,10 @@ KMSDRM_Create(int devindex)
     SDL_VideoDevice *device;
     SDL_VideoData *vdata;
 
-    if (devindex < 0 || devindex > 99) {
+    if (!devindex || devindex > 99)
+        devindex = get_driindex();
+
+    if (devindex < 0) {
         SDL_SetError("devindex (%d) must be between 0 and 99.\n", devindex);
         return NULL;
     }
-- 
2.18.0