$OpenBSD: patch-cups_gdevcups_c,v 1.5 2011/07/06 13:49:17 kili Exp $
--- cups/gdevcups.c.orig	Mon Mar  7 16:40:13 2011
+++ cups/gdevcups.c	Tue Jun 28 13:37:01 2011
@@ -70,6 +70,7 @@
 #include "gsparam.h"
 #include "arch.h"
 
+#include <dlfcn.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <cups/raster.h>
@@ -227,6 +228,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 =
@@ -425,7 +435,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 */
 };
 
 /*
@@ -458,7 +475,7 @@ cups_close(gx_device *pdev)		/* I - Device info */
 
   if (cups->stream != NULL)
   {
-    cupsRasterClose(cups->stream);
+    cups->cupsRasterClose(cups->stream);
     close(fileno(cups->file));
     cups->stream = NULL;
     cups->file = NULL;
@@ -2687,6 +2704,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.4", 0))) {
+      eprintf1("ERROR: libcupsimage.so.4: %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;
 
@@ -2702,7 +2752,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);
 }
@@ -2799,7 +2849,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);
@@ -2812,7 +2862,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)
@@ -2843,7 +2893,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);
@@ -3150,7 +3200,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);
@@ -3162,7 +3212,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",
@@ -4074,7 +4124,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);
@@ -4251,7 +4301,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
     {
@@ -4259,7 +4309,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);
@@ -4301,7 +4351,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);
@@ -4972,7 +5022,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);
 }
@@ -5344,7 +5394,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);
 }
