- * A loader function for use with _eglPreloadForEach. The loader data is the
- * pattern (prefix) of the files to look for.
- */
-static EGLBoolean
-_eglLoaderPattern(const char *dir, size_t len, void *loader_data)
-{
-#if defined(_EGL_OS_UNIX)
- const char *prefix, *suffix;
- size_t prefix_len, suffix_len;
- DIR *dirp;
- struct dirent *dirent;
- char path[1024];
-
- if (len + 2 > sizeof(path))
- return EGL_TRUE;
- if (len) {
- memcpy(path, dir, len);
- path[len++] = '/';
- }
- path[len] = '\0';
-
- dirp = opendir(path);
- if (!dirp)
- return EGL_TRUE;
-
- prefix = (const char *) loader_data;
- prefix_len = strlen(prefix);
- suffix = library_suffix();
- suffix_len = (suffix) ? strlen(suffix) : 0;
-
- while ((dirent = readdir(dirp))) {
- size_t dirent_len = strlen(dirent->d_name);
- const char *p;
-
- /* match the prefix */
- if (strncmp(dirent->d_name, prefix, prefix_len) != 0)
- continue;
- /* match the suffix */
- if (suffix) {
- p = dirent->d_name + dirent_len - suffix_len;
- if (p < dirent->d_name || strcmp(p, suffix) != 0)
- continue;
- }
-
- /* make a full path and add it to the module array */
- if (len + dirent_len + 1 <= sizeof(path)) {
- strcpy(path + len, dirent->d_name);
- _eglAddModule(path);
- }
- }
-
- closedir(dirp);
-
- return EGL_TRUE;
-#else /* _EGL_OS_UNIX */
- /* stop immediately */
- return EGL_FALSE;
-#endif
-}
-
-
-/**
- * Run the callback function on each driver directory.
- *
- * The process may end prematurely if the callback function returns false.
- */
-static void
-_eglPreloadForEach(const char *search_path,
- EGLBoolean (*loader)(const char *, size_t, void *),
- void *loader_data)
-{
- const char *cur, *next;
- size_t len;
-
- cur = search_path;
- while (cur) {
- next = strchr(cur, ':');
- len = (next) ? next - cur : strlen(cur);
-
- if (!loader(cur, len, loader_data))
- break;
-
- cur = (next) ? next + 1 : NULL;
- }
-}
-
-
-/**
- * Return a list of colon-separated driver directories.
- */
-static const char *
-_eglGetSearchPath(void)
-{
- static const char *search_path;
-
-#if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
- if (!search_path) {
- static char buffer[1024];
- const char *p;
- int ret;
-
- p = getenv("EGL_DRIVERS_PATH");
-#if defined(_EGL_OS_UNIX)
- if (p && (geteuid() != getuid() || getegid() != getgid())) {
- _eglLog(_EGL_DEBUG,
- "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
- p = NULL;
- }
-#endif /* _EGL_OS_UNIX */
-
- if (p) {
- ret = _eglsnprintf(buffer, sizeof(buffer),
- "%s:%s", p, _EGL_DRIVER_SEARCH_DIR);
- if (ret > 0 && ret < sizeof(buffer))
- search_path = buffer;
- }
- }
- if (!search_path)
- search_path = _EGL_DRIVER_SEARCH_DIR;
-#else
- search_path = "";
-#endif
-
- return search_path;
-}
-
-
-/**
- * Add the user driver to the module array.
- *
- * The user driver is specified by EGL_DRIVER.
- */
-static void
-_eglAddUserDriver(void)
-{
- const char *search_path = _eglGetSearchPath();
- char *env;
-
- env = getenv("EGL_DRIVER");
-#if defined(_EGL_OS_UNIX)
- if (env && strchr(env, '/')) {
- search_path = "";
- if ((geteuid() != getuid() || getegid() != getgid())) {
- _eglLog(_EGL_DEBUG,
- "ignore EGL_DRIVER for setuid/setgid binaries");
- env = NULL;
- }
- }
-#endif /* _EGL_OS_UNIX */
- if (env)
- _eglPreloadForEach(search_path, _eglLoaderFile, (void *) env);
-}
-
-
-/**
- * Add default drivers to the module array.
- */
-static void
-_eglAddDefaultDrivers(void)
-{
- const char *search_path = _eglGetSearchPath();
- EGLint i;
-#if defined(_EGL_OS_WINDOWS)
- const char *DefaultDriverNames[] = {
- "egl_gallium"
- };
-#elif defined(_EGL_OS_UNIX)
- const char *DefaultDriverNames[] = {
- "egl_gallium",
- "egl_dri2",
- "egl_glx"
- };
-#endif
-
- for (i = 0; i < ARRAY_SIZE(DefaultDriverNames); i++) {
- void *name = (void *) DefaultDriverNames[i];
- _eglPreloadForEach(search_path, _eglLoaderFile, name);
- }
-}
-
-
-/**
- * Add drivers to the module array. Drivers will be loaded as they are matched
- * to displays.
- */
-static EGLBoolean
-_eglAddDrivers(void)
-{
- if (_eglModules)
- return EGL_TRUE;
-
- /* the order here decides the priorities of the drivers */
- _eglAddUserDriver();
- _eglAddDefaultDrivers();
- _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderPattern, (void *) "egl_");
-
- return (_eglModules != NULL);
-}
-
-
-/**
- * Match a display to a driver. The display is initialized unless use_probe is
- * true.
- *
- * The matching is done by finding the first driver that can initialize the
- * display, or when use_probe is true, the driver with highest score.