File: services.cpp

package info (click to toggle)
hplip 3.21.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 182,104 kB
  • sloc: python: 83,189; ansic: 70,905; cpp: 63,443; sh: 11,445; perl: 4,397; makefile: 934
file content (777 lines) | stat: -rw-r--r-- 19,775 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*****************************************************************************\
    services.cpp : HP Inkjet Server

    Copyright (c) 2001 - 2004, HP Co.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:
    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
    3. Neither the name of the Hewlett-Packard nor the names of its
       contributors may be used to endorse or promote products derived
       from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
\*****************************************************************************/

#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "header.h"
#include "ijs.h"
#include "ijs_server.h"
#include "hpijs.h"
#include "services.h"

#if defined(HAVE_LIBHPIP) && defined(HAVE_DBUS) 
#include <dbus/dbus.h>
#define DBUS_INTERFACE "com.hplip.StatusService"
#define DBUS_PATH "/"
static DBusError dbus_err;
static DBusConnection *dbus_conn;
void InitDbus (void);
void SendDbusMessage (const char *dev, const char *printer, int code, 
                      const char *username, const int jobid, const char *title);
#else
void SendDbusMessage (const char *dev, const char *printer, int code, 
                      const char *username, const int jobid, const char *title)
{
}
#endif

int UXServices::InitDuplexBuffer()
{
    /* Free buffer if new page size in middle of print job. */
    if (RastersOnPage)
       delete [] RastersOnPage;
    if (KRastersOnPage)
       delete [] KRastersOnPage;

    /* Calculate duplex page buffer */
    CurrentRaster = ph.height - 1;  /* Height = physical page in pixels */
    RastersOnPage = (BYTE **) new BYTE[(ph.height) * sizeof (BYTE *)];
    KRastersOnPage = (BYTE **) new BYTE[(ph.height) * sizeof (BYTE *)];
    for (int i = 0; i < ph.height; i++)
    {
       RastersOnPage[i] = NULL;
       KRastersOnPage[i] = NULL;
    }
    return 0;
}

int UXServices::SendBackPage()
{
    DRIVER_ERROR    err;
    int i = CurrentRaster+1;

    while (i < ph.height)
    {
       if (KRGB)
       {
          if ((err = pJob->SendRasters(KRastersOnPage[i], RastersOnPage[i])) != NO_ERROR)
            return err;
       }
       else
       {
          if ((err = pJob->SendRasters(RastersOnPage[i])) != NO_ERROR)
            return err;
       }

       if (RastersOnPage[i])
           delete [] RastersOnPage[i];
       if (KRastersOnPage[i])
           delete [] KRastersOnPage[i];
       i++;
    }

    CurrentRaster = ph.height - 1;   /* reset raster index */

    return 0;
}

static unsigned char xmask[] =
{
   0x80,    /* x=0 */
   0x40,    /* 1 */
   0x20,    /* 2 */
   0x10,    /* 3 */
   0x08,    /* 4 */
   0x04,    /* 5 */
   0x02,    /* 6 */
   0x01     /* 7 */
};

int UXServices::ProcessRaster(char *raster, char *k_raster)
{
    if (!((pPC->QueryDuplexMode() == DUPLEXMODE_BOOK) && pPC->RotateImageForBackPage() && BackPage))
    {
       if (KRGB)
          return pJob->SendRasters((unsigned char *)k_raster, (unsigned char *)raster);
       else
          return pJob->SendRasters((unsigned char *)raster);
    }
    else
    {
        if (CurrentRaster < 0)
           return -1;

        BYTE   *new_raster;
        int    new_raster_size;
        int    i,w;

        if (raster == NULL)
        {
           RastersOnPage[CurrentRaster] = NULL;
        }
        else
        {
           new_raster_size = pPC->InputPixelsPerRow() * 3;
           new_raster = new BYTE[new_raster_size];
           if (new_raster == 0)
           {
               BUG("unable to create duplex buffer, size=%d: %m\n", new_raster_size);
               return -1;
           }
           memset(new_raster, 0xFF, new_raster_size);
           RastersOnPage[CurrentRaster] = new_raster;
           BYTE *p = new_raster + new_raster_size - 3;
           for (i = 0; i < new_raster_size; i += 3)
           {
               memcpy (p, raster+i, 3);  /* rotate rgb image */
               p -= 3;
           }
        }

        if (k_raster == NULL)
        {
           KRastersOnPage[CurrentRaster] = NULL;
        }
        else
        {
           new_raster_size = (pPC->InputPixelsPerRow() + 7) >> 3;
           new_raster = new BYTE[new_raster_size];
           if (new_raster == 0)
           {
               BUG("unable to create black duplex buffer, size=%d: %m\n", new_raster_size);
               return -1;
           }
           memset(new_raster, 0, new_raster_size);
           KRastersOnPage[CurrentRaster] = new_raster;
           w = pPC->InputPixelsPerRow();
           for (i=0; i<w; i++)
           {
               if (k_raster[i>>3] & xmask[i&7])
                  new_raster[(w-i)>>3] |= xmask[(w-i)&7];  /* rotate k image */
           }
	   int    k = ((w + 7) / 8) * 8 - w;
	   BYTE   c = 0xff << k;
	   if (k != 0)
	       new_raster[0] = c & k_raster[new_raster_size-1];
        }

        CurrentRaster--;

        return 0;
    }
}


