[RFC weston 2/5] libweston: Move weston_load_module here

Quentin Glidic sardemff7+wayland at sardemff7.net
Tue Feb 9 15:14:41 UTC 2016


From: Quentin Glidic <sardemff7+git at sardemff7.net>

Signed-off-by: Quentin Glidic <sardemff7+git at sardemff7.net>
---
 ivi-shell/ivi-layout.c   |  3 ++-
 lib/libweston.c          | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/libweston.h          |  2 ++
 src/compositor-drm.c     |  9 +++++----
 src/compositor-fbdev.c   |  3 ++-
 src/compositor-wayland.c |  5 +++--
 src/compositor-x11.c     |  5 +++--
 src/compositor.c         | 44 --------------------------------------------
 src/compositor.h         |  3 ---
 src/main.c               |  4 ++--
 10 files changed, 66 insertions(+), 59 deletions(-)

diff --git a/ivi-shell/ivi-layout.c b/ivi-shell/ivi-layout.c
index f7c4f27..fe2e47f 100644
--- a/ivi-shell/ivi-layout.c
+++ b/ivi-shell/ivi-layout.c
@@ -60,6 +60,7 @@
 #include <string.h>
 #include <assert.h>
 
+#include "libweston.h"
 #include "compositor.h"
 #include "ivi-layout-export.h"
 #include "ivi-layout-private.h"
@@ -2853,7 +2854,7 @@ load_controller_modules(struct weston_compositor *compositor, const char *module
 		end = strchrnul(p, ',');
 		snprintf(buffer, sizeof buffer, "%.*s", (int)(end - p), p);
 
-		controller_module_init = weston_load_module(buffer, "controller_module_init");
+		controller_module_init = libweston_load_module(buffer, "controller_module_init");
 		if (!controller_module_init)
 			return -1;
 
diff --git a/lib/libweston.c b/lib/libweston.c
index 801d137..da21f8e 100644
--- a/lib/libweston.c
+++ b/lib/libweston.c
@@ -1,3 +1,7 @@
+
+#include <limits.h>
+#include <dlfcn.h>
+
 #include "shared/zalloc.h"
 
 #include "libweston-internal.h"
@@ -22,6 +26,49 @@ libweston_uninit(struct libweston_context *context)
 	free(context);
 }
 
