diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2011-06-29 09:07:14 +0100 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2011-06-29 09:07:14 +0100 |
commit | b1702f2ab3ab44b1418315270e727478008a6c62 (patch) | |
tree | 3e76962ee1f9551047e7824eacae83d43893fdba | |
parent | e95232ddab6826dd7ea8306495aa122ea64829c9 (diff) |
Move the common device open into its own routine.
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | demo.c | 74 | ||||
-rw-r--r-- | demo.h | 27 | ||||
-rw-r--r-- | poppler-demo.c | 79 | ||||
-rw-r--r-- | spiral-demo.c | 82 |
5 files changed, 115 insertions, 155 deletions
@@ -1,6 +1,6 @@ -SOURCES:=xlib.c ximage.c +SOURCES:=demo.c xlib.c ximage.c REQUIRES:=cairo-xlib xext gdk-pixbuf-2.0 -DEFINES:=-DHAVE_XLIB=1 -DHAVE_XIMAGE=1 -DHAVE_GLX=1 +DEFINES:=-DHAVE_XLIB=1 -DHAVE_XIMAGE=1 DRM:=0 ifneq ($(DRM),0) @@ -11,7 +11,7 @@ else DEFINES+=-DHAVE_DRM=0 endif -XCB:=1 +XCB:=$(shell pkg-config --exists cairo-xcb && echo 1 || echo 0) ifneq ($(XCB),0) DEFINES+=-DHAVE_XCB=1 SOURCES+=xcb.c @@ -20,7 +20,7 @@ else DEFINES+=-DHAVE_XCB=0 endif -GLX:=1 +GLX:=$(shell pkg-config --exists cairo-gl && echo 1 || echo 0) ifneq ($(GLX),0) DEFINES+=-DHAVE_GLX=1 SOURCES+=glx.c @@ -0,0 +1,74 @@ +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +#include "demo.h" + +struct device *device_open(int argc, char **argv) +{ + struct device *device = 0; + enum { + AUTO, + XLIB, + XIMAGE, + XCB, + GLX, + DRM, + } backend = AUTO; + int n; + + for (n = 1; n < argc; n++) { + if (strcmp (argv[n], "--xlib") == 0) { + backend = XLIB; + } else if (strcmp (argv[n], "--xcb") == 0) { + backend = XCB; + } else if (strcmp (argv[n], "--ximage") == 0) { + backend = XIMAGE; + } else if (strcmp (argv[n], "--drm") == 0) { + backend = DRM; + } else if (strcmp (argv[n], "--glx") == 0) { + backend = GLX; + } + } + + if (backend == AUTO) { + if (device == 0 && HAVE_DRM) + device = drm_open (argc, argv); + if (device == 0 && HAVE_XCB) + device = xcb_open (argc, argv); + if (device == 0 && HAVE_XLIB) + device = xlib_open (argc, argv); + if (device == 0 && HAVE_XLIB) + device = glx_open (argc, argv); + if (device == 0 && HAVE_XIMAGE) + device = ximage_open (argc, argv); + } else switch (backend) { + case AUTO: + case XLIB: + device = xlib_open (argc, argv); + break; + case XCB: + device = xcb_open (argc, argv); + break; + case XIMAGE: + device = ximage_open (argc, argv); + break; + case GLX: + device = glx_open (argc, argv); + break; + case DRM: +#if HAVE_DRM + device = drm_open (argc, argv); +#endif + break; + } + + if (device == 0) { + fprintf(stderr, "Failed to open a drawing device\n"); + exit(1); + } + + printf("Using backend \"%s\"\n", device->name); + return device; +} + @@ -25,9 +25,34 @@ struct slide { void (*draw) (struct slide *, cairo_t *, int, int); }; +struct device *device_open(int argc, char **argv); +#if HAVE_GLX +struct device *glx_open (int argc, char **argv); +#else +static inline struct device *glx_open (int argc, char **argv) { return 0; } +#endif + +#if HAVE_XCB struct device *xcb_open (int argc, char **argv); +#else +static inline struct device *xcb_open (int argc, char **argv) { return 0; } +#endif + +#if HAVE_XLIB struct device *xlib_open (int argc, char **argv); +#else +static inline struct device *xlib_open (int argc, char **argv) { return 0; } +#endif + +#if HAVE_XIMAGE struct device *ximage_open (int argc, char **argv); -struct device *glx_open (int argc, char **argv); +#else +static inline struct device *ximage_open (int argc, char **argv) { return 0; } +#endif + +#if HAVE_DRM struct device *drm_open (int argc, char **argv); +#else +static inline struct device *drm_open (int argc, char **argv) { return 0; } +#endif diff --git a/poppler-demo.c b/poppler-demo.c index 5f054db..31d03ad 100644 --- a/poppler-demo.c +++ b/poppler-demo.c @@ -43,44 +43,28 @@ main (int argc, char **argv) { struct device *device; struct timeval last, now; - enum { - AUTO, - XLIB, - XIMAGE, - XCB, - GLX, - DRM, - } backend = AUTO; const char *filename = NULL; PopplerDocument *document; gchar *absolute, *uri; GError *error = NULL; int n, num_pages; - g_type_init (); + device = device_open(argc, argv); for (n = 1; n < argc; n++) { - if (strcmp (argv[n], "--xlib") == 0) { - backend = XLIB; - } else if (strcmp (argv[n], "--xcb") == 0) { - backend = XCB; - } else if (strcmp (argv[n], "--ximage") == 0) { - backend = XIMAGE; - } else if (strcmp (argv[n], "--glx") == 0) { - backend = GLX; - } else if (strcmp (argv[n], "--drm") == 0) { - backend = DRM; - } else if (strcmp (argv[n], "--filename") == 0) { + if (strcmp (argv[n], "--filename") == 0) { filename = argv[n+1]; n++; } } - if (filename == NULL) { fprintf (stderr, "Please use --filename <filename> to specify the PDF file to render\n"); return 1; } + + g_type_init (); + if (g_path_is_absolute (filename)) { absolute = g_strdup (filename); } else { @@ -101,59 +85,6 @@ main (int argc, char **argv) num_pages = poppler_document_get_n_pages (document); - if (backend == AUTO) { - device = NULL; - if (device == NULL && HAVE_DRM) { - device = drm_open (argc, argv); - } - if (device == NULL && HAVE_XCB) - device = xcb_open (argc, argv); - if (device == NULL && HAVE_XLIB) - device = xlib_open (argc, argv); - if (device == NULL && HAVE_XIMAGE) - device = ximage_open (argc, argv); - if (device == NULL && HAVE_GLX) - device = glx_open (argc, argv); - if (device == NULL) { - fprintf (stderr, "Failed to open output device.\n"); - return 1; - } - } else { - switch (backend) { - case AUTO: - case XLIB: -#if HAVE_XLIB - device = xlib_open (argc, argv); -#endif - break; - case XCB: -#if HAVE_XCB - device = xcb_open (argc, argv); -#endif - break; - case XIMAGE: -#if HAVE_XIMAGE - device = ximage_open (argc, argv); -#endif - break; - case DRM: -#if HAVE_DRM - device = drm_open (argc, argv); -#endif - break; - case GLX: -#if HAVE_GLX - device = glx_open (argc, argv); -#endif - break; - } - - if (device == NULL) { - fprintf (stderr, "Failed to open backend device\n"); - return 1; - } - } - n = 0; gettimeofday (&last, NULL); while (1) { diff --git a/spiral-demo.c b/spiral-demo.c index 5aaa5c1..f1fa0c5 100644 --- a/spiral-demo.c +++ b/spiral-demo.c @@ -174,89 +174,19 @@ static void load_sources(const char *path, cairo_surface_t *target) int main(int argc, char **argv) { struct device *device; - enum { - AUTO, - XLIB, - XIMAGE, - XCB, - GLX, - DRM, - } backend = AUTO; const char *path = "/usr/share/backgrounds"; float sincos_lut[360]; struct timeval start, last, now; int theta, frames, n; - for (n = 1; n < argc; n++) { - if (strcmp (argv[n], "--xlib") == 0) { - backend = XLIB; - } else if (strcmp (argv[n], "--xcb") == 0) { - backend = XCB; - } else if (strcmp (argv[n], "--ximage") == 0) { - backend = XIMAGE; - } else if (strcmp (argv[n], "--drm") == 0) { - backend = DRM; - } else if (strcmp (argv[n], "--glx") == 0) { - backend = GLX; - } else if (strcmp (argv[n], "--images") == 0) { - path = argv[n+1]; - n++; - } - } - - if (backend == AUTO) { - device = NULL; - if (device == NULL && HAVE_DRM) { - device = drm_open (argc, argv); - } - if (device == NULL && HAVE_XCB) - device = xcb_open (argc, argv); - if (device == NULL && HAVE_XLIB) - device = xlib_open (argc, argv); - if (device == NULL && HAVE_XLIB) - device = glx_open (argc, argv); - if (device == NULL && HAVE_XIMAGE) - device = ximage_open (argc, argv); - if (device == NULL) { - fprintf (stderr, "Failed to open output device.\n"); - return 1; - } - } else { - switch (backend) { - case AUTO: - case XLIB: -#if HAVE_XLIB - device = xlib_open (argc, argv); -#endif - break; - case XCB: -#if HAVE_XCB - device = xcb_open (argc, argv); -#endif - break; - case XIMAGE: -#if HAVE_XIMAGE - device = ximage_open (argc, argv); -#endif - break; - case GLX: -#if HAVE_GLX - device = glx_open (argc, argv); -#endif - break; - case DRM: -#if HAVE_DRM - device = drm_open (argc, argv); -#endif - break; - } + device = device_open(argc, argv); - if (device == NULL) { - fprintf (stderr, "Failed to open backend device\n"); - return 1; - } + for (n = 1; n < argc; n++) { + if (strcmp (argv[n], "--images") == 0) { + path = argv[n+1]; + n++; + } } - printf("Using backend \"%s\"\n", device->name); g_type_init(); |