#ifdef HAVE_LIBHPIP

/*
 *  Check models.xml for bi-di flag and also check the
 *  device id string for integrity. Some devices return
 *  device id without some expected fields.
 *
 */
BOOL UXServices::CanDoBiDi ()
{
    char            *hpDev;
    struct hpmud_model_attributes ma;
    char            strDevID[512];

    // Check for CUPS environment

    if ((hpDev = getenv ("DEVICE_URI")) == NULL)
    {
        return FALSE;
    }

    // Check for HP Backend

    if (strncmp (hpDev, "hp:", 3))
    {
        return FALSE;
    }

    // Check io-mode in models.xml for this device

    hpmud_query_model(hpDev, &ma);

    if (ma.prt_mode == HPMUD_UNI_MODE)
    {
        return FALSE;
    }
    if (hpmud_open_device(hpDev, ma.prt_mode, &hpFD) != HPMUD_R_OK)
    {
        return FALSE;
    }
    memset (strDevID, 0, 512);
    if ((ReadDeviceID ((BYTE *) strDevID, 512)) != NO_ERROR)
    {
        return FALSE;
    }

    // Check if this is a laser device
    if (strstr (strDevID, "Laser") || strstr (strDevID, "laser"))
    {
        return TRUE;
    }

    // Check if device id is complete
    if (!(strstr (strDevID, ";S:")) && !(strstr (strDevID, "VSTATUS")))
    {
        return FALSE;
    }
    return TRUE;
}

#else

BOOL UXServices::CanDoBiDi ()
{
    return FALSE;
}

#endif  // HAVE_LIBHPIP

UXServices::UXServices():SystemServices()
{
   constructor_error = NO_ERROR;
   hpFD = -1;

   // instead of InitDeviceComm(), just do...
   IOMode.bDevID = IOMode.bStatus = FALSE;   /* uni-di support is default */

#if 0 // Old code

   /* Check for CUPS environment and HP backend. */
   if ((hpDev = getenv("DEVICE_URI")) != NULL)
   {
      if (strncmp(hpDev, "hp:", 3) == 0)
      {
         hplip_Init();
         hplip_ModelQuery(hpDev, &ma);      /* check io-mode in models.xml for this device */  
         if (ma.prt_mode != UNI_MODE)
         {
            if ((hpFD = hplip_OpenHP(hpDev, &ma)) >= 0)
            {
                InitDeviceComm();            /* lets try bi-di support */
            }
            if(IOMode.bDevID == FALSE)
               BUG("unable to set bi-di for hp backend\n");
         }
      }
   }

#endif // Old code

   if (CanDoBiDi ())
   {
       InitDeviceComm ();
       if (IOMode.bDevID == FALSE)
       {
           BUG ("Unable to set bi-di for hp backend\n");
       }
   }

   Quality = QUALITY_NORMAL;
   MediaType = MEDIA_PLAIN;
   ColorMode = COLOR;
   PenSet = DUMMY_PEN;
   
   RastersOnPage = 0;
   KRastersOnPage = 0;
   pPC = NULL;
   pJob = NULL;
   Duplex = 0;
   Tumble = 0;
   FullBleed = 0;
   FirstRaster = 1;
   MediaPosition = sourceTrayAuto;
   Model = -1;
   strcpy(ph.cs, "sRGB");
   VertAlign = -1;
   DisplayStatus = NODISPLAYSTATUS;
   OutputPath = -1;
   outfp = NULL;
   m_iLogLevel = 0;

   m_pbyPclBuffer      = NULL;
   m_iPclBufferSize    = BUFFER_CHUNK_SIZE;
   m_iCurPclBufferPos  = 0;
   m_iPageCount        = 0;
   m_bSpeedMechEnabled = FALSE;
}

