- * Load a module and create the driver object.
- */
-static EGLBoolean
-_eglLoadModule(_EGLModule *mod)
-{
- _EGLMain_t mainFunc;
- lib_handle lib;
- _EGLDriver *drv;
-
- mainFunc = _eglOpenLibrary(mod->Path, &lib);
- if (!mainFunc)
- return EGL_FALSE;
-
- drv = mainFunc(NULL);
- if (!drv) {
- if (lib)
- close_library(lib);
- return EGL_FALSE;
- }
-
- if (!drv->Name) {
- _eglLog(_EGL_WARNING, "Driver loaded from %s has no name", mod->Path);
- drv->Name = "UNNAMED";
- }
-
- mod->Handle = (void *) lib;
- mod->Driver = drv;
-
- return EGL_TRUE;
-}
-
-
-/**
- * Unload a module.
- */
-static void
-_eglUnloadModule(_EGLModule *mod)
-{
- /* destroy the driver */
- if (mod->Driver && mod->Driver->Unload)
- mod->Driver->Unload(mod->Driver);
- if (mod->Handle)
- close_library(mod->Handle);
-
- mod->Driver = NULL;
- mod->Handle = NULL;
-}
-
-
-/**
- * Add a module to the module array.
- */
-static _EGLModule *
-_eglAddModule(const char *path)
-{
- _EGLModule *mod;
- EGLint i;
-
- if (!_eglModules) {
- _eglModules = _eglCreateArray("Module", 8);
- if (!_eglModules)
- return NULL;
- }
-
- /* find duplicates */
- for (i = 0; i < _eglModules->Size; i++) {
- mod = _eglModules->Elements[i];
- if (strcmp(mod->Path, path) == 0)
- return mod;
- }
-
- /* allocate a new one */
- mod = calloc(1, sizeof(*mod));
- if (mod) {
- mod->Path = _eglstrdup(path);
- if (!mod->Path) {
- free(mod);
- mod = NULL;
- }
- }
- if (mod) {
- _eglAppendArray(_eglModules, (void *) mod);
- _eglLog(_EGL_DEBUG, "added %s to module array", mod->Path);
- }
-
- return mod;
-}
-
-
-/**
- * Free a module.
- */
-static void
-_eglFreeModule(void *module)
-{
- _EGLModule *mod = (_EGLModule *) module;
-
- _eglUnloadModule(mod);
- free(mod->Path);
- free(mod);
-}
-
-
-/**
- * A loader function for use with _eglPreloadForEach. The loader data is the
- * filename of the driver. This function stops on the first valid driver.
- */
-static EGLBoolean
-_eglLoaderFile(const char *dir, size_t len, void *loader_data)
-{
- char path[1024];
- const char *filename = (const char *) loader_data;
- size_t flen = strlen(filename);
-
- /* make a full path */
- if (len + flen + 2 > sizeof(path))
- return EGL_TRUE;
- if (len) {
- memcpy(path, dir, len);
- path[len++] = '/';
- }
- memcpy(path + len, filename, flen);
- len += flen;
- path[len] = '\0';
-
- if (library_suffix()) {
- const char *suffix = library_suffix();
- size_t slen = strlen(suffix);
- const char *p;
- EGLBoolean need_suffix;
-
- p = filename + flen - slen;
- need_suffix = (p < filename || strcmp(p, suffix) != 0);
- if (need_suffix) {
- /* overflow */
- if (len + slen + 1 > sizeof(path))
- return EGL_TRUE;
- strcpy(path + len, suffix);
- }
- }
-
-#if defined(_EGL_OS_UNIX)
- /* check if the file exists */
- if (access(path, F_OK))
- return EGL_TRUE;
-#endif
-
- _eglAddModule(path);
-
- return EGL_TRUE;
-}
-
-
-/**
- * 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 char search_path[1024];
-
-#if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
- if (search_path[0] == '\0') {
- char *buf = search_path;
- size_t len = sizeof(search_path);
- EGLBoolean use_env;
- char dir_sep;
- int ret;
-
-#if defined(_EGL_OS_UNIX)
- use_env = (geteuid() == getuid() && getegid() == getgid());
- dir_sep = '/';
-#else
- use_env = EGL_TRUE;
- dir_sep = '\\';
-#endif
-
- if (use_env) {
- char *p;
-
- /* extract the dirname from EGL_DRIVER */
- p = getenv("EGL_DRIVER");
- if (p && strchr(p, dir_sep)) {
- ret = _eglsnprintf(buf, len, "%s", p);
- if (ret > 0 && ret < len) {
- p = strrchr(buf, dir_sep);
- *p++ = ':';
-
- len -= p - buf;
- buf = p;
- }
- }
-
- /* append EGL_DRIVERS_PATH */
- p = getenv("EGL_DRIVERS_PATH");
- if (p) {
- ret = _eglsnprintf(buf, len, "%s:", p);
- if (ret > 0 && ret < len) {
- buf += ret;
- len -= ret;
- }
- }
- }
- else {
- _eglLog(_EGL_DEBUG,
- "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
- }
-
- ret = _eglsnprintf(buf, len, "%s", _EGL_DRIVER_SEARCH_DIR);
- if (ret < 0 || ret >= len)
- search_path[0] = '\0';
-
- _eglLog(_EGL_DEBUG, "EGL search path is %s", search_path);
- }
-#endif /* defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS) */
-
- 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.