Example Code

Here is the complete code for the two examples used in the guide. I normally write the C code in comments before the Pyrex code so I can check I have all the arguments correct, hence the large comments before each declaration.

C
U
P
S

The Entire pyxcups.pyx Code

Here, for your editing pleasure is the entire wrapper for the CUPS cupsGetDests function call.

# typedef struct                 /**** Printer Options ****/
# {
#   char          *name;         /* Name of option */
#   char          *value;        /* Value of option */
# } cups_option_t;
cdef struct cups_option_t:
    char *name
    char *value

# typedef struct               /**** Destination ****/
# {
#   char          *name,       /* Printer or class name */
#                 *instance;   /* Local instance name or */
#                              /*   NULL */
#   int           is_default;  /* Is this printer the */
#                              /*   default? */
#   int           num_options; /* Number of options */
#   cups_option_t *options;    /* Options */
# } cups_dest_t;
cdef struct cups_dest_t:
    char          *name
    char          *instance
    int           is_default
    int           num_options
    cups_option_t *options

# extern int              cupsGetDests(cups_dest_t **dests);
cdef extern int cupsGetDests(cups_dest_t **dests)

def get_dests():
    cdef cups_dest_t dests
    cdef cups_dest_t *currDest

    numDests = cupsGetDests(&dests)

    retval = []
    for i in range(numDests):
        currDest = dests[i]
        retval.append(currDest.name)

    return retval

Example Python Code for pyxcups.pyx

import pyxcups
for printer in pyxcups.get_dests():
    print printer
      
X
O
S
D

The Entire pyxxosd.pyx Code

# typedef enum
#    {
#    XOSD_top,
#    XOSD_bottom
#    } xosd_pos;
cdef enum xosd_pos:
    XOSD_top
    XOSD_bottom

# typedef enum
#    {
#    XOSD_percentage,
#    XOSD_string,
#    XOSD_printf,
#    XOSD_slider
#    } xosd_command;
cdef enum xosd_command:
   XOSD_percentage
   XOSD_string
   XOSD_printf
   XOSD_slider

# typedef struct xosd xosd;
cdef extern struct xosd

# xosd *xosd_init (char *font, char *colour, int timeout,
#                  xosd_pos pos, int offset,
#                  int shadow_offset);
cdef extern xosd *xosd_init(char *font, char *colour,
                 int timeout, xosd_pos pos, int offset, 
                 int shadow_offset)

# int xosd_uninit (xosd *osd);
cdef int xosd_uninit (xosd *osd)

# int xosd_display (xosd *osd, int line, 
#                   xosd_command command, ...);
cdef extern int xosd_display(xosd *osd, int line,
                             xosd_command command, 
                             ...)

cdef class XOSD:
    cdef xosd *xosdWin

    def __new__(self, font, colour, timeout, pos):
        self.xosdWin = xosd_init(font, colour, timeout, pos,
                                 0, 1)

    def __dealloc__(self):
        xosd_uninit(self.xosdWin)

    def display_text(self, text):
        cdef char *cText
        cText = text
        xosd_display(self.xosdWin, 1, XOSD_string, cText)

Example Python Code for pyxosd.pyx

import pyxosd, time

font = "-adobe-helvetica-medium-r-normal-*-*-400-*-*-p-*-iso8859-1"
x = pyxosd.XOSD(font, "LawnGreen", 3, 1)

x.display_text('You have been R00ted')
time.sleep(3)

x.display_text('by an 31373 HaX0r.')
time.sleep(5)
Previous Contents Next

Michael JasonSmith
Last modified: Mon Jun 24 11:56:16 NZST 2002