$OpenBSD: patch-cups_gdevcups_c,v 1.7 2011/11/16 12:43:23 kili Exp $
--- cups/gdevcups.c.orig	Fri Aug  5 13:12:21 2011
+++ cups/gdevcups.c	Mon Nov 14 14:25:31 2011
@@ -71,6 +71,7 @@
 #include "arch.h"
 #include "gsicc_manage.h"
 
+#include <dlfcn.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <cups/raster.h>
@@ -228,6 +229,15 @@ typedef struct gx_device_cups_s
   int                   cupsRasterVersion;
 
   /* Used by cups_put_params(): */
+
+  /* cups stubs, initialized in cups_open() */
+  int libCupsReady;
+  void (*cupsRasterClose) (cups_raster_t *);
+  cups_raster_t *(*cupsRasterOpen) (int, cups_mode_t);
+  unsigned (*cupsRasterWriteHeader2) (cups_raster_t *, cups_page_header2_t *);
+  unsigned (*cupsRasterWritePixels) (cups_raster_t *, unsigned char *, unsigned);
+  ppd_attr_t *(*ppdFindAttr) (ppd_file_t *, const char *, const char *);
+  ppd_file_t *(*ppdOpenFile) (const char *);
 } gx_device_cups;
 
 private gx_device_procs	cups_procs =
@@ -426,7 +436,14 @@ gx_device_cups	gs_cups_device =
   {0x00},                                  /* EncodeLUT */
   {0x00},                                  /* Density */
   {0x00},                                  /* Matrix */
-  3                                     /* cupsRasterVersion */
+  3,                                    /* cupsRasterVersion */
+  0,					/* libCupsReady */
+  NULL,					/* cupsRasterClose */
+  NULL,					/* cupsRasterOpen */
+  NULL,					/* cupsRasterWriteHeader2 */
+  NULL,					/* cupsRasterWritePixels */
+  NULL,					/* ppdFindAttr */
+  NULL					/* ppdOpenFile */
 };
 
 /*
@@ -459,7 +476,7 @@ cups_close(gx_device *pdev)		/* I - Device info */
 
   if (cups->stream != NULL)
   {
-    cupsRasterClose(cups->stream);
+    cups->cupsRasterClose(cups->stream);
     cups->stream = NULL;
   }
 
@@ -2699,6 +2716,39 @@ cups_open(gx_device *pdev)		/* I - Device info */
   dprintf1("DEBUG2: cups_open(%p)\n", pdev);
 #endif /* DEBUG */
 
+  if (!cups->libCupsReady) {
+    void *handle;
+    if (!(handle = dlopen("libcupsimage.so.5", 0))) {
+      eprintf1("ERROR: libcupsimage.so.5: %s\n", dlerror());
+      return -1;
+    }
+    if (!(cups->cupsRasterClose = dlsym(handle, "cupsRasterClose"))) {
+      eprintf1("ERROR: cupsRasterClose: %s\n", dlerror());
+      return -1;
+    }
+    if (!(cups->cupsRasterOpen = dlsym(handle, "cupsRasterOpen"))) {
+      eprintf1("ERROR: cupsRasterOpen: %s\n", dlerror());
+      return -1;
+    }
+    if (!(cups->cupsRasterWriteHeader2 = dlsym(handle, "cupsRasterWriteHeader2"))) {
+      eprintf1("ERROR: cupsRasterWriteHeader2: %s\n", dlerror());
+      return -1;
+    }
+    if (!(cups->cupsRasterWritePixels = dlsym(handle, "cupsRasterWritePixels"))) {
+      eprintf1("ERROR: cupsRasterWritePixels: %s\n", dlerror());
+      return -1;
+    }
+    if (!(cups->ppdFindAttr = dlsym(handle, "ppdFindAttr"))) {
+      eprintf1("ERROR: ppdFindAttr: %s\n", dlerror());
+      return -1;
+    }
+    if (!(cups->ppdOpenFile = dlsym(handle, "ppdOpenFile"))) {
+      eprintf1("ERROR: ppdOpenFile: %s\n", dlerror());
+      return -1;
+    }
+    cups->libCupsReady = 1;
+  }
+
   dprintf("INFO: Start rendering...\n");
   cups->printer_procs.get_space_params = cups_get_space_params;
 
@@ -2714,7 +2764,7 @@ cups_open(gx_device *pdev)		/* I - Device info */
     return (code);
 
   if (cups->PPD == NULL)
-    cups->PPD = ppdOpenFile(getenv("PPD"));
+    cups->PPD = cups->ppdOpenFile(getenv("PPD"));
 
   return (0);
 }
