kmsdrm: find available card if called without index.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 09 Oct 2018 00:27:55 -0400
changeset 1231018f3294b230e
parent 12309 5c478c2f0422
child 12311 b19b09eb9e6d
kmsdrm: find available card if called without index.

This work was done by Michael Grzeschik, I just cleaned up the patch a little.

Fixes Bugzilla #4241.
src/video/kmsdrm/SDL_kmsdrmvideo.c
     1.1 --- a/src/video/kmsdrm/SDL_kmsdrmvideo.c	Mon Oct 08 19:46:01 2018 -0700
     1.2 +++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c	Tue Oct 09 00:27:55 2018 -0400
     1.3 @@ -41,20 +41,27 @@
     1.4  #include "SDL_kmsdrmopengles.h"
     1.5  #include "SDL_kmsdrmmouse.h"
     1.6  #include "SDL_kmsdrmdyn.h"
     1.7 +#include <sys/stat.h>
     1.8 +#include <dirent.h>
     1.9 +#include <errno.h>
    1.10  
    1.11 -#define KMSDRM_DRI_CARD_0 "/dev/dri/card0"
    1.12 +#define KMSDRM_DRI_PATH "/dev/dri/"
    1.13  
    1.14  static int
    1.15 -KMSDRM_Available(void)
    1.16 +check_modestting(int devindex)
    1.17  {
    1.18 -    int available = 0;
    1.19 +    SDL_bool available = SDL_FALSE;
    1.20 +    char device[512];
    1.21 +    int drm_fd;
    1.22  
    1.23 -    int drm_fd = open(KMSDRM_DRI_CARD_0, O_RDWR | O_CLOEXEC);
    1.24 +    SDL_snprintf(device, sizeof (device), "%scard%d", KMSDRM_DRI_PATH, devindex);
    1.25 +
    1.26 +    drm_fd = open(device, O_RDWR | O_CLOEXEC);
    1.27      if (drm_fd >= 0) {
    1.28          if (SDL_KMSDRM_LoadSymbols()) {
    1.29              drmModeRes *resources = KMSDRM_drmModeGetResources(drm_fd);
    1.30              if (resources != NULL) {
    1.31 -                available = 1;
    1.32 +                available = SDL_TRUE;
    1.33                  KMSDRM_drmModeFreeResources(resources);
    1.34              }
    1.35              SDL_KMSDRM_UnloadSymbols();
    1.36 @@ -65,6 +72,66 @@
    1.37      return available;
    1.38  }
    1.39  
    1.40 +static int get_dricount(void)
    1.41 +{
    1.42 +    int devcount = 0;
    1.43 +    struct dirent *res;
    1.44 +    struct stat sb;
    1.45 +    DIR *folder;
    1.46 +
    1.47 +    if (!(stat(KMSDRM_DRI_PATH, &sb) == 0
    1.48 +                && S_ISDIR(sb.st_mode))) {
    1.49 +        printf("The path %s cannot be opened or is not available\n",
    1.50 +               KMSDRM_DRI_PATH);
    1.51 +        return 0;
    1.52 +    }
    1.53 +
    1.54 +    if (access(KMSDRM_DRI_PATH, F_OK) == -1) {
    1.55 +        printf("The path %s cannot be opened\n",
    1.56 +               KMSDRM_DRI_PATH);
    1.57 +        return 0;
    1.58 +    }
    1.59 +
    1.60 +    folder = opendir(KMSDRM_DRI_PATH);
    1.61 +    if (folder) {
    1.62 +        while ((res = readdir(folder))) {
    1.63 +            if (res->d_type == DT_CHR) {
    1.64 +                devcount++;
    1.65 +            }
    1.66 +        }
    1.67 +        closedir(folder);
    1.68 +    }
    1.69 +
    1.70 +    return devcount;
    1.71 +}
    1.72 +
    1.73 +static int
    1.74 +get_driindex(void)
    1.75 +{
    1.76 +    const int devcount = get_dricount();
    1.77 +    int i;
    1.78 +
    1.79 +    for (i = 0; i < devcount; i++) {
    1.80 +        if (check_modestting(i)) {
    1.81 +            return i;
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    return -ENOENT;
    1.86 +}
    1.87 +
    1.88 +static int
    1.89 +KMSDRM_Available(void)
    1.90 +{
    1.91 +    int ret = -ENOENT;
    1.92 +
    1.93 +    ret = get_driindex();
    1.94 +    if (ret >= 0)
    1.95 +        return 1;
    1.96 +
    1.97 +    return ret;
    1.98 +}
    1.99 +
   1.100  static void
   1.101  KMSDRM_Destroy(SDL_VideoDevice * device)
   1.102  {
   1.103 @@ -83,7 +150,11 @@
   1.104      SDL_VideoDevice *device;
   1.105      SDL_VideoData *vdata;
   1.106  
   1.107 -    if (devindex < 0 || devindex > 99) {
   1.108 +    if (!devindex || (devindex > 99)) {
   1.109 +        devindex = get_driindex();
   1.110 +    }
   1.111 +
   1.112 +    if (devindex < 0) {
   1.113          SDL_SetError("devindex (%d) must be between 0 and 99.\n", devindex);
   1.114          return NULL;
   1.115      }