+WL_EXPORT void *
+libweston_load_module(const char *name, const char *entrypoint)
+{
+	const char *builddir = getenv("WESTON_BUILD_DIR");
+	char path[PATH_MAX];
+	void *module, *init;
+
+	if (name == NULL)
+		return NULL;
+
+	if (name[0] != '/') {
+		if (builddir)
+			snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
+		else
+			snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
+	} else {
+		snprintf(path, sizeof path, "%s", name);
+	}
+
+	module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
+	if (module) {
+		weston_log("Module '%s' already loaded\n", path);
+		dlclose(module);
+		return NULL;
+	}
+
+	weston_log("Loading module '%s'\n", path);
+	module = dlopen(path, RTLD_NOW);
+	if (!module) {
+		weston_log("Failed to load module: %s\n", dlerror());
+		return NULL;
+	}
+
+	init = dlsym(module, entrypoint);
+	if (!init) {
+		weston_log("Failed to lookup init function: %s\n", dlerror());
+		dlclose(module);
+		return NULL;
+	}
+
+	return init;
+}
+
 WL_EXPORT int
 libweston_load_backend(struct libweston_context *context, enum libweston_backend preffered)
 {
diff --git a/lib/libweston.h b/lib/libweston.h
index ca98df1..9d1bfc1 100644
--- a/lib/libweston.h
+++ b/lib/libweston.h
@@ -9,6 +9,8 @@ struct libweston_context;
 struct libweston_context *libweston_init(struct weston_compositor *compositor);
 void libweston_uninit(struct libweston_context *context);
 
+void *libweston_load_module(const char *name, const char *entrypoint);
+
 enum libweston_backend {
 	LIBWESTON_BACKEND_NONE = 0,
 };
diff --git a/src/compositor-drm.c b/src/compositor-drm.c
index 8b9882e..04de95e 100644
--- a/src/compositor-drm.c
+++ b/src/compositor-drm.c
@@ -46,6 +46,7 @@
 #include <gbm.h>
 #include <libudev.h>
 
+#include "libweston.h"
 #include "shared/helpers.h"
 #include "shared/timespec-util.h"
 #include "libbacklight.h"
@@ -608,13 +609,13 @@ drm_output_set_gamma(struct weston_output *output_base,
 }
 
 /* Determine the type of vblank synchronization to use for the output.
- * 
+ *
  * The pipe parameter indicates which CRTC is in use.  Knowing this, we
  * can determine which vblank sequence type to use for it.  Traditional
  * cards had only two CRTCs, with CRTC 0 using no special flags, and
  * CRTC 1 using DRM_VBLANK_SECONDARY.  The first bit of the pipe
  * parameter indicates this.
- * 
+ *
  * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
  * 0-31.  If this is non-zero it indicates we're dealing with a
  * multi-gpu situation and we need to calculate the vblank sync
@@ -1525,8 +1526,8 @@ create_gbm_device(int fd)
 {
 	struct gbm_device *gbm;
 
-	gl_renderer = weston_load_module("gl-renderer.so",
-					 "gl_renderer_interface");
+	gl_renderer = libweston_load_module("gl-renderer.so",
+					    "gl_renderer_interface");
 	if (!gl_renderer)
 		return NULL;
 
diff --git a/src/compositor-fbdev.c b/src/compositor-fbdev.c
index 36d7ae0..e46b5cf 100644
--- a/src/compositor-fbdev.c
+++ b/src/compositor-fbdev.c
@@ -42,6 +42,7 @@
 
 #include <libudev.h>
 
+#include "libweston.h"
 #include "shared/helpers.h"
 #include "compositor.h"
 #include "launcher-util.h"
@@ -790,7 +791,7 @@ fbdev_backend_create(struct weston_compositor *compositor, int *argc, char *argv
 		if (pixman_renderer_init(compositor) < 0)
 			goto out_launcher;
 	} else {
-		gl_renderer = weston_load_module("gl-renderer.so",
+		gl_renderer = libweston_load_module("gl-renderer.so",
 						 "gl_renderer_interface");
 		if (!gl_renderer) {
 			weston_log("could not load gl renderer\n");
diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c
index d1c020d..244807a 100644
--- a/src/compositor-wayland.c
+++ b/src/compositor-wayland.c
@@ -39,6 +39,7 @@
 #include <wayland-egl.h>
 #include <wayland-cursor.h>
 
+#include "libweston.h"
 #include "compositor.h"
 #include "gl-renderer.h"
 #include "pixman-renderer.h"
@@ -2240,8 +2241,8 @@ wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
 	b->use_pixman = use_pixman;
 
 	if (!b->use_pixman) {
-		gl_renderer = weston_load_module("gl-renderer.so",
-						 "gl_renderer_interface");
+		gl_renderer = libweston_load_module("gl-renderer.so",
+						    "gl_renderer_interface");
 		if (!gl_renderer)
 			b->use_pixman = 1;
 	}
diff --git a/src/compositor-x11.c b/src/compositor-x11.c
index b70c119..cac96cb 100644
--- a/src/compositor-x11.c
+++ b/src/compositor-x11.c
@@ -49,6 +49,7 @@
 
 #include <xkbcommon/xkbcommon.h>
 
+#include "libweston.h"
 #include "compositor.h"
 #include "gl-renderer.h"
 #include "pixman-renderer.h"
@@ -1552,8 +1553,8 @@ init_gl_renderer(struct x11_backend *b)
 {
 	int ret;
 
-	gl_renderer = weston_load_module("gl-renderer.so",
-					 "gl_renderer_interface");
+	gl_renderer = libweston_load_module("gl-renderer.so",
+					    "gl_renderer_interface");
 	if (!gl_renderer)
 		return -1;
 
diff --git a/src/compositor.c b/src/compositor.c
index 56eefc4..5b698d1 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -44,7 +44,6 @@
 #include <unistd.h>
 #include <math.h>
 #include <linux/input.h>
-#include <dlfcn.h>
 #include <signal.h>
 #include <setjmp.h>
 #include <sys/time.h>
@@ -4964,49 +4963,6 @@ weston_version(int *major, int *minor, int *micro)
 	*micro = WESTON_VERSION_MICRO;
 }
 
-WL_EXPORT void *
-weston_load_module(const char *name, const char *entrypoint)
-{
-	const char *builddir = getenv("WESTON_BUILD_DIR");
-	char path[PATH_MAX];
-	void *module, *init;
-
-	if (name == NULL)
-		return NULL;
-
-	if (name[0] != '/') {
-		if (builddir)
-			snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
-		else
-			snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
-	} else {
-		snprintf(path, sizeof path, "%s", name);
-	}
-
-	module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
-	if (module) {
-		weston_log("Module '%s' already loaded\n", path);
-		dlclose(module);
-		return NULL;
-	}
-
-	weston_log("Loading module '%s'\n", path);
-	module = dlopen(path, RTLD_NOW);
-	if (!module) {
-		weston_log("Failed to load module: %s\n", dlerror());
-		return NULL;
-	}
-
-	init = dlsym(module, entrypoint);
-	if (!init) {
-		weston_log("Failed to lookup init function: %s\n", dlerror());
-		dlclose(module);
-		return NULL;
-	}
-
-	return init;
-}
-
 
 /** Destroys the compositor.
  *
diff --git a/src/compositor.h b/src/compositor.h
index 10dd641..7b6b013 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -1691,9 +1691,6 @@ weston_transformed_region(int width, int height,
 			  int32_t scale,
 			  pixman_region32_t *src, pixman_region32_t *dest);
 
-void *
-weston_load_module(const char *name, const char *entrypoint);
-
 int
 weston_parse_transform(const char *transform, uint32_t *out);
 
diff --git a/src/main.c b/src/main.c
index 922e46e..2425fda 100644
--- a/src/main.c
+++ b/src/main.c
@@ -479,7 +479,7 @@ load_modules(struct weston_compositor *ec, const char *modules,
 	while (*p) {
 		end = strchrnul(p, ',');
 		snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
-		module_init = weston_load_module(buffer, "module_init");
+		module_init = libweston_load_module(buffer, "module_init");
 		if (!module_init)
 			return -1;
 		if (module_init(ec, argc, argv) < 0)
@@ -647,7 +647,7 @@ load_backend_old(struct weston_compositor *compositor, const char *backend,
 			    struct weston_config *config,
 			    struct weston_backend_config *config_base);
 
-	backend_init = weston_load_module(backend, "backend_init");
+	backend_init = libweston_load_module(backend, "backend_init");
 	if (!backend_init)
 		return -1;
 
-- 
2.6.4



More information about the wayland-devel mailing list