@@ -2811,7 +2861,7 @@ cups_print_pages(gx_device_printer *pdev,
 
   if (cups->stream == NULL)
   {
-    RasterVersion = ppdFindAttr(cups->PPD, "cupsRasterVersion", NULL); 
+    RasterVersion = cups->ppdFindAttr(cups->PPD, "cupsRasterVersion", NULL); 
     if (RasterVersion) {
 #ifdef DEBUG
       dprintf1("DEBUG2: cupsRasterVersion = %s\n", RasterVersion->value);
@@ -2824,7 +2874,7 @@ cups_print_pages(gx_device_printer *pdev,
 	return_error(gs_error_unknownerror);
       }
     }
-    if ((cups->stream = cupsRasterOpen(fileno(cups->file),
+    if ((cups->stream = cups->cupsRasterOpen(fileno(cups->file),
                                        (cups->cupsRasterVersion == 3 ?
 					CUPS_RASTER_WRITE :
 					CUPS_RASTER_WRITE_COMPRESSED))) == NULL)
@@ -2855,7 +2905,7 @@ cups_print_pages(gx_device_printer *pdev,
 
   for (copy = num_copies; copy > 0; copy --)
   {
-    cupsRasterWriteHeader(cups->stream, &(cups->header));
+    cups->cupsRasterWriteHeader(cups->stream, &(cups->header));
 
     if (pdev->color_info.num_components == 1)
       code = cups_print_chunked(pdev, src, dst, srcbytes);
@@ -3168,7 +3218,7 @@ cups_put_params(gx_device     *pdev,	/* I - Device inf
       dprintf1("DEBUG2: cups->PPD = %p\n", cups->PPD);
 #endif /* DEBUG */
 
-      backside = ppdFindAttr(cups->PPD, "cupsBackSide", NULL); 
+      backside = cups->ppdFindAttr(cups->PPD, "cupsBackSide", NULL); 
       if (backside) {
 #ifdef DEBUG
 	dprintf1("DEBUG2: cupsBackSide = %s\n", backside->value);
@@ -3180,7 +3230,7 @@ cups_put_params(gx_device     *pdev,	/* I - Device inf
 #endif /* DEBUG */
 
       backsiderequiresflippedmargins =
-	ppdFindAttr(cups->PPD, "APDuplexRequiresFlippedMargin", NULL);
+	cups->ppdFindAttr(cups->PPD, "APDuplexRequiresFlippedMargin", NULL);
 #ifdef DEBUG
       if (backsiderequiresflippedmargins)
 	dprintf1("DEBUG2: APDuplexRequiresFlippedMargin = %s\n",
@@ -4107,7 +4157,7 @@ cups_print_chunked(gx_device_printer *pdev,
 #endif /* DEBUG */
 
   if (cups->PPD) {
-    backside = ppdFindAttr(cups->PPD, "cupsBackSide", NULL);
+    backside = cups->ppdFindAttr(cups->PPD, "cupsBackSide", NULL);
     if (backside) {
 #ifdef DEBUG
       dprintf1("DEBUG2: cupsBackSide = %s\n", backside->value);
@@ -4284,7 +4334,7 @@ cups_print_chunked(gx_device_printer *pdev,
       * Write the bitmap data to the raster stream...
       */
 
-      cupsRasterWritePixels(cups->stream, dst, cups->header.cupsBytesPerLine);
+      cups->cupsRasterWritePixels(cups->stream, dst, cups->header.cupsBytesPerLine);
     }
     else
     {
@@ -4292,7 +4342,7 @@ cups_print_chunked(gx_device_printer *pdev,
       * Write the scanline data to the raster stream...
       */
 
-      cupsRasterWritePixels(cups->stream, srcptr, cups->header.cupsBytesPerLine);
+      cups->cupsRasterWritePixels(cups->stream, srcptr, cups->header.cupsBytesPerLine);
     }
   }
   return (0);
@@ -4334,7 +4384,7 @@ cups_print_banded(gx_device_printer *pdev,
 #endif /* DEBUG */
 
   if (cups->PPD) {
-    backside = ppdFindAttr(cups->PPD, "cupsBackSide", NULL);
+    backside = cups->ppdFindAttr(cups->PPD, "cupsBackSide", NULL);
     if (backside) {
 #ifdef DEBUG
       dprintf1("DEBUG2: cupsBackSide = %s\n", backside->value);
@@ -5005,7 +5055,7 @@ cups_print_banded(gx_device_printer *pdev,
     * Write the bitmap data to the raster stream...
     */
 
-    cupsRasterWritePixels(cups->stream, dst, cups->header.cupsBytesPerLine);
+    cups->cupsRasterWritePixels(cups->stream, dst, cups->header.cupsBytesPerLine);
   }
   return (0);
 }
@@ -5377,7 +5427,7 @@ cups_print_planar(gx_device_printer *pdev,
       * Write the bitmap data to the raster stream...
       */
 
-      cupsRasterWritePixels(cups->stream, dst, cups->header.cupsBytesPerLine);
+      cups->cupsRasterWritePixels(cups->stream, dst, cups->header.cupsBytesPerLine);
     }
   return (0);
 }
