[Xprint] Application printing using Xprint

Roland Mainz roland.mainz at nrubsig.org
Sun Apr 24 04:29:28 EDT 2005


Roland Mainz wrote:
> Martin Deppe wrote:
> > 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.
[snip]

I have modified your test application to get it working here:
-- snip --
% gcc martin_deppe_xprinttest001.c -L/usr/X11R6/lib -lXp -lXext -lX11
% ./a.out 
pContext=2097153
XpStartJob()
XpGetScreen()=134521544
XpGetPageDimensions()=1
pwin=2097154
XCreateGC()=134522384
XDrawLine=1
XDrawLine=1
XDrawLine=1
XDrawLine=1
-- snip --

Souce code is attached as "martin_deppe_xprinttest001.c" to this
email...
... I hope that helps you a little bit... :)

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) roland.mainz at nrubsig.org
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 7950090
 (;O/ \/ \O;)
-------------- next part --------------

#include <X11/Xlib.h>
#include <X11/extensions/Print.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct IsNotifyEventContext_
{
  int event_base;
  int detail;
} IsNotifyEventContext;

static
Bool IsXpNotifyEvent( Display *pdpy, XEvent *ev, XPointer arg )
{
  Bool match;
  IsNotifyEventContext *context = (IsNotifyEventContext *)arg;
  XPPrintEvent *pev = (XPPrintEvent *)ev;
  
  match = pev->type == (context->event_base+XPPrintNotify) && 
          pev->detail == context->detail;
  return match;
}

void WaitForPrintNotify( Display *pdpy, int xp_event_base, int detail )
{
  XEvent                  dummy;
  IsNotifyEventContext matchcontext;

  matchcontext.event_base = xp_event_base;
  matchcontext.detail     = detail;
  XIfEvent(pdpy, &dummy, IsXpNotifyEvent, (XPointer)&matchcontext);
}      


int main(int ac, char **av)
{
   Display    *pdsp;
   Window      pwin;
   Screen     *pScreen;
   int         xp_event_base, /* XpExtension even base */
               xp_error_base; /* XpExtension error base */

   XPContext   pContext;
   XGCValues values;
   unsigned long valuemask = 0;
   GC          gc;
   XRectangle  rect;
   unsigned short width, height;
   int rc;

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

   if( XpQueryExtension(pdsp, &xp_event_base, &xp_error_base) == False )
   {
     fprintf(stderr, "XpQueryExtension() failed.\n");
     exit(EXIT_FAILURE);
   }

   pContext = XpCreateContext(pdsp, "xp_ps_spooldir_tmp_Xprintjobs");
   fprintf(stderr, "pContext=%d\n", pContext);

   /* Listen to XP(Start|End)(Job|Doc|Page)Notify events).
    * This is mantatory as Xp(Start|End)(Job|Doc|Page) functions are _not_ 
    * syncronous !!
    * Not waiting for such events may cause that subsequent data may be 
    * destroyed/corrupted!!
    */
   XpSelectInput(pdsp, pContext, XPPrintMask);

   XpSetContext(pdsp, pContext); // void XpSetContext()
   XpStartJob(pdsp, XPSpool); // void XpStartJob()
   fprintf(stderr, "XpStartJob()\n"); // debug output
   WaitForPrintNotify(pdsp, xp_event_base, XPStartJobNotify);

   // 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, XRootWindowOfScreen(pScreen),
                              rect.x, rect.y, rect.width, rect.height, 2,
                              XBlackPixelOfScreen(pScreen),
                              XWhitePixelOfScreen(pScreen));
   fprintf(stderr, "pwin=%d\n", pwin); // debug output
   gc = XCreateGC(pdsp, pwin, valuemask, &values);
   fprintf(stderr, "XCreateGC()=%d\n", gc); // debug output

   XpStartPage(pdsp, pwin);
   WaitForPrintNotify(pdsp, xp_event_base, XPStartPageNotify);
   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);
   WaitForPrintNotify(pdsp, xp_event_base, XPEndPageNotify);
   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);
   WaitForPrintNotify(pdsp, xp_event_base, XPEndJobNotify);
   
   XpDestroyContext(pdsp, pContext);
   XCloseDisplay(pdsp);
   
   return EXIT_SUCCESS;
}



More information about the Xprint mailing list