pipe-loader: pass only the driver_name to pipe_loader_find_module
[mesa.git] / src / gallium / auxiliary / pipe-loader / pipe_loader.c
1 /**************************************************************************
2 *
3 * Copyright 2012 Francisco Jerez
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "pipe_loader_priv.h"
29
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_string.h"
33 #include "util/u_dl.h"
34 #include "util/xmlconfig.h"
35 #include "util/xmlpool.h"
36
37 #ifdef _MSC_VER
38 #include <stdlib.h>
39 #define PATH_MAX _MAX_PATH
40 #endif
41
42 #define MODULE_PREFIX "pipe_"
43
44 static int (*backends[])(struct pipe_loader_device **, int) = {
45 #ifdef HAVE_LIBDRM
46 &pipe_loader_drm_probe,
47 #endif
48 &pipe_loader_sw_probe
49 };
50
51 const char gallium_driinfo_xml[] =
52 #include "driinfo_gallium.h"
53 ;
54
55 int
56 pipe_loader_probe(struct pipe_loader_device **devs, int ndev)
57 {
58 int i, n = 0;
59
60 for (i = 0; i < ARRAY_SIZE(backends); i++)
61 n += backends[i](&devs[n], MAX2(0, ndev - n));
62
63 return n;
64 }
65
66 void
67 pipe_loader_release(struct pipe_loader_device **devs, int ndev)
68 {
69 int i;
70
71 for (i = 0; i < ndev; i++)
72 devs[i]->ops->release(&devs[i]);
73 }
74
75 void
76 pipe_loader_base_release(struct pipe_loader_device **dev)
77 {
78 driDestroyOptionCache(&(*dev)->option_cache);
79 driDestroyOptionInfo(&(*dev)->option_info);
80
81 FREE(*dev);
82 *dev = NULL;
83 }
84
85 const struct drm_conf_ret *
86 pipe_loader_configuration(struct pipe_loader_device *dev,
87 enum drm_conf conf)
88 {
89 return dev->ops->configuration(dev, conf);
90 }
91
92 void
93 pipe_loader_load_options(struct pipe_loader_device *dev)
94 {
95 if (dev->option_info.info)
96 return;
97
98 const char *xml_options = gallium_driinfo_xml;
99 const struct drm_conf_ret *xml_options_conf =
100 pipe_loader_configuration(dev, DRM_CONF_XML_OPTIONS);
101
102 if (xml_options_conf)
103 xml_options = xml_options_conf->val.val_pointer;
104
105 driParseOptionInfo(&dev->option_info, xml_options);
106 driParseConfigFiles(&dev->option_cache, &dev->option_info, 0,
107 dev->driver_name);
108 }
109
110 struct pipe_screen *
111 pipe_loader_create_screen(struct pipe_loader_device *dev,
112 struct pipe_screen_config *config)
113 {
114 pipe_loader_load_options(dev);
115 config->options = &dev->option_cache;
116
117 return dev->ops->create_screen(dev, config);
118 }
119
120 struct util_dl_library *
121 pipe_loader_find_module(const char *driver_name,
122 const char *library_paths)
123 {
124 struct util_dl_library *lib;
125 const char *next;
126 char path[PATH_MAX];
127 int len, ret;
128
129 for (next = library_paths; *next; library_paths = next + 1) {
130 next = util_strchrnul(library_paths, ':');
131 len = next - library_paths;
132
133 if (len)
134 ret = util_snprintf(path, sizeof(path), "%.*s/%s%s%s",
135 len, library_paths,
136 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
137 else
138 ret = util_snprintf(path, sizeof(path), "%s%s%s",
139 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
140
141 if (ret > 0 && ret < sizeof(path)) {
142 lib = util_dl_open(path);
143 if (lib) {
144 return lib;
145 }
146 }
147 }
148
149 return NULL;
150 }