[Xprint] Application printing using Xprint

Martin Deppe Martin.Deppe at web.de
Sun Apr 24 00:02:46 EDT 2005


Dear Listmembers,

I just subscribed to this list because I got xprint knowing a few days 
ago and tried to create little program using xprint to print something 
from inside my application.

I have been looking for the documentation of the client functions and 
some sample source code. I found something, but nothing that really 
helped me to finish my little applictation successfully.

I hope my "problem" is very easy to answer and the answer is as easy as 
the question. Maybe someone can point me to some sample source code in 
which a connection to the X print server is being established, some more 
initializations are done, a job is being started, a page is being 
opened, some output (such as text and - more important - graphics, such 
as lines and points) is being done and then everything that has been 
started is being completed so that the data can be show on my printer.

What I found were some sources that needed GL-something, xpu-something, 
xpt-something or whatever, which I didn't have and didn't find. The 
xprintutils, which I downloaded didn't compile so that I couldn't use 
them as a blueprint for my own application.

Here is the code I wanted to use to print some lines on the printer:
-----------------------------------------------------------------------------------------------------------------------------
void printDrawing(void)
{
   Display    *pdsp;
   Window      pwin;
   Screen     *pScreen;
   XPContext   pContext;
   XGCValues values;
   unsigned long valuemask = 0;
   GC          gc;
   XRectangle  rect;
   unsigned short width, height;
   int rc;

   if (!printingAvailable)
   {
      chkPrinter(0);
      return;
   }

   pdsp = XOpenDisplay(":33");
   if (pdsp == NULL)
   {
      fprintf(stderr, "could not connect to X printserver (again)\n");
      return;
   }

   pContext = XpCreateContext(pdsp, prtName);
   fprintf(stderr, "pContext=%d\n", pContext);
   XpSetContext(pdsp, pContext); // void XpSetContext()
   gc = XCreateGC(pdsp, pwin, valuemask, &values);
   fprintf(stderr, "XCreateGC()=%d\n", gc); // debug output
   if (gc < 0)
      fprintf(stderr, "XCreateGC failed - cannot process print request\n");
   else
   {
      XpStartJob(pdsp, XPSpool); // void XpStartJob()
      fprintf(stderr, "XpStartJob()\n"); // debug output

      // Generate the first page
      pScreen = XpGetScreenOfContext(pdsp, pContext);
      fprintf(stderr, "XpGetScreen()=%d\n", pScreen); // debug output
      fprintf(stderr, "XpGetPageDimensions()=%d\n", 
XpGetPageDimensions(pdsp, pContext, &width, &height, &rect)); // debug 
output included
      pwin = XCreateSimpleWindow(pdsp, RootWindowOfScreen(pScreen),
                                 rect.x, rect.y, rect.width, rect.height, 2,
                                 BlackPixelOfScreen(pScreen),
                                 WhitePixelOfScreen(pScreen));
      fprintf(stderr, "pwin=%d\n", pwin); // debug output

      XpStartPage(pdsp, pwin);
      fprintf(stderr, "XDrawLine=%d\n", XDrawLine(pdsp, pwin, gc, 20, 
20, 120, 120));
      fprintf(stderr, "XDrawLine=%d\n", XDrawLine(pdsp, pwin, gc, 120, 
120, 20, 20));
      fprintf(stderr, "XDrawLine=%d\n", XDrawLine(pdsp, pwin, gc, 20, 
20, 120, 20));
      fprintf(stderr, "XDrawLine=%d\n", XDrawLine(pdsp, pwin, gc, 20, 
120, 120, 120));

      XpEndPage(pdsp);
      XFreeGC(pdsp, gc); /* free the GC */

      // End the print job - the final results are sent by the X print 
server to the spooler sub system.
      XpEndJob(pdsp);
   }
   XpDestroyContext(pdsp, pContext);
   XCloseDisplay(pdsp);
}
-----------------------------------------------------------------------------------------------------------------------------

I hope that the code is not to confusing and someone can tell me, why I 
get some response from the printer, but no page out of it.

I could connect to the X print server and get a list of my available 
printers. I could start a job and a page, but I didn't get anything else 
out of the printer than the initialization of it, after which it simply 
stopped doing anything again, as if I wouldn't have done anything.

The debug output of this code was:
-----------------------------------------------------------------------------------------------------------------------------
pContext=2097153
XCreateGC()=134588832
1: X-Server Error code: BadDrawable (invalid Pixmap or Window parameter)
XpStartJob()
XpGetScreen()=134588368
XpGetPageDimensions()=1
pwin=2097155
XDrawLine=1
XDrawLine=1
XDrawLine=1
XDrawLine=1
2: X-Server Error code: BadGC (invalid GC parameter)
3: X-Server Error code: BadGC (invalid GC parameter)
-----------------------------------------------------------------------------------------------------------------------------

Thank you in advance
Martin



More information about the Xprint mailing list