UXServices::~UXServices()
{
   if (m_bSpeedMechEnabled)
   {
       SendLastPage ();
   }

   if (RastersOnPage)
      delete [] RastersOnPage;
   if (KRastersOnPage)
      delete [] KRastersOnPage;
#ifdef HAVE_LIBHPIP
   if (hpFD >= 0)
      hpmud_close_device(hpFD);  
#endif
    if (outfp)
    {
        fclose (outfp);
    }
}

DRIVER_ERROR UXServices::ToDevice(const BYTE * pBuffer, DWORD * Count)
{
    if (OutputPath == -1)
    {
        return IO_ERROR;
    }

   if (m_bSpeedMechEnabled)
   {
       if ((CopyData (pBuffer, *Count)) == 0)
       {
           *Count = 0;
	   return NO_ERROR;
       }
   }

    if (outfp)
    {
        fwrite (pBuffer, 1, *Count, outfp);
	if (!(m_iLogLevel & SEND_TO_PRINTER))
	{
	    *Count = 0;
	    return NO_ERROR;
	}
    }

   /* Write must be not-buffered, don't use streams */
   if (write(OutputPath, pBuffer, *Count) != (ssize_t)*Count) 
   {
      static int cnt=0;
      if (cnt < 5)
      {
	 cnt++;
         BUG("unable to write to output, fd=%d, count=%d: %m\n", OutputPath, *Count);
      }

      if (errno == EPIPE)
	 /* The backend has exited.  There's no recovering from that. */
	 exit (1);

      return IO_ERROR;
   }

   *Count = 0;
   return NO_ERROR;
}

BOOL UXServices::GetStatusInfo (BYTE * bStatReg)
{
#ifdef HAVE_LIBHPIP
   unsigned int s;
   if (hpmud_get_device_status(hpFD, &s) == HPMUD_R_OK)
   {
      *bStatReg = (BYTE)s;
      return TRUE;
   }
#endif
   return FALSE;
}

DRIVER_ERROR UXServices::ReadDeviceID (BYTE * strID, int iSize)
{
#ifdef HAVE_LIBHPIP
   int len;
   hpmud_get_device_id(hpFD, (char *)strID, iSize, &len);
   if (len < 3)
      return IO_ERROR;
#endif
   return NO_ERROR;
}

#ifdef HP_PRINTVIEW
const char *szPJLHeader = "@PJL SET JOBATTR=\"JobAcct7=HPPrintView.exe\"\012";
int UXServices::GetPJLHeaderBuffer (char **szPJLBuffer)
{
    *szPJLBuffer = (char *) szPJLHeader;
    return strlen (szPJLHeader);
}
#endif // HP_PRINTVIEW

BOOL UXServices::GetVerticalAlignmentValue(BYTE* cVertAlignVal)
{
   if (VertAlign == -1)
      return FALSE;

   *cVertAlignVal = (BYTE)VertAlign;
   return TRUE;
}

BOOL UXServices::GetVertAlignFromDevice()
{
#ifdef HAVE_LIBHPIP
   if ((VertAlign = ReadHPVertAlign(hpFD)) == -1)
      return FALSE;
#endif
   return TRUE;
}

void UXServices::DisplayPrinterStatus (DISPLAY_STATUS ePrinterStatus)
{
   DisplayStatus = ePrinterStatus;
}

DRIVER_ERROR UXServices::BusyWait (DWORD msec)
{
   switch (DisplayStatus)
   {
      case DISPLAY_ERROR_TRAP:
      case DISPLAY_COMM_PROBLEM:
      case DISPLAY_PRINTER_NOT_SUPPORTED:
      case DISPLAY_OUT_OF_PAPER:
      case DISPLAY_PHOTOTRAY_MISMATCH:
      case DISPLAY_TOP_COVER_OPEN:
      case DISPLAY_NO_COLOR_PEN:
      case DISPLAY_NO_BLACK_PEN:
      case DISPLAY_NO_PENS:
         BUG("WARNING: printer bi-di error=%d\n", DisplayStatus);
         DisplayStatus = DISPLAY_PRINTING_CANCELED;
         return JOB_CANCELED;   /* bail-out otherwise APDK will wait forever */
      default:
         break;
   }
   return NO_ERROR;
}

