backend.c revision 01e04c3f
13464ebd5Sriastradh/*
23464ebd5Sriastradh * Copyright © 2011 Intel Corporation
33464ebd5Sriastradh *
43464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a
53464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"),
63464ebd5Sriastradh * to deal in the Software without restriction, including without limitation
73464ebd5Sriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83464ebd5Sriastradh * and/or sell copies of the Software, and to permit persons to whom the
93464ebd5Sriastradh * Software is furnished to do so, subject to the following conditions:
103464ebd5Sriastradh *
113464ebd5Sriastradh * The above copyright notice and this permission notice (including the next
123464ebd5Sriastradh * paragraph) shall be included in all copies or substantial portions of the
133464ebd5Sriastradh * Software.
143464ebd5Sriastradh *
153464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
163464ebd5Sriastradh * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
173464ebd5Sriastradh * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
183464ebd5Sriastradh * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
193464ebd5Sriastradh * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
203464ebd5Sriastradh * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
213464ebd5Sriastradh * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
223464ebd5Sriastradh * DEALINGS IN THE SOFTWARE.
233464ebd5Sriastradh *
243464ebd5Sriastradh * Authors:
253464ebd5Sriastradh *    Benjamin Franzke <benjaminfranzke@googlemail.com>
263464ebd5Sriastradh */
273464ebd5Sriastradh
283464ebd5Sriastradh#include <stdio.h>
293464ebd5Sriastradh#include <stddef.h>
303464ebd5Sriastradh#include <stdlib.h>
313464ebd5Sriastradh#include <string.h>
323464ebd5Sriastradh#include <limits.h>
333464ebd5Sriastradh
343464ebd5Sriastradh#include "backend.h"
353464ebd5Sriastradh
363464ebd5Sriastradh#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
373464ebd5Sriastradh
383464ebd5Sriastradhextern const struct gbm_backend gbm_dri_backend;
393464ebd5Sriastradh
403464ebd5Sriastradhstruct backend_desc {
413464ebd5Sriastradh   const char *name;
423464ebd5Sriastradh   const struct gbm_backend *builtin;
433464ebd5Sriastradh};
443464ebd5Sriastradh
453464ebd5Sriastradhstatic const struct backend_desc backends[] = {
463464ebd5Sriastradh   { "gbm_dri.so", &gbm_dri_backend },
473464ebd5Sriastradh};
483464ebd5Sriastradh
493464ebd5Sriastradhstatic const void *
503464ebd5Sriastradhload_backend(const struct backend_desc *backend)
513464ebd5Sriastradh{
523464ebd5Sriastradh   const void *init = NULL;
533464ebd5Sriastradh
543464ebd5Sriastradh   if (backend == NULL)
553464ebd5Sriastradh      return NULL;
563464ebd5Sriastradh
573464ebd5Sriastradh   if (backend->builtin) {
583464ebd5Sriastradh      init = backend->builtin;
593464ebd5Sriastradh   }
603464ebd5Sriastradh
613464ebd5Sriastradh   return init;
623464ebd5Sriastradh}
633464ebd5Sriastradh
643464ebd5Sriastradhstatic const struct backend_desc *
653464ebd5Sriastradhfind_backend(const char *name)
663464ebd5Sriastradh{
673464ebd5Sriastradh   const struct backend_desc *backend = NULL;
6801e04c3fSmrg   unsigned i;
693464ebd5Sriastradh
703464ebd5Sriastradh   for (i = 0; i < ARRAY_SIZE(backends); ++i) {
713464ebd5Sriastradh      if (strcmp(backends[i].name, name) == 0) {
723464ebd5Sriastradh         backend = &backends[i];
733464ebd5Sriastradh         break;
743464ebd5Sriastradh      }
753464ebd5Sriastradh   }
763464ebd5Sriastradh
773464ebd5Sriastradh   return backend;
783464ebd5Sriastradh}
793464ebd5Sriastradh
803464ebd5Sriastradhstruct gbm_device *
813464ebd5Sriastradh_gbm_create_device(int fd)
823464ebd5Sriastradh{
833464ebd5Sriastradh   const struct gbm_backend *backend = NULL;
843464ebd5Sriastradh   struct gbm_device *dev = NULL;
8501e04c3fSmrg   unsigned i;
863464ebd5Sriastradh   const char *b;
873464ebd5Sriastradh
883464ebd5Sriastradh   b = getenv("GBM_BACKEND");
893464ebd5Sriastradh   if (b)
903464ebd5Sriastradh      backend = load_backend(find_backend(b));
913464ebd5Sriastradh
923464ebd5Sriastradh   if (backend)
933464ebd5Sriastradh      dev = backend->create_device(fd);
943464ebd5Sriastradh
953464ebd5Sriastradh   for (i = 0; i < ARRAY_SIZE(backends) && dev == NULL; ++i) {
963464ebd5Sriastradh      backend = load_backend(&backends[i]);
973464ebd5Sriastradh      if (backend == NULL)
983464ebd5Sriastradh         continue;
993464ebd5Sriastradh
1003464ebd5Sriastradh      dev = backend->create_device(fd);
1013464ebd5Sriastradh   }
1023464ebd5Sriastradh
1033464ebd5Sriastradh   return dev;
1043464ebd5Sriastradh}
105