Fix for https://freedesktop.org/bugzilla/show_bug.cgi?id=1518 - Add filter support... 91/9391/1
authorRoland Mainz <roland.mainz@nrubsig.org>
Wed, 6 Oct 2004 19:29:59 +0000 (19:29 +0000)
committerSung-Jin Park <sj76.park@samsung.com>
Tue, 3 Sep 2013 04:09:48 +0000 (00:09 -0400)
Change-Id: Ic50bc0261cfd0645b9ed6d3064f8f0afb0cc4706

dsimple.c
dsimple.h

index 8cdce02..c84ac3b 100644 (file)
--- a/dsimple.c
+++ b/dsimple.c
@@ -41,6 +41,9 @@ from The Open Group.
  * Written by Mark Lillibridge.   Last updated 7/1/87
  */
 
+#ifdef BUILD_PRINTSUPPORT
+#include <X11/XprintUtil/xprintutil.h>
+#endif /* BUILD_PRINTSUPPORT */
 #include "dsimple.h"
 
 /*
@@ -55,9 +58,13 @@ from The Open Group.
 
 
 /* This stuff is defined in the calling program by just_display.h */
-char *program_name = "unknown_program";
-Display *dpy;
-int screen;
+char    *program_name = "unknown_program";
+Display *dpy = NULL;
+int      screen = 0;
+Bool     printer_output = False; /* Video or printer output ? */
+#ifdef BUILD_PRINTSUPPORT
+XPContext pcontext = None;
+#endif /* BUILD_PRINTSUPPORT */
 
 static void _bitmap_error(int, char *);
 
@@ -131,6 +138,43 @@ char *Get_Display_Name(pargc, argv)
 }
 
 
+#ifdef BUILD_PRINTSUPPORT
+/*
+ * Get_Printer_Name (argc, argv) Look for -printer, -p,
+ * If found, remove it from command line.  Don't go past a lone -.
+ */
+char *Get_Printer_Name(pargc, argv)
+    int *pargc;  /* MODIFIED */
+    char **argv; /* MODIFIED */
+{
+    int argc = *pargc;
+    char **pargv = argv+1;
+    char *printername = NULL;
+    int i;
+
+    for (i = 1; i < argc; i++) {
+       char *arg = argv[i];
+
+       if (!strcmp (arg, "-printer") || !strcmp (arg, "-p")) {
+           if (++i >= argc) usage ();
+
+           printername = argv[i];
+           *pargc -= 2;
+           continue;
+       }
+       if (!strcmp(arg,"-")) {
+               while (i<argc)
+                       *pargv++ = argv[i++];
+               break;
+       }
+       *pargv++ = arg;
+    }
+
+    *pargv = NULL;
+    return (printername);
+}
+#endif /* BUILD_PRINTSUPPORT */
+
 /*
  * Open_Display: Routine to open a display with correct error handling.
  *               Does not require dpy or screen defined on entry.
@@ -163,8 +207,60 @@ void Setup_Display_And_Screen(argc, argv)
 int *argc;      /* MODIFIED */
 char **argv;    /* MODIFIED */
 {
-       dpy = Open_Display (Get_Display_Name(argc, argv));
-       screen = DefaultScreen(dpy);
+        char *displayname = NULL,
+             *printername = NULL;
+        
+        displayname = Get_Display_Name(argc, argv);
+#ifdef BUILD_PRINTSUPPORT
+        printername = Get_Printer_Name(argc, argv);
+        
+        if (displayname && printername) {
+           fprintf (stderr, "%s:  you cannot specify -printer (-p) and -display (-d) at the same time.\n",
+                    program_name);
+           usage ();
+        }
+
+        if (printername) {
+            printer_output = True;
+            
+            if (XpuGetPrinter(printername, &dpy, &pcontext) != 1) {
+                fprintf(stderr, "%s: Cannot open printer '%s'.\n", program_name, printername);
+                exit(EXIT_FAILURE);
+            }
+
+            screen = XScreenNumberOfScreen(XpGetScreenOfContext(dpy, pcontext));
+        }
+        else
+#endif /* BUILD_PRINTSUPPORT */
+        {
+            printer_output = False;
+
+           dpy = Open_Display (displayname);
+           screen = XDefaultScreen(dpy);
+        }
+}
+
+/*
+ * Close_Display: Close display
+ */
+void Close_Display(void)
+{
+    if (dpy == NULL)
+      return;
+      
+#ifdef BUILD_PRINTSUPPORT
+    if (printer_output) {
+        XpuClosePrinterDisplay(dpy, pcontext);
+        dpy            = NULL;
+        pcontext       = None;
+        printer_output = False;
+    }
+    else
+#endif /* BUILD_PRINTSUPPORT */
+    {
+        XCloseDisplay(dpy);
+        dpy = NULL;
+    }
 }
 
 
@@ -517,5 +613,6 @@ void Fatal_Error(char *msg, ...)
        vfprintf(stderr, msg, args);
        va_end(args);
        fprintf(stderr, "\n");
-       exit(1);
+        Close_Display();
+       exit(EXIT_FAILURE);
 }
index ae520aa..d4dbd01 100644 (file)
--- a/dsimple.h
+++ b/dsimple.h
@@ -39,11 +39,23 @@ from The Open Group.
  * Send bugs, etc. to chariot@athena.mit.edu.
  */
 
+    /* Simple helper macros */
+#ifndef MAX
+#define MAX(a,b) (((a)>(b))?(a):(b))
+#endif /* MAX */
+#ifndef MIN
+#define MIN(a,b) (((a)<(b))?(a):(b))
+#endif /* MIN */
+
     /* Global variables used by routines in just_display.c */
 
 extern char *program_name;                   /* Name of this program */
 extern Display *dpy;                         /* The current display */
 extern int screen;                           /* The current screen */
+extern Bool printer_output;                  /* Video or printer output ? */
+#ifdef BUILD_PRINTSUPPORT
+extern XPContext pcontext;                   /* The current print context */
+#endif /* BUILD_PRINTSUPPORT */
 
 #define INIT_NAME program_name=argv[0]        /* use this in main to setup
                                                  program_name */
@@ -53,8 +65,12 @@ extern int screen;                           /* The current screen */
 char *Malloc(unsigned);
 char *Realloc(char *, int);
 char *Get_Display_Name(int *, char **);
+#ifdef BUILD_PRINTSUPPORT
+char *Get_Printer_Name(int *, char **);
+#endif /* BUILD_PRINTSUPPORT */
 Display *Open_Display(char *);
 void Setup_Display_And_Screen(int *, char **);
+void Close_Display(void);
 XFontStruct *Open_Font(char *);
 void Beep(void);
 Pixmap ReadBitmapFile(Drawable, char *, int *, int *, int *, int *);