const char * UXServices::GetDriverMessage (DRIVER_ERROR err)
{
   const char *p=NULL;

	/* Map driver error to text message. TODO: text needs to be localized. */
   switch(err)
   {
      case(WARN_MODE_MISMATCH):
         p = "printmode mismatch with pen, tray, etc.";
         break;
      case(WARN_LOW_INK_BOTH_PENS):
         p = "both pens have low ink";
         break;
      case(WARN_LOW_INK_BLACK):
         p = "black pen has low ink";
         break;
      case(WARN_LOW_INK_COLOR):
         p = "color pen has low ink";
         break;
      case(WARN_LOW_INK_PHOTO):
         p = "photo pen has low ink";
         break;
      case(WARN_LOW_INK_GREY):
         p = "grey pen has low ink";
         break;
      case(WARN_LOW_INK_BLACK_PHOTO):
         p = "black photo has low ink";
         break;
      case(WARN_LOW_INK_COLOR_PHOTO):
         p = "color photo pen has low ink";
         break;
      case(WARN_LOW_INK_GREY_PHOTO):
         p = "grey photo pen has low ink";
         break;
      case(WARN_LOW_INK_COLOR_GREY):
         p = "grey pen has low ink";
         break;
      case(WARN_LOW_INK_COLOR_GREY_PHOTO):
         p = "color grey photo pen has low ink";
         break;
      case(WARN_LOW_INK_COLOR_BLACK_PHOTO):
         p = "color back pen has low ink";
         break;
      case(WARN_LOW_INK_CYAN):
         p = "cyan has low ink";
         break;
      case(WARN_LOW_INK_MAGENTA):
         p = "magenta has low ink";
         break;
      case(WARN_LOW_INK_YELLOW):
         p = "yellow has low ink";
         break;
      case(WARN_LOW_INK_MULTIPLE_PENS):
         p = "more that one ink is low";
         break;
      case(WARN_FULL_BLEED_UNSUPPORTED):
         p = "fullbleed is not supported";
         break;
      case(WARN_FULL_BLEED_3SIDES):
         p = "fullbleed is 3 sides";
         break;
      case(WARN_FULL_BLEED_PHOTOPAPER_ONLY):
         p = "fullbleed photo paper only";
         break;
      case(WARN_FULL_BLEED_3SIDES_PHOTOPAPER_ONLY):
         p = "fullbleed 3 sides photo paper only";
         break;
      case(WARN_ILLEGAL_PAPERSIZE):
         p = "illegal paper size";
         break;
      case(WARN_INVALID_MEDIA_SOURCE):
         p = "invalid media source";
         break;
      default:
         p = "driver error";
         BUG("driver error=%d\n", err);
         break;
   }
   return p;
}

int UXServices::MapPaperSize (float width, float height)
{
    int    i, r, size;
    float  dx, dy;

    /* Map gs paper sizes to APDK paper sizes, or do custom. */
    size = CUSTOM_SIZE;
    for (i=0; i<MAX_PAPER_SIZE; i++)
    {
        r = pPC->SetPaperSize ((PAPER_SIZE)i);

        if (r != NO_ERROR)
            continue;

        dx = width  > pPC->PhysicalPageSizeX () ? width  - pPC->PhysicalPageSizeX () : pPC->PhysicalPageSizeX () - width;
        dy = height > pPC->PhysicalPageSizeY () ? height - pPC->PhysicalPageSizeY () : pPC->PhysicalPageSizeY () - height;

        if ((dx < 0.05) && (dy < 0.05))
        {
            size = i;   /* found standard paper size */
            break;
        }
    }

    if (size == CUSTOM_SIZE)
        pPC->SetCustomSize (width, height);

    if ((r = pPC->SetPaperSize ((PAPER_SIZE)size, FullBleed)) != NO_ERROR)
    {
        if (r > 0)
        {
            BUG("unable to set paper size=%d, err=%d, width=%0.5g, height=%0.5g\n", size, r, width, height);
        }
        else 
        {
            BUG("warning setting paper size=%d, err=%d, width=%0.5g, height=%0.5g\n", size, r, width, height);
        }
/*
 *      Call failed, reset our PaperWidth and PaperHeight values.
 *      This ensures that we return correct values when gs queries for printable area.
 */

        PaperWidth  = pPC->PhysicalPageSizeX ();
        PaperHeight = pPC->PhysicalPageSizeY ();
        return -1;
    }

    PaperWidth  = pPC->PhysicalPageSizeX ();
    PaperHeight = pPC->PhysicalPageSizeY ();

    return 0; 
}

void UXServices::ResetIOMode (BOOL bDevID, BOOL bStatus)
{
    if (pPC)
    {
        IOMode.bDevID  = bDevID;
        IOMode.bStatus = bStatus;
        pPC->ResetIOMode (bDevID, bStatus);
    }
}

