- * 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 EGLBoolean
-_eglAddUserDriver(void)
-{
- const char *search_path = _eglGetSearchPath();
- char *env;
- size_t name_len = 0;
-
- 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;
- }
- }
- else if (env) {
- char *suffix = strchr(env, '.');
- name_len = (suffix) ? suffix - env : strlen(env);
- }
-#else
- if (env)
- name_len = strlen(env);
-#endif /* _EGL_OS_UNIX */
-
- /*
- * Try built-in drivers first if we know the driver name. This makes sure
- * we do not load the outdated external driver that is still on the
- * filesystem.
- */
- if (name_len) {
- _EGLModule *mod;
- EGLint i;
-
- for (i = 0; _eglBuiltInDrivers[i].name; i++) {
- if (strlen(_eglBuiltInDrivers[i].name) == name_len &&
- !strncmp(_eglBuiltInDrivers[i].name, env, name_len)) {
- mod = _eglAddModule(env);
- if (mod)
- mod->BuiltIn = _eglBuiltInDrivers[i].main;
-
- return EGL_TRUE;
- }
- }
- }
-
- /* otherwise, treat env as a path */
- if (env) {
- _eglPreloadForEach(search_path, _eglLoaderFile, (void *) env);
-
- return EGL_TRUE;
- }
-
- return EGL_FALSE;
-}
-
-
-/**
- * Add egl_gallium to the module array.
- */
-static void
-_eglAddGalliumDriver(void)
-{
-#ifndef _EGL_BUILT_IN_DRIVER_GALLIUM
- void *external = (void *) "egl_gallium";
- _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderFile, external);
-#endif
-}
-
-
-/**
- * Add built-in drivers to the module array.
- */
-static void
-_eglAddBuiltInDrivers(void)
-{
- _EGLModule *mod;
- EGLint i;
-
- for (i = 0; _eglBuiltInDrivers[i].name; i++) {
- mod = _eglAddModule(_eglBuiltInDrivers[i].name);
- if (mod)
- mod->BuiltIn = _eglBuiltInDrivers[i].main;
- }
-}
-
-
-/**
- * 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;
-
- if (!_eglAddUserDriver()) {
- /*
- * Add other drivers only when EGL_DRIVER is not set. The order here
- * decides the priorities.
- */
- _eglAddGalliumDriver();
- _eglAddBuiltInDrivers();
- }
-
- return (_eglModules != NULL);
-}
-
-
-/**
- * A helper function for _eglMatchDriver. It finds the first driver that can
- * initialize the display and return.
- */
-static _EGLDriver *
-_eglMatchAndInitialize(_EGLDisplay *dpy)
-{
- _EGLDriver *drv = NULL;
- EGLint i = 0;
-
- if (!_eglAddDrivers()) {
- _eglLog(_EGL_WARNING, "failed to find any driver");
- return NULL;
- }
-
- if (dpy->Driver) {
- drv = dpy->Driver;
- /* no re-matching? */
- if (!drv->API.Initialize(drv, dpy))
- drv = NULL;
- return drv;
- }
-
- while (i < _eglModules->Size) {
- _EGLModule *mod = (_EGLModule *) _eglModules->Elements[i];
-
- if (!_eglLoadModule(mod)) {
- /* remove invalid modules */
- _eglEraseArray(_eglModules, i, _eglFreeModule);
- continue;
- }
-
- if (mod->Driver->API.Initialize(mod->Driver, dpy)) {
- drv = mod->Driver;
- break;
- }
- else {
- i++;
- }
- }
-
- return drv;
-}
-
-
-/**
- * Match a display to a driver. The display is initialized unless test_only is
- * true. The matching is done by finding the first driver that can initialize
- * the display.