void UXServices::InitSpeedMechBuffer ()
{
    if (m_pbyPclBuffer)
    {
        return;
    }
    m_pbyPclBuffer = new BYTE[m_iPclBufferSize + 2];
    if (m_pbyPclBuffer)
    {
        iSendBufferSize = 0;
    }
}

int UXServices::SendPreviousPage ()
{
    DRIVER_ERROR    err;
    if (m_bSpeedMechEnabled == FALSE)
    {
        return 0;
    }
    m_iPageCount++;
    if (m_iPageCount == 1)
    {
        return 0;
    }
    m_bSpeedMechEnabled = FALSE;
    err = ToDevice (m_pbyPclBuffer, (DWORD *) &m_iCurPclBufferPos);
    if (err != NO_ERROR)
    {
        return 1;
    }
    m_bSpeedMechEnabled = TRUE;
    m_iCurPclBufferPos = 0;

//  Request the printer to inject speed mech command. Also, let it know this is not the last page

    pPC->SetPrinterHint (SPEED_MECH_HINT, 0);
    return 0;
}

int UXServices::CopyData (const BYTE *pBuffer, DWORD iCount)
{
    if (m_iCurPclBufferPos + (int) iCount < m_iPclBufferSize)
    {
        memcpy (m_pbyPclBuffer + m_iCurPclBufferPos, pBuffer, iCount);
        m_iCurPclBufferPos += iCount;
       return 0;
    }
    BYTE    *p = new BYTE[m_iPclBufferSize + BUFFER_CHUNK_SIZE + 2];
    if (p == NULL)
    {
        m_bSpeedMechEnabled = FALSE;
	return 1;
    }
    memcpy (p, m_pbyPclBuffer, m_iCurPclBufferPos);
    delete [] m_pbyPclBuffer;
    m_pbyPclBuffer = p;
    memcpy (m_pbyPclBuffer + m_iCurPclBufferPos, pBuffer, iCount);
    m_iCurPclBufferPos += iCount;
    m_iPclBufferSize += BUFFER_CHUNK_SIZE;
    return 0;
}

//  Note that this is good only for VIP printers
const char *pbySpeedMechCmd = "\x1B*o5W\x0D\x02\x00";

void UXServices::SendLastPage ()
{
    if (m_pbyPclBuffer == NULL)
    {
        return;
    }
    // Look for speed mech command in the buffer, set the page count and last page flag
    int    i = 0;
    BYTE   *p = m_pbyPclBuffer;
    while (i < m_iPclBufferSize)
    {
        if (*p == '\x1B')
	{
	    if (!(memcmp (p, pbySpeedMechCmd, 8)))
	    {
	        p += 8;
		*p++ = (BYTE) ((m_iPageCount & 0xFF00) >> 8);
		*p++ = (BYTE) ((m_iPageCount & 0x00FF));
		*(p + 9) = 1;
		break;
	    }
	}
        i++;
	p++;
    }
    m_bSpeedMechEnabled = FALSE;
    ToDevice (m_pbyPclBuffer, (DWORD *) &m_iCurPclBufferPos);
    delete [] m_pbyPclBuffer;
}

#if defined(HAVE_LIBHPIP) && defined(HAVE_DBUS) 
void SendDbusMessage (const char *dev, const char *printer, int code, 
                      const char *username, const int jobid, const char *title)
{
    DBusMessage * msg = NULL;

    InitDbus ();
    if (dbus_conn == NULL)
        return;
    msg = dbus_message_new_signal(DBUS_PATH, DBUS_INTERFACE, "Event");

    if (NULL == msg)
    {
        BUG("dbus message is NULL!\n");
        return;
    }

    dbus_message_append_args(msg, 
        DBUS_TYPE_STRING, &dev,
        DBUS_TYPE_STRING, &printer,
        DBUS_TYPE_UINT32, &code, 
        DBUS_TYPE_STRING, &username, 
        DBUS_TYPE_UINT32, &jobid,
        DBUS_TYPE_STRING, &title, 
        DBUS_TYPE_INVALID);

    if (!dbus_connection_send(dbus_conn, msg, NULL))
    {
        BUG("dbus message send failed!\n");
        return;
    }

    dbus_connection_flush(dbus_conn);
    dbus_message_unref(msg);

    return;
}

void InitDbus (void)
{
   dbus_error_init (&dbus_err);
   dbus_conn = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_err);
    
   if (dbus_error_is_set (&dbus_err))
   { 
      BUG ("dBus Connection Error (%s)!\n", dbus_err.message); 
      dbus_error_free (&dbus_err); 
   }

   return;
}
#endif  /* HAVE